Skip to content

Commit

Permalink
fix: improve reliability of block value reporting (#77)
Browse files Browse the repository at this point in the history
* fix: improve reliability of block value reporting

* chore: add comment clarifying recycling behavior
  • Loading branch information
gonfunko authored May 16, 2024
1 parent aeec5ec commit cb5b068
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/block_reporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ export function reportValue(id, value) {
if (!block) {
throw 'Tried to report value on block that does not exist.';
}
const field = block.inputList[0].fieldRow[0];

let field;
for (const input of block.inputList) {
for (const f of input.fieldRow) {
field = f;
break;
}
}
if (!field) return;

const contentDiv = Blockly.DropDownDiv.getContentDiv();
const valueReportBox = document.createElement('div');
valueReportBox.setAttribute('class', 'valueReportBox');
Expand Down
8 changes: 8 additions & 0 deletions src/checkable_continuous_flyout.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,12 @@ export class CheckableContinuousFlyout extends ContinuousFlyout {
getFlyoutScale() {
return 0.675;
}

blockIsRecyclable_(block) {
const recyclable = super.blockIsRecyclable_(block);
// Exclude blocks with output connections, because they are able to report their current
// value in a popover and recycling them interacts poorly with the VM's maintenance of its
// state.
return recyclable && !block.outputConnection;
}
}
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
ContinuousMetrics,
} from '@blockly/continuous-toolbox';
import {CheckableContinuousFlyout} from './checkable_continuous_flyout.js';
import {ScratchContinuousToolbox} from './scratch_continuous_toolbox.js';
import {buildGlowFilter, glowStack} from './glows.js';
import './scratch_continuous_category.js';

Expand All @@ -51,7 +52,7 @@ export {CheckableContinuousFlyout};
export function inject(container, options) {
Object.assign(options, {
plugins: {
toolbox: ContinuousToolbox,
toolbox: ScratchContinuousToolbox,
flyoutsVerticalToolbox: CheckableContinuousFlyout,
metricsManager: ContinuousMetrics,
},
Expand Down
12 changes: 12 additions & 0 deletions src/scratch_continuous_toolbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as Blockly from 'blockly/core';
import {ContinuousToolbox} from '@blockly/continuous-toolbox';

export class ScratchContinuousToolbox extends ContinuousToolbox {
refreshSelection() {
// Intentionally a no-op, Scratch manually manages refreshing the toolbox via forceRerender().
}

forceRerender() {
super.refreshSelection();
}
}

0 comments on commit cb5b068

Please # to comment.