From f5f604094e1fc8aebf23ab47d02e67228942bb00 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 16 Mar 2022 12:19:51 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=B8=BB=E9=A2=98=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/global.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/global.js b/lib/global.js index 0432b5e0..c9564cce 100644 --- a/lib/global.js +++ b/lib/global.js @@ -17,9 +17,7 @@ export function GlobalContextProvider ({ children }) { const [locale, updateLocale] = useState(generateLocaleDict('en-US')) const [isDarkMode, updateDarkMode] = useState(false) const [onLoading, changeLoadingState] = useState(false) - // 会优先读取浏览器中的主题参数 - const [theme, setTheme] = useState(Router?.router?.query?.theme || BLOG.THEME) - + const [theme, setTheme] = useState(BLOG.THEME) Router.events.on('routeChangeStart', (...args) => { changeLoadingState(true) }) @@ -30,6 +28,11 @@ export function GlobalContextProvider ({ children }) { useEffect(() => { if (!hasInit) { + const userTheme = getQueryVariable('theme') + console.log('用户主题', userTheme) + if (userTheme && ALL_THEME.indexOf(userTheme) > -1) { + setTheme(userTheme) + } initLocale(locale, updateLocale) initDarkMode(isDarkMode, updateDarkMode) hasInit = true @@ -43,6 +46,16 @@ export function GlobalContextProvider ({ children }) { ) } +function getQueryVariable (variable) { + const query = window.location.search.substring(1) + const vars = query.split('&') + for (let i = 0; i < vars.length; i++) { + const pair = vars[i].split('=') + if (pair[0] == variable) { return pair[1] } + } + return (false) +} + /** * 深度合并两个对象 * @param target From 3d86afe001dd092c7a9eeff73bc63764be9008f9 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 16 Mar 2022 12:20:00 +0800 Subject: [PATCH 2/3] =?UTF-8?q?medium=E5=AF=BC=E8=88=AA=E6=A0=8F=E5=BE=AE?= =?UTF-8?q?=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/medium/components/LogoBar.js | 2 +- themes/medium/components/TopNavBar.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/themes/medium/components/LogoBar.js b/themes/medium/components/LogoBar.js index 9bb3aa30..c2719a76 100644 --- a/themes/medium/components/LogoBar.js +++ b/themes/medium/components/LogoBar.js @@ -4,7 +4,7 @@ import Link from 'next/link' export default function LogoBar () { return } diff --git a/themes/medium/components/TopNavBar.js b/themes/medium/components/TopNavBar.js index 1d31d251..69dfb3ce 100644 --- a/themes/medium/components/TopNavBar.js +++ b/themes/medium/components/TopNavBar.js @@ -21,7 +21,7 @@ export default function TopNavBar (props) { if (link.show) { const selected = (router.pathname === link.to) || (router.asPath === link.to) return -
From ddcf06b0da3b41cea6cd590a4077eb213f5b3466 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 16 Mar 2022 12:20:51 +0800 Subject: [PATCH 3/3] build --- lib/global.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/global.js b/lib/global.js index c9564cce..59b9e506 100644 --- a/lib/global.js +++ b/lib/global.js @@ -46,12 +46,17 @@ export function GlobalContextProvider ({ children }) { ) } +/** + * 查询url中的query参数 + * @param {}} variable + * @returns + */ function getQueryVariable (variable) { const query = window.location.search.substring(1) const vars = query.split('&') for (let i = 0; i < vars.length; i++) { const pair = vars[i].split('=') - if (pair[0] == variable) { return pair[1] } + if (pair[0] === variable) { return pair[1] } } return (false) }