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