Skip to content

Commit 22284a1

Browse files
authored
Option to expand/contract expandables in context menu (#40)
1 parent 5a6f04d commit 22284a1

6 files changed

Lines changed: 59 additions & 3 deletions

File tree

blocks_vertical/control.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ Blockly.Blocks['control_expandableIf'] = {
251251
this.branches_ = 0;
252252
this.nextIsElse = true;
253253
this.endsInElse = false;
254+
this.expandable_ = true;
254255
},
255256

256257
fillInBlock: Blockly.scratchBlocksUtils.generateMutatorShadow,

blocks_vertical/live_tests.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ Blockly.Blocks['operator_expandableBool'] = {
2929
});
3030

3131
this.inputs_ = 0;
32+
this.expandable_ = true;
3233
},
3334

34-
// EDIT THIS IN https://github.com/PenguinMod/penguinmod.github.io/blob/develop/src/addons/addons/editor-tweaks/userscript.js#L105
3535
fillInBlock: Blockly.scratchBlocksUtils.generateMutatorShadow,
3636
menuGenerator: function () {
3737
const dropdown = new Blockly.FieldDropdown(function () {
@@ -158,6 +158,7 @@ Blockly.Blocks['operator_expandableCompare'] = {
158158
});
159159

160160
this.inputs_ = 0;
161+
this.expandable_ = true;
161162
},
162163

163164
fillInBlock: Blockly.scratchBlocksUtils.generateMutatorShadow,
@@ -225,7 +226,7 @@ Blockly.Blocks['operator_expandableCompare'] = {
225226
const menu = input.appendField(this.menuGenerator());
226227
menu.fieldRow[0].setValue(menuValues[i - 1] ? menuValues[i - 1] : "m", true);
227228
}
228-
// vm will automatically replace2 empty inputs with saved shadows
229+
// vm will automatically replace empty inputs with saved shadows
229230
}
230231
},
231232

blocks_vertical/operators.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ Blockly.Blocks['operator_expandableMath'] = {
146146
});
147147

148148
this.inputs_ = 0;
149+
this.expandable_ = true;
149150
},
150151

151152
fillInBlock: Blockly.scratchBlocksUtils.generateMutatorShadow,
@@ -663,6 +664,7 @@ Blockly.Blocks['operator_expandablejoininputs'] = {
663664

664665
this.messageList = ["apple", "banana", "pear", "orange", "mango", "strawberry", "pineapple", "grape", "kiwi"];
665666
this.inputs_ = 0;
667+
this.expandable_ = true;
666668
},
667669

668670
fillInBlock: Blockly.scratchBlocksUtils.generateMutatorShadow,

core/block.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ Blockly.Block = function(workspace, prototypeName, opt_id) {
115115
*/
116116
this.editable_ = true;
117117

118+
/**
119+
* @type {boolean}
120+
* @private
121+
*/
122+
this.expandable_ = false;
123+
118124
/**
119125
* @type {boolean}
120126
* @private
@@ -704,6 +710,14 @@ Blockly.Block.prototype.isEditable = function() {
704710
return this.editable_ && !(this.workspace && this.workspace.options.readOnly);
705711
};
706712

713+
/**
714+
* Get whether this block is expandable or not
715+
* @return {boolean} True if expandable.
716+
*/
717+
Blockly.Block.prototype.isExpandable = function() {
718+
return this.expandable_;
719+
};
720+
707721
/**
708722
* Set whether this block is editable or not.
709723
* @param {boolean} editable True if editable.

core/block_svg.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,13 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) {
731731
if (this.isEditable() && this.workspace.options.comments) {
732732
menuOptions.push(Blockly.ContextMenu.blockCommentOption(block));
733733
}
734-
//menuOptions.push(Blockly.ContextMenu.blockCollapseOption(block));
734+
// UNUSED menuOptions.push(Blockly.ContextMenu.blockCollapseOption(block));
735+
if (this.isExpandable()) {
736+
menuOptions.push(
737+
Blockly.ContextMenu.expandBlockOption(block),
738+
Blockly.ContextMenu.contractBlockOption(block),
739+
);
740+
}
735741
menuOptions.push(Blockly.ContextMenu.blockDeleteOption(block));
736742
} else if (this.parentBlock_ && this.isShadow_ && this.type !== 'polygon') {
737743
this.parentBlock_.showContextMenu_(e);

core/contextmenu.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,38 @@ Blockly.ContextMenu.blockCommentOption = function(block) {
339339
return commentOption;
340340
};
341341

342+
/**
343+
* Make a context menu option for expanding an expandable block.
344+
* @param {!Blockly.BlockSvg} block The block where the right-click originated.
345+
* @return {!Object} A menu option, containing text, enabled, and a callback.
346+
* @package
347+
*/
348+
Blockly.ContextMenu.expandBlockOption = function(block) {
349+
return {
350+
enabled: true,
351+
text: "Expand",
352+
callback: function() {
353+
block.onExpandableButtonClicked_(true);
354+
}
355+
};
356+
};
357+
358+
/**
359+
* Make a context menu option for contracting an expandable block.
360+
* @param {!Blockly.BlockSvg} block The block where the right-click originated.
361+
* @return {!Object} A menu option, containing text, enabled, and a callback.
362+
* @package
363+
*/
364+
Blockly.ContextMenu.contractBlockOption = function(block) {
365+
return {
366+
enabled: true,
367+
text: "Contract",
368+
callback: function() {
369+
block.onExpandableButtonClicked_(false);
370+
}
371+
};
372+
};
373+
342374
/**
343375
* Make a context menu option for undoing the most recent action on the
344376
* workspace.

0 commit comments

Comments
 (0)