-
Notifications
You must be signed in to change notification settings - Fork 3k
/
context.js
233 lines (202 loc) · 8.1 KB
/
context.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { createUniqueMap } from '../utils/dataStructures/uniqueMap';
import { normalizeKeys, getKeysList } from './utils';
import { isUndefined, isDefined } from '../helpers/mixed';
import { isFunction } from '../helpers/function';
import { objectEach, isObject } from '../helpers/object';
import { toSingleLine } from '../helpers/templateLiteralTag';
const __kindOf = Symbol('shortcut-context');
/**
* Checks if the provided object is a context object.
*
* @param {*} objectToCheck An object to check.
* @returns {boolean}
*/
export function isContextObject(objectToCheck) {
return isObject(objectToCheck) && objectToCheck.__kindOf === __kindOf;
}
/* eslint-disable jsdoc/require-description-complete-sentence */
/**
* The `ShortcutContext` API lets you store and manage [keyboard shortcuts](@/guides/navigation/keyboard-shortcuts.md) in a given [context](@/guides/navigation/keyboard-shortcuts.md#keyboard-shortcut-contexts).
*
* Each `ShortcutContext` object stores and manages its own set of keyboard shortcuts.
*
* @alias ShortcutContext
* @class ShortcutContext
* @param {string} name The name of the keyboard shortcut context
* @returns {object}
*/
export const createContext = (name) => {
const SHORTCUTS = createUniqueMap({
errorIdExists: keys => `The "${keys}" shortcut is already registered in the "${name}" context.`
});
/**
* Add a keyboard shortcut to this context.
*
* @memberof ShortcutContext#
* @param {object} options The shortcut's options
* @param {Array<Array<string>>} options.keys Names of the shortcut's keys,
* (coming from [`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)),
* in lowercase or uppercase, unified across browsers
* @param {Function} options.callback The shortcut's action
* @param {object} options.group A group of shortcuts to which the shortcut belongs
* @param {object} [options.runOnlyIf] A condition on which the shortcut's action runs
* @param {object} [options.stopPropagation=false] If set to `true`: stops the event's propagation
* @param {object} [options.captureCtrl=false] If set to `true`: captures the state of the Control/Meta modifier key
* @param {object} [options.preventDefault=true] If set to `true`: prevents the default behavior
* @param {object} [options.position='after'] The order in which the shortcut's action runs:
* `'before'` or `'after'` the `relativeToGroup` group of actions
* @param {object} [options.relativeToGroup] The name of a group of actions, used to determine an action's `position`
* @param {object} [options.forwardToContext] The context object where the event will be forwarded to.
*/
const addShortcut = (
{
keys,
callback,
group,
runOnlyIf = () => true,
captureCtrl = false,
preventDefault = true,
stopPropagation = false,
relativeToGroup,
position,
forwardToContext,
} = {}) => {
if (isUndefined(group)) {
throw new Error('You need to define the shortcut\'s group.');
}
if (isFunction(callback) === false) {
throw new Error('The shortcut\'s callback needs to be a function.');
}
if (Array.isArray(keys) === false) {
throw new Error(toSingleLine`Pass the shortcut\'s keys as an array of arrays,\x20
using the KeyboardEvent.key properties:\x20
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values.`);
}
const newShortcut = {
callback,
group,
runOnlyIf,
captureCtrl,
preventDefault,
stopPropagation,
};
if (isDefined(relativeToGroup)) {
[newShortcut.relativeToGroup, newShortcut.position] = [relativeToGroup, position];
}
if (isContextObject(forwardToContext)) {
newShortcut.forwardToContext = forwardToContext;
}
keys.forEach((keyCombination) => {
const normalizedKeys = normalizeKeys(keyCombination);
const hasKeyCombination = SHORTCUTS.hasItem(normalizedKeys);
if (hasKeyCombination) {
const shortcuts = SHORTCUTS.getItem(normalizedKeys);
let insertionIndex = shortcuts.findIndex(shortcut => shortcut.group === relativeToGroup);
if (insertionIndex !== -1) {
if (position === 'before') {
insertionIndex -= 1;
} else {
insertionIndex += 1;
}
} else {
insertionIndex = shortcuts.length;
}
shortcuts.splice(insertionIndex, 0, newShortcut);
} else {
SHORTCUTS.addItem(normalizedKeys, [newShortcut]);
}
});
};
/**
* Add multiple keyboard shortcuts to this context.
*
* @memberof ShortcutContext#
* @param {Array<object>} shortcuts List of shortcuts to add to this shortcut context
* @param {object} [options] A shortcut's options
* @param {Function} [options.callback] A shortcut's action
* @param {object} [options.group] A group of shortcuts to which a shortcut belongs
* @param {object} [options.runOnlyIf] A condition on which a shortcut's action runs
* @param {object} [options.stopPropagation=false] If set to `true`: stops the event's propagation
* @param {object} [options.preventDefault=true] If set to `true`: prevents the default behavior
* @param {object} [options.position='after'] The order in which a shortcut's action runs:
* `'before'` or `'after'` a `relativeToGroup` group of actions
* @param {object} [options.relativeToGroup] The name of a group of actions, used to determine an action's `position`
* @param {object} [options.forwardToContext] The context object where the event will be forwarded to.
*/
const addShortcuts = (shortcuts, options = {}) => {
shortcuts.forEach((shortcut) => {
objectEach(options, (value, key) => {
if (Object.prototype.hasOwnProperty.call(shortcut, key) === false) {
shortcut[key] = options[key];
}
});
addShortcut(shortcut);
});
};
/**
* Remove a shortcut from this context.
*
* @memberof ShortcutContext#
* @param {Array<string>} keys Names of the shortcut's keys,
* (coming from [`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)),
* in lowercase or uppercase, unified across browsers
*/
const removeShortcutsByKeys = (keys) => {
const normalizedKeys = normalizeKeys(keys);
SHORTCUTS.removeItem(normalizedKeys);
};
/**
* Remove a group of shortcuts from this context.
*
* @memberof ShortcutContext#
* @param {string} group The name of the group of shortcuts
*/
const removeShortcutsByGroup = (group) => {
const shortcuts = SHORTCUTS.getItems();
shortcuts.forEach(([normalizedKeys, shortcutOptions]) => {
const leftOptions = shortcutOptions.filter(option => option.group !== group);
if (leftOptions.length === 0) {
removeShortcutsByKeys(getKeysList(normalizedKeys));
} else {
shortcutOptions.length = 0;
shortcutOptions.push(...leftOptions);
}
});
};
/**
* Get a shortcut's details.
*
* @memberof ShortcutContext#
* @param {Array<string>} keys Names of the shortcut's keys,
* (coming from [`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)),
* in lowercase or uppercase, unified across browsers
* @returns {Array}
*/
const getShortcuts = (keys) => {
const normalizedKeys = normalizeKeys(keys);
const shortcuts = SHORTCUTS.getItem(normalizedKeys);
return isDefined(shortcuts) ? shortcuts.slice() : [];
};
/**
* Check if a shortcut exists in this context.
*
* @memberof ShortcutContext#
* @param {Array<string>} keys Names of the shortcut's keys,
* (coming from [`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)),
* in lowercase or uppercase, unified across browsers
* @returns {boolean}
*/
const hasShortcut = (keys) => {
const normalizedKeys = normalizeKeys(keys);
return SHORTCUTS.hasItem(normalizedKeys);
};
return {
__kindOf,
addShortcut,
addShortcuts,
getShortcuts,
hasShortcut,
removeShortcutsByKeys,
removeShortcutsByGroup,
};
};