import { createRef, useEffect } from 'react'
import { init } from '@waline/client'
import { useRouter } from 'next/router'
import '../node_modules/@waline/client/dist/waline.css'
const path = ''
let waline = null
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: '',//修改为你的Waline服务端
lang: "zh-cn",
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