diff options
author | amzrk2 | 2020-05-31 16:31:27 +0800 |
---|---|---|
committer | amzrk2 | 2020-05-31 16:31:27 +0800 |
commit | 8009dfd8f5d33211a07d01319715fc3993979138 (patch) | |
tree | 5effe87db759b4bd2d62e0abeb170c2eca900428 /static/assets/js/fuji.dev.js | |
parent | bde0e702941721e0f408390478a1f497cf8a7a31 (diff) | |
download | hugo-theme-fuji-8009dfd8f5d33211a07d01319715fc3993979138.tar.gz hugo-theme-fuji-8009dfd8f5d33211a07d01319715fc3993979138.tar.bz2 hugo-theme-fuji-8009dfd8f5d33211a07d01319715fc3993979138.zip |
Scripts adjustment
Diffstat (limited to 'static/assets/js/fuji.dev.js')
-rw-r--r-- | static/assets/js/fuji.dev.js | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/static/assets/js/fuji.dev.js b/static/assets/js/fuji.dev.js new file mode 100644 index 0000000..d6917a3 --- /dev/null +++ b/static/assets/js/fuji.dev.js @@ -0,0 +1,130 @@ +'use strict'; + +// toggle theme +function toggleTheme() { + $('body').attr('data-theme', (index, attr) => { + if (attr === 'light') { + localStorage.setItem('fuji_theme', 'dark'); + return 'dark'; + } else { + localStorage.setItem('fuji_theme', 'light'); + return 'light'; + } + }); +} + +// update medium-zoom theme +// @params targetTheme, mediumInst +function updateMeidumTheme(targetTheme, mediumInst) { + if (targetTheme && mediumInst) { + mediumInst.detach(); + if (targetTheme === 'dark') { + mediumInst = mediumZoom('.img-zoomable', { + background: '#2f3136', + }); + } else { + mediumInst = mediumZoom('.img-zoomable', { + background: '#fffffd', + }); + } + } +} + +// update utterances theme +// @params targetTheme, utterancesFrame +function updateUtterancesTheme(targetTheme, utterancesFrame) { + if (targetTheme && utterancesFrame) { + if (targetTheme === 'dark') { + utterancesFrame.contentWindow.postMessage( + { + type: 'set-theme', + theme: 'photon-dark', + }, + 'https://utteranc.es' + ); + } else { + utterancesFrame.contentWindow.postMessage( + { + type: 'set-theme', + theme: 'github-light', + }, + 'https://utteranc.es' + ); + } + } +} + +// to-top button +$('.btn .btn-scroll-top').on('click', () => { + $('html, body').animate({ + scrollTop: 0, + }); +}); + +// toc anchors +$('.sidebar-toc a').on('click', (event) => { + $('html, body').animate({ + scrollTop: $($(event.currentTarget).attr('href')).offset().top, + }); +}); + +// remove empty ul in toc if article only have ## and ### +if ($('.sidebar-toc ul ul').length > 0 && $('.sidebar-toc ul ul li').text() === '') { + $('.sidebar-toc ul ul').hide(); +} + +// init medium-zoom +var mediumInst; // medium-zoom instance +if ($('body').attr('data-theme') === 'dark') { + mediumInst = mediumZoom('.img-zoomable', { + background: '#2f3136', + }); +} else { + mediumInst = mediumZoom('.img-zoomable', { + background: '#fffffd', + }); +} + +// init highlight.js +hljs.initHighlighting(); + +// if in post page and using utterances +// add utterances comment loading indicator +if ($('.post-loading').length >= 1) { + var commentStatus; // utterence status + var commentLoadingTime = 0; // loading time passed + var commentCheckInterval = self.setInterval(checkUtterances, 500); + + function checkUtterances() { + commentStatus = $('.post-comment .utterances').attr('style'); + if (commentStatus) { + clearInterval(commentCheckInterval); + updateUtterancesTheme($('body').attr('data-theme'), $('.post-comment iframe')[0]); + $('.post-loading').hide(); + } else { + if (++commentLoadingTime > 20) { + clearInterval(commentCheckInterval); + $('.post-comment').hide(); + $('.post-loading i').attr('class', 'far fa-times-circle'); + } + } + } +} + +// init theme switch button +$('.btn .btn-toggle-mode').on('click', () => { + // toggle theme + toggleTheme(); + // update medium background + updateMeidumTheme($('body').attr('data-theme'), mediumInst); + // switch comment area theme + // only works after comment area are initialized + if ($('.post-loading').length >= 1 && commentStatus) { + updateUtterancesTheme($('body').attr('data-theme'), $('.post-comment iframe')[0]); + } + if ($('#disqus_thread').length >= 1 && typeof DISQUS !== 'undefined') { + DISQUS.reset({ + reload: true, + }); + } +}); |