diff options
Diffstat (limited to 'layouts/partials/scripts-end.html')
-rw-r--r-- | layouts/partials/scripts-end.html | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/layouts/partials/scripts-end.html b/layouts/partials/scripts-end.html new file mode 100644 index 0000000..d82ab58 --- /dev/null +++ b/layouts/partials/scripts-end.html @@ -0,0 +1,146 @@ +<script> + // 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 fujiTheme, mediumEvent + function updateMeidumTheme(fujiTheme, mediumEvent) { + if (fujiTheme && mediumEvent) { + mediumEvent.detach(); + let mediumTheme; + if (fujiTheme === 'dark') { + mediumTheme = '#2f3136'; + } else { + mediumTheme = '#fffffd'; + } + mediumEvent = mediumZoom('.img-zoomable', { + background: mediumTheme + }); + } + } + // update utterances theme + // @params fujiTheme, utterancesFrame + function updateUtterancesTheme(fujiTheme, utterancesFrame) { + if (fujiTheme && utterancesFrame) { + let ifMessage; + if (fujiTheme === 'dark') { + ifMessage = { + type: 'set-theme', + theme: 'photon-dark' + }; + } else { + ifMessage = { + type: 'set-theme', + theme: 'github-light' + }; + } + utterancesFrame.contentWindow.postMessage(ifMessage, 'https://utteranc.es'); + } + } +</script> + +<script> + // toc anchors & to-top button + $('.btn .btn-scroll-top').on('click', () => { + $('html, body').animate({ + scrollTop: 0, + }); + }); + $('.sidebar-toc a').on('click', (event) => { + $('html, body').animate({ + scrollTop: $($(event.currentTarget).attr('href')).offset().top + }); + }); + + // init medium-zoom + var mediumTheme; + if ($('body').attr('data-theme') === 'dark') { + mediumTheme = '#2f3136'; + } else { + mediumTheme = '#fffffd'; + } + var mediumEvent = mediumZoom('.img-zoomable', { + background: mediumTheme + }); + + // init highlight.js + hljs.initHighlightingOnLoad(); + + // 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(); + } +</script> + +{{ if and (and (ne .Params.noComments true) (.IsPage)) (.Site.Params.utterancesRepo) }} +<script> + // utterances comment loading indicator + 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 p').text('{{ i18n "postCommentLoadingFailed" }}'); + $('.post-loading i').attr('class', 'far fa-times-circle'); + } + } + } + + // theme switch button + $('.btn .btn-toggle-mode').on('click', () => { + // toggle theme + toggleTheme(); + // update medium background + updateMeidumTheme($('body').attr('data-theme'), mediumEvent); + // update utterances theme if available + if (commentStatus) { + updateUtterancesTheme($('body').attr('data-theme'), $('.post-comment iframe')[0]); + } + }); +</script> +{{ else }} +<script> + // theme switch button + $('.btn .btn-toggle-mode').on('click', () => { + // toggle theme + toggleTheme(); + // update medium background + updateMeidumTheme($('body').attr('data-theme'), mediumEvent); + }); +</script> +{{ end }} + +{{ with .Params.playerURL }} +<script> + // in-post aplayer + var aPlayer = new APlayer({ + container: document.getElementById('post-aplayer'), + theme: '#8f82bc', + volume: 0.1, + audio: [{ + name: '{{ $.Params.playerName }}', + artist: '{{ $.Params.playerArtist }}', + url: '{{ . }}', + cover: '{{ $.Params.playerCover }}' + }] + }); +</script> +{{ end }}
\ No newline at end of file |