Skip to content

Commit 8946e47

Browse files
authored
fix: Fix bug that made some keyboard shortcuts non-idempotent (#10286)
* fix: Fix bug that made some keyboard shortcuts non-idempotent * fix: Fix bug that could cause unexpected block movement when inserting from toolbox
1 parent 1477ab1 commit 8946e47

5 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎packages/blockly/core/dragging/block_drag_strategy.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ export class BlockDragStrategy implements IDragStrategy {
257257
recordUndo: true,
258258
},
259259
) as BlockSvg;
260+
newBlock.setDragging(true);
260261
eventUtils.setRecordUndo(false);
261262
newBlock.render();
262263
this.positionNewBlock(this.block, newBlock);

‎packages/blockly/core/dropdowndiv.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ export function createDom() {
147147
content,
148148
'keydown',
149149
null,
150-
common.globalShortcutHandler,
150+
(e: KeyboardEvent) => {
151+
common.globalShortcutHandler(e);
152+
e.stopPropagation();
153+
},
151154
);
152155

153156
arrow = document.createElement('div');

‎packages/blockly/core/widgetdiv.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ export function createDom() {
102102
containerDiv,
103103
'keydown',
104104
null,
105-
common.globalShortcutHandler,
105+
(e: KeyboardEvent) => {
106+
common.globalShortcutHandler(e);
107+
e.stopPropagation();
108+
},
106109
);
107110

108111
container.appendChild(containerDiv);

‎packages/blockly/tests/mocha/shortcut_items_test.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,22 @@ suite('Keyboard Shortcut Items', function () {
18851885
this.workspace.getInjectionDiv().dispatchEvent(event);
18861886
assert.equal(this.workspace.getTopComments().length, 1);
18871887
});
1888+
1889+
test('Is idempotent when a contextual menu is open', function () {
1890+
const comment = this.workspace.newComment();
1891+
comment.setText('Hello');
1892+
Blockly.getFocusManager().focusNode(comment);
1893+
comment.showContextMenu();
1894+
assert.equal(this.workspace.getTopComments().length, 1);
1895+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.D);
1896+
// The WidgetDiv (used by the dropdown menu) registers the global shortcut
1897+
// handler for its own div, since it may live outside of the injection
1898+
// div. Ensure that it also prevents event bubbling to the main shortcut
1899+
// handler, which could cause shortcuts like Duplicate to be invoked
1900+
// multiple times from one keypress. See #10250.
1901+
Blockly.WidgetDiv.getDiv().dispatchEvent(event);
1902+
assert.equal(this.workspace.getTopComments().length, 2);
1903+
});
18881904
});
18891905

18901906
suite('Clean up workspace (C)', function () {

‎packages/blockly/tests/mocha/test_helpers/user_input.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export function dispatchPointerEvent(target, type, properties) {
3939
export function createKeyDownEvent(keyCode, modifiers) {
4040
const event = {
4141
keyCode: keyCode,
42+
bubbles: true,
4243
};
4344
if (modifiers && modifiers.length > 0) {
4445
event.altKey = modifiers.includes(KeyCodes.ALT);

0 commit comments

Comments
 (0)