Skip to content

Commit

Permalink
Merge pull request tangly1024#227 from tangly1024/bug-fix-code-copy-r…
Browse files Browse the repository at this point in the history
…epeat

Bug fix code copy repeat
  • Loading branch information
tangly1024 authored May 6, 2022
2 parents cd26256 + c569b29 commit 4cd93f2
Showing 1 changed file with 73 additions and 29 deletions.
102 changes: 73 additions & 29 deletions components/NotionPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const Pdf = dynamic(
const Modal = dynamic(
() => import('react-notion-x/build/third-party/modal').then((m) => m.Modal), { ssr: false }
)

const NotionPage = ({ post }) => {
if (!post || !post.blockMap) {
return <>{post?.summary || ''}</>
Expand All @@ -34,40 +35,23 @@ const NotionPage = ({ post }) => {

const zoomRef = React.useRef(zoom ? zoom.clone() : null)

React.useEffect(() => {
addWatch4Dom()
}, [])
setTimeout(() => {
if (typeof document !== 'undefined') {
const buttons = document.getElementsByClassName('notion-code-copy')
for (const e of buttons) {
e.addEventListener('click', fixCopy)
}
// 将相册gallery下的图片加入放大功能
// const container = document?.getElementById('container')
const imgList = document?.querySelectorAll('.notion-collection-card-cover img')
if (imgList && zoomRef.current) {
for (let i = 0; i < imgList.length; i++) {
(zoomRef.current).attach(imgList[i])
}
}

const cards = document.getElementsByClassName('notion-collection-card')
for (const e of cards) {
e.removeAttribute('href')
// 将相册gallery下的图片加入放大功能
const imgList = document?.querySelectorAll('.notion-collection-card-cover img')
if (imgList && zoomRef.current) {
for (let i = 0; i < imgList.length; i++) {
(zoomRef.current).attach(imgList[i])
}
}
}, 800)

/**
* 复制代码后,会重复 @see https://github.com/tangly1024/NotionNext/issues/165
* @param {*} e
*/
function fixCopy(e) {
const codeE = e.target.parentElement.parentElement.lastElementChild
console.log(codeE)
const codeEnd = codeE.lastChild
if (codeEnd.nodeName === '#text' && codeE.childNodes.length > 1) {
codeEnd.nodeValue = null
const cards = document?.getElementsByClassName('notion-collection-card')
for (const e of cards) {
e.removeAttribute('href')
}
}
}, 800)

return <div id='container' className='max-w-4xl mx-auto'>
<NotionRenderer
Expand All @@ -83,6 +67,66 @@ const NotionPage = ({ post }) => {
</div>
}

/**
* 监听DOM变化
* @param {*} element
*/
function addWatch4Dom(element) {
// 选择需要观察变动的节点
const targetNode = element || document?.getElementById('container')
// 观察器的配置(需要观察什么变动)
const config = {
attributes: true,
childList: true,
subtree: true
}

// 当观察到变动时执行的回调函数
const mutationCallback = (mutations) => {
for (const mutation of mutations) {
const type = mutation.type
switch (type) {
case 'childList':
if (mutation.target.className === 'notion-code-copy') {
fixCopy(mutation)
}
// console.log('A child node has been added or removed.')
break
case 'attributes':
// console.log(`The ${mutation.attributeName} attribute was modified.`)
// console.log(mutation.attributeName)
break
case 'subtree':
// console.log('The subtree was modified.')
break
default:
break
}
}
}

// 创建一个观察器实例并传入回调函数
const observer = new MutationObserver(mutationCallback)
// console.log(observer)
// 以上述配置开始观察目标节点
observer.observe(targetNode, config)

// observer.disconnect();
}

/**
* 复制代码后,会重复 @see https://github.com/tangly1024/NotionNext/issues/165
* @param {*} e
*/
function fixCopy(e) {
const codeE = e.target.parentElement.lastElementChild
// console.log('2', codeE)
const codeEnd = codeE.lastChild
if (codeEnd.nodeName === '#text' && codeE.childNodes.length > 1) {
codeEnd.nodeValue = null
}
}

/**
* 将id映射成博文内部链接。
* @param {*} id
Expand Down

0 comments on commit 4cd93f2

Please sign in to comment.