Skip to content

Commit e26bdd4

Browse files
DNin01Samq64mxmou
authored
"Go to definition" in context menu (ScratchAddons#8064)
* "Go to definition" in context menu * Apply mxmou's suggestions Co-authored-by: Maximouse <brestl1c@outlook.com> * Consistent ordering * Bump latestUpdate version --------- Co-authored-by: Samq64 <81489795+Samq64@users.noreply.github.com> Co-authored-by: Maximouse <brestl1c@outlook.com>
1 parent ce5cfc2 commit e26bdd4

4 files changed

Lines changed: 41 additions & 16 deletions

File tree

addon-api/content-script/Tab.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const DATA_PNG = "data:image/png;base64,";
1111
const isScratchGui = location.origin === "https://scratchfoundation.github.io" || location.port === "8601";
1212

1313
const contextMenuCallbacks = [];
14-
const CONTEXT_MENU_ORDER = ["editor-devtools", "block-switching", "blocks2image", "swap-local-global"];
14+
const CONTEXT_MENU_ORDER = ["jump-to-def", "editor-devtools", "block-switching", "blocks2image", "swap-local-global"];
1515
let createdAnyBlockContextMenus = false;
1616

1717
const PROJECT_ACTIONS_CSS = `

addons-l10n/en/jump-to-def.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"jump-to-def/to-def": "Go to Definition"
3+
}

addons/jump-to-def/addon.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Jump to custom block definition",
3-
"description": "Allows you to jump to a custom block's definition using the middle mouse button or Shift+Click on the block.",
3+
"description": "Jump to a custom block's definition by middle-clicking or Shift+clicking on the block or selecting \"Go to Definition\" in the right click context menu.",
44
"credits": [
55
{
66
"name": "griffpatch"
@@ -13,6 +13,10 @@
1313
}
1414
],
1515
"versionAdded": "1.28.0",
16+
"latestUpdate": {
17+
"version": "1.43.0",
18+
"temporaryNotice": "You can now use the context menu (open by right clicking or touching and holding) to jump to custom block definitions."
19+
},
1620
"tags": ["editor", "codeEditor", "recommended"],
1721
"enabledByDefault": true,
1822
"dynamicEnable": true,

addons/jump-to-def/userscript.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ export default async function ({ addon, msg, console }) {
44

55
const Blockly = await addon.tab.traps.getBlockly();
66

7+
function jumpToBlockDefinition(block) {
8+
let findProcCode = block.getProcCode();
9+
10+
let topBlocks = addon.tab.traps.getWorkspace().getTopBlocks();
11+
for (const root of topBlocks) {
12+
if (root.type === "procedures_definition") {
13+
let label = root.getChildren()[0];
14+
let procCode = label.getProcCode();
15+
if (procCode && procCode === findProcCode) {
16+
// Found... navigate to it!
17+
utils.scrollBlockIntoView(root);
18+
}
19+
}
20+
}
21+
}
22+
723
Object.defineProperty(Blockly.Gesture.prototype, "jumpToDef", {
824
get() {
925
return !addon.self.disabled;
@@ -20,24 +36,26 @@ export default async function ({ addon, msg, console }) {
2036
let block = Blockly.registry ? this.startBlock : this.startBlock_;
2137
for (; block; block = block.getSurroundParent()) {
2238
if (block.type === "procedures_call") {
23-
let findProcCode = block.getProcCode();
24-
25-
let topBlocks = addon.tab.traps.getWorkspace().getTopBlocks();
26-
for (const root of topBlocks) {
27-
if (root.type === "procedures_definition") {
28-
let label = root.getChildren()[0];
29-
let procCode = label.getProcCode();
30-
if (procCode && procCode === findProcCode) {
31-
// Found... navigate to it!
32-
utils.scrollBlockIntoView(root);
33-
return;
34-
}
35-
}
36-
}
39+
jumpToBlockDefinition(block);
40+
return;
3741
}
3842
}
3943
}
4044

4145
_doBlockClick_.call(this);
4246
};
47+
48+
addon.tab.createBlockContextMenu(
49+
(items, block) => {
50+
if (!addon.self.disabled && block.type === "procedures_call") {
51+
items.push({
52+
enabled: true,
53+
text: msg("to-def"),
54+
callback: () => jumpToBlockDefinition(block),
55+
});
56+
}
57+
return items;
58+
},
59+
{ blocks: true, flyout: true }
60+
);
4361
}

0 commit comments

Comments
 (0)