-
Notifications
You must be signed in to change notification settings - Fork 205
Support Command shortcuts in Crumble on macOS #1065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GautamNambiar
wants to merge
12
commits into
quantumlib:main
Choose a base branch
from
GautamNambiar:crumble-mac-shortcuts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9fe9158
Add support for meta key in keyboard shortcuts for Crumble
GautamNambiar deef92c
Allow metaKey in getFocusedRow and getFocusedCol
GautamNambiar 041d9db
Update keyboard shortcuts in README.md to allow command key
GautamNambiar d3440dc
Update keyboard shortcuts for buttons in crumble.html
GautamNambiar 888d6e8
Refactor keyboard shortcuts for cut and paste, support for cmd
GautamNambiar 08a942d
Regenerate Crumble embedded resource
GautamNambiar 4d109be
Merge branch 'main' into crumble-mac-shortcuts
GautamNambiar 41cf451
Removed the cmd+v feature because it created bugs
GautamNambiar a4382dd
Remove standalone Crumble test artifact
GautamNambiar 620ef88
Support Command paste in Crumble on macOS
GautamNambiar 2c610da
Update glue/crumble/README.md
GautamNambiar fca85a1
Address part of Crumble shortcut review feedback by getting rid of du…
GautamNambiar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -197,7 +197,7 @@ editorState.canvas.addEventListener('mouseup', ev => { | |
| editorState.mouseDownY = undefined; | ||
| editorState.curMouseX = ev.offsetX + OFFSET_X; | ||
| editorState.curMouseY = ev.offsetY + OFFSET_Y; | ||
| editorState.changeFocus(highlightedArea, ev.shiftKey, ev.ctrlKey); | ||
| editorState.changeFocus(highlightedArea, ev.shiftKey, ev.ctrlKey || ev.metaKey); | ||
| if (ev.buttons === 1) { | ||
| isInScrubber = false; | ||
| } | ||
|
|
@@ -222,17 +222,8 @@ function makeChordHandlers() { | |
| res.set('ctrl+shift+z', preview => { if (!preview) editorState.redo() }); | ||
| res.set('ctrl+c', async preview => { await copyToClipboard(); }); | ||
| res.set('ctrl+v', pasteFromClipboard); | ||
| res.set('ctrl+x', async preview => { | ||
| await copyToClipboard(); | ||
| if (editorState.focusedSet.size === 0) { | ||
| let c = editorState.copyOfCurCircuit(); | ||
| c.layers[editorState.curLayer].id_ops.clear(); | ||
| c.layers[editorState.curLayer].markers.length = 0; | ||
| editorState.commit_or_preview(c, preview); | ||
| } else { | ||
| editorState.deleteAtFocus(preview); | ||
| } | ||
| }); | ||
| res.set('ctrl+x', cutToClipboard); | ||
|
|
||
| res.set('l', preview => { | ||
| if (!preview) { | ||
| editorState.timelineSet = new Map(editorState.focusedSet.entries()); | ||
|
|
@@ -360,6 +351,8 @@ function makeChordHandlers() { | |
| } | ||
|
|
||
| let fallbackEmulatedClipboard = undefined; | ||
| let pendingMetaPaste = false; | ||
| let pendingMetaPasteTimeout = undefined; | ||
| async function copyToClipboard() { | ||
| let c = editorState.copyOfCurCircuit(); | ||
| c.layers = [c.layers[editorState.curLayer]] | ||
|
|
@@ -397,6 +390,14 @@ async function pasteFromClipboard(preview) { | |
| return; | ||
| } | ||
|
|
||
| pasteTextAtFocus(text, preview); | ||
| } | ||
|
|
||
| /** | ||
| * @param {!string} text | ||
| * @param {!boolean} preview | ||
| */ | ||
| function pasteTextAtFocus(text, preview) { | ||
| let pastedCircuit = Circuit.fromStimCircuit(text); | ||
| if (pastedCircuit.layers.length !== 1) { | ||
| throw new Error(text); | ||
|
|
@@ -442,12 +443,86 @@ async function pasteFromClipboard(preview) { | |
| editorState.commit_or_preview(newCircuit, preview); | ||
| } | ||
|
|
||
| function clearPendingMetaPaste() { | ||
| pendingMetaPaste = false; | ||
| if (pendingMetaPasteTimeout !== undefined) { | ||
| clearTimeout(pendingMetaPasteTimeout); | ||
| pendingMetaPasteTimeout = undefined; | ||
| } | ||
| } | ||
|
|
||
| async function cutToClipboard(preview) { | ||
| await copyToClipboard(); | ||
| if (editorState.focusedSet.size === 0) { | ||
| let c = editorState.copyOfCurCircuit(); | ||
| c.layers[editorState.curLayer].id_ops.clear(); | ||
| c.layers[editorState.curLayer].markers.length = 0; | ||
| editorState.commit_or_preview(c, preview); | ||
| } else { | ||
| editorState.deleteAtFocus(preview); | ||
| } | ||
| } | ||
|
|
||
| const CHORD_HANDLERS = makeChordHandlers(); | ||
| /** | ||
| * @param {!KeyboardEvent} ev | ||
| */ | ||
| function handleKeyboardEvent(ev) { | ||
| async function handleKeyboardEvent(ev) { | ||
| if (ev.type === 'keydown' && ev.metaKey) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't these better be right next to the other chord handlers ( |
||
| if (ev.repeat) { | ||
| ev.preventDefault(); | ||
| editorState.chorder.handleFocusChanged(); | ||
| return; | ||
| } | ||
|
|
||
| let key = ev.key.toLowerCase(); | ||
|
|
||
| if (key === 'z' && !ev.shiftKey) { | ||
| ev.preventDefault(); | ||
| editorState.chorder.handleFocusChanged(); | ||
| editorState.undo(); | ||
| return; | ||
| } | ||
| if ((key === 'z' && ev.shiftKey) || key === 'y') { | ||
| ev.preventDefault(); | ||
| editorState.chorder.handleFocusChanged(); | ||
| editorState.redo(); | ||
| return; | ||
| } | ||
| if (key === 'c') { | ||
| ev.preventDefault(); | ||
| editorState.chorder.handleFocusChanged(); | ||
| await copyToClipboard(); | ||
| return; | ||
| } | ||
| if (key === 'v') { | ||
| editorState.chorder.handleFocusChanged(); | ||
| pendingMetaPaste = true; | ||
| pendingMetaPasteTimeout = setTimeout(clearPendingMetaPaste, 1000); | ||
| return; | ||
| } | ||
| if (key === 'x') { | ||
| ev.preventDefault(); | ||
| editorState.chorder.handleFocusChanged(); | ||
| await cutToClipboard(false); | ||
| return; | ||
| } | ||
| if (key === 'backspace' || key === 'delete') { | ||
| ev.preventDefault(); | ||
| editorState.chorder.handleFocusChanged(); | ||
| editorState.deleteCurLayer(false); | ||
| return; | ||
| } | ||
| if (key === 'enter') { | ||
| ev.preventDefault(); | ||
| editorState.chorder.handleFocusChanged(); | ||
| editorState.insertLayer(false); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| editorState.chorder.handleKeyEvent(ev); | ||
|
|
||
| if (ev.type === 'keydown') { | ||
| if (ev.key.toLowerCase() === 'q') { | ||
| let d = ev.shiftKey ? 5 : 1; | ||
|
|
@@ -511,6 +586,20 @@ function handleKeyboardEvent(ev) { | |
| } | ||
| } | ||
|
|
||
| document.addEventListener('paste', ev => { | ||
| if (!pendingMetaPaste) { | ||
| return; | ||
| } | ||
| clearPendingMetaPaste(); | ||
|
|
||
| let text = ev.clipboardData.getData('text/plain'); | ||
| if (text === '') { | ||
| return; | ||
| } | ||
|
|
||
| ev.preventDefault(); | ||
| pasteTextAtFocus(text, false); | ||
| }); | ||
| document.addEventListener('keydown', handleKeyboardEvent); | ||
| document.addEventListener('keyup', handleKeyboardEvent); | ||
|
|
||
|
|
@@ -532,7 +621,7 @@ window.addEventListener('blur', () => { | |
| for (let anchor of document.getElementById('examples-div').querySelectorAll('a')) { | ||
| anchor.onclick = ev => { | ||
| // Don't stop the user from e.g. opening the example in a new tab using ctrl+click. | ||
| if (ev.shiftKey || ev.ctrlKey || ev.altKey || ev.button !== 0) { | ||
| if (ev.shiftKey || ev.ctrlKey || ev.metaKey || ev.altKey || ev.button !== 0) { | ||
| return undefined; | ||
| } | ||
| let circuitText = anchor.href.split('#circuit=')[1]; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very similar to pasteFromClipboard. This code should be written once.