-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathKeyExplorer.ts
More file actions
2248 lines (2075 loc) · 65 KB
/
KeyExplorer.ts
File metadata and controls
2248 lines (2075 loc) · 65 KB
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
/*************************************************************
*
* Copyright (c) 2009-2025 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file Explorers based on keyboard events.
*
* @author v.sorge@mathjax.org (Volker Sorge)
*/
import { HoverRegion, SpeechRegion, LiveRegion } from './Region.js';
import { STATE } from '../../core/MathItem.js';
import type { ExplorerMathItem, ExplorerMathDocument } from '../explorer.js';
import { Explorer, AbstractExplorer } from './Explorer.js';
import { ExplorerPool } from './ExplorerPool.js';
import { MmlNode } from '../../core/MmlTree/MmlNode.js';
import { honk, SemAttr } from '../speech/SpeechUtil.js';
import { GeneratorPool } from '../speech/GeneratorPool.js';
import { context } from '../../util/context.js';
import { InfoDialog } from '../../ui/dialog/InfoDialog.js';
/**********************************************************************/
const isWindows = context.os === 'Windows';
const BRAILLE_PADDING = Array(40).fill('\u2800').join('');
/**
* Interface for keyboard explorers. Adds the necessary keyboard events.
*
* @interface
* @augments {Explorer}
*/
export interface KeyExplorer extends Explorer {
/**
* Function to be executed on key down.
*
* @param {KeyboardEvent} event The keyboard event.
*/
KeyDown(event: KeyboardEvent): void;
/**
* Function to be executed on focus in.
*
* @param {KeyboardEvent} event The keyboard event.
*/
FocusIn(event: FocusEvent): void;
/**
* Function to be executed on focus out.
*
* @param {KeyboardEvent} event The keyboard event.
*/
FocusOut(event: FocusEvent): void;
/**
* A method that is executed if no move is executed.
*/
NoMove(): void;
}
/**********************************************************************/
/**
* Type of function that implements a key press action
*/
export type keyMapping = (
explorer: SpeechExplorer,
event: KeyboardEvent
) => boolean | void;
/**
* Selectors for walking.
*/
const nav = '[data-speech-node]';
/**
* Predicate to check if element is a MJX container.
*
* @param {HTMLElement} el The HTML element.
* @returns {boolean} True if the element is an mjx-container.
*/
export function isContainer(el: HTMLElement): boolean {
return el.matches('mjx-container');
}
/**
* Test if an event has any modifier keys
*
* @param {MouseEvent|KeyboardEvent} event The event to check
* @param {boolean} shift True if shift is to be included in check
* @returns {boolean} True if shift, ctrl, alt, or meta key is pressed
*/
export function hasModifiers(
event: MouseEvent | KeyboardEvent,
shift: boolean = true
): boolean {
return (
(event.shiftKey && shift) || event.metaKey || event.altKey || event.ctrlKey
);
}
/**********************************************************************/
/**********************************************************************/
/**
* @class
* @augments {AbstractExplorer}
*
* @template T The type that is consumed by the Region of this explorer.
*/
export class SpeechExplorer
extends AbstractExplorer<string>
implements KeyExplorer
{
/**
* Creates a customized help dialog
*
* @param {string} title The title to use for the message
* @param {string} select Additional ways to select the typeset math
* @param {string} braille Additional Braille information
* @returns {string} The customized message
*/
protected static helpMessage(
title: string,
select: string,
braille: string
): string {
return `
<h2 role="heading" aria-level="2">Exploring expressions ${title}</h2>
<p>The mathematics on this page is being rendered by <a
href="https://www.mathjax.org/" target="_blank">MathJax</a>, which
generates both the text spoken by screen readers, as well as the
visual layout for sighted users.</p>
<p>Expressions typeset by MathJax can be explored interactively, and
are focusable. You can use the <kbd>Tab</kbd> key to move to a typeset
expression${select}. Initially, the expression will be read in full,
but you can use the following keys to explore the expression
further:</p>
<ul>
<li><kbd>Down Arrow</kbd> moves one level deeper into the
expression to allow you to explore the current subexpression term by
term.</li>
<li><kbd>Up Arrow</kbd> moves back up a level within the
expression.</li>
<li><kbd>Right Arrow</kbd> moves to the next term in the
current subexpression.</li>
<li><kbd>Left Arrow</kbd> moves to the next term in the
current subexpression.</li>
<li><kbd>Shift</kbd>+<kbd>Arrow</kbd> moves to a
neighboring cell within a table.</li>
<li><kbd>0-9</kbd>+<kbd>0-9</kbd> jumps to a cell
by its index in the table, where 0 = 10.</li>
<li><kbd>Home</kbd> takes you to the top of the
expression.</li>
<li><kbd>Enter</kbd> or <kbd>Return</kbd> clicks a
link or activates an active subexpression.</li>
<li><kbd>Space</kbd> opens the MathJax contextual menu
where you can view or copy the source format of the expression, or
modify MathJax's settings.</li>
<li><kbd>Escape</kbd> exits the expression
explorer.</li>
<li><kbd>x</kbd> gives a summary of the current
subexpression.</li>
<li><kbd>z</kbd> gives the full text of a collapsed
expression.</li>
<li><kbd>d</kbd> gives the current depth within the
expression.</li>
<li><kbd>s</kbd> starts or stops auto-voicing with
synchronized highlighting.</li>
<li><kbd>v</kbd> marks the current position in the
expression.</li>
<li><kbd>p</kbd> cycles through the marked positions in
the expression.</li>
<li><kbd>u</kbd> clears all marked positions and returns
to the starting position.</li>
<li><kbd>></kbd> cycles through the available speech
rule sets (MathSpeak, ClearSpeak).</li>
<li><kbd><</kbd> cycles through the verbosity levels
for the current rule set.</li>
<li><kbd>b</kbd> toggles whether Braille notation is combined
with speech text for tactile Braille devices, as discussed
below.
<li><kbd>h</kbd> produces this help listing.</li>
</ul>
<p>The MathJax contextual menu allows you to enable or disable speech
or Braille generation for mathematical expressions, the language to
use for the spoken mathematics, and other features of MathJax. In
particular, the Explorer submenu allows you to specify how the
mathematics should be identified in the page (e.g., by saying "math"
when the expression is spoken), and whether or not to include a
message about the letter "h" bringing up this dialog box. Turning off
speech and Braille will disable the expression explorer, its
highlighting, and its help icon.</p>
<p>Support for tactile Braille devices varies across screen readers,
browsers, and operative systems. If you are using a Braille output
device, you may need to select the "Combine with Speech" option in the
contextual menu's Braille submenu in order to obtain Nemeth or Euro
Braille output rather than the speech text on your Braille
device. ${braille}</p>
<p>The contextual menu also provides options for viewing or copying a
MathML version of the expression or its original source format,
creating an SVG version of the expression, and viewing various other
information.</p>
<p>Finally, selecting the "Insert Hidden MathML" item from the options
submenu will turn of MathJax's speech and Braille generation and
instead use visually hidden MathML that some screen readers can voice,
though support for this is not universal across all screen readers and
operating systems. Selecting speech or Braille generation in their
submenus will remove the hidden MathML again.</p>
<p>For more help, see the <a
href="https://docs.mathjax.org/en/latest/basic/accessibility.html"
target="_blank">MathJax accessibility documentation.</a></p>
`;
}
/**
* Help for the different OS versions
*/
protected static helpData: Map<string, [string, string, string]> = new Map([
[
'MacOS',
[
'on MacOS and iOS using VoiceOver',
', or the VoiceOver arrow keys to select an expression',
'',
],
],
[
'Windows',
[
'in Windows using NVDA or JAWS',
`. The screen reader should enter focus or forms mode automatically
when the expression gets the browser focus, but if not, you can toggle
focus mode using NVDA+space in NVDA; for JAWS, Enter should start
forms mode while Numpad Plus leaves it. Also note that you can use
the NVDA or JAWS key plus the arrow keys to explore the expression
even in browse mode, and you can use NVDA+shift+arrow keys to
navigate out of an expression that has the focus in NVDA`,
`NVDA users need to select this option, while JAWS users should be able
to get Braille output without changing this setting.`,
],
],
[
'Unix',
[
'in Unix using Orca',
`, and Orca should enter focus mode automatically. If not, use the
Orca+a key to toggle focus mode on or off. Also note that you can use
Orca+arrow keys to explore expressions even in browse mode`,
'',
],
],
['unknown', ['with a Screen Reader.', '', '']],
]);
/*
* The explorer key mapping
*/
protected static keyMap: Map<string, [keyMapping, boolean?]> = new Map([
['Tab', [(explorer, event) => explorer.tabKey(event)]],
['Escape', [(explorer, event) => explorer.escapeKey(event)]],
['Enter', [(explorer, event) => explorer.enterKey(event)]],
['Home', [(explorer) => explorer.homeKey()]],
[
'ArrowDown',
[(explorer, event) => explorer.moveDown(event.shiftKey), true],
],
['ArrowUp', [(explorer, event) => explorer.moveUp(event.shiftKey), true]],
[
'ArrowLeft',
[(explorer, event) => explorer.moveLeft(event.shiftKey), true],
],
[
'ArrowRight',
[(explorer, event) => explorer.moveRight(event.shiftKey), true],
],
[' ', [(explorer) => explorer.spaceKey()]],
['h', [(explorer) => explorer.hKey()]],
['>', [(explorer) => explorer.nextRules(), false]],
['<', [(explorer) => explorer.nextStyle(), false]],
['x', [(explorer) => explorer.summary(), false]],
['z', [(explorer) => explorer.details(), false]],
['d', [(explorer) => explorer.depth(), false]],
['v', [(explorer) => explorer.addMark(), false]],
['p', [(explorer) => explorer.prevMark(), false]],
['u', [(explorer) => explorer.clearMarks(), false]],
['s', [(explorer) => explorer.autoVoice(), false]],
['b', [(explorer) => explorer.toggleBraille(), false]],
...[...'0123456789'].map((n) => [
n,
[(explorer: SpeechExplorer) => explorer.numberKey(parseInt(n)), false],
]),
] as [string, [keyMapping, boolean?]][]);
/**
* Switches on or off the use of sound on this explorer.
*/
public sound: boolean = false;
/**
* Convenience getter for generator pool of the item.
*
* @returns {GeneratorPool} The item's generator pool.
*/
private get generators(): GeneratorPool<HTMLElement, Text, Document> {
return this.item?.generatorPool;
}
/**
* Shorthand for the item's speech ARIA role
*
* @returns {string} The role
*/
protected get role(): string {
return this.item.ariaRole;
}
/**
* Shorthand for the item's ARIA role description
*
* @returns {string} The role description
*/
protected get description(): string {
return this.item.roleDescription;
}
/**
* Shorthand for the item's "none" indicator
*
* @returns {string} The string to use for no description
*/
protected get none(): string {
return this.document.options.a11y.brailleSpeech
? this.item.brailleNone
: this.item.none;
}
/**
* Shorthand for the item's "brailleNone" indicator
*
* @returns {string} The string to use for no description
*/
protected get brailleNone(): string {
return this.item.brailleNone;
}
/**
* The currently focused element.
*/
protected current: HTMLElement = null;
/**
* The clicked node from a mousedown event
*/
protected clicked: HTMLElement = null;
/**
* Node to focus on when restarted
*/
public refocus: HTMLElement = null;
/**
* True when we are refocusing on the speech node
*/
protected focusSpeech: boolean = false;
/**
* Selector string for re-focusing after re-rendering
*/
public restarted: string = null;
/**
* The transient speech node
*/
protected speech: HTMLElement = null;
/**
* Set to 'd' when depth is showing, 'x' when summary, '' when speech.
*/
protected speechType: string = '';
/**
* The speech node when the top-level node has no role
*/
protected img: HTMLElement = null;
/**
* True when explorer is attached to a node
*/
public attached: boolean = false;
/**
* Treu if events of the explorer are attached.
*/
private eventsAttached: boolean = false;
/**
* The array of saved positions.
*/
protected marks: HTMLElement[] = [];
/**
* The index of the current position in the array.
*/
protected currentMark: number = -1;
/**
* The last explored position from previously exploring this
* expression.
*/
protected lastMark: HTMLElement = null;
/**
* First index of cell to jump to
*/
protected pendingIndex: number[] = [];
/**
* The possible types for a "table" cell
*/
protected cellTypes: string[] = ['cell', 'line'];
/**
* The anchors in this expression
*/
protected anchors: HTMLElement[];
/**
* The elements that are focusable for tab navigation
*/
protected tabs: HTMLElement[];
/**
* Whether the expression was focused by a back tab
*/
protected backTab: boolean = false;
/********************************************************************/
/*
* The event handlers
*/
/**
* @override
*/
protected events: [string, (x: Event) => void][] = super.Events().concat([
['focusin', this.FocusIn.bind(this)],
['focusout', this.FocusOut.bind(this)],
['keydown', this.KeyDown.bind(this)],
['mousedown', this.MouseDown.bind(this)],
['click', this.Click.bind(this)],
['dblclick', this.DblClick.bind(this)],
]);
/**
* Semantic id to subtree map.
*/
private subtrees: Map<string, Set<string>> = null;
/**
* @override
*/
public FocusIn(event: FocusEvent) {
if ((event.target as HTMLElement).closest('mjx-html')) return;
if (this.item.outputData.nofocus) {
//
// we are refocusing after a menu or dialog box has closed
//
this.item.outputData.nofocus = false;
return;
}
if (!this.clicked) {
this.Start();
this.backTab = event.target === this.img;
}
this.clicked = null;
}
/**
* @override
*/
public FocusOut(_event: FocusEvent) {
if (this.current && !this.focusSpeech) {
if (!this.document.options.keepRegions) {
this.setCurrent(null);
this.Stop();
}
if (!document.hasFocus()) {
this.focusTop();
}
}
}
/**
* @override
*/
public KeyDown(event: KeyboardEvent) {
this.pendingIndex.shift();
this.region.cancelVoice();
//
if (hasModifiers(event, false)) return;
//
// Get the key action, if there is one and perform it
//
const CLASS = this.constructor as typeof SpeechExplorer;
const key = event.key.length === 1 ? event.key.toLowerCase() : event.key;
const [action, value] = CLASS.keyMap.get(key) || [];
const result = action
? value === undefined || this.active
? action(this, event)
: value
: this.undefinedKey(event);
//
// If result is true, propagate event,
// Otherwise stop the event, and if false, play the honk sound
//
if (result) return;
this.stopEvent(event);
if (result === false && this.sound) {
this.NoMove();
}
}
/**
* Handle clicks that perform selections, and keep track of clicked node
* so that the focusin event will know something was clicked.
*
* @param {MouseEvent} event The mouse down event
*/
private MouseDown(event: MouseEvent) {
this.pendingIndex = [];
this.region.cancelVoice();
//
if (hasModifiers(event) || event.buttons === 2) {
this.item.outputData.nofocus = true;
return;
}
//
// Get the speech element that was clicked
//
const clicked = this.findClicked(
event.target as HTMLElement,
event.x,
event.y
);
//
// If it is the info icon, top the event and let the click handler process it
//
if (clicked === this.document.infoIcon) {
this.stopEvent(event);
return;
}
//
// Remove any selection ranges and
// If the target is the highlight rectangle, refocus on the clicked element
// otherwise record the click for the focusin handler
//
document.getSelection()?.removeAllRanges();
if ((event.target as HTMLElement).getAttribute('sre-highlighter-added')) {
this.refocus = clicked;
} else {
this.clicked = clicked;
}
}
/**
* Handle a click event
*
* @param {MouseEvent} event The mouse click event
*/
public Click(event: MouseEvent) {
//
// If we are extending a click region, focus out
//
if (
hasModifiers(event) ||
event.buttons === 2 ||
document.getSelection().type === 'Range'
) {
this.FocusOut(null);
return;
}
//
// Get the speech element that was clicked
//
const clicked = this.findClicked(
event.target as HTMLElement,
event.x,
event.y
);
//
// If it was the info icon, open the help dialog
//
if (clicked === this.document.infoIcon) {
this.stopEvent(event);
this.help();
return;
}
//
// If the node contains the clicked element,
// don't propagate the event
// focus on the clicked element when focusin occurs
// start the explorer if this isn't a link
//
if (!this.clicked && (!clicked || this.node.contains(clicked))) {
this.refocus = clicked;
if (!this.triggerLinkMouse()) {
this.Start();
}
}
}
/**
* Handle a double-click event (focus full expression)
*
* @param {MouseEvent} event The mouse click event
*/
public DblClick(event: MouseEvent) {
const direction = (document.getSelection() as any).direction ?? 'none';
if (hasModifiers(event) || event.buttons === 2 || direction !== 'none') {
this.FocusOut(null);
} else {
this.refocus = this.rootNode();
this.Start();
}
}
/********************************************************************/
/*
* The Key action functions
*/
/**
* The space key opens the menu, so it propagates, but we retain the
* current focus to refocus it when the menu closes.
*
* @returns {boolean} Don't cancel the event
*/
protected spaceKey(): boolean {
this.refocus = this.current;
return true;
}
/**
* Open the help dialog, and refocus when it closes.
*
* @returns {boolean | void} True cancels the event
*/
protected hKey(): boolean | void {
if (!this.document.options.enableExplorerHelp) {
return true;
}
this.refocus = this.current;
this.help();
}
/**
* Stop exploring and focus the top element
*
* @param {KeyboardEvent} event The event for the escape key
* @returns {boolean} Don't cancel the event
*/
protected escapeKey(event: KeyboardEvent): void | boolean {
if ((event.target as HTMLElement).closest('mjx-html')) {
this.refocus = (event.target as HTMLElement).closest(nav);
this.Start();
} else {
this.Stop();
this.focusTop();
this.setCurrent(null);
}
return true;
}
/**
* Tab to the next internal link or focusable HTML elelemt, if any,
* and stop the event from propagating, or if no more focusable
* elements, let it propagate so that the browser moves to the next
* focusable item.
*
* @param {KeyboardEvent} event The event for the enter key
* @returns {void | boolean} False means play the honk sound
*/
protected tabKey(event: KeyboardEvent): void | boolean {
//
// Get the currently active element in the expression
//
const active =
this.current ??
(this.node.contains(document.activeElement)
? document.activeElement
: null);
if (this.tabs.length === 0 || !active) return true;
//
// If we back tabbed into the expression, tab to the first focusable item.
//
if (this.backTab) {
if (!event.shiftKey) return true;
this.tabTo(this.tabs[this.tabs.length - 1]);
return;
}
//
// Otherwise, look through the list of focusable items to find the
// next one after (or before) the active item, and tab to it.
//
const [tabs, position, current] = event.shiftKey
? [
this.tabs.slice(0).reverse(),
Node.DOCUMENT_POSITION_PRECEDING,
this.current && this.isLink() ? this.getAnchor() : active,
]
: [this.tabs, Node.DOCUMENT_POSITION_FOLLOWING, active];
for (const tab of tabs) {
if (current.compareDocumentPosition(tab) & position) {
this.tabTo(tab);
return;
}
}
//
// If we are shift-tabbing from the root node, set up to tab out of
// the expression.
//
if (event.shiftKey && this.current === this.rootNode()) {
this.tabOut();
}
//
// Process the tab as normal
//
return true;
}
/**
* @param {HTMLElement} node The node within the expression to receive the focus
*/
protected tabTo(node: HTMLElement) {
if (node.getAttribute('data-mjx-href')) {
this.setCurrent(this.linkFor(node));
} else {
node.focus();
}
}
/**
* Shift-Tab to previous focusable element (by temporarily making
* any focusable elements in the expression have display none, so
* they will be skipped by tabbing).
*/
protected tabOut() {
const html = Array.from(
this.node.querySelectorAll('mjx-html')
) as HTMLElement[];
if (html.length) {
html.forEach((node) => {
node.style.display = 'none';
});
setTimeout(() => {
html.forEach((node) => {
node.style.display = '';
});
}, 0);
}
}
/**
* Process Enter key events
*
* @param {KeyboardEvent} event The event for the enter key
* @returns {void | boolean} False means play the honk sound
*/
protected enterKey(event: KeyboardEvent): void | boolean {
if (this.active) {
if (this.triggerLinkKeyboard(event)) {
this.Stop();
} else {
const expandable = this.actionable(this.current);
if (expandable) {
this.refocus = expandable;
expandable.dispatchEvent(new Event('click'));
return;
}
const tabs = this.getInternalTabs(this.current).filter(
(node) => !node.getAttribute('data-mjx-href')
);
if (tabs.length) {
tabs[0].focus();
return;
}
}
} else {
this.Start();
}
}
/**
* Select top-level of expression
*/
protected homeKey() {
this.setCurrent(this.rootNode());
}
/**
* Move to deeper level in the expression
*
* @param {boolean} shift True if shift is pressed
* @returns {boolean | void} False if no node, void otherwise
*/
protected moveDown(shift: boolean): boolean | void {
return shift
? this.moveToNeighborCell(1, 0)
: this.moveTo(this.firstNode(this.current));
}
/**
* Move to higher level in expression
*
* @param {boolean} shift True if shift is pressed
* @returns {boolean | void} False if no node, void otherwise
*/
protected moveUp(shift: boolean): boolean | void {
return shift
? this.moveToNeighborCell(-1, 0)
: this.moveTo(this.getParent(this.current));
}
/**
* Move to next term in the expression
*
* @param {boolean} shift True if shift is pressed
* @returns {boolean | void} False if no node, void otherwise
*/
protected moveRight(shift: boolean): boolean | void {
return shift
? this.moveToNeighborCell(0, 1)
: this.moveTo(this.nextSibling(this.current));
}
/**
* Move to previous term in the expression
*
* @param {boolean} shift True if shift is pressed
* @returns {boolean | void} False if no node, void otherwise
*/
protected moveLeft(shift: boolean): boolean | void {
return shift
? this.moveToNeighborCell(0, -1)
: this.moveTo(this.prevSibling(this.current));
}
/**
* Move to a specified node, unless it is null
*
* @param {HTMLElement} node The node to move it
* @returns {boolean | void} False if no node, void otherwise
*/
protected moveTo(node: HTMLElement): void | boolean {
if (!node) return false;
this.setCurrent(node);
}
/**
* Move to an adjacent table cell
*
* @param {number} di Change in row number
* @param {number} dj Change in column number
* @returns {boolean | void} False if no such cell, void otherwise
*/
protected moveToNeighborCell(di: number, dj: number): boolean | void {
const cell = this.tableCell(this.current);
if (!cell) return false;
const [i, j] = this.cellPosition(cell);
if (i == null) return false;
const move = this.cellAt(this.cellTable(cell), i + di, j + dj);
if (!move) return false;
this.setCurrent(move);
}
/**
* Determine if an event that is not otherwise mapped should be
* allowed to propagate.
*
* @param {KeyboardEvent} event The event to check
* @returns {boolean} True if not active or the event has a modifier
*/
protected undefinedKey(event: KeyboardEvent): boolean {
return !this.active || hasModifiers(event);
}
/**
* Mark a location so we can return to it later
*/
protected addMark() {
if (this.current === this.marks[this.marks.length - 1]) {
this.setCurrent(this.current);
} else {
this.currentMark = this.marks.length - 1;
this.marks.push(this.current);
this.speak('Position marked');
}
}
/**
* Return to a previous location (loop through them).
* If no saved marks, go to the last previous position,
* or if not, the top level.
*/
protected prevMark() {
if (this.currentMark < 0) {
if (this.marks.length === 0) {
this.setCurrent(this.lastMark || this.rootNode());
return;
}
this.currentMark = this.marks.length - 1;
}
const current = this.currentMark;
this.setCurrent(this.marks[current]);
this.currentMark = current - 1;
}
/**
* Clear all saved positions and return to the last explored position.
*/
protected clearMarks() {
this.marks = [];
this.currentMark = -1;
this.prevMark();
}
/**
* Toggle auto voicing.
*/
protected autoVoice() {
const value = !this.document.options.a11y.voicing;
if (this.document.menu) {
this.document.menu.menu.pool.lookup('voicing').setValue(value);
} else {
this.document.options.a11y.voicing = value;
}
this.Update();
}
protected toggleBraille() {
const value = !this.document.options.a11y.brailleCombine;
if (this.document.menu) {
this.document.menu.menu.pool.lookup('brailleCombine').setValue(value);
} else {
this.document.options.a11y.brailleCombine = value;
}
}
/**
* Get index for cell to jump to.
*
* @param {number} n The number key that was pressed
* @returns {boolean|void} False if not in a table or no such cell to jump to.
*/
protected numberKey(n: number): boolean | void {
if (!this.tableCell(this.current)) return false;