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

fix(core): check if pos is valid before resolving in backspace core keymap handler #4835

Merged
merged 2 commits into from
Apr 5, 2024

Conversation

solastley
Copy link
Contributor

Please describe your changes

So I'm working on a TipTap editor where I don't want to allow block elements in the document. Not exactly a common use case I'm guessing, but it makes sense for what I'm doing.

I discovered that when I override the content of the Document extension to only allow text like so...

Document.extend({
    content: 'text*',
})

...it causes an error to be thrown whenever I press Backspace and either:

  1. The editor is empty OR
  2. All of the text is selected with the anchor at the beginning of the text

The error comes from the backspace keyboard shortcut handler which is added in TipTap core. This handler attempts to resolve a position in the document at pos - 1 where pos is equal to editor.state.selection.$anchor.pos. I think this works fine when there are block elements in the editor since even an empty editor will still have at least an empty block element. Whenever only text is allowed, though, this pos is equal to zero whenever the editor is empty. This results in the handler attempting to resolve the position in the document at a pos of -1, which throws an error.

How did you accomplish your changes

Added a check that pos > 0 before attempting to resolve pos - 1 in the document.

How have you tested your changes

Tested both cases where I found the error manually, and with this check, the error no longer occurs.

I also tested backspace in a more typical editor configuration with the default Document extension. Everything works as expected, including backspace when there is a lot of text, backspace when there is no text, and backspace when all text is selected.

How can we verify your changes

To reproduce bug:

  1. Create an editor and extend Document as I've outlined above.
  2. Press Backspace when the editor is empty
  3. See the error

To verify fix, repeat the process with my changes applied and see the error is gone.

Remarks

Loving TipTap so far :)

Checklist

  • The changes are not breaking the editor
  • Added tests where possible
  • Followed the guidelines
  • Fixed linting issues

Related issues

N/A

Copy link

netlify bot commented Jan 26, 2024

Deploy Preview for tiptap-embed ready!

Name Link
🔨 Latest commit e79bde0
🔍 Latest deploy log https://app.netlify.com/sites/tiptap-embed/deploys/65b8c2c0e32c1f0008762f36
😎 Deploy Preview https://deploy-preview-4835--tiptap-embed.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@bdbch bdbch changed the base branch from develop to main January 30, 2024 09:35
Copy link
Contributor

@bdbch bdbch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - @svenadlung any thoughts on this?

@bdbch bdbch changed the title fix: check if pos is valid before resolving in backspace core keymap handler fix(core): check if pos is valid before resolving in backspace core keymap handler Jan 30, 2024
@alexmalev
Copy link

Please describe your changes

So I'm working on a TipTap editor where I don't want to allow block elements in the document. Not exactly a common use case I'm guessing, but it makes sense for what I'm doing.

I discovered that when I override the content of the Document extension to only allow text like so...

Document.extend({
    content: 'text*',
})

...it causes an error to be thrown whenever I press Backspace and either:

  1. The editor is empty OR
  2. All of the text is selected with the anchor at the beginning of the text

The error comes from the backspace keyboard shortcut handler which is added in TipTap core. This handler attempts to resolve a position in the document at pos - 1 where pos is equal to editor.state.selection.$anchor.pos. I think this works fine when there are block elements in the editor since even an empty editor will still have at least an empty block element. Whenever only text is allowed, though, this pos is equal to zero whenever the editor is empty. This results in the handler attempting to resolve the position in the document at a pos of -1, which throws an error.

How did you accomplish your changes

Added a check that pos > 0 before attempting to resolve pos - 1 in the document.

How have you tested your changes

Tested both cases where I found the error manually, and with this check, the error no longer occurs.

I also tested backspace in a more typical editor configuration with the default Document extension. Everything works as expected, including backspace when there is a lot of text, backspace when there is no text, and backspace when all text is selected.

How can we verify your changes

To reproduce bug:

  1. Create an editor and extend Document as I've outlined above.
  2. Press Backspace when the editor is empty
  3. See the error

To verify fix, repeat the process with my changes applied and see the error is gone.

Remarks

Loving TipTap so far :)

Checklist

  • The changes are not breaking the editor
  • Added tests where possible
  • Followed the guidelines
  • Fixed linting issues

Related issues

N/A

very much waiting for this. same exact problem. thanks!

@solastley
Copy link
Contributor Author

@svenadlung gentle bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants