File size: 14,896 Bytes
2daf8de | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | import type {
DecorationWithType,
Editor,
NodeViewRenderer,
NodeViewRendererOptions,
NodeViewRendererProps,
} from '@tiptap/core'
import { getRenderedAttributes, isNodeViewSelected, NodeView } from '@tiptap/core'
import type { Node, Node as ProseMirrorNode } from '@tiptap/pm/model'
import type { Decoration, DecorationSource, NodeView as ProseMirrorNodeView } from '@tiptap/pm/view'
import type { ComponentType, NamedExoticComponent } from 'react'
import { createElement, createRef, memo } from 'react'
import { captureDOMSelection } from './captureDOMSelection.js'
import type { EditorWithContentComponent } from './Editor.js'
import { ReactRenderer } from './ReactRenderer.js'
import type { ReactNodeViewProps } from './types.js'
import type { ReactNodeViewContextProps } from './useReactNodeView.js'
import { ReactNodeViewContext } from './useReactNodeView.js'
export interface ReactNodeViewRendererOptions extends NodeViewRendererOptions {
/**
* This function is called when the node view is updated.
* It allows you to compare the old node with the new node and decide if the component should update.
*/
update:
| ((props: {
oldNode: ProseMirrorNode
oldDecorations: readonly Decoration[]
oldInnerDecorations: DecorationSource
newNode: ProseMirrorNode
newDecorations: readonly Decoration[]
innerDecorations: DecorationSource
updateProps: () => void
}) => boolean)
| null
/**
* The tag name of the element wrapping the React component.
*/
as?: string
/**
* The class name of the element wrapping the React component.
*/
className?: string
/**
* Attributes that should be applied to the element wrapping the React component.
* If this is a function, it will be called each time the node view is updated.
* If this is an object, it will be applied once when the node view is mounted.
*/
attrs?:
| Record<string, string>
| ((props: {
node: ProseMirrorNode
HTMLAttributes: Record<string, any>
}) => Record<string, string>)
}
export class ReactNodeView<
T = HTMLElement,
Component extends ComponentType<ReactNodeViewProps<T>> = ComponentType<ReactNodeViewProps<T>>,
NodeEditor extends Editor = Editor,
Options extends ReactNodeViewRendererOptions = ReactNodeViewRendererOptions,
> extends NodeView<Component, NodeEditor, Options> {
/**
* The renderer instance.
*/
renderer!: ReactRenderer<unknown, ReactNodeViewProps<T>>
/**
* The element that holds the rich-text content of the node.
*/
contentDOMElement!: HTMLElement | null
/**
* The requestAnimationFrame ID used for selection updates.
*/
selectionRafId: number | null = null
/**
* The last known position of this node view, used to detect position-only
* changes that don't produce a new node object reference.
*/
private currentPos: number | undefined
/**
* Fires on editor updates when trackNodeViewPosition is enabled.
* Detects position shifts where update() is NOT called (e.g. typing above).
*/
private handlePositionUpdate = () => {
const newPos = this.getPos()
if (typeof newPos !== 'number' || newPos === this.currentPos) {
return
}
this.currentPos = newPos
this.renderer.updateProps({ getPos: () => this.getPos() })
if (typeof this.options.attrs === 'function') {
this.updateElementAttributes()
}
}
constructor(component: Component, props: NodeViewRendererProps, options?: Partial<Options>) {
super(component, props, options)
if (!this.node.isLeaf) {
if (this.options.contentDOMElementTag) {
this.contentDOMElement = document.createElement(this.options.contentDOMElementTag)
} else {
this.contentDOMElement = document.createElement(this.node.isInline ? 'span' : 'div')
}
this.contentDOMElement.dataset.nodeViewContentReact = ''
this.contentDOMElement.dataset.nodeViewWrapper = ''
// For some reason the whiteSpace prop is not inherited properly in Chrome and Safari
// With this fix it seems to work fine
// See: https://github.com/ueberdosis/tiptap/issues/1197
this.contentDOMElement.style.whiteSpace = 'inherit'
const contentTarget = this.dom.querySelector('[data-node-view-content]')
if (contentTarget) {
contentTarget.appendChild(this.contentDOMElement)
} else {
// ProseMirror maps the selection before the queued portal render.
this.dom.appendChild(this.contentDOMElement)
}
}
if (this.options.trackNodeViewPosition) {
this.editor.on('update', this.handlePositionUpdate)
}
}
private cachedExtensionWithSyncedStorage: NodeViewRendererProps['extension'] | null = null
/**
* Returns a proxy of the extension that redirects storage access to the editor's mutable storage.
* This preserves the original prototype chain (instanceof checks, methods like configure/extend work).
* Cached to avoid proxy creation on every update.
*/
get extensionWithSyncedStorage(): NodeViewRendererProps['extension'] {
if (!this.cachedExtensionWithSyncedStorage) {
const editor = this.editor
const extension = this.extension
this.cachedExtensionWithSyncedStorage = new Proxy(extension, {
get(target, prop, receiver) {
if (prop === 'storage') {
return editor.storage[extension.name as keyof typeof editor.storage] ?? {}
}
return Reflect.get(target, prop, receiver)
},
})
}
return this.cachedExtensionWithSyncedStorage
}
/**
* Setup the React component.
* Called on initialization.
*/
mount() {
const props: Record<string, any> = {
editor: this.editor,
node: this.node,
decorations: this.decorations as DecorationWithType[],
innerDecorations: this.innerDecorations,
view: this.view,
selected: false,
extension: this.extensionWithSyncedStorage,
HTMLAttributes: this.HTMLAttributes,
getPos: () => this.getPos(),
updateAttributes: (attributes = {}) => this.updateAttributes(attributes),
deleteNode: () => this.deleteNode(),
ref: createRef<T>(),
}
const mountProps = props as ReactNodeViewProps<T>
if (!(this.component as any).displayName) {
const capitalizeFirstChar = (string: string): string => {
return string.charAt(0).toUpperCase() + string.substring(1)
}
this.component.displayName = capitalizeFirstChar(this.extension.name)
}
const onDragStart = this.onDragStart.bind(this)
const nodeViewContentRef: ReactNodeViewContextProps['nodeViewContentRef'] = element => {
if (element && this.contentDOMElement && element.firstChild !== this.contentDOMElement) {
// remove the nodeViewWrapper attribute from the element
if (element.hasAttribute('data-node-view-wrapper')) {
element.removeAttribute('data-node-view-wrapper')
}
// The caret can already be inside here. Moving would lose it.
const restoreSelection = captureDOMSelection(this.contentDOMElement)
element.appendChild(this.contentDOMElement)
restoreSelection?.()
}
}
const context = { onDragStart, nodeViewContentRef }
const Component = this.component
// For performance reasons, we memoize the provider component
// And all of the things it requires are declared outside of the component, so it doesn't need to re-render
const ReactNodeViewProvider: NamedExoticComponent<ReactNodeViewProps<T>> = memo(
componentProps => {
return (
<ReactNodeViewContext.Provider value={context}>
{createElement(Component, componentProps)}
</ReactNodeViewContext.Provider>
)
},
)
ReactNodeViewProvider.displayName = 'ReactNodeView'
let as = this.node.isInline ? 'span' : 'div'
if (this.options.as) {
as = this.options.as
}
const { className = '' } = this.options
this.handleSelectionUpdate = this.handleSelectionUpdate.bind(this)
this.renderer = new ReactRenderer(ReactNodeViewProvider, {
editor: this.editor,
props: mountProps,
as,
className: `node-${this.node.type.name} ${className}`.trim(),
})
this.editor.on('selectionUpdate', this.handleSelectionUpdate)
this.updateElementAttributes()
this.currentPos = this.getPos()
}
/**
* Return the DOM element.
* This is the element that will be used to display the node view.
*/
get dom() {
if (
this.renderer.element.firstElementChild &&
!this.renderer.element.firstElementChild?.hasAttribute('data-node-view-wrapper')
) {
throw Error('Please use the NodeViewWrapper component for your node view.')
}
return this.renderer.element
}
/**
* Return the content DOM element.
* This is the element that will be used to display the rich-text content of the node.
*/
get contentDOM() {
if (this.node.isLeaf) {
return null
}
return this.contentDOMElement
}
/**
* On editor selection update, check if the node is selected.
* If it is, call `selectNode`, otherwise call `deselectNode`.
*/
handleSelectionUpdate() {
if (this.selectionRafId) {
cancelAnimationFrame(this.selectionRafId)
this.selectionRafId = null
}
this.selectionRafId = requestAnimationFrame(() => {
this.selectionRafId = null
// getPos() is undefined while the node view is mid-update, so fall back to the last known position.
const pos = this.getPos() ?? this.currentPos
if (typeof pos !== 'number') {
return
}
const isSelected = isNodeViewSelected({
selection: this.editor.state.selection,
pos,
nodeSize: this.node.nodeSize,
selectedOnTextSelection: this.options.selectedOnTextSelection,
})
if (isSelected) {
if (this.renderer.props.selected) {
return
}
this.selectNode()
} else {
if (!this.renderer.props.selected) {
return
}
this.deselectNode()
}
})
}
/**
* On update, update the React component.
* To prevent unnecessary updates, the `update` option can be used.
*/
update(
node: Node,
decorations: readonly Decoration[],
innerDecorations: DecorationSource,
): boolean {
const rerenderComponent = (props?: Record<string, any>) => {
this.renderer.updateProps(props)
if (typeof this.options.attrs === 'function') {
this.updateElementAttributes()
}
}
if (node.type !== this.node.type) {
return false
}
if (typeof this.options.update === 'function') {
const oldNode = this.node
const oldDecorations = this.decorations
const oldInnerDecorations = this.innerDecorations
this.node = node
this.decorations = decorations
this.innerDecorations = innerDecorations
this.currentPos = this.getPos()
return this.options.update({
oldNode,
oldDecorations,
newNode: node,
newDecorations: decorations,
oldInnerDecorations,
innerDecorations,
updateProps: () =>
rerenderComponent({
node,
decorations,
innerDecorations,
extension: this.extensionWithSyncedStorage,
}),
})
}
const nodeChanged = node !== this.node
// Node reference unchanged — only decorations may have changed.
// ProseMirror renders decorations independently on the contentDOM,
// and the getPos closure (bound in mount()) calls through to
// ProseMirror's position function at call time, so it is always
// current. Update internal refs and skip the React re-render.
// Keep currentPos unchanged here so the editor update listener can
// still detect and publish a position shift for tracked node views.
if (!nodeChanged) {
this.node = node
this.decorations = decorations
this.innerDecorations = innerDecorations
return true
}
const newPos = this.getPos()
this.node = node
this.decorations = decorations
this.innerDecorations = innerDecorations
this.currentPos = newPos
const extraProps: Record<string, any> = {
node,
decorations,
innerDecorations,
extension: this.extensionWithSyncedStorage,
}
if (this.options.trackNodeViewPosition) {
extraProps.getPos = () => this.getPos()
}
rerenderComponent(extraProps)
return true
}
/**
* Select the node.
* Add the `selected` prop and the `ProseMirror-selectednode` class.
*/
selectNode() {
this.renderer.updateProps({
selected: true,
})
this.renderer.element.classList.add('ProseMirror-selectednode')
}
/**
* Deselect the node.
* Remove the `selected` prop and the `ProseMirror-selectednode` class.
*/
deselectNode() {
this.renderer.updateProps({
selected: false,
})
this.renderer.element.classList.remove('ProseMirror-selectednode')
}
/**
* Destroy the React component instance.
*/
destroy() {
this.renderer.destroy()
this.editor.off('selectionUpdate', this.handleSelectionUpdate)
if (this.options.trackNodeViewPosition) {
this.editor.off('update', this.handlePositionUpdate)
}
this.contentDOMElement = null
if (this.selectionRafId) {
cancelAnimationFrame(this.selectionRafId)
this.selectionRafId = null
}
}
/**
* Update the attributes of the top-level element that holds the React component.
* Applying the attributes defined in the `attrs` option.
*/
updateElementAttributes() {
if (this.options.attrs) {
let attrsObj: Record<string, string> = {}
if (typeof this.options.attrs === 'function') {
const extensionAttributes = this.editor.extensionManager.attributes
const HTMLAttributes = getRenderedAttributes(this.node, extensionAttributes)
attrsObj = this.options.attrs({ node: this.node, HTMLAttributes })
} else {
attrsObj = this.options.attrs
}
this.renderer.updateAttributes(attrsObj)
}
}
}
/**
* Create a React node view renderer.
*/
export function ReactNodeViewRenderer<T = HTMLElement>(
component: ComponentType<ReactNodeViewProps<T>>,
options?: Partial<ReactNodeViewRendererOptions>,
): NodeViewRenderer {
return props => {
// 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 EditorWithContentComponent).contentComponent) {
return {} as unknown as ProseMirrorNodeView
}
return new ReactNodeView<T>(component, props, options)
}
}
|