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 {
|
2021-03-05 07:02:28 +08:00
|
|
|
|
ref,
|
2021-04-04 05:15:13 +08:00
|
|
|
|
Ref,
|
2021-03-05 07:02:28 +08:00
|
|
|
|
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,
|
|
|
|
|
},
|
2021-05-19 06:01:49 +08:00
|
|
|
|
deleteNode: {
|
|
|
|
|
type: Function as PropType<NodeViewProps['deleteNode']>,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
2021-03-18 04:43:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-28 06:56:08 +08:00
|
|
|
|
interface VueNodeViewRendererOptions {
|
|
|
|
|
stopEvent: ((event: Event) => boolean) | null,
|
|
|
|
|
update: ((node: ProseMirrorNode, decorations: Decoration[]) => boolean) | null,
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 04:55:40 +08:00
|
|
|
|
class VueNodeView extends NodeView<Component, Editor> {
|
2021-02-28 06:56:08 +08:00
|
|
|
|
|
|
|
|
|
renderer!: VueRenderer
|
|
|
|
|
|
2021-04-04 05:15:13 +08:00
|
|
|
|
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),
|
2021-05-19 06:01:49 +08:00
|
|
|
|
deleteNode: () => this.deleteNode(),
|
2021-02-28 06:56:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 07:02:28 +08:00
|
|
|
|
const onDragStart = this.onDragStart.bind(this)
|
|
|
|
|
|
2021-04-04 05:15:13 +08:00
|
|
|
|
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: () => {
|
2021-03-05 07:02:28 +08:00
|
|
|
|
provide('onDragStart', onDragStart)
|
2021-04-04 05:15:13 +08:00
|
|
|
|
provide('decorationClasses', this.decorationClasses)
|
2021-03-05 07:02:28 +08:00
|
|
|
|
|
2021-03-30 03:24:20 +08:00
|
|
|
|
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) {
|
2021-03-05 07:02:28 +08:00
|
|
|
|
return null
|
2021-02-28 06:56:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 07:02:28 +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
|
2021-04-04 05:15:13 +08:00
|
|
|
|
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,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-04 05:15:13 +08:00
|
|
|
|
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 it’s `undefined` because <editor-content> isn’t 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
|
|
|
|
}
|
|
|
|
|
}
|