Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 4 | |
| 5 | import * as SDK from '../sdk/sdk.js'; // eslint-disable-line no-unused-vars |
| 6 | |
Patrick Brosset | d93ee76 | 2020-10-05 12:33:28 | [diff] [blame] | 7 | import {StylePropertyTreeElement} from './StylePropertyTreeElement.js'; // eslint-disable-line no-unused-vars |
| 8 | import {StylesSidebarPane} from './StylesSidebarPane.js'; // eslint-disable-line no-unused-vars |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * @unrestricted |
| 12 | */ |
Tim van der Lippe | aabc830 | 2019-12-10 15:34:45 | [diff] [blame] | 13 | export class StylePropertyHighlighter { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 14 | /** |
Tim van der Lippe | aabc830 | 2019-12-10 15:34:45 | [diff] [blame] | 15 | * @param {!StylesSidebarPane} ssp |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 16 | */ |
Patrick Brosset | d93ee76 | 2020-10-05 12:33:28 | [diff] [blame] | 17 | constructor(ssp) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 18 | this._styleSidebarPane = ssp; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 19 | } |
| 20 | |
Patrick Brosset | d93ee76 | 2020-10-05 12:33:28 | [diff] [blame] | 21 | /** |
| 22 | * Expand all shorthands, find the given property, scroll to it and highlight it. |
| 23 | * @param {!SDK.CSSProperty.CSSProperty} cssProperty |
| 24 | */ |
| 25 | highlightProperty(cssProperty) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 26 | // Expand all shorthands. |
| 27 | for (const section of this._styleSidebarPane.allSections()) { |
| 28 | for (let treeElement = section.propertiesTreeOutline.firstChild(); treeElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 29 | treeElement = treeElement.nextSibling) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 30 | treeElement.onpopulate(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 31 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 32 | } |
Patrick Brosset | d93ee76 | 2020-10-05 12:33:28 | [diff] [blame] | 33 | |
| 34 | const treeElement = this._findTreeElement(treeElement => treeElement.property === cssProperty); |
| 35 | if (treeElement) { |
| 36 | treeElement.parent.expand(); |
| 37 | this._scrollAndHighlightTreeElement(treeElement); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Find the first property that matches the provided name, scroll to it and highlight it. |
| 43 | * @param {string} propertyName |
| 44 | */ |
| 45 | findAndHighlightPropertyName(propertyName) { |
| 46 | const treeElement = this._findTreeElement(treeElement => treeElement.property.name === propertyName); |
| 47 | if (treeElement) { |
| 48 | this._scrollAndHighlightTreeElement(treeElement); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Traverse the styles pane tree, execute the provided callback for every tree element found, and |
| 54 | * return the first tree element for which the callback returns a truthy value. |
| 55 | * @param {function(!StylePropertyTreeElement):boolean} compareCb |
| 56 | * @return {?StylePropertyTreeElement} |
| 57 | */ |
| 58 | _findTreeElement(compareCb) { |
| 59 | let result = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 60 | for (const section of this._styleSidebarPane.allSections()) { |
| 61 | let treeElement = section.propertiesTreeOutline.firstChild(); |
Alfonso CastaƱo | 0fef99f | 2020-10-23 08:30:21 | [diff] [blame] | 62 | while (treeElement && !result && (treeElement instanceof StylePropertyTreeElement)) { |
Patrick Brosset | d93ee76 | 2020-10-05 12:33:28 | [diff] [blame] | 63 | if (compareCb(treeElement)) { |
| 64 | result = treeElement; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 65 | break; |
| 66 | } |
| 67 | treeElement = treeElement.traverseNextTreeElement(false, null, true); |
| 68 | } |
Patrick Brosset | d93ee76 | 2020-10-05 12:33:28 | [diff] [blame] | 69 | if (result) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 70 | break; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 71 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 72 | } |
Patrick Brosset | d93ee76 | 2020-10-05 12:33:28 | [diff] [blame] | 73 | return result; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param {!StylePropertyTreeElement} treeElement |
| 78 | */ |
| 79 | _scrollAndHighlightTreeElement(treeElement) { |
| 80 | treeElement.listItemElement.scrollIntoViewIfNeeded(); |
| 81 | treeElement.listItemElement.animate( |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 82 | [ |
| 83 | {offset: 0, backgroundColor: 'rgba(255, 255, 0, 0.2)'}, |
| 84 | {offset: 0.1, backgroundColor: 'rgba(255, 255, 0, 0.7)'}, {offset: 1, backgroundColor: 'transparent'} |
| 85 | ], |
| 86 | {duration: 2000, easing: 'cubic-bezier(0, 0, 0.2, 1)'}); |
| 87 | } |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 88 | } |