From 405a264c24a35562e09f0740246110d0f1154048 Mon Sep 17 00:00:00 2001 From: Dominik <6538827+bdbch@users.noreply.github.com> Date: Fri, 7 Jul 2023 11:21:58 +0200 Subject: [PATCH] fix(react): update select state when text selection is around node (#4148) --- src/ReactNodeViewRenderer.tsx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/ReactNodeViewRenderer.tsx b/src/ReactNodeViewRenderer.tsx index 899907c585e..a75b47e111f 100644 --- a/src/ReactNodeViewRenderer.tsx +++ b/src/ReactNodeViewRenderer.tsx @@ -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, @@ -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) => { this.renderer.updateProps(props) @@ -178,6 +191,7 @@ class ReactNodeView extends NodeView< destroy() { this.renderer.destroy() + this.editor.off('selectionUpdate', this.handleSelectionUpdate) this.contentDOMElement = null } }