Is this the right way to determine if a NodeView is selected?

constructor(node, view, getPos) {

		const { state } = view;
		const selection = state.selection;

		this._selected = false;

		state.doc.nodesBetween(selection.from, selection.to, (selected_node, position) => {
			if ( selected_node === node ) {
				this._selected = true;
			}
		});

		this.renderElement(node, view, getPos);
	    
  	}

No. Node identity (==) isn’t meaningful in ProseMirror (the same node object might occur multiple times in a document). Checking whether getPos() is >= selection.from and getPos() + node.nodeSize <= selection.to should work better.

1 Like

Awesome, thankyou.

Seems like I might be attacking this problem the wrong way… now I have an issue that deselectNode isn’t called when I click elsewhere.

Some background… when the below code runs after the image is resized it causes the nodeView to recreate itself and so I use the above code to show the resize handles, because setSelection doesn’t seem to call selectNode… ?

const imgselection = TextSelection.create(
	      	tr.doc,
	      	selection.from,
	      	selection.from + 1
	    );

    	tr = tr.setSelection(imgselection);

    	if (tr.docChanged) {
			dispatch(tr);
		}

If I use NodeSelection instead of TextSelection everything seems to work as expected, I guess this is where I was going wrong.