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

177 lines
4.3 KiB
TypeScript
Raw Normal View History

2021-03-17 05:22:13 +08:00
import {
NodeView,
NodeViewProps,
NodeViewRenderer,
NodeViewRendererProps,
} 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 {
2021-02-28 06:56:08 +08:00
stopEvent: ((event: Event) => boolean) | null,
update: ((node: ProseMirrorNode, decorations: Decoration[]) => boolean) | null,
ignoreMutation: ((mutation: MutationRecord | { type: 'selection', target: Element }) => boolean) | null,
2021-02-28 06:56:08 +08:00
}
2021-03-17 04:55:40 +08:00
class VueNodeView extends NodeView<Component, Editor> {
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[]) {
if (typeof this.options.update === 'function') {
return this.options.update(node, decorations)
}
if (node.type !== this.node.type) {
return false
}
if (node === this.node && this.decorations === decorations) {
return true
}
this.node = node
this.decorations = decorations
this.decorationClasses.value = this.getDecorationClasses()
2021-02-28 06:56:08 +08:00
this.renderer.updateProps({ node, decorations })
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
}
}