/
KeyboardShortcutMenu.tsx
179 lines (155 loc) · 4.78 KB
/
KeyboardShortcutMenu.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module KeyboardShortcut
*/
import * as React from "react";
import type { CommonProps } from "@itwin/core-react";
import {
ContextMenuItem,
ContextSubMenu,
GlobalContextMenu,
} from "@itwin/core-react";
import type { KeyboardShortcut } from "./KeyboardShortcut";
import { ConditionalBooleanValue, UiEvent } from "@itwin/appui-abstract";
/** State for a [[KeyboardShortcutMenuEvent]] and [[KeyboardShortcutMenu]] component
* @public
*/
export interface KeyboardShortcutMenuState {
menuVisible: boolean;
menuX: number;
menuY: number;
shortcuts?: KeyboardShortcut[];
}
/** KeyboardShortcut Menu Event class.
* @public
*/
// eslint-disable-next-line deprecation/deprecation
export class KeyboardShortcutMenuEvent extends UiEvent<KeyboardShortcutMenuState> {}
/** React component that displays a context menu at the cursor containing keyboard shortcuts.
* @public
*/
export class KeyboardShortcutMenu extends React.PureComponent<
CommonProps,
KeyboardShortcutMenuState
> {
/** @internal */
public override readonly state: KeyboardShortcutMenuState = {
menuVisible: false,
menuX: 0,
menuY: 0,
};
/** Get KeyboardShortcut Menu Event. */
public static readonly onKeyboardShortcutMenuEvent =
new KeyboardShortcutMenuEvent();
public override componentDidMount() {
KeyboardShortcutMenu.onKeyboardShortcutMenuEvent.addListener(
this._handleKeyboardShortcutMenuEvent
);
}
public override componentWillUnmount() {
KeyboardShortcutMenu.onKeyboardShortcutMenuEvent.removeListener(
this._handleKeyboardShortcutMenuEvent
);
}
private _handleKeyboardShortcutMenuEvent = (
state: KeyboardShortcutMenuState
) => {
this.setState(state);
};
public override render(): React.ReactNode {
const { shortcuts, menuX, menuY, menuVisible } = this.state;
const onClose = this._hideContextMenu;
if (menuVisible) {
const items = this.getShortcutMenuItems(shortcuts);
return (
<GlobalContextMenu
className={this.props.className}
style={this.props.style}
identifier="keyboard-shortcuts"
x={menuX}
y={menuY}
opened={menuVisible}
onEsc={onClose}
onOutsideClick={onClose}
edgeLimit={false}
autoflip={true}
ignoreNextKeyUp={true} // Executing the shortcut on the keydown, so ignore the keyup in the menu
>
{items}
</GlobalContextMenu>
);
}
return null;
}
private getShortcutMenuItems(
shortcuts?: KeyboardShortcut[]
): React.ReactNode[] {
const items: React.ReactNode[] = [];
// istanbul ignore else
if (shortcuts) {
shortcuts.forEach((shortcut: KeyboardShortcut, index: number) => {
const item = this.getShortcutMenuItem(shortcut, index);
// istanbul ignore else
if (item) items.push(item);
});
}
return items;
}
private getShortcutMenuItem(
shortcut: KeyboardShortcut,
index: number
): React.ReactNode {
const shortcutKey = shortcut.key;
const isHidden = ConditionalBooleanValue.getValue(shortcut.isHidden);
// Only pure characters go into the context menu
if (
shortcutKey !== shortcut.keyMapKey ||
shortcut.isFunctionKey ||
shortcut.isSpecialKey ||
isHidden
)
return null;
let node: React.ReactNode = null;
let label = shortcut.label;
const iconSpec = shortcut.iconSpec;
label = `~${shortcutKey.toLocaleUpperCase()} ${label}`;
if (shortcut.shortcutContainer.areKeyboardShortcutsAvailable()) {
const shortcuts =
shortcut.shortcutContainer.getAvailableKeyboardShortcuts();
const items = this.getShortcutMenuItems(shortcuts);
node = (
<ContextSubMenu
key={index}
icon={iconSpec}
label={label}
disabled={shortcut.isDisabled}
>
{items}
</ContextSubMenu>
);
} else {
const sel = () => this._itemPicked(shortcut);
node = (
<ContextMenuItem
key={index}
onSelect={sel}
icon={iconSpec}
disabled={shortcut.isDisabled}
>
{label}
</ContextMenuItem>
);
}
return node;
}
private _hideContextMenu = () => {
this.setState({ menuVisible: false, shortcuts: undefined });
};
private _itemPicked = (shortcut: KeyboardShortcut): void => {
this._hideContextMenu();
shortcut.itemPicked();
};
}