fix(vue-3): faster component rendering (#5206)

This commit is contained in:
Rirax 2024-06-12 07:37:12 +02:00 committed by GitHub
parent bd480a2009
commit 95a1d4b7de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 57 additions and 54 deletions

View File

@ -1,16 +1,14 @@
import { Editor as CoreEditor, EditorOptions } from '@tiptap/core' import { Editor as CoreEditor, EditorOptions } from '@tiptap/core'
import { EditorState, Plugin, PluginKey } from '@tiptap/pm/state' import { EditorState, Plugin, PluginKey } from '@tiptap/pm/state'
import { import {
AppContext,
ComponentInternalInstance, ComponentInternalInstance,
ComponentPublicInstance, ComponentPublicInstance,
customRef, customRef,
markRaw, markRaw,
reactive,
Ref, Ref,
} from 'vue' } from 'vue'
import { VueRenderer } from './VueRenderer.js'
function useDebouncedRef<T>(value: T) { function useDebouncedRef<T>(value: T) {
return customRef<T>((track, trigger) => { return customRef<T>((track, trigger) => {
return { return {
@ -42,10 +40,10 @@ export class Editor extends CoreEditor {
private reactiveExtensionStorage: Ref<Record<string, any>> private reactiveExtensionStorage: Ref<Record<string, any>>
public vueRenderers = reactive<Map<string, VueRenderer>>(new Map())
public contentComponent: ContentComponent | null = null public contentComponent: ContentComponent | null = null
public appContext: AppContext | null = null
constructor(options: Partial<EditorOptions> = {}) { constructor(options: Partial<EditorOptions> = {}) {
super(options) super(options)

View File

@ -1,5 +1,4 @@
import { import {
DefineComponent,
defineComponent, defineComponent,
getCurrentInstance, getCurrentInstance,
h, h,
@ -8,7 +7,6 @@ import {
PropType, PropType,
Ref, Ref,
ref, ref,
Teleport,
unref, unref,
watchEffect, watchEffect,
} from 'vue' } from 'vue'
@ -45,6 +43,17 @@ export const EditorContent = defineComponent({
// @ts-ignore // @ts-ignore
editor.contentComponent = instance.ctx._ editor.contentComponent = instance.ctx._
if (instance) {
editor.appContext = {
...instance.appContext,
provides: {
// @ts-ignore
...instance.provides,
...instance.appContext.provides,
},
}
}
editor.setOptions({ editor.setOptions({
element, element,
}) })
@ -69,6 +78,7 @@ export const EditorContent = defineComponent({
} }
editor.contentComponent = null editor.contentComponent = null
editor.appContext = null
if (!editor.options.element.firstChild) { if (!editor.options.element.firstChild) {
return return
@ -87,35 +97,11 @@ export const EditorContent = defineComponent({
}, },
render() { 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( return h(
'div', 'div',
{ {
ref: (el: any) => { this.rootEl = el }, ref: (el: any) => { this.rootEl = el },
}, },
...vueRenderers,
) )
}, },
}) })

View File

@ -124,7 +124,7 @@ class VueNodeView extends NodeView<Component, Editor, VueNodeViewRendererOptions
} }
get dom() { 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.') throw Error('Please use the NodeViewWrapper component for your node view.')
} }
@ -183,14 +183,18 @@ class VueNodeView extends NodeView<Component, Editor, VueNodeViewRendererOptions
this.renderer.updateProps({ this.renderer.updateProps({
selected: true, selected: true,
}) })
this.renderer.element.classList.add('ProseMirror-selectednode') if (this.renderer.element) {
this.renderer.element.classList.add('ProseMirror-selectednode')
}
} }
deselectNode() { deselectNode() {
this.renderer.updateProps({ this.renderer.updateProps({
selected: false, selected: false,
}) })
this.renderer.element.classList.remove('ProseMirror-selectednode') if (this.renderer.element) {
this.renderer.element.classList.remove('ProseMirror-selectednode')
}
} }
getDecorationClasses() { getDecorationClasses() {

View File

@ -1,5 +1,7 @@
import { Editor } from '@tiptap/core' 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' import { Editor as ExtendedEditor } from './Editor.js'
@ -8,19 +10,27 @@ export interface VueRendererOptions {
props?: Record<string, any>, props?: Record<string, any>,
} }
type ExtendedVNode = ReturnType<typeof h> | null
interface RenderedComponent {
vNode: ExtendedVNode
destroy: () => void
el: Element | null
}
/** /**
* This class is used to render Vue components inside the editor. * This class is used to render Vue components inside the editor.
*/ */
export class VueRenderer { export class VueRenderer {
id: string id: string
renderedComponent!: RenderedComponent
editor: ExtendedEditor editor: ExtendedEditor
component: Component component: Component
teleportElement: Element el: Element | null
element: Element
props: Record<string, any> props: Record<string, any>
@ -28,35 +38,40 @@ export class VueRenderer {
this.id = Math.floor(Math.random() * 0xFFFFFFFF).toString() this.id = Math.floor(Math.random() * 0xFFFFFFFF).toString()
this.editor = editor as ExtendedEditor this.editor = editor as ExtendedEditor
this.component = markRaw(component) this.component = markRaw(component)
this.teleportElement = document.createElement('div') this.el = document.createElement('div')
this.element = this.teleportElement
this.props = reactive(props) this.props = reactive(props)
this.editor.vueRenderers.set(this.id, this) this.renderedComponent = this.renderComponent()
if (this.editor.contentComponent) {
this.editor.contentComponent.update()
if (this.teleportElement.children.length !== 1) {
throw Error('VueRenderer doesnt support multiple child elements.')
}
this.element = this.teleportElement.firstElementChild as Element
}
} }
get ref(): any { get element(): Element | null {
return this.editor.contentComponent?.refs[this.id] return this.renderedComponent.el
}
renderComponent() {
let vNode: ExtendedVNode = h(this.component as DefineComponent, this.props)
if (typeof document !== 'undefined' && this.el) { render(vNode, this.el) }
const destroy = () => {
if (this.el) { render(null, this.el) }
this.el = null
vNode = null
}
return { vNode, destroy, el: this.el ? this.el.firstElementChild : null }
} }
updateProps(props: Record<string, any> = {}): void { updateProps(props: Record<string, any> = {}): void {
Object Object
.entries(props) .entries(props)
.forEach(([key, value]) => { .forEach(([key, value]) => {
this.props[key] = value this.props[key] = value
}) })
this.renderComponent()
} }
destroy(): void { destroy(): void {
this.editor.vueRenderers.delete(this.id) this.renderedComponent.destroy()
} }
} }