Skip to content

Commit

Permalink
feat: Add dangerouslyLowLevelInstance (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jungwoo-An authored Jul 2, 2022
1 parent 47719a3 commit 2d6ee2c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,29 @@ const handleSave = React.useCallback(async () => {
<ReactEditorJS onInitialize={handleInitialize} defaultValue={blocks} />
```

If you want to access low-level instance, you can use `dangerouslyLowLevelInstance`

⚠️ dangerouslyLowLevelInstance depends on the execution environment.

| Environment | Instnace Type |
| - | - |
| Browser | EditorJS instance|
| NodeJS | null |

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

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

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

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

### Haven't received data from server (when use Link)

You should set linkTool [config](https://github.com/editor-js/link#usage). 💪🏻
Expand Down
4 changes: 4 additions & 0 deletions packages/@react-editor-js/client/src/client-editor-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export class ClientEditorCore implements EditorCore {
})
}

public get dangerouslyLowLevelInstance() {
return this._editorJS
}

public async clear() {
await this._editorJS.clear()
}
Expand Down
2 changes: 2 additions & 0 deletions packages/@react-editor-js/core/src/editor-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ export interface EditorCore {
save(): Promise<OutputData>

render(data: OutputData): Promise<void>

get dangerouslyLowLevelInstance(): any | null
}
4 changes: 4 additions & 0 deletions packages/@react-editor-js/core/tests/TestEditorCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export class TestEditorCore implements EditorCore {
return this._data
}

public get dangerouslyLowLevelInstance() {
return null
}

public async clear() {}

public async save() {
Expand Down
4 changes: 4 additions & 0 deletions packages/@react-editor-js/server/src/server-editor-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export class ServerEditorCore implements EditorCore {
}
}

public get dangerouslyLowLevelInstance() {
return null
}

public async clear() {}

public async save() {
Expand Down

0 comments on commit 2d6ee2c

Please sign in to comment.