Skip to content

Commit

Permalink
fix: fix using react node views with insertContent
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed Apr 11, 2021
1 parent 009a948 commit ea0992f
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/react/src/ReactNodeViewRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class ReactNodeView extends NodeView<React.FunctionComponent, Editor> {

renderer!: ReactRenderer

contentDOMElement!: Element | null

mount() {
const props: NodeViewProps = {
editor: this.editor,
Expand Down Expand Up @@ -52,6 +54,10 @@ class ReactNodeView extends NodeView<React.FunctionComponent, Editor> {

ReactNodeViewProvider.displayName = 'ReactNodeView'

this.contentDOMElement = this.node.isLeaf
? null
: document.createElement(this.node.isInline ? 'span' : 'div')

this.renderer = new ReactRenderer(ReactNodeViewProvider, {
editor: this.editor,
props,
Expand All @@ -62,7 +68,10 @@ class ReactNodeView extends NodeView<React.FunctionComponent, Editor> {
}

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.')
}

Expand All @@ -76,7 +85,15 @@ class ReactNodeView extends NodeView<React.FunctionComponent, Editor> {

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[]) {
Expand Down Expand Up @@ -113,6 +130,7 @@ class ReactNodeView extends NodeView<React.FunctionComponent, Editor> {

destroy() {
this.renderer.destroy()
this.contentDOMElement = null
}
}

Expand Down

0 comments on commit ea0992f

Please sign in to comment.