tiptap/packages/react/src/EditorContent.tsx
Philipp Kühn 956566eaad
fix: fix some react focus issues (#1724), fix #1716, fix #1608, fix #1520
* remove async createNodeViews

* focus asynchronously to fix weird bugs in react
2021-08-12 18:03:45 +02:00

111 lines
2.1 KiB
TypeScript

import React, { HTMLProps } from 'react'
import ReactDOM from 'react-dom'
import { Editor } from './Editor'
import { ReactRenderer } from './ReactRenderer'
const Portals: React.FC<{ renderers: Map<string, ReactRenderer> }> = ({ renderers }) => {
return (
<>
{Array.from(renderers).map(([key, renderer]) => {
return ReactDOM.createPortal(
renderer.reactElement,
renderer.element,
key,
)
})}
</>
)
}
export interface EditorContentProps extends HTMLProps<HTMLDivElement> {
editor: Editor | null,
}
export interface EditorContentState {
renderers: Map<string, ReactRenderer>
}
export class PureEditorContent extends React.Component<EditorContentProps, EditorContentState> {
editorContentRef: React.RefObject<any>
constructor(props: EditorContentProps) {
super(props)
this.editorContentRef = React.createRef()
this.state = {
renderers: new Map(),
}
}
componentDidMount() {
this.init()
}
componentDidUpdate() {
this.init()
}
init() {
const { editor } = this.props
if (editor && editor.options.element) {
if (editor.contentComponent) {
return
}
const element = this.editorContentRef.current
element.append(...editor.options.element.childNodes)
editor.setOptions({
element,
})
editor.contentComponent = this
editor.createNodeViews()
}
}
componentWillUnmount() {
const { editor } = this.props
if (!editor) {
return
}
if (!editor.isDestroyed) {
editor.view.setProps({
nodeViews: {},
})
}
editor.contentComponent = null
if (!editor.options.element.firstChild) {
return
}
const newElement = document.createElement('div')
newElement.append(...editor.options.element.childNodes)
editor.setOptions({
element: newElement,
})
}
render() {
const { editor, ...rest } = this.props
return (
<>
<div ref={this.editorContentRef} {...rest} />
<Portals renderers={this.state.renderers} />
</>
)
}
}
export const EditorContent = React.memo(PureEditorContent)