Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 34 | Console.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 Einbinder | ca8fa5a | 2018-08-24 23:30:25 | [diff] [blame] | 54 | UI.ARIAUtils.markAsHidden(this._topGapElement); |
| 55 | UI.ARIAUtils.markAsHidden(this._bottomGapElement); |
| 56 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 57 | 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 Feldman | db31091 | 2019-01-30 00:31:20 | [diff] [blame] | 61 | 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 Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 64 | this._virtualSelectedIndex = -1; |
Erik Luo | b5bfff4 | 2018-09-20 02:52:39 | [diff] [blame] | 65 | this._contentElement.tabIndex = -1; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 66 | |
| 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 95 | if (this._stickToBottom) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 96 | this._observer.observe(this._contentElement, this._observerConfig); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 97 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 98 | this._observer.disconnect(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 99 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 100 | } |
| 101 | |
Erik Luo | ad6c91c | 2018-12-06 03:28:42 | [diff] [blame] | 102 | /** |
| 103 | * @return {boolean} |
| 104 | */ |
| 105 | hasVirtualSelection() { |
| 106 | return this._virtualSelectedIndex !== -1; |
| 107 | } |
| 108 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 109 | 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 119 | if (this._muteCopyHandler) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 120 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 121 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 122 | const text = this._selectedText(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 123 | if (!text) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 124 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 125 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 126 | event.preventDefault(); |
| 127 | event.clipboardData.setData('text/plain', text); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @param {!Event} event |
| 132 | */ |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 133 | _onFocusIn(event) { |
Erik Luo | b5bfff4 | 2018-09-20 02:52:39 | [diff] [blame] | 134 | const renderedIndex = this._renderedItems.findIndex(item => item.element().isSelfOrAncestor(event.target)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 135 | if (renderedIndex !== -1) { |
Erik Luo | b5bfff4 | 2018-09-20 02:52:39 | [diff] [blame] | 136 | this._virtualSelectedIndex = this._firstActiveIndex + renderedIndex; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 137 | } |
Erik Luo | fc6a630 | 2018-11-02 06:48:52 | [diff] [blame] | 138 | let focusLastChild = false; |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 139 | // 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 Luo | ad6c91c | 2018-12-06 03:28:42 | [diff] [blame] | 141 | event.target === this._contentElement && this._itemCount) { |
Erik Luo | fc6a630 | 2018-11-02 06:48:52 | [diff] [blame] | 142 | focusLastChild = true; |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 143 | this._virtualSelectedIndex = this._itemCount - 1; |
Erik Luo | 840be6b | 2018-12-03 20:54:27 | [diff] [blame] | 144 | |
| 145 | // Update stick to bottom before scrolling into view. |
| 146 | this.refresh(); |
| 147 | this.scrollItemIntoView(this._virtualSelectedIndex); |
Erik Luo | fc6a630 | 2018-11-02 06:48:52 | [diff] [blame] | 148 | } |
| 149 | this._updateFocusedItem(focusLastChild); |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 150 | } |
| 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 157 | if (this._isOutsideViewport(/** @type {?Element} */ (event.relatedTarget))) { |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 158 | this._virtualSelectedIndex = -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 159 | } |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 160 | this._updateFocusedItem(); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @param {?Element} element |
| 165 | * @return {boolean} |
| 166 | */ |
| 167 | _isOutsideViewport(element) { |
Erik Luo | b5bfff4 | 2018-09-20 02:52:39 | [diff] [blame] | 168 | return !!element && !element.isSelfOrDescendant(this._contentElement); |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @param {!Event} event |
| 173 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 174 | _onDragStart(event) { |
| 175 | const text = this._selectedText(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 176 | if (!text) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 177 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 178 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 179 | event.dataTransfer.clearData(); |
| 180 | event.dataTransfer.setData('text/plain', text); |
| 181 | event.dataTransfer.effectAllowed = 'copy'; |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | /** |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 186 | * @param {!Event} event |
| 187 | */ |
| 188 | _onKeyDown(event) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 189 | if (UI.isEditing() || !this._itemCount || event.shiftKey) { |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 190 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 191 | } |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 192 | let isArrowUp = false; |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 193 | switch (event.key) { |
| 194 | case 'ArrowUp': |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 195 | if (this._virtualSelectedIndex > 0) { |
| 196 | isArrowUp = true; |
| 197 | this._virtualSelectedIndex--; |
| 198 | } else { |
| 199 | return; |
| 200 | } |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 201 | break; |
| 202 | case 'ArrowDown': |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 203 | if (this._virtualSelectedIndex < this._itemCount - 1) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 204 | this._virtualSelectedIndex++; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 205 | } else { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 206 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 207 | } |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 208 | 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 Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 220 | this._updateFocusedItem(isArrowUp); |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 221 | } |
| 222 | |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 223 | /** |
| 224 | * @param {boolean=} focusLastChild |
| 225 | */ |
| 226 | _updateFocusedItem(focusLastChild) { |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 227 | const selectedElement = this.renderedElementAt(this._virtualSelectedIndex); |
| 228 | const changed = this._lastSelectedElement !== selectedElement; |
Erik Luo | b5bfff4 | 2018-09-20 02:52:39 | [diff] [blame] | 229 | const containerHasFocus = this._contentElement === this.element.ownerDocument.deepActiveElement(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 230 | if (this._lastSelectedElement && changed) { |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 231 | this._lastSelectedElement.classList.remove('console-selected'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 232 | } |
Erik Luo | 840be6b | 2018-12-03 20:54:27 | [diff] [blame] | 233 | if (selectedElement && (focusLastChild || changed || containerHasFocus) && this.element.hasFocus()) { |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 234 | selectedElement.classList.add('console-selected'); |
Erik Luo | b5bfff4 | 2018-09-20 02:52:39 | [diff] [blame] | 235 | // Do not focus the message if something within holds focus (e.g. object). |
Erik Luo | 840be6b | 2018-12-03 20:54:27 | [diff] [blame] | 236 | if (focusLastChild) { |
| 237 | this.setStickToBottom(false); |
| 238 | this._renderedItems[this._virtualSelectedIndex - this._firstActiveIndex].focusLastChildOrSelf(); |
| 239 | } else if (!selectedElement.hasFocus()) { |
| 240 | focusWithoutScroll(selectedElement); |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 241 | } |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 242 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 243 | if (this._itemCount && !this._contentElement.hasFocus()) { |
Erik Luo | b5bfff4 | 2018-09-20 02:52:39 | [diff] [blame] | 244 | this._contentElement.tabIndex = 0; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 245 | } else { |
Erik Luo | b5bfff4 | 2018-09-20 02:52:39 | [diff] [blame] | 246 | this._contentElement.tabIndex = -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 247 | } |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 248 | 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 261 | * @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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 270 | if (this._virtualSelectedIndex > this._itemCount - 1) { |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 271 | this._virtualSelectedIndex = this._itemCount - 1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 272 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 273 | this._rebuildCumulativeHeights(); |
| 274 | this.refresh(); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * @param {number} index |
| 279 | * @return {?Console.ConsoleViewportElement} |
| 280 | */ |
| 281 | _providerElement(index) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 282 | if (!this._cachedProviderElements) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 283 | this._cachedProviderElements = new Array(this._itemCount); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 284 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 285 | 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 299 | if (firstActiveIndex <= i && i - firstActiveIndex < this._renderedItems.length && i <= lastActiveIndex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 300 | height += this._renderedItems[i - firstActiveIndex].element().offsetHeight; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 301 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 302 | height += this._provider.fastHeight(i); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 303 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 304 | this._cumulativeHeights[i] = height; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | _rebuildCumulativeHeightsIfNeeded() { |
Erik Luo | 4b00232 | 2018-07-30 21:23:31 | [diff] [blame] | 309 | let totalCachedHeight = 0; |
| 310 | let totalMeasuredHeight = 0; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 311 | // 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 Luo | 4b00232 | 2018-07-30 21:23:31 | [diff] [blame] | 315 | const measuredHeight = this._renderedItems[i].element().offsetHeight; |
| 316 | if (Math.abs(cachedItemHeight - measuredHeight) > 1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 317 | this._rebuildCumulativeHeights(); |
Erik Luo | 4b00232 | 2018-07-30 21:23:31 | [diff] [blame] | 318 | return; |
| 319 | } |
| 320 | totalMeasuredHeight += measuredHeight; |
| 321 | totalCachedHeight += cachedItemHeight; |
| 322 | if (Math.abs(totalCachedHeight - totalMeasuredHeight) > 1) { |
| 323 | this._rebuildCumulativeHeights(); |
| 324 | return; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 325 | } |
| 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 343 | if (!selection || !selection.rangeCount) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 344 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 345 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 346 | 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 441 | if (this._anchorSelection.item < this._firstActiveIndex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 442 | anchorElement = this._topGapElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 443 | } else if (this._anchorSelection.item > this._lastActiveIndex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 444 | anchorElement = this._bottomGapElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 445 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 446 | 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 455 | if (this._headSelection.item < this._firstActiveIndex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 456 | headElement = this._topGapElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 457 | } else if (this._headSelection.item > this._lastActiveIndex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 458 | headElement = this._bottomGapElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 459 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 460 | 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 469 | if (this._stickToBottom) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 470 | this._observer.observe(this._contentElement, this._observerConfig); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 471 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | _innerRefresh() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 475 | if (!this._visibleHeight()) { |
| 476 | return; |
| 477 | } // Do nothing for invisible controls. |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 478 | |
| 479 | if (!this._itemCount) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 480 | for (let i = 0; i < this._renderedItems.length; ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 481 | this._renderedItems[i].willHide(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 482 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 483 | 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 Feldman | db31091 | 2019-01-30 00:31:20 | [diff] [blame] | 489 | this._updateFocusedItem(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 490 | 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 534 | if (shouldRestoreSelection) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 535 | this._restoreSelection(selection); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 536 | } |
| 537 | if (this._stickToBottom) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 538 | this.element.scrollTop = 10000000; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 539 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /** |
| 543 | * @param {function()} prepare |
| 544 | */ |
| 545 | _partialViewportUpdate(prepare) { |
| 546 | const itemsToRender = new Set(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 547 | for (let i = this._firstActiveIndex; i <= this._lastActiveIndex; ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 548 | itemsToRender.add(this._providerElement(i)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 549 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 550 | const willBeHidden = this._renderedItems.filter(item => !itemsToRender.has(item)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 551 | for (let i = 0; i < willBeHidden.length; ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 552 | willBeHidden[i].willHide(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 553 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 554 | prepare(); |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 555 | let hadFocus = false; |
| 556 | for (let i = 0; i < willBeHidden.length; ++i) { |
Pavel Feldman | db31091 | 2019-01-30 00:31:20 | [diff] [blame] | 557 | hadFocus = hadFocus || willBeHidden[i].element().hasFocus(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 558 | willBeHidden[i].element().remove(); |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 559 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 560 | |
| 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 567 | if (shouldCallWasShown) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 568 | wasShown.push(viewportElement); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 569 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 570 | this._contentElement.insertBefore(element, anchor); |
| 571 | } else { |
| 572 | anchor = anchor.nextSibling; |
| 573 | } |
| 574 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 575 | for (let i = 0; i < wasShown.length; ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 576 | wasShown[i].wasShown(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 577 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 578 | this._renderedItems = Array.from(itemsToRender); |
Erik Luo | 39452ff | 2018-09-01 01:08:07 | [diff] [blame] | 579 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 580 | if (hadFocus) { |
Pavel Feldman | db31091 | 2019-01-30 00:31:20 | [diff] [blame] | 581 | this._contentElement.focus(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 582 | } |
Pavel Feldman | db31091 | 2019-01-30 00:31:20 | [diff] [blame] | 583 | this._updateFocusedItem(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /** |
| 587 | * @return {?string} |
| 588 | */ |
| 589 | _selectedText() { |
| 590 | this._updateSelectionModel(this.element.getComponentSelection()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 591 | if (!this._headSelection || !this._anchorSelection) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 592 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 593 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 594 | |
| 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 648 | node.parentElement.nodeName === 'SCRIPT') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 649 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 650 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 651 | 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 655 | if (offset > 0 && untruncatedContainerLength !== selectionNode.textContent.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 656 | offset = untruncatedContainerLength; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 657 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 658 | 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 672 | if (!this._cumulativeHeights.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 673 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 674 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 675 | this._rebuildCumulativeHeightsIfNeeded(); |
| 676 | return this._cumulativeHeights.lowerBound(this.element.scrollTop + 1); |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * @return {number} |
| 681 | */ |
| 682 | lastVisibleIndex() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 683 | if (!this._cumulativeHeights.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 684 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 685 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 686 | 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 696 | if (index === -1 || index < this._firstActiveIndex || index > this._lastActiveIndex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 697 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 698 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 699 | 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 709 | if (index > firstVisibleIndex && index < lastVisibleIndex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 710 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 711 | } |
Erik Luo | 4b00232 | 2018-07-30 21:23:31 | [diff] [blame] | 712 | // If the prompt is visible, then the last item must be fully on screen. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 713 | if (index === lastVisibleIndex && |
| 714 | this._cumulativeHeights[index] <= this.element.scrollTop + this._visibleHeight()) { |
Erik Luo | 4b00232 | 2018-07-30 21:23:31 | [diff] [blame] | 715 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 716 | } |
| 717 | if (makeLast) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 718 | this.forceScrollItemToBeLast(index); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 719 | } else if (index <= firstVisibleIndex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 720 | this.forceScrollItemToBeFirst(index); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 721 | } else if (index >= lastVisibleIndex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 722 | this.forceScrollItemToBeLast(index); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 723 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 724 | } |
| 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 734 | if (this.element.isScrolledToBottom()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 735 | this.setStickToBottom(true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 736 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 737 | this.refresh(); |
Erik Luo | 4b00232 | 2018-07-30 21:23:31 | [diff] [blame] | 738 | // 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 740 | } |
| 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 750 | if (this.element.isScrolledToBottom()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 751 | this.setStickToBottom(true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame^] | 752 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 753 | this.refresh(); |
Erik Luo | 4b00232 | 2018-07-30 21:23:31 | [diff] [blame] | 754 | // 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 756 | } |
| 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 | */ |
| 770 | Console.ConsoleViewportProvider = function() {}; |
| 771 | |
| 772 | Console.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 | */ |
| 807 | Console.ConsoleViewportElement = function() {}; |
| 808 | Console.ConsoleViewportElement.prototype = { |
| 809 | willHide() {}, |
| 810 | |
| 811 | wasShown() {}, |
| 812 | |
| 813 | /** |
| 814 | * @return {!Element} |
| 815 | */ |
| 816 | element() {}, |
| 817 | }; |