Skip to content

Commit

Permalink
feat: add onReady property (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
vault-developer authored Jan 27, 2021
1 parent a24f513 commit d243539
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
.rpt2_cache
.rpt2_cache
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Allow all options of [editor-js](https://github.com/codex-team/editor.js/blob/ma
| enableReInitialize | Boolean | Change editor-js data when componentDidUpdate |
| onChange | (api: API, newData: OutputData) => void | Fires when something changed in DOM |
| onCompareBlocks | (newBlocks?: OutputData['blocks'], oldBlocks?: OutputData['blocks']) => boolean | Use to avoid Infinite update when enableReInitialize used with onChange ([Recommended Library](https://github.com/FormidableLabs/react-fast-compare)) |
| onReady | (instance?: EditorJS) => void | Use to execute callback when editor-js instance has initialized |

## 🧐 FAQ

Expand Down
15 changes: 15 additions & 0 deletions lib/EditorJs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface EditorJsProps {
instanceRef?: (instance: EditorJS) => void

onChange?: (api: API, data?: OutputData) => void
onReady?: (instance?: EditorJS) => void,
onCompareBlocks?: (
newBlocks: BlockToolData | undefined,
oldBlocks: BlockToolData | undefined
Expand Down Expand Up @@ -62,13 +63,23 @@ class EditorJsContainer extends React.PureComponent<Props> {
onChange(api, newData)
}

handleReady = () => {
const { onReady } = this.props
if (!onReady) {
return
}

onReady(this.instance)
}

initEditor() {
const {
instanceRef,
children,
enableReInitialize,
tools,
onChange,
onReady,
...props
} = this.props

Expand All @@ -85,6 +96,10 @@ class EditorJsContainer extends React.PureComponent<Props> {
tools: extendTools,
holder: this.holder,

...(onReady && {
onReady: this.handleReady,
}),

...(onChange && {
onChange: this.handleChange,
}),
Expand Down

0 comments on commit d243539

Please sign in to comment.