Skip to content

fix issue with code pasting from VS Code when at the last line of code#5106

Merged
bdbch merged 1 commit into
mainfrom
fix/code-pasting
Apr 30, 2024
Merged

fix issue with code pasting from VS Code when at the last line of code#5106
bdbch merged 1 commit into
mainfrom
fix/code-pasting

Conversation

@bdbch

@bdbch bdbch commented Apr 30, 2024

Copy link
Copy Markdown
Member

Please describe your changes

This PR fixes an minor issue where code copied from VSCode wouldn't be correctly pasted at the end of a document because replaceSelectionWith would cause a buggy insertion when the end position of the selection is in a node at the end of the document.

How did you accomplish your changes

I'm checking if the cursor is at the end - if so I insert the code block before the current position - otherwise I use the replaceSelectionWith function

How have you tested your changes

Tested it locally in the default demo

How can we verify your changes

See above

Checklist

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

@bdbch
bdbch requested a review from svenadlung as a code owner April 30, 2024 14:20
@netlify

netlify Bot commented Apr 30, 2024

Copy link
Copy Markdown

Deploy Preview for tiptap-embed ready!

Name Link
🔨 Latest commit d426463
🔍 Latest deploy log https://app.netlify.com/sites/tiptap-embed/deploys/6630fe34c82f2300089f37df
😎 Deploy Preview https://deploy-preview-5106--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.

// if the cursor is at the absolute end of the document, insert the code block before the cursor instead
// of replacing the selection as the replaceSelectionWith function will cause the insertion to
// happen at the previous node
if (view.state.selection.from === view.state.doc.nodeSize - (1 + (view.state.selection.$to.depth * 2))) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not going to pretend that I completely understand this. What is tripping me up is the * 2.

But otherwise this looks right

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The 2 is the offset of a prosemirror node to the next one. If you for example have a nested node - the wrapping node will always add an offset of 1 at the beginning and the end which you have to account for if you want to find the correct position after said node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants