|
221 | 221 | hook.afterEach(function(html) { |
222 | 222 | window.ViewImage && ViewImage.init('.ebook img,a.zoom'); |
223 | 223 | }); |
224 | | - // 动态插入 div#tcomment 的 dom |
225 | | - hook.mounted(function () { |
| 224 | + function ensureTcomment() { |
| 225 | + var tcomment = document.getElementById('tcomment'); |
| 226 | + if (tcomment) return tcomment; |
226 | 227 | var a = Docsify.dom; |
227 | 228 | var n = a.create("div"); |
228 | 229 | n.id = "tcomment"; |
229 | | - a.appendTo(a.find(".content"), n); |
| 230 | + a.appendTo(a.find(".content"), n); |
| 231 | + return n; |
| 232 | + } |
| 233 | + |
| 234 | + function normalizeCommentPath(path) { |
| 235 | + var p = decodeURIComponent(path || '/').trim(); |
| 236 | + if (!p || p === '#') return '/'; |
| 237 | + if (p.charAt(0) !== '/') p = '/' + p; |
| 238 | + p = p.replace(/\/\.\//g, '/'); |
| 239 | + p = p.replace(/\/{2,}/g, '/'); |
| 240 | + // 统一 README 首页写法 |
| 241 | + if (p === '/README' || p === '/README.md' || p === '/./README') return '/README'; |
| 242 | + return p; |
| 243 | + } |
| 244 | + |
| 245 | + // 动态插入 div#tcomment 的 dom |
| 246 | + hook.mounted(function () { |
| 247 | + ensureTcomment(); |
230 | 248 | }) |
231 | 249 |
|
232 | 250 | // twikoo 初始化控制变量 |
233 | 251 | var twikooTimer = null; |
| 252 | + var twikooObserver = null; |
| 253 | + var twikooLoading = false; |
234 | 254 | var lastPath = null; |
| 255 | + |
| 256 | + function loadTwikoo(path) { |
| 257 | + if (twikooLoading) return; |
| 258 | + twikooLoading = true; |
| 259 | + twikoo.init({ |
| 260 | + envId: 'https://twikooeo.sicnuwiki.com', |
| 261 | + el: '#tcomment', |
| 262 | + lang: 'zh-CN', |
| 263 | + path: path, |
| 264 | + onCommentLoaded: function () { |
| 265 | + window.ViewImage && ViewImage.init('.tk-content img:not(.avatar,.tk-avatar-img,.tk-owo-emotion),.ebook img,a.zoom'); |
| 266 | + } |
| 267 | + }).then(function () { |
| 268 | + twikooLoading = false; |
| 269 | + // 可选:强制刷新一次,确保切换页面后评论更新 |
| 270 | + setTimeout(function () { |
| 271 | + var tkIcon = document.getElementsByClassName("tk-icon")[0]; |
| 272 | + if (tkIcon) tkIcon.click(); |
| 273 | + }, 200); |
| 274 | + }).catch(function () { |
| 275 | + twikooLoading = false; |
| 276 | + }); |
| 277 | + } |
| 278 | + |
| 279 | + function observeAndLoad(path) { |
| 280 | + if (twikooObserver) { |
| 281 | + twikooObserver.disconnect(); |
| 282 | + twikooObserver = null; |
| 283 | + } |
| 284 | + if (twikooTimer) { |
| 285 | + clearTimeout(twikooTimer); |
| 286 | + twikooTimer = null; |
| 287 | + } |
| 288 | + |
| 289 | + var tcomment = ensureTcomment(); |
| 290 | + if (!tcomment) return; |
| 291 | + |
| 292 | + if (!('IntersectionObserver' in window)) { |
| 293 | + twikooTimer = setTimeout(function () { |
| 294 | + loadTwikoo(path); |
| 295 | + }, 0); |
| 296 | + return; |
| 297 | + } |
| 298 | + |
| 299 | + twikooObserver = new IntersectionObserver(function (entries) { |
| 300 | + if (!entries || !entries.length) return; |
| 301 | + if (entries[0].isIntersecting) { |
| 302 | + if (twikooObserver) { |
| 303 | + twikooObserver.disconnect(); |
| 304 | + twikooObserver = null; |
| 305 | + } |
| 306 | + twikooTimer = setTimeout(function () { |
| 307 | + loadTwikoo(path); |
| 308 | + }, 0); |
| 309 | + } |
| 310 | + }, { rootMargin: '200px 0px' }); |
| 311 | + |
| 312 | + twikooObserver.observe(tcomment); |
| 313 | + } |
235 | 314 |
|
236 | 315 | // 调用 twikoo 评论,正则 path,刷新当前评论 |
237 | 316 | hook.doneEach(function () { |
238 | 317 | // 获取当前 docsify 页面路径作为评论区分标识 |
239 | | - function normalizeCommentPath(path) { |
240 | | - var p = decodeURIComponent(path || '/').trim(); |
241 | | - if (!p || p === '#') return '/'; |
242 | | - if (p.charAt(0) !== '/') p = '/' + p; |
243 | | - p = p.replace(/\/\.\//g, '/'); |
244 | | - p = p.replace(/\/{2,}/g, '/'); |
245 | | - // 统一 README 首页写法 |
246 | | - if (p === '/README' || p === '/README.md' || p === '/./README') return '/README'; |
247 | | - return p; |
248 | | - } |
249 | 318 | var currentPath = normalizeCommentPath(vm.route.path || location.hash.replace(/^#/, '').replace(/\?.*$/, '') || '/'); |
250 | 319 |
|
251 | 320 | // 如果路径相同,不重复初始化 |
|
254 | 323 | } |
255 | 324 | lastPath = currentPath; |
256 | 325 |
|
257 | | - // 取消之前的延迟初始化(防止快速切换页面导致叠加) |
258 | | - if (twikooTimer) { |
259 | | - clearTimeout(twikooTimer); |
260 | | - twikooTimer = null; |
261 | | - } |
262 | | - |
263 | 326 | // 先清空评论容器,避免叠加 |
264 | | - var tcomment = document.getElementById('tcomment'); |
| 327 | + var tcomment = ensureTcomment(); |
265 | 328 | if (tcomment) { |
266 | 329 | tcomment.innerHTML = ''; |
267 | 330 | } |
268 | | - |
269 | | - // 延迟加载 twikoo,避免影响页面滑动性能 |
270 | | - twikooTimer = setTimeout(function() { |
271 | | - // 再次检查路径是否仍然匹配(用户可能已切换) |
272 | | - var nowPath = normalizeCommentPath(vm.route.path || location.hash.replace(/^#/, '').replace(/\?.*$/, '') || '/'); |
273 | | - if (nowPath !== currentPath) { |
274 | | - return; |
275 | | - } |
276 | | - |
277 | | - twikoo.init({ |
278 | | - envId: 'https://twikooeo.sicnuwiki.com', |
279 | | - el: '#tcomment', |
280 | | - lang: 'zh-CN', |
281 | | - path: currentPath, |
282 | | - onCommentLoaded: function () { |
283 | | - window.ViewImage && ViewImage.init('.tk-content img:not(.avatar,.tk-avatar-img,.tk-owo-emotion),.ebook img,a.zoom'); |
284 | | - } |
285 | | - }); |
286 | | - }, 300); |
| 331 | + |
| 332 | + observeAndLoad(currentPath); |
287 | 333 | }) |
288 | 334 | } |
289 | 335 | ] |
|
0 commit comments