Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: selection and getText are not in sync inside lists #5633

Open
1 task done
pownkel opened this issue Sep 19, 2024 · 0 comments
Open
1 task done

[Bug]: selection and getText are not in sync inside lists #5633

pownkel opened this issue Sep 19, 2024 · 0 comments
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

Comments

@pownkel
Copy link

pownkel commented Sep 19, 2024

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, and editor.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 that getText() returns 4 newlines in place of 1., so the selection index from editor.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 by editor.getText()

Additional Context (Optional)

No response

Dependency Updates

  • Yes, I've updated all my dependencies.
@pownkel pownkel added 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 labels Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
Status: Triage open
Development

No branches or pull requests

1 participant