Skip to content

Commit 6cd7bc5

Browse files
committed
[opt]: twikoo 评论分页
1 parent e38710c commit 6cd7bc5

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

docs/index.html

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -225,24 +225,52 @@
225225
n.id = "tcomment";
226226
a.appendTo(a.find(".content"), n);
227227
})
228+
229+
// twikoo 初始化控制变量
230+
var twikooTimer = null;
231+
var lastPath = null;
232+
228233
// 调用 twikoo 评论,正则 path,刷新当前评论
229234
hook.doneEach(function () {
230-
twikoo.init({
231-
envId: 'https://twikooeo.sicnuwiki.com',
232-
el: '#tcomment',
233-
lang: 'zh-CN',
234-
// path: location.pathname+location.hash.replace(/\?.*/,''),
235-
onCommentLoaded: function () {
236-
window.ViewImage && ViewImage.init('.tk-content img:not(.avatar,.tk-avatar-img,.tk-owo-emotion),.ebook img,a.zoom');
237-
}
238-
}).then(function () {
239-
console.log('Twikoo 加载完成');
240-
setTimeout(function() {
241-
// 通过点击刷新按钮,获取当前页评论
242-
let tk_icon = document.getElementsByClassName("tk-icon")[0];
243-
tk_icon ? tk_icon.click() : undefined;
244-
},700)
245-
});
235+
// 获取当前 docsify 页面路径作为评论区分标识
236+
var currentPath = location.hash.replace(/^#/, '').replace(/\?.*$/, '') || '/';
237+
238+
// 如果路径相同,不重复初始化
239+
if (lastPath === currentPath) {
240+
return;
241+
}
242+
lastPath = currentPath;
243+
244+
// 取消之前的延迟初始化(防止快速切换页面导致叠加)
245+
if (twikooTimer) {
246+
clearTimeout(twikooTimer);
247+
twikooTimer = null;
248+
}
249+
250+
// 先清空评论容器,避免叠加
251+
var tcomment = document.getElementById('tcomment');
252+
if (tcomment) {
253+
tcomment.innerHTML = '';
254+
}
255+
256+
// 延迟加载 twikoo,避免影响页面滑动性能
257+
twikooTimer = setTimeout(function() {
258+
// 再次检查路径是否仍然匹配(用户可能已切换)
259+
var nowPath = location.hash.replace(/^#/, '').replace(/\?.*$/, '') || '/';
260+
if (nowPath !== currentPath) {
261+
return;
262+
}
263+
264+
twikoo.init({
265+
envId: 'https://twikooeo.sicnuwiki.com',
266+
el: '#tcomment',
267+
lang: 'zh-CN',
268+
path: currentPath,
269+
onCommentLoaded: function () {
270+
window.ViewImage && ViewImage.init('.tk-content img:not(.avatar,.tk-avatar-img,.tk-owo-emotion),.ebook img,a.zoom');
271+
}
272+
});
273+
}, 300);
246274
})
247275
}
248276
]

0 commit comments

Comments
 (0)