From 09e561deb7e77770e26a8871e24eebd86a5bb439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20M=C3=B6nig?= Date: Wed, 9 Feb 2022 16:55:31 +0100 Subject: [PATCH] preserve contents of variadic inputs when editing or translating a custom blocks --- HISTORY.md | 2 ++ snap.html | 4 ++-- src/blocks.js | 4 ++-- src/byob.js | 59 +++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 49 insertions(+), 20 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index fc2e1497..fb385c49 100755 --- a/HISTORY.md +++ b/HISTORY.md @@ -5,11 +5,13 @@ * **New Features:** * **Notable Changes:** * **Notable Fixes:** + * preserve contents of variadic inputs when editing or translating a custom blocks * **Documentation Updates:** * **Translation Updates:** ### 2022-02-09 * new dev version +* blocks, byob: preserve contents of variadic inputs when editing or translating a custom blocks ## 7.1.3: * **New Features:** diff --git a/snap.html b/snap.html index 599189dd..20a5ad1d 100755 --- a/snap.html +++ b/snap.html @@ -16,14 +16,14 @@ - + - + diff --git a/src/blocks.js b/src/blocks.js index 2efc64e1..8a3c1ac7 100644 --- a/src/blocks.js +++ b/src/blocks.js @@ -161,7 +161,7 @@ CostumeIconMorph, SoundIconMorph, SVG_Costume*/ // Global stuff //////////////////////////////////////////////////////// -modules.blocks = '2022-January-30'; +modules.blocks = '2022-February-09'; var SyntaxElementMorph; var BlockMorph; @@ -1250,7 +1250,7 @@ SyntaxElementMorph.prototype.replaceInput = function (oldArg, newArg) { oldArg.inputs().forEach(inp => // preserve nested reporters oldArg.replaceInput(inp, new InputSlotMorph()) ); - if (this.dynamicInputLabels) { + if (this.dynamicInputLabels && newArg instanceof ReporterBlockMorph) { replacement = new ArgLabelMorph(newArg); } } diff --git a/src/byob.js b/src/byob.js index 286dfff1..8db1ed26 100644 --- a/src/byob.js +++ b/src/byob.js @@ -104,13 +104,14 @@ nop, radians, BoxMorph, ArrowMorph, PushButtonMorph, contains, InputSlotMorph, ToggleButtonMorph, IDE_Morph, MenuMorph, ToggleElementMorph, fontHeight, isNil, StageMorph, SyntaxElementMorph, CommentMorph, localize, CSlotMorph, Variable, MorphicPreferences, SymbolMorph, CursorMorph, VariableFrame, BooleanSlotMorph, -WatcherMorph, XML_Serializer, SnapTranslator, SnapExtensions*/ +WatcherMorph, XML_Serializer, SnapTranslator, SnapExtensions, MultiArgMorph, +ArgLabelMorph*/ /*jshint esversion: 6*/ // Global stuff //////////////////////////////////////////////////////// -modules.byob = '2022-January-07'; +modules.byob = '2022-February-09'; // Declarations @@ -747,34 +748,60 @@ CustomCommandBlockMorph.prototype.refresh = function (aDefinition) { CustomCommandBlockMorph.prototype.restoreInputs = function (oldInputs) { // try to restore my previous inputs when my spec has been changed - var i = 0, - old; + var newInputs = this.inputs(), + len = Math.max(oldInputs.length, newInputs.length), + scripts = this.parentThatIsA(ScriptsMorph), + old, + inp, + i; + + function preserve(item) { + // keep unused blocks around in the scripting area + if (item instanceof MultiArgMorph) { + return item.inputs().forEach(slot => preserve(slot)); + } + if (item instanceof BlockMorph && scripts) { + scripts.add(item); + item.moveBy(new Point(20, 20)); + item.fixBlockColor(); + } + } + if (this.isPrototype) {return; } this.cachedInputs = null; - this.inputs().forEach(inp => { + for (i = 0; i < len; i += 1) { + inp = newInputs[i]; old = oldInputs[i]; - if (old instanceof ReporterBlockMorph && + if (old instanceof ArgLabelMorph) { + old = old.argMorph(); + } + if (old instanceof ReporterBlockMorph && inp && (!(inp instanceof TemplateSlotMorph))) { this.replaceInput(inp, old.fullCopy()); - } else if (old instanceof InputSlotMorph - && inp instanceof InputSlotMorph) { + } else if (old instanceof InputSlotMorph && + inp instanceof InputSlotMorph) { if (old.isEmptySlot()) { inp.setContents(''); } else { inp.setContents(old.evaluate()); } - } else if (old instanceof BooleanSlotMorph - && inp instanceof BooleanSlotMorph) { + } else if (old instanceof BooleanSlotMorph && + inp instanceof BooleanSlotMorph) { inp.setContents(old.evaluate()); - } else if (old instanceof TemplateSlotMorph - && inp instanceof TemplateSlotMorph) { + } else if (old instanceof TemplateSlotMorph && + inp instanceof TemplateSlotMorph) { inp.setContents(old.evaluate()); - } else if (old instanceof CSlotMorph - && inp instanceof CSlotMorph) { + } else if (old instanceof CSlotMorph && + inp instanceof CSlotMorph) { inp.nestedBlock(old.evaluate()); + } else if (old instanceof MultiArgMorph && + inp instanceof MultiArgMorph && + (old.slotSpec === inp.slotSpec)) { + this.replaceInput(inp, old.fullCopy()); + } else { + preserve(old); } - i += 1; - }); + } this.cachedInputs = null; };