From 2f103195e6e817163ea1633cf2a0108094cf6210 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Mon, 5 Feb 2024 22:58:02 +0000 Subject: [PATCH] feat: add chameleon test block --- plugins/block-test/src/chameleon.js | 61 +++++++++++++++++++++++++++++ plugins/block-test/src/index.js | 6 +++ 2 files changed, 67 insertions(+) create mode 100644 plugins/block-test/src/chameleon.js diff --git a/plugins/block-test/src/chameleon.js b/plugins/block-test/src/chameleon.js new file mode 100644 index 0000000000..00754aab84 --- /dev/null +++ b/plugins/block-test/src/chameleon.js @@ -0,0 +1,61 @@ +/** + * @license + * Copyright 2024 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ +'use strict'; + +/** + * @fileoverview Blocks that have both a previous connection and an output + * connection while they are being dragged. + */ + +import * as Blockly from 'blockly/core'; + +Blockly.Blocks['test_chameleon'] = { + hasOutput: true, + hasPrevious: true, + + init: function () { + this.appendDummyInput().appendField('chameleon'); + this.setColour(120); + this.updateShape(); + }, + + onchange: function (e) { + if (e.type === Blockly.Events.BLOCK_DRAG) { + console.log('got drag event'); + this.hasPrevious = + !this.outputConnection || !this.outputConnection.targetBlock(); + this.hasOutput = !this.getPreviousBlock(); + this.updateShape(); + } + }, + + updateShape: function () { + this.setPreviousStatement(this.hasPrevious); + this.setOutput(this.hasOutput); + }, +}; + +/** + * The Chameleon Category. + */ +export const category = { + kind: 'CATEGORY', + name: 'Chameleon', + contents: [ + { + kind: 'BLOCK', + type: 'test_chameleon', + }, + ], +}; + +/** + * Initialize this toolbox category. + * @param {!Blockly.WorkspaceSvg} workspace The Blockly workspace. + */ +export function onInit(workspace) { + // NOP +} diff --git a/plugins/block-test/src/index.js b/plugins/block-test/src/index.js index 68b33c67b6..4b07718345 100644 --- a/plugins/block-test/src/index.js +++ b/plugins/block-test/src/index.js @@ -14,6 +14,10 @@ import * as Blockly from 'blockly/core'; import {category as alignCategory, onInit as initAlign} from './align'; import {category as basicCategory, onInit as initBasic} from './basic'; +import { + category as chameleonCategory, + onInit as initChameleon, +} from './chameleon'; import { category as connectionsCategory, onInit as initConnections, @@ -34,6 +38,7 @@ export const toolboxTestBlocks = { contents: [ alignCategory, basicCategory, + chameleonCategory, connectionsCategory, dragCategory, fieldsCategory, @@ -50,6 +55,7 @@ export const toolboxTestBlocks = { export function toolboxTestBlocksInit(workspace) { initAlign(workspace); initBasic(workspace); + initChameleon(workspace); initConnections(workspace); initDrag(workspace); initFields(workspace);