tiptap/packages/react/src/useEditor.ts

29 lines
614 B
TypeScript
Raw Normal View History

2021-03-09 05:00:07 +08:00
import { useState, useEffect } from 'react'
2021-03-09 05:16:32 +08:00
import { EditorOptions } from '@tiptap/core'
2021-03-09 05:00:07 +08:00
import { Editor } from './Editor'
function useForceUpdate() {
2021-03-09 06:10:10 +08:00
const [, setValue] = useState(0)
2021-03-09 05:00:07 +08:00
return () => setValue(value => value + 1)
}
2021-03-09 05:16:32 +08:00
export const useEditor = (options: Partial<EditorOptions> = {}) => {
const [editor, setEditor] = useState<Editor | null>(null)
2021-03-09 05:00:07 +08:00
const forceUpdate = useForceUpdate()
useEffect(() => {
const instance = new Editor(options)
setEditor(instance)
2021-03-09 05:16:32 +08:00
instance.on('transaction', forceUpdate)
2021-03-09 05:00:07 +08:00
return () => {
instance.destroy()
}
}, [])
return editor
}