Skip to content

Commit

Permalink
fix: reenable reporting block values (#55)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gonfunko authored May 4, 2024
1 parent 003afd0 commit 70c8cfd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/block_reporting.js
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 10 additions & 0 deletions src/checkable_continuous_flyout.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 70c8cfd

Please sign in to comment.