From bc01598ef892b4964ba28ea66e314f39f7f50892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20M=C3=B6nig?= Date: Tue, 25 Jan 2022 19:00:17 +0100 Subject: [PATCH] support deleting and inserting individual variadic slots under construction --- HISTORY.md | 4 +++ snap.html | 2 +- src/blocks.js | 71 ++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 64 insertions(+), 13 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index df21ce75..65b04522 100755 --- a/HISTORY.md +++ b/HISTORY.md @@ -4,12 +4,16 @@ * **New Features:** * support dragging blocks out from result bubbles, and from speech balloons and variable watchers when in edit mode + * support deleting and inserting individual variadic slots * **Notable Changes:** * **Notable Fixes:** * fixed layout for scrolling custom categories, thanks, Eckart, for the bug report! * **Documentation Updates:** * **Translation Updates:** +### 2022-01-25 +* blocks: support deleting and inserting individual variadic slots + ### 2022-01-23 * morphic: added Node >> childThatIsA * tables: support dragging blocks out from table views diff --git a/snap.html b/snap.html index d5350b2b..776c7f23 100755 --- a/snap.html +++ b/snap.html @@ -16,7 +16,7 @@ - + diff --git a/src/blocks.js b/src/blocks.js index e33b3d49..c28c2822 100644 --- a/src/blocks.js +++ b/src/blocks.js @@ -160,7 +160,7 @@ CustomCommandBlockMorph, ToggleButtonMorph, DialMorph, SnapExtensions*/ // Global stuff //////////////////////////////////////////////////////// -modules.blocks = '2022-January-22'; +modules.blocks = '2022-January-25'; var SyntaxElementMorph; var BlockMorph; @@ -10419,23 +10419,43 @@ InputSlotMorph.prototype.freshTextEdit = function (aStringOrTextMorph) { InputSlotMorph.prototype.userMenu = function () { var menu = new MenuMorph(this); - if (!StageMorph.prototype.enableCodeMapping) { + if (!StageMorph.prototype.enableCodeMapping && + !(this.parent instanceof MultiArgMorph)) { return this.parent.userMenu(); } - if (this.isNumeric) { - menu.addItem( - 'code number mapping...', - 'mapNumberToCode' - ); - } else { - menu.addItem( - 'code string mapping...', - 'mapStringToCode' - ); + if (this.parent instanceof MultiArgMorph && + this.parentThatIsA(ScriptsMorph)) { + if (!this.parent.maxInputs || + (this.parent.children.length <= this.parent.maxInputs)) { + menu.addItem( + 'insert', + () => this.parent.insertNewInputBefore(this) + ); + } + if (this.parent.children.length >= this.parent.minInputs) { + menu.addItem( + 'delete', + () => this.parent.deleteSlot(this) + ); + } + } + if (StageMorph.prototype.enableCodeMapping) { + if (this.isNumeric) { + menu.addItem( + 'code number mapping...', + 'mapNumberToCode' + ); + } else { + menu.addItem( + 'code string mapping...', + 'mapStringToCode' + ); + } } return menu; }; + // InputSlotMorph reacting to user choices /* @@ -12313,6 +12333,33 @@ MultiArgMorph.prototype.refresh = function () { }); }; +// MultiArgMorph deleting & inserting slots: + + // caution, only call these methods with "primitive" inputs, + // since they don't preserve embedded blocks (yes, on purpose) + +MultiArgMorph.prototype.deleteSlot = function (anInput) { + if (this.children.length <= this.minInputs + 1) { + return; + } + this.removeChild(anInput); + this.fixLayout(); +}; + +MultiArgMorph.prototype.insertNewInputBefore = function (anInput) { // +++ + var idx = this.children.indexOf(anInput), + newPart = this.labelPart(this.slotSpec); + + if (this.maxInputs && (this.children.length > this.maxInputs)) { + return; + } + newPart.parent = this; + this.children.splice(idx, 0, newPart); + newPart.fixLayout(); + this.fixLayout(); + return newPart; +}; + // MultiArgMorph arity control: MultiArgMorph.prototype.addInput = function (contents) {