-
Notifications
You must be signed in to change notification settings - Fork 160
/
GlobalKeyEventStrategy.js
735 lines (609 loc) · 23.7 KB
/
GlobalKeyEventStrategy.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
import KeyEventRecordManager from '../KeyEventRecordManager';
import KeyEventRecordIndex from '../../const/KeyEventRecordIndex';
import AbstractKeyEventStrategy from './AbstractKeyEventStrategy';
import capitalize from '../../utils/string/capitalize';
import describeKeyEventType from '../../helpers/logging/describeKeyEventType';
import KeyEventCounter from '../KeyEventCounter';
import Logger from '../Logger';
import removeAtIndex from '../../utils/array/removeAtIndex';
import isUndefined from '../../utils/isUndefined';
import printComponent from '../../helpers/logging/printComponent';
import getKeyName from '../../helpers/resolving-handlers/getKeyName';
import Configuration from '../Configuration';
import describeKeyEvent from '../../helpers/logging/describeKeyEvent';
import isCmdKey from '../../helpers/parsing-key-maps/isCmdKey';
import EventResponse from '../../const/EventResponse';
import contains from '../../utils/collection/contains';
import dictionaryFrom from '../../utils/object/dictionaryFrom';
/**
* Defines behaviour for dealing with key maps defined in global HotKey components
* @class
*/
class GlobalKeyEventStrategy extends AbstractKeyEventStrategy {
/********************************************************************************
* Init & Reset
********************************************************************************/
constructor(configuration = {}, keyEventManager) {
/**
* Set state that gets cleared every time a component gets mounted or unmounted
*/
super(configuration, keyEventManager);
/**
* Set state that doesn't get cleared each time a new new component is mounted
* or unmounted
* @type {number}
*/
/**
* Whether the global key event handlers have been bound to document yet or not
* @type {boolean}
*/
this.listenersBound = false;
this.eventOptions = {
ignoreEventsCondition: Configuration.option('ignoreEventsCondition')
};
/**
* Dictionary of listener functions - currently only intended to house
* keyCombinationListener
*/
this.listeners = {};
}
/********************************************************************************
* Enabling key maps and handlers
********************************************************************************/
/**
* Registers the actions and handlers of a HotKeys component that has mounted
* @param {ComponentId} componentId - Id of the component that the keyMap belongs to
* @param {KeyMap} actionNameToKeyMap - Map of actions to key expressions
* @param {HandlersMap} actionNameToHandlersMap - Map of actions to handler functions
* @param {Object} options Hash of options that configure how the actions
* and handlers are associated and called.
* @param {Object} eventOptions - Options for how the event should be handled
*/
enableHotKeys(componentId, actionNameToKeyMap = {}, actionNameToHandlersMap = {}, options, eventOptions) {
this.eventOptions = eventOptions;
this._addComponentToList(
componentId,
actionNameToKeyMap,
actionNameToHandlersMap,
options
);
this._updateDocumentHandlers();
/**
* Reset handler resolution state
*/
this._initHandlerResolutionState();
this.logger.debug(
this._logPrefix(componentId, {eventId: false}),
'Mounted.',
);
this.logger.verbose(
this._logPrefix(componentId, {eventId: false}),
'Component options: \n',
printComponent(this._getComponent(componentId))
);
}
/**
* Handles when a mounted global HotKeys component updates its props and changes
* either the keyMap or handlers prop value
* @param {ComponentId} componentId - The component index of the component to
* update
* @param {KeyMap} actionNameToKeyMap - Map of actions to key expressions
* @param {HandlersMap} actionNameToHandlersMap - Map of actions to handler functions
* @param {Object} options Hash of options that configure how the actions
* and handlers are associated and called.
* @param {Object} eventOptions - Options for how the event should be handled
*/
updateEnabledHotKeys(componentId, actionNameToKeyMap = {}, actionNameToHandlersMap = {}, options, eventOptions) {
this.eventOptions = eventOptions;
const componentPosition = this._getComponentPosition(componentId);
/**
* Manually update the registered key map state, usually reset using
* _resetRegisteredKeyMapsState() method
*/
this.componentList[componentPosition] = this._buildComponentOptions(
componentId,
actionNameToKeyMap,
actionNameToHandlersMap,
options
);
this._updateLongestKeySequenceIfNecessary(componentId);
/**
* Reset strategy state specific to the global strategy
*/
this._updateDocumentHandlers();
/**
* Reset handler resolution state
*/
this._initHandlerResolutionState();
this.logger.debug(
this._logPrefix(componentId, {eventId: false}),
`Global component ${componentId} updated.`,
);
this.logger.verbose(
this._logPrefix(componentId, {eventId: false}),
'Component options: \n',
printComponent(this._getComponent(componentId))
);
}
/**
* Handles when a component is unmounted
* @param {ComponentId} componentId - Index of component that is being unmounted
*/
disableHotKeys(componentId) {
const [{ keyMapEventRecord }, componentPosition ] =
this._getComponentAndPosition(componentId);
/**
* Manually update the registered key map state, usually reset using
* _resetRegisteredKeyMapsState() method
*/
this.componentList = removeAtIndex(this.componentList, componentPosition);
this._updateLongestKeySequenceIfNecessary(componentId);
/**
* Reset strategy state specific to the global strategy
*/
this._updateComponentIndexDictFromList({ startingAt: componentPosition });
this._updateDocumentHandlers(
keyMapEventRecord,
KeyEventRecordManager.newRecord()
);
/**
* Reset handler resolution state
*/
this._initHandlerResolutionState();
this.logger.debug(
this._logPrefix(componentId, {eventId: false}),
`Unmounted global component ${componentId}`
);
}
_updateLongestKeySequenceIfNecessary(componentId) {
if (componentId === this.longestSequenceComponentIndex) {
this.longestSequence = 1;
this.componentList.forEach(({longestSequence}) => {
if(longestSequence > this.longestSequence) {
this.longestSequence = longestSequence;
}
});
}
}
_updateComponentIndexDictFromList(options = { startingAt: 0 }) {
let counter = options.startingAt;
while(counter < this.componentList.length) {
this._setComponentPosition(this.componentList[counter].componentId, counter);
counter++;
}
}
_updateDocumentHandlers(){
const listenersShouldBeBound = this._listenersShouldBeBound();
if (!this.listenersBound && listenersShouldBeBound) {
for(let recordIndex = 0; recordIndex < this.keyMapEventRecord.length; recordIndex++) {
const eventName = describeKeyEventType(recordIndex);
document[`on${eventName}`] = (keyEvent) => {
this.keyEventManager[`handleGlobal${capitalize(eventName)}`](keyEvent);
};
this.logger.debug(
this._logPrefix(this.componentId, {eventId: false}),
`Bound handler handleGlobal${capitalize(eventName)}() to document.on${eventName}()`
);
}
this.listenersBound = true;
} else if(this.listenersBound && !listenersShouldBeBound) {
for(let recordIndex = 0; recordIndex < this.keyMapEventRecord.length; recordIndex++) {
const eventName = describeKeyEventType(recordIndex);
delete document[`on${eventName}`];
this.logger.debug(
this._logPrefix(this.componentId, {eventId: false}),
`Removed handler handleGlobal${capitalize(eventName)}() from document.on${eventName}()`
);
}
this.listenersBound = false;
}
}
/**
* Whether the document listeners should be bound, to record key events. Basically a check
* to see if there are any global key maps, or whether the user is currently rebinding to
* a new key combination.
* @returns {boolean} True if the document listeners should be bound
* @private
*/
_listenersShouldBeBound() {
return this.componentList.length > 0 || this.listeners.keyCombination;
}
/********************************************************************************
* Recording key events
********************************************************************************/
/**
* Records a keydown keyboard event and matches it against the list of pre-registered
* event handlers, calling the first matching handler with the highest priority if
* one exists.
*
* This method is called once when a keyboard event bubbles up to document, and checks
* the keymaps for all of the mounted global HotKey components.
* @param {KeyboardEvent} event - Event containing the key name and state
*/
handleKeydown(event) {
const _key = getKeyName(event);
if (event.repeat && Configuration.option('ignoreRepeatedEventsWhenKeyHeldDown')) {
this.logger.debug(
this._logPrefix(),
`Ignored repeated ${describeKeyEvent(event, _key, KeyEventRecordIndex.keydown)} event.`
);
return true;
}
this._checkForModifierFlagDiscrepancies(event, _key, KeyEventRecordIndex.keydown);
const reactAppResponse = this._howReactAppRespondedTo(
event,
_key,
KeyEventRecordIndex.keydown
);
if (reactAppResponse === EventResponse.unseen &&
this.eventOptions.ignoreEventsCondition(event)) {
this.logger.debug(
this._logPrefix(),
`Ignored ${describeKeyEvent(event, _key, KeyEventRecordIndex.keydown)} event because ignoreEventsFilter rejected it.`
);
return;
}
if (reactAppResponse !== EventResponse.ignored) {
const keyInCurrentCombination = !!this._getCurrentKeyState(_key);
const keyEventState = this._stateFromEvent(event);
if (keyInCurrentCombination || this.keyCombinationIncludesKeyUp) {
this._startAndLogNewKeyCombination(
_key,
KeyEventRecordIndex.keydown,
keyEventState
);
} else {
this._addToAndLogCurrentKeyCombination(
_key,
KeyEventRecordIndex.keydown,
keyEventState
);
}
}
if (!contains([EventResponse.ignored, EventResponse.handled], reactAppResponse)) {
this._callHandlerIfExists(event, _key, KeyEventRecordIndex.keydown);
}
this._simulateKeypressForNonPrintableKeys(event, _key);
}
_howReactAppRespondedTo(event, key, eventRecordIndex) {
const reactAppHistoryWithEvent =
this.keyEventManager.reactAppHistoryWithEvent(key, eventRecordIndex);
switch(reactAppHistoryWithEvent) {
case EventResponse.handled:
this.logger.debug(
this._logPrefix(),
`Ignored ${describeKeyEvent(event, key, eventRecordIndex)} event because React app has already handled it.`
);
break;
case EventResponse.ignored:
this.logger.debug(
this._logPrefix(),
`Ignored ${describeKeyEvent(event, key, eventRecordIndex)} event because React app has declared it should be ignored.`
);
break;
case EventResponse.seen:
this.logger.debug(
this._logPrefix(),
`Received ${describeKeyEvent(event, key, eventRecordIndex)} event (that has already passed through React app).`
);
break;
default:
KeyEventCounter.incrementId();
this.logger.debug(
this._logPrefix(),
`New ${describeKeyEvent(event, key, eventRecordIndex)} event (that has NOT passed through React app).`
);
}
return reactAppHistoryWithEvent;
}
/**
* Records a keypress keyboard event and matches it against the list of pre-registered
* event handlers, calling the first matching handler with the highest priority if
* one exists.
*
* This method is called once when a keyboard event bubbles up to document, and checks
* the keymaps for all of the mounted global HotKey components.
* @param {KeyboardEvent} event - Event containing the key name and state
*/
handleKeypress(event) {
const key = getKeyName(event);
if (event.repeat && Configuration.option('ignoreRepeatedEventsWhenKeyHeldDown')) {
this.logger.debug(
this._logPrefix(),
`Ignored repeated ${describeKeyEvent(event, key, KeyEventRecordIndex.keypress)} event.`
);
return true;
}
if (this._alreadySimulatedEvent(KeyEventRecordIndex.keypress, key)) {
this.logger.debug(
this._logPrefix(),
`Ignored ${describeKeyEvent(event, key, KeyEventRecordIndex.keypress)} as it was not expected, and has already been simulated.`
);
return true;
}
/**
* We first decide if the keypress event should be handled (to ensure the correct
* order of logging statements)
*/
const reactAppResponse = this._howReactAppRespondedTo(
event,
key,
KeyEventRecordIndex.keypress
);
/**
* Add new key event to key combination history
*/
if (this._getCurrentKeyState(key)) {
this._addToAndLogCurrentKeyCombination(
key,
KeyEventRecordIndex.keypress,
this._stateFromEvent(event)
);
}
if (reactAppResponse === EventResponse.unseen) {
/**
* If the key event has not been seen by the React application, we ensure that
* it's not still waiting for it. This occurs when action handlers bound to keydown
* move the focus outside of the react app before it can record the keypress or
* keyup
*/
this.keyEventManager.closeHangingKeyCombination(key, KeyEventRecordIndex.keypress);
if (this.eventOptions.ignoreEventsCondition(event)) {
this.logger.debug(
this._logPrefix(),
`Ignored ${describeKeyEvent(event, key, KeyEventRecordIndex.keypress)} event because ignoreEventsFilter rejected it.`
);
return;
}
}
if (!contains([EventResponse.ignored, EventResponse.handled], reactAppResponse)) {
this._callHandlerIfExists(event, key, KeyEventRecordIndex.keypress);
}
}
/**
* Records a keyup keyboard event and matches it against the list of pre-registered
* event handlers, calling the first matching handler with the highest priority if
* one exists.
*
* This method is called once when a keyboard event bubbles up to document, and checks
* the keymaps for all of the mounted global HotKey components.
* @param {KeyboardEvent} event - Event containing the key name and state
*/
handleKeyup(event) {
const key = getKeyName(event);
if (this._alreadySimulatedEvent(KeyEventRecordIndex.keyup, key)) {
this.logger.debug(
this._logPrefix(),
`Ignored ${describeKeyEvent(event, key, KeyEventRecordIndex.keyup)} as it was not expected, and has already been simulated.`
);
return true;
}
/**
* We first decide if the keyup event should be handled (to ensure the correct
* order of logging statements)
*/
const reactAppResponse = this._howReactAppRespondedTo(
event,
key,
KeyEventRecordIndex.keyup
);
/**
* We then add the keyup to our current combination - regardless of whether
* it's to be handled or not. We need to do this to ensure that if a handler
* function changes focus to a context that ignored events, the keyup event
* is not lost (leaving react hotkeys thinking the key is still pressed).
*/
if (this._getCurrentKeyState(key)) {
this._addToAndLogCurrentKeyCombination(
key,
KeyEventRecordIndex.keyup,
this._stateFromEvent(event)
);
}
if (reactAppResponse === EventResponse.unseen){
/**
* If the key event has not been seen by the React application, we ensure that
* it's not still waiting for it. This occurs when action handlers bound to keydown
* or keypress move the focus outside of the react app before it can record the keyup
*/
this.keyEventManager.closeHangingKeyCombination(key, KeyEventRecordIndex.keyup);
if(this.eventOptions.ignoreEventsCondition(event)) {
this.logger.debug(
this._logPrefix(),
`Ignored ${describeKeyEvent(event, key, KeyEventRecordIndex.keyup)} event because ignoreEventsFilter rejected it.`
);
} else {
/**
* We attempt to find a handler of the event, only if it has not already
* been handled and should not be ignored
*/
if (!contains([EventResponse.ignored, EventResponse.handled], reactAppResponse)) {
this._callHandlerIfExists(event, key, KeyEventRecordIndex.keyup);
}
}
} else {
/**
* We attempt to find a handler of the event, only if it has not already
* been handled and should not be ignored
*/
if (!contains([EventResponse.ignored, EventResponse.handled], reactAppResponse)) {
this._callHandlerIfExists(event, key, KeyEventRecordIndex.keyup);
}
}
/**
* We simulate any hidden keyup events hidden by the command key, regardless
* of whether the event should be ignored or not
*/
this._simulateKeyUpEventsHiddenByCmd(event, key);
if (this.listeners.keyCombination && this._allKeysAreReleased()) {
const {keys,ids} = this._getCurrentKeyCombination();
this.listeners.keyCombination({
keys: dictionaryFrom(Object.keys(keys), true),
id: ids[0]
});
}
}
_simulateKeypressForNonPrintableKeys(event, key) {
this.keyEventManager.simulatePendingKeyPressEvents();
this._handleEventSimulation(
'handleKeypress',
this._shouldSimulate(KeyEventRecordIndex.keypress, key),
{event, key}
);
}
_simulateKeyUpEventsHiddenByCmd(event, key) {
if (isCmdKey(key)) {
/**
* We simulate pending key events in the React app before we do it globally
*/
this.keyEventManager.simulatePendingKeyUpEvents();
Object.keys(this._getCurrentKeyCombination().keys).forEach((keyName) => {
if (isCmdKey(keyName)) {
return;
}
this._handleEventSimulation(
'handleKeyup',
this._shouldSimulate(KeyEventRecordIndex.keyup, keyName),
{event, key: keyName}
);
});
}
}
_startAndLogNewKeyCombination(keyName, eventRecordIndex, keyEventState) {
this._startNewKeyCombination(keyName, eventRecordIndex, keyEventState);
this.logger.verbose(
this._logPrefix(),
`Started a new combination with '${keyName}'.`
);
this.logger.verbose(
this._logPrefix(),
`Key history: ${printComponent(this.keyCombinationHistory)}.`
);
}
_addToAndLogCurrentKeyCombination(keyName, eventRecordIndex, keyEventState) {
this._addToCurrentKeyCombination(keyName, eventRecordIndex, keyEventState);
if (eventRecordIndex === KeyEventRecordIndex.keydown) {
this.logger.verbose(
this._logPrefix(),
`Added '${keyName}' to current combination: '${this._getCurrentKeyCombination().ids[0]}'.`
);
}
this.logger.verbose(
this._logPrefix(),
`Key history: ${printComponent(this.keyCombinationHistory)}.`
);
}
/********************************************************************************
* Event simulation
********************************************************************************/
_handleEventSimulation(handlerName, shouldSimulate, {event, key}) {
if (shouldSimulate && Configuration.option('simulateMissingKeyPressEvents')) {
/**
* If a key does not have a keypress event, we simulate one immediately after
* the keydown event, to keep the behaviour consistent across all keys
*/
const _event = this._cloneAndMergeEvent(event, {key, simulated: true});
this[handlerName](_event);
}
}
/********************************************************************************
* Matching and calling handlers
********************************************************************************/
_callHandlerIfExists(event, keyName, eventRecordIndex) {
const eventName = describeKeyEventType(eventRecordIndex);
const combinationName = this._describeCurrentKeyCombination();
if (this.keyMapEventRecord[eventRecordIndex]) {
/**
* If there is at least one handler for the specified key event type (keydown,
* keypress, keyup), then attempt to find a handler that matches the current
* key combination
*/
this.logger.verbose(
this._logPrefix(),
`Attempting to find action matching '${combinationName}' ${eventName} . . .`
);
this._callMatchingHandlerClosestToEventTarget(
event,
keyName,
eventRecordIndex
);
} else {
/**
* If there are no handlers registered for the particular key event type
* (keydown, keypress, keyup) then skip trying to find a matching handler
* for the current key combination
*/
this.logger.debug(
this._logPrefix(),
`Ignored '${combinationName}' ${eventName} because it doesn't have any ${eventName} handlers.`
);
}
}
_callMatchingHandlerClosestToEventTarget(event, keyName, eventRecordIndex) {
for(let componentPosition = 0; componentPosition < this.componentList.length; componentPosition++) {
const matchFound = super._callMatchingHandlerClosestToEventTarget(
event,
keyName,
eventRecordIndex,
componentPosition,
0
);
if (matchFound) {
this.logger.debug(
this._logPrefix(),
`Searching no further, as handler has been found (and called).`
);
return;
}
}
}
_stopEventPropagation(event, componentId) {
this.logger.debug(
this._logPrefix(componentId),
'Stopping further event propagation.'
);
if (!event.simulated) {
event.stopPropagation();
}
}
/********************************************************************************
* Recording key combination
********************************************************************************/
/**
* Add a new key combination listener function to be called the next time a key
* combination completes (assuming the cancel function is not called).
* @param {keyCombinationListener} callbackFunction Function to call with the next
* completed key combination
* @returns {function} Function to call to cancel listening for the next key
* combination
*/
addKeyCombinationListener(callbackFunction) {
const cancel = () => {
delete this.listeners.keyCombination;
};
this.listeners.keyCombination = (keyCombination) => {
callbackFunction(keyCombination);
cancel();
};
this._updateDocumentHandlers();
return cancel;
}
/********************************************************************************
* Logging
********************************************************************************/
_logPrefix(componentId, options = {}) {
const eventIcons = Logger.eventIcons;
const componentIcons = Logger.componentIcons;
let base = 'HotKeys (GLOBAL';
if (options.eventId !== false) {
const eventId = isUndefined(options.eventId) ? KeyEventCounter.getId() : options.eventId;
base = `${base}-E${eventId}${eventIcons[eventId % eventIcons.length]}`
}
if (isUndefined(componentId)) {
return `${base}):`
} else {
return `${base}-C${componentId}${componentIcons[componentId % componentIcons.length]}):`;
}
}
}
export default GlobalKeyEventStrategy;