This repository was archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathhterm_scrollport.js
2217 lines (1919 loc) · 66.1 KB
/
hterm_scrollport.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
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) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* The RowProvider should return rows rooted by the custom tag name 'x-row'.
* This ensures that we can quickly assign the correct display height
* to the rows with css.
*
* @interface
*/
hterm.RowProvider = function() {};
/**
* @abstract
* @return {number} The current number of rows.
*/
hterm.RowProvider.prototype.getRowCount = function() {};
/**
* Get specified row.
*
* @abstract
* @param {number} index The index of the row.
* @return {!Element}
*/
hterm.RowProvider.prototype.getRowNode = function(index) {};
/**
* A 'viewport' view of fixed-height rows with support for selection and
* copy-to-clipboard.
*
* 'Viewport' in this case means that only the visible rows are in the DOM.
* If the rowProvider has 100,000 rows, but the ScrollPort is only 25 rows
* tall, then only 25 dom nodes are created. The ScrollPort will ask the
* RowProvider to create new visible rows on demand as they are scrolled in
* to the visible area.
*
* This viewport is designed so that select and copy-to-clipboard still works,
* even when all or part of the selection is scrolled off screen.
*
* Note that the X11 mouse clipboard does not work properly when all or part
* of the selection is off screen. It would be difficult to fix this without
* adding significant overhead to pathologically large selection cases.
*
* @param {!hterm.RowProvider} rowProvider An object capable of providing rows
* as raw text or row nodes.
* @constructor
* @extends {hterm.PubSub}
*/
hterm.ScrollPort = function(rowProvider) {
hterm.PubSub.addBehavior(this);
this.rowProvider_ = rowProvider;
// SWAG the character size until we can measure it.
this.characterSize = new hterm.Size(10, 10);
// DOM node used for character measurement.
this.ruler_ = null;
this.selection = new hterm.ScrollPort.Selection(this);
// A map of rowIndex => rowNode for each row that is drawn as part of a
// pending redraw_() call. Null if there is no pending redraw_ call.
this.currentRowNodeCache_ = null;
// A map of rowIndex => rowNode for each row that was drawn as part of the
// previous redraw_() call.
this.previousRowNodeCache_ = {};
// Used during scroll events to detect when the underlying cause is a resize.
this.lastScreenWidth_ = 0;
this.lastScreenHeight_ = 0;
// True if the user should be allowed to select text in the terminal.
// This is disabled when the host requests mouse drag events so that we don't
// end up with two notions of selection.
this.selectionEnabled_ = true;
// The last row count returned by the row provider, re-populated during
// syncScrollHeight().
this.lastRowCount_ = 0;
// The scroll wheel pixel delta multiplier to increase/decrease
// the scroll speed of mouse wheel events. See: https://goo.gl/sXelnq
this.scrollWheelMultiplier_ = 1;
// The last touch events we saw to support touch based scrolling. Indexed
// by touch identifier since we can have more than one touch active.
this.lastTouch_ = {};
/**
* Size of screen padding in pixels.
*/
this.screenPaddingSize = 0;
/**
* True if the last scroll caused the scrollport to show the final row.
*/
this.isScrolledEnd = true;
/**
* A guess at the current scrollbar width, fixed in resize().
*/
this.currentScrollbarWidthPx = hterm.ScrollPort.DEFAULT_SCROLLBAR_WIDTH;
/**
* Whether the ctrl-v key on the screen should paste.
*/
this.ctrlVPaste = false;
/**
* Whether to paste on dropped text.
*/
this.pasteOnDrop = true;
this.div_ = null;
this.document_ = null;
/** @type {?Element} */
this.screen_ = null;
// Collection of active timeout handles.
this.timeouts_ = {};
this.observers_ = {};
// Offscreen selection rows that are set with 'aria-hidden'.
// They must be unset when selection changes or the rows are visible.
this.ariaHiddenSelectionRows_ = [];
this.DEBUG_ = false;
};
/**
* Default width for scrollbar used when the system such as CrOS pretends that
* scrollbar is zero width. CrOS currently uses 11px when expanded.
*
* @const {number}
*/
hterm.ScrollPort.DEFAULT_SCROLLBAR_WIDTH = 12;
/**
* Proxy for the native selection object which understands how to walk up the
* DOM to find the containing row node and sort out which comes first.
*
* @param {!hterm.ScrollPort} scrollPort The parent hterm.ScrollPort instance.
* @constructor
*/
hterm.ScrollPort.Selection = function(scrollPort) {
this.scrollPort_ = scrollPort;
/**
* The row containing the start of the selection.
*
* This may be partially or fully selected. It may be the selection anchor
* or the focus, but its rowIndex is guaranteed to be less-than-or-equal-to
* that of the endRow.
*
* If only one row is selected then startRow == endRow. If there is no
* selection or the selection is collapsed then startRow == null.
*
* @type {?Node}
*/
this.startRow = null;
/**
* Node where selection starts.
*
* @type {?Node}
*/
this.startNode = null;
/**
* Character offset in startNode where selection starts.
*
* @type {number}
*/
this.startOffset = 0;
/**
* The row containing the end of the selection.
*
* This may be partially or fully selected. It may be the selection anchor
* or the focus, but its rowIndex is guaranteed to be greater-than-or-equal-to
* that of the startRow.
*
* If only one row is selected then startRow == endRow. If there is no
* selection or the selection is collapsed then startRow == null.
*
* @type {?Node}
*/
this.endRow = null;
/**
* Node where selection ends.
*
* @type {?Node}
*/
this.endNode = null;
/**
* Character offset in endNode where selection ends.
*
* @type {number}
*/
this.endOffset = 0;
/**
* True if startRow != endRow.
*
* @type {boolean}
*/
this.isMultiline = false;
/**
* True if the selection is just a point (empty) rather than a range.
*
* @type {boolean}
*/
this.isCollapsed = true;
/**
* @private
* @const
*/
this.autoScrollOnMouseMoveBound_ =
/** @type {!EventListener} */ (this.autoScrollOnMouseMove_.bind(this));
/**
* True when 'mousedown' event is received for primary button until 'mouseup'
* is received for primary button.
*
* @private {boolean}
*/
this.autoScrollEnabled_ = false;
/**
* Direction of auto scroll. 1 for scrolling down, -1 for scrolling up. Set by
* detecting mouse position from 'mousemove' events.
*
* @private {number}
*/
this.autoScrollDirection_ = 1;
/**
* ID of interval running this.autoScroll_(). Set by startAutoScroll_(),
* cleared by stopAutoScroll_().
*
* @private {?number}
*/
this.autoScrollInterval_ = null;
/**
* Number of rows to scroll at a time. Auto scroll runs at a 200ms interval.
* It starts by scrolling 1 row and accelerates by 20% each invocation.
*
* @private {number}
*/
this.autoScrollDelta_ = 1;
};
/**
* Given a list of DOM nodes and a container, return the DOM node that
* is first according to a depth-first search.
*
* @param {!Node} parent
* @param {!Array<!Node>} childAry
* @return {?Node} Returns null if none of the children are found.
*/
hterm.ScrollPort.Selection.prototype.findFirstChild = function(
parent, childAry) {
let node = parent.firstChild;
while (node) {
if (childAry.indexOf(node) != -1) {
return node;
}
if (node.childNodes.length) {
const rv = this.findFirstChild(node, childAry);
if (rv) {
return rv;
}
}
node = node.nextSibling;
}
return null;
};
/**
* Capture mousemove events while auto scroll is enabled. Set scroll direction
* up if mouse is above midpoint of screen, else set direction down. Start and
* stop auto scroll when mouse moves above or below rows.
*
* @param {!MouseEvent} e
* @private
*/
hterm.ScrollPort.Selection.prototype.autoScrollOnMouseMove_ = function(e) {
// If mouse is in top half of screen, then direction is up, else down.
const screenHeight = this.scrollPort_.lastScreenHeight_;
this.autoScrollDirection_ = (e.pageY * 2) < screenHeight ? -1 : 1;
const padding = this.scrollPort_.screenPaddingSize;
if (e.pageY < padding) {
// Mouse above rows.
this.startAutoScroll_();
} else if (e.pageY < (this.scrollPort_.visibleRowsHeight + padding)) {
// Mouse inside rows.
this.stopAutoScroll_();
} else {
// Mouse below rows.
this.startAutoScroll_();
}
};
/**
* Enable auto scrolling. True while primary mouse button is down.
*
* @param {boolean} enabled
*/
hterm.ScrollPort.Selection.prototype.setAutoScrollEnabled = function(enabled) {
this.autoScrollEnabled_ = enabled;
const doc = this.scrollPort_.getDocument();
if (enabled) {
doc.addEventListener('mousemove', this.autoScrollOnMouseMoveBound_);
} else {
doc.removeEventListener('mousemove', this.autoScrollOnMouseMoveBound_);
this.stopAutoScroll_();
}
};
/**
* Increase this.autoScrollDelta_ by 20% and scroll.
*
* @private
*/
hterm.ScrollPort.Selection.prototype.autoScroll_ = function() {
this.autoScrollDelta_ *= 1.2;
const delta = Math.floor(this.autoScrollDelta_) * this.autoScrollDirection_;
this.scrollPort_.scrollRowToTop(this.scrollPort_.getTopRowIndex() + delta);
};
/**
* Start auto scrolling if primary mouse is down and it is above or below rows.
*
* @private
*/
hterm.ScrollPort.Selection.prototype.startAutoScroll_ = function() {
if (this.autoScrollEnabled_ && this.autoScrollInterval_ === null) {
this.autoScrollInterval_ = setInterval(this.autoScroll_.bind(this), 200);
}
};
/**
* Stop auto scrolling called on 'mouseup' or if mouse moves back into rows.
*
* @private
*/
hterm.ScrollPort.Selection.prototype.stopAutoScroll_ = function() {
clearInterval(this.autoScrollInterval_);
this.autoScrollInterval_ = null;
this.autoScrollDelta_ = 1;
};
/**
* Synchronize this object with the current DOM selection.
*
* This is a one-way synchronization, the DOM selection is copied to this
* object, not the other way around.
*/
hterm.ScrollPort.Selection.prototype.sync = function() {
// The dom selection object has no way to tell which nodes come first in
// the document, so we have to figure that out.
//
// This function is used when we detect that the "anchor" node is first.
const anchorFirst = () => {
this.startRow = anchorRow;
this.startNode = selection.anchorNode;
this.startOffset = selection.anchorOffset;
this.endRow = focusRow;
this.endNode = focusNode;
this.endOffset = focusOffset;
};
// This function is used when we detect that the "focus" node is first.
const focusFirst = () => {
this.startRow = focusRow;
this.startNode = focusNode;
this.startOffset = focusOffset;
this.endRow = anchorRow;
this.endNode = selection.anchorNode;
this.endOffset = selection.anchorOffset;
};
const selection = this.scrollPort_.getDocument().getSelection();
const clear = () => {
this.startRow = null;
this.startNode = null;
this.startOffset = 0;
this.endRow = null;
this.endNode = null;
this.endOffset = 0;
this.isMultiline = false;
this.isCollapsed = true;
};
if (!selection) {
clear();
return;
}
// Do not ignore collapsed selections. They must not be cleared.
// Screen readers will set them as they navigate through the DOM.
// Auto scroll can also create them as the selection inverts if you scroll
// one way and then reverse direction.
this.isCollapsed = !selection || selection.isCollapsed;
let anchorRow = selection.anchorNode;
while (anchorRow && anchorRow.nodeName != 'X-ROW') {
anchorRow = anchorRow.parentNode;
}
if (!anchorRow) {
// Don't set a selection if it's not a row node that's selected.
clear();
return;
}
let focusRow = selection.focusNode;
let focusNode = focusRow;
let focusOffset = selection.focusOffset;
const focusIsStartOfTopRow = () => {
focusRow = this.scrollPort_.topFold_.nextSibling;
focusNode = focusRow;
focusOffset = 0;
};
const focusIsEndOfBottomRow = () => {
focusRow = this.scrollPort_.bottomFold_.previousSibling;
focusNode = focusRow;
while (focusNode.lastChild) {
focusNode = focusNode.lastChild;
}
focusOffset = focusNode.length || 0;
};
// If focus is topFold or bottomFold, use adjacent row.
if (focusRow === this.scrollPort_.topFold_) {
focusIsStartOfTopRow();
} else if (focusRow === this.scrollPort_.bottomFold_) {
focusIsEndOfBottomRow();
}
while (focusRow && focusRow.nodeName != 'X-ROW') {
focusRow = focusRow.parentNode;
}
if (!focusRow) {
// Keep existing selection (do not clear()) if focus is not a valid row.
return;
}
// During auto scroll, if focusRow is one of the selection rows inside the
// fold, use adjacent row.
if (this.scrollPort_.autoScrollEnabled_) {
let node = this.scrollPort_.topFold_;
while ((node = node.previousSibling) !== null) {
if (node === focusRow) {
focusIsStartOfTopRow();
}
}
node = this.scrollPort_.bottomFold_;
while ((node = node.nextSibling) !== null) {
if (node === focusRow) {
focusIsEndOfBottomRow();
}
}
}
if (anchorRow.rowIndex < focusRow.rowIndex) {
anchorFirst();
} else if (anchorRow.rowIndex > focusRow.rowIndex) {
focusFirst();
} else if (focusNode == selection.anchorNode) {
if (selection.anchorOffset < focusOffset) {
anchorFirst();
} else {
focusFirst();
}
} else {
// The selection starts and ends in the same row, but isn't contained all
// in a single node.
const firstNode = this.findFirstChild(
anchorRow, [selection.anchorNode, focusNode]);
if (!firstNode) {
throw new Error('Unexpected error syncing selection.');
}
if (firstNode == selection.anchorNode) {
anchorFirst();
} else {
focusFirst();
}
}
this.isMultiline = anchorRow.rowIndex != focusRow.rowIndex;
};
/**
* Turn a div into this hterm.ScrollPort.
*
* @param {!Element} div
* @param {function()=} callback
*/
hterm.ScrollPort.prototype.decorate = function(div, callback) {
this.div_ = div;
this.iframe_ = div.ownerDocument.createElement('iframe');
this.iframe_.style.cssText = (
'border: 0;' +
'height: 100%;' +
'position: absolute;' +
'width: 100%');
div.appendChild(this.iframe_);
const onLoad = () => {
this.paintIframeContents_();
if (callback) {
callback();
}
};
// Insert Iframe content asynchronously in FF. Otherwise when the frame's
// load event fires in FF it clears out the content of the iframe.
if ('mozInnerScreenX' in window) { // detect a FF only property
this.iframe_.addEventListener('load', () => onLoad());
} else {
onLoad();
}
};
/**
* Initialises the content of this.iframe_. This needs to be done asynchronously
* in FF after the Iframe's load event has fired.
*
* @private
*/
hterm.ScrollPort.prototype.paintIframeContents_ = function() {
this.iframe_.contentWindow.addEventListener('resize',
this.onResize_.bind(this));
const doc = this.document_ = this.iframe_.contentDocument;
doc.body.style.cssText = (
'margin: 0px;' +
'padding: 0px;' +
'height: 100%;' +
'width: 100%;' +
'overflow: hidden;' +
'cursor: var(--hterm-mouse-cursor-style);' +
'user-select: none;');
const metaCharset = doc.createElement('meta');
metaCharset.setAttribute('charset', 'utf-8');
doc.head.appendChild(metaCharset);
if (this.DEBUG_) {
// When we're debugging we add padding to the body so that the offscreen
// elements are visible.
this.document_.body.style.paddingTop =
this.document_.body.style.paddingBottom =
'calc(var(--hterm-charsize-height) * 3)';
}
const style = doc.createElement('style');
style.textContent = (
'x-row {' +
' display: block;' +
' height: var(--hterm-charsize-height);' +
' line-height: var(--hterm-charsize-height);' +
'}');
doc.head.appendChild(style);
this.userCssLink_ = doc.createElement('link');
this.userCssLink_.setAttribute('rel', 'stylesheet');
this.userCssText_ = doc.createElement('style');
doc.head.appendChild(this.userCssText_);
// TODO(rginda): Sorry, this 'screen_' isn't the same thing as hterm.Screen
// from screen.js. I need to pick a better name for one of them to avoid
// the collision.
// We make this field editable even though we don't actually allow anything
// to be edited here so that Chrome will do the right thing with virtual
// keyboards and IMEs. But make sure we turn off all the input helper logic
// that doesn't make sense here, and might inadvertently mung or save input.
// Some of these attributes are standard while others are browser specific,
// but should be safely ignored by other browsers.
this.screen_ = doc.createElement('x-screen');
this.screen_.setAttribute('contenteditable', 'true');
this.screen_.setAttribute('spellcheck', 'false');
this.screen_.setAttribute('autocomplete', 'off');
this.screen_.setAttribute('autocorrect', 'off');
this.screen_.setAttribute('autocapitalize', 'none');
// In some ways the terminal behaves like a text box but not in all ways. It
// is not editable in the same ways a text box is editable and the content we
// want to be read out by a screen reader does not always align with the edits
// (selection changes) that happen in the terminal window. Use the log role so
// that the screen reader doesn't treat it like a text box and announce all
// selection changes. The announcements that we want spoken are generated
// by a separate live region, which gives more control over what will be
// spoken.
this.screen_.setAttribute('role', 'log');
this.screen_.setAttribute('aria-live', 'off');
this.screen_.setAttribute('aria-roledescription', 'Terminal');
// Set aria-readonly to indicate to the screen reader that the text on the
// screen is not modifiable by the html cursor. It may be modifiable by
// sending input to the application running in the terminal, but this is
// orthogonal to the DOM's notion of modifiable.
this.screen_.setAttribute('aria-readonly', 'true');
this.screen_.setAttribute('tabindex', '-1');
this.screen_.style.cssText = `
background-color: rgb(var(--hterm-background-color));
caret-color: transparent;
color: rgb(var(--hterm-foreground-color));
display: block;
font-family: monospace;
font-size: 15px;
font-variant-ligatures: none;
height: 100%;
overflow-y: scroll; overflow-x: hidden;
white-space: pre;
width: 100%;
outline: none !important;
`;
/**
* @param {function(...)} f
* @return {!EventListener}
*/
const el = (f) => /** @type {!EventListener} */ (f);
this.screen_.addEventListener('scroll', el(this.onScroll_.bind(this)));
this.screen_.addEventListener('wheel', el(this.onScrollWheel_.bind(this)));
this.screen_.addEventListener('touchstart', el(this.onTouch_.bind(this)));
this.screen_.addEventListener('touchmove', el(this.onTouch_.bind(this)));
this.screen_.addEventListener('touchend', el(this.onTouch_.bind(this)));
this.screen_.addEventListener('touchcancel', el(this.onTouch_.bind(this)));
this.screen_.addEventListener('copy', el(this.onCopy_.bind(this)));
this.screen_.addEventListener('paste', el(this.onPaste_.bind(this)));
this.screen_.addEventListener('drop', el(this.onDragAndDrop_.bind(this)));
doc.body.addEventListener('keydown', this.onBodyKeyDown_.bind(this));
// Add buttons to make accessible scrolling through terminal history work
// well. These are positioned off-screen until they are selected, at which
// point they are moved on-screen.
const a11yButtonHeight = 30;
const a11yButtonBorder = 1;
const a11yButtonTotalHeight = a11yButtonHeight + 2 * a11yButtonBorder;
const a11yButtonStyle = `
border-style: solid;
border-width: ${a11yButtonBorder}px;
color: rgb(var(--hterm-foreground-color));
cursor: pointer;
font-family: monospace;
font-weight: bold;
height: ${a11yButtonHeight}px;
line-height: ${a11yButtonHeight}px;
padding: 0 8px;
position: fixed;
right: var(--hterm-screen-padding-size);
text-align: center;
z-index: 1;
`;
// Note: we use a <div> rather than a <button> because we don't want it to be
// focusable. If it's focusable this interferes with the contenteditable
// focus.
this.scrollUpButton_ = this.document_.createElement('div');
this.scrollUpButton_.id = 'hterm:a11y:page-up';
this.scrollUpButton_.innerText = hterm.msg('BUTTON_PAGE_UP', [], 'Page up');
this.scrollUpButton_.setAttribute('role', 'button');
this.scrollUpButton_.style.cssText = a11yButtonStyle;
this.scrollUpButton_.style.top = `${-a11yButtonTotalHeight}px`;
this.scrollUpButton_.addEventListener('click', this.scrollPageUp.bind(this));
this.scrollDownButton_ = this.document_.createElement('div');
this.scrollDownButton_.id = 'hterm:a11y:page-down';
this.scrollDownButton_.innerText =
hterm.msg('BUTTON_PAGE_DOWN', [], 'Page down');
this.scrollDownButton_.setAttribute('role', 'button');
this.scrollDownButton_.style.cssText = a11yButtonStyle;
this.scrollDownButton_.style.bottom = `${-a11yButtonTotalHeight}px`;
this.scrollDownButton_.addEventListener(
'click', this.scrollPageDown.bind(this));
this.optionsButton_ = this.document_.createElement('div');
this.optionsButton_.id = 'hterm:a11y:options';
this.optionsButton_.innerText =
hterm.msg('OPTIONS_BUTTON_LABEL', [], 'Options');
this.optionsButton_.setAttribute('role', 'button');
this.optionsButton_.style.cssText = a11yButtonStyle;
this.optionsButton_.style.bottom = `${-2 * a11yButtonTotalHeight}px`;
this.optionsButton_.addEventListener(
'click', this.publish.bind(this, 'options'));
doc.body.appendChild(this.scrollUpButton_);
doc.body.appendChild(this.screen_);
doc.body.appendChild(this.scrollDownButton_);
doc.body.appendChild(this.optionsButton_);
// We only allow the scroll buttons to display after a delay, otherwise the
// page up button can flash onto the screen during the intial change in focus.
// This seems to be because it is the first element inside the <x-screen>
// element, which will get focussed on page load.
this.allowA11yButtonsToDisplay_ = false;
setTimeout(() => { this.allowA11yButtonsToDisplay_ = true; }, 500);
this.document_.addEventListener('selectionchange', () => {
this.selection.sync();
if (!this.allowA11yButtonsToDisplay_) {
return;
}
const accessibilityEnabled = this.accessibilityReader_ &&
this.accessibilityReader_.accessibilityEnabled;
const selection = this.document_.getSelection();
let selectedElement;
if (selection.anchorNode && selection.anchorNode.parentElement) {
selectedElement = selection.anchorNode.parentElement;
}
if (accessibilityEnabled && selectedElement == this.scrollUpButton_) {
this.scrollUpButton_.style.top = `${this.screenPaddingSize}px`;
} else {
this.scrollUpButton_.style.top = `${-a11yButtonTotalHeight}px`;
}
if (accessibilityEnabled && selectedElement == this.scrollDownButton_) {
this.scrollDownButton_.style.bottom = `${this.screenPaddingSize}px`;
} else {
this.scrollDownButton_.style.bottom = `${-a11yButtonTotalHeight}px`;
}
if (accessibilityEnabled && selectedElement == this.optionsButton_) {
this.optionsButton_.style.bottom = `${this.screenPaddingSize}px`;
} else {
this.optionsButton_.style.bottom = `${-2 * a11yButtonTotalHeight}px`;
}
});
// This is the main container for the fixed rows.
this.rowNodes_ = doc.createElement('div');
this.rowNodes_.id = 'hterm:row-nodes';
this.rowNodes_.style.cssText = (
'display: block;' +
'position: fixed;' +
'overflow: hidden;' +
'user-select: text;');
this.screen_.appendChild(this.rowNodes_);
// Two nodes to hold offscreen text during the copy event.
this.topSelectBag_ = doc.createElement('x-select-bag');
this.topSelectBag_.style.cssText = (
'display: block;' +
'overflow: hidden;' +
'height: var(--hterm-charsize-height);' +
'white-space: pre;');
this.bottomSelectBag_ = this.topSelectBag_.cloneNode();
// Nodes above the top fold and below the bottom fold are hidden. They are
// only used to hold rows that are part of the selection but are currently
// scrolled off the top or bottom of the visible range.
this.topFold_ = doc.createElement('x-fold');
this.topFold_.id = 'hterm:top-fold-for-row-selection';
this.topFold_.style.cssText = `
display: block;
height: var(--hterm-screen-padding-size);
`;
this.rowNodes_.appendChild(this.topFold_);
this.bottomFold_ = this.topFold_.cloneNode();
this.bottomFold_.id = 'hterm:bottom-fold-for-row-selection';
this.rowNodes_.appendChild(this.bottomFold_);
// This hidden div accounts for the vertical space that would be consumed by
// all the rows in the buffer if they were visible. It's what causes the
// scrollbar to appear on the 'x-screen', and it moves within the screen when
// the scrollbar is moved.
//
// It is set 'visibility: hidden' to keep the browser from trying to include
// it in the selection when a user 'drag selects' upwards (drag the mouse to
// select and scroll at the same time). Without this, the selection gets
// out of whack.
this.scrollArea_ = doc.createElement('div');
this.scrollArea_.id = 'hterm:scrollarea';
this.scrollArea_.style.cssText = 'visibility: hidden';
this.screen_.appendChild(this.scrollArea_);
// We send focus to this element just before a paste happens, so we can
// capture the pasted text and forward it on to someone who cares.
this.pasteTarget_ = doc.createElement('textarea');
this.pasteTarget_.id = 'hterm:ctrl-v-paste-target';
this.pasteTarget_.setAttribute('tabindex', '-1');
this.pasteTarget_.setAttribute('aria-hidden', 'true');
this.pasteTarget_.style.cssText = (
'position: absolute;' +
'height: 1px;' +
'width: 1px;' +
'left: 0px; ' +
'bottom: 0px;' +
'opacity: 0');
this.pasteTarget_.contentEditable = true;
this.screen_.appendChild(this.pasteTarget_);
this.pasteTarget_.addEventListener(
'textInput', this.handlePasteTargetTextInput_.bind(this));
this.resize();
};
/**
* Set the AccessibilityReader object to use to announce page scroll updates.
*
* @param {!hterm.AccessibilityReader} accessibilityReader for announcing page
* scroll updates.
*/
hterm.ScrollPort.prototype.setAccessibilityReader =
function(accessibilityReader) {
this.accessibilityReader_ = accessibilityReader;
};
/**
* Scroll the terminal one page up (minus one line) relative to the current
* position.
*/
hterm.ScrollPort.prototype.scrollPageUp = function() {
if (this.getTopRowIndex() == 0) {
return;
}
const i = this.getTopRowIndex();
this.scrollRowToTop(i - this.visibleRowCount + 1);
this.assertiveAnnounce_();
};
/**
* Scroll the terminal one page down (minus one line) relative to the current
* position.
*/
hterm.ScrollPort.prototype.scrollPageDown = function() {
if (this.isScrolledEnd) {
return;
}
const i = this.getTopRowIndex();
this.scrollRowToTop(i + this.visibleRowCount - 1);
this.assertiveAnnounce_();
};
/**
* Select the font-family and font-smoothing for this scrollport.
*
* @param {string} fontFamily Value of the CSS 'font-family' to use for this
* scrollport. Should be a monospace font.
* @param {string=} smoothing Optional value for '-webkit-font-smoothing'.
* Defaults to an empty string if not specified.
*/
hterm.ScrollPort.prototype.setFontFamily = function(
fontFamily, smoothing = '') {
this.screen_.style.fontFamily = fontFamily;
this.screen_.style.webkitFontSmoothing = smoothing;
this.syncCharacterSize();
};
/** @return {string} */
hterm.ScrollPort.prototype.getFontFamily = function() {
return this.screen_.style.fontFamily;
};
/**
* Set a custom stylesheet to include in the scrollport.
*
* Defaults to null, meaning no custom css is loaded. Set it back to null or
* the empty string to remove a previously applied custom css.
*
* @param {?string} url
*/
hterm.ScrollPort.prototype.setUserCssUrl = function(url) {
if (url) {
this.userCssLink_.setAttribute('href', url);
if (!this.userCssLink_.parentNode) {
this.document_.head.appendChild(this.userCssLink_);
}
} else if (this.userCssLink_.parentNode) {
this.document_.head.removeChild(this.userCssLink_);
}
};
/** @param {string} text */
hterm.ScrollPort.prototype.setUserCssText = function(text) {
this.userCssText_.textContent = text;
};
/** Focus. */
hterm.ScrollPort.prototype.focus = function() {
this.iframe_.focus();
this.screen_.focus();
this.publish('focus');
};
/**
* Unfocus the scrollport.
*/
hterm.ScrollPort.prototype.blur = function() {
this.screen_.blur();
};
/** @param {string} image */
hterm.ScrollPort.prototype.setBackgroundImage = function(image) {
this.screen_.style.backgroundImage = image;
};
/** @param {string} size */
hterm.ScrollPort.prototype.setBackgroundSize = function(size) {
this.screen_.style.backgroundSize = size;
};
/** @param {string} position */
hterm.ScrollPort.prototype.setBackgroundPosition = function(position) {
this.screen_.style.backgroundPosition = position;
};
/** @param {number} size */
hterm.ScrollPort.prototype.setScreenPaddingSize = function(size) {
this.screenPaddingSize = size;
this.resize();
};
/** @param {boolean} ctrlVPaste */
hterm.ScrollPort.prototype.setCtrlVPaste = function(ctrlVPaste) {
this.ctrlVPaste = ctrlVPaste;
};
/** @param {boolean} pasteOnDrop */
hterm.ScrollPort.prototype.setPasteOnDrop = function(pasteOnDrop) {
this.pasteOnDrop = pasteOnDrop;
};
/**
* Get the usable size of the scrollport screen.
*
* The width will not include the scrollbar width.
*
* @return {{height: number, width: number}}
*/
hterm.ScrollPort.prototype.getScreenSize = function() {
const size = this.screen_.getBoundingClientRect();
const rightPadding = Math.max(
this.screenPaddingSize, this.currentScrollbarWidthPx);
return {
height: size.height - (2 * this.screenPaddingSize),
width: size.width - this.screenPaddingSize - rightPadding,
};
};
/**
* Get the usable width of the scrollport screen.
*
* This the widget width minus scrollbar width.
*
* @return {number}
*/
hterm.ScrollPort.prototype.getScreenWidth = function() {
return this.getScreenSize().width;
};
/**
* Get the usable height of the scrollport screen.
*
* @return {number}
*/
hterm.ScrollPort.prototype.getScreenHeight = function() {
return this.getScreenSize().height;
};