Separate fancybox scripts

This commit is contained in:
Mimi 2021-08-30 00:58:06 +08:00
parent c377515fd7
commit 2eaf869e89
7 changed files with 47 additions and 40 deletions

3
layout/_third-party/fancybox.njk vendored Normal file
View File

@ -0,0 +1,3 @@
{%- if theme.fancybox %}
{{ next_js('third-party/fancybox.js') }}
{%- endif %}

View File

@ -17,4 +17,6 @@
{%- include 'tags/pdf.njk' -%} {%- include 'tags/pdf.njk' -%}
{%- include 'tags/mermaid.njk' -%} {%- include 'tags/mermaid.njk' -%}
{%- include 'fancybox.njk' -%}
{%- include 'nprogress.njk' -%} {%- include 'nprogress.njk' -%}

View File

@ -133,6 +133,10 @@ hexo.extend.filter.register('after_generate', () => {
} }
// Others // Others
if (!theme.fancybox) {
hexo.route.remove('js/third-party/fancybox.js');
}
if (!theme.nprogress.enable) { if (!theme.nprogress.enable) {
hexo.route.remove('js/third-party/nprogress.js'); hexo.route.remove('js/third-party/nprogress.js');
} }

View File

@ -19,7 +19,6 @@ hexo.extend.helper.register('next_config', function() {
sidebar : theme.sidebar, sidebar : theme.sidebar,
copycode : theme.codeblock.copy_button.enable, copycode : theme.codeblock.copy_button.enable,
bookmark : theme.bookmark, bookmark : theme.bookmark,
fancybox : theme.fancybox,
mediumzoom: theme.mediumzoom, mediumzoom: theme.mediumzoom,
lazyload : theme.lazyload, lazyload : theme.lazyload,
pangu : theme.pangu, pangu : theme.pangu,

View File

@ -63,7 +63,6 @@ NexT.boot.refresh = function() {
* Need to add config option in Front-End at 'scripts/helpers/next-config.js' file. * Need to add config option in Front-End at 'scripts/helpers/next-config.js' file.
*/ */
CONFIG.prism && window.Prism.highlightAll(); CONFIG.prism && window.Prism.highlightAll();
CONFIG.fancybox && NexT.utils.wrapImageWithFancyBox();
CONFIG.mediumzoom && window.mediumZoom('.post-body :not(a) > img, .post-body > img', { CONFIG.mediumzoom && window.mediumZoom('.post-body :not(a) > img, .post-body > img', {
background: 'var(--content-bg-color)' background: 'var(--content-bg-color)'
}); });

38
source/js/third-party/fancybox.js vendored Normal file
View File

@ -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(`<a class="fancybox fancybox.image" href="${imageLink}" itemscope itemtype="http://schema.org/ImageObject" itemprop="url"></a>`).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(`<p class="image-caption">${imageTitle}</p>`);
}
// 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
}
}
});
});

View File

@ -31,44 +31,6 @@ if (typeof DOMTokenList.prototype.replace !== 'function') {
NexT.utils = { 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(`<a class="fancybox fancybox.image" href="${imageLink}" itemscope itemtype="http://schema.org/ImageObject" itemprop="url"></a>`).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(`<p class="image-caption">${imageTitle}</p>`);
}
// 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() { registerExtURL: function() {
document.querySelectorAll('span.exturl').forEach(element => { document.querySelectorAll('span.exturl').forEach(element => {
const link = document.createElement('a'); const link = document.createElement('a');