-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComment.vue
92 lines (83 loc) · 1.95 KB
/
Comment.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<template>
<div></div>
</template>
<script>
import {
provider,
} from './util'
const commentDomID = 'vuepress-plugin-comment'
let timer = null
export default {
mounted () {
timer = setTimeout(() => {
const frontmatter = {
to: {},
from: {},
...this.$frontmatter
}
clear() && needComment(frontmatter) && renderComment(frontmatter)
}, 1000)
this.$router.afterEach((to, from) => {
if (to && from && to.path === from.path) {
return
}
const frontmatter = {
to,
from,
...this.$frontmatter
}
clear() && needComment(frontmatter) && renderComment(frontmatter)
})
}
}
/**
* Clear last page comment dom
*/
function clear (frontmatter) {
let el = COMMENT_OPTIONS.el || commentDomID
if (el.startsWith('#')) {
el = el.slice(1)
}
switch (COMMENT_CHOOSEN) {
case 'gitalk':
return provider.gitalk.clear(commentDomID)
case 'valine':
console.log(el)
return provider.valine.clear(el)
case 'waline':
console.log(el)
return provider.waline.clear(el)
default: return false
}
}
/**
* Check if current page needs render comment
*/
function needComment (frontmatter) {
return frontmatter.comment !== false && frontmatter.comments !== false
}
/**
* Render comment dom and append it to container
*/
function renderComment (frontmatter) {
clearTimeout(timer)
const parentDOM = document.querySelector(COMMENT_CONTAINER)
if (!parentDOM) {
timer = setTimeout(() => renderComment(frontmatter), 200)
return
}
let el = COMMENT_OPTIONS.el || commentDomID
if (el.startsWith('#')) {
el = el.slice(1)
}
switch (COMMENT_CHOOSEN) {
case 'gitalk':
return provider.gitalk.render(frontmatter, commentDomID)
case 'valine':
return provider.valine.render(frontmatter, el)
case 'waline':
return provider.waline.render(frontmatter, el)
default: return false
}
}
</script>