2021-03-09 05:16:32 +08:00
|
|
|
import React from 'react'
|
2021-03-13 04:21:13 +08:00
|
|
|
import ReactDOM from 'react-dom'
|
2021-03-09 06:10:10 +08:00
|
|
|
import { Editor } from './Editor'
|
2021-03-15 01:00:50 +08:00
|
|
|
import { ReactRenderer } from './ReactRenderer'
|
2021-03-09 05:00:07 +08:00
|
|
|
|
2021-03-09 06:10:10 +08:00
|
|
|
type EditorContentProps = {
|
|
|
|
editor: Editor | null
|
|
|
|
}
|
|
|
|
|
2021-03-15 01:00:50 +08:00
|
|
|
const Portals: React.FC<{ renderers: Map<string, ReactRenderer> }> = ({ renderers }) => {
|
2021-03-13 04:21:13 +08:00
|
|
|
return (
|
2021-03-15 01:00:50 +08:00
|
|
|
<>
|
2021-03-13 04:21:13 +08:00
|
|
|
{Array.from(renderers).map(([key, renderer]) => {
|
2021-03-14 23:30:06 +08:00
|
|
|
return ReactDOM.createPortal(
|
2021-03-15 00:01:52 +08:00
|
|
|
renderer.reactElement,
|
2021-03-15 01:00:50 +08:00
|
|
|
renderer.element,
|
|
|
|
key,
|
2021-03-13 04:21:13 +08:00
|
|
|
)
|
|
|
|
})}
|
2021-03-15 01:00:50 +08:00
|
|
|
</>
|
2021-03-13 04:21:13 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PureEditorContent extends React.Component<EditorContentProps, any> {
|
2021-03-09 06:10:10 +08:00
|
|
|
editorContentRef: React.RefObject<any>
|
|
|
|
|
|
|
|
constructor(props: EditorContentProps) {
|
2021-03-08 20:19:05 +08:00
|
|
|
super(props)
|
|
|
|
this.editorContentRef = React.createRef()
|
|
|
|
|
|
|
|
this.state = {
|
2021-03-13 04:21:13 +08:00
|
|
|
editor: this.props.editor,
|
|
|
|
renderers: new Map(),
|
2021-03-08 20:19:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
const { editor } = this.props
|
|
|
|
|
|
|
|
if (editor && editor.options.element) {
|
2021-03-13 04:21:13 +08:00
|
|
|
if (editor.contentComponent) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// this.setState({
|
|
|
|
// editor,
|
|
|
|
// })
|
|
|
|
|
2021-03-08 20:19:05 +08:00
|
|
|
const element = this.editorContentRef.current
|
|
|
|
|
|
|
|
element.appendChild(editor.options.element.firstChild)
|
|
|
|
|
|
|
|
editor.setOptions({
|
|
|
|
element,
|
|
|
|
})
|
|
|
|
|
|
|
|
editor.contentComponent = this
|
|
|
|
|
|
|
|
// TODO: why setTimeout?
|
|
|
|
setTimeout(() => {
|
|
|
|
editor.createNodeViews()
|
|
|
|
}, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2021-03-13 04:21:13 +08:00
|
|
|
<>
|
|
|
|
<div ref={this.editorContentRef} />
|
|
|
|
<Portals renderers={this.state.renderers} />
|
|
|
|
</>
|
2021-03-08 20:19:05 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 05:00:07 +08:00
|
|
|
|
2021-03-13 04:21:13 +08:00
|
|
|
export const EditorContent = React.memo(PureEditorContent)
|