Skip to content

Commit

Permalink
refacto: render vue component without Teleport
Browse files Browse the repository at this point in the history
  • Loading branch information
relchapt committed Jun 3, 2024
1 parent 0f41e38 commit 7ca826b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 51 deletions.
8 changes: 3 additions & 5 deletions packages/vue-3/src/Editor.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Editor as CoreEditor, EditorOptions } from '@tiptap/core'
import { EditorState, Plugin, PluginKey } from '@tiptap/pm/state'
import {
AppContext,
ComponentInternalInstance,
ComponentPublicInstance,
customRef,
markRaw,
reactive,
Ref,
} from 'vue'

import { VueRenderer } from './VueRenderer.js'

function useDebouncedRef<T>(value: T) {
return customRef<T>((track, trigger) => {
return {
Expand Down Expand Up @@ -42,10 +40,10 @@ export class Editor extends CoreEditor {

private reactiveExtensionStorage: Ref<Record<string, any>>

public vueRenderers = reactive<Map<string, VueRenderer>>(new Map())

public contentComponent: ContentComponent | null = null

public appContext: AppContext | null = null

constructor(options: Partial<EditorOptions> = {}) {
super(options)

Expand Down
37 changes: 11 additions & 26 deletions packages/vue-3/src/EditorContent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
DefineComponent,
defineComponent,
getCurrentInstance,
h,
Expand All @@ -8,7 +7,6 @@ import {
PropType,
Ref,
ref,
Teleport,
unref,
watchEffect,
} from 'vue'
Expand Down Expand Up @@ -44,6 +42,16 @@ export const EditorContent = defineComponent({

// @ts-ignore
editor.contentComponent = instance.ctx._
editor.appContext = {
// @ts-ignore
...instance.appContext,
provides: {
// @ts-ignore
...instance.provides,
// @ts-ignore
...instance.appContext.provides,
},
}

editor.setOptions({
element,
Expand All @@ -69,6 +77,7 @@ export const EditorContent = defineComponent({
}

editor.contentComponent = null
editor.appContext = null

if (!editor.options.element.firstChild) {
return
Expand All @@ -87,35 +96,11 @@ export const EditorContent = defineComponent({
},

render() {
const vueRenderers: any[] = []

if (this.editor) {
this.editor.vueRenderers.forEach(vueRenderer => {
const node = h(
Teleport,
{
to: vueRenderer.teleportElement,
key: vueRenderer.id,
},
h(
vueRenderer.component as DefineComponent,
{
ref: vueRenderer.id,
...vueRenderer.props,
},
),
)

vueRenderers.push(node)
})
}

return h(
'div',
{
ref: (el: any) => { this.rootEl = el },
},
...vueRenderers,
)
},
})
10 changes: 7 additions & 3 deletions packages/vue-3/src/VueNodeViewRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class VueNodeView extends NodeView<Component, Editor, VueNodeViewRendererOptions
}

get dom() {
if (!this.renderer.element.hasAttribute('data-node-view-wrapper')) {
if (!this.renderer.element || !this.renderer.element.hasAttribute('data-node-view-wrapper')) {
throw Error('Please use the NodeViewWrapper component for your node view.')
}

Expand Down Expand Up @@ -183,14 +183,18 @@ class VueNodeView extends NodeView<Component, Editor, VueNodeViewRendererOptions
this.renderer.updateProps({
selected: true,
})
this.renderer.element.classList.add('ProseMirror-selectednode')
if (this.renderer.element) {
this.renderer.element.classList.add('ProseMirror-selectednode')
}
}

deselectNode() {
this.renderer.updateProps({
selected: false,
})
this.renderer.element.classList.remove('ProseMirror-selectednode')
if (this.renderer.element) {
this.renderer.element.classList.remove('ProseMirror-selectednode')
}
}

getDecorationClasses() {
Expand Down
48 changes: 31 additions & 17 deletions packages/vue-3/src/VueRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Editor } from '@tiptap/core'
import { Component, markRaw, reactive } from 'vue'
import {
Component, DefineComponent, h, markRaw, reactive, render,
} from 'vue'

import { Editor as ExtendedEditor } from './Editor.js'

Expand All @@ -8,52 +10,64 @@ export interface VueRendererOptions {
props?: Record<string, any>,
}

type ExtendedVNode = ReturnType<typeof h> | null
interface RenderedComponent {
vNode: ExtendedVNode
destroy: () => void
el: Element | null
}

export class VueRenderer {
id: string

renderedComponent!: RenderedComponent

editor: ExtendedEditor

component: Component

teleportElement: Element

element: Element
el: Element | null

props: Record<string, any>

constructor(component: Component, { props = {}, editor }: VueRendererOptions) {
this.id = Math.floor(Math.random() * 0xFFFFFFFF).toString()
this.editor = editor as ExtendedEditor
this.component = markRaw(component)
this.teleportElement = document.createElement('div')
this.element = this.teleportElement
this.el = document.createElement('div')
this.props = reactive(props)
this.editor.vueRenderers.set(this.id, this)
this.renderedComponent = this.renderComponent()
}

if (this.editor.contentComponent) {
this.editor.contentComponent.update()
get element(): Element | null {
return this.renderedComponent.el
}

renderComponent() {
let vNode: ExtendedVNode = h(this.component as DefineComponent, this.props)

if (this.teleportElement.children.length !== 1) {
throw Error('VueRenderer doesn’t support multiple child elements.')
}
if (typeof document !== 'undefined' && this.el) { render(vNode, this.el) }

this.element = this.teleportElement.firstElementChild as Element
const destroy = () => {
if (this.el) { render(null, this.el) }
this.el = null
vNode = null
}
}

get ref(): any {
return this.editor.contentComponent?.refs[this.id]
return { vNode, destroy, el: this.el ? this.el.firstElementChild : null }
}

updateProps(props: Record<string, any> = {}): void {

Object
.entries(props)
.forEach(([key, value]) => {
this.props[key] = value
})
this.renderComponent()
}

destroy(): void {
this.editor.vueRenderers.delete(this.id)
this.renderedComponent.destroy()
}
}

0 comments on commit 7ca826b

Please sign in to comment.