Its difficult to explain so I recorded it. pgadmin-copy-cm-issue.mov - Google Drive
Basically, when I select something in codemirror, then go to some outside element and hit copy - then it still copies codemirror text. When I do the same thing but before going to outside element, I focus on a input element like textarea, then go to some outside element and hit copy - then it works fine.
Edited: The grid below handles keydown and on ctrl+c copies the code to clipboard. I tried to preventDefault/stopPropagation from grid but it didn’t work.
I don’t know what the page your are on in that screencast is doing with focus and DOM selection, but if I try to reproduce this with a simple focusable non-input element (a paragraph with tabindex=0), I cannot copy the previously-selected text from CodeMirror after I focus that element.
I create a stackblitz here - Vitejs - Vite (forked) - StackBlitz.
In this example, select the code in the CM window and then click on ‘click here’.
The winow.getSelection().isCollapsed remain false. And when Ctrl+C is pressed it copies from CM window.
There is still a ton of React stuff going on in that reproduction. I’m not going to pick through that to figure out what’s happening. If you can reduce this to a simple raw DOM setup, I can take a look.
This seems to be how browsers treat the selection when you click into a tabindex=0 element. If you use a simple <div contenteditable=true>abcd</div> element instead of the CodeMirror editor, the behavior you see is the same. The active selection remains in the editable element, even though the focus moved to the other element. I don’t think there’s a reason for CodeMirror to fight the native behavior in this case.