2021-01-24 19:54:44 +08:00
|
|
|
/* global KEEP */
|
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
KEEP.initModeToggle = () => {
|
2020-11-20 12:02:22 +08:00
|
|
|
KEEP.utils.modeToggle = {
|
2020-11-25 16:54:44 +08:00
|
|
|
modeToggleButton_dom: document.querySelector('.tool-dark-light-toggle'),
|
|
|
|
iconDom: document.querySelector('.tool-dark-light-toggle i'),
|
2020-10-21 19:36:38 +08:00
|
|
|
|
2021-01-23 22:31:20 +08:00
|
|
|
enableLightMode() {
|
2022-09-30 11:40:30 +08:00
|
|
|
document.body.classList.remove('dark-mode')
|
|
|
|
document.body.classList.add('light-mode')
|
|
|
|
this.iconDom.className = 'fas fa-moon'
|
|
|
|
KEEP.styleStatus.isDark = false
|
|
|
|
KEEP.setStyleStatus()
|
2021-01-23 22:31:20 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
enableDarkMode() {
|
2022-09-30 11:40:30 +08:00
|
|
|
document.body.classList.add('dark-mode')
|
|
|
|
document.body.classList.remove('light-mode')
|
|
|
|
this.iconDom.className = 'fas fa-sun'
|
|
|
|
KEEP.styleStatus.isDark = true
|
|
|
|
KEEP.setStyleStatus()
|
2021-01-24 19:54:44 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
isDarkPrefersColorScheme() {
|
2022-09-30 11:40:30 +08:00
|
|
|
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)')
|
2020-10-21 19:36:38 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
initModeStatus() {
|
2022-09-30 11:40:30 +08:00
|
|
|
const styleStatus = KEEP.getStyleStatus()
|
2021-01-24 19:54:44 +08:00
|
|
|
|
|
|
|
if (styleStatus) {
|
2022-09-30 11:40:30 +08:00
|
|
|
styleStatus.isDark ? this.enableDarkMode() : this.enableLightMode()
|
2021-01-23 22:31:20 +08:00
|
|
|
} else {
|
2022-09-30 11:40:30 +08:00
|
|
|
this.isDarkPrefersColorScheme().matches ? this.enableDarkMode() : this.enableLightMode()
|
2020-10-21 19:36:38 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
initModeToggleButton() {
|
2020-12-30 18:11:46 +08:00
|
|
|
this.modeToggleButton_dom.addEventListener('click', () => {
|
2022-09-30 11:40:30 +08:00
|
|
|
const isDark = document.body.classList.contains('dark-mode')
|
|
|
|
isDark ? this.enableLightMode() : this.enableDarkMode()
|
|
|
|
})
|
2021-01-23 22:31:20 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
initModeAutoTrigger() {
|
2022-09-30 11:40:30 +08:00
|
|
|
const isDarkMode = this.isDarkPrefersColorScheme()
|
|
|
|
isDarkMode.addEventListener('change', (e) => {
|
|
|
|
e.matches ? this.enableDarkMode() : this.enableLightMode()
|
|
|
|
})
|
2020-10-21 19:36:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 11:40:30 +08:00
|
|
|
KEEP.utils.modeToggle.initModeStatus()
|
|
|
|
KEEP.utils.modeToggle.initModeToggleButton()
|
|
|
|
KEEP.utils.modeToggle.initModeAutoTrigger()
|
|
|
|
}
|