blob: 96f5e5fcdebb2257fad7f8442a9a3fe0420b2e0d [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// 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 Lippe97611c92020-02-12 16:56:584
5import * as SDK from '../sdk/sdk.js'; // eslint-disable-line no-unused-vars
6
Patrick Brossetd93ee762020-10-05 12:33:287import {StylePropertyTreeElement} from './StylePropertyTreeElement.js'; // eslint-disable-line no-unused-vars
8import {StylesSidebarPane} from './StylesSidebarPane.js'; // eslint-disable-line no-unused-vars
Blink Reformat4c46d092018-04-07 15:32:379
10/**
11 * @unrestricted
12 */
Tim van der Lippeaabc8302019-12-10 15:34:4513export class StylePropertyHighlighter {
Blink Reformat4c46d092018-04-07 15:32:3714 /**
Tim van der Lippeaabc8302019-12-10 15:34:4515 * @param {!StylesSidebarPane} ssp
Blink Reformat4c46d092018-04-07 15:32:3716 */
Patrick Brossetd93ee762020-10-05 12:33:2817 constructor(ssp) {
Blink Reformat4c46d092018-04-07 15:32:3718 this._styleSidebarPane = ssp;
Blink Reformat4c46d092018-04-07 15:32:3719 }
20
Patrick Brossetd93ee762020-10-05 12:33:2821 /**
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 Reformat4c46d092018-04-07 15:32:3726 // Expand all shorthands.
27 for (const section of this._styleSidebarPane.allSections()) {
28 for (let treeElement = section.propertiesTreeOutline.firstChild(); treeElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:3429 treeElement = treeElement.nextSibling) {
Blink Reformat4c46d092018-04-07 15:32:3730 treeElement.onpopulate();
Tim van der Lippe1d6e57a2019-09-30 11:55:3431 }
Blink Reformat4c46d092018-04-07 15:32:3732 }
Patrick Brossetd93ee762020-10-05 12:33:2833
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 Reformat4c46d092018-04-07 15:32:3760 for (const section of this._styleSidebarPane.allSections()) {
61 let treeElement = section.propertiesTreeOutline.firstChild();
Alfonso CastaƱo0fef99f2020-10-23 08:30:2162 while (treeElement && !result && (treeElement instanceof StylePropertyTreeElement)) {
Patrick Brossetd93ee762020-10-05 12:33:2863 if (compareCb(treeElement)) {
64 result = treeElement;
Blink Reformat4c46d092018-04-07 15:32:3765 break;
66 }
67 treeElement = treeElement.traverseNextTreeElement(false, null, true);
68 }
Patrick Brossetd93ee762020-10-05 12:33:2869 if (result) {
Blink Reformat4c46d092018-04-07 15:32:3770 break;
Tim van der Lippe1d6e57a2019-09-30 11:55:3471 }
Blink Reformat4c46d092018-04-07 15:32:3772 }
Patrick Brossetd93ee762020-10-05 12:33:2873 return result;
74 }
75
76 /**
77 * @param {!StylePropertyTreeElement} treeElement
78 */
79 _scrollAndHighlightTreeElement(treeElement) {
80 treeElement.listItemElement.scrollIntoViewIfNeeded();
81 treeElement.listItemElement.animate(
Blink Reformat4c46d092018-04-07 15:32:3782 [
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 Lippe13f71fb2019-11-29 11:17:3988}