From a36327842f7dbb6538ecf7f537da8ca3edc63019 Mon Sep 17 00:00:00 2001 From: jmoenig Date: Sun, 17 May 2020 14:13:10 +0200 Subject: [PATCH] experimental "blockify list" feature --- src/blocks.js | 3 ++- src/lists.js | 37 +++++++++++++++++++++++++++++++++++++ src/objects.js | 6 ++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/blocks.js b/src/blocks.js index d1e0bf8a..15a11775 100644 --- a/src/blocks.js +++ b/src/blocks.js @@ -148,7 +148,7 @@ CustomCommandBlockMorph, SymbolMorph, ToggleButtonMorph, DialMorph*/ // Global stuff //////////////////////////////////////////////////////// -modules.blocks = '2020-May-16'; +modules.blocks = '2020-May-17'; var SyntaxElementMorph; var BlockMorph; @@ -11060,6 +11060,7 @@ MultiArgMorph.prototype.addInput = function (contents) { this.children.splice(idx, 0, newPart); newPart.fixLayout(); this.fixLayout(); + return newPart; }; MultiArgMorph.prototype.removeInput = function () { diff --git a/src/lists.js b/src/lists.js index 700ce0ab..3990c0a7 100644 --- a/src/lists.js +++ b/src/lists.js @@ -612,6 +612,43 @@ List.prototype.hasOnlyAtomicData = function () { }); }; +// List-to-block (experimental) + +List.prototype.blockify = function (limit = 100) { + var block = SpriteMorph.prototype.blockForSelector('reportNewList'), + slots = block.inputs()[0], + bool, + i, len, value; + + block.isDraggable = true; + slots.removeInput(); + + // fill the slots with the data + len = Math.min(this.length(), limit); + for (i = 0; i < len; i += 1) { + value = this.at(i + 1); + if (value instanceof List) { + slots.replaceInput( + slots.addInput(), + value.blockify(limit - i) + ); + } else if (typeof value === 'boolean') { + bool = SpriteMorph.prototype.blockForSelector('reportBoolean'); + bool.inputs()[0].setContents(value); + bool.isDraggable = true; + slots.replaceInput( + slots.addInput(), + bool + ); + } else { + slots.addInput(value); + } + } + + slots.fixBlockColor(null, true); + return block; +}; + // ListWatcherMorph //////////////////////////////////////////////////// /* diff --git a/src/objects.js b/src/objects.js index 81bfb6ae..c013f760 100644 --- a/src/objects.js +++ b/src/objects.js @@ -11435,6 +11435,12 @@ WatcherMorph.prototype.userMenu = function () { null, new Color(100, 0, 0) ); + menu.addItem( // experimental + 'blockify...', + () => this.currentValue.blockify().pickUp(this.world()), + null, + new Color(100, 0, 0) + ); } } if (isString(this.currentValue) || !isNaN(+this.currentValue)) {