fix: fix using react node views with insertContent

This commit is contained in:
Philipp Kühn
2021-04-11 14:03:16 +02:00
parent 009a948cae
commit ea0992f66e

View File

@@ -20,6 +20,8 @@ class ReactNodeView extends NodeView<React.FunctionComponent, Editor> {
renderer!: ReactRenderer renderer!: ReactRenderer
contentDOMElement!: Element | null
mount() { mount() {
const props: NodeViewProps = { const props: NodeViewProps = {
editor: this.editor, editor: this.editor,
@@ -52,6 +54,10 @@ class ReactNodeView extends NodeView<React.FunctionComponent, Editor> {
ReactNodeViewProvider.displayName = 'ReactNodeView' ReactNodeViewProvider.displayName = 'ReactNodeView'
this.contentDOMElement = this.node.isLeaf
? null
: document.createElement(this.node.isInline ? 'span' : 'div')
this.renderer = new ReactRenderer(ReactNodeViewProvider, { this.renderer = new ReactRenderer(ReactNodeViewProvider, {
editor: this.editor, editor: this.editor,
props, props,
@@ -62,7 +68,10 @@ class ReactNodeView extends NodeView<React.FunctionComponent, Editor> {
} }
get dom() { get dom() {
if (!this.renderer.element.firstElementChild?.hasAttribute('data-node-view-wrapper')) { 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.') throw Error('Please use the NodeViewWrapper component for your node view.')
} }
@@ -76,7 +85,15 @@ class ReactNodeView extends NodeView<React.FunctionComponent, Editor> {
const contentElement = this.dom.querySelector('[data-node-view-content]') const contentElement = this.dom.querySelector('[data-node-view-content]')
return contentElement || this.dom if (
this.contentDOMElement
&& contentElement
&& !contentElement.contains(this.contentDOMElement)
) {
contentElement.appendChild(this.contentDOMElement)
}
return this.contentDOMElement
} }
update(node: ProseMirrorNode, decorations: Decoration[]) { update(node: ProseMirrorNode, decorations: Decoration[]) {
@@ -113,6 +130,7 @@ class ReactNodeView extends NodeView<React.FunctionComponent, Editor> {
destroy() { destroy() {
this.renderer.destroy() this.renderer.destroy()
this.contentDOMElement = null
} }
} }