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-10-21 19:36:38 +08:00
|
|
|
|
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() {
|
|
|
|
document.body.classList.remove('dark-mode');
|
2021-01-24 20:04:04 +08:00
|
|
|
document.body.classList.add('light-mode');
|
2021-01-23 22:31:20 +08:00
|
|
|
this.iconDom.className = 'fas fa-moon';
|
2021-01-24 19:54:44 +08:00
|
|
|
KEEP.styleStatus.isDark = false;
|
|
|
|
KEEP.setStyleStatus();
|
2021-01-23 22:31:20 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
enableDarkMode() {
|
|
|
|
document.body.classList.add('dark-mode');
|
2021-01-24 20:04:04 +08:00
|
|
|
document.body.classList.remove('light-mode');
|
2021-01-23 22:31:20 +08:00
|
|
|
this.iconDom.className = 'fas fa-sun';
|
2021-01-24 19:54:44 +08:00
|
|
|
KEEP.styleStatus.isDark = true;
|
|
|
|
KEEP.setStyleStatus();
|
|
|
|
},
|
|
|
|
|
|
|
|
isDarkPrefersColorScheme() {
|
|
|
|
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)');
|
2020-10-21 19:36:38 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
initModeStatus() {
|
2021-01-24 19:54:44 +08:00
|
|
|
const styleStatus = KEEP.getStyleStatus();
|
|
|
|
|
|
|
|
if (styleStatus) {
|
|
|
|
styleStatus.isDark ? this.enableDarkMode() : this.enableLightMode();
|
2021-01-23 22:31:20 +08:00
|
|
|
} else {
|
2021-01-24 19:54:44 +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', () => {
|
2021-01-24 19:54:44 +08:00
|
|
|
const isDark = document.body.classList.contains('dark-mode');
|
|
|
|
isDark ? this.enableLightMode() : this.enableDarkMode();
|
2020-10-21 19:36:38 +08:00
|
|
|
});
|
2021-01-23 22:31:20 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
initModeAutoTrigger() {
|
2021-01-24 19:54:44 +08:00
|
|
|
const isDarkMode = this.isDarkPrefersColorScheme();
|
|
|
|
isDarkMode.addEventListener('change', e => {
|
|
|
|
e.matches ? this.enableDarkMode() : this.enableLightMode();
|
2020-10-21 19:36:38 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 12:02:22 +08:00
|
|
|
KEEP.utils.modeToggle.initModeStatus();
|
|
|
|
KEEP.utils.modeToggle.initModeToggleButton();
|
2021-01-23 22:31:20 +08:00
|
|
|
KEEP.utils.modeToggle.initModeAutoTrigger();
|
2020-12-30 18:11:46 +08:00
|
|
|
};
|