fix(clipboard): use ProseMirror selection state for Shadow DOM compatibility#2677
fix(clipboard): use ProseMirror selection state for Shadow DOM compatibility#2677wielinde wants to merge 2 commits intoTypeCellOS:mainfrom
Conversation
…ibility
OpenProject embeds BlockNote inside a Shadow DOM (attachShadow({ mode: 'open' }))
to isolate it from the host Angular application. In this setup,
window.getSelection() returns null or a collapsed selection even when text is
selected (Firefox all versions, Safari ≤16.3, Chromium edge cases), causing
checkIfSelectionInNonEditableBlock to always return true and skip the
clipboard write entirely. The browser's default copy then fires, which uses
ProseMirror's DOMSerializer without semantic wrappers — so list formatting,
headings, and bold/italic are lost on paste into external apps.
Fix: use view.state.selection.empty as the primary empty-selection guard.
ProseMirror's internal state is always accurate regardless of DOM mode. The
DOM-level non-editable-island check is kept as a secondary guard, but only
when window.getSelection() actually returns a non-collapsed selection.
Fixes copy/cut for editors mounted inside attachShadow({ mode: 'open' }).
fix(clipboard): use ProseMirror selection state for Shadow DOM compatibility
|
@wielinde is attempting to deploy a commit to the TypeCell Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe change refactors the Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes 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 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 |
Problem
BlockNote embeds ProseMirror inside editors that may be mounted in a Shadow DOM (e.g. OpenProject uses
attachShadow({ mode: 'open' })to isolate the editor from its Angular host). In this setup,window.getSelection()returnsnullor a collapsed selection even when text is visually selected — this affects Firefox (all versions), Safari ≤16.3, and Chromium edge cases.checkIfSelectionInNonEditableBlockusedwindow.getSelection()as its primary empty-selection guard. BecausegetSelection()misfires in Shadow DOM, the guard always returnedtrue, causing both thecopyandcuthandlers to bail out early without writing to the clipboard. The browser's default copy then ran, which uses ProseMirror'sDOMSerializerwithout BlockNote's semantic wrappers — losing list formatting, headings, and bold/italic on paste into external apps.Fix
Use
view.state.selection.emptyas the primary guard. ProseMirror's internal selection state is updated via its own reconciliation loop and is always accurate regardless of DOM mode.The non-editable-block DOM walk (which does need
window.getSelection()) is kept as a secondary guard, but only runs whengetSelection()actually returns a non-collapsed selection — so it's a no-op in Shadow DOM environments wheregetSelection()is unreliable.Test
Verified in OpenProject (Shadow DOM setup) with a bullet list: the clipboard now contains all three expected types —
blocknote/html,text/html(with<ul>/<li>), andtext/plain(markdown* item).Summary by CodeRabbit