tiptap/packages/react/src/ReactNodeViewRenderer.tsx

146 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-03-15 03:40:40 +08:00
import React, { useState, useEffect } 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-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-03-17 04:55:40 +08:00
// @ts-ignore
this.component.displayName = capitalizeFirstChar(this.extension.config.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 03:40:40 +08:00
const [isEditable, setIsEditable] = useState(this.editor.isEditable)
2021-03-15 16:00:44 +08:00
const onDragStart = this.onDragStart.bind(this)
2021-03-16 01:48:51 +08:00
const onViewUpdate = () => setIsEditable(this.editor.isEditable)
2021-03-17 05:22:13 +08:00
const Component = this.component
2021-03-15 03:40:40 +08:00
useEffect(() => {
2021-03-15 16:00:44 +08:00
this.editor.on('viewUpdate', onViewUpdate)
2021-03-15 03:40:40 +08:00
return () => {
2021-03-15 16:00:44 +08:00
this.editor.off('viewUpdate', onViewUpdate)
2021-03-15 03:40:40 +08:00
}
}, [])
return (
2021-04-07 05:00:03 +08:00
// @ts-ignore
2021-03-15 16:00:44 +08:00
<ReactNodeViewContext.Provider value={{ onDragStart, isEditable }}>
2021-04-07 05:00:03 +08:00
{
// @ts-ignore
<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'
this.renderer = new ReactRenderer(ReactNodeViewProvider, {
2021-03-13 04:21:13 +08:00
editor: this.editor,
props,
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-03-15 01:00:50 +08:00
if (!this.renderer.element.firstElementChild?.hasAttribute('data-node-view-wrapper')) {
throw Error('Please use the ReactViewWrapper component for your node view.')
}
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-03-15 01:00:50 +08:00
return contentElement || this.dom
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-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 its `undefined` because <editor-content> isnt 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
}
}