-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support copy/pasting between tabs and projects (#10366)
* support pasting between tabs and projects * pr feedback
- Loading branch information
Showing
4 changed files
with
307 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import * as Blockly from "blockly"; | ||
import { getCopyPasteHandlers } from "./external"; | ||
|
||
let oldCopy: Blockly.ShortcutRegistry.KeyboardShortcut; | ||
let oldCut: Blockly.ShortcutRegistry.KeyboardShortcut; | ||
let oldPaste: Blockly.ShortcutRegistry.KeyboardShortcut; | ||
|
||
export function initCopyPaste() { | ||
if (oldCopy) return; | ||
|
||
const shortcuts = Blockly.ShortcutRegistry.registry.getRegistry() | ||
|
||
oldCopy = { ...shortcuts[Blockly.ShortcutItems.names.COPY] }; | ||
oldCut = { ...shortcuts[Blockly.ShortcutItems.names.CUT] }; | ||
oldPaste = { ...shortcuts[Blockly.ShortcutItems.names.PASTE] }; | ||
|
||
Blockly.ShortcutRegistry.registry.unregister(Blockly.ShortcutItems.names.COPY); | ||
Blockly.ShortcutRegistry.registry.unregister(Blockly.ShortcutItems.names.CUT); | ||
Blockly.ShortcutRegistry.registry.unregister(Blockly.ShortcutItems.names.PASTE); | ||
|
||
registerCopy(); | ||
registerCut(); | ||
registerPaste(); | ||
} | ||
|
||
function registerCopy() { | ||
const copyShortcut: Blockly.ShortcutRegistry.KeyboardShortcut = { | ||
name: Blockly.ShortcutItems.names.COPY, | ||
preconditionFn(workspace) { | ||
return oldCopy.preconditionFn(workspace); | ||
}, | ||
callback(workspace, e, shortcut) { | ||
const handler = getCopyPasteHandlers()?.copy; | ||
|
||
if (handler) { | ||
return handler(workspace, e); | ||
} | ||
|
||
return oldCopy.callback(workspace, e, shortcut); | ||
}, | ||
// the registered shortcut from blockly isn't an array, it's some sort | ||
// of serialized object so we have to convert it back to an array | ||
keyCodes: [oldCopy.keyCodes[0], oldCopy.keyCodes[1], oldCopy.keyCodes[2]], | ||
}; | ||
Blockly.ShortcutRegistry.registry.register(copyShortcut); | ||
} | ||
|
||
function registerCut() { | ||
const cutShortcut: Blockly.ShortcutRegistry.KeyboardShortcut = { | ||
name: Blockly.ShortcutItems.names.CUT, | ||
preconditionFn(workspace) { | ||
return oldCut.preconditionFn(workspace); | ||
}, | ||
callback(workspace, e, shortcut) { | ||
const handler = getCopyPasteHandlers()?.cut; | ||
|
||
if (handler) { | ||
return handler(workspace, e); | ||
} | ||
|
||
return oldCut.callback(workspace, e, shortcut); | ||
}, | ||
keyCodes: [oldCut.keyCodes[0], oldCut.keyCodes[1], oldCut.keyCodes[2]], | ||
}; | ||
|
||
Blockly.ShortcutRegistry.registry.register(cutShortcut); | ||
} | ||
|
||
function registerPaste() { | ||
const pasteShortcut: Blockly.ShortcutRegistry.KeyboardShortcut = { | ||
name: Blockly.ShortcutItems.names.PASTE, | ||
preconditionFn(workspace) { | ||
return oldPaste.preconditionFn(workspace); | ||
}, | ||
callback(workspace, e, shortcut) { | ||
const handler = getCopyPasteHandlers()?.paste; | ||
|
||
if (handler) { | ||
return handler(workspace, e); | ||
} | ||
|
||
return oldPaste.callback(workspace, e, shortcut); | ||
}, | ||
keyCodes: [oldPaste.keyCodes[0], oldPaste.keyCodes[1], oldPaste.keyCodes[2]], | ||
}; | ||
|
||
Blockly.ShortcutRegistry.registry.register(pasteShortcut); | ||
} |
This file contains 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 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 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