From 33e1d1e224cc4db80f2186cdc09be66c127a044c Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Wed, 13 Dec 2023 00:48:35 +0000 Subject: [PATCH] feat: add fancy collapsing stacks (#2109) --- examples/sample-p5-app/src/blocks/p5.js | 18 +++++++++++++- .../src/generators/javascript.js | 4 ++++ examples/sample-p5-app/src/index.js | 24 +++++++++++++++---- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/examples/sample-p5-app/src/blocks/p5.js b/examples/sample-p5-app/src/blocks/p5.js index 1ea75a2833..a965fef199 100644 --- a/examples/sample-p5-app/src/blocks/p5.js +++ b/examples/sample-p5-app/src/blocks/p5.js @@ -1,5 +1,21 @@ import * as Blockly from 'blockly/core'; +// Dummy collapse block + +const collapse = { + 'type': 'collapse', + 'message0': '%1', + 'args0': [ + { + 'type': 'input_statement', + 'name': 'COLLAPSE', + }, + ], + 'nextStatement': null, + 'previousStatement': null, + 'colour': '#ccc', +} + // p5 Basic Setup Blocks const p5SetupJson = { @@ -387,7 +403,7 @@ const print = { // Create the block definitions for all the JSON-only blocks. // This does not register their definitions with Blockly. const jsonBlocks = Blockly.common.createBlockDefinitionsFromJsonArray( - [background, stroke, fill, point, line, triangle, rect, ellipse, arc, print]); + [collapse, background, stroke, fill, point, line, triangle, rect, ellipse, arc, print]); export const blocks = { 'p5_setup': p5Setup, 'p5_draw': p5Draw, 'p5_canvas': p5Canvas, ...jsonBlocks, diff --git a/examples/sample-p5-app/src/generators/javascript.js b/examples/sample-p5-app/src/generators/javascript.js index 195acb161a..80051ae02e 100644 --- a/examples/sample-p5-app/src/generators/javascript.js +++ b/examples/sample-p5-app/src/generators/javascript.js @@ -11,6 +11,10 @@ import {Order} from 'blockly/javascript'; // This file has no side effects! export const forBlock = Object.create(null); +forBlock['collapse'] = function(block, generator) { + return generator.statementToCode(block, 'COLLAPSE'); +} + forBlock['p5_print'] = function(block, generator) { const msg = generator.valueToCode(block, 'TEXT', Order.NONE) || '\'\''; diff --git a/examples/sample-p5-app/src/index.js b/examples/sample-p5-app/src/index.js index 1f656c59d5..637e9f260e 100644 --- a/examples/sample-p5-app/src/index.js +++ b/examples/sample-p5-app/src/index.js @@ -143,17 +143,31 @@ const getStringification = { } Blockly.ContextMenuRegistry.registry.register(getStringification); -/* const fancyCollapse = { +const fancyCollapse = { callback: function(scope) { const ws = scope.block.workspace; - combineBlocks(ws, block.blockSelectionWeakMap.get(ws)); + const stacks = combineBlocks(ws, blockSelectionWeakMap.get(ws)); + for (const stack of stacks) { + const prevParentConn = stack.first.previousConnection?.targetConnection; + const prevDanglerConn = stack.last.nextConnection?.targetConnection; + stack.first.previousConnection?.disconnect(); + stack.last.nextConnection?.disconnect(); + const newBlock = + Blockly.serialization.blocks.append({'type': 'collapse'}, ws); + newBlock.getInput('COLLAPSE').connection.connect( + stack.first.previousConnection); + prevParentConn?.connect(newBlock.previousConnection); + prevDanglerConn?.connect(newBlock.nextConnection); + newBlock.setCollapsed(true); + } }, scopeType: Blockly.ContextMenuRegistry.ScopeType.BLOCK, - displayText: "Get lists of contiguous blocks", + displayText: "Summarize ✨Fancily✨", preconditionFn: () => {return 'enabled'}, weight: 100, - id: 'getContiguous' -} */ + id: 'FancyCollapse' +} +Blockly.ContextMenuRegistry.registry.register(fancyCollapse); // Load the initial state from storage and run the code. load(ws);