This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathkeymap-manager-spec.coffee
1132 lines (943 loc) · 62.7 KB
/
keymap-manager-spec.coffee
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{$$} = require 'space-pencil'
debounce = require 'debounce'
fs = require 'fs-plus'
path = require 'path'
temp = require 'temp'
KeyboardLayout = require('keyboard-layout')
KeymapManager = require '../src/keymap-manager'
{appendContent, stub, getFakeClock, mockProcessPlatform, buildKeydownEvent, buildKeyupEvent} = require './helpers/helpers'
describe "KeymapManager", ->
keymapManager = null
beforeEach ->
mockProcessPlatform('darwin')
keymapManager = new KeymapManager
afterEach ->
keymapManager.destroy()
describe "::handleKeyboardEvent(event)", ->
describe "when the keystroke matches no bindings", ->
it "does not prevent the event's default action", ->
event = buildKeydownEvent(key: 'q')
keymapManager.handleKeyboardEvent(event)
assert(not event.defaultPrevented)
describe "when the keystroke matches one binding on any particular element", ->
[events, elementA, elementB] = []
beforeEach ->
elementA = appendContent $$ ->
@div class: 'a', ->
@div class: 'b c'
elementB = elementA.firstChild
events = []
elementA.addEventListener 'x-command', (e) -> events.push(e)
elementA.addEventListener 'y-command', (e) -> events.push(e)
keymapManager.add "test",
".a":
"ctrl-x": "x-command"
"ctrl-y": "y-command"
".c":
"ctrl-y": "z-command"
".b":
"ctrl-y": "y-command"
it "dispatches the matching binding's command event on the keyboard event's target", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'y', ctrlKey: true, target: elementB))
assert.equal(events.length, 1)
assert.equal(events[0].type, 'y-command')
assert.equal(events[0].target, elementB)
events = []
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'x', ctrlKey: true, target: elementB))
assert.equal(events.length, 1)
assert.equal(events[0].type, 'x-command')
assert.equal(events[0].target, elementB)
it "prevents the default action", ->
event = buildKeydownEvent(key: 'y', ctrlKey: true, target: elementB)
keymapManager.handleKeyboardEvent(event)
assert(event.defaultPrevented)
describe "if .abortKeyBinding() is called on the command event", ->
it "proceeds directly to the next matching binding and does not prevent the keyboard event's default action", ->
elementB.addEventListener 'y-command', (e) -> events.push(e); e.abortKeyBinding()
elementB.addEventListener 'y-command', (e) -> events.push(e) # should never be called
elementB.addEventListener 'z-command', (e) -> events.push(e); e.abortKeyBinding()
event = buildKeydownEvent(key: 'y', ctrlKey: true, target: elementB)
keymapManager.handleKeyboardEvent(event)
assert(not event.defaultPrevented)
assert.equal(events.length, 3)
assert.equal(events[0].type, 'y-command')
assert.equal(events[0].target, elementB)
assert.equal(events[1].type, 'z-command')
assert.equal(events[1].target, elementB)
assert.equal(events[2].type, 'y-command')
assert.equal(events[2].target, elementB)
describe "if the keyboard event's target is document.body", ->
it "starts matching keybindings at the .defaultTarget", ->
keymapManager.defaultTarget = elementA
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'y', ctrlKey: true, target: document.body))
assert.equal(events.length, 1)
assert.equal(events[0].type, 'y-command')
assert.equal(events[0].target, elementA)
describe "if the matching binding's command is 'native!'", ->
it "terminates without preventing the browser's default action", ->
elementA.addEventListener 'native!', (e) -> events.push(e)
keymapManager.add "test",
".b":
"ctrl-y": "native!"
event = buildKeydownEvent(key: 'y', ctrlKey: true, target: elementB)
keymapManager.handleKeyboardEvent(event)
assert.deepEqual(events, [])
assert(not event.defaultPrevented)
describe "if the matching binding's command is 'unset!'", ->
it "continues searching for a matching binding on the parent element", ->
elementA.addEventListener 'unset!', (e) -> events.push(e)
keymapManager.add "test",
".a":
"ctrl-y": "x-command"
".b":
"ctrl-y": "unset!"
event = buildKeydownEvent(key: 'y', ctrlKey: true, target: elementB)
keymapManager.handleKeyboardEvent(event)
assert.equal(events.length, 1)
assert.equal(events[0].type, 'x-command')
assert.equal(event.defaultPrevented, true)
describe "if the matching binding's command is 'unset!' and there is another match", ->
it "immediately matches the other match", ->
elementA.addEventListener 'unset!', (e) -> events.push(e)
keymapManager.add "test",
".a":
"ctrl-y": "x-command"
"ctrl-y ctrl-y": "unset!"
event = buildKeydownEvent(key: 'y', ctrlKey: true, target: elementA)
keymapManager.handleKeyboardEvent(event)
assert.equal(events.length, 1)
assert.equal(events[0].type, 'x-command')
describe "if the matching binding's command is 'abort!'", ->
it "stops searching for a matching binding immediately and emits no command event", ->
elementA.addEventListener 'abort!', (e) -> events.push(e)
keymapManager.add "test",
".a":
"ctrl-y": "y-command"
"ctrl-x": "x-command"
".b":
"ctrl-y": "abort!"
event = buildKeydownEvent(key: 'y', ctrlKey: true, target: elementB)
keymapManager.handleKeyboardEvent(event)
assert.equal(events.length, 0)
assert(event.defaultPrevented)
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'x', ctrlKey: true, target: elementB))
assert.equal(events.length, 1)
describe "when the keystroke matches multiple bindings on the same element", ->
[elementA, elementB, events] = []
beforeEach ->
elementA = appendContent $$ ->
@div class: 'a', ->
@div class: 'b c d'
elementB = elementA.firstChild
events = []
elementA.addEventListener 'command-1', ((e) -> events.push(e)), false
elementA.addEventListener 'command-2', ((e) -> events.push(e)), false
elementA.addEventListener 'command-3', ((e) -> events.push(e)), false
describe "when the bindings have the same priority", ->
describe "when the bindings have selectors with different specificity", ->
beforeEach ->
keymapManager.add "test",
".b.c":
"ctrl-x": "command-1"
".b":
"ctrl-x": "command-2"
it "dispatches the command associated with the most specific binding", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'x', ctrlKey: true, target: elementB))
assert.equal(events.length, 1)
assert.equal(events[0].type, 'command-1')
assert.equal(events[0].target, elementB)
describe "when the bindings have selectors with the same specificity", ->
it "dispatches the command associated with the most recently added binding", ->
keymapManager.add "test",
".b.c":
"ctrl-x": "command-1"
".c.d":
"ctrl-x": "command-2"
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'x', ctrlKey: true, target: elementB))
assert.equal(events.length, 1)
assert.equal(events[0].type, 'command-2')
assert.equal(events[0].target, elementB)
describe "when bindings have different priorities", ->
it "dispatches the command associated with the binding which has the highest priority", ->
keymapManager.add "keybindings-with-super-priority", {".c": {"ctrl-x": "command-1"}}, 2
keymapManager.add "keybindings-with-priority", {".b.c.d": {"ctrl-x": "command-2"}}, 1
keymapManager.add "normal-keybindings", {".b.c.d": {"ctrl-x": "command-3"}}, 0
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'x', ctrlKey: true, target: elementB))
assert.equal(events.length, 1)
assert.equal(events[0].type, 'command-1')
assert.equal(events[0].target, elementB)
describe "when the keystroke partially matches bindings", ->
[workspace, editor, events] = []
beforeEach ->
workspace = appendContent $$ ->
@div class: 'workspace', ->
@div class: 'editor'
editor = workspace.firstChild
keymapManager.add 'test',
'.workspace':
'd o g': 'dog'
'd p': 'dp'
'v i v a': 'viva!'
'v i v': 'viv'
'shift shift-S x': 'shift-then-s'
'.editor': 'v': 'enter-visual-mode'
'.editor.visual-mode': 'i w': 'select-inside-word'
events = []
editor.addEventListener 'textInput', (event) -> events.push("input:#{event.data}")
workspace.addEventListener 'dog', -> events.push('dog')
workspace.addEventListener 'dp', -> events.push('dp')
workspace.addEventListener 'viva!', -> events.push('viva!')
workspace.addEventListener 'viv', -> events.push('viv')
workspace.addEventListener 'select-inside-word', -> events.push('select-inside-word')
workspace.addEventListener 'enter-visual-mode', -> events.push('enter-visual-mode'); editor.classList.add('visual-mode')
describe "when subsequent keystrokes yield an exact match", ->
it "dispatches the command associated with the matched multi-keystroke binding", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'v', target: editor))
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'v', target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'i', target: editor))
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'i', target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'v', target: editor))
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'v', target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'a', target: editor))
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'a', target: editor))
assert.deepEqual(events, ['viva!'])
describe "when subsequent keystrokes yield no matches", ->
it "disables the bindings with the longest keystroke sequences and replays the queued keystrokes", ->
keymapManager.handleKeyboardEvent(vEvent = buildKeydownEvent(key: 'v', target: editor))
keymapManager.handleKeyboardEvent(iEvent = buildKeydownEvent(key: 'i', target: editor))
keymapManager.handleKeyboardEvent(wEvent = buildKeydownEvent(key: 'w', target: editor))
assert.equal(vEvent.defaultPrevented, true)
assert.equal(iEvent.defaultPrevented, true)
assert.equal(wEvent.defaultPrevented, true)
assert.deepEqual(events, ['enter-visual-mode', 'select-inside-word'])
events = []
keymapManager.handleKeyboardEvent(vEvent = buildKeydownEvent(key: 'v', target: editor))
keymapManager.handleKeyboardEvent(iEvent = buildKeydownEvent(key: 'i', target: editor))
keymapManager.handleKeyboardEvent(kEvent = buildKeydownEvent(key: 'k', target: editor))
assert.equal(vEvent.defaultPrevented, true)
assert.equal(iEvent.defaultPrevented, true)
assert.equal(kEvent.defaultPrevented, false)
assert.deepEqual(events, ['enter-visual-mode', 'input:i'])
# FIXME
# expect(clearTimeout).toHaveBeenCalled()
it "dispatches a text-input event for any replayed keyboard events that would have inserted characters", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'd', target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'o', target: editor))
keymapManager.handleKeyboardEvent(lastEvent = buildKeydownEvent(key: 'q', target: editor))
assert.deepEqual(events, ['input:d', 'input:o'])
assert(not lastEvent.defaultPrevented)
events = []
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'Shift', target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'S', target: editor, shiftKey: true))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'y', target: editor))
assert.deepEqual(events, ['input:S'])
describe "when the currently queued keystrokes exactly match at least one binding", ->
it "disables partially-matching bindings and replays the queued keystrokes if the ::partialMatchTimeout expires", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'v', target: editor))
assert.deepEqual(events, [])
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, ['enter-visual-mode'])
events = []
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'v', target: editor))
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'v', target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'i', target: editor))
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'i', target: editor))
assert.deepEqual(events, [])
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, ['enter-visual-mode', 'input:i'])
events = []
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'v', target: editor))
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'v', target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'i', target: editor))
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'i', target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'v', target: editor))
assert.deepEqual(events, [])
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, ['viv'])
it "disables partially-matching bindings and replays the queued keystrokes if the ::partialMatchTimeout expires, even when the exact match contains the control or meta key", ->
keymapManager.add 'test',
'.workspace':
'ctrl-d o g': 'control-dog'
'ctrl-d': 'control-d'
workspace.addEventListener 'control-dog', -> events.push('control-dog')
workspace.addEventListener 'control-d', -> events.push('control-d')
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'd', ctrlKey: true, target: editor))
assert.deepEqual(events, [])
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, ['control-d'])
# Also make sure we canceled the arpeggio.
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'o', target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'g', target: editor))
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, ['control-d'])
# Alright, now let's just double check with meta instead of control.
events = []
keymapManager.add 'test',
'.workspace':
'cmd-d o g': 'meta-dog'
'cmd-d': 'meta-d'
workspace.addEventListener 'meta-dog', -> events.push('meta-dog')
workspace.addEventListener 'meta-d', -> events.push('meta-d')
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'd', metaKey: true, target: editor))
assert.deepEqual(events, [])
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, ['meta-d'])
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'o', target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'g', target: editor))
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, ['meta-d'])
it "does not enter a pending state or prevent the default action if the matching binding's command is 'native!'", ->
keymapManager.add 'test', '.workspace': 'v': 'native!'
event = buildKeydownEvent(key: 'v', target: editor)
keymapManager.handleKeyboardEvent(event)
assert(not event.defaultPrevented)
getFakeClock().next()
assert.equal(keymapManager.queuedKeyboardEvents.length, 0)
describe "when the first queued keystroke corresponds to a character insertion", ->
it "disables partially-matching bindings and replays the queued keystrokes if the ::partialMatchTimeout expires", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'd', target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'o', target: editor))
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, ['input:d', 'input:o'])
describe "when the currently queued keystrokes don't exactly match any bindings", ->
it "never times out of the pending state", ->
keymapManager.add 'test',
'.workspace':
'ctrl-d o g': 'control-dog'
workspace.addEventListener 'control-dog', -> events.push('control-dog')
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'd', ctrlKey: true, target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'o', target: editor))
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, [])
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'g', target: editor))
assert.deepEqual(events, ['control-dog'])
describe "when the partially matching bindings all map to the 'unset!' directive", ->
it "ignores the 'unset!' bindings and invokes the command associated with the matching binding as normal", ->
keymapManager.add 'test-2',
'.workspace':
'v i v a': 'unset!'
'v i v': 'unset!'
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'v', target: editor))
assert.deepEqual(events, ['enter-visual-mode'])
describe "when a subsequent keystroke begins a new match of an already pending binding", ->
it "recognizes the match", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'd', target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'o', target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'd', target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'o', target: editor))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'g', target: editor))
assert.deepEqual(events, ['input:d', 'input:o', 'dog'])
describe "when the binding specifies a keyup handler", ->
[events, elementA] = []
beforeEach ->
elementA = appendContent $$ ->
@div class: 'a'
events = []
elementA.addEventListener 'y-command', (e) -> events.push('y-keydown')
elementA.addEventListener 'y-command-ctrl-up', (e) -> events.push('y-ctrl-keyup')
elementA.addEventListener 'x-command-ctrl-up', (e) -> events.push('x-ctrl-keyup')
elementA.addEventListener 'y-command-y-up-ctrl-up', (e) -> events.push('y-up-ctrl-keyup')
elementA.addEventListener 'abc-secret-code-command', (e) -> events.push('abc-secret-code')
elementA.addEventListener 'z-command-d-e-f', (e) -> events.push('z-keydown-d-e-f')
keymapManager.add "test",
".a":
"ctrl-y": "y-command"
"ctrl-y ^ctrl": "y-command-ctrl-up"
"ctrl-x ^ctrl": "x-command-ctrl-up"
"ctrl-y ^y ^ctrl": "y-command-y-up-ctrl-up"
"a b c ^b ^a ^c": "abc-secret-code-command"
"ctrl-z d e f": "z-command-d-e-f"
it "dispatches the command for a binding containing only keydown events immediately even when there is a corresponding multi-stroke binding that contains only other keyup events", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'y', ctrlKey: true, target: elementA))
assert.deepEqual(events, ['y-keydown'])
it "dispatches the command when a matching keystroke precedes it", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'y', ctrlKey: true, target: elementA))
assert.deepEqual(events, ['y-keydown'])
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'y', ctrlKey: true, metaKey: true, shiftKey: true, altKey: true, target: elementA))
assert.deepEqual(events, ['y-keydown'])
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'Control', target: elementA))
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, ['y-keydown', 'y-up-ctrl-keyup'])
it "dispatches the command when the keyup comes after the partial match timeout", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'y', ctrlKey: true, target: elementA))
assert.deepEqual(events, ['y-keydown'])
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'y', ctrlKey: true, cmd: true, shift: true, alt: true, target: elementA))
assert.deepEqual(events, ['y-keydown'])
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'ctrl', target: elementA))
assert.deepEqual(events, ['y-keydown', 'y-up-ctrl-keyup'])
it "dispatches the command multiple times when multiple keydown events for the binding come in before the binding with a keyup handler", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'y', ctrlKey: true, target: elementA))
assert.deepEqual(events, ['y-keydown'])
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'y', ctrlKey: true, target: elementA))
assert.deepEqual(events, ['y-keydown'])
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'y', ctrlKey: true, target: elementA))
assert.deepEqual(events, ['y-keydown', 'y-keydown'])
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, ['y-keydown', 'y-keydown'])
it "dispatches the command when the modifier is lifted before the character", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'y', ctrlKey: true, target: elementA))
assert.deepEqual(events, ['y-keydown'])
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'Control', target: elementA))
assert.deepEqual(events, ['y-keydown', 'y-ctrl-keyup'])
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, ['y-keydown', 'y-ctrl-keyup'])
it "dispatches the command when extra user-generated keyup events are not specified in the binding", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'x', ctrlKey: true, target: elementA))
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'x', ctrlKey: true, target: elementA)) # not specified in binding
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'Control', target: elementA))
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, ['x-ctrl-keyup'])
it "dispatches the command when extra user-generated keydown events not specified in the binding occur between keydown and keyup", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'y', ctrlKey: true, target: elementA))
assert.deepEqual(events, ['y-keydown'])
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'j', ctrlKey: true, target: elementA)) # not specified in binding
assert.deepEqual(events, ['y-keydown'])
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'Control', target: elementA))
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, ['y-keydown', 'y-ctrl-keyup'])
it "does _not_ dispatch the command when extra user-generated keydown events not specified in the binding occur between keydowns", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent('z', ctrl: true, target: elementA))
keymapManager.handleKeyboardEvent(buildKeyupEvent('ctrl', target: elementA))
keymapManager.handleKeyboardEvent(buildKeyupEvent('ctrl', target: elementA))
keymapManager.handleKeyboardEvent(buildKeydownEvent('z', target: elementA)) # not specified in binding
keymapManager.handleKeyboardEvent(buildKeydownEvent('d', target: elementA))
keymapManager.handleKeyboardEvent(buildKeydownEvent('e', target: elementA))
keymapManager.handleKeyboardEvent(buildKeydownEvent('f', target: elementA))
assert.deepEqual(events, [])
it "dispatches the command when multiple keyup keystrokes are specified", ->
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'a', target: elementA))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'b', target: elementA))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'c', target: elementA))
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'a', target: elementA))
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'b', target: elementA))
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'c', target: elementA))
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, [])
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'a', target: elementA))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'b', target: elementA))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'c', target: elementA))
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'b', target: elementA))
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'a', target: elementA))
keymapManager.handleKeyboardEvent(buildKeyupEvent(key: 'c', target: elementA))
getFakeClock().tick(keymapManager.getPartialMatchTimeout())
assert.deepEqual(events, ['abc-secret-code'])
it "only counts entire keystrokes when checking for partial matches", ->
element = $$ -> @div class: 'a'
keymapManager.add 'test',
'.a':
'ctrl-alt-a': 'command-a'
'ctrl-a': 'command-b'
events = []
element.addEventListener 'command-a', -> events.push('command-a')
element.addEventListener 'command-b', -> events.push('command-b')
# Should *only* match ctrl-a, not ctrl-alt-a (can't just use a textual prefix match)
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'a', ctrlKey: true, target: element))
assert.deepEqual(events, ['command-b'])
it "does not enqueue keydown events consisting only of modifier keys", ->
element = $$ -> @div class: 'a'
keymapManager.add 'test', '.a': 'ctrl-a ctrl-alt-b': 'command'
events = []
element.addEventListener 'command', -> events.push('command')
# Simulate keydown events for the modifier key being pressed on its own
# prior to the key it is modifying.
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'Control', ctrlKey: true, target: element))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'a', ctrlKey: true, target: element))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'Control', ctrlKey: true, target: element))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'Alt', ctrlKey: true, target: element))
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'b', ctrlKey: true, altKey: true, target: element))
assert.deepEqual(events, ['command'])
it "allows solo modifier-keys to be bound", ->
element = $$ -> @div class: 'a'
keymapManager.add 'test', '.a': 'ctrl': 'command'
events = []
element.addEventListener 'command', -> events.push('command')
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'Control', target: element))
assert.deepEqual(events, ['command'])
it "simulates bubbling if the target is detached", ->
elementA = $$ ->
@div class: 'a', ->
@div class: 'b', ->
@div class: 'c'
elementB = elementA.firstChild
elementC = elementB.firstChild
keymapManager.add 'test', '.c': 'x': 'command'
events = []
elementA.addEventListener 'command', -> events.push('a')
elementB.addEventListener 'command', (e) ->
events.push('b1')
e.stopImmediatePropagation()
elementB.addEventListener 'command', (e) ->
events.push('b2')
assert.equal(e.target, elementC)
assert.equal(e.currentTarget, elementB)
elementC.addEventListener 'command', (e) ->
events.push('c')
assert.equal(e.target, elementC)
assert.equal(e.currentTarget, elementC)
keymapManager.handleKeyboardEvent(buildKeydownEvent(key: 'x', target: elementC))
assert.deepEqual(events, ['c', 'b1'])
describe "::build(source, bindings)", ->
it "normalizes keystrokes containing capitalized alphabetic characters", ->
commandA = keymapManager.build 'test', '*': 'ctrl-shift-l': 'a'
commandB = keymapManager.build 'test', '*': 'ctrl-shift-L': 'b'
commandC = keymapManager.build 'test', '*': 'ctrl-L': 'c'
assert.equal(commandA[0].keystrokes, 'ctrl-shift-L')
assert.equal(commandB[0].keystrokes, 'ctrl-shift-L')
assert.equal(commandC[0].keystrokes, 'ctrl-shift-L')
it "normalizes the order of modifier keys based on the Apple interface guidelines", ->
commandA = keymapManager.build 'test', '*': 'alt-cmd-ctrl-shift-l': 'a'
commandB = keymapManager.build 'test', '*': 'shift-ctrl-l': 'b'
commandC = keymapManager.build 'test', '*': 'alt-ctrl-l': 'c'
commandD = keymapManager.build 'test', '*': 'ctrl-alt--': 'd'
assert.equal(commandA[0].keystrokes, 'ctrl-alt-shift-cmd-L')
assert.equal(commandB[0].keystrokes, 'ctrl-shift-L')
assert.equal(commandC[0].keystrokes, 'ctrl-alt-l')
assert.equal(commandD[0].keystrokes, 'ctrl-alt--')
it "rejects bindings with unknown modifier keys and logs a warning to the console", ->
stub(console, 'warn')
commandA = keymapManager.build 'test', '*': 'meta-shift-A': 'a'
assert.equal(console.warn.callCount, 1)
assert.lengthOf(commandA, 0)
it "rejects bindings with an invalid selector and logs a warning to the console", ->
stub(console, 'warn')
assert.lengthOf(keymapManager.build('test', '<>': 'shift-a': 'a'), 0)
assert.equal(console.warn.callCount, 1)
it "ignores bindings with an invalid selector when throwOnInvalidSelector is false", ->
stub(console, 'warn')
assert.lengthOf(keymapManager.build('test', {'<>': 'shift-a': 'a'}, 0, false), 1)
assert.equal(console.warn.callCount, 0)
it "rejects bindings with an empty command and logs a warning to the console", ->
stub(console, 'warn')
assert.lengthOf(keymapManager.build('test', 'body': 'shift-a': ''), 0)
assert.equal(console.warn.callCount, 1)
it "rejects bindings without a command and logs a warning to the console", ->
stub(console, 'warn')
assert.lengthOf(keymapManager.build('test', 'body': 'shift-a': null), 0)
assert.equal(console.warn.callCount, 1)
it "rejects bindings with a non object command", ->
stub(console, 'warn')
assert.lengthOf(keymapManager.build('test', 'body': 'my-sweet-command:that-is-evil'), 0)
assert.equal(console.warn.callCount, 1)
describe "::add(source, bindings)", ->
it "registers the new bindings", ->
keymapManager.add 'foo',
'body':
'ctrl-a': 'x'
'ctrl-b': 'y'
event = buildKeydownEvent(key: 'A', ctrlKey: true, target: document.body)
keymapManager.handleKeyboardEvent(event)
assert.isTrue(event.defaultPrevented)
it "returns a disposable allowing the added bindings to be removed", ->
disposable1 = keymapManager.add 'foo',
'.a':
'ctrl-a': 'x'
'.b':
'ctrl-b': 'y'
disposable2 = keymapManager.add 'bar',
'.c':
'ctrl-c': 'z'
assert.equal(keymapManager.findKeyBindings(command: 'x').length, 1)
assert.equal(keymapManager.findKeyBindings(command: 'y').length, 1)
assert.equal(keymapManager.findKeyBindings(command: 'z').length, 1)
disposable2.dispose()
assert.equal(keymapManager.findKeyBindings(command: 'x').length, 1)
assert.equal(keymapManager.findKeyBindings(command: 'y').length, 1)
assert.equal(keymapManager.findKeyBindings(command: 'z').length, 0)
describe "::keystrokeForKeyboardEvent(event)", ->
describe "when no extra modifiers are pressed", ->
it "returns a string that identifies the unmodified keystroke", ->
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'a'})), 'a')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'A', shiftKey: true})), 'shift-A')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '['})), '[')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '*', shiftKey: true})), '*')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'ArrowLeft'})), 'left')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Backspace'})), 'backspace')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Delete'})), 'delete')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'PageUp'})), 'pageup')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: ' ', code: 'Space'})), 'space')
describe "when a modifier key is combined with a non-modifier key", ->
it "returns a string that identifies the modified keystroke", ->
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'a', altKey: true})), 'alt-a')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '[', metaKey: true})), 'cmd-[')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '[', code: 'BracketLeft', shiftKey: true, metaKey: true})), 'cmd-{')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '*', ctrlKey: true, shiftKey: true})), 'ctrl-*')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '8', code: 'Digit8', ctrlKey: true, shiftKey: true})), 'ctrl-*')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'ArrowLeft', ctrlKey: true, altKey: true, metaKey: true})), 'ctrl-alt-cmd-left')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'A', shiftKey: true})), 'shift-A')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'A', ctrlKey: true, shiftKey: true})), 'ctrl-shift-A')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '{', shiftKey: true})), '{')
describe "when the KeyboardEvent.key is a capital letter due to caps lock, but shift is not pressed", ->
it "converts the letter to lower case", ->
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'A', shiftKey: false})), 'a')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'A', shiftKey: false, altKey: true})), 'alt-a')
describe "when the KeyboardEvent.key is a lower-case letter due to caps lock + shift", ->
it "converts the letter to upper case and honors the shift key", ->
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'a', shiftKey: true})), 'shift-A')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'a', shiftKey: true, altKey: true})), 'alt-shift-A')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'a', shiftKey: true, ctrlKey: true})), 'ctrl-shift-A')
it "doesn't drop the ctrl-alt modifiers when there is no AltGraph variant", ->
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'p', shiftKey: true, altKey: true, ctrlKey: true})), 'ctrl-alt-shift-P')
describe "when the KeyboardEvent.key is 'Delete' but KeyboardEvent.code is 'Backspace' due to pressing ctrl-delete with numlock enabled on Windows", ->
it "translates as ctrl-backspace instead of ctrl-delete", ->
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Delete', code: 'Backspace', ctrlKey: true})), 'ctrl-backspace')
describe "when the KeyboardEvent.key is '' but the KeyboardEvent.code is 'NumpadDecimal' and getModifierState('NumLock') returns false", ->
it "translates as delete to work around a Chrome bug on Linux", ->
mockProcessPlatform('linux')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '.', code: 'NumpadDecimal', modifierState: {NumLock: true}})), '.')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '', code: 'NumpadDecimal', modifierState: {NumLock: false}})), 'delete')
describe "when numlock is on", ->
it "translates numpad digits using KeyboardEvent.code", ->
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '0', code: 'Numpad0', modifierState: {NumLock: true}})), 'numpad0')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '1', code: 'Numpad1', modifierState: {NumLock: true}})), 'numpad1')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '2', code: 'Numpad2', modifierState: {NumLock: true}})), 'numpad2')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '3', code: 'Numpad3', modifierState: {NumLock: true}})), 'numpad3')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '4', code: 'Numpad4', modifierState: {NumLock: true}})), 'numpad4')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '5', code: 'Numpad5', modifierState: {NumLock: true}})), 'numpad5')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '6', code: 'Numpad6', modifierState: {NumLock: true}})), 'numpad6')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '7', code: 'Numpad7', modifierState: {NumLock: true}})), 'numpad7')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '8', code: 'Numpad8', modifierState: {NumLock: true}})), 'numpad8')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '9', code: 'Numpad9', modifierState: {NumLock: true}})), 'numpad9')
describe "when numlock is off", ->
it "doesn't translate numpad digits using KeyboardEvent.code", ->
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Insert', code: 'Numpad0', modifierState: {NumLock: false}})), 'insert')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'End', code: 'Numpad1', modifierState: {NumLock: false}})), 'end')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'ArrowDown', code: 'Numpad2', modifierState: {NumLock: false}})), 'down')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'PageDown', code: 'Numpad3', modifierState: {NumLock: false}})), 'pagedown')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'ArrowLeft', code: 'Numpad4', modifierState: {NumLock: false}})), 'left')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Clear', code: 'Numpad5', modifierState: {NumLock: false}})), 'clear')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'ArrowRight', code: 'Numpad6', modifierState: {NumLock: false}})), 'right')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Home', code: 'Numpad7', modifierState: {NumLock: false}})), 'home')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'ArrowUp', code: 'Numpad8', modifierState: {NumLock: false}})), 'up')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'PageUp', code: 'Numpad9', modifierState: {NumLock: false}})), 'pageup')
describe "when the Dvorak QWERTY-⌘ layout is in use on macOS", ->
it "uses the US layout equivalent when the command key is held down", ->
mockProcessPlatform('darwin')
stub(KeyboardLayout, 'getCurrentKeymap', -> require('./helpers/keymaps/mac-dvorak-qwerty-cmd'))
stub(KeyboardLayout, 'getCurrentKeyboardLayout', -> 'com.apple.keylayout.DVORAK-QWERTYCMD')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'l', code: 'KeyP', altKey: true})), 'alt-l')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'l', code: 'KeyP', ctrlKey: true, altKey: true})), 'ctrl-alt-l')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'l', code: 'KeyP', metaKey: true})), 'cmd-p')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'L', code: 'KeyP', metaKey: true, shiftKey: true})), 'shift-cmd-P')
describe "when a custom Dvorak QWERTY-⌘ layout is in use on macOS", ->
it "uses the US layout equivalent when the command key is held down", ->
mockProcessPlatform('darwin')
stub(KeyboardLayout, 'getCurrentKeymap', -> require('./helpers/keymaps/mac-dvorak-qwerty-cmd'))
stub(KeyboardLayout, 'getCurrentKeyboardLayout', -> 'org.unknown.keylayout.DVORAK-QWERTYCMD')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'l', code: 'KeyP', altKey: true})), 'alt-l')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'l', code: 'KeyP', ctrlKey: true, altKey: true})), 'ctrl-alt-l')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'l', code: 'KeyP', metaKey: true})), 'cmd-p')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'L', code: 'KeyP', metaKey: true, shiftKey: true})), 'shift-cmd-P')
describe "when the current system keymap cannot be obtained on macOS", ->
it "does not throw exceptions and just takes the current key value", ->
mockProcessPlatform('darwin')
stub(KeyboardLayout, 'getCurrentKeymap', -> null)
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '@', code: 'KeyG', modifierState: {AltGraph: true}})), '@')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Dead', code: 'KeyU', modifierState: {AltGraph: true}})), 'alt-dead')
describe "international layouts", ->
currentKeymap = null
beforeEach ->
currentKeymap = null
stub(KeyboardLayout, 'getCurrentKeymap', -> currentKeymap)
it "correctly resolves to AltGraph to ctrl-alt when key is AltGraph on Windows", ->
mockProcessPlatform('win32')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'AltGraph', code: 'AltRight', ctrlKey: true, altKey: true})), 'ctrl-alt')
it "allows ASCII characters (<= 127) to be typed via an option modifier on macOS", ->
mockProcessPlatform('darwin')
currentKeymap = require('./helpers/keymaps/mac-swiss-german')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '@', code: 'KeyG', altKey: true})), '@')
# Does not use alt variant characters outside of basic ASCII range
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '‚', code: 'KeyG', altKey: true, shiftKey: true})), 'alt-shift-G')
# Does not use alt variant character if ctrl modifier is used
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '@', code: 'KeyG', ctrlKey: true, altKey: true})), 'ctrl-alt-g')
it "allows ASCII characters (<= 127) to be typed via the ctrl-alt- modifiers on Windows", ->
mockProcessPlatform('win32')
currentKeymap = require('./helpers/keymaps/windows-swiss-german')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '@', code: 'Digit2', ctrlKey: true, altKey: true})), '@')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '°', code: 'Digit4', ctrlKey: true, altKey: true})), 'ctrl-alt-4')
currentKeymap = require('./helpers/keymaps/windows-us-international')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '¢', code: 'KeyC', ctrlKey: true, altKey: true, shiftKey: true})), 'ctrl-alt-shift-C')
it "allows arbitrary characters to be typed via an altgraph modifier on Linux", ->
mockProcessPlatform('linux')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '@', modifierState: {AltGraph: true}})), '@')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '€', modifierState: {AltGraph: true}})), '€')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Ë', shiftKey: true, modifierState: {AltGraph: true}})), 'shift-Ë')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'g', altKey: true, altGraphKey: false})), 'alt-g')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'e', altKey: true, altGraphKey: false})), 'alt-e')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'E', altKey: true, shiftKey: true, altGraphKey: false})), 'alt-shift-E')
it "falls back to the non-alt key if other modifiers are combined with ALtGraph on Linux", ->
mockProcessPlatform('linux')
currentKeymap = require('./helpers/keymaps/linux-swiss-german')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '@', code: 'KeyG', ctrlKey: true, modifierState: {AltGraph: true}})), 'ctrl-alt-g')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '@', code: 'KeyG', metaKey: true, modifierState: {AltGraph: true}})), 'alt-cmd-g')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '@', code: 'KeyG', altKey: true, modifierState: {AltGraph: true}})), 'alt-g')
it "resolves events with a key value of Unknown and a code of IntlRo to '/' (this occurs on a Brazillian Portuguese keyboard layout on Mint Linux)", ->
mockProcessPlatform('linux')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Unidentified', code: 'IntlRo', ctrlKey: true})), 'ctrl-/')
it "on non-Latin keyboards, converts keystrokes with modifiers to U.S. layout equivalent characters", ->
currentKeymap = require('./helpers/keymaps/mac-greek')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'δ', code: 'KeyD'})), 'd')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Δ', code: 'KeyD', shiftKey: true})), 'shift-D')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '÷', altKey: true, code: 'KeyD'})), 'alt-d')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'δ', code: 'KeyD', metaKey: true})), 'cmd-d')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Δ', code: 'KeyD', metaKey: true, shiftKey: true})), 'shift-cmd-D')
# If *any* key on the keyboard is non-Latin, even characters that *are* Latin remap to the U.S. equivalent character for the physical key
currentKeymap = require('./helpers/keymaps/mac-russian-pc')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '.', code: 'Slash', ctrlKey: true})), 'ctrl-/')
currentKeymap = require('./helpers/keymaps/mac-hebrew')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: ']', code: 'BracketLeft', ctrlKey: true})), 'ctrl-[')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: '[', code: 'BracketRight', ctrlKey: true})), 'ctrl-]')
# Don't use U.S. counterpart for keyboards with all Latin characters
currentKeymap = require('./helpers/keymaps/mac-turkish')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'ö', code: 'KeyX', metaKey: true})), 'cmd-ö')
# Don't blow up if A, S, D, or F don't have entries in the keymap
currentKeymap = {KeyA: null, KeyS: null, KeyD: null, KeyF: null}
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'a', code: 'KeyA', ctrlKey: true})), 'ctrl-a')
it "translates dead keys to their printable equivalents on macOS, but not Windows", ->
mockProcessPlatform('darwin')
currentKeymap = require('./helpers/keymaps/mac-swedish')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Dead', code: 'BracketRight'})), '¨')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Dead', code: 'BracketRight', shiftKey: true})), '^')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Dead', code: 'BracketRight', altKey: true})), '~')
# We can't determine the character for a dead key on Windows without breaking dead key handling
# in some cases because they have a terrible API, so we don't try.
mockProcessPlatform('win32')
currentKeymap = require('./helpers/keymaps/windows-swedish')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Dead', code: 'BracketRight'})), 'dead')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Dead', code: 'BracketRight', shiftKey: true})), 'shift-dead')
assert.equal(keymapManager.keystrokeForKeyboardEvent(buildKeydownEvent({key: 'Dead', code: 'BracketRight', ctrlKey: true, altKey: true, shiftKey: true})), 'ctrl-alt-shift-dead')
describe "when custom keystroke resolvers are installed", ->
it "resolves to the keystroke string of the most recently-installed resolver returning a defined value", ->
mockProcessPlatform('darwin')
currentKeymap = require('./helpers/keymaps/mac-swiss-german')
currentLayoutName = 'com.apple.keylayout.SwissGerman'
stub(KeyboardLayout, 'getCurrentKeymap', -> currentKeymap)
stub(KeyboardLayout, 'getCurrentKeyboardLayout', -> currentLayoutName)
keydownEvent = buildKeydownEvent({key: '@', code: 'KeyG', ctrlKey: true, altKey: true})
disposable1 = keymapManager.addKeystrokeResolver(({keystroke, event, layoutName, keymap}) ->
assert.equal(keystroke, 'ctrl-alt-g')
assert.equal(event, keydownEvent)
assert.equal(layoutName, currentLayoutName)
assert.equal(keymap, currentKeymap)
# simulate the user wishing to honor the alt-modified character in the presence of other modifiers
'ctrl-@'
)
assert.equal(keymapManager.keystrokeForKeyboardEvent(keydownEvent), 'ctrl-@')
# Test that multiple keytsroke resolvers cascade
disposable2 = keymapManager.addKeystrokeResolver(({keystroke}) ->
assert.equal(keystroke, 'ctrl-@')
# Ensure that we normalize the returned custom keystroke resolved
'alt-ctrl-X'
)
expectedKeystroke = 'ctrl-alt-shift-X'
disposable3 = keymapManager.addKeystrokeResolver(({keystroke}) ->
assert.equal(keystroke, expectedKeystroke)
null
)
assert.equal(keymapManager.keystrokeForKeyboardEvent(keydownEvent), expectedKeystroke)
# Test that keystroke resolvers can be disposed
disposable2.dispose()
expectedKeystroke = 'ctrl-@'
assert.equal(keymapManager.keystrokeForKeyboardEvent(keydownEvent), expectedKeystroke)
describe "::findKeyBindings({command, target, keystrokes})", ->
[elementA, elementB] = []
beforeEach ->
elementA = appendContent $$ ->
@div class: 'a', ->
@div class: 'b c'
@div class: 'd'
elementB = elementA.querySelector('.b.c')
keymapManager.add 'test',
'.a':
'ctrl-a': 'x'
'ctrl-b': 'y'
'.b':
'ctrl-c': 'x'
'.b.c':
'ctrl-d': 'x'
'.d':
'ctrl-e': 'x'
describe "when only passed a command", ->
it "returns all bindings that dispatch the given command", ->
keystrokes = keymapManager.findKeyBindings(command: 'x').map((b) -> b.keystrokes).sort()
assert.deepEqual(keystrokes, ['ctrl-a', 'ctrl-c', 'ctrl-d', 'ctrl-e'])
describe "when only passed a target", ->
it "returns all bindings that can be invoked from the given target", ->
keystrokes = keymapManager.findKeyBindings(target: elementB).map((b) -> b.keystrokes)
assert.deepEqual(keystrokes, ['ctrl-d', 'ctrl-c', 'ctrl-b', 'ctrl-a'])
describe "when passed keystrokes", ->
it "returns all bindings that can be invoked with the given keystrokes", ->
keystrokes = keymapManager.findKeyBindings(keystrokes: 'ctrl-a').map((b) -> b.keystrokes)
assert.deepEqual(keystrokes, ['ctrl-a'])
describe "when passed a command and a target", ->
it "returns all bindings that would invoke the given command from the given target element, ordered by specificity", ->
keystrokes = keymapManager.findKeyBindings(command: 'x', target: elementB).map((b) -> b.keystrokes)
assert.deepEqual(keystrokes, ['ctrl-d', 'ctrl-c', 'ctrl-a'])
describe "::loadKeymap(path, options)", ->
@timeout(5000)
beforeEach ->
getFakeClock().uninstall()
describe "if called with a file path", ->
it "loads the keybindings from the file at the given path", ->
keymapManager.loadKeymap(path.join(__dirname, 'fixtures', 'a.cson'))
assert.equal(keymapManager.findKeyBindings(command: 'x').length, 1)
describe "if called with watch: true", ->
[keymapFilePath, subscription] = []
beforeEach ->
keymapFilePath = path.join(temp.mkdirSync('keymap-manager-spec'), "keymapManager.cson")
fs.writeFileSync keymapFilePath, """
'.a': 'ctrl-a': 'x'
"""
keymapManager.loadKeymap(keymapFilePath, watch: true)
subscription = keymapManager.watchSubscriptions[keymapFilePath]
assert.equal(keymapManager.findKeyBindings(command: 'x').length, 1)
describe "when the file is changed", ->
it "reloads the file's key bindings and notifies ::onDidReloadKeymap observers with the keymap path", (done) ->
done = debounce(done, 500)
fs.writeFileSync keymapFilePath, """
'.a': 'ctrl-a': 'y'
'.b': 'ctrl-b': 'z'
"""
keymapManager.onDidReloadKeymap (event) ->
assert.equal(event.path, keymapFilePath)
assert.equal(keymapManager.findKeyBindings(command: 'x').length, 0)
assert.equal(keymapManager.findKeyBindings(command: 'y').length, 1)
assert.equal(keymapManager.findKeyBindings(command: 'z').length, 1)
done()
it "reloads the file's key bindings and notifies ::onDidReloadKeymap observers with the keymap path even if the file is empty", (done) ->
done = debounce(done, 500)
fs.writeFileSync keymapFilePath, ""
keymapManager.onDidReloadKeymap (event) ->
assert.equal(event.path, keymapFilePath)
assert.equal(keymapManager.getKeyBindings().length, 0)
done()
it "reloads the file's key bindings and notifies ::onDidReloadKeymap observers with the keymap path even if the file has only comments", (done) ->
done = debounce(done, 500)
fs.writeFileSync keymapFilePath, """
# '.a': 'ctrl-a': 'y'
# '.b': 'ctrl-b': 'z'
"""
keymapManager.onDidReloadKeymap (event) ->
assert.equal(event.path, keymapFilePath)
assert.equal(keymapManager.getKeyBindings().length, 0)
done()
it "emits an event, logs a warning and does not reload if there is a problem reloading the file", (done) ->
done = debounce(done, 500)
stub(console, 'warn')
fs.writeFileSync keymapFilePath, "junk1."
keymapManager.onDidFailToReadFile ->
assert(console.warn.callCount > 0)
assert.equal(keymapManager.findKeyBindings(command: 'x').length, 1)
done()
describe "when the file is removed", ->
it "removes the bindings and notifies ::onDidUnloadKeymap observers with keymap path", (done) ->
fs.removeSync(keymapFilePath)
keymapManager.onDidUnloadKeymap (event) ->
assert.equal(event.path, keymapFilePath)