fix: Make sure editor is available on first render (#2282), fix #2040, fix #2182

This commit is contained in:
Ryan Toronto 2021-12-16 07:56:35 -05:00 committed by GitHub
parent 8ed485ba53
commit 2436e2c8fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,13 +9,18 @@ function useForceUpdate() {
}
export const useEditor = (options: Partial<EditorOptions> = {}, deps: DependencyList = []) => {
const [editor, setEditor] = useState<Editor | null>(null)
const [editor, setEditor] = useState<Editor>(() => new Editor(options))
const forceUpdate = useForceUpdate()
useEffect(() => {
const instance = new Editor(options)
let instance: Editor
setEditor(instance)
if (editor.isDestroyed) {
instance = new Editor(options)
setEditor(instance)
} else {
instance = editor
}
instance.on('transaction', () => {
requestAnimationFrame(() => {