Skip to content

Commit

Permalink
fix(vue-3): render node only once
Browse files Browse the repository at this point in the history
  • Loading branch information
relchapt committed Apr 8, 2024
1 parent 0f41e38 commit 5cdab90
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
9 changes: 6 additions & 3 deletions packages/vue-3/src/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {
markRaw,
reactive,
Ref,
RendererElement,
RendererNode,
VNode,
} from 'vue'

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

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

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

public vueRenderers = reactive<Map<string, VueRenderer>>(new Map())
public vueRenderers = reactive<Map<string, VNode<RendererNode, RendererElement, {
[key: string]: any;
}>>>(new Map())

public contentComponent: ContentComponent | null = null

Expand Down
19 changes: 1 addition & 18 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 @@ -91,22 +89,7 @@ export const EditorContent = defineComponent({

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)
vueRenderers.push(vueRenderer)
})
}

Expand Down
23 changes: 21 additions & 2 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, Teleport,
} from 'vue'

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

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

const nodeRenderer = (node: VueRenderer) => {
return h(
Teleport,
{
to: node.teleportElement,
key: node.id,
},
h(node.component as DefineComponent, {
ref: node.id,
...node.props,
}),
)
}

export class VueRenderer {
id: string

Expand All @@ -28,7 +44,10 @@ export class VueRenderer {
this.teleportElement = document.createElement('div')
this.element = this.teleportElement
this.props = reactive(props)
this.editor.vueRenderers.set(this.id, this)

const nodeRendered = nodeRenderer(this)

this.editor.vueRenderers.set(this.id, nodeRendered)

if (this.editor.contentComponent) {
this.editor.contentComponent.update()
Expand Down

0 comments on commit 5cdab90

Please sign in to comment.