Skip to content

Commit

Permalink
refactor: Add EditorCore for isomorphic react-editor-js (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jungwoo-An authored Dec 30, 2021
1 parent 307aeb5 commit 26e061c
Show file tree
Hide file tree
Showing 23 changed files with 200 additions and 120 deletions.
20 changes: 15 additions & 5 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified .yarn/install-state.gz
Binary file not shown.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Allow all options of [editor-js](https://github.com/codex-team/editor.js/blob/ma
| ------------------ | ------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| defaulltValue | OutputData | Initial data when using editor js as [uncontrolled component](https://ko.reactjs.org/docs/uncontrolled-components.html). highly recommend it |
| 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. |
| onInitialize | (editorJS?: EditorJS) => void | Call after editor-js is initialized |
| onInitialize | (editorCore?: EditorCore) => void | Call after editor-js is initialized |

## 🧐 FAQ

Expand Down Expand Up @@ -148,17 +148,30 @@ It's simpleeeee

### How to access editor-js instance?

You can access using instanceRef
The editor-js instance is inaccessible. However, you can access the abstracted editor-js for isomorphic react-editor-js.

```ts
// abstracted editor-js interface
interface EditorCore {
destroy(): Promise<void>

clear(): Promise<void>

save(): Promise<OutputData>

render(data: OutputData): Promise<void>
}
```

```tsx
const editorJS = React.useRef(null)
const editorCore = React.useRef(null)

const handleInitialize = React.useCallback((instance) => {
editorJS.current = instance
editorCore.current = instance
}, [])

const handleSave = React.useCallback(() => {
const savedData = await editorJS.current.save();
const savedData = await editorCore.current.save();
}, [])

<ReactEditorJS onInitialize={handleInitialize} defaultValue={blocks} />
Expand Down
3 changes: 3 additions & 0 deletions packages/@react-editor-js/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
},
"peerDependencies": {
"@editorjs/editorjs": "*",
"@editorjs/paragraph": "*",
"react": "*"
},
"dependencies": {
"@react-editor-js/core": "2.0.5"
},
"devDependencies": {
"@editorjs/editorjs": "*",
"@editorjs/paragraph": "*",
"@types/react": "*",
"react": "*",
"tslib": "^2.3.1",
"typescript": "^4.3.5"
}
}
8 changes: 0 additions & 8 deletions packages/@react-editor-js/client/src/ClientEditorJSFactory.ts

This file was deleted.

8 changes: 6 additions & 2 deletions packages/@react-editor-js/client/src/ReactEditorJSClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import {
ReactEditorJS,
} from '@react-editor-js/core'

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

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

function ReactEditorJSClient(props: Props) {
const factory = React.useMemo(() => new ClientEditorJSFactory(), [])
const factory = React.useCallback(
(config: EditorConfig) => new ClientEditorCore(config),
[]
)

return <ReactEditorJS factory={factory} {...props} />
}
Expand Down
39 changes: 39 additions & 0 deletions packages/@react-editor-js/client/src/client-editor-core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import EditorJS, { EditorConfig, OutputData } from '@editorjs/editorjs'
import Paragraph from '@editorjs/paragraph'
import { EditorCore } from '@react-editor-js/core'

export class ClientEditorCore implements EditorCore {
private _editorJS: EditorJS

constructor({ tools, ...config }: EditorConfig) {
const extendTools = {
// default tools
paragraph: {
class: Paragraph,
inlineToolbar: true,
},
...tools,
}

this._editorJS = new EditorJS({
tools: extendTools,
...config,
})
}

public async clear() {
await this._editorJS.clear()
}

public async save() {
return this._editorJS.save()
}

public async destroy() {
await this._editorJS.destroy()
}

public async render(data: OutputData) {
await this._editorJS.render(data)
}
}
2 changes: 0 additions & 2 deletions packages/@react-editor-js/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
},
"peerDependencies": {
"@editorjs/editorjs": "*",
"@editorjs/paragraph": "*",
"react": "*"
},
"devDependencies": {
"@editorjs/editorjs": "*",
"@editorjs/paragraph": "*",
"@testing-library/react": "^12.1.2",
"@types/jest": "^27.0.3",
"@types/react": "*",
Expand Down
23 changes: 6 additions & 17 deletions packages/@react-editor-js/core/src/ReactEditorJS.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React from 'react'
import { Props } from '@react-editor-js/core'
import EditorJS from '@editorjs/editorjs'
import Paragraph from '@editorjs/paragraph'

import { Props } from './component-types'
import { EditorCore } from './editor-core'

function ReactEditorJS({
factory,
holder,
tools,
defaultValue,
children,
value,
Expand All @@ -18,20 +17,10 @@ function ReactEditorJS({
holder ?? `react-editor-js-${Date.now().toString(16)}`
)

const editorJS = React.useRef<EditorJS | null>(null)
const editorJS = React.useRef<EditorCore | null>(null)

React.useEffect(() => {
const extendTools = {
// default tools
paragraph: {
class: Paragraph,
inlineToolbar: true,
},
...tools,
}

editorJS.current = factory.create({
tools: extendTools,
editorJS.current = factory({
holder: memoizedHolder.current,
...(defaultValue && { data: defaultValue }),
...restProps,
Expand All @@ -46,7 +35,7 @@ function ReactEditorJS({

React.useEffect(() => {
if (value) {
editorJS.current?.blocks.render(value)
editorJS.current?.render(value)
}
}, [value])

Expand Down
9 changes: 5 additions & 4 deletions packages/@react-editor-js/core/src/component-types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import React from 'react'
import EditorJS, { EditorConfig } from '@editorjs/editorjs'
import { EditorConfig } from '@editorjs/editorjs'

import { EditorJSFactory } from './factory'
import { EditorCoreFactory } from './factory'
import { EditorCore } from './editor-core'

export interface Props extends Omit<EditorConfig, 'data'> {
factory: EditorJSFactory
factory: EditorCoreFactory

holder?: string
children?: React.ReactElement
value?: EditorConfig['data']
defaultValue?: EditorConfig['data']

onInitialize?: (editorJS: EditorJS) => void
onInitialize?: (core: EditorCore) => void
}
11 changes: 11 additions & 0 deletions packages/@react-editor-js/core/src/editor-core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { OutputData } from '@editorjs/editorjs'

export interface EditorCore {
destroy(): Promise<void>

clear(): Promise<void>

save(): Promise<OutputData>

render(data: OutputData): Promise<void>
}
8 changes: 4 additions & 4 deletions packages/@react-editor-js/core/src/factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import EditorJS, { EditorConfig } from '@editorjs/editorjs'
import { EditorConfig } from '@editorjs/editorjs'

export interface EditorJSFactory {
create(config?: EditorConfig | string): EditorJS
}
import { EditorCore } from './editor-core'

export type EditorCoreFactory = (config: EditorConfig) => EditorCore
1 change: 1 addition & 0 deletions packages/@react-editor-js/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './component-types'
export * from './factory'
export * from './editor-core'

export { default as ReactEditorJS } from './ReactEditorJS'
15 changes: 0 additions & 15 deletions packages/@react-editor-js/core/tests/EditorJSStub.ts

This file was deleted.

18 changes: 0 additions & 18 deletions packages/@react-editor-js/core/tests/EditorJSStubFactory.ts

This file was deleted.

Loading

0 comments on commit 26e061c

Please sign in to comment.