vuepress-plugin-twikoo 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Comment.vue +64 -44
- package/package.json +1 -1
package/Comment.vue
CHANGED
|
@@ -1,34 +1,30 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<div
|
|
2
|
+
<div></div>
|
|
3
3
|
</template>
|
|
4
4
|
|
|
5
5
|
<script>
|
|
6
|
+
|
|
6
7
|
export default {
|
|
7
8
|
mounted() {
|
|
8
|
-
//
|
|
9
|
+
// 页面刷新/组件挂载时,直接执行初始化+移动逻辑
|
|
9
10
|
if (typeof window !== 'undefined') {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
this.initTwikoo()
|
|
15
|
-
})
|
|
11
|
+
this.$nextTick(async () => {
|
|
12
|
+
// 先初始化 Twikoo(异步)
|
|
13
|
+
await this.initTwikoo();
|
|
14
|
+
});
|
|
16
15
|
}
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
})
|
|
30
|
-
}
|
|
31
|
-
})
|
|
17
|
+
// 监听路由变化(仅处理路由跳转,刷新不触发)
|
|
18
|
+
this.$watch(
|
|
19
|
+
() => this.$route,
|
|
20
|
+
async (to, from) => {
|
|
21
|
+
if (to.path === from.path) return; // 仅 hash 变化时跳过
|
|
22
|
+
if (typeof window !== 'undefined') {
|
|
23
|
+
await this.initTwikoo();
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
{ immediate: false }
|
|
27
|
+
);
|
|
32
28
|
},
|
|
33
29
|
|
|
34
30
|
methods: {
|
|
@@ -37,38 +33,62 @@ export default {
|
|
|
37
33
|
to: {},
|
|
38
34
|
from: {},
|
|
39
35
|
...this.$frontmatter
|
|
40
|
-
}
|
|
36
|
+
};
|
|
41
37
|
|
|
42
38
|
if (!this.needComment(frontmatter)) {
|
|
43
|
-
return
|
|
39
|
+
return;
|
|
44
40
|
}
|
|
45
41
|
|
|
46
|
-
|
|
47
|
-
twikoo
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
42
|
+
// 异步加载 Twikoo 并初始化
|
|
43
|
+
const twikoo = await import('twikoo');
|
|
44
|
+
// 初始化后返回 Promise,确保 DOM 生成完成
|
|
45
|
+
await twikoo.init({
|
|
46
|
+
...TWIKOO_OPTIONS
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
this.moveContainerWithRetry();
|
|
51
50
|
},
|
|
52
51
|
|
|
53
52
|
needComment(frontmatter) {
|
|
54
|
-
return frontmatter.twikoo !== false
|
|
53
|
+
return frontmatter.twikoo !== false;
|
|
55
54
|
},
|
|
56
55
|
|
|
56
|
+
// 基础移动逻辑(抽离成独立函数)
|
|
57
57
|
moveContainer() {
|
|
58
|
-
if (TWIKOO_CONTAINER)
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
}
|
|
58
|
+
if (!TWIKOO_CONTAINER) return; // 无目标容器则直接返回
|
|
59
|
+
|
|
60
|
+
const targetContainer = document.querySelector(TWIKOO_CONTAINER);
|
|
61
|
+
if (!targetContainer) return; // 目标容器不存在则返回
|
|
62
|
+
|
|
63
|
+
const commentContainer = document.querySelector('#twikoo');
|
|
64
|
+
if (commentContainer) {
|
|
65
|
+
targetContainer.appendChild(commentContainer);
|
|
66
|
+
return true; // 移动成功,返回 true
|
|
69
67
|
}
|
|
70
|
-
|
|
68
|
+
return false;
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
// 带重试机制的移动逻辑(解决异步 DOM 生成问题)
|
|
72
|
+
moveContainerWithRetry(maxRetries = 5, interval = 100) {
|
|
73
|
+
let retries = 0;
|
|
74
|
+
|
|
75
|
+
const retry = () => {
|
|
76
|
+
const success = this.moveContainer();
|
|
77
|
+
retries++;
|
|
78
|
+
|
|
79
|
+
// 移动成功 或 达到最大重试次数,终止重试
|
|
80
|
+
if (success || retries >= maxRetries) {
|
|
81
|
+
if (!success) console.warn('Twikoo 容器移动失败:未找到 Twikoo 评论容器元素');
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 继续重试
|
|
86
|
+
setTimeout(retry, interval);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
retry();
|
|
90
|
+
},
|
|
71
91
|
|
|
72
92
|
}
|
|
73
|
-
}
|
|
74
|
-
</script>
|
|
93
|
+
};
|
|
94
|
+
</script>
|