-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheditor.tsx
126 lines (112 loc) · 2.86 KB
/
editor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import React, {
FunctionComponent,
memo,
MutableRefObject,
ReactElement,
useCallback,
useEffect,
useRef,
} from 'react'
import EditorJS from '@editorjs/editorjs'
import Paragraph from '@editorjs/paragraph'
import Header from '@editorjs/header'
export interface IEditorJsProps extends EditorJS.EditorConfig {
children?: ReactElement
/**
* Element id where Editor will be append
* @deprecated property will be removed in next major release,
* use holder instead
*/
holderId?: string
/**
* Element id where Editor will be append
*/
holder?: string
/**
* reinitialize editor.js when component did update
*/
reinitializeOnPropsChange?: boolean
/**
* returns the editorjs instance
*/
editorInstance?: (instance: EditorJS) => void
}
const DEFAULT_ID = 'editorjs'
/**
* EditorJs wraps editor.js in a React component and providing an API to be able
* to interact with the editor.js instance.
*/
const EditorJs: FunctionComponent<IEditorJsProps> = (props): ReactElement => {
const {
holderId: deprecatedId,
holder: customHolderId,
editorInstance,
reinitializeOnPropsChange,
children,
tools,
...otherProps
} = props
const instance: MutableRefObject<EditorJS | null> = useRef(null)
const holderId = deprecatedId || customHolderId || DEFAULT_ID
/**
* initialise editorjs with default settings
*/
const initEditor = useCallback(async () => {
if (!instance.current) {
instance.current = new EditorJS({
tools: {
paragraph: {
class: Paragraph,
inlineToolbar: true,
},
header: Header,
...tools,
},
holder: holderId,
...otherProps,
})
}
// callback returns current editorjs instance once it is ready
if (editorInstance) {
await instance.current.isReady
editorInstance(instance.current)
}
}, [editorInstance, holderId, otherProps, tools])
/**
* destroy current editorjs instance
*/
const destroyEditor = useCallback(async () => {
if (!instance.current) {
return true
}
await instance.current.isReady
instance.current.destroy()
instance.current = null
return true
}, [instance])
/**
* initEditor on mount and destroy it on unmount
*/
useEffect(() => {
initEditor()
return (): void => {
destroyEditor()
}
}, []) // eslint-disable-line
/**
* when props change and reinitializeOnPropsChange is true, the component will
* first destroy and then init EditorJS again.
*/
useEffect(() => {
const doEffect = async () => {
if (!reinitializeOnPropsChange) {
return
}
await destroyEditor()
initEditor()
}
doEffect()
}, [destroyEditor, initEditor, instance, reinitializeOnPropsChange])
return children || <div id={holderId} />
}
export default memo(EditorJs)