add useEditor

This commit is contained in:
Philipp Kühn
2021-03-08 22:00:07 +01:00
parent 0c494ab136
commit ed7f8c257e
5 changed files with 239 additions and 213 deletions

View File

@@ -0,0 +1,31 @@
// @ts-nocheck
import { useState, useEffect } from 'react'
import { Editor } from './Editor'
function useForceUpdate() {
const [_, setValue] = useState(0)
return () => setValue(value => value + 1)
}
export const useEditor = (options = {}) => {
const [editor, setEditor] = useState(null)
const forceUpdate = useForceUpdate()
useEffect(() => {
const instance = new Editor(options)
setEditor(instance)
instance.on('transaction', () => {
forceUpdate()
})
return () => {
instance.destroy()
}
}, [])
return editor
}