Skip to content

Commit 5d84f65

Browse files
authored
fix: Remove unsafe cast in ContinuousFlyout (#2671)
* fix: Remove unsafe cast in `ContinuousFlyout` * fix: Upgrade console output to a warning
1 parent ecaf83f commit 5d84f65

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

plugins/continuous-toolbox/src/ContinuousFlyout.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,17 @@ export class ContinuousFlyout extends Blockly.VerticalFlyout {
7272
*
7373
* @returns Toolbox that owns this flyout.
7474
*/
75-
private getParentToolbox(): ContinuousToolbox {
76-
return this.targetWorkspace.getToolbox() as ContinuousToolbox;
75+
private getParentToolbox(): ContinuousToolbox | null {
76+
const toolbox = this.targetWorkspace.getToolbox();
77+
if (!toolbox || toolbox instanceof ContinuousToolbox) return toolbox;
78+
79+
console.warn(
80+
'Expected a `ContinuousToolbox` instance but did not find one. ' +
81+
'Make sure `registerContinuousToolbox()` has been called and the ' +
82+
'continuous toolbox has been injected.',
83+
);
84+
85+
return null;
7786
}
7887

7988
/**
@@ -110,7 +119,7 @@ export class ContinuousFlyout extends Blockly.VerticalFlyout {
110119
// Note that `FlyoutButton` represents both buttons and labels.
111120
element instanceof Blockly.FlyoutButton &&
112121
element.isLabel() &&
113-
this.getParentToolbox().getCategoryByName(element.getButtonText())
122+
this.getParentToolbox()?.getCategoryByName(element.getButtonText())
114123
);
115124
}
116125

@@ -146,7 +155,7 @@ export class ContinuousFlyout extends Blockly.VerticalFlyout {
146155
...this.scrollPositions.entries(),
147156
].reverse()) {
148157
if (scaledPosition >= position) {
149-
this.getParentToolbox().selectCategoryByName(name);
158+
this.getParentToolbox()?.selectCategoryByName(name);
150159
return;
151160
}
152161
}
@@ -271,7 +280,7 @@ export class ContinuousFlyout extends Blockly.VerticalFlyout {
271280
super.show(flyoutDef);
272281
this.recordScrollPositions();
273282
this.getWorkspace().resizeContents();
274-
if (!this.getParentToolbox().getSelectedItem()) {
283+
if (!this.getParentToolbox()?.getSelectedItem()) {
275284
this.selectCategoryByScrollPosition(0);
276285
}
277286
this.getRecyclableInflater().emptyRecycledBlocks();

0 commit comments

Comments
 (0)