Support Command shortcuts in Crumble on macOS#1065
Conversation
Updated keyboard shortcuts to support the use of macOS command key to substitute for all instances of the control key.
mac users can use command key instead of control key. For Windows keyboard users, since the meta key is the Windows key, this means that the control key can be replaced by the Windows key. (This is a harmless side effect.)
cmd can be used instead of ctrl
Fixing issues with keydown when pressing cmd
|
The main issue that prompted me to make this fix was that ctrl+click on mac usually leads to the equivalent of a right click in Windows. So I had found difficulty with group-selecting two sets of qubits (as a symmetric difference). The PR fixes it. |
| * @param {!boolean} preview | ||
| */ | ||
| function pasteTextFromClipboardEvent(text, preview) { | ||
| let pastedCircuit = Circuit.fromStimCircuit(text); |
There was a problem hiding this comment.
This is very similar to pasteFromClipboard. This code should be written once.
There was a problem hiding this comment.
Thanks! This is fixed now. Please also see my reply to the next comment for further clarification.
|
Description says that Cmd+V behaviour was unchanged, but the code does have it in scope. |
| */ | ||
| function handleKeyboardEvent(ev) { | ||
| async function handleKeyboardEvent(ev) { | ||
| if (ev.type === 'keydown' && ev.metaKey) { |
There was a problem hiding this comment.
Wouldn't these better be right next to the other chord handlers (makeChordHandlers) with meta+z etc.?
There was a problem hiding this comment.
I had originally tried what you suggested: meta+... chord handlers. But on my browsers (chrome and Safari, macOS), doing so, i.e. using the chord preview/finalization flow made shortcuts like cmd+Z and cmd+Enter unreliable. I don't know why, but handling these shortcuts immediately when the key is pressed made them consistent (this was suggested by chatGPT). It seems that for these shortcuts, bypassing the preview system seems to be harmless. Existing ctrl shortcuts still go through Crumble’s chord handler.
So I kept the cmd shortcut handling separate from makeChordHandlers.
cmd+V has one extra difference: it uses the browser paste event to get pasted text, instead of directly calling navigator.clipboard.readText(). This avoided the browser clipboard-read permission prompt I saw during testing.
ctrl+V still gets text using the existing navigator.clipboard.readText() path.
After the pasted text is obtained, cmd+V and ctrl+V now both call the same pasteTextAtFocus helper, so the actual Crumble editing behavior is shared (so the duplication is fixed).
A short comment has been added above the helper explaining why it exists.
I should note that I know very little about web development. I was asking chatGPT, testing, asking again and so on. So, probably there is a cleaner way to do this...
Co-authored-by: Iftach Yakar <DeDuckProject@users.noreply.github.com>
Sorry, my bad. I initially wanted to separate the fix to cmd +v from everything else (because cmd+v was leading to browser permission prompts to look into the clipboard, and so this had to be treated separately). But I messed up with version control. Anyway, now, cmd+v is in scope and I have updated the description. Please review again if you get a chance. Thanks! |
Adds macOS Command-key support for Crumble editing shortcuts, excluding paste.
Changes include:
Command shortcuts are handled directly when the key is pressed because, in local macOS testing, routing them through Crumble’s chord preview/finalization flow made shortcuts like Command+Z and Command+Enter unreliable.
Command+V uses the browser paste event instead of directly reading from
navigator.clipboard, avoiding clipboard-read permission prompts while preserving the existing Control+V path.Addresses #651.
I worked with ChatGPT to help understand the code and make edits.