fix: Fix bug that prevented using keyboard shortcuts when the DropDownDiv is open. (#9085)

* fix: Fix bug that prevented using keyboard shortcuts when the DropDownDiv is open.

* chore: Remove obsolete comment.

* Refactor: Remove unreachable null check.

* chore: Add tests for handling Escape to dismiss the Widget/DropDownDivs.

* chore: Satisfy the linter.

* fix: Fix post-merge test failure.
This commit is contained in:
Aaron Dodson
2025-05-27 11:57:58 -07:00
committed by GitHub
parent ab15372683
commit d2c4016fcc
6 changed files with 113 additions and 38 deletions

View File

@@ -136,6 +136,39 @@ suite('DropDownDiv', function () {
});
});
suite('Keyboard Shortcuts', function () {
setup(function () {
this.boundsStub = sinon
.stub(Blockly.DropDownDiv.TEST_ONLY, 'getBoundsInfo')
.returns({
left: 0,
right: 100,
top: 0,
bottom: 100,
width: 100,
height: 100,
});
this.workspace = Blockly.inject('blocklyDiv', {});
});
teardown(function () {
this.boundsStub.restore();
});
test('Escape dismisses DropDownDiv', function () {
let hidden = false;
Blockly.DropDownDiv.show(this, false, 0, 0, 0, 0, false, () => {
hidden = true;
});
assert.isFalse(hidden);
Blockly.DropDownDiv.getContentDiv().dispatchEvent(
new KeyboardEvent('keydown', {
key: 'Escape',
keyCode: 27, // example values.
}),
);
assert.isTrue(hidden);
});
});
suite('show()', function () {
test('without bounds set throws error', function () {
const block = this.setUpBlockWithField();

View File

@@ -287,6 +287,29 @@ suite('WidgetDiv', function () {
});
});
suite('Keyboard Shortcuts', function () {
test('Escape dismisses WidgetDiv', function () {
let hidden = false;
Blockly.WidgetDiv.show(
this,
false,
() => {
hidden = true;
},
this.workspace,
false,
);
assert.isFalse(hidden);
Blockly.WidgetDiv.getDiv().dispatchEvent(
new KeyboardEvent('keydown', {
key: 'Escape',
keyCode: 27, // example values.
}),
);
assert.isTrue(hidden);
});
});
suite('show()', function () {
test('shows nowhere', function () {
const block = this.setUpBlockWithField();