mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
3.6.0 最后的优化
This commit is contained in:
@@ -45,6 +45,8 @@ import 'prismjs/components/prism-r.js'
|
||||
|
||||
// 化学方程式
|
||||
import '@/lib/mhchem'
|
||||
// 页面渲染观察者
|
||||
let observer
|
||||
|
||||
// https://github.com/txs
|
||||
// import PrismMac from '@/components/PrismMac'
|
||||
@@ -74,6 +76,10 @@ const NotionPage = ({ post }) => {
|
||||
|
||||
const zoomRef = React.useRef(zoom ? zoom.clone() : null)
|
||||
|
||||
if (isBrowser() && !observer) {
|
||||
addWatch4Dom()
|
||||
}
|
||||
|
||||
React.useEffect(() => {
|
||||
setTimeout(() => {
|
||||
if (window.location.hash) {
|
||||
@@ -148,4 +154,61 @@ function getMediumZoomMargin() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听DOM变化
|
||||
* @param {*} element
|
||||
*/
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
function addWatch4Dom(element) {
|
||||
// 选择需要观察变动的节点
|
||||
const targetNode = element || document?.getElementById('container')
|
||||
// 观察器的配置(需要观察什么变动)
|
||||
const config = {
|
||||
attributes: true,
|
||||
childList: true,
|
||||
subtree: true
|
||||
}
|
||||
|
||||
// 创建一个观察器实例并传入回调函数
|
||||
observer = new MutationObserver(mutationCallback)
|
||||
// console.log('观察节点变化', observer)
|
||||
// 以上述配置开始观察目标节点
|
||||
if (targetNode) {
|
||||
observer.observe(targetNode, config)
|
||||
}
|
||||
|
||||
// observer.disconnect()
|
||||
}
|
||||
|
||||
// 当页面发生时会调用此函数
|
||||
const mutationCallback = (mutations) => {
|
||||
for (const mutation of mutations) {
|
||||
const type = mutation.type
|
||||
switch (type) {
|
||||
case 'childList':
|
||||
// console.log('A child node has been added or removed.', mutation)
|
||||
if (mutation.addedNodes.length > 0) {
|
||||
if (mutation?.target?.parentElement?.nodeName === 'PRE' || mutation?.target?.parentElement?.nodeName === 'CODE') {
|
||||
if (mutation.addedNodes[0].nodeName === '#text' && mutation.target.childElementCount > 0) {
|
||||
mutation.addedNodes[0].remove() // 移除新增的内容
|
||||
}
|
||||
}
|
||||
if (mutation?.target?.className === 'language-mermaid') {
|
||||
// mermaid重刷新bug TODO
|
||||
}
|
||||
}
|
||||
break
|
||||
case 'attributes':
|
||||
// console.log(`The ${mutation.attributeName} attribute was modified.`, mutation.target)
|
||||
// console.log(mutation.attributeName)
|
||||
break
|
||||
case 'subtree':
|
||||
// console.log('The subtree was modified.', mutation.target)
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default NotionPage
|
||||
|
||||
Reference in New Issue
Block a user