tiptap/packages/vue-2/src/EditorContent.ts

76 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-05-03 21:11:26 +08:00
import Vue, { PropType, Component } from 'vue'
2021-03-05 18:01:26 +08:00
import { Editor } from './Editor'
2021-02-28 06:56:08 +08:00
2021-05-04 17:03:11 +08:00
export interface EditorContentInterface extends Vue {
editor: Editor,
2021-05-03 21:11:26 +08:00
}
export const EditorContent: Component = {
2021-02-28 06:56:08 +08:00
name: 'EditorContent',
props: {
editor: {
default: null,
2021-03-05 18:01:26 +08:00
type: Object as PropType<Editor>,
2021-02-28 06:56:08 +08:00
},
},
watch: {
editor: {
immediate: true,
2021-05-03 21:11:26 +08:00
handler(this: EditorContentInterface, editor: Editor) {
2021-02-28 06:56:08 +08:00
if (editor && editor.options.element) {
this.$nextTick(() => {
2021-03-05 18:01:26 +08:00
const element = this.$el
if (!element || !editor.options.element.firstChild) {
return
}
element.append(...editor.options.element.childNodes)
2021-03-05 18:01:26 +08:00
editor.contentComponent = this
editor.setOptions({
element,
})
2021-02-28 06:56:08 +08:00
editor.createNodeViews()
})
}
},
},
},
render(createElement) {
return createElement('div')
},
2021-05-04 17:03:11 +08:00
beforeDestroy(this: EditorContentInterface) {
2021-03-05 18:01:26 +08:00
const { editor } = this
if (!editor) {
return
}
2021-03-05 18:01:26 +08:00
if (!editor.isDestroyed) {
editor.view.setProps({
nodeViews: {},
})
}
editor.contentComponent = null
if (!editor.options.element.firstChild) {
return
}
const newElement = document.createElement('div')
newElement.append(...editor.options.element.childNodes)
2021-03-05 18:01:26 +08:00
editor.setOptions({
element: newElement,
2021-02-28 06:56:08 +08:00
})
},
2021-05-03 21:11:26 +08:00
}