2021-01-23 01:02:56 +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
|
|
|
|
|
|
|
setItemUtil(modeClass, prefersColorScheme) {
|
|
|
|
document.body.classList.toggle(modeClass);
|
|
|
|
const isDark = document.body.className.indexOf(modeClass) === -1;
|
|
|
|
|
|
|
|
if (isDark) {
|
2020-11-24 10:55:24 +08:00
|
|
|
this.iconDom.className = 'fas fa-moon';
|
2020-10-21 19:36:38 +08:00
|
|
|
} else {
|
2020-11-24 10:55:24 +08:00
|
|
|
this.iconDom.className = 'fas fa-sun';
|
2020-10-21 19:36:38 +08:00
|
|
|
}
|
2021-01-23 01:02:56 +08:00
|
|
|
KEEP.styleStatus.isDark = isDark;
|
|
|
|
KEEP.styleStatus.prefersColorScheme = prefersColorScheme;
|
|
|
|
KEEP.setStyleStatus();
|
2020-10-21 19:36:38 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
initModeStatus() {
|
2021-01-23 01:02:56 +08:00
|
|
|
const styleStatus = KEEP.getStyleStatus();
|
|
|
|
if (styleStatus) {
|
|
|
|
if (styleStatus.prefersColorScheme === 'dark') {
|
|
|
|
if (styleStatus.isDark) {
|
2020-10-21 19:36:38 +08:00
|
|
|
document.body.classList.remove('light-mode');
|
2020-11-24 10:55:24 +08:00
|
|
|
this.iconDom.className = 'fas fa-sun';
|
2020-10-21 19:36:38 +08:00
|
|
|
} else {
|
|
|
|
document.body.classList.add('light-mode');
|
2020-11-24 10:55:24 +08:00
|
|
|
this.iconDom.className = 'fas fa-moon';
|
2020-10-21 19:36:38 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2021-01-23 01:02:56 +08:00
|
|
|
if (styleStatus.isDark) {
|
2020-10-21 19:36:38 +08:00
|
|
|
document.body.classList.remove('dark-mode');
|
2020-11-24 10:55:24 +08:00
|
|
|
this.iconDom.className = 'fas fa-moon';
|
2020-10-21 19:36:38 +08:00
|
|
|
} else {
|
|
|
|
document.body.classList.add('dark-mode');
|
2020-11-24 10:55:24 +08:00
|
|
|
this.iconDom.className = 'fas fa-sun';
|
2020-10-21 19:36:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
initModeToggleButton() {
|
2020-12-30 18:11:46 +08:00
|
|
|
this.modeToggleButton_dom.addEventListener('click', () => {
|
2020-10-21 19:36:38 +08:00
|
|
|
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
|
|
|
this.setItemUtil('light-mode', 'dark');
|
|
|
|
} else {
|
|
|
|
this.setItemUtil('dark-mode', 'light');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 12:02:22 +08:00
|
|
|
KEEP.utils.modeToggle.initModeStatus();
|
|
|
|
KEEP.utils.modeToggle.initModeToggleButton();
|
2020-12-30 18:11:46 +08:00
|
|
|
|
|
|
|
};
|