Skip to content

Commit 26e061c

Browse files
authored
refactor: Add EditorCore for isomorphic react-editor-js (#178)
1 parent 307aeb5 commit 26e061c

23 files changed

+200
-120
lines changed

.pnp.cjs

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.yarn/install-state.gz

405 Bytes
Binary file not shown.

README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Allow all options of [editor-js](https://github.com/codex-team/editor.js/blob/ma
6060
| ------------------ | ------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
6161
| defaulltValue | OutputData | Initial data when using editor js as [uncontrolled component](https://ko.reactjs.org/docs/uncontrolled-components.html). highly recommend it |
6262
| value | OutputData | data when using editor js as [controlled component](https://ko.reactjs.org/docs/forms.html#controlled-components). <br> ⚠️ Don't use it with onChange prop. Infinite loops can occur. |
63-
| onInitialize | (editorJS?: EditorJS) => void | Call after editor-js is initialized |
63+
| onInitialize | (editorCore?: EditorCore) => void | Call after editor-js is initialized |
6464

6565
## 🧐 FAQ
6666

@@ -148,17 +148,30 @@ It's simpleeeee
148148

149149
### How to access editor-js instance?
150150

151-
You can access using instanceRef
151+
The editor-js instance is inaccessible. However, you can access the abstracted editor-js for isomorphic react-editor-js.
152+
153+
```ts
154+
// abstracted editor-js interface
155+
interface EditorCore {
156+
destroy(): Promise<void>
157+
158+
clear(): Promise<void>
159+
160+
save(): Promise<OutputData>
161+
162+
render(data: OutputData): Promise<void>
163+
}
164+
```
152165

153166
```tsx
154-
const editorJS = React.useRef(null)
167+
const editorCore = React.useRef(null)
155168

156169
const handleInitialize = React.useCallback((instance) => {
157-
editorJS.current = instance
170+
editorCore.current = instance
158171
}, [])
159172

160173
const handleSave = React.useCallback(() => {
161-
const savedData = await editorJS.current.save();
174+
const savedData = await editorCore.current.save();
162175
}, [])
163176

164177
<ReactEditorJS onInitialize={handleInitialize} defaultValue={blocks} />

packages/@react-editor-js/client/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
},
1515
"peerDependencies": {
1616
"@editorjs/editorjs": "*",
17+
"@editorjs/paragraph": "*",
1718
"react": "*"
1819
},
1920
"dependencies": {
2021
"@react-editor-js/core": "2.0.5"
2122
},
2223
"devDependencies": {
2324
"@editorjs/editorjs": "*",
25+
"@editorjs/paragraph": "*",
2426
"@types/react": "*",
2527
"react": "*",
28+
"tslib": "^2.3.1",
2629
"typescript": "^4.3.5"
2730
}
2831
}

packages/@react-editor-js/client/src/ClientEditorJSFactory.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/@react-editor-js/client/src/ReactEditorJSClient.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import {
44
ReactEditorJS,
55
} from '@react-editor-js/core'
66

7-
import { ClientEditorJSFactory } from './ClientEditorJSFactory'
7+
import { ClientEditorCore } from './client-editor-core'
8+
import { EditorConfig } from '@editorjs/editorjs'
89

910
export type Props = Omit<ReactEditorJSProps, 'factory'>
1011

1112
function ReactEditorJSClient(props: Props) {
12-
const factory = React.useMemo(() => new ClientEditorJSFactory(), [])
13+
const factory = React.useCallback(
14+
(config: EditorConfig) => new ClientEditorCore(config),
15+
[]
16+
)
1317

1418
return <ReactEditorJS factory={factory} {...props} />
1519
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import EditorJS, { EditorConfig, OutputData } from '@editorjs/editorjs'
2+
import Paragraph from '@editorjs/paragraph'
3+
import { EditorCore } from '@react-editor-js/core'
4+
5+
export class ClientEditorCore implements EditorCore {
6+
private _editorJS: EditorJS
7+
8+
constructor({ tools, ...config }: EditorConfig) {
9+
const extendTools = {
10+
// default tools
11+
paragraph: {
12+
class: Paragraph,
13+
inlineToolbar: true,
14+
},
15+
...tools,
16+
}
17+
18+
this._editorJS = new EditorJS({
19+
tools: extendTools,
20+
...config,
21+
})
22+
}
23+
24+
public async clear() {
25+
await this._editorJS.clear()
26+
}
27+
28+
public async save() {
29+
return this._editorJS.save()
30+
}
31+
32+
public async destroy() {
33+
await this._editorJS.destroy()
34+
}
35+
36+
public async render(data: OutputData) {
37+
await this._editorJS.render(data)
38+
}
39+
}

packages/@react-editor-js/core/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414
},
1515
"peerDependencies": {
1616
"@editorjs/editorjs": "*",
17-
"@editorjs/paragraph": "*",
1817
"react": "*"
1918
},
2019
"devDependencies": {
2120
"@editorjs/editorjs": "*",
22-
"@editorjs/paragraph": "*",
2321
"@testing-library/react": "^12.1.2",
2422
"@types/jest": "^27.0.3",
2523
"@types/react": "*",

packages/@react-editor-js/core/src/ReactEditorJS.tsx

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import React from 'react'
2-
import { Props } from '@react-editor-js/core'
3-
import EditorJS from '@editorjs/editorjs'
4-
import Paragraph from '@editorjs/paragraph'
2+
3+
import { Props } from './component-types'
4+
import { EditorCore } from './editor-core'
55

66
function ReactEditorJS({
77
factory,
88
holder,
9-
tools,
109
defaultValue,
1110
children,
1211
value,
@@ -18,20 +17,10 @@ function ReactEditorJS({
1817
holder ?? `react-editor-js-${Date.now().toString(16)}`
1918
)
2019

21-
const editorJS = React.useRef<EditorJS | null>(null)
20+
const editorJS = React.useRef<EditorCore | null>(null)
2221

2322
React.useEffect(() => {
24-
const extendTools = {
25-
// default tools
26-
paragraph: {
27-
class: Paragraph,
28-
inlineToolbar: true,
29-
},
30-
...tools,
31-
}
32-
33-
editorJS.current = factory.create({
34-
tools: extendTools,
23+
editorJS.current = factory({
3524
holder: memoizedHolder.current,
3625
...(defaultValue && { data: defaultValue }),
3726
...restProps,
@@ -46,7 +35,7 @@ function ReactEditorJS({
4635

4736
React.useEffect(() => {
4837
if (value) {
49-
editorJS.current?.blocks.render(value)
38+
editorJS.current?.render(value)
5039
}
5140
}, [value])
5241

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import React from 'react'
2-
import EditorJS, { EditorConfig } from '@editorjs/editorjs'
2+
import { EditorConfig } from '@editorjs/editorjs'
33

4-
import { EditorJSFactory } from './factory'
4+
import { EditorCoreFactory } from './factory'
5+
import { EditorCore } from './editor-core'
56

67
export interface Props extends Omit<EditorConfig, 'data'> {
7-
factory: EditorJSFactory
8+
factory: EditorCoreFactory
89

910
holder?: string
1011
children?: React.ReactElement
1112
value?: EditorConfig['data']
1213
defaultValue?: EditorConfig['data']
1314

14-
onInitialize?: (editorJS: EditorJS) => void
15+
onInitialize?: (core: EditorCore) => void
1516
}

0 commit comments

Comments
 (0)