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

69 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-03-05 18:01:26 +08:00
import Vue, { PropType } from 'vue'
import { Editor } from './Editor'
2021-02-28 06:56:08 +08:00
2021-03-01 06:24:26 +08:00
export const EditorContent = Vue.extend({
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-03-05 18:01:26 +08:00
handler(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.appendChild(editor.options.element.firstChild)
editor.contentComponent = this
editor.setOptions({
element,
})
2021-02-28 06:56:08 +08:00
editor.createNodeViews()
})
}
},
},
},
render(createElement) {
return createElement('div')
},
beforeDestroy() {
2021-03-05 18:01:26 +08:00
const { editor } = this
if (!editor.isDestroyed) {
editor.view.setProps({
nodeViews: {},
})
}
editor.contentComponent = null
if (!editor.options.element.firstChild) {
return
}
const newElement = document.createElement('div')
newElement.appendChild(editor.options.element.firstChild)
editor.setOptions({
element: newElement,
2021-02-28 06:56:08 +08:00
})
},
})