diff options
author | jeffreytse <jeffreytse.mail@gmail.com> | 2020-10-10 14:13:34 +0800 |
---|---|---|
committer | jeffreytse <jeffreytse.mail@gmail.com> | 2020-10-10 14:13:34 +0800 |
commit | c8e4149792530a5de2fb6a26b01a5c14317d7769 (patch) | |
tree | c514f5999429b24b1500eeec56f694ca519f9039 /_includes/extensions | |
parent | a10b6a50cea820aaf6d7f94d971bf38dd281df0e (diff) |
feat: add config option for night mode
Diffstat (limited to '_includes/extensions')
-rw-r--r-- | _includes/extensions/theme-toggle.html | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/_includes/extensions/theme-toggle.html b/_includes/extensions/theme-toggle.html index 7976e21..d426f65 100644 --- a/_includes/extensions/theme-toggle.html +++ b/_includes/extensions/theme-toggle.html @@ -9,6 +9,10 @@ </label> </div> +{%- assign name = 'night_mode' -%} +{%- include functions.html func='get_value' default='auto' -%} +{%- assign night_mode = return -%} + <script> (function() { var sw = document.getElementById('theme-switch'); @@ -24,7 +28,7 @@ try { data = JSON.parse(data ? data : ''); } catch(e) { - data = { nightShift: null, autoToggleAt: 0 }; + data = { nightShift: false, autoToggleAt: 0 }; saveThemeData(data); } return data; @@ -72,19 +76,28 @@ }; } - var data = autoThemeToggle(); - // Listen the theme toggle event sw.addEventListener('change', function(event) { handleThemeToggle(event.target.checked); }); - // Toggle theme by local setting - if (data.toggleAt > themeData.autoToggleAt) { - themeData.autoToggleAt = data.toggleAt; - handleThemeToggle(data.nightShift); - } else { + var nightModeOption = '{{ night_mode }}'; + nightModeOption = nightModeOption.toLowerCase(); + + if (nightModeOption == 'auto') { + var data = autoThemeToggle(); + + // Toggle theme by local setting + if (data.toggleAt > themeData.autoToggleAt) { + themeData.autoToggleAt = data.toggleAt; + handleThemeToggle(data.nightShift); + } else { + handleThemeToggle(themeData.nightShift); + } + } else if (nightModeOption == 'mannual') { handleThemeToggle(themeData.nightShift); + } else { + handleThemeToggle(nightModeOption == 'on'); } })(); </script> |