From 70c8cfd73ee1081eafcb2773fe56a4e078cb2bea Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Fri, 3 May 2024 19:44:43 -0700 Subject: [PATCH] fix: reenable reporting block values (#55) * fix: reenable reporting block values * fix: make reporter bubbles appear on the correct block * fix: avoid error when attempting to report values of nonexistent blocks --- src/block_reporting.js | 21 +++++++++++++++++++++ src/checkable_continuous_flyout.js | 10 ++++++++++ src/index.js | 1 + 3 files changed, 32 insertions(+) create mode 100644 src/block_reporting.js diff --git a/src/block_reporting.js b/src/block_reporting.js new file mode 100644 index 0000000000..6950d57175 --- /dev/null +++ b/src/block_reporting.js @@ -0,0 +1,21 @@ +import * as Blockly from 'blockly/core'; +import {Colours} from '../core/colours.js'; + +export function reportValue(id, value) { + const block = Blockly.getMainWorkspace().getBlockById(id) || + Blockly.getMainWorkspace().getFlyout().getWorkspace().getBlockById(id); + if (!block) { + throw 'Tried to report value on block that does not exist.'; + } + const field = block.inputList[0].fieldRow[0]; + const contentDiv = Blockly.DropDownDiv.getContentDiv(); + const valueReportBox = document.createElement('div'); + valueReportBox.setAttribute('class', 'valueReportBox'); + valueReportBox.innerText = value; + contentDiv.appendChild(valueReportBox); + Blockly.DropDownDiv.setColour( + Colours.valueReportBackground, + Colours.valueReportBorder + ); + Blockly.DropDownDiv.showPositionedByBlock(field, block); +} diff --git a/src/checkable_continuous_flyout.js b/src/checkable_continuous_flyout.js index fc2db632b1..f67833ab0d 100644 --- a/src/checkable_continuous_flyout.js +++ b/src/checkable_continuous_flyout.js @@ -74,6 +74,16 @@ export class CheckableContinuousFlyout extends ContinuousFlyout { super.show(flyoutDef); } + serializeBlock(block) { + const json = super.serializeBlock(block); + // Delete the serialized block's ID so that a new one is generated when it is + // placed on the workspace. Otherwise, the block on the workspace may be + // indistinguishable from the one in the flyout, which can cause reporter blocks + // to have their value dropdown shown in the wrong place. + delete json.id; + return json; + } + clearOldCheckboxes() { for (const checkbox of this.checkboxes_.values()) { checkbox.svgRoot.remove(); diff --git a/src/index.js b/src/index.js index d4a89f660d..2c58a3aed1 100644 --- a/src/index.js +++ b/src/index.js @@ -32,6 +32,7 @@ import {CheckableContinuousFlyout} from './checkable_continuous_flyout.js'; import './scratch_continuous_category.js'; export * from 'blockly'; +export * from './block_reporting.js'; export * from './categories.js'; export * from './procedures.js'; export * from '../core/colours.js';