Skip to content

Commit

Permalink
fix(vue-3): reapply performance updates (#5373)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: relchapt <reynald.lechapt@getmayday.co>
Co-authored-by: Rirax <rlechapt@student.42.fr>
Co-authored-by: Segev Finer <segev@swimm.io>
  • Loading branch information
4 people authored Jul 22, 2024
1 parent a64cbf8 commit ab8389a
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 65 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-clouds-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/vue-3": patch
---

VueNodeViewRenderer should return `null` for `contentDOM` for a non-leaf node with no `NodeViewContent`
5 changes: 5 additions & 0 deletions packages/core/src/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ export class Editor extends EventEmitter<EditorEvents> {
const state = this.state.apply(transaction)
const selectionHasChanged = !this.state.selection.eq(state.selection)

this.emit('beforeTransaction', {
editor: this,
transaction,
nextState: state,
})
this.view.updateState(state)
this.emit('transaction', {
editor: this,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface EditorEvents {
}
update: { editor: Editor; transaction: Transaction }
selectionUpdate: { editor: Editor; transaction: Transaction }
beforeTransaction: { editor: Editor; transaction: Transaction, nextState: EditorState }
transaction: { editor: Editor; transaction: Transaction }
focus: { editor: Editor; event: FocusEvent; transaction: Transaction }
blur: { editor: Editor; event: FocusEvent; transaction: Transaction }
Expand Down
12 changes: 5 additions & 7 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,18 +40,18 @@ 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)

this.reactiveState = useDebouncedRef(this.view.state)
this.reactiveExtensionStorage = useDebouncedRef(this.extensionStorage)

this.on('transaction', () => {
this.reactiveState.value = this.view.state
this.on('beforeTransaction', ({ nextState }) => {
this.reactiveState.value = nextState
this.reactiveExtensionStorage.value = this.extensionStorage
})

Expand Down
38 changes: 12 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 @@ -45,6 +43,17 @@ export const EditorContent = defineComponent({
// @ts-ignore
editor.contentComponent = instance.ctx._

if (instance) {
editor.appContext = {
...instance.appContext,
provides: {
// @ts-ignore
...instance.provides,
...instance.appContext.provides,
},
}
}

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

editor.contentComponent = null
editor.appContext = null

if (!editor.options.element.firstChild) {
return
Expand All @@ -87,35 +97,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,
)
},
})
14 changes: 8 additions & 6 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 All @@ -136,9 +136,7 @@ class VueNodeView extends NodeView<Component, Editor, VueNodeViewRendererOptions
return null
}

const contentElement = this.dom.querySelector('[data-node-view-content]')

return (contentElement || this.dom) as HTMLElement | null
return this.dom.querySelector('[data-node-view-content]') as HTMLElement | null
}

update(node: ProseMirrorNode, decorations: DecorationWithType[]) {
Expand Down Expand Up @@ -183,14 +181,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
77 changes: 51 additions & 26 deletions packages/vue-3/src/VueRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,87 @@
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'

export interface VueRendererOptions {
editor: Editor,
props?: Record<string, any>,
editor: Editor;
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.
*/
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)

if (this.editor.contentComponent) {
this.editor.contentComponent.update()
this.renderedComponent = this.renderComponent()
}

if (this.teleportElement.children.length !== 1) {
throw Error('VueRenderer doesn’t support multiple child elements.')
}
get element(): Element | null {
return this.renderedComponent.el
}

this.element = this.teleportElement.firstElementChild as Element
get ref(): any {
// Composition API
if (this.renderedComponent.vNode?.component?.exposed) {
return this.renderedComponent.vNode.component.exposed
}
// Option API
return this.renderedComponent.vNode?.component?.proxy
}

get ref(): any {
return this.editor.contentComponent?.refs[this.id]
renderComponent() {
let vNode: ExtendedVNode = h(this.component as DefineComponent, this.props)

if (this.editor.appContext) {
vNode.appContext = this.editor.appContext
}
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 {
Object
.entries(props)
.forEach(([key, value]) => {
this.props[key] = value
})
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 ab8389a

Please sign in to comment.