forked from tangly1024/NotionNext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWalineComponent.js
76 lines (67 loc) · 2.04 KB
/
WalineComponent.js
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
import React from 'react'
import { init } from '@waline/client'
import BLOG from '@/blog.config'
import { useRouter } from 'next/router'
const path = ''
let waline = null
/**
* @see https://waline.js.org/guide/get-started.html
* @param {*} props
* @returns
*/
const WalineComponent = (props) => {
const containerRef = React.createRef()
const router = useRouter()
const updateWaline = url => {
if (url !== path && waline) {
waline.update(props)
}
}
React.useEffect(() => {
if (!waline) {
waline = init({
...props,
el: containerRef.current,
serverURL: BLOG.COMMENT_WALINE_SERVER_URL,
lang: BLOG.lang,
reaction: true
})
}
// 跳转评论
router.events.on('routeChangeComplete', updateWaline)
const anchor = window.location.hash
if (anchor) {
// 选择需要观察变动的节点
const targetNode = document.getElementsByClassName('wl-cards')[0]
// 当观察到变动时执行的回调函数
const mutationCallback = (mutations) => {
for (const mutation of mutations) {
const type = mutation.type
if (type === 'childList') {
const anchorElement = document.getElementById(anchor.substring(1))
if (anchorElement && anchorElement.className === 'wl-item') {
anchorElement.scrollIntoView({ block: 'end', behavior: 'smooth' })
setTimeout(() => {
anchorElement.classList.add('animate__animated')
anchorElement.classList.add('animate__bounceInRight')
observer.disconnect()
}, 300)
}
}
}
}
// 观察子节点 变化
const observer = new MutationObserver(mutationCallback)
observer.observe(targetNode, { childList: true })
}
return () => {
if (waline) {
waline.destroy()
waline = null
}
router.events.off('routeChangeComplete', updateWaline)
}
}, [])
return <div ref={containerRef} />
}
export default WalineComponent