From 2581446907168b3c3a127c5a43703ad451a80528 Mon Sep 17 00:00:00 2001 From: Andy O'Neill Date: Mon, 12 Dec 2022 16:54:32 -0500 Subject: [PATCH] feat: ability to clear flyout blocks when language changes --- src/engine/blocks.js | 8 ++++++++ src/virtual-machine.js | 7 +++++++ test/unit/virtual-machine.js | 13 +++++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/engine/blocks.js b/src/engine/blocks.js index aca5da91b..c276564ed 100644 --- a/src/engine/blocks.js +++ b/src/engine/blocks.js @@ -831,6 +831,14 @@ class Blocks { this.emitProjectChanged(); } + /** + * Delete all blocks and their associated scripts. + */ + deleteAllBlocks () { + const blockIds = Object.keys(this._blocks); + blockIds.forEach(blockId => this.deleteBlock(blockId)); + } + /** * Returns a map of all references to variables or lists from blocks * in this block container. diff --git a/src/virtual-machine.js b/src/virtual-machine.js index 85520c1ba..8094efbf8 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -1198,6 +1198,13 @@ class VirtualMachine extends EventEmitter { } } + /** + * Delete all of the flyout blocks. + */ + clearFlyoutBlocks () { + this.runtime.flyoutBlocks.deleteAllBlocks(); + } + /** * Set an editing target. An editor UI can use this function to switch * between editing different targets, sprites, etc. diff --git a/test/unit/virtual-machine.js b/test/unit/virtual-machine.js index 87b213ff0..cd8c032f5 100644 --- a/test/unit/virtual-machine.js +++ b/test/unit/virtual-machine.js @@ -1055,3 +1055,16 @@ test('toJSON encodes Infinity/NaN as 0, not null', t => { t.end(); }); + +test('clearFlyoutBlocks removes all of the flyout blocks', t => { + const vm = new VirtualMachine(); + const flyoutBlocks = vm.runtime.flyoutBlocks; + + flyoutBlocks.createBlock(adapter(events.mockVariableBlock)[0]); + t.equal(Object.keys(flyoutBlocks._blocks).length, 1); + + vm.clearFlyoutBlocks(); + t.equal(Object.keys(flyoutBlocks._blocks).length, 0); + + t.end(); +});