Skip to content

Commit f78ab1a

Browse files
committed
refactor: restructure fancybox invocation functions and revamp implementation methods
Refactored the way fancybox functions are called and rewritten the implementation methods. Additionally, added compatibility with the <picture> tag to provide responsive images and/or multiple image formats.
1 parent be35812 commit f78ab1a

File tree

8 files changed

+109
-129
lines changed

8 files changed

+109
-129
lines changed

_config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ default:
2323

2424
cdn_system:
2525
# CDN 优先级 从高到低排序 写入 priority 列表即表示启用(enable)
26-
priority: [custom, local, cdnjs, npm, static]
26+
priority: [custom, local, npm, static, cdnjs]
2727
# ========================================================================
2828
# cdn数据配置文件见 "theme/_cdn.yml", 可以在 source/_data/cdn.yml 覆盖 theme/_cdn.yml
2929
# 变量说明:

layout/_plugins/comments/artalk/script.ejs

+2-10
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,8 @@
5151
},
5252
<% } %>
5353
}));
54-
55-
if (typeof VolantisFancyBox === "undefined"){
56-
const checkFancyBox = setInterval(() => {
57-
if(typeof VolantisFancyBox === "undefined") return;
58-
clearInterval(checkFancyBox);
59-
VolantisFancyBox.bind('.atk-content img:not([atk-emoticon])');
60-
})
61-
} else {
62-
VolantisFancyBox.bind('.atk-content img:not([atk-emoticon])');
63-
}
54+
const fancyBoxInstance = new VolantisFancyBox();
55+
fancyBoxInstance.bind('.atk-content img:not([atk-emoticon])');
6456
}
6557
6658
load_artalk();

layout/_plugins/comments/twikoo/script.ejs

+2-9
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,8 @@
1717
path: path,
1818
placeholder: placeholder,
1919
onCommentLoaded: function() {
20-
if(typeof VolantisFancyBox === "undefined") {
21-
const checkFancyBox = setInterval(() => {
22-
if(typeof VolantisFancyBox === "undefined") return;
23-
clearInterval(checkFancyBox);
24-
VolantisFancyBox.groupBind('.tk-content img:not([class*="emo"])', 'Comments');
25-
})
26-
} else {
27-
VolantisFancyBox.groupBind('.tk-content img:not([class*="emo"])', 'Comments');
28-
}
20+
const fancyBoxInstance = new VolantisFancyBox();
21+
fancyBoxInstance.groupBind('.tk-content img:not([class*="emo"])', 'Comments');
2922
}
3023
}),pdata.commentConfig))
3124
}

layout/_plugins/comments/vssue/script.ejs

+2-9
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,8 @@
44
<script>
55
volantis.css("<%- theme.cdn.vssue_css %>");
66
function fancybox_vssue() {
7-
if(typeof VolantisFancyBox === "undefined") {
8-
const checkFancyBox = setInterval(() => {
9-
if(typeof VolantisFancyBox === "undefined") return;
10-
clearInterval(checkFancyBox);
11-
VolantisFancyBox.bind('#vssue .vssue-comment-main img');
12-
})
13-
} else {
14-
VolantisFancyBox.bind('#vssue .vssue-comment-main img');
15-
}
7+
const fancyBoxInstance = new VolantisFancyBox();
8+
fancyBoxInstance.bind('#vssue .vssue-comment-main img');
169
}
1710
</script>
1811
<script>

layout/_plugins/comments/waline/script.ejs

+2-9
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,8 @@
4848
}
4949
5050
function fancybox_waline() {
51-
if (typeof VolantisFancyBox === "undefined") {
52-
const checkFancyBox = setInterval(() => {
53-
if (typeof VolantisFancyBox === "undefined") return;
54-
clearInterval(checkFancyBox);
55-
VolantisFancyBox.bind('#waline .wl-content img:not(.wl-emoji)');
56-
});
57-
} else {
58-
VolantisFancyBox.bind('#waline .wl-content img:not(.wl-emoji)');
59-
}
51+
const fancyBoxInstance = new VolantisFancyBox();
52+
fancyBoxInstance.bind('#waline .wl-content img:not(.wl-emoji)');
6053
}
6154
6255
volantis.css('<%= theme.cdn.waline_css %>');
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.fancybox__container
2+
padding 10px !important
3+
.fancybox__slide
4+
padding 2.5rem !important
5+
.f-button
6+
&:first-child
7+
border-radius 12px 0 0 12px
8+
&:last-child
9+
border-radius 0 12px 12px 0

source/css/_style/_plugins/index.styl

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ if hexo-config('custom_css.cursor.enable')
1717
if hexo-config('rightmenus.enable')
1818
@import '_rightmenu/*'
1919

20+
@import 'fancybox'
2021

2122
// 暗黑模式样式放到最后加载
2223
if hexo-config('plugins.darkmode.enable')

source/js/app.js

+90-91
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ document.addEventListener("DOMContentLoaded", function () {
22
volantis.requestAnimationFrame(() => {
33
VolantisApp.init();
44
VolantisApp.subscribe();
5-
VolantisFancyBox.init();
5+
new VolantisFancyBox();
66
highlightKeyWords.startFromURL();
77
locationHash();
88
});
@@ -670,118 +670,117 @@ const VolantisApp = (() => {
670670
Object.freeze(VolantisApp);
671671

672672
/* FancyBox */
673-
const VolantisFancyBox = (() => {
674-
const fn = {};
675-
676-
fn.loadFancyBox = (done) => {
677-
volantis.css(volantis.GLOBAL_CONFIG.cdn.fancybox_css);
678-
volantis.js(volantis.GLOBAL_CONFIG.cdn.fancybox_js).then(() => {
679-
if (done) done();
680-
})
673+
class VolantisFancyBox {
674+
constructor(checkMain = true) {
675+
this.option = {
676+
Hash: false,
677+
groupAll: true,
678+
caption: (fancybox, slide) => slide.thumbEl?.alt || "",
679+
wheel: "slide",
680+
contentClick: 'iterateZoom',
681+
Thumbs: {
682+
showOnStart: false
683+
},
684+
Images: {
685+
content: (_ref, slide) => {
686+
const imgElement = slide.thumbEl;
687+
const pictureElement = imgElement.closest('picture');
688+
if (imgElement.hasAttribute('data-src')) {
689+
imgElement.setAttribute('src', imgElement.getAttribute('data-src'));
690+
}
691+
if (pictureElement) {
692+
pictureElement.classList.remove("lazy");
693+
let sources = pictureElement.getElementsByTagName('source');
694+
for (let source of sources) {
695+
if (source.hasAttribute('data-srcset')) {
696+
source.setAttribute('srcset', source.getAttribute('data-srcset'));
697+
}
698+
}
699+
return pictureElement.outerHTML;
700+
} else {
701+
return imgElement.outerHTML;
702+
}
703+
},
704+
Panzoom: {
705+
maxScale: 1.5,
706+
panMode: "mousemove",
707+
mouseMoveFactor: 1.1,
708+
mouseMoveFriction: 0.12,
709+
}
710+
},
711+
Toolbar: {
712+
display: {
713+
left: ["infobar"],
714+
middle: [
715+
"zoomIn",
716+
"zoomOut",
717+
"toggle1to1",
718+
"rotateCCW",
719+
"rotateCW",
720+
"flipX",
721+
"flipY",
722+
],
723+
right: ["slideshow", "download", "thumbs", "close"],
724+
},
725+
}
726+
};
727+
this.#init(checkMain);
681728
}
682729

683-
/**
684-
* 加载及处理
685-
*
686-
* @param {*} checkMain 是否只处理文章区域的文章
687-
* @param {*} done FancyBox 加载完成后的动作,默认执行分组绑定
688-
* @returns
689-
*/
690-
fn.init = (checkMain = true, done = fn.groupBind) => {
730+
#init(checkMain) {
691731
if (!document.querySelector(".md .gallery img, .fancybox") && checkMain) return;
732+
this.groupBind();
733+
}
734+
735+
async #checkFancybox(done) {
692736
if (typeof Fancybox === "undefined") {
693-
fn.loadFancyBox(done);
737+
await volantis.css(volantis.GLOBAL_CONFIG.cdn.fancybox_css);
738+
await volantis.js(volantis.GLOBAL_CONFIG.cdn.fancybox_js);
739+
done.call(this);
694740
} else {
695-
done();
741+
done.call(this);
696742
}
697743
}
698744

699-
/**
700-
* 图片元素预处理
701-
*
702-
* @param {*} selectors 选择器
703-
* @param {*} name 分组
704-
*/
705-
fn.elementHandling = (selectors, name) => {
706-
const nodeList = document.querySelectorAll(selectors);
707-
nodeList.forEach($item => {
745+
#elementHandling(selectors, groupName) {
746+
if (!selectors) return;
747+
document.querySelectorAll(selectors).forEach($item => {
708748
if ($item.hasAttribute('fancybox')) return;
709749
$item.setAttribute('fancybox', '');
710750
const $link = document.createElement('a');
711-
$link.setAttribute('href', $item.src);
712-
$link.setAttribute('data-caption', $item.alt);
713-
$link.setAttribute('data-fancybox', name);
751+
$link.setAttribute('href', $item.src || $item.dataset?.src);
752+
$link.setAttribute('data-caption', $item.alt || '');
753+
$link.setAttribute('data-fancybox', groupName);
714754
$link.classList.add('fancybox');
715755
$link.append($item.cloneNode());
716756
$item.replaceWith($link);
717-
})
757+
});
718758
}
719759

720-
/**
721-
* 原生绑定
722-
*
723-
* @param {*} selectors 选择器
724-
*/
725-
fn.bind = (selectors) => {
726-
fn.init(false, () => {
727-
Fancybox.bind(selectors, {
728-
groupAll: true,
729-
Hash: false,
730-
hideScrollbar: false,
731-
Thumbs: {
732-
autoStart: false,
733-
},
734-
caption: function (fancybox, slide) {
735-
return slide.thumbEl?.alt || "";
736-
}
737-
});
760+
bind(selectors) {
761+
this.#checkFancybox(() => {
762+
Fancybox?.unbind(selectors);
763+
Fancybox?.bind(selectors, this.option);
738764
});
739765
}
740766

741-
/**
742-
* 分组绑定
743-
*
744-
* @param {*} groupName 分组名称
745-
*/
746-
fn.groupBind = (groupName = null) => {
747-
const group = new Set();
748-
749-
document.querySelectorAll(".gallery").forEach(ele => {
750-
if (ele.querySelector("img")) {
751-
group.add(ele.getAttribute('data-group') || 'default');
752-
}
753-
})
754-
755-
if (!!groupName) group.add(groupName);
756-
757-
for (const iterator of group) {
758-
Fancybox.unbind('[data-fancybox="' + iterator + '"]');
759-
Fancybox.bind('[data-fancybox="' + iterator + '"]', {
760-
Hash: false,
761-
hideScrollbar: false,
762-
Thumbs: {
763-
autoStart: false,
767+
groupBind(selectors, groupName = 'default') {
768+
this.#checkFancybox(() => {
769+
this.#elementHandling(selectors, groupName);
770+
const group = new Set();
771+
document.querySelectorAll('.gallery').forEach(ele => {
772+
if (ele.querySelector("img")) {
773+
group.add(ele.getAttribute('data-group') || 'default');
764774
}
765775
});
766-
}
767-
}
768-
769-
return {
770-
init: fn.init,
771-
bind: fn.bind,
772-
groupBind: (selectors, groupName = 'default') => {
773-
try {
774-
fn.elementHandling(selectors, groupName);
775-
fn.init(false, () => {
776-
fn.groupBind(groupName)
777-
});
778-
} catch (error) {
779-
console.error(error)
780-
}
781-
}
776+
if (groupName) group.add(groupName);
777+
group.forEach(name => {
778+
Fancybox?.unbind(`[data-fancybox="${name}"]`);
779+
Fancybox?.bind(`[data-fancybox="${name}"]`, this.option);
780+
});
781+
});
782782
}
783-
})()
784-
Object.freeze(VolantisFancyBox);
783+
}
785784

786785
// highlightKeyWords 与 搜索功能搭配 https://github.com/next-theme/hexo-theme-next/blob/eb194a7258058302baf59f02d4b80b6655338b01/source/js/third-party/search/local-search.js
787786
// Question: 锚点稳定性未知

0 commit comments

Comments
 (0)