Fix Cmd+C copying only the first line when CodeMirror is the first child of ProseMirror #79

Merged
iamakulov merged 1 commit from fix/cmd-c into main 2026-01-14 06:27:06 +01:00
iamakulov commented 2026-01-11 23:45:18 +01:00 (Migrated from github.com)

This fixes an issue originally discovered in https://github.com/codemirror/view/pull/78#issuecomment-3729314747.

Issue

When CodeMirror is the first child of a ProseMirror editor, and you do Cmd+A Cmd+C in ProseMirror, only the first line of the CodeMirror is copied instead of the whole ProseMirror.

Steps to reproduce:

  • Make a ProseMirror document with CodeMirror being the first child of the editor
  • Press Cmd+A in the ProseMirror editor. (The whole ProseMirror editor should get selected)
  • Press Cmd+C

Expected behavior: the whole ProseMirror document is copied. Actual behavior: only the first line of the CodeMirror block is copied.

Affected browsers: Chrome, Safari. (Firefox is okay)

Demo: https://codesandbox.io/p/devbox/codemirror-workaround-with-zero-width-space-forked-lwdxcv?file=/src/index.html:30,15&workspaceId=ws_G7ZXTydwb2wV26Xn2zPSPT

This PR’s Solution

The root cause appears to be that when you do Cmd+A and then press Cmd+C, Chrome and Safari bubble the copy event up from the first child of the ProseMirror editor. When the first child is CodeMirror, it intercepts the copy event, checks its own internal selection (which, at that point, goes from position 0 to position 0), and copies line 0 of the code block.

The solution here is to ignore the copy event if the selection begins and ends outside of the CodeMirror editor.

Demo: To play with the fix, check the CodeSandbox above, but replace the import of @codemirror/view with @iammakulov/codemirror-view

Is this good enough? I don’t know!

  • Maybe we should ignore all events where the selection begins OR ends outside of the editor? The current fix is pretty conservative; doing the or could be more correct.
  • Maybe we should do something completely else? Very open to feedback.
This fixes an issue originally discovered in https://github.com/codemirror/view/pull/78#issuecomment-3729314747. ### Issue When CodeMirror is the first child of a ProseMirror editor, and you do Cmd+A Cmd+C in ProseMirror, only the first line of the CodeMirror is copied instead of the whole ProseMirror. **Steps to reproduce:** - Make a ProseMirror document with CodeMirror being the first child of the editor - Press Cmd+A in the ProseMirror editor. (The whole ProseMirror editor should get selected) - Press Cmd+C **Expected behavior:** the whole ProseMirror document is copied. **Actual behavior:** only the first line of the CodeMirror block is copied. **Affected browsers:** Chrome, Safari. (Firefox is okay) **Demo:** https://codesandbox.io/p/devbox/codemirror-workaround-with-zero-width-space-forked-lwdxcv?file=/src/index.html:30,15&workspaceId=ws_G7ZXTydwb2wV26Xn2zPSPT ### This PR’s Solution The root cause appears to be that when you do Cmd+A and then press Cmd+C, Chrome and Safari bubble the `copy` event up from the first child of the ProseMirror editor. When the first child is CodeMirror, it intercepts the `copy` event, checks _its own_ internal selection (which, at that point, goes from position 0 to position 0), and copies line 0 of the code block. The solution here is to ignore the `copy` event if the selection begins and ends outside of the CodeMirror editor. **Demo:** To play with the fix, check the CodeSandbox above, but replace the import of `@codemirror/view` with `@iammakulov/codemirror-view` **Is this good enough?** I don’t know! - Maybe we should ignore all events where the selection begins OR ends outside of the editor? The current fix is pretty conservative; doing the _or_ could be more correct. - Maybe we should do something completely else? Very open to feedback.
marijnh commented 2026-01-13 11:13:18 +01:00 (Migrated from github.com)

I think checking one side should be fine. You could use hasSelection from dom.ts, to be a bit less verbose. Can you make that change?

I think checking one side should be fine. You could use `hasSelection` from `dom.ts`, to be a bit less verbose. Can you make that change?
iamakulov commented 2026-01-14 00:48:47 +01:00 (Migrated from github.com)

Done! If you’d like to try out: published as @iamakulov/codemirror-view@9999.99.99-fixcmdc.1, updated the dependency in the codesandbox.

Done! If you’d like to try out: published as `@iamakulov/codemirror-view@9999.99.99-fixcmdc.1`, updated the dependency [in the codesandbox](https://codesandbox.io/p/devbox/codemirror-workaround-with-zero-width-space-forked-lwdxcv?file=/src/index.mjs:21,9&workspaceId=ws_G7ZXTydwb2wV26Xn2zPSPT).
marijnh commented 2026-01-14 06:27:26 +01:00 (Migrated from github.com)

Thanks!

Thanks!
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
codemirror/view!79
No description provided.