diff --git a/layout/_third-party/fancybox.njk b/layout/_third-party/fancybox.njk new file mode 100644 index 0000000..a42c374 --- /dev/null +++ b/layout/_third-party/fancybox.njk @@ -0,0 +1,3 @@ +{%- if theme.fancybox %} + {{ next_js('third-party/fancybox.js') }} +{%- endif %} diff --git a/layout/_third-party/index.njk b/layout/_third-party/index.njk index ee6f5a2..55630c3 100644 --- a/layout/_third-party/index.njk +++ b/layout/_third-party/index.njk @@ -17,4 +17,6 @@ {%- include 'tags/pdf.njk' -%} {%- include 'tags/mermaid.njk' -%} +{%- include 'fancybox.njk' -%} + {%- include 'nprogress.njk' -%} diff --git a/scripts/filters/minify.js b/scripts/filters/minify.js index 761a51b..75f3142 100644 --- a/scripts/filters/minify.js +++ b/scripts/filters/minify.js @@ -133,6 +133,10 @@ hexo.extend.filter.register('after_generate', () => { } // Others + if (!theme.fancybox) { + hexo.route.remove('js/third-party/fancybox.js'); + } + if (!theme.nprogress.enable) { hexo.route.remove('js/third-party/nprogress.js'); } diff --git a/scripts/helpers/next-config.js b/scripts/helpers/next-config.js index 1efa610..b7603da 100644 --- a/scripts/helpers/next-config.js +++ b/scripts/helpers/next-config.js @@ -19,7 +19,6 @@ hexo.extend.helper.register('next_config', function() { sidebar : theme.sidebar, copycode : theme.codeblock.copy_button.enable, bookmark : theme.bookmark, - fancybox : theme.fancybox, mediumzoom: theme.mediumzoom, lazyload : theme.lazyload, pangu : theme.pangu, diff --git a/source/js/next-boot.js b/source/js/next-boot.js index f55ef51..3d58faf 100644 --- a/source/js/next-boot.js +++ b/source/js/next-boot.js @@ -63,7 +63,6 @@ NexT.boot.refresh = function() { * Need to add config option in Front-End at 'scripts/helpers/next-config.js' file. */ CONFIG.prism && window.Prism.highlightAll(); - CONFIG.fancybox && NexT.utils.wrapImageWithFancyBox(); CONFIG.mediumzoom && window.mediumZoom('.post-body :not(a) > img, .post-body > img', { background: 'var(--content-bg-color)' }); diff --git a/source/js/third-party/fancybox.js b/source/js/third-party/fancybox.js new file mode 100644 index 0000000..bb436ab --- /dev/null +++ b/source/js/third-party/fancybox.js @@ -0,0 +1,38 @@ +document.addEventListener('page:loaded', () => { + + /** + * Wrap images with fancybox. + */ + document.querySelectorAll('.post-body :not(a) > img, .post-body > img').forEach(element => { + const $image = $(element); + const imageLink = $image.attr('data-src') || $image.attr('src'); + const $imageWrapLink = $image.wrap(``).parent('a'); + if ($image.is('.post-gallery img')) { + $imageWrapLink.attr('data-fancybox', 'gallery').attr('rel', 'gallery'); + } else if ($image.is('.group-picture img')) { + $imageWrapLink.attr('data-fancybox', 'group').attr('rel', 'group'); + } else { + $imageWrapLink.attr('data-fancybox', 'default').attr('rel', 'default'); + } + + const imageTitle = $image.attr('title') || $image.attr('alt'); + if (imageTitle) { + // Do not append image-caption if pandoc has already created a figcaption + if (!$imageWrapLink.next('figcaption').length) { + $imageWrapLink.append(`

${imageTitle}

`); + } + // Make sure img title tag will show correctly in fancybox + $imageWrapLink.attr('title', imageTitle).attr('data-caption', imageTitle); + } + }); + + $.fancybox.defaults.hash = false; + $('.fancybox').fancybox({ + loop : true, + helpers: { + overlay: { + locked: false + } + } + }); +}); diff --git a/source/js/utils.js b/source/js/utils.js index 63d72f9..05e493d 100644 --- a/source/js/utils.js +++ b/source/js/utils.js @@ -31,44 +31,6 @@ if (typeof DOMTokenList.prototype.replace !== 'function') { NexT.utils = { - /** - * Wrap images with fancybox. - */ - wrapImageWithFancyBox: function() { - document.querySelectorAll('.post-body :not(a) > img, .post-body > img').forEach(element => { - const $image = $(element); - const imageLink = $image.attr('data-src') || $image.attr('src'); - const $imageWrapLink = $image.wrap(``).parent('a'); - if ($image.is('.post-gallery img')) { - $imageWrapLink.attr('data-fancybox', 'gallery').attr('rel', 'gallery'); - } else if ($image.is('.group-picture img')) { - $imageWrapLink.attr('data-fancybox', 'group').attr('rel', 'group'); - } else { - $imageWrapLink.attr('data-fancybox', 'default').attr('rel', 'default'); - } - - const imageTitle = $image.attr('title') || $image.attr('alt'); - if (imageTitle) { - // Do not append image-caption if pandoc has already created a figcaption - if (!$imageWrapLink.next('figcaption').length) { - $imageWrapLink.append(`

${imageTitle}

`); - } - // Make sure img title tag will show correctly in fancybox - $imageWrapLink.attr('title', imageTitle).attr('data-caption', imageTitle); - } - }); - - $.fancybox.defaults.hash = false; - $('.fancybox').fancybox({ - loop : true, - helpers: { - overlay: { - locked: false - } - } - }); - }, - registerExtURL: function() { document.querySelectorAll('span.exturl').forEach(element => { const link = document.createElement('a');