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-09 05:00:07 +08:00
|
|
|
|
2021-03-09 06:10:10 +08:00
|
|
|
type EditorContentProps = {
|
|
|
|
editor: Editor | null
|
|
|
|
}
|
|
|
|
|
2021-03-13 04:21:13 +08:00
|
|
|
const Portals = ({ renderers }: { renderers: Map<any, any> }) => {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{Array.from(renderers).map(([key, renderer]) => {
|
2021-03-14 23:30:06 +08:00
|
|
|
return ReactDOM.createPortal(
|
|
|
|
renderer.comp,
|
|
|
|
renderer.teleportElement,
|
|
|
|
renderer.id,
|
2021-03-13 04:21:13 +08:00
|
|
|
)
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// const Content = React.memo(({ reference }: { reference: React.RefObject<any> }) => {
|
|
|
|
// return (
|
|
|
|
// <div ref={reference} />
|
|
|
|
// )
|
|
|
|
// })
|
|
|
|
|
|
|
|
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() {
|
2021-03-14 23:30:06 +08:00
|
|
|
// console.log('render', this.state)
|
2021-03-13 04:21:13 +08:00
|
|
|
// console.log('render', this.props.editor, this.state.editor)
|
2021-03-08 20:19:05 +08:00
|
|
|
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)
|