Skip to content

Commit 82f5998

Browse files
fix: account for ephemeral focus in enter precondition (#619)
* chore: upgrade field colour to buggy version This illustrates that the latest version, based on field-grid-dropdown, does not work with keyboard navigation enabled. This is because Enter propagates to the global handler, causing the field editor to be retriggered or block toast messages to be shown. * test: inverted test for colour field enter Currently with 6.0.2 this triggers the global shortcuts. * fix: account for ephemeral focus in enter precondition Previously bugs could occur if the field editor contained a button but did prevent keydown events propagating.
1 parent c3544b7 commit 82f5998

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed

package-lock.json

Lines changed: 19 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
],
5050
"devDependencies": {
5151
"@blockly/dev-scripts": "^4.0.8",
52-
"@blockly/field-colour": "^6.0.0",
52+
"@blockly/field-colour": "^6.0.2",
5353
"@eslint/eslintrc": "^2.1.2",
5454
"@eslint/js": "^8.49.0",
5555
"@types/chai": "^5.2.1",

src/actions/enter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export class EnterAction {
6464
const targetWorkspace = workspace.isFlyout
6565
? workspace.targetWorkspace
6666
: workspace;
67-
return !!targetWorkspace && !targetWorkspace.isReadOnly();
67+
if (!targetWorkspace) return false;
68+
return this.navigation.canCurrentlyEdit(targetWorkspace);
6869
}
6970
default:
7071
return false;
@@ -111,6 +112,8 @@ export class EnterAction {
111112
* @returns True if the enter action should be handled.
112113
*/
113114
private shouldHandleEnterForWS(workspace: WorkspaceSvg): boolean {
115+
if (!this.navigation.canCurrentlyNavigate(workspace)) return false;
116+
114117
const cursor = workspace.getCursor();
115118
const curNode = cursor?.getCurNode();
116119
if (!curNode) return false;

src/navigation.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ export class Navigation {
9696
* @returns The state of the given workspace.
9797
*/
9898
getState(): Constants.STATE {
99-
const focusedTree = Blockly.getFocusManager().getFocusedTree();
99+
const focusManager = Blockly.getFocusManager();
100+
if (focusManager.ephemeralFocusTaken()) {
101+
return Constants.STATE.NOWHERE;
102+
}
103+
104+
const focusedTree = focusManager.getFocusedTree();
100105
if (focusedTree instanceof Blockly.WorkspaceSvg) {
101106
if (focusedTree.isFlyout) {
102107
return Constants.STATE.FLYOUT;
@@ -837,11 +842,7 @@ export class Navigation {
837842
workspace.targetWorkspace ??
838843
workspace
839844
).keyboardAccessibilityMode;
840-
return (
841-
!!accessibilityMode &&
842-
this.getState() !== Constants.STATE.NOWHERE &&
843-
!Blockly.getFocusManager().ephemeralFocusTaken()
844-
);
845+
return !!accessibilityMode && this.getState() !== Constants.STATE.NOWHERE;
845846
}
846847

847848
/**
@@ -854,7 +855,7 @@ export class Navigation {
854855
* @returns whether keyboard navigation and editing is currently allowed.
855856
*/
856857
canCurrentlyEdit(workspace: Blockly.WorkspaceSvg) {
857-
return this.canCurrentlyNavigate(workspace) && !workspace.options.readOnly;
858+
return this.canCurrentlyNavigate(workspace) && !workspace.isReadOnly();
858859
}
859860

860861
/**

test/webdriverio/test/basic_test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,26 @@ suite('Keyboard navigation on Fields', function () {
363363
// The same field should still be focused
364364
chai.assert.equal(await getFocusedFieldName(this.browser), 'BOOL');
365365
});
366+
367+
test('Do not reopen field editor when handling enter to make a choice inside the editor', async function () {
368+
// Open colour picker
369+
await focusOnBlockField(this.browser, 'colour_picker_1', 'COLOUR');
370+
await this.browser.pause(PAUSE_TIME);
371+
await this.browser.keys(Key.Enter);
372+
await this.browser.pause(PAUSE_TIME);
373+
374+
// Move right to pick a new colour.
375+
await keyRight(this.browser);
376+
// Enter to choose.
377+
await this.browser.keys(Key.Enter);
378+
379+
// Focus seems to take longer than a single pause to settle.
380+
await this.browser.waitUntil(
381+
() =>
382+
this.browser.execute(() =>
383+
document.activeElement?.classList.contains('blocklyActiveFocus'),
384+
),
385+
{timeout: 1000},
386+
);
387+
});
366388
});

0 commit comments

Comments
 (0)