2021-04-08 04:09:46 +08:00
|
|
|
|
import React from 'react'
|
2021-03-17 05:22:13 +08:00
|
|
|
|
import {
|
|
|
|
|
NodeView,
|
|
|
|
|
NodeViewProps,
|
|
|
|
|
NodeViewRenderer,
|
|
|
|
|
NodeViewRendererProps,
|
|
|
|
|
} from '@tiptap/core'
|
2021-03-17 04:55:40 +08:00
|
|
|
|
import { Decoration, NodeView as ProseMirrorNodeView } from 'prosemirror-view'
|
2021-03-13 04:21:13 +08:00
|
|
|
|
import { Node as ProseMirrorNode } from 'prosemirror-model'
|
|
|
|
|
import { Editor } from './Editor'
|
|
|
|
|
import { ReactRenderer } from './ReactRenderer'
|
2021-03-15 03:40:40 +08:00
|
|
|
|
import { ReactNodeViewContext } from './useReactNodeView'
|
2021-03-13 04:21:13 +08:00
|
|
|
|
|
|
|
|
|
interface ReactNodeViewRendererOptions {
|
|
|
|
|
stopEvent: ((event: Event) => boolean) | null,
|
|
|
|
|
update: ((node: ProseMirrorNode, decorations: Decoration[]) => boolean) | null,
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 04:55:40 +08:00
|
|
|
|
class ReactNodeView extends NodeView<React.FunctionComponent, Editor> {
|
2021-03-13 04:21:13 +08:00
|
|
|
|
|
|
|
|
|
renderer!: ReactRenderer
|
|
|
|
|
|
2021-04-11 20:03:16 +08:00
|
|
|
|
contentDOMElement!: Element | null
|
|
|
|
|
|
2021-03-17 04:55:40 +08:00
|
|
|
|
mount() {
|
2021-03-17 05:22:13 +08:00
|
|
|
|
const props: NodeViewProps = {
|
2021-03-13 04:21:13 +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-03-17 04:55:40 +08:00
|
|
|
|
if (!(this.component as any).displayName) {
|
2021-03-15 03:40:40 +08:00
|
|
|
|
const capitalizeFirstChar = (string: string): string => {
|
|
|
|
|
return string.charAt(0).toUpperCase() + string.substring(1)
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 04:03:45 +08:00
|
|
|
|
this.component.displayName = capitalizeFirstChar(this.extension.name)
|
2021-03-15 03:40:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 05:22:13 +08:00
|
|
|
|
const ReactNodeViewProvider: React.FunctionComponent = componentProps => {
|
2021-03-15 16:00:44 +08:00
|
|
|
|
const onDragStart = this.onDragStart.bind(this)
|
2021-03-17 05:22:13 +08:00
|
|
|
|
const Component = this.component
|
2021-03-15 03:40:40 +08:00
|
|
|
|
|
|
|
|
|
return (
|
2021-04-08 04:07:36 +08:00
|
|
|
|
<ReactNodeViewContext.Provider value={{ onDragStart }}>
|
2021-04-07 20:29:51 +08:00
|
|
|
|
<Component {...componentProps} />
|
2021-03-15 03:40:40 +08:00
|
|
|
|
</ReactNodeViewContext.Provider>
|
|
|
|
|
)
|
2021-03-13 04:21:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 05:22:13 +08:00
|
|
|
|
ReactNodeViewProvider.displayName = 'ReactNodeView'
|
|
|
|
|
|
2021-04-11 20:03:16 +08:00
|
|
|
|
this.contentDOMElement = this.node.isLeaf
|
|
|
|
|
? null
|
|
|
|
|
: document.createElement(this.node.isInline ? 'span' : 'div')
|
|
|
|
|
|
2021-03-17 05:22:13 +08:00
|
|
|
|
this.renderer = new ReactRenderer(ReactNodeViewProvider, {
|
2021-03-13 04:21:13 +08:00
|
|
|
|
editor: this.editor,
|
|
|
|
|
props,
|
2021-04-05 03:43:48 +08:00
|
|
|
|
as: this.node.isInline
|
|
|
|
|
? 'span'
|
2021-04-05 03:44:43 +08:00
|
|
|
|
: 'div',
|
2021-03-13 04:21:13 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get dom() {
|
2021-04-11 20:03:16 +08:00
|
|
|
|
if (
|
|
|
|
|
this.renderer.element.firstElementChild
|
|
|
|
|
&& !this.renderer.element.firstElementChild?.hasAttribute('data-node-view-wrapper')
|
|
|
|
|
) {
|
2021-04-08 15:05:43 +08:00
|
|
|
|
throw Error('Please use the NodeViewWrapper component for your node view.')
|
2021-03-15 01:00:50 +08:00
|
|
|
|
}
|
2021-03-13 04:21:13 +08:00
|
|
|
|
|
2021-03-15 01:00:50 +08:00
|
|
|
|
return this.renderer.element
|
2021-03-13 04:21:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get contentDOM() {
|
2021-03-15 01:00:50 +08:00
|
|
|
|
if (this.node.isLeaf) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
2021-03-13 04:21:13 +08:00
|
|
|
|
|
2021-03-15 01:00:50 +08:00
|
|
|
|
const contentElement = this.dom.querySelector('[data-node-view-content]')
|
2021-03-13 04:21:13 +08:00
|
|
|
|
|
2021-04-11 20:03:16 +08:00
|
|
|
|
if (
|
|
|
|
|
this.contentDOMElement
|
|
|
|
|
&& contentElement
|
|
|
|
|
&& !contentElement.contains(this.contentDOMElement)
|
|
|
|
|
) {
|
|
|
|
|
contentElement.appendChild(this.contentDOMElement)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.contentDOMElement
|
2021-03-13 04:21:13 +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.renderer.updateProps({ node, decorations })
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
selectNode() {
|
|
|
|
|
this.renderer.updateProps({
|
|
|
|
|
selected: true,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deselectNode() {
|
|
|
|
|
this.renderer.updateProps({
|
|
|
|
|
selected: false,
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-03-17 04:55:40 +08:00
|
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
|
this.renderer.destroy()
|
2021-04-11 20:03:16 +08:00
|
|
|
|
this.contentDOMElement = null
|
2021-03-17 04:55:40 +08:00
|
|
|
|
}
|
2021-03-13 04:21:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ReactNodeViewRenderer(component: any, options?: Partial<ReactNodeViewRendererOptions>): NodeViewRenderer {
|
|
|
|
|
return (props: NodeViewRendererProps) => {
|
|
|
|
|
// 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
|
|
|
|
|
if (!(props.editor as Editor).contentComponent) {
|
|
|
|
|
return {}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 04:55:40 +08:00
|
|
|
|
return new ReactNodeView(component, props, options) as ProseMirrorNodeView
|
2021-03-13 04:21:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|