Skip to content

Commit b3bce67

Browse files
authored
Fix: Remove the collapsed block warning when expanding a block (#8854)
* Fix: Remove the collapsed block warning when expanding a block * Add tests and make warnings appear as soon as a block collapses * Combine if checks to simplify code
1 parent bb8df93 commit b3bce67

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

core/block_svg.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,21 @@ export class BlockSvg
507507
this.updateCollapsed();
508508
}
509509

510+
/**
511+
* Traverses child blocks to see if any of them have a warning.
512+
*
513+
* @returns true if any child has a warning, false otherwise.
514+
*/
515+
private childHasWarning(): boolean {
516+
const children = this.getChildren(false);
517+
for (const child of children) {
518+
if (child.getIcon(WarningIcon.TYPE) || child.childHasWarning()) {
519+
return true;
520+
}
521+
}
522+
return false;
523+
}
524+
510525
/**
511526
* Makes sure that when the block is collapsed, it is rendered correctly
512527
* for that state.
@@ -529,9 +544,17 @@ export class BlockSvg
529544
if (!collapsed) {
530545
this.updateDisabled();
531546
this.removeInput(collapsedInputName);
547+
this.setWarningText(null, BlockSvg.COLLAPSED_WARNING_ID);
532548
return;
533549
}
534550

551+
if (this.childHasWarning()) {
552+
this.setWarningText(
553+
Msg['COLLAPSED_WARNINGS_WARNING'],
554+
BlockSvg.COLLAPSED_WARNING_ID,
555+
);
556+
}
557+
535558
const text = this.toString(internalConstants.COLLAPSE_CHARS);
536559
const field = this.getField(collapsedFieldName);
537560
if (field) {

tests/mocha/block_test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,62 @@ suite('Blocks', function () {
18401840
});
18411841
});
18421842

1843+
suite('Warning icons and collapsing', function () {
1844+
setup(function () {
1845+
this.workspace = Blockly.inject('blocklyDiv');
1846+
this.parentBlock = Blockly.serialization.blocks.append(
1847+
{
1848+
'type': 'statement_block',
1849+
'inputs': {
1850+
'STATEMENT': {
1851+
'block': {
1852+
'type': 'statement_block',
1853+
},
1854+
},
1855+
},
1856+
},
1857+
this.workspace,
1858+
);
1859+
this.parentBlock.initSvg();
1860+
this.parentBlock.render();
1861+
1862+
this.childBlock = this.parentBlock.getInputTargetBlock('STATEMENT');
1863+
this.childBlock.initSvg();
1864+
this.childBlock.render();
1865+
});
1866+
1867+
teardown(function () {
1868+
workspaceTeardown.call(this, this.workspace);
1869+
});
1870+
1871+
test('Adding a warning to a child block does not affect the parent', function () {
1872+
const text = 'Warning Text';
1873+
this.childBlock.setWarningText(text);
1874+
const icon = this.parentBlock.getIcon(Blockly.icons.WarningIcon.TYPE);
1875+
assert.isUndefined(
1876+
icon,
1877+
"Setting a child block's warning should not add a warning to the parent",
1878+
);
1879+
});
1880+
1881+
test('Warnings are added and removed when collapsing a stack with warnings', function () {
1882+
const text = 'Warning Text';
1883+
1884+
this.childBlock.setWarningText(text);
1885+
1886+
this.parentBlock.setCollapsed(true);
1887+
let icon = this.parentBlock.getIcon(Blockly.icons.WarningIcon.TYPE);
1888+
assert.exists(icon?.getText(), 'Expected warning icon text to be set');
1889+
1890+
this.parentBlock.setCollapsed(false);
1891+
icon = this.parentBlock.getIcon(Blockly.icons.WarningIcon.TYPE);
1892+
assert.isUndefined(
1893+
icon,
1894+
'Warning should be removed from parent after expanding',
1895+
);
1896+
});
1897+
});
1898+
18431899
suite('Bubbles and collapsing', function () {
18441900
setup(function () {
18451901
this.workspace = Blockly.inject('blocklyDiv');

0 commit comments

Comments
 (0)