Skip to content

Commit

Permalink
feat: Change enableReInitialize prop works (#26)
Browse files Browse the repository at this point in the history
* feat: Update enableReInitialize prop

Change data blocks instead of reload editor-js instance

* docs: Update README.md

Change enableReInitialize prop description
  • Loading branch information
Jungwoo-An committed Apr 5, 2020
1 parent f8ea7e2 commit 89a5f81
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Allow all options of [editor-js](https://github.com/codex-team/editor.js/blob/ma

| Name | Type | Description |
| ------------------ | ------- | --------------------------------------------- |
| enableReInitialize | Boolean | editor-js rerendering when componentDidUpdate |
| enableReInitialize | Boolean | Change editor-js data when componentDidUpdate |

## 🧐 FAQ

Expand Down
23 changes: 17 additions & 6 deletions lib/EditorJs.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react'

import EditorJS from '@editorjs/editorjs'
import EditorJS, { OutputData } from '@editorjs/editorjs'
import Paragraph from '@editorjs/paragraph'

export interface EditorJsProps {
Expand All @@ -20,14 +20,12 @@ class EditorJsContainer extends React.PureComponent<Props> {
}

async componentDidUpdate() {
const { enableReInitialize } = this.props
if (!enableReInitialize) {
const { enableReInitialize, data } = this.props
if (!enableReInitialize || !data) {
return
}

await this.destroyEditor()

this.initEditor()
this.changeData(data);
}

componentWillUnmount() {
Expand Down Expand Up @@ -84,6 +82,19 @@ class EditorJsContainer extends React.PureComponent<Props> {
})
}

changeData(data: OutputData) {
if (!this.instance) {
return;
}

this.instance?.isReady.then(() => {
this.instance!.clear();
this.instance!.render(data);
}).catch(() => {
// do nothing
});
}

render() {
const { children } = this.props

Expand Down

0 comments on commit 89a5f81

Please sign in to comment.