blob: 72c9945ce0b35c70e510ea3e63b113033b3754b2 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371/*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/**
32 * @unrestricted
33 */
34Console.ConsoleViewport = class {
35 /**
36 * @param {!Console.ConsoleViewportProvider} provider
37 */
38 constructor(provider) {
39 this.element = createElement('div');
40 this.element.style.overflow = 'auto';
41 this._topGapElement = this.element.createChild('div');
42 this._topGapElement.style.height = '0px';
43 this._topGapElement.style.color = 'transparent';
44 this._contentElement = this.element.createChild('div');
45 this._bottomGapElement = this.element.createChild('div');
46 this._bottomGapElement.style.height = '0px';
47 this._bottomGapElement.style.color = 'transparent';
48
49 // Text content needed for range intersection checks in _updateSelectionModel.
50 // Use Unicode ZERO WIDTH NO-BREAK SPACE, which avoids contributing any height to the element's layout overflow.
51 this._topGapElement.textContent = '\uFEFF';
52 this._bottomGapElement.textContent = '\uFEFF';
53
Joel Einbinderca8fa5a2018-08-24 23:30:2554 UI.ARIAUtils.markAsHidden(this._topGapElement);
55 UI.ARIAUtils.markAsHidden(this._bottomGapElement);
56
Blink Reformat4c46d092018-04-07 15:32:3757 this._provider = provider;
58 this.element.addEventListener('scroll', this._onScroll.bind(this), false);
59 this.element.addEventListener('copy', this._onCopy.bind(this), false);
60 this.element.addEventListener('dragstart', this._onDragStart.bind(this), false);
Pavel Feldmandb310912019-01-30 00:31:2061 this._contentElement.addEventListener('focusin', this._onFocusIn.bind(this), false);
62 this._contentElement.addEventListener('focusout', this._onFocusOut.bind(this), false);
63 this._contentElement.addEventListener('keydown', this._onKeyDown.bind(this), false);
Erik Luo39452ff2018-09-01 01:08:0764 this._virtualSelectedIndex = -1;
Erik Luob5bfff42018-09-20 02:52:3965 this._contentElement.tabIndex = -1;
Blink Reformat4c46d092018-04-07 15:32:3766
67 this._firstActiveIndex = -1;
68 this._lastActiveIndex = -1;
69 this._renderedItems = [];
70 this._anchorSelection = null;
71 this._headSelection = null;
72 this._itemCount = 0;
73 this._cumulativeHeights = new Int32Array(0);
74 this._muteCopyHandler = false;
75
76 // Listen for any changes to descendants and trigger a refresh. This ensures
77 // that items updated asynchronously will not break stick-to-bottom behavior
78 // if they change the scroll height.
79 this._observer = new MutationObserver(this.refresh.bind(this));
80 this._observerConfig = {childList: true, subtree: true};
81 }
82
83 /**
84 * @return {boolean}
85 */
86 stickToBottom() {
87 return this._stickToBottom;
88 }
89
90 /**
91 * @param {boolean} value
92 */
93 setStickToBottom(value) {
94 this._stickToBottom = value;
Tim van der Lippe1d6e57a2019-09-30 11:55:3495 if (this._stickToBottom) {
Blink Reformat4c46d092018-04-07 15:32:3796 this._observer.observe(this._contentElement, this._observerConfig);
Tim van der Lippe1d6e57a2019-09-30 11:55:3497 } else {
Blink Reformat4c46d092018-04-07 15:32:3798 this._observer.disconnect();
Tim van der Lippe1d6e57a2019-09-30 11:55:3499 }
Blink Reformat4c46d092018-04-07 15:32:37100 }
101
Erik Luoad6c91c2018-12-06 03:28:42102 /**
103 * @return {boolean}
104 */
105 hasVirtualSelection() {
106 return this._virtualSelectedIndex !== -1;
107 }
108
Blink Reformat4c46d092018-04-07 15:32:37109 copyWithStyles() {
110 this._muteCopyHandler = true;
111 this.element.ownerDocument.execCommand('copy');
112 this._muteCopyHandler = false;
113 }
114
115 /**
116 * @param {!Event} event
117 */
118 _onCopy(event) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34119 if (this._muteCopyHandler) {
Blink Reformat4c46d092018-04-07 15:32:37120 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34121 }
Blink Reformat4c46d092018-04-07 15:32:37122 const text = this._selectedText();
Tim van der Lippe1d6e57a2019-09-30 11:55:34123 if (!text) {
Blink Reformat4c46d092018-04-07 15:32:37124 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34125 }
Blink Reformat4c46d092018-04-07 15:32:37126 event.preventDefault();
127 event.clipboardData.setData('text/plain', text);
128 }
129
130 /**
131 * @param {!Event} event
132 */
Erik Luo39452ff2018-09-01 01:08:07133 _onFocusIn(event) {
Erik Luob5bfff42018-09-20 02:52:39134 const renderedIndex = this._renderedItems.findIndex(item => item.element().isSelfOrAncestor(event.target));
Tim van der Lippe1d6e57a2019-09-30 11:55:34135 if (renderedIndex !== -1) {
Erik Luob5bfff42018-09-20 02:52:39136 this._virtualSelectedIndex = this._firstActiveIndex + renderedIndex;
Tim van der Lippe1d6e57a2019-09-30 11:55:34137 }
Erik Luofc6a6302018-11-02 06:48:52138 let focusLastChild = false;
Erik Luo39452ff2018-09-01 01:08:07139 // Make default selection when moving from external (e.g. prompt) to the container.
140 if (this._virtualSelectedIndex === -1 && this._isOutsideViewport(/** @type {?Element} */ (event.relatedTarget)) &&
Erik Luoad6c91c2018-12-06 03:28:42141 event.target === this._contentElement && this._itemCount) {
Erik Luofc6a6302018-11-02 06:48:52142 focusLastChild = true;
Erik Luo39452ff2018-09-01 01:08:07143 this._virtualSelectedIndex = this._itemCount - 1;
Erik Luo840be6b2018-12-03 20:54:27144
145 // Update stick to bottom before scrolling into view.
146 this.refresh();
147 this.scrollItemIntoView(this._virtualSelectedIndex);
Erik Luofc6a6302018-11-02 06:48:52148 }
149 this._updateFocusedItem(focusLastChild);
Erik Luo39452ff2018-09-01 01:08:07150 }
151
152 /**
153 * @param {!Event} event
154 */
155 _onFocusOut(event) {
156 // Remove selection when focus moves to external location (e.g. prompt).
Tim van der Lippe1d6e57a2019-09-30 11:55:34157 if (this._isOutsideViewport(/** @type {?Element} */ (event.relatedTarget))) {
Erik Luo39452ff2018-09-01 01:08:07158 this._virtualSelectedIndex = -1;
Tim van der Lippe1d6e57a2019-09-30 11:55:34159 }
Erik Luo39452ff2018-09-01 01:08:07160 this._updateFocusedItem();
161 }
162
163 /**
164 * @param {?Element} element
165 * @return {boolean}
166 */
167 _isOutsideViewport(element) {
Erik Luob5bfff42018-09-20 02:52:39168 return !!element && !element.isSelfOrDescendant(this._contentElement);
Erik Luo39452ff2018-09-01 01:08:07169 }
170
171 /**
172 * @param {!Event} event
173 */
Blink Reformat4c46d092018-04-07 15:32:37174 _onDragStart(event) {
175 const text = this._selectedText();
Tim van der Lippe1d6e57a2019-09-30 11:55:34176 if (!text) {
Blink Reformat4c46d092018-04-07 15:32:37177 return false;
Tim van der Lippe1d6e57a2019-09-30 11:55:34178 }
Blink Reformat4c46d092018-04-07 15:32:37179 event.dataTransfer.clearData();
180 event.dataTransfer.setData('text/plain', text);
181 event.dataTransfer.effectAllowed = 'copy';
182 return true;
183 }
184
185 /**
Erik Luo39452ff2018-09-01 01:08:07186 * @param {!Event} event
187 */
188 _onKeyDown(event) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34189 if (UI.isEditing() || !this._itemCount || event.shiftKey) {
Erik Luo39452ff2018-09-01 01:08:07190 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34191 }
Erik Luo0b8282e2018-10-08 20:37:46192 let isArrowUp = false;
Erik Luo39452ff2018-09-01 01:08:07193 switch (event.key) {
194 case 'ArrowUp':
Erik Luo0b8282e2018-10-08 20:37:46195 if (this._virtualSelectedIndex > 0) {
196 isArrowUp = true;
197 this._virtualSelectedIndex--;
198 } else {
199 return;
200 }
Erik Luo39452ff2018-09-01 01:08:07201 break;
202 case 'ArrowDown':
Tim van der Lippe1d6e57a2019-09-30 11:55:34203 if (this._virtualSelectedIndex < this._itemCount - 1) {
Erik Luo0b8282e2018-10-08 20:37:46204 this._virtualSelectedIndex++;
Tim van der Lippe1d6e57a2019-09-30 11:55:34205 } else {
Erik Luo0b8282e2018-10-08 20:37:46206 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34207 }
Erik Luo39452ff2018-09-01 01:08:07208 break;
209 case 'Home':
210 this._virtualSelectedIndex = 0;
211 break;
212 case 'End':
213 this._virtualSelectedIndex = this._itemCount - 1;
214 break;
215 default:
216 return;
217 }
218 event.consume(true);
219 this.scrollItemIntoView(this._virtualSelectedIndex);
Erik Luo0b8282e2018-10-08 20:37:46220 this._updateFocusedItem(isArrowUp);
Erik Luo39452ff2018-09-01 01:08:07221 }
222
Erik Luo0b8282e2018-10-08 20:37:46223 /**
224 * @param {boolean=} focusLastChild
225 */
226 _updateFocusedItem(focusLastChild) {
Erik Luo39452ff2018-09-01 01:08:07227 const selectedElement = this.renderedElementAt(this._virtualSelectedIndex);
228 const changed = this._lastSelectedElement !== selectedElement;
Erik Luob5bfff42018-09-20 02:52:39229 const containerHasFocus = this._contentElement === this.element.ownerDocument.deepActiveElement();
Tim van der Lippe1d6e57a2019-09-30 11:55:34230 if (this._lastSelectedElement && changed) {
Erik Luo39452ff2018-09-01 01:08:07231 this._lastSelectedElement.classList.remove('console-selected');
Tim van der Lippe1d6e57a2019-09-30 11:55:34232 }
Erik Luo840be6b2018-12-03 20:54:27233 if (selectedElement && (focusLastChild || changed || containerHasFocus) && this.element.hasFocus()) {
Erik Luo39452ff2018-09-01 01:08:07234 selectedElement.classList.add('console-selected');
Erik Luob5bfff42018-09-20 02:52:39235 // Do not focus the message if something within holds focus (e.g. object).
Erik Luo840be6b2018-12-03 20:54:27236 if (focusLastChild) {
237 this.setStickToBottom(false);
238 this._renderedItems[this._virtualSelectedIndex - this._firstActiveIndex].focusLastChildOrSelf();
239 } else if (!selectedElement.hasFocus()) {
240 focusWithoutScroll(selectedElement);
Erik Luo0b8282e2018-10-08 20:37:46241 }
Erik Luo39452ff2018-09-01 01:08:07242 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34243 if (this._itemCount && !this._contentElement.hasFocus()) {
Erik Luob5bfff42018-09-20 02:52:39244 this._contentElement.tabIndex = 0;
Tim van der Lippe1d6e57a2019-09-30 11:55:34245 } else {
Erik Luob5bfff42018-09-20 02:52:39246 this._contentElement.tabIndex = -1;
Tim van der Lippe1d6e57a2019-09-30 11:55:34247 }
Erik Luo39452ff2018-09-01 01:08:07248 this._lastSelectedElement = selectedElement;
249
250 /**
251 * @suppress {checkTypes}
252 * @param {!Element} element
253 */
254 function focusWithoutScroll(element) {
255 // TODO(luoe): Closure has an outdated typedef for Element.prototype.focus.
256 element.focus({preventScroll: true});
257 }
258 }
259
260 /**
Blink Reformat4c46d092018-04-07 15:32:37261 * @return {!Element}
262 */
263 contentElement() {
264 return this._contentElement;
265 }
266
267 invalidate() {
268 delete this._cachedProviderElements;
269 this._itemCount = this._provider.itemCount();
Tim van der Lippe1d6e57a2019-09-30 11:55:34270 if (this._virtualSelectedIndex > this._itemCount - 1) {
Erik Luo39452ff2018-09-01 01:08:07271 this._virtualSelectedIndex = this._itemCount - 1;
Tim van der Lippe1d6e57a2019-09-30 11:55:34272 }
Blink Reformat4c46d092018-04-07 15:32:37273 this._rebuildCumulativeHeights();
274 this.refresh();
275 }
276
277 /**
278 * @param {number} index
279 * @return {?Console.ConsoleViewportElement}
280 */
281 _providerElement(index) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34282 if (!this._cachedProviderElements) {
Blink Reformat4c46d092018-04-07 15:32:37283 this._cachedProviderElements = new Array(this._itemCount);
Tim van der Lippe1d6e57a2019-09-30 11:55:34284 }
Blink Reformat4c46d092018-04-07 15:32:37285 let element = this._cachedProviderElements[index];
286 if (!element) {
287 element = this._provider.itemElement(index);
288 this._cachedProviderElements[index] = element;
289 }
290 return element;
291 }
292
293 _rebuildCumulativeHeights() {
294 const firstActiveIndex = this._firstActiveIndex;
295 const lastActiveIndex = this._lastActiveIndex;
296 let height = 0;
297 this._cumulativeHeights = new Int32Array(this._itemCount);
298 for (let i = 0; i < this._itemCount; ++i) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34299 if (firstActiveIndex <= i && i - firstActiveIndex < this._renderedItems.length && i <= lastActiveIndex) {
Blink Reformat4c46d092018-04-07 15:32:37300 height += this._renderedItems[i - firstActiveIndex].element().offsetHeight;
Tim van der Lippe1d6e57a2019-09-30 11:55:34301 } else {
Blink Reformat4c46d092018-04-07 15:32:37302 height += this._provider.fastHeight(i);
Tim van der Lippe1d6e57a2019-09-30 11:55:34303 }
Blink Reformat4c46d092018-04-07 15:32:37304 this._cumulativeHeights[i] = height;
305 }
306 }
307
308 _rebuildCumulativeHeightsIfNeeded() {
Erik Luo4b002322018-07-30 21:23:31309 let totalCachedHeight = 0;
310 let totalMeasuredHeight = 0;
Blink Reformat4c46d092018-04-07 15:32:37311 // Check whether current items in DOM have changed heights. Tolerate 1-pixel
312 // error due to double-to-integer rounding errors.
313 for (let i = 0; i < this._renderedItems.length; ++i) {
314 const cachedItemHeight = this._cachedItemHeight(this._firstActiveIndex + i);
Erik Luo4b002322018-07-30 21:23:31315 const measuredHeight = this._renderedItems[i].element().offsetHeight;
316 if (Math.abs(cachedItemHeight - measuredHeight) > 1) {
Blink Reformat4c46d092018-04-07 15:32:37317 this._rebuildCumulativeHeights();
Erik Luo4b002322018-07-30 21:23:31318 return;
319 }
320 totalMeasuredHeight += measuredHeight;
321 totalCachedHeight += cachedItemHeight;
322 if (Math.abs(totalCachedHeight - totalMeasuredHeight) > 1) {
323 this._rebuildCumulativeHeights();
324 return;
Blink Reformat4c46d092018-04-07 15:32:37325 }
326 }
327 }
328
329 /**
330 * @param {number} index
331 * @return {number}
332 */
333 _cachedItemHeight(index) {
334 return index === 0 ? this._cumulativeHeights[0] :
335 this._cumulativeHeights[index] - this._cumulativeHeights[index - 1];
336 }
337
338 /**
339 * @param {?Selection} selection
340 * @suppressGlobalPropertiesCheck
341 */
342 _isSelectionBackwards(selection) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34343 if (!selection || !selection.rangeCount) {
Blink Reformat4c46d092018-04-07 15:32:37344 return false;
Tim van der Lippe1d6e57a2019-09-30 11:55:34345 }
Blink Reformat4c46d092018-04-07 15:32:37346 const range = document.createRange();
347 range.setStart(selection.anchorNode, selection.anchorOffset);
348 range.setEnd(selection.focusNode, selection.focusOffset);
349 return range.collapsed;
350 }
351
352 /**
353 * @param {number} itemIndex
354 * @param {!Node} node
355 * @param {number} offset
356 * @return {!{item: number, node: !Node, offset: number}}
357 */
358 _createSelectionModel(itemIndex, node, offset) {
359 return {item: itemIndex, node: node, offset: offset};
360 }
361
362 /**
363 * @param {?Selection} selection
364 */
365 _updateSelectionModel(selection) {
366 const range = selection && selection.rangeCount ? selection.getRangeAt(0) : null;
367 if (!range || selection.isCollapsed || !this.element.hasSelection()) {
368 this._headSelection = null;
369 this._anchorSelection = null;
370 return false;
371 }
372
373 let firstSelected = Number.MAX_VALUE;
374 let lastSelected = -1;
375
376 let hasVisibleSelection = false;
377 for (let i = 0; i < this._renderedItems.length; ++i) {
378 if (range.intersectsNode(this._renderedItems[i].element())) {
379 const index = i + this._firstActiveIndex;
380 firstSelected = Math.min(firstSelected, index);
381 lastSelected = Math.max(lastSelected, index);
382 hasVisibleSelection = true;
383 }
384 }
385 if (hasVisibleSelection) {
386 firstSelected =
387 this._createSelectionModel(firstSelected, /** @type {!Node} */ (range.startContainer), range.startOffset);
388 lastSelected =
389 this._createSelectionModel(lastSelected, /** @type {!Node} */ (range.endContainer), range.endOffset);
390 }
391 const topOverlap = range.intersectsNode(this._topGapElement) && this._topGapElement._active;
392 const bottomOverlap = range.intersectsNode(this._bottomGapElement) && this._bottomGapElement._active;
393 if (!topOverlap && !bottomOverlap && !hasVisibleSelection) {
394 this._headSelection = null;
395 this._anchorSelection = null;
396 return false;
397 }
398
399 if (!this._anchorSelection || !this._headSelection) {
400 this._anchorSelection = this._createSelectionModel(0, this.element, 0);
401 this._headSelection = this._createSelectionModel(this._itemCount - 1, this.element, this.element.children.length);
402 this._selectionIsBackward = false;
403 }
404
405 const isBackward = this._isSelectionBackwards(selection);
406 const startSelection = this._selectionIsBackward ? this._headSelection : this._anchorSelection;
407 const endSelection = this._selectionIsBackward ? this._anchorSelection : this._headSelection;
408 if (topOverlap && bottomOverlap && hasVisibleSelection) {
409 firstSelected = firstSelected.item < startSelection.item ? firstSelected : startSelection;
410 lastSelected = lastSelected.item > endSelection.item ? lastSelected : endSelection;
411 } else if (!hasVisibleSelection) {
412 firstSelected = startSelection;
413 lastSelected = endSelection;
414 } else if (topOverlap) {
415 firstSelected = isBackward ? this._headSelection : this._anchorSelection;
416 } else if (bottomOverlap) {
417 lastSelected = isBackward ? this._anchorSelection : this._headSelection;
418 }
419
420 if (isBackward) {
421 this._anchorSelection = lastSelected;
422 this._headSelection = firstSelected;
423 } else {
424 this._anchorSelection = firstSelected;
425 this._headSelection = lastSelected;
426 }
427 this._selectionIsBackward = isBackward;
428 return true;
429 }
430
431 /**
432 * @param {?Selection} selection
433 */
434 _restoreSelection(selection) {
435 let anchorElement = null;
436 let anchorOffset;
437 if (this._firstActiveIndex <= this._anchorSelection.item && this._anchorSelection.item <= this._lastActiveIndex) {
438 anchorElement = this._anchorSelection.node;
439 anchorOffset = this._anchorSelection.offset;
440 } else {
Tim van der Lippe1d6e57a2019-09-30 11:55:34441 if (this._anchorSelection.item < this._firstActiveIndex) {
Blink Reformat4c46d092018-04-07 15:32:37442 anchorElement = this._topGapElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:34443 } else if (this._anchorSelection.item > this._lastActiveIndex) {
Blink Reformat4c46d092018-04-07 15:32:37444 anchorElement = this._bottomGapElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:34445 }
Blink Reformat4c46d092018-04-07 15:32:37446 anchorOffset = this._selectionIsBackward ? 1 : 0;
447 }
448
449 let headElement = null;
450 let headOffset;
451 if (this._firstActiveIndex <= this._headSelection.item && this._headSelection.item <= this._lastActiveIndex) {
452 headElement = this._headSelection.node;
453 headOffset = this._headSelection.offset;
454 } else {
Tim van der Lippe1d6e57a2019-09-30 11:55:34455 if (this._headSelection.item < this._firstActiveIndex) {
Blink Reformat4c46d092018-04-07 15:32:37456 headElement = this._topGapElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:34457 } else if (this._headSelection.item > this._lastActiveIndex) {
Blink Reformat4c46d092018-04-07 15:32:37458 headElement = this._bottomGapElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:34459 }
Blink Reformat4c46d092018-04-07 15:32:37460 headOffset = this._selectionIsBackward ? 0 : 1;
461 }
462
463 selection.setBaseAndExtent(anchorElement, anchorOffset, headElement, headOffset);
464 }
465
466 refresh() {
467 this._observer.disconnect();
468 this._innerRefresh();
Tim van der Lippe1d6e57a2019-09-30 11:55:34469 if (this._stickToBottom) {
Blink Reformat4c46d092018-04-07 15:32:37470 this._observer.observe(this._contentElement, this._observerConfig);
Tim van der Lippe1d6e57a2019-09-30 11:55:34471 }
Blink Reformat4c46d092018-04-07 15:32:37472 }
473
474 _innerRefresh() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34475 if (!this._visibleHeight()) {
476 return;
477 } // Do nothing for invisible controls.
Blink Reformat4c46d092018-04-07 15:32:37478
479 if (!this._itemCount) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34480 for (let i = 0; i < this._renderedItems.length; ++i) {
Blink Reformat4c46d092018-04-07 15:32:37481 this._renderedItems[i].willHide();
Tim van der Lippe1d6e57a2019-09-30 11:55:34482 }
Blink Reformat4c46d092018-04-07 15:32:37483 this._renderedItems = [];
484 this._contentElement.removeChildren();
485 this._topGapElement.style.height = '0px';
486 this._bottomGapElement.style.height = '0px';
487 this._firstActiveIndex = -1;
488 this._lastActiveIndex = -1;
Pavel Feldmandb310912019-01-30 00:31:20489 this._updateFocusedItem();
Blink Reformat4c46d092018-04-07 15:32:37490 return;
491 }
492
493 const selection = this.element.getComponentSelection();
494 const shouldRestoreSelection = this._updateSelectionModel(selection);
495
496 const visibleFrom = this.element.scrollTop;
497 const visibleHeight = this._visibleHeight();
498 const activeHeight = visibleHeight * 2;
499 this._rebuildCumulativeHeightsIfNeeded();
500
501 // When the viewport is scrolled to the bottom, using the cumulative heights estimate is not
502 // precise enough to determine next visible indices. This stickToBottom check avoids extra
503 // calls to refresh in those cases.
504 if (this._stickToBottom) {
505 this._firstActiveIndex =
506 Math.max(this._itemCount - Math.ceil(activeHeight / this._provider.minimumRowHeight()), 0);
507 this._lastActiveIndex = this._itemCount - 1;
508 } else {
509 this._firstActiveIndex =
510 Math.max(this._cumulativeHeights.lowerBound(visibleFrom + 1 - (activeHeight - visibleHeight) / 2), 0);
511 // Proactively render more rows in case some of them will be collapsed without triggering refresh. @see crbug.com/390169
512 this._lastActiveIndex = this._firstActiveIndex + Math.ceil(activeHeight / this._provider.minimumRowHeight()) - 1;
513 this._lastActiveIndex = Math.min(this._lastActiveIndex, this._itemCount - 1);
514 }
515
516 const topGapHeight = this._cumulativeHeights[this._firstActiveIndex - 1] || 0;
517 const bottomGapHeight =
518 this._cumulativeHeights[this._cumulativeHeights.length - 1] - this._cumulativeHeights[this._lastActiveIndex];
519
520 /**
521 * @this {Console.ConsoleViewport}
522 */
523 function prepare() {
524 this._topGapElement.style.height = topGapHeight + 'px';
525 this._bottomGapElement.style.height = bottomGapHeight + 'px';
526 this._topGapElement._active = !!topGapHeight;
527 this._bottomGapElement._active = !!bottomGapHeight;
528 this._contentElement.style.setProperty('height', '10000000px');
529 }
530
531 this._partialViewportUpdate(prepare.bind(this));
532 this._contentElement.style.removeProperty('height');
533 // Should be the last call in the method as it might force layout.
Tim van der Lippe1d6e57a2019-09-30 11:55:34534 if (shouldRestoreSelection) {
Blink Reformat4c46d092018-04-07 15:32:37535 this._restoreSelection(selection);
Tim van der Lippe1d6e57a2019-09-30 11:55:34536 }
537 if (this._stickToBottom) {
Blink Reformat4c46d092018-04-07 15:32:37538 this.element.scrollTop = 10000000;
Tim van der Lippe1d6e57a2019-09-30 11:55:34539 }
Blink Reformat4c46d092018-04-07 15:32:37540 }
541
542 /**
543 * @param {function()} prepare
544 */
545 _partialViewportUpdate(prepare) {
546 const itemsToRender = new Set();
Tim van der Lippe1d6e57a2019-09-30 11:55:34547 for (let i = this._firstActiveIndex; i <= this._lastActiveIndex; ++i) {
Blink Reformat4c46d092018-04-07 15:32:37548 itemsToRender.add(this._providerElement(i));
Tim van der Lippe1d6e57a2019-09-30 11:55:34549 }
Blink Reformat4c46d092018-04-07 15:32:37550 const willBeHidden = this._renderedItems.filter(item => !itemsToRender.has(item));
Tim van der Lippe1d6e57a2019-09-30 11:55:34551 for (let i = 0; i < willBeHidden.length; ++i) {
Blink Reformat4c46d092018-04-07 15:32:37552 willBeHidden[i].willHide();
Tim van der Lippe1d6e57a2019-09-30 11:55:34553 }
Blink Reformat4c46d092018-04-07 15:32:37554 prepare();
Erik Luo39452ff2018-09-01 01:08:07555 let hadFocus = false;
556 for (let i = 0; i < willBeHidden.length; ++i) {
Pavel Feldmandb310912019-01-30 00:31:20557 hadFocus = hadFocus || willBeHidden[i].element().hasFocus();
Blink Reformat4c46d092018-04-07 15:32:37558 willBeHidden[i].element().remove();
Erik Luo39452ff2018-09-01 01:08:07559 }
Blink Reformat4c46d092018-04-07 15:32:37560
561 const wasShown = [];
562 let anchor = this._contentElement.firstChild;
563 for (const viewportElement of itemsToRender) {
564 const element = viewportElement.element();
565 if (element !== anchor) {
566 const shouldCallWasShown = !element.parentElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:34567 if (shouldCallWasShown) {
Blink Reformat4c46d092018-04-07 15:32:37568 wasShown.push(viewportElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34569 }
Blink Reformat4c46d092018-04-07 15:32:37570 this._contentElement.insertBefore(element, anchor);
571 } else {
572 anchor = anchor.nextSibling;
573 }
574 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34575 for (let i = 0; i < wasShown.length; ++i) {
Blink Reformat4c46d092018-04-07 15:32:37576 wasShown[i].wasShown();
Tim van der Lippe1d6e57a2019-09-30 11:55:34577 }
Blink Reformat4c46d092018-04-07 15:32:37578 this._renderedItems = Array.from(itemsToRender);
Erik Luo39452ff2018-09-01 01:08:07579
Tim van der Lippe1d6e57a2019-09-30 11:55:34580 if (hadFocus) {
Pavel Feldmandb310912019-01-30 00:31:20581 this._contentElement.focus();
Tim van der Lippe1d6e57a2019-09-30 11:55:34582 }
Pavel Feldmandb310912019-01-30 00:31:20583 this._updateFocusedItem();
Blink Reformat4c46d092018-04-07 15:32:37584 }
585
586 /**
587 * @return {?string}
588 */
589 _selectedText() {
590 this._updateSelectionModel(this.element.getComponentSelection());
Tim van der Lippe1d6e57a2019-09-30 11:55:34591 if (!this._headSelection || !this._anchorSelection) {
Blink Reformat4c46d092018-04-07 15:32:37592 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34593 }
Blink Reformat4c46d092018-04-07 15:32:37594
595 let startSelection = null;
596 let endSelection = null;
597 if (this._selectionIsBackward) {
598 startSelection = this._headSelection;
599 endSelection = this._anchorSelection;
600 } else {
601 startSelection = this._anchorSelection;
602 endSelection = this._headSelection;
603 }
604
605 const textLines = [];
606 for (let i = startSelection.item; i <= endSelection.item; ++i) {
607 const element = this._providerElement(i).element();
608 const lineContent = element.childTextNodes().map(Components.Linkifier.untruncatedNodeText).join('');
609 textLines.push(lineContent);
610 }
611
612 const endSelectionElement = this._providerElement(endSelection.item).element();
613 if (endSelection.node && endSelection.node.isSelfOrDescendant(endSelectionElement)) {
614 const itemTextOffset = this._textOffsetInNode(endSelectionElement, endSelection.node, endSelection.offset);
615 textLines[textLines.length - 1] = textLines.peekLast().substring(0, itemTextOffset);
616 }
617
618 const startSelectionElement = this._providerElement(startSelection.item).element();
619 if (startSelection.node && startSelection.node.isSelfOrDescendant(startSelectionElement)) {
620 const itemTextOffset = this._textOffsetInNode(startSelectionElement, startSelection.node, startSelection.offset);
621 textLines[0] = textLines[0].substring(itemTextOffset);
622 }
623
624 return textLines.join('\n');
625 }
626
627 /**
628 * @param {!Element} itemElement
629 * @param {!Node} selectionNode
630 * @param {number} offset
631 * @return {number}
632 */
633 _textOffsetInNode(itemElement, selectionNode, offset) {
634 // If the selectionNode is not a TextNode, we may need to convert a child offset into a character offset.
635 if (selectionNode.nodeType !== Node.TEXT_NODE) {
636 if (offset < selectionNode.childNodes.length) {
637 selectionNode = /** @type {!Node} */ (selectionNode.childNodes.item(offset));
638 offset = 0;
639 } else {
640 offset = selectionNode.textContent.length;
641 }
642 }
643
644 let chars = 0;
645 let node = itemElement;
646 while ((node = node.traverseNextNode(itemElement)) && node !== selectionNode) {
647 if (node.nodeType !== Node.TEXT_NODE || node.parentElement.nodeName === 'STYLE' ||
Tim van der Lippe1d6e57a2019-09-30 11:55:34648 node.parentElement.nodeName === 'SCRIPT') {
Blink Reformat4c46d092018-04-07 15:32:37649 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34650 }
Blink Reformat4c46d092018-04-07 15:32:37651 chars += Components.Linkifier.untruncatedNodeText(node).length;
652 }
653 // If the selected node text was truncated, treat any non-zero offset as the full length.
654 const untruncatedContainerLength = Components.Linkifier.untruncatedNodeText(selectionNode).length;
Tim van der Lippe1d6e57a2019-09-30 11:55:34655 if (offset > 0 && untruncatedContainerLength !== selectionNode.textContent.length) {
Blink Reformat4c46d092018-04-07 15:32:37656 offset = untruncatedContainerLength;
Tim van der Lippe1d6e57a2019-09-30 11:55:34657 }
Blink Reformat4c46d092018-04-07 15:32:37658 return chars + offset;
659 }
660
661 /**
662 * @param {!Event} event
663 */
664 _onScroll(event) {
665 this.refresh();
666 }
667
668 /**
669 * @return {number}
670 */
671 firstVisibleIndex() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34672 if (!this._cumulativeHeights.length) {
Blink Reformat4c46d092018-04-07 15:32:37673 return -1;
Tim van der Lippe1d6e57a2019-09-30 11:55:34674 }
Blink Reformat4c46d092018-04-07 15:32:37675 this._rebuildCumulativeHeightsIfNeeded();
676 return this._cumulativeHeights.lowerBound(this.element.scrollTop + 1);
677 }
678
679 /**
680 * @return {number}
681 */
682 lastVisibleIndex() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34683 if (!this._cumulativeHeights.length) {
Blink Reformat4c46d092018-04-07 15:32:37684 return -1;
Tim van der Lippe1d6e57a2019-09-30 11:55:34685 }
Blink Reformat4c46d092018-04-07 15:32:37686 this._rebuildCumulativeHeightsIfNeeded();
687 const scrollBottom = this.element.scrollTop + this.element.clientHeight;
688 const right = this._itemCount - 1;
689 return this._cumulativeHeights.lowerBound(scrollBottom, undefined, undefined, right);
690 }
691
692 /**
693 * @return {?Element}
694 */
695 renderedElementAt(index) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34696 if (index === -1 || index < this._firstActiveIndex || index > this._lastActiveIndex) {
Blink Reformat4c46d092018-04-07 15:32:37697 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34698 }
Blink Reformat4c46d092018-04-07 15:32:37699 return this._renderedItems[index - this._firstActiveIndex].element();
700 }
701
702 /**
703 * @param {number} index
704 * @param {boolean=} makeLast
705 */
706 scrollItemIntoView(index, makeLast) {
707 const firstVisibleIndex = this.firstVisibleIndex();
708 const lastVisibleIndex = this.lastVisibleIndex();
Tim van der Lippe1d6e57a2019-09-30 11:55:34709 if (index > firstVisibleIndex && index < lastVisibleIndex) {
Blink Reformat4c46d092018-04-07 15:32:37710 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34711 }
Erik Luo4b002322018-07-30 21:23:31712 // If the prompt is visible, then the last item must be fully on screen.
Tim van der Lippe1d6e57a2019-09-30 11:55:34713 if (index === lastVisibleIndex &&
714 this._cumulativeHeights[index] <= this.element.scrollTop + this._visibleHeight()) {
Erik Luo4b002322018-07-30 21:23:31715 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34716 }
717 if (makeLast) {
Blink Reformat4c46d092018-04-07 15:32:37718 this.forceScrollItemToBeLast(index);
Tim van der Lippe1d6e57a2019-09-30 11:55:34719 } else if (index <= firstVisibleIndex) {
Blink Reformat4c46d092018-04-07 15:32:37720 this.forceScrollItemToBeFirst(index);
Tim van der Lippe1d6e57a2019-09-30 11:55:34721 } else if (index >= lastVisibleIndex) {
Blink Reformat4c46d092018-04-07 15:32:37722 this.forceScrollItemToBeLast(index);
Tim van der Lippe1d6e57a2019-09-30 11:55:34723 }
Blink Reformat4c46d092018-04-07 15:32:37724 }
725
726 /**
727 * @param {number} index
728 */
729 forceScrollItemToBeFirst(index) {
730 console.assert(index >= 0 && index < this._itemCount, 'Cannot scroll item at invalid index');
731 this.setStickToBottom(false);
732 this._rebuildCumulativeHeightsIfNeeded();
733 this.element.scrollTop = index > 0 ? this._cumulativeHeights[index - 1] : 0;
Tim van der Lippe1d6e57a2019-09-30 11:55:34734 if (this.element.isScrolledToBottom()) {
Blink Reformat4c46d092018-04-07 15:32:37735 this.setStickToBottom(true);
Tim van der Lippe1d6e57a2019-09-30 11:55:34736 }
Blink Reformat4c46d092018-04-07 15:32:37737 this.refresh();
Erik Luo4b002322018-07-30 21:23:31738 // After refresh, the item is in DOM, but may not be visible (items above were larger than expected).
739 this.renderedElementAt(index).scrollIntoView(true /* alignTop */);
Blink Reformat4c46d092018-04-07 15:32:37740 }
741
742 /**
743 * @param {number} index
744 */
745 forceScrollItemToBeLast(index) {
746 console.assert(index >= 0 && index < this._itemCount, 'Cannot scroll item at invalid index');
747 this.setStickToBottom(false);
748 this._rebuildCumulativeHeightsIfNeeded();
749 this.element.scrollTop = this._cumulativeHeights[index] - this._visibleHeight();
Tim van der Lippe1d6e57a2019-09-30 11:55:34750 if (this.element.isScrolledToBottom()) {
Blink Reformat4c46d092018-04-07 15:32:37751 this.setStickToBottom(true);
Tim van der Lippe1d6e57a2019-09-30 11:55:34752 }
Blink Reformat4c46d092018-04-07 15:32:37753 this.refresh();
Erik Luo4b002322018-07-30 21:23:31754 // After refresh, the item is in DOM, but may not be visible (items above were larger than expected).
755 this.renderedElementAt(index).scrollIntoView(false /* alignTop */);
Blink Reformat4c46d092018-04-07 15:32:37756 }
757
758 /**
759 * @return {number}
760 */
761 _visibleHeight() {
762 // Use offsetHeight instead of clientHeight to avoid being affected by horizontal scroll.
763 return this.element.offsetHeight;
764 }
765};
766
767/**
768 * @interface
769 */
770Console.ConsoleViewportProvider = function() {};
771
772Console.ConsoleViewportProvider.prototype = {
773 /**
774 * @param {number} index
775 * @return {number}
776 */
777 fastHeight(index) {
778 return 0;
779 },
780
781 /**
782 * @return {number}
783 */
784 itemCount() {
785 return 0;
786 },
787
788 /**
789 * @return {number}
790 */
791 minimumRowHeight() {
792 return 0;
793 },
794
795 /**
796 * @param {number} index
797 * @return {?Console.ConsoleViewportElement}
798 */
799 itemElement(index) {
800 return null;
801 }
802};
803
804/**
805 * @interface
806 */
807Console.ConsoleViewportElement = function() {};
808Console.ConsoleViewportElement.prototype = {
809 willHide() {},
810
811 wasShown() {},
812
813 /**
814 * @return {!Element}
815 */
816 element() {},
817};