Skip to content

Commit 2d401e5

Browse files
authored
feat: only init the navigation cursor when needed (#141)
This introduces improve initialization logic around the navigation cursor whereby it's only initialized upon focus (with previous position being retained) rather than upon enabling the controller (since the cursor doesn't actually need to be active when navigation isn't enabled).
1 parent 5031327 commit 2d401e5

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ export class KeyboardNavigation {
3636
workspace.getParentSvg().removeAttribute('tabindex');
3737

3838
workspace.getSvgGroup().addEventListener('focus', () => {
39-
navigationController.setHasFocus(true);
39+
navigationController.setHasFocus(workspace, true);
4040
});
4141
workspace.getSvgGroup().addEventListener('blur', () => {
42-
navigationController.setHasFocus(false);
42+
navigationController.setHasFocus(workspace, false);
4343
});
4444
}
4545

src/navigation.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,14 +535,15 @@ export class Navigation {
535535
* top block on a workspace or to the workspace.
536536
*
537537
* @param workspace The workspace to focus on.
538+
* @param keepCursorPosition Whether to retain the cursor's previous position.
538539
*/
539-
focusWorkspace(workspace: Blockly.WorkspaceSvg) {
540+
focusWorkspace(workspace: Blockly.WorkspaceSvg, keepCursorPosition: boolean = false) {
540541
workspace.hideChaff();
541542
const reset = !!workspace.getToolbox();
542543

543544
this.resetFlyout(workspace, reset);
544545
this.setState(workspace, Constants.STATE.WORKSPACE);
545-
this.setCursorOnWorkspaceFocus(workspace);
546+
this.setCursorOnWorkspaceFocus(workspace, keepCursorPosition);
546547
}
547548

548549
/**
@@ -551,13 +552,18 @@ export class Navigation {
551552
* the workspace.
552553
*
553554
* @param workspace The main Blockly workspace.
555+
* @param keepPosition Whether to retain the cursor's previous position.
554556
*/
555-
setCursorOnWorkspaceFocus(workspace: Blockly.WorkspaceSvg) {
557+
setCursorOnWorkspaceFocus(workspace: Blockly.WorkspaceSvg, keepPosition: boolean) {
556558
const topBlocks = workspace.getTopBlocks(true);
557559
const cursor = workspace.getCursor();
558560
if (!cursor) {
559561
return;
560562
}
563+
if (cursor.getCurNode() && keepPosition) {
564+
// Retain the cursor's previous position since it's set.
565+
return;
566+
}
561567
const wsCoordinates = new Blockly.utils.Coordinate(
562568
this.DEFAULT_WS_COORDINATE.x / workspace.scale,
563569
this.DEFAULT_WS_COORDINATE.y / workspace.scale,
@@ -1128,7 +1134,6 @@ export class Navigation {
11281134
!workspace.keyboardAccessibilityMode
11291135
) {
11301136
workspace.keyboardAccessibilityMode = true;
1131-
this.focusWorkspace(workspace);
11321137
}
11331138
}
11341139

src/navigation_controller.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,18 @@ export class NavigationController {
138138
}
139139

140140
/**
141-
* Sets whether the navigation controller has focus. This has no side effect
142-
* unless auto navigation is enabled via setHasAutoNavigationEnabled.
141+
* Sets whether the navigation controller has focus. This will enable keyboard
142+
* navigation if focus is now gained. Additionally, the cursor may be reset if
143+
* it hasn't already been positioned in the workspace.
143144
*
145+
* @param workspace the workspace that now has input focus.
144146
* @param isFocused whether the environment has browser focus.
145147
*/
146-
setHasFocus(isFocused: boolean) {
148+
setHasFocus(workspace: WorkspaceSvg, isFocused: boolean) {
147149
this.hasNavigationFocus = isFocused;
150+
if (isFocused) {
151+
this.navigation.focusWorkspace(workspace, true);
152+
}
148153
}
149154

150155
/**

0 commit comments

Comments
 (0)