2021-03-17 05:22:13 +08:00
|
|
|
|
import {
|
|
|
|
|
NodeView,
|
|
|
|
|
NodeViewProps,
|
|
|
|
|
NodeViewRenderer,
|
|
|
|
|
NodeViewRendererProps,
|
2021-07-27 18:26:24 +08:00
|
|
|
|
NodeViewRendererOptions,
|
2021-03-17 05:22:13 +08:00
|
|
|
|
} 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-07-27 18:26:24 +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
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 18:26:24 +08:00
|
|
|
|
class VueNodeView extends NodeView<Component, Editor, VueNodeViewRendererOptions> {
|
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-10-03 05:18:12 +08:00
|
|
|
|
setup: reactiveProps => {
|
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-10-03 05:18:12 +08:00
|
|
|
|
return (this.component as any).setup?.(reactiveProps, {
|
2021-09-21 05:57:48 +08:00
|
|
|
|
expose: () => undefined,
|
|
|
|
|
})
|
2021-02-28 06:56:08 +08:00
|
|
|
|
},
|
2021-09-23 15:53:11 +08:00
|
|
|
|
// add support for scoped styles
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
// eslint-disable-next-line
|
|
|
|
|
__scopeId: this.component.__scopeId,
|
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[]) {
|
2021-07-27 18:26:24 +08:00
|
|
|
|
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') {
|
2021-07-27 18:26:24 +08:00
|
|
|
|
const oldNode = this.node
|
|
|
|
|
const oldDecorations = this.decorations
|
|
|
|
|
|
|
|
|
|
this.node = node
|
|
|
|
|
this.decorations = decorations
|
|
|
|
|
|
|
|
|
|
return this.options.update({
|
|
|
|
|
oldNode,
|
|
|
|
|
oldDecorations,
|
|
|
|
|
newNode: node,
|
|
|
|
|
newDecorations: decorations,
|
2021-07-28 20:51:58 +08:00
|
|
|
|
updateProps: () => updateProps({ node, decorations }),
|
2021-07-27 18:26:24 +08:00
|
|
|
|
})
|
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
|
2021-07-27 18:26:24 +08:00
|
|
|
|
|
2021-07-28 20:49:00 +08:00
|
|
|
|
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,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|