/
keyboard-shortcuts.ts
211 lines (197 loc) · 6.04 KB
/
keyboard-shortcuts.ts
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* Add a tooltip showing the keyboard shortcut over the target element.
* @param target - The target element to show the tooltip on.
* @returns A function to remove event handler.
*/
function showTooltip(target: HTMLElement): () => void {
const tooltip = document.createElement("div");
const isHidden = target.classList.contains("hidden");
if (isHidden) {
target.classList.remove("hidden");
}
tooltip.className = "keyboard-tooltip";
tooltip.textContent = target.getAttribute("data-key") ?? "";
document.body.appendChild(tooltip);
const parentCoords = target.getBoundingClientRect();
// Padded 10px to the left if there is space or centered otherwise
const left =
parentCoords.left +
Math.min((target.offsetWidth - tooltip.offsetWidth) / 2, 10);
const top =
parentCoords.top + (target.offsetHeight - tooltip.offsetHeight) / 2;
tooltip.style.left = `${left}px`;
tooltip.style.top = `${top + window.pageYOffset}px`;
return (): void => {
tooltip.remove();
if (isHidden) {
target.classList.add("hidden");
}
};
}
/**
* Show all keyboard shortcut tooltips.
*/
function showTooltips(): () => void {
const removes: (() => void)[] = [];
document.querySelectorAll("[data-key]").forEach((el) => {
if (el instanceof HTMLElement) {
removes.push(showTooltip(el));
}
});
return (): void => {
removes.forEach((r) => r());
};
}
/**
* Ignore events originating from editable elements.
* @param element - The element to check.
* @returns true if the element is one of input/select/textarea or a
* contentEditable element.
*/
function isEditableElement(element: EventTarget | null): boolean {
return (
element instanceof HTMLElement &&
(element instanceof HTMLInputElement ||
element instanceof HTMLSelectElement ||
element instanceof HTMLTextAreaElement ||
element.isContentEditable)
);
}
/** A handler function or an element to click. */
type KeyboardShortcutAction = ((event: KeyboardEvent) => void) | HTMLElement;
const keyboardShortcuts = new Map<string, KeyboardShortcutAction>();
// The last typed character to check for sequences of two keys.
let lastChar = "";
/**
* Handle a `keydown` event on the document.
*
* Dispatch to the relevant handler.
*/
function keydown(event: KeyboardEvent): void {
if (isEditableElement(event.target)) {
// ignore events in editable elements.
return;
}
let eventKey = event.key;
if (event.metaKey) {
eventKey = `Meta+${eventKey}`;
}
if (event.altKey) {
eventKey = `Alt+${eventKey}`;
}
if (event.ctrlKey) {
eventKey = `Control+${eventKey}`;
}
const lastTwoKeys = `${lastChar} ${eventKey}`;
const handler =
keyboardShortcuts.get(lastTwoKeys) ?? keyboardShortcuts.get(eventKey);
if (handler) {
if (handler instanceof HTMLInputElement) {
event.preventDefault();
handler.focus();
} else if (handler instanceof HTMLElement) {
handler.click();
} else {
handler(event);
}
}
if (event.key !== "Alt" && event.key !== "Control" && event.key !== "Shift") {
lastChar = eventKey;
}
}
document.addEventListener("keydown", keydown);
/** A type to specify a platform-dependent keyboard shortcut. */
export type KeySpec = string | { key: string; mac: string };
const currentPlatform: "mac" | "key" =
typeof navigator !== "undefined" && /Mac/.test(navigator.platform)
? "mac"
: "key";
export const modKey = currentPlatform === "mac" ? "Cmd" : "Ctrl";
/**
* Get the keyboard key specifier string for the current platform.
* @param keySpec - The key spec.
*/
export function getKeySpecKey(keySpec: KeySpec): string {
if (typeof keySpec === "string") {
return keySpec;
}
return currentPlatform === "mac" ? keySpec.mac : keySpec.key;
}
/**
* Bind an event handler to a key.
* @param key - The key to bind.
* @param handler - The callback to run on key press.
* @returns A function to unbind the keyboard handler.
*/
export function bindKey(
keySpec: KeySpec,
handler: KeyboardShortcutAction
): () => void {
const key = getKeySpecKey(keySpec);
const sequence = key.split(" ");
if (sequence.length > 2) {
// eslint-disable-next-line no-console
console.error("Only key sequences of length <=2 are supported: ", key);
}
if (keyboardShortcuts.has(key)) {
// eslint-disable-next-line no-console
console.warn("Duplicate keyboard shortcut: ", key, handler);
}
keyboardShortcuts.set(key, handler);
return (): void => {
keyboardShortcuts.delete(key);
};
}
/**
* A svelte action to attach a global keyboard shortcut.
*
* This will attach a listener for the given key (or key sequence of length 2).
* This listener will focus the given node if it is an <input> element and
* trigger a click on it otherwise.
*/
export function keyboardShortcut(
node: HTMLElement,
key?: string
): { destroy?: () => void } {
if (!key) {
return {};
}
node.setAttribute("data-key", key);
const destroy = bindKey(key, node);
return { destroy };
}
/**
* Register keyboard shortcuts for all newly added elements with a
* `data-keyboard-shortcut` attribute.
*/
export function initCurrentKeyboardShortcuts(): void {
// clean up
for (const [key, action] of keyboardShortcuts.entries()) {
if (action instanceof HTMLElement && !document.contains(action)) {
keyboardShortcuts.delete(key);
}
}
document.querySelectorAll("[data-keyboard-shortcut]").forEach((element) => {
const key = element.getAttribute("data-keyboard-shortcut");
if (key && element instanceof HTMLElement) {
element.removeAttribute("data-keyboard-shortcut");
element.setAttribute("data-key", key);
bindKey(key, element);
}
});
}
/**
* Register the keys to show/hide the tooltips
*/
export function initGlobalKeyboardShortcuts(): void {
bindKey("?", () => {
const hide = showTooltips();
const once = () => {
hide();
document.removeEventListener("mousedown", once);
document.removeEventListener("keydown", once);
};
document.addEventListener("mousedown", once);
document.addEventListener("keydown", once);
});
}