tiptap/packages/vue-3/src/VueNodeViewRenderer.ts

199 lines
4.7 KiB
TypeScript
Raw Normal View History

2021-03-17 05:22:13 +08:00
import {
NodeView,
NodeViewProps,
NodeViewRenderer,
NodeViewRendererProps,
NodeViewRendererOptions,
2021-03-17 05:22:13 +08:00
} from '@tiptap/core'
2021-02-28 06:56:08 +08:00
import {
ref,
Ref,
provide,
2021-03-18 04:43:45 +08:00
PropType,
2021-02-28 06:56:08 +08:00
Component,
defineComponent,
} from 'vue'
2021-03-17 04:55:40 +08:00
import { Decoration, NodeView as ProseMirrorNodeView } from 'prosemirror-view'
2021-02-28 06:56:08 +08:00
import { Node as ProseMirrorNode } from 'prosemirror-model'
import { Editor } from './Editor'
import { VueRenderer } from './VueRenderer'
2021-03-18 04:43:45 +08:00
export const nodeViewProps = {
editor: {
type: Object as PropType<NodeViewProps['editor']>,
required: true,
},
node: {
type: Object as PropType<NodeViewProps['node']>,
required: true,
},
decorations: {
type: Object as PropType<NodeViewProps['decorations']>,
required: true,
},
selected: {
type: Boolean as PropType<NodeViewProps['selected']>,
required: true,
},
extension: {
type: Object as PropType<NodeViewProps['extension']>,
required: true,
},
getPos: {
type: Function as PropType<NodeViewProps['getPos']>,
required: true,
},
updateAttributes: {
type: Function as PropType<NodeViewProps['updateAttributes']>,
required: true,
},
deleteNode: {
type: Function as PropType<NodeViewProps['deleteNode']>,
required: true,
},
2021-03-18 04:43:45 +08:00
}
export interface VueNodeViewRendererOptions extends NodeViewRendererOptions {
update: ((props: {
oldNode: ProseMirrorNode,
oldDecorations: Decoration[],
newNode: ProseMirrorNode,
newDecorations: Decoration[],
updateProps: () => void,
}) => boolean) | null,
2021-02-28 06:56:08 +08:00
}
class VueNodeView extends NodeView<Component, Editor, VueNodeViewRendererOptions> {
2021-02-28 06:56:08 +08:00
renderer!: VueRenderer
decorationClasses!: Ref<string>
2021-03-17 04:55:40 +08:00
mount() {
2021-03-17 05:22:13 +08:00
const props: NodeViewProps = {
2021-02-28 06:56:08 +08:00
editor: this.editor,
node: this.node,
decorations: this.decorations,
selected: false,
extension: this.extension,
getPos: () => this.getPos(),
updateAttributes: (attributes = {}) => this.updateAttributes(attributes),
deleteNode: () => this.deleteNode(),
2021-02-28 06:56:08 +08:00
}
const onDragStart = this.onDragStart.bind(this)
this.decorationClasses = ref(this.getDecorationClasses())
2021-02-28 06:56:08 +08:00
const extendedComponent = defineComponent({
2021-03-17 04:55:40 +08:00
extends: { ...this.component },
2021-02-28 06:56:08 +08:00
props: Object.keys(props),
2021-03-17 04:55:40 +08:00
setup: () => {
provide('onDragStart', onDragStart)
provide('decorationClasses', this.decorationClasses)
return (this.component as any).setup?.(props)
2021-02-28 06:56:08 +08:00
},
})
this.renderer = new VueRenderer(extendedComponent, {
editor: this.editor,
props,
})
}
get dom() {
2021-03-05 16:46:47 +08:00
if (!this.renderer.element.hasAttribute('data-node-view-wrapper')) {
throw Error('Please use the NodeViewWrapper component for your node view.')
}
2021-02-28 06:56:08 +08:00
return this.renderer.element
}
get contentDOM() {
2021-03-15 01:00:50 +08:00
if (this.node.isLeaf) {
return null
2021-02-28 06:56:08 +08:00
}
const contentElement = this.dom.querySelector('[data-node-view-content]')
return contentElement || this.dom
2021-02-28 06:56:08 +08:00
}
update(node: ProseMirrorNode, decorations: Decoration[]) {
const updateProps = (props?: Record<string, any>) => {
this.decorationClasses.value = this.getDecorationClasses()
this.renderer.updateProps(props)
}
2021-02-28 06:56:08 +08:00
if (typeof this.options.update === 'function') {
const oldNode = this.node
const oldDecorations = this.decorations
this.node = node
this.decorations = decorations
return this.options.update({
oldNode,
oldDecorations,
newNode: node,
newDecorations: decorations,
updateProps,
})
2021-02-28 06:56:08 +08:00
}
if (node.type !== this.node.type) {
return false
}
if (node === this.node && this.decorations === decorations) {
return true
}
this.node = node
this.decorations = decorations
updateProps({ node, decorations })
2021-02-28 06:56:08 +08:00
return true
}
selectNode() {
this.renderer.updateProps({
selected: true,
})
}
deselectNode() {
this.renderer.updateProps({
selected: false,
})
}
getDecorationClasses() {
return this.decorations
// @ts-ignore
.map(item => item.type.attrs.class)
.flat()
.join(' ')
}
2021-03-17 04:55:40 +08:00
destroy() {
this.renderer.destroy()
}
2021-02-28 06:56:08 +08:00
}
export function VueNodeViewRenderer(component: Component, options?: Partial<VueNodeViewRendererOptions>): NodeViewRenderer {
return (props: NodeViewRendererProps) => {
2021-03-05 18:01:26 +08:00
// try to get the parent component
// this is important for vue devtools to show the component hierarchy correctly
// maybe its `undefined` because <editor-content> isnt rendered yet
2021-03-05 18:58:52 +08:00
if (!(props.editor as Editor).contentComponent) {
2021-02-28 06:56:08 +08:00
return {}
}
2021-03-17 04:55:40 +08:00
return new VueNodeView(component, props, options) as ProseMirrorNodeView
2021-02-28 06:56:08 +08:00
}
}