Hi there!
I am trying to find the Mark of a given type (if one exists) that is at the current cursor position or between the current selection if the selection is within the mark “bounds” (included).
Here is the function I wrote:
getMarkForCursorOrSelectionBetweenMarkOfType(state: EditorState, markType: MarkType): Mark {
const selection = state.selection;
if (selection.empty) {
return markType.isInSet(state.storedMarks || selection.$from.marks());
}
const fromMark = markType.isInSet(selection.$from.marks());
const toMark = markType.isInSet(selection.$to.marks());
if (fromMark === toMark) {
return fromMark;
}
return null;
}
The logic to find the mark of a given type at the cursor position works well. The issue I have is when a range selection is performed. The condition above returns the mark of a given type such that the mark is between the range selection but bounds excluded. Here is a visual example of the code above running:
-
Cursor between m and p -> link mark returned -> OK
-
Selection is “is a sa” -> no mark returned -> OK
-
Selection is “e, nothi” -> no mark returned -> OK
-
Selection is “ample” -> link mark returned -> OK
-
Selection is “mp” -> link mark returned -> OK
-
Selection is “sam” -> link mark not returned -> NOT OK
-
Selection is “ple” -> link mark not returned -> NOT OK
-
Selection is “sample” -> link mark not returned -> NOT OK
What I would like to do is to consider the mark bounds (bounds included) so that when I select sam, ple or sample, the mark is returned. The problem seems to be with selection.$from.marks()
and selection.$to.marks()
that respectively ignore the leading and trailing character. How could I make them consider the leading and trailing character?