Skip to content

Commit

Permalink
fix(react): update select state when text selection is around node (u…
Browse files Browse the repository at this point in the history
  • Loading branch information
bdbch committed Jul 7, 2023
1 parent 0cb8c10 commit 405a264
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/ReactNodeViewRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class ReactNodeView extends NodeView<

const { className = '' } = this.options

this.handleSelectionUpdate = this.handleSelectionUpdate.bind(this)
this.editor.on('selectionUpdate', this.handleSelectionUpdate)

this.renderer = new ReactRenderer(ReactNodeViewProvider, {
editor: this.editor,
props,
Expand Down Expand Up @@ -127,6 +130,16 @@ class ReactNodeView extends NodeView<
return this.contentDOMElement
}

handleSelectionUpdate() {
const { from, to } = this.editor.state.selection

if (from <= this.getPos() && to >= this.getPos() + this.node.nodeSize) {
this.selectNode()
} else {
this.deselectNode()
}
}

update(node: ProseMirrorNode, decorations: DecorationWithType[]) {
const updateProps = (props?: Record<string, any>) => {
this.renderer.updateProps(props)
Expand Down Expand Up @@ -178,6 +191,7 @@ class ReactNodeView extends NodeView<

destroy() {
this.renderer.destroy()
this.editor.off('selectionUpdate', this.handleSelectionUpdate)
this.contentDOMElement = null
}
}
Expand Down

0 comments on commit 405a264

Please sign in to comment.