[Bug]: selection and getText are not in sync inside lists #5633
Labels
Category: Open Source
The issue or pull reuqest is related to the open source packages of Tiptap.
Type: Bug
The issue or pullrequest is related to a bug
Affected Packages
core
Version(s)
2.7.1
Bug Description
I'm trying to get the plain text content of the editor at a certain index near the current selection. In most cases I can do this easily with
editor.getText()
to get the text string, andeditor.view.state.selection
to get the cursor index. However, this doesn't work if the current selection is inside an ordered or unordered list.If I create an ordered or unordered list at the beginning of the editor content, the beginning of the current selection jumps from 1 to 3. For an ordered list, this makes some degree of sense to me, since there are two characters (
1.
) at the beginning of the line. It makes less sense for an unordered list, since there's only the one bullet point. The bigger problem, though, is thatgetText()
returns 4 newlines in place of1.
, so the selection index fromeditor.view.state.selection
is off from the text by 2.Weirdly, everytime I press enter to add a new list item, the selection index increases by 4, not 2, with the same 4 newlines added to the text, so the index is just consistently off by 2. If I indent one of the items to create a sublist, an additional two newlines are added to the text before that line, and the selection index does not change. Subsequent items added to the sublist have four newlines in the text. As soon as I exit the list, 2 newlines are added to the text and the selection increases by 4, which ends up correcting for the problem, so this only happens if the cursor is inside a list.
This is confusing and makes it difficult to grab the plain text near the selection. I don't particularly care about having the list numbers or bullets represented in getText (although it would be nice), but the selection index should at least stay in sync with the text position in getText. Is there a workaround for this? How can I get the actual current index of the cursor inside the text returned by getText?
Editing to add: My use case is I'm running a regex on the result of getText() to find sets of characters that match a pattern, and then trying to determine whether the cursor is between two of those character sets, so something like
editor.state.doc.getTextBetween
wouldn't really work since I'm using the text to find the indices and not the other way around.Browser Used
Chrome
Code Example URL
No response
Expected Behavior
I should be able to use
editor.view.state.selection
(or some other method) to get the current position of the cursor inside the string returned byeditor.getText()
Additional Context (Optional)
No response
Dependency Updates
The text was updated successfully, but these errors were encountered: