fix: Pasting plain text from VSCode (BLO-366)#2713
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughThe PR modifies clipboard paste handling for VSCode-formatted data. The ChangesClipboard Paste Control Flow
Estimated code review effort🎯 2 (Simple) | ⏱️ ~15 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/core/src/api/clipboard/fromClipboard/handleVSCodePaste.ts (1)
21-22:⚠️ Potential issue | 🟠 Major | ⚡ Quick winGuard
vscode-editor-dataparsing so fallback still works.
JSON.parsecan throw for malformed clipboard metadata, which would abort paste handling beforepasteExtensioncan fall back totext/plain. Returnfalseon parse failure instead.Proposed fix
- const vscode = event.clipboardData!.getData("vscode-editor-data"); - const vscodeData = vscode ? JSON.parse(vscode) : undefined; - const language = vscodeData?.mode; + const vscode = event.clipboardData!.getData("vscode-editor-data"); + let language: string | undefined; + if (vscode) { + try { + const vscodeData = JSON.parse(vscode) as { mode?: unknown }; + language = + typeof vscodeData.mode === "string" ? vscodeData.mode : undefined; + } catch { + return false; + } + }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/core/src/api/clipboard/fromClipboard/handleVSCodePaste.ts` around lines 21 - 22, Guard the `vscode` JSON parse so malformed `vscode-editor-data` doesn't throw: wrap the `JSON.parse(vscode)` call in a try/catch (where `vscode` and `vscodeData` are used) and on any parse error set `vscodeData` to undefined (or return false from `handleVSCodePaste` immediately) so the function falls back to `pasteExtension`/`text/plain`; ensure the `language` assignment uses the safely-parsed `vscodeData?.mode` and that parse failures do not abort paste handling.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@packages/core/src/api/clipboard/fromClipboard/handleVSCodePaste.ts`:
- Around line 21-22: Guard the `vscode` JSON parse so malformed
`vscode-editor-data` doesn't throw: wrap the `JSON.parse(vscode)` call in a
try/catch (where `vscode` and `vscodeData` are used) and on any parse error set
`vscodeData` to undefined (or return false from `handleVSCodePaste` immediately)
so the function falls back to `pasteExtension`/`text/plain`; ensure the
`language` assignment uses the safely-parsed `vscodeData?.mode` and that parse
failures do not abort paste handling.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: c1f3f52c-83c6-4927-90f9-1173367f6060
📒 Files selected for processing (2)
packages/core/src/api/clipboard/fromClipboard/handleVSCodePaste.tspackages/core/src/api/clipboard/fromClipboard/pasteExtension.ts
@blocknote/ariakit
@blocknote/code-block
@blocknote/core
@blocknote/mantine
@blocknote/react
@blocknote/server-util
@blocknote/shadcn
@blocknote/xl-ai
@blocknote/xl-docx-exporter
@blocknote/xl-email-exporter
@blocknote/xl-multi-column
@blocknote/xl-odt-exporter
@blocknote/xl-pdf-exporter
commit: |
nperez0111
left a comment
There was a problem hiding this comment.
Good catch @matthewlipski
Summary
When VSCode content is on the clipboard, it stores content in the
plain/textMIME type. It also contains a bunch of information about the source, like the language of the code being pasted, which depends on the source file type.BlockNote always attempts to parse VSCode content as a code block. If this fails for whatever reason, e.g. code blocks are not in the schema, a language wasn't found, etc, it won't paste anything.
This PR makes it so that if
handleVSCodePasteis unable to parse thetext/plaincontent as a code block, we attempt to parse it the usual way.Rationale
While minor, it's not that uncommon for people to have plain text files in VSCode, or files without an ending. Copy/pasting from these into BlockNote is totally broken right now, so this should be fixed.
Changes
handleVSCodePastereturns false,formatis set totext/plainand the content is parsed normally.handleVSCodePastefor all branches where the content cannot be parsed as a code block.Impact
N/A
Testing
Our unit testing setup doesn't support adding custom content types to the clipboard the way that VSCode does, so we can't really replicate VSCode clipboard content for testing.
Screenshots/Video
N/A
Checklist
Additional Notes
N/A
Summary by CodeRabbit