mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-31 07:26:43 +00:00
代码copy机制调整
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { NotionRenderer } from 'react-notion-x'
|
import { NotionRenderer } from 'react-notion-x'
|
||||||
import dynamic from 'next/dynamic'
|
import dynamic from 'next/dynamic'
|
||||||
|
import React from 'react'
|
||||||
|
|
||||||
const Code = dynamic(() =>
|
const Code = dynamic(() =>
|
||||||
import('react-notion-x/build/third-party/code').then((m) => m.Code), { ssr: false }
|
import('react-notion-x/build/third-party/code').then((m) => m.Code), { ssr: false }
|
||||||
@@ -19,33 +20,15 @@ const Pdf = dynamic(
|
|||||||
const Modal = dynamic(
|
const Modal = dynamic(
|
||||||
() => import('react-notion-x/build/third-party/modal').then((m) => m.Modal), { ssr: false }
|
() => import('react-notion-x/build/third-party/modal').then((m) => m.Modal), { ssr: false }
|
||||||
)
|
)
|
||||||
|
|
||||||
const NotionPage = ({ post }) => {
|
const NotionPage = ({ post }) => {
|
||||||
if (!post || !post.blockMap) {
|
if (!post || !post.blockMap) {
|
||||||
return <>{post?.summary || ''}</>
|
return <>{post?.summary || ''}</>
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(() => {
|
React.useEffect(() => {
|
||||||
if (typeof document === 'undefined') {
|
addWatch4Dom()
|
||||||
return
|
})
|
||||||
}
|
|
||||||
const buttons = document.getElementsByClassName('notion-code-copy')
|
|
||||||
for (const e of buttons) {
|
|
||||||
e.addEventListener('click', fixCopy)
|
|
||||||
}
|
|
||||||
}, 500)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 复制代码后,会重复 @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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return <div id='container'>
|
return <div id='container'>
|
||||||
<NotionRenderer
|
<NotionRenderer
|
||||||
@@ -61,6 +44,69 @@ const NotionPage = ({ post }) => {
|
|||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default NotionPage
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听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') {
|
||||||
|
// console.log('1', mutation)
|
||||||
|
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映射成博文内部链接。
|
* 将id映射成博文内部链接。
|
||||||
* @param {*} id
|
* @param {*} id
|
||||||
@@ -70,5 +116,3 @@ const mapPageUrl = id => {
|
|||||||
// return 'https://www.notion.so/' + id.replace(/-/g, '')
|
// return 'https://www.notion.so/' + id.replace(/-/g, '')
|
||||||
return '/article/' + id.replace(/-/g, '')
|
return '/article/' + id.replace(/-/g, '')
|
||||||
}
|
}
|
||||||
|
|
||||||
export default NotionPage
|
|
||||||
|
|||||||
Reference in New Issue
Block a user