forked from xaoxuu/hexo-theme-stellar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] support run-time theme switch, and visitor preferred theme (xa…
- Loading branch information
Showing
16 changed files
with
139 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -272,6 +272,9 @@ footer: | |
# comments: | ||
# icon: '<img src="https://gcore.jsdelivr.net/gh/cdn-x/[email protected]/social/942ebbf1a4b91.svg"/>' | ||
# url: /about/#comments | ||
# theme: | ||
# icon: default:theme | ||
# onclick: 'switchTheme()' | ||
sitemap: | ||
# '博客': | ||
# - '[近期](/)' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<script type="text/javascript"> | ||
const applyTheme = (theme) => { | ||
if (theme === 'auto') { | ||
document.documentElement.removeAttribute('data-theme') | ||
} else { | ||
document.documentElement.setAttribute('data-theme', theme) | ||
} | ||
applyThemeToGiscus(theme) | ||
} | ||
const applyThemeToGiscus = (theme) => { | ||
theme = theme === 'auto' ? 'preferred_color_scheme' : theme | ||
const cmt = document.getElementById('giscus') | ||
if (cmt) { | ||
// This works before giscus load. | ||
cmt.setAttribute('data-theme', theme) | ||
} | ||
const iframe = document.querySelector('#comments > section.giscus > iframe') | ||
if (iframe) { | ||
// This works after giscus loaded. | ||
const src = iframe.src | ||
const newSrc = src.replace(/theme=[\w]+/, `theme=${theme}`) | ||
iframe.src = newSrc | ||
} | ||
} | ||
const switchTheme = () => { | ||
// light -> dark -> auto -> light -> ... | ||
const currentTheme = document.documentElement.getAttribute('data-theme') | ||
let newTheme; | ||
switch (currentTheme) { | ||
case 'light': | ||
newTheme = 'dark' | ||
break | ||
case 'dark': | ||
newTheme = 'auto' | ||
break | ||
default: | ||
newTheme = 'light' | ||
} | ||
applyTheme(newTheme) | ||
window.localStorage.setItem('Stellar.theme', newTheme) | ||
const messages = { | ||
light: `<%- __('message.theme_switched.light') %>`, | ||
dark: `<%- __('message.theme_switched.dark') %>`, | ||
auto: `<%- __('message.theme_switched.auto') %>`, | ||
} | ||
hud?.toast?.(messages[newTheme]) | ||
} | ||
(() => { | ||
// Apply user's preferred theme, if any. | ||
const theme = window.localStorage.getItem('Stellar.theme') | ||
if (theme !== null) { | ||
applyTheme(theme) | ||
} | ||
})() | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters