Skip to content

Commit 2f76a73

Browse files
committed
移除Preact框架、引入react-hook检查
1 parent c527673 commit 2f76a73

File tree

12 files changed

+54
-171
lines changed

12 files changed

+54
-171
lines changed

.eslintrc.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@ module.exports = {
1616
ecmaVersion: 12,
1717
sourceType: 'module'
1818
},
19-
plugins: ['react'],
19+
plugins: [
20+
'react',
21+
'react-hooks'
22+
],
2023
settings: {
2124
react: {
2225
version: 'detect'
2326
}
2427
},
2528
rules: {
2629
'react/prop-types': 'off',
27-
'space-before-function-paren': 0
30+
'space-before-function-paren': 0,
31+
'react-hooks/rules-of-hooks': 'error' // Checks rules of Hooks
2832
},
2933
globals: {
3034
React: true

components/Comment.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ const ValineComponent = dynamic(() => import('@/components/ValineComponent'), {
4343
})
4444

4545
const Comment = ({ frontMatter }) => {
46-
if (!frontMatter) {
47-
return <>Loading...</>
48-
}
4946
const { isDarkMode } = useGlobal()
5047
const router = useRouter()
5148

@@ -61,6 +58,10 @@ const Comment = ({ frontMatter }) => {
6158
}, 200)
6259
}, [])
6360

61+
if (!frontMatter) {
62+
return <>Loading...</>
63+
}
64+
6465
return (
6566
<div id='comment' className='comment mt-5 text-gray-800 dark:text-gray-300'>
6667
<Tabs>

components/Live2D.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@
22
import BLOG from '@/blog.config'
33
import { useGlobal } from '@/lib/global'
44
import { loadExternalResource } from '@/lib/utils'
5-
import { useEffect } from 'react'
5+
import React from 'react'
66

77
export default function Live2D() {
8-
if (!BLOG.WIDGET_PET || !JSON.parse(BLOG.WIDGET_PET)) {
9-
return <></>
10-
}
118
const { switchTheme } = useGlobal()
129

10+
React.useEffect(() => {
11+
window.addEventListener('scroll', initLive2D)
12+
return () => {
13+
window.removeEventListener('scroll', initLive2D)
14+
}
15+
}, [])
16+
1317
function handleClick() {
1418
if (BLOG.WIDGET_PET_SWITCH_THEME) {
1519
switchTheme()
1620
}
1721
}
1822

19-
useEffect(() => {
20-
window.addEventListener('scroll', initLive2D)
21-
return () => {
22-
window.removeEventListener('scroll', initLive2D)
23-
}
24-
}, [])
23+
if (!BLOG.WIDGET_PET || !JSON.parse(BLOG.WIDGET_PET)) {
24+
return <></>
25+
}
2526

2627
return <canvas id="live2d" className='cursor-pointer' width="280" height="250" onClick={handleClick} alt='切换主题' title='切换主题' />
2728
}

components/NotionPage.js

+7-68
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ import 'prismjs/components/prism-r.js'
4545

4646
// 化学方程式
4747
import '@/lib/mhchem'
48-
// 页面渲染观察者
49-
let observer
5048

5149
// https://github.com/txs
5250
// import PrismMac from '@/components/PrismMac'
@@ -63,10 +61,6 @@ const Modal = dynamic(
6361
)
6462

6563
const NotionPage = ({ post }) => {
66-
if (!post || !post.blockMap) {
67-
return <>{post?.summary || ''}</>
68-
}
69-
7064
const zoom = isBrowser() && mediumZoom({
7165
container: '.notion-viewport',
7266
background: 'rgba(0, 0, 0, 0.2)',
@@ -76,10 +70,6 @@ const NotionPage = ({ post }) => {
7670

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

79-
if (isBrowser() && !observer) {
80-
addWatch4Dom()
81-
}
82-
8373
React.useEffect(() => {
8474
setTimeout(() => {
8575
if (window.location.hash) {
@@ -109,8 +99,11 @@ const NotionPage = ({ post }) => {
10999
}, 800)
110100
}, [])
111101

102+
if (!post || !post.blockMap) {
103+
return <>{post?.summary || ''}</>
104+
}
105+
112106
return <div id='container' className='max-w-5xl overflow-x-visible mx-auto'>
113-
<PrismMac />
114107
<NotionRenderer
115108
recordMap={post.blockMap}
116109
mapPageUrl={mapPageUrl}
@@ -123,6 +116,9 @@ const NotionPage = ({ post }) => {
123116
nextImage: Image,
124117
nextLink: Link
125118
}} />
119+
120+
<PrismMac />
121+
126122
</div>
127123
}
128124

@@ -154,61 +150,4 @@ function getMediumZoomMargin() {
154150
}
155151
}
156152

157-
/**
158-
* 监听DOM变化
159-
* @param {*} element
160-
*/
161-
// eslint-disable-next-line no-unused-vars
162-
function addWatch4Dom(element) {
163-
// 选择需要观察变动的节点
164-
const targetNode = element || document?.getElementById('container')
165-
// 观察器的配置(需要观察什么变动)
166-
const config = {
167-
attributes: true,
168-
childList: true,
169-
subtree: true
170-
}
171-
172-
// 创建一个观察器实例并传入回调函数
173-
observer = new MutationObserver(mutationCallback)
174-
// console.log('观察节点变化', observer)
175-
// 以上述配置开始观察目标节点
176-
if (targetNode) {
177-
observer.observe(targetNode, config)
178-
}
179-
180-
// observer.disconnect()
181-
}
182-
183-
// 当页面发生时会调用此函数
184-
const mutationCallback = (mutations) => {
185-
for (const mutation of mutations) {
186-
const type = mutation.type
187-
switch (type) {
188-
case 'childList':
189-
// console.log('A child node has been added or removed.', mutation)
190-
if (mutation.addedNodes.length > 0) {
191-
if (mutation?.target?.parentElement?.nodeName === 'PRE' || mutation?.target?.parentElement?.nodeName === 'CODE') {
192-
if (mutation.addedNodes[0].nodeName === '#text' && mutation.target.childElementCount > 0) {
193-
mutation.addedNodes[0].remove() // 移除新增的内容
194-
}
195-
}
196-
if (mutation?.target?.className === 'language-mermaid') {
197-
// mermaid重刷新bug TODO
198-
}
199-
}
200-
break
201-
case 'attributes':
202-
// console.log(`The ${mutation.attributeName} attribute was modified.`, mutation.target)
203-
// console.log(mutation.attributeName)
204-
break
205-
case 'subtree':
206-
// console.log('The subtree was modified.', mutation.target)
207-
break
208-
default:
209-
break
210-
}
211-
}
212-
}
213-
214153
export default NotionPage

components/PrismMac.js

-61
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import 'prismjs/plugins/line-numbers/prism-line-numbers.css'
1111
import mermaid from 'mermaid'
1212
import { useGlobal } from '@/lib/global'
1313
import { useRouter } from 'next/router'
14-
import { isBrowser } from '@/lib/utils'
15-
// 页面渲染观察者
16-
let observer
1714

1815
/**
1916
* @author https://github.com/txs/
@@ -24,10 +21,6 @@ const PrismMac = () => {
2421
const { isDarkMode } = useGlobal()
2522
const scrollTop = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop
2623

27-
if (isBrowser() && !observer) {
28-
addWatch4Dom()
29-
}
30-
3124
React.useEffect(() => {
3225
renderPrismMac()
3326
window.scrollTo(0, scrollTop)
@@ -111,58 +104,4 @@ function renderPrismMac() {
111104
}
112105
}
113106

114-
/**
115-
* 监听DOM变化
116-
* @param {*} element
117-
*/
118-
// eslint-disable-next-line no-unused-vars
119-
function addWatch4Dom(element) {
120-
// 选择需要观察变动的节点
121-
const targetNode = element || document?.getElementById('container')
122-
// 观察器的配置(需要观察什么变动)
123-
const config = {
124-
attributes: true,
125-
childList: true,
126-
subtree: true
127-
}
128-
129-
// 创建一个观察器实例并传入回调函数
130-
observer = new MutationObserver(mutationCallback)
131-
// console.log('观察节点变化', observer)
132-
// 以上述配置开始观察目标节点
133-
if (targetNode) {
134-
observer.observe(targetNode, config)
135-
}
136-
137-
// observer.disconnect()
138-
}
139-
140-
// 当页面发生时会调用此函数
141-
const mutationCallback = (mutations) => {
142-
for (const mutation of mutations) {
143-
const type = mutation.type
144-
switch (type) {
145-
case 'childList':
146-
// console.log('A child node has been added or removed.', mutation)
147-
if (mutation.addedNodes.length > 0) {
148-
if (mutation?.target?.parentElement?.nodeName === 'PRE' || mutation?.target?.parentElement?.nodeName === 'CODE') {
149-
if (mutation.addedNodes[0].nodeName === '#text') {
150-
mutation.addedNodes[0].remove() // 移除新增的内容
151-
}
152-
}
153-
}
154-
break
155-
case 'attributes':
156-
// console.log(`The ${mutation.attributeName} attribute was modified.`, mutation.target)
157-
// console.log(mutation.attributeName)
158-
break
159-
case 'subtree':
160-
// console.log('The subtree was modified.', mutation.target)
161-
break
162-
default:
163-
break
164-
}
165-
}
166-
}
167-
168107
export default PrismMac

components/Tabs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import React, { useState } from 'react'
66
* @returns
77
*/
88
const Tabs = ({ className, children }) => {
9+
const [currentTab, setCurrentTab] = useState(0)
10+
911
if (!children) {
1012
return <></>
1113
}
@@ -29,8 +31,6 @@ const Tabs = ({ className, children }) => {
2931
</section>
3032
}
3133

32-
const [currentTab, setCurrentTab] = useState(0)
33-
3434
function tabClickHandle (i) {
3535
setCurrentTab(i)
3636
}

next.config.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const withBundleAnalyzer = require('@next/bundle-analyzer')({
22
enabled: process.env.ANALYZE === 'true'
33
})
4+
45
module.exports = withBundleAnalyzer({
56
future: {
67
webpack5: true
@@ -38,13 +39,13 @@ module.exports = withBundleAnalyzer({
3839
},
3940
webpack: (config, { dev, isServer }) => {
4041
// Replace React with Preact only in client production build
41-
if (!dev && !isServer) {
42-
Object.assign(config.resolve.alias, {
43-
react: 'preact/compat',
44-
'react-dom/test-utils': 'preact/test-utils',
45-
'react-dom': 'preact/compat'
46-
})
47-
}
42+
// if (!dev && !isServer) {
43+
// Object.assign(config.resolve.alias, {
44+
// react: 'preact/compat',
45+
// 'react-dom/test-utils': 'preact/test-utils',
46+
// 'react-dom': 'preact/compat'
47+
// })
48+
// }
4849
return config
4950
}
5051
})

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"animejs": "^3.2.1",
2828
"axios": ">=0.21.1",
2929
"copy-to-clipboard": "^3.3.1",
30+
"eslint-plugin-react-hooks": "^4.6.0",
3031
"feed": "^4.2.2",
3132
"gitalk": "^1.7.2",
3233
"localStorage": "^1.0.4",

pages/[...slug].js

+11-10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ const Slug = props => {
2020
const { post, siteInfo } = props
2121
const router = Router.useRouter()
2222

23+
// 文章锁🔐
24+
const [lock, setLock] = React.useState(true)
25+
26+
React.useEffect(() => {
27+
if (post?.password && post?.password !== '') {
28+
setLock(true)
29+
} else {
30+
setLock(false)
31+
}
32+
}, [post])
33+
2334
if (!post) {
2435
changeLoadingState(true)
2536
setTimeout(() => {
@@ -38,16 +49,6 @@ const Slug = props => {
3849

3950
changeLoadingState(false)
4051

41-
// 文章锁🔐
42-
const [lock, setLock] = React.useState(post?.password && post?.password !== '')
43-
React.useEffect(() => {
44-
if (post?.password && post?.password !== '') {
45-
setLock(true)
46-
} else {
47-
setLock(false)
48-
}
49-
}, [post])
50-
5152
/**
5253
* 验证文章密码
5354
* @param {*} result

pages/search/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import * as ThemeMap from '@/themes'
55

66
const Search = props => {
77
const { posts, siteInfo } = props
8+
const router = useRouter()
89
let filteredPosts
9-
const searchKey = getSearchKey()
10+
const searchKey = getSearchKey(router)
1011
// 静态过滤
1112
if (searchKey) {
1213
filteredPosts = posts.filter(post => {
@@ -61,8 +62,7 @@ export async function getStaticProps() {
6162
}
6263
}
6364

64-
function getSearchKey() {
65-
const router = useRouter()
65+
function getSearchKey(router) {
6666
if (router.query && router.query.s) {
6767
return router.query.s
6868
}

styles/prism-mac-style.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
.code-toolbar {
55
position: relative;
66
box-shadow: 0 10px 30px 0 rgba(0, 0, 0, .4);
7-
@apply mb-8 pt-6 w-full rounded-lg bg-black
7+
@apply mb-8 mt-4 pt-6 w-full rounded-lg bg-black
88
}
99

1010
.toolbar {

0 commit comments

Comments
 (0)