modul-s / node_modules /@tiptap /react /src /EditorContent.tsx
sitcod3's picture
Upload folder using huggingface_hub (part 12)
2daf8de verified
Raw History Blame
5.51 kB
import type { Editor } from '@tiptap/core'
import type { ForwardedRef, HTMLProps, LegacyRef, MutableRefObject } from 'react'
import React, { forwardRef } from 'react'
import ReactDOM from 'react-dom'
import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js'
import type { ContentComponent, EditorWithContentComponent } from './Editor.js'
import type { ReactRenderer } from './ReactRenderer.js'
const mergeRefs = <T extends HTMLDivElement>(
...refs: Array<MutableRefObject<T> | LegacyRef<T> | undefined>
) => {
return (node: T) => {
refs.forEach(ref => {
if (typeof ref === 'function') {
ref(node)
} else if (ref) {
;(ref as MutableRefObject<T | null>).current = node
}
})
}
}
/**
* This component renders all of the editor's node views.
*/
const Portals: React.FC<{ contentComponent: ContentComponent }> = ({ contentComponent }) => {
// For performance reasons, we render the node view portals on state changes only
const renderers = useSyncExternalStore(
contentComponent.subscribe,
contentComponent.getSnapshot,
contentComponent.getServerSnapshot,
)
// This allows us to directly render the portals without any additional wrapper
return <>{Object.values(renderers)}</>
}
export interface EditorContentProps extends HTMLProps<HTMLDivElement> {
editor: Editor | null
innerRef?: ForwardedRef<HTMLDivElement | null>
}
export function createContentComponent(): ContentComponent {
const subscribers = new Set<() => void>()
let renderers: Record<string, React.ReactPortal> = {}
let isNotificationQueued = false
const notifySubscribers = () => {
if (isNotificationQueued || !subscribers.size) {
return
}
isNotificationQueued = true
queueMicrotask(() => {
isNotificationQueued = false
subscribers.forEach(subscriber => subscriber())
})
}
return {
/**
* Subscribe to the editor instance's changes.
*/
subscribe(callback: () => void) {
subscribers.add(callback)
return () => {
subscribers.delete(callback)
}
},
getSnapshot() {
return renderers
},
getServerSnapshot() {
return renderers
},
/**
* Adds a new NodeView Renderer to the editor.
*/
setRenderer(id: string, renderer: ReactRenderer) {
renderers = {
...renderers,
[id]: ReactDOM.createPortal(renderer.reactElement, renderer.element, id),
}
notifySubscribers()
},
/**
* Removes a NodeView Renderer from the editor.
*/
removeRenderer(id: string) {
const nextRenderers = { ...renderers }
delete nextRenderers[id]
renderers = nextRenderers
notifySubscribers()
},
}
}
export class PureEditorContent extends React.Component<
EditorContentProps,
{ hasContentComponentInitialized: boolean }
> {
editorContentRef: React.RefObject<any>
constructor(props: EditorContentProps) {
super(props)
this.editorContentRef = React.createRef()
}
componentDidMount() {
this.init()
}
componentDidUpdate() {
this.init()
}
init() {
const editor = this.props.editor as EditorWithContentComponent | null
if (editor && !editor.isDestroyed && editor.view.dom?.parentNode) {
if (editor.contentComponent) {
return
}
const element = this.editorContentRef.current
element.append(...editor.view.dom.parentNode.childNodes)
editor.setOptions({
element,
})
editor.contentComponent = createContentComponent()
editor.createNodeViews()
editor.isEditorContentInitialized = true
this.forceUpdate()
}
}
componentWillUnmount() {
const editor = this.props.editor as EditorWithContentComponent | null
if (!editor) {
return
}
editor.isEditorContentInitialized = false
if (!editor.isDestroyed) {
editor.view.setProps({
nodeViews: {},
})
}
editor.contentComponent = null
// try to reset the editor element
// may fail if this editor's view.dom was never initialized/mounted yet
try {
if (!editor.view.dom?.parentNode) {
return
}
// TODO using the new editor.mount method might allow us to remove this
const newElement = document.createElement('div')
newElement.append(...editor.view.dom.parentNode.childNodes)
editor.setOptions({
element: newElement,
})
} catch {
// do nothing, nothing to reset
}
}
render() {
const { editor, innerRef, ...rest } = this.props
return (
<>
<div ref={mergeRefs(innerRef, this.editorContentRef)} {...rest} />
{/* @ts-ignore */}
{editor?.contentComponent && <Portals contentComponent={editor.contentComponent} />}
</>
)
}
}
// EditorContent should be re-created whenever the Editor instance changes
const EditorContentWithKey = forwardRef<HTMLDivElement, EditorContentProps>(
(props: Omit<EditorContentProps, 'innerRef'>, ref) => {
const key = React.useMemo(() => {
return Math.floor(Math.random() * 0xffffffff).toString()
// oxlint-disable-next-line react-hooks/exhaustive-deps
}, [props.editor])
// Can't use JSX here because it conflicts with the type definition of Vue's JSX, so use createElement
return React.createElement(PureEditorContent, {
key,
innerRef: ref,
...props,
})
},
)
export const EditorContent = React.memo(EditorContentWithKey)