Skip to content

Commit 0fa417b

Browse files
committed
remove usage of onChange mehod in editorjs
Get content from editor.save at submit time and let editorjs handle it's state. Also add a variable to handle double rendring of editor in strict mode in local
1 parent f1878aa commit 0fa417b

File tree

3 files changed

+61
-51
lines changed

3 files changed

+61
-51
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
"dependencies": {
1919
"@editorjs/code": "^2.7.0",
20-
"@editorjs/editorjs": "^2.24.3",
20+
"@editorjs/editorjs": "^2.26.4",
2121
"@editorjs/header": "^2.6.2",
2222
"@editorjs/image": "^2.6.2",
2323
"@editorjs/list": "^1.7.0",

src/components/story/StoryEditor.tsx

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import EditorJS, { OutputData } from '@editorjs/editorjs'
44
import { stripHtml } from 'string-strip-html'
5-
import React, { useEffect, useRef } from 'react'
5+
import React, { useCallback, useEffect, useRef, useState } from 'react'
66
import {
77
Environment,
88
graphql,
@@ -74,6 +74,7 @@ type EditorProps = {
7474
body?: string
7575
requestCookie?: string | null
7676
}
77+
let isEditorInitialized = false
7778

7879
export default function StoryEditor({
7980
editable,
@@ -89,38 +90,42 @@ export default function StoryEditor({
8990
}
9091

9192
function Editor({ editable, body }: EditorProps) {
92-
const ejInstance = useRef<EditorJS | null>()
93+
const editorRef = useRef<EditorJS>()
9394

94-
const [editorData, setEditorData] = React.useState(
95+
const [editorData] = useState(
9596
(body && JSON.parse(body)) || DEFAULT_INITIAL_DATA
9697
)
97-
useEffect(() => {
98-
if (!ejInstance.current) {
99-
initEditor()
100-
}
101-
return () => {
102-
ejInstance?.current?.destroy()
103-
ejInstance.current = null
104-
}
105-
}, [])
10698

107-
const initEditor = () => {
99+
const getEditorContent = useCallback(async () => {
100+
const content = await editorRef.current?.save()
101+
return content
102+
}, [])
103+
const initEditor = useCallback(() => {
108104
const editor = new EditorJS({
109105
holder: EDITTOR_HOLDER_ID,
110106
data: editorData,
107+
placeholder: 'title',
108+
defaultBlock: 'paragraph',
111109
onReady: () => {
112-
ejInstance.current = editor
113-
},
114-
onChange: async () => {
115-
const content = await editor.saver.save()
116-
setEditorData(content)
110+
editorRef.current = editor
117111
},
118112
autofocus: true,
119113
tools: EDITOR_JS_TOOLS,
120114
inlineToolbar: true,
121115
readOnly: !editable,
122116
})
123-
}
117+
}, [editable, editorData])
118+
useEffect(() => {
119+
if (!editorRef.current && !isEditorInitialized) {
120+
initEditor()
121+
isEditorInitialized = true
122+
}
123+
return () => {
124+
editorRef?.current?.destroy()
125+
editorRef.current = undefined
126+
}
127+
}, [editorRef, initEditor])
128+
124129
const [commitMutation] = useMutation(
125130
graphql`
126131
mutation StoryEditorCreateMutation($input: StoryInput!) {
@@ -130,23 +135,26 @@ function Editor({ editable, body }: EditorProps) {
130135
}
131136
`
132137
)
133-
const handleStorySubmit = () => {
134-
const postTitle = getTitle(editorData)
135-
commitMutation({
136-
variables: {
137-
input: {
138-
title: postTitle,
139-
contentJson: JSON.stringify(editorData),
140-
abstractContent: getAbstract(editorData),
141-
urlSuffix: postTitle
142-
.toLowerCase()
143-
.replaceAll(' ', '-')
144-
.substring(0, 32),
145-
thumbnail: getThubnail(editorData),
138+
const handleStorySubmit = useCallback(async () => {
139+
const editorContent = await getEditorContent()
140+
if (editorContent) {
141+
const postTitle = getTitle(editorContent)
142+
commitMutation({
143+
variables: {
144+
input: {
145+
title: postTitle,
146+
contentJson: JSON.stringify(editorContent),
147+
abstractContent: getAbstract(editorContent),
148+
urlSuffix: postTitle
149+
.toLowerCase()
150+
.replaceAll(' ', '-')
151+
.substring(0, 32),
152+
thumbnail: getThubnail(editorContent),
153+
},
146154
},
147-
},
148-
})
149-
}
155+
})
156+
}
157+
}, [commitMutation, getEditorContent])
150158
return (
151159
<Card classes="prose max-w-none dark:prose-invert">
152160
<div className="prose-code">

yarn.lock

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,11 @@
12691269
exec-sh "^0.3.2"
12701270
minimist "^1.2.0"
12711271

1272+
"@codexteam/[email protected]":
1273+
version "0.1.0"
1274+
resolved "https://registry.yarnpkg.com/@codexteam/icons/-/icons-0.1.0.tgz#a02885fe8699f69902d05b077b5f1cd48a2ca6b9"
1275+
integrity sha512-jW1fWnwtWzcP4FBGsaodbJY3s1ZaRU+IJy1pvJ7ygNQxkQinybJcwXoyt0a5mWwu/4w30A42EWhCrZn8lp4fdw==
1276+
12721277
12731278
version "1.5.0"
12741279
resolved "https://registry.yarnpkg.com/@colors/colors/-/colors-1.5.0.tgz#bb504579c1cae923e6576a4f5da43d25f97bdbd9"
@@ -1330,13 +1335,15 @@
13301335
resolved "https://registry.npmjs.org/@editorjs/code/-/code-2.7.0.tgz"
13311336
integrity sha512-gXtTce915fHp3H9i4IqhTxEDbbkT2heFfYiW/bhFHsCmZDpyGzfZxi94kmrEqDmbxXjV49ZZ6GZbR26If13KJw==
13321337

1333-
"@editorjs/editorjs@^2.24.3":
1334-
version "2.24.3"
1335-
resolved "https://registry.npmjs.org/@editorjs/editorjs/-/editorjs-2.24.3.tgz"
1336-
integrity sha512-VzrWaQ7mggNUAPTDGcqXJNIlBZH3S2IqsIUGA43UM2Q9VFaeS5KuVFVOTrFJvAzF7G+vZTO52ocm+hrDhTwvyw==
1338+
"@editorjs/editorjs@^2.26.4":
1339+
version "2.26.4"
1340+
resolved "https://registry.yarnpkg.com/@editorjs/editorjs/-/editorjs-2.26.4.tgz#097e29341501d81cd1721604a74da1530f5ff74f"
1341+
integrity sha512-yuJ2NM1Y5+8DDNWr4C00tHMVHKy0uCqK1HS4pwFPVcXUawgJnU2Li2vKGvPLuf0Jj2ir7MVgADO6oC1TbYBOYQ==
13371342
dependencies:
1343+
"@codexteam/icons" "0.1.0"
13381344
codex-notifier "^1.1.2"
13391345
codex-tooltip "^1.0.5"
1346+
html-janitor "^2.0.4"
13401347
nanoid "^3.1.22"
13411348

13421349
"@editorjs/header@^2.6.2":
@@ -4540,17 +4547,7 @@ camelcase@^6.2.0:
45404547
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.3.0.tgz#5685b95eb209ac9c0c177467778c9c84df58ba9a"
45414548
integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==
45424549

4543-
caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001400:
4544-
version "1.0.30001444"
4545-
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001444.tgz"
4546-
integrity sha512-ecER9xgJQVMqcrxThKptsW0pPxSae8R2RB87LNa+ivW9ppNWRHEplXcDzkCOP4LYWGj8hunXLqaiC41iBATNyg==
4547-
4548-
caniuse-lite@^1.0.30001297, caniuse-lite@^1.0.30001312:
4549-
version "1.0.30001444"
4550-
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001444.tgz"
4551-
integrity sha512-ecER9xgJQVMqcrxThKptsW0pPxSae8R2RB87LNa+ivW9ppNWRHEplXcDzkCOP4LYWGj8hunXLqaiC41iBATNyg==
4552-
4553-
caniuse-lite@^1.0.30001406:
4550+
caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001297, caniuse-lite@^1.0.30001312, caniuse-lite@^1.0.30001400, caniuse-lite@^1.0.30001406:
45544551
version "1.0.30001444"
45554552
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001444.tgz"
45564553
integrity sha512-ecER9xgJQVMqcrxThKptsW0pPxSae8R2RB87LNa+ivW9ppNWRHEplXcDzkCOP4LYWGj8hunXLqaiC41iBATNyg==
@@ -6952,6 +6949,11 @@ html-escaper@^2.0.0:
69526949
resolved "https://registry.yarnpkg.com/html-escaper/-/html-escaper-2.0.2.tgz#dfd60027da36a36dfcbe236262c00a5822681453"
69536950
integrity sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==
69546951

6952+
html-janitor@^2.0.4:
6953+
version "2.0.4"
6954+
resolved "https://registry.yarnpkg.com/html-janitor/-/html-janitor-2.0.4.tgz#ae5a115cdf3331cd5501edd7b5471b18ea44cdbb"
6955+
integrity sha512-92J5h9jNZRk30PMHapjHEJfkrBWKCOy0bq3oW2pBungky6lzYSoboBGPMvxl1XRKB2q+kniQmsLsPbdpY7RM2g==
6956+
69556957
html-minifier-terser@^5.0.1:
69566958
version "5.1.1"
69576959
resolved "https://registry.yarnpkg.com/html-minifier-terser/-/html-minifier-terser-5.1.1.tgz#922e96f1f3bb60832c2634b79884096389b1f054"

0 commit comments

Comments
 (0)