Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: (vue-3) render node only once #5041

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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