mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 15:09:22 +00:00
84 lines
2.3 KiB
JavaScript
84 lines
2.3 KiB
JavaScript
import { createRef, useEffect } from 'react'
|
|
import { init } from '@waline/client'
|
|
import { useRouter } from 'next/router'
|
|
import '@waline/client/dist/waline.css'
|
|
import { siteConfig } from '@/lib/config'
|
|
|
|
const path = ''
|
|
let waline = null
|
|
/**
|
|
* @see https://waline.js.org/guide/get-started.html
|
|
* @param {*} props
|
|
* @returns
|
|
*/
|
|
const WalineComponent = (props) => {
|
|
const containerRef = createRef()
|
|
const router = useRouter()
|
|
|
|
const updateWaline = url => {
|
|
if (url !== path && waline) {
|
|
waline.update(props)
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (!waline) {
|
|
waline = init({
|
|
...props,
|
|
el: containerRef.current,
|
|
serverURL: siteConfig('COMMENT_WALINE_SERVER_URL'),
|
|
lang: siteConfig('LANG'),
|
|
reaction: true,
|
|
dark: 'html.dark',
|
|
emoji: [
|
|
'//npm.elemecdn.com/@waline/emojis@1.1.0/tieba',
|
|
'//npm.elemecdn.com/@waline/emojis@1.1.0/weibo',
|
|
'//npm.elemecdn.com/@waline/emojis@1.1.0/bilibili'
|
|
]
|
|
})
|
|
}
|
|
|
|
// 跳转评论
|
|
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
|