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 | |
| 54 | this._provider = provider; |
| 55 | this.element.addEventListener('scroll', this._onScroll.bind(this), false); |
| 56 | this.element.addEventListener('copy', this._onCopy.bind(this), false); |
| 57 | this.element.addEventListener('dragstart', this._onDragStart.bind(this), false); |
| 58 | |
| 59 | this._firstActiveIndex = -1; |
| 60 | this._lastActiveIndex = -1; |
| 61 | this._renderedItems = []; |
| 62 | this._anchorSelection = null; |
| 63 | this._headSelection = null; |
| 64 | this._itemCount = 0; |
| 65 | this._cumulativeHeights = new Int32Array(0); |
| 66 | this._muteCopyHandler = false; |
| 67 | |
| 68 | // Listen for any changes to descendants and trigger a refresh. This ensures |
| 69 | // that items updated asynchronously will not break stick-to-bottom behavior |
| 70 | // if they change the scroll height. |
| 71 | this._observer = new MutationObserver(this.refresh.bind(this)); |
| 72 | this._observerConfig = {childList: true, subtree: true}; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @return {boolean} |
| 77 | */ |
| 78 | stickToBottom() { |
| 79 | return this._stickToBottom; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param {boolean} value |
| 84 | */ |
| 85 | setStickToBottom(value) { |
| 86 | this._stickToBottom = value; |
| 87 | if (this._stickToBottom) |
| 88 | this._observer.observe(this._contentElement, this._observerConfig); |
| 89 | else |
| 90 | this._observer.disconnect(); |
| 91 | } |
| 92 | |
| 93 | copyWithStyles() { |
| 94 | this._muteCopyHandler = true; |
| 95 | this.element.ownerDocument.execCommand('copy'); |
| 96 | this._muteCopyHandler = false; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @param {!Event} event |
| 101 | */ |
| 102 | _onCopy(event) { |
| 103 | if (this._muteCopyHandler) |
| 104 | return; |
| 105 | const text = this._selectedText(); |
| 106 | if (!text) |
| 107 | return; |
| 108 | event.preventDefault(); |
| 109 | event.clipboardData.setData('text/plain', text); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param {!Event} event |
| 114 | */ |
| 115 | _onDragStart(event) { |
| 116 | const text = this._selectedText(); |
| 117 | if (!text) |
| 118 | return false; |
| 119 | event.dataTransfer.clearData(); |
| 120 | event.dataTransfer.setData('text/plain', text); |
| 121 | event.dataTransfer.effectAllowed = 'copy'; |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @return {!Element} |
| 127 | */ |
| 128 | contentElement() { |
| 129 | return this._contentElement; |
| 130 | } |
| 131 | |
| 132 | invalidate() { |
| 133 | delete this._cachedProviderElements; |
| 134 | this._itemCount = this._provider.itemCount(); |
| 135 | this._rebuildCumulativeHeights(); |
| 136 | this.refresh(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @param {number} index |
| 141 | * @return {?Console.ConsoleViewportElement} |
| 142 | */ |
| 143 | _providerElement(index) { |
| 144 | if (!this._cachedProviderElements) |
| 145 | this._cachedProviderElements = new Array(this._itemCount); |
| 146 | let element = this._cachedProviderElements[index]; |
| 147 | if (!element) { |
| 148 | element = this._provider.itemElement(index); |
| 149 | this._cachedProviderElements[index] = element; |
| 150 | } |
| 151 | return element; |
| 152 | } |
| 153 | |
| 154 | _rebuildCumulativeHeights() { |
| 155 | const firstActiveIndex = this._firstActiveIndex; |
| 156 | const lastActiveIndex = this._lastActiveIndex; |
| 157 | let height = 0; |
| 158 | this._cumulativeHeights = new Int32Array(this._itemCount); |
| 159 | for (let i = 0; i < this._itemCount; ++i) { |
| 160 | if (firstActiveIndex <= i && i - firstActiveIndex < this._renderedItems.length && i <= lastActiveIndex) |
| 161 | height += this._renderedItems[i - firstActiveIndex].element().offsetHeight; |
| 162 | else |
| 163 | height += this._provider.fastHeight(i); |
| 164 | this._cumulativeHeights[i] = height; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | _rebuildCumulativeHeightsIfNeeded() { |
Erik Luo | 4b00232 | 2018-07-30 21:23:31 | [diff] [blame^] | 169 | let totalCachedHeight = 0; |
| 170 | let totalMeasuredHeight = 0; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 171 | // Check whether current items in DOM have changed heights. Tolerate 1-pixel |
| 172 | // error due to double-to-integer rounding errors. |
| 173 | for (let i = 0; i < this._renderedItems.length; ++i) { |
| 174 | const cachedItemHeight = this._cachedItemHeight(this._firstActiveIndex + i); |
Erik Luo | 4b00232 | 2018-07-30 21:23:31 | [diff] [blame^] | 175 | const measuredHeight = this._renderedItems[i].element().offsetHeight; |
| 176 | if (Math.abs(cachedItemHeight - measuredHeight) > 1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 177 | this._rebuildCumulativeHeights(); |
Erik Luo | 4b00232 | 2018-07-30 21:23:31 | [diff] [blame^] | 178 | return; |
| 179 | } |
| 180 | totalMeasuredHeight += measuredHeight; |
| 181 | totalCachedHeight += cachedItemHeight; |
| 182 | if (Math.abs(totalCachedHeight - totalMeasuredHeight) > 1) { |
| 183 | this._rebuildCumulativeHeights(); |
| 184 | return; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @param {number} index |
| 191 | * @return {number} |
| 192 | */ |
| 193 | _cachedItemHeight(index) { |
| 194 | return index === 0 ? this._cumulativeHeights[0] : |
| 195 | this._cumulativeHeights[index] - this._cumulativeHeights[index - 1]; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @param {?Selection} selection |
| 200 | * @suppressGlobalPropertiesCheck |
| 201 | */ |
| 202 | _isSelectionBackwards(selection) { |
| 203 | if (!selection || !selection.rangeCount) |
| 204 | return false; |
| 205 | const range = document.createRange(); |
| 206 | range.setStart(selection.anchorNode, selection.anchorOffset); |
| 207 | range.setEnd(selection.focusNode, selection.focusOffset); |
| 208 | return range.collapsed; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * @param {number} itemIndex |
| 213 | * @param {!Node} node |
| 214 | * @param {number} offset |
| 215 | * @return {!{item: number, node: !Node, offset: number}} |
| 216 | */ |
| 217 | _createSelectionModel(itemIndex, node, offset) { |
| 218 | return {item: itemIndex, node: node, offset: offset}; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @param {?Selection} selection |
| 223 | */ |
| 224 | _updateSelectionModel(selection) { |
| 225 | const range = selection && selection.rangeCount ? selection.getRangeAt(0) : null; |
| 226 | if (!range || selection.isCollapsed || !this.element.hasSelection()) { |
| 227 | this._headSelection = null; |
| 228 | this._anchorSelection = null; |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | let firstSelected = Number.MAX_VALUE; |
| 233 | let lastSelected = -1; |
| 234 | |
| 235 | let hasVisibleSelection = false; |
| 236 | for (let i = 0; i < this._renderedItems.length; ++i) { |
| 237 | if (range.intersectsNode(this._renderedItems[i].element())) { |
| 238 | const index = i + this._firstActiveIndex; |
| 239 | firstSelected = Math.min(firstSelected, index); |
| 240 | lastSelected = Math.max(lastSelected, index); |
| 241 | hasVisibleSelection = true; |
| 242 | } |
| 243 | } |
| 244 | if (hasVisibleSelection) { |
| 245 | firstSelected = |
| 246 | this._createSelectionModel(firstSelected, /** @type {!Node} */ (range.startContainer), range.startOffset); |
| 247 | lastSelected = |
| 248 | this._createSelectionModel(lastSelected, /** @type {!Node} */ (range.endContainer), range.endOffset); |
| 249 | } |
| 250 | const topOverlap = range.intersectsNode(this._topGapElement) && this._topGapElement._active; |
| 251 | const bottomOverlap = range.intersectsNode(this._bottomGapElement) && this._bottomGapElement._active; |
| 252 | if (!topOverlap && !bottomOverlap && !hasVisibleSelection) { |
| 253 | this._headSelection = null; |
| 254 | this._anchorSelection = null; |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | if (!this._anchorSelection || !this._headSelection) { |
| 259 | this._anchorSelection = this._createSelectionModel(0, this.element, 0); |
| 260 | this._headSelection = this._createSelectionModel(this._itemCount - 1, this.element, this.element.children.length); |
| 261 | this._selectionIsBackward = false; |
| 262 | } |
| 263 | |
| 264 | const isBackward = this._isSelectionBackwards(selection); |
| 265 | const startSelection = this._selectionIsBackward ? this._headSelection : this._anchorSelection; |
| 266 | const endSelection = this._selectionIsBackward ? this._anchorSelection : this._headSelection; |
| 267 | if (topOverlap && bottomOverlap && hasVisibleSelection) { |
| 268 | firstSelected = firstSelected.item < startSelection.item ? firstSelected : startSelection; |
| 269 | lastSelected = lastSelected.item > endSelection.item ? lastSelected : endSelection; |
| 270 | } else if (!hasVisibleSelection) { |
| 271 | firstSelected = startSelection; |
| 272 | lastSelected = endSelection; |
| 273 | } else if (topOverlap) { |
| 274 | firstSelected = isBackward ? this._headSelection : this._anchorSelection; |
| 275 | } else if (bottomOverlap) { |
| 276 | lastSelected = isBackward ? this._anchorSelection : this._headSelection; |
| 277 | } |
| 278 | |
| 279 | if (isBackward) { |
| 280 | this._anchorSelection = lastSelected; |
| 281 | this._headSelection = firstSelected; |
| 282 | } else { |
| 283 | this._anchorSelection = firstSelected; |
| 284 | this._headSelection = lastSelected; |
| 285 | } |
| 286 | this._selectionIsBackward = isBackward; |
| 287 | return true; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * @param {?Selection} selection |
| 292 | */ |
| 293 | _restoreSelection(selection) { |
| 294 | let anchorElement = null; |
| 295 | let anchorOffset; |
| 296 | if (this._firstActiveIndex <= this._anchorSelection.item && this._anchorSelection.item <= this._lastActiveIndex) { |
| 297 | anchorElement = this._anchorSelection.node; |
| 298 | anchorOffset = this._anchorSelection.offset; |
| 299 | } else { |
| 300 | if (this._anchorSelection.item < this._firstActiveIndex) |
| 301 | anchorElement = this._topGapElement; |
| 302 | else if (this._anchorSelection.item > this._lastActiveIndex) |
| 303 | anchorElement = this._bottomGapElement; |
| 304 | anchorOffset = this._selectionIsBackward ? 1 : 0; |
| 305 | } |
| 306 | |
| 307 | let headElement = null; |
| 308 | let headOffset; |
| 309 | if (this._firstActiveIndex <= this._headSelection.item && this._headSelection.item <= this._lastActiveIndex) { |
| 310 | headElement = this._headSelection.node; |
| 311 | headOffset = this._headSelection.offset; |
| 312 | } else { |
| 313 | if (this._headSelection.item < this._firstActiveIndex) |
| 314 | headElement = this._topGapElement; |
| 315 | else if (this._headSelection.item > this._lastActiveIndex) |
| 316 | headElement = this._bottomGapElement; |
| 317 | headOffset = this._selectionIsBackward ? 0 : 1; |
| 318 | } |
| 319 | |
| 320 | selection.setBaseAndExtent(anchorElement, anchorOffset, headElement, headOffset); |
| 321 | } |
| 322 | |
| 323 | refresh() { |
| 324 | this._observer.disconnect(); |
| 325 | this._innerRefresh(); |
| 326 | if (this._stickToBottom) |
| 327 | this._observer.observe(this._contentElement, this._observerConfig); |
| 328 | } |
| 329 | |
| 330 | _innerRefresh() { |
| 331 | if (!this._visibleHeight()) |
| 332 | return; // Do nothing for invisible controls. |
| 333 | |
| 334 | if (!this._itemCount) { |
| 335 | for (let i = 0; i < this._renderedItems.length; ++i) |
| 336 | this._renderedItems[i].willHide(); |
| 337 | this._renderedItems = []; |
| 338 | this._contentElement.removeChildren(); |
| 339 | this._topGapElement.style.height = '0px'; |
| 340 | this._bottomGapElement.style.height = '0px'; |
| 341 | this._firstActiveIndex = -1; |
| 342 | this._lastActiveIndex = -1; |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | const selection = this.element.getComponentSelection(); |
| 347 | const shouldRestoreSelection = this._updateSelectionModel(selection); |
| 348 | |
| 349 | const visibleFrom = this.element.scrollTop; |
| 350 | const visibleHeight = this._visibleHeight(); |
| 351 | const activeHeight = visibleHeight * 2; |
| 352 | this._rebuildCumulativeHeightsIfNeeded(); |
| 353 | |
| 354 | // When the viewport is scrolled to the bottom, using the cumulative heights estimate is not |
| 355 | // precise enough to determine next visible indices. This stickToBottom check avoids extra |
| 356 | // calls to refresh in those cases. |
| 357 | if (this._stickToBottom) { |
| 358 | this._firstActiveIndex = |
| 359 | Math.max(this._itemCount - Math.ceil(activeHeight / this._provider.minimumRowHeight()), 0); |
| 360 | this._lastActiveIndex = this._itemCount - 1; |
| 361 | } else { |
| 362 | this._firstActiveIndex = |
| 363 | Math.max(this._cumulativeHeights.lowerBound(visibleFrom + 1 - (activeHeight - visibleHeight) / 2), 0); |
| 364 | // Proactively render more rows in case some of them will be collapsed without triggering refresh. @see crbug.com/390169 |
| 365 | this._lastActiveIndex = this._firstActiveIndex + Math.ceil(activeHeight / this._provider.minimumRowHeight()) - 1; |
| 366 | this._lastActiveIndex = Math.min(this._lastActiveIndex, this._itemCount - 1); |
| 367 | } |
| 368 | |
| 369 | const topGapHeight = this._cumulativeHeights[this._firstActiveIndex - 1] || 0; |
| 370 | const bottomGapHeight = |
| 371 | this._cumulativeHeights[this._cumulativeHeights.length - 1] - this._cumulativeHeights[this._lastActiveIndex]; |
| 372 | |
| 373 | /** |
| 374 | * @this {Console.ConsoleViewport} |
| 375 | */ |
| 376 | function prepare() { |
| 377 | this._topGapElement.style.height = topGapHeight + 'px'; |
| 378 | this._bottomGapElement.style.height = bottomGapHeight + 'px'; |
| 379 | this._topGapElement._active = !!topGapHeight; |
| 380 | this._bottomGapElement._active = !!bottomGapHeight; |
| 381 | this._contentElement.style.setProperty('height', '10000000px'); |
| 382 | } |
| 383 | |
| 384 | this._partialViewportUpdate(prepare.bind(this)); |
| 385 | this._contentElement.style.removeProperty('height'); |
| 386 | // Should be the last call in the method as it might force layout. |
| 387 | if (shouldRestoreSelection) |
| 388 | this._restoreSelection(selection); |
| 389 | if (this._stickToBottom) |
| 390 | this.element.scrollTop = 10000000; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * @param {function()} prepare |
| 395 | */ |
| 396 | _partialViewportUpdate(prepare) { |
| 397 | const itemsToRender = new Set(); |
| 398 | for (let i = this._firstActiveIndex; i <= this._lastActiveIndex; ++i) |
| 399 | itemsToRender.add(this._providerElement(i)); |
| 400 | const willBeHidden = this._renderedItems.filter(item => !itemsToRender.has(item)); |
| 401 | for (let i = 0; i < willBeHidden.length; ++i) |
| 402 | willBeHidden[i].willHide(); |
| 403 | prepare(); |
| 404 | for (let i = 0; i < willBeHidden.length; ++i) |
| 405 | willBeHidden[i].element().remove(); |
| 406 | |
| 407 | const wasShown = []; |
| 408 | let anchor = this._contentElement.firstChild; |
| 409 | for (const viewportElement of itemsToRender) { |
| 410 | const element = viewportElement.element(); |
| 411 | if (element !== anchor) { |
| 412 | const shouldCallWasShown = !element.parentElement; |
| 413 | if (shouldCallWasShown) |
| 414 | wasShown.push(viewportElement); |
| 415 | this._contentElement.insertBefore(element, anchor); |
| 416 | } else { |
| 417 | anchor = anchor.nextSibling; |
| 418 | } |
| 419 | } |
| 420 | for (let i = 0; i < wasShown.length; ++i) |
| 421 | wasShown[i].wasShown(); |
| 422 | this._renderedItems = Array.from(itemsToRender); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * @return {?string} |
| 427 | */ |
| 428 | _selectedText() { |
| 429 | this._updateSelectionModel(this.element.getComponentSelection()); |
| 430 | if (!this._headSelection || !this._anchorSelection) |
| 431 | return null; |
| 432 | |
| 433 | let startSelection = null; |
| 434 | let endSelection = null; |
| 435 | if (this._selectionIsBackward) { |
| 436 | startSelection = this._headSelection; |
| 437 | endSelection = this._anchorSelection; |
| 438 | } else { |
| 439 | startSelection = this._anchorSelection; |
| 440 | endSelection = this._headSelection; |
| 441 | } |
| 442 | |
| 443 | const textLines = []; |
| 444 | for (let i = startSelection.item; i <= endSelection.item; ++i) { |
| 445 | const element = this._providerElement(i).element(); |
| 446 | const lineContent = element.childTextNodes().map(Components.Linkifier.untruncatedNodeText).join(''); |
| 447 | textLines.push(lineContent); |
| 448 | } |
| 449 | |
| 450 | const endSelectionElement = this._providerElement(endSelection.item).element(); |
| 451 | if (endSelection.node && endSelection.node.isSelfOrDescendant(endSelectionElement)) { |
| 452 | const itemTextOffset = this._textOffsetInNode(endSelectionElement, endSelection.node, endSelection.offset); |
| 453 | textLines[textLines.length - 1] = textLines.peekLast().substring(0, itemTextOffset); |
| 454 | } |
| 455 | |
| 456 | const startSelectionElement = this._providerElement(startSelection.item).element(); |
| 457 | if (startSelection.node && startSelection.node.isSelfOrDescendant(startSelectionElement)) { |
| 458 | const itemTextOffset = this._textOffsetInNode(startSelectionElement, startSelection.node, startSelection.offset); |
| 459 | textLines[0] = textLines[0].substring(itemTextOffset); |
| 460 | } |
| 461 | |
| 462 | return textLines.join('\n'); |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * @param {!Element} itemElement |
| 467 | * @param {!Node} selectionNode |
| 468 | * @param {number} offset |
| 469 | * @return {number} |
| 470 | */ |
| 471 | _textOffsetInNode(itemElement, selectionNode, offset) { |
| 472 | // If the selectionNode is not a TextNode, we may need to convert a child offset into a character offset. |
| 473 | if (selectionNode.nodeType !== Node.TEXT_NODE) { |
| 474 | if (offset < selectionNode.childNodes.length) { |
| 475 | selectionNode = /** @type {!Node} */ (selectionNode.childNodes.item(offset)); |
| 476 | offset = 0; |
| 477 | } else { |
| 478 | offset = selectionNode.textContent.length; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | let chars = 0; |
| 483 | let node = itemElement; |
| 484 | while ((node = node.traverseNextNode(itemElement)) && node !== selectionNode) { |
| 485 | if (node.nodeType !== Node.TEXT_NODE || node.parentElement.nodeName === 'STYLE' || |
| 486 | node.parentElement.nodeName === 'SCRIPT') |
| 487 | continue; |
| 488 | chars += Components.Linkifier.untruncatedNodeText(node).length; |
| 489 | } |
| 490 | // If the selected node text was truncated, treat any non-zero offset as the full length. |
| 491 | const untruncatedContainerLength = Components.Linkifier.untruncatedNodeText(selectionNode).length; |
| 492 | if (offset > 0 && untruncatedContainerLength !== selectionNode.textContent.length) |
| 493 | offset = untruncatedContainerLength; |
| 494 | return chars + offset; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * @param {!Event} event |
| 499 | */ |
| 500 | _onScroll(event) { |
| 501 | this.refresh(); |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * @return {number} |
| 506 | */ |
| 507 | firstVisibleIndex() { |
| 508 | if (!this._cumulativeHeights.length) |
| 509 | return -1; |
| 510 | this._rebuildCumulativeHeightsIfNeeded(); |
| 511 | return this._cumulativeHeights.lowerBound(this.element.scrollTop + 1); |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * @return {number} |
| 516 | */ |
| 517 | lastVisibleIndex() { |
| 518 | if (!this._cumulativeHeights.length) |
| 519 | return -1; |
| 520 | this._rebuildCumulativeHeightsIfNeeded(); |
| 521 | const scrollBottom = this.element.scrollTop + this.element.clientHeight; |
| 522 | const right = this._itemCount - 1; |
| 523 | return this._cumulativeHeights.lowerBound(scrollBottom, undefined, undefined, right); |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * @return {?Element} |
| 528 | */ |
| 529 | renderedElementAt(index) { |
| 530 | if (index < this._firstActiveIndex) |
| 531 | return null; |
| 532 | if (index > this._lastActiveIndex) |
| 533 | return null; |
| 534 | return this._renderedItems[index - this._firstActiveIndex].element(); |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * @param {number} index |
| 539 | * @param {boolean=} makeLast |
| 540 | */ |
| 541 | scrollItemIntoView(index, makeLast) { |
| 542 | const firstVisibleIndex = this.firstVisibleIndex(); |
| 543 | const lastVisibleIndex = this.lastVisibleIndex(); |
| 544 | if (index > firstVisibleIndex && index < lastVisibleIndex) |
| 545 | return; |
Erik Luo | 4b00232 | 2018-07-30 21:23:31 | [diff] [blame^] | 546 | // If the prompt is visible, then the last item must be fully on screen. |
| 547 | if (index === lastVisibleIndex && this._cumulativeHeights[index] <= this.element.scrollTop + this._visibleHeight()) |
| 548 | return; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 549 | if (makeLast) |
| 550 | this.forceScrollItemToBeLast(index); |
| 551 | else if (index <= firstVisibleIndex) |
| 552 | this.forceScrollItemToBeFirst(index); |
| 553 | else if (index >= lastVisibleIndex) |
| 554 | this.forceScrollItemToBeLast(index); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * @param {number} index |
| 559 | */ |
| 560 | forceScrollItemToBeFirst(index) { |
| 561 | console.assert(index >= 0 && index < this._itemCount, 'Cannot scroll item at invalid index'); |
| 562 | this.setStickToBottom(false); |
| 563 | this._rebuildCumulativeHeightsIfNeeded(); |
| 564 | this.element.scrollTop = index > 0 ? this._cumulativeHeights[index - 1] : 0; |
| 565 | if (this.element.isScrolledToBottom()) |
| 566 | this.setStickToBottom(true); |
| 567 | this.refresh(); |
Erik Luo | 4b00232 | 2018-07-30 21:23:31 | [diff] [blame^] | 568 | // After refresh, the item is in DOM, but may not be visible (items above were larger than expected). |
| 569 | this.renderedElementAt(index).scrollIntoView(true /* alignTop */); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /** |
| 573 | * @param {number} index |
| 574 | */ |
| 575 | forceScrollItemToBeLast(index) { |
| 576 | console.assert(index >= 0 && index < this._itemCount, 'Cannot scroll item at invalid index'); |
| 577 | this.setStickToBottom(false); |
| 578 | this._rebuildCumulativeHeightsIfNeeded(); |
| 579 | this.element.scrollTop = this._cumulativeHeights[index] - this._visibleHeight(); |
| 580 | if (this.element.isScrolledToBottom()) |
| 581 | this.setStickToBottom(true); |
| 582 | this.refresh(); |
Erik Luo | 4b00232 | 2018-07-30 21:23:31 | [diff] [blame^] | 583 | // After refresh, the item is in DOM, but may not be visible (items above were larger than expected). |
| 584 | this.renderedElementAt(index).scrollIntoView(false /* alignTop */); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /** |
| 588 | * @return {number} |
| 589 | */ |
| 590 | _visibleHeight() { |
| 591 | // Use offsetHeight instead of clientHeight to avoid being affected by horizontal scroll. |
| 592 | return this.element.offsetHeight; |
| 593 | } |
| 594 | }; |
| 595 | |
| 596 | /** |
| 597 | * @interface |
| 598 | */ |
| 599 | Console.ConsoleViewportProvider = function() {}; |
| 600 | |
| 601 | Console.ConsoleViewportProvider.prototype = { |
| 602 | /** |
| 603 | * @param {number} index |
| 604 | * @return {number} |
| 605 | */ |
| 606 | fastHeight(index) { |
| 607 | return 0; |
| 608 | }, |
| 609 | |
| 610 | /** |
| 611 | * @return {number} |
| 612 | */ |
| 613 | itemCount() { |
| 614 | return 0; |
| 615 | }, |
| 616 | |
| 617 | /** |
| 618 | * @return {number} |
| 619 | */ |
| 620 | minimumRowHeight() { |
| 621 | return 0; |
| 622 | }, |
| 623 | |
| 624 | /** |
| 625 | * @param {number} index |
| 626 | * @return {?Console.ConsoleViewportElement} |
| 627 | */ |
| 628 | itemElement(index) { |
| 629 | return null; |
| 630 | } |
| 631 | }; |
| 632 | |
| 633 | /** |
| 634 | * @interface |
| 635 | */ |
| 636 | Console.ConsoleViewportElement = function() {}; |
| 637 | Console.ConsoleViewportElement.prototype = { |
| 638 | willHide() {}, |
| 639 | |
| 640 | wasShown() {}, |
| 641 | |
| 642 | /** |
| 643 | * @return {!Element} |
| 644 | */ |
| 645 | element() {}, |
| 646 | }; |