diff options
| author | Reorx <novoreorx@gmail.com> | 2022-06-01 17:14:46 +0800 |
|---|---|---|
| committer | Reorx <novoreorx@gmail.com> | 2022-06-01 17:15:04 +0800 |
| commit | 07c4add7bed65cb14f70aed58e7f71133df32814 (patch) | |
| tree | a8901cd6caf1e898a4e61a0549dae84b843a1a1c /layouts/partials/remark42.html | |
| parent | 0aa7a35ee0535a0ba5bdde7ed1a6c4fa12b3008d (diff) | |
refactor toggleThemeCallbacks
- use object instead of array to ensure uniqueness for each callback
- do not use <script data-no-instant> to add callback, they won't be called if the page is not loaded first time;
instead, try to set the callback each time the page is loaded
Diffstat (limited to 'layouts/partials/remark42.html')
| -rw-r--r-- | layouts/partials/remark42.html | 42 |
1 files changed, 17 insertions, 25 deletions
diff --git a/layouts/partials/remark42.html b/layouts/partials/remark42.html index 393128e..a2d8fd9 100644 --- a/layouts/partials/remark42.html +++ b/layouts/partials/remark42.html @@ -16,8 +16,24 @@ } (function() { - const remark42 = window.REMARK42 + // toogle theme callback + const key = 'remark42' + if (!toggleThemeCallbacks.hasOwnProperty(key)) { + toggleThemeCallbacks[key] = (isDark) => { + const remark42 = window.REMARK42 + if (!remark42 || !document.querySelector('#remark42')) { + return; + } + if (isDark) { + remark42.changeTheme('light'); + } else { + remark42.changeTheme('dark'); + } + } + } + // init or reset remark42 + const remark42 = window.REMARK42 if (remark42) { remark42.destroy() remark42.createInstance(remark_config) @@ -34,27 +50,3 @@ } })(); </script> - -<script data-no-instant> - (function() { - if (!toggleThemeCallbacks) { - return; - } - - toggleThemeCallbacks.push((isDark) => { - const remark42 = window.REMARK42 - if (!remark42) { - return; - } - if (!document.getElementById('remark42')) { - return; - } - - if (isDark) { - remark42.changeTheme('light'); - } else { - remark42.changeTheme('dark'); - } - }); - })(); -</script> |
