Fix Cmd+C copying only the first line when CodeMirror is the first child of ProseMirror #79
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "fix/cmd-c"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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:
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
copyevent up from the first child of the ProseMirror editor. When the first child is CodeMirror, it intercepts thecopyevent, 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
copyevent 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/viewwith@iammakulov/codemirror-viewIs this good enough? I don’t know!
I think checking one side should be fine. You could use
hasSelectionfromdom.ts, to be a bit less verbose. Can you make that change?Done! If you’d like to try out: published as
@iamakulov/codemirror-view@9999.99.99-fixcmdc.1, updated the dependency in the codesandbox.Thanks!