GapCursor.near and GapCursor.findFrom error

The following code produces an error:

const tr = state.tr;
const hr = state.schema.nodes.hr.create();
tr.replaceSelectionWith(hr);
if (tr.selection instanceof NodeSelection) {
	const near = GapCursor.near(tr.selection.$to, 1);
	tr.setSelection(near);
}

dispatch(tr);

image image

I’ve had to resort to doing this instead:

const near = TextSelection.near(tr.selection.$to, 1);
tr.setSelection(
	new GapCursor(near.$to, near.$to)
);

The error strikes me as a bit strange, so I figured I’d ask if it could be a bug before I dive too deep into figuring out what I’m doing wrong. It happens with GapCursor.findFrom as well.

I’m not sure what the error is, but GapCursor.near is just the inherited static method Selection.near, so I very much doubt it does what you are looking for here.

Interesting – I wonder then why using GapCursor.near results in an error and Selection.near does not :confused:

I was hoping to use this to create a gap cursor in the nearest valid position. Is there a better way?

But yeah, I think I’m confused about what Selection.near is for, especially in the derived classes. I just realized that TextSelection.near can sometimes result in a NodeSelection :thinking: