tiptap/packages/vue-3/src/VueRenderer.ts

59 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-03-02 04:04:25 +08:00
import { reactive, markRaw, Component } from 'vue'
2021-02-28 06:56:08 +08:00
import { Editor } from './Editor'
export interface VueRendererOptions {
as?: string;
editor: Editor;
props?: { [key: string]: any };
}
export class VueRenderer {
id: string
editor: Editor
component: Component
teleportElement: Element
element: Element
props: { [key: string]: any }
constructor(component: Component, { props = {}, editor }: VueRendererOptions) {
this.id = Math.floor(Math.random() * 0xFFFFFFFF).toString()
this.editor = editor
this.component = markRaw(component)
this.teleportElement = document.createElement('div')
this.element = this.teleportElement
2021-03-02 04:04:25 +08:00
this.props = reactive(props)
2021-02-28 06:56:08 +08:00
this.editor.vueRenderers.set(this.id, this)
if (this.editor.contentComponent) {
this.editor.contentComponent.update()
2021-03-05 16:46:47 +08:00
if (this.teleportElement.children.length !== 1) {
throw Error('VueRenderer doesnt support multiple child elements.')
}
2021-02-28 06:56:08 +08:00
this.element = this.teleportElement.firstElementChild as Element
}
}
get ref() {
return this.editor.contentComponent?.ctx.$refs[this.id]
}
updateProps(props: { [key: string]: any } = {}) {
2021-03-02 04:04:25 +08:00
Object
.entries(props)
.forEach(([key, value]) => {
this.props[key] = value
})
2021-02-28 06:56:08 +08:00
}
destroy() {
this.editor.vueRenderers.delete(this.id)
}
}