2021-11-05 20:45:16 +08:00
|
|
|
import {
|
|
|
|
onMounted,
|
|
|
|
onBeforeUnmount,
|
|
|
|
shallowRef,
|
|
|
|
Ref,
|
|
|
|
} from 'vue'
|
2021-03-01 06:47:34 +08:00
|
|
|
import { EditorOptions } from '@tiptap/core'
|
|
|
|
import { Editor } from './Editor'
|
|
|
|
|
2021-11-05 20:45:16 +08:00
|
|
|
// We set a custom return type. Otherwise TypeScript will throw TS4023. Not sure why.
|
|
|
|
export const useEditor = (options: Partial<EditorOptions> = {}): Ref<Editor> => {
|
|
|
|
const editor = shallowRef<Editor>() as Ref<Editor>
|
2021-03-01 06:47:34 +08:00
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
editor.value = new Editor(options)
|
|
|
|
})
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
2021-03-18 04:21:57 +08:00
|
|
|
editor.value?.destroy()
|
2021-03-01 06:47:34 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
return editor
|
|
|
|
}
|