Skip to content

Commit d3dc71d

Browse files
authored
fix: Make navigation direction align with arrow keys in RTL. (#460)
1 parent 85378e5 commit d3dc71d

File tree

1 file changed

+70
-54
lines changed

1 file changed

+70
-54
lines changed

src/actions/arrow_navigation.ts

Lines changed: 70 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,70 @@ export class ArrowNavigation {
4747
* Adds all arrow key navigation shortcuts to the registry.
4848
*/
4949
install() {
50+
const navigateIn = (
51+
workspace: WorkspaceSvg,
52+
e: Event,
53+
shortcut: ShortcutRegistry.KeyboardShortcut,
54+
): boolean => {
55+
const toolbox = workspace.getToolbox() as Toolbox;
56+
let isHandled = false;
57+
switch (this.navigation.getState(workspace)) {
58+
case Constants.STATE.WORKSPACE:
59+
isHandled = this.fieldShortcutHandler(workspace, shortcut);
60+
if (!isHandled && workspace) {
61+
if (
62+
!this.navigation.defaultWorkspaceCursorPositionIfNeeded(workspace)
63+
) {
64+
workspace.getCursor()?.in();
65+
}
66+
isHandled = true;
67+
}
68+
return isHandled;
69+
case Constants.STATE.TOOLBOX:
70+
isHandled =
71+
toolbox && typeof toolbox.onShortcut === 'function'
72+
? toolbox.onShortcut(shortcut)
73+
: false;
74+
if (!isHandled) {
75+
this.navigation.focusFlyout(workspace);
76+
}
77+
return true;
78+
default:
79+
return false;
80+
}
81+
};
82+
83+
const navigateOut = (
84+
workspace: WorkspaceSvg,
85+
e: Event,
86+
shortcut: ShortcutRegistry.KeyboardShortcut,
87+
): boolean => {
88+
const toolbox = workspace.getToolbox() as Toolbox;
89+
let isHandled = false;
90+
switch (this.navigation.getState(workspace)) {
91+
case Constants.STATE.WORKSPACE:
92+
isHandled = this.fieldShortcutHandler(workspace, shortcut);
93+
if (!isHandled && workspace) {
94+
if (
95+
!this.navigation.defaultWorkspaceCursorPositionIfNeeded(workspace)
96+
) {
97+
workspace.getCursor()?.out();
98+
}
99+
isHandled = true;
100+
}
101+
return isHandled;
102+
case Constants.STATE.FLYOUT:
103+
this.navigation.focusToolbox(workspace);
104+
return true;
105+
case Constants.STATE.TOOLBOX:
106+
return toolbox && typeof toolbox.onShortcut === 'function'
107+
? toolbox.onShortcut(shortcut)
108+
: false;
109+
default:
110+
return false;
111+
}
112+
};
113+
50114
const shortcuts: {
51115
[name: string]: ShortcutRegistry.KeyboardShortcut;
52116
} = {
@@ -56,34 +120,9 @@ export class ArrowNavigation {
56120
preconditionFn: (workspace) =>
57121
this.navigation.canCurrentlyNavigate(workspace),
58122
callback: (workspace, e, shortcut) => {
59-
const toolbox = workspace.getToolbox() as Toolbox;
60-
let isHandled = false;
61-
switch (this.navigation.getState(workspace)) {
62-
case Constants.STATE.WORKSPACE:
63-
isHandled = this.fieldShortcutHandler(workspace, shortcut);
64-
if (!isHandled && workspace) {
65-
if (
66-
!this.navigation.defaultWorkspaceCursorPositionIfNeeded(
67-
workspace,
68-
)
69-
) {
70-
workspace.getCursor()?.in();
71-
}
72-
isHandled = true;
73-
}
74-
return isHandled;
75-
case Constants.STATE.TOOLBOX:
76-
isHandled =
77-
toolbox && typeof toolbox.onShortcut === 'function'
78-
? toolbox.onShortcut(shortcut)
79-
: false;
80-
if (!isHandled) {
81-
this.navigation.focusFlyout(workspace);
82-
}
83-
return true;
84-
default:
85-
return false;
86-
}
123+
return workspace.RTL
124+
? navigateOut(workspace, e, shortcut)
125+
: navigateIn(workspace, e, shortcut);
87126
},
88127
keyCodes: [KeyCodes.RIGHT],
89128
},
@@ -94,32 +133,9 @@ export class ArrowNavigation {
94133
preconditionFn: (workspace) =>
95134
this.navigation.canCurrentlyNavigate(workspace),
96135
callback: (workspace, e, shortcut) => {
97-
const toolbox = workspace.getToolbox() as Toolbox;
98-
let isHandled = false;
99-
switch (this.navigation.getState(workspace)) {
100-
case Constants.STATE.WORKSPACE:
101-
isHandled = this.fieldShortcutHandler(workspace, shortcut);
102-
if (!isHandled && workspace) {
103-
if (
104-
!this.navigation.defaultWorkspaceCursorPositionIfNeeded(
105-
workspace,
106-
)
107-
) {
108-
workspace.getCursor()?.out();
109-
}
110-
isHandled = true;
111-
}
112-
return isHandled;
113-
case Constants.STATE.FLYOUT:
114-
this.navigation.focusToolbox(workspace);
115-
return true;
116-
case Constants.STATE.TOOLBOX:
117-
return toolbox && typeof toolbox.onShortcut === 'function'
118-
? toolbox.onShortcut(shortcut)
119-
: false;
120-
default:
121-
return false;
122-
}
136+
return workspace.RTL
137+
? navigateIn(workspace, e, shortcut)
138+
: navigateOut(workspace, e, shortcut);
123139
},
124140
keyCodes: [KeyCodes.LEFT],
125141
},

0 commit comments

Comments
 (0)