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 Common from '../common/common.js'; |
Simon Zünd | da7058f | 2020-02-28 13:57:28 | [diff] [blame] | 6 | import * as Platform from '../platform/platform.js'; |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 7 | import * as SDK from '../sdk/sdk.js'; |
| 8 | import * as UI from '../ui/ui.js'; |
| 9 | |
Tim van der Lippe | aabc830 | 2019-12-10 15:34:45 | [diff] [blame] | 10 | import {ElementsPanel} from './ElementsPanel.js'; |
| 11 | |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 12 | export class ClassesPaneWidget extends UI.Widget.Widget { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 13 | constructor() { |
| 14 | super(true); |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame] | 15 | this.registerRequiredCSS('elements/classesPaneWidget.css', {enableLegacyPatching: true}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 16 | this.contentElement.className = 'styles-element-classes-pane'; |
| 17 | const container = this.contentElement.createChild('div', 'title-container'); |
| 18 | this._input = container.createChild('div', 'new-class-input monospace'); |
| 19 | this.setDefaultFocusedElement(this._input); |
| 20 | this._classesContainer = this.contentElement.createChild('div', 'source-code'); |
| 21 | this._classesContainer.classList.add('styles-element-classes-container'); |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 22 | this._prompt = new ClassNamePrompt(this._nodeClasses.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 23 | this._prompt.setAutocompletionTimeout(0); |
| 24 | this._prompt.renderAsBlock(); |
| 25 | |
Tim van der Lippe | bcd6b5c | 2021-01-13 12:31:51 | [diff] [blame^] | 26 | const proxyElement = /** @type {!HTMLElement} */ (this._prompt.attach(this._input)); |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 27 | this._prompt.setPlaceholder(Common.UIString.UIString('Add new class')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 28 | this._prompt.addEventListener(UI.TextPrompt.Events.TextChanged, this._onTextChanged, this); |
| 29 | proxyElement.addEventListener('keydown', this._onKeyDown.bind(this), false); |
| 30 | |
Paul Lewis | daac106 | 2020-03-05 14:37:10 | [diff] [blame] | 31 | SDK.SDKModel.TargetManager.instance().addModelListener( |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 32 | SDK.DOMModel.DOMModel, SDK.DOMModel.Events.DOMMutated, this._onDOMMutated, this); |
| 33 | /** @type {!Set<!SDK.DOMModel.DOMNode>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 34 | this._mutatingNodes = new Set(); |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 35 | /** @type {!Map<!SDK.DOMModel.DOMNode, string>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 36 | this._pendingNodeClasses = new Map(); |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 37 | this._updateNodeThrottler = new Common.Throttler.Throttler(0); |
| 38 | /** @type {?SDK.DOMModel.DOMNode} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 39 | this._previousTarget = null; |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 40 | UI.Context.Context.instance().addFlavorChangeListener(SDK.DOMModel.DOMNode, this._onSelectedNodeChanged, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @param {string} text |
| 45 | * @return {!Array.<string>} |
| 46 | */ |
| 47 | _splitTextIntoClasses(text) { |
Mathias Bynens | 3abc095 | 2020-04-20 14:15:52 | [diff] [blame] | 48 | return text.split(/[,\s]/).map(className => className.trim()).filter(className => className.length); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
Tim van der Lippe | bcd6b5c | 2021-01-13 12:31:51 | [diff] [blame^] | 52 | * @param {!KeyboardEvent} event |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 53 | */ |
| 54 | _onKeyDown(event) { |
Tim van der Lippe | bcd6b5c | 2021-01-13 12:31:51 | [diff] [blame^] | 55 | if (!(event.key === 'Enter') && !isEscKey(event)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 56 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 57 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 58 | |
Tim van der Lippe | bcd6b5c | 2021-01-13 12:31:51 | [diff] [blame^] | 59 | if (event.key === 'Enter') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 60 | event.consume(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 61 | if (this._prompt.acceptAutoComplete()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 62 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 63 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 64 | } |
| 65 | |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 66 | const eventTarget = /** @type {!HTMLElement} */ (event.target); |
| 67 | let text = /** @type {string} */ (eventTarget.textContent); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 68 | if (isEscKey(event)) { |
Simon Zünd | da7058f | 2020-02-28 13:57:28 | [diff] [blame] | 69 | if (!Platform.StringUtilities.isWhitespace(text)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 70 | event.consume(true); |
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 | text = ''; |
| 73 | } |
| 74 | |
| 75 | this._prompt.clearAutocomplete(); |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 76 | eventTarget.textContent = ''; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 77 | |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 78 | const node = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 79 | if (!node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 80 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 81 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 82 | |
| 83 | const classNames = this._splitTextIntoClasses(text); |
Michael Liao | cafccfd | 2020-04-02 17:11:59 | [diff] [blame] | 84 | if (!classNames.length) { |
Patrick Brosset | 78fa1f5 | 2020-08-17 14:33:48 | [diff] [blame] | 85 | this._installNodeClasses(node); |
Michael Liao | cafccfd | 2020-04-02 17:11:59 | [diff] [blame] | 86 | return; |
| 87 | } |
| 88 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 89 | for (const className of classNames) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 90 | this._toggleClass(node, className, true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 91 | } |
Michael Liao | cafccfd | 2020-04-02 17:11:59 | [diff] [blame] | 92 | |
| 93 | // annoucementString is used for screen reader to announce that the class(es) has been added successfully. |
| 94 | const joinClassString = classNames.join(' '); |
| 95 | const announcementString = |
| 96 | classNames.length > 1 ? ls`Classes ${joinClassString} added.` : ls`Class ${joinClassString} added.`; |
| 97 | UI.ARIAUtils.alert(announcementString, this.contentElement); |
| 98 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 99 | this._installNodeClasses(node); |
| 100 | this._update(); |
| 101 | } |
| 102 | |
| 103 | _onTextChanged() { |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 104 | const node = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 105 | if (!node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 106 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 107 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 108 | this._installNodeClasses(node); |
| 109 | } |
| 110 | |
| 111 | /** |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame] | 112 | * @param {!Common.EventTarget.EventTargetEvent} event |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 113 | */ |
| 114 | _onDOMMutated(event) { |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 115 | const node = /** @type {!SDK.DOMModel.DOMNode} */ (event.data); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 116 | if (this._mutatingNodes.has(node)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 117 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 118 | } |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 119 | cachedClassesMap.delete(node); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 120 | this._update(); |
| 121 | } |
| 122 | |
| 123 | /** |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame] | 124 | * @param {!Common.EventTarget.EventTargetEvent} event |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 125 | */ |
| 126 | _onSelectedNodeChanged(event) { |
| 127 | if (this._previousTarget && this._prompt.text()) { |
| 128 | this._input.textContent = ''; |
| 129 | this._installNodeClasses(this._previousTarget); |
| 130 | } |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 131 | this._previousTarget = /** @type {?SDK.DOMModel.DOMNode} */ (event.data); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 132 | this._update(); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @override |
| 137 | */ |
| 138 | wasShown() { |
| 139 | this._update(); |
| 140 | } |
| 141 | |
| 142 | _update() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 143 | if (!this.isShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 144 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 145 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 146 | |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 147 | let node = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 148 | if (node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 149 | node = node.enclosingElementOrSelf(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 150 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 151 | |
| 152 | this._classesContainer.removeChildren(); |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 153 | // @ts-ignore this._input is a div, not an input element. So this line makes no sense at all |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 154 | this._input.disabled = !node; |
| 155 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 156 | if (!node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 157 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 158 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 159 | |
| 160 | const classes = this._nodeClasses(node); |
Simon Zünd | f27be3d | 2020-02-11 14:46:27 | [diff] [blame] | 161 | const keys = [...classes.keys()]; |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 162 | keys.sort(Platform.StringUtilities.caseInsensetiveComparator); |
| 163 | for (const className of keys) { |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 164 | const label = UI.UIUtils.CheckboxLabel.create(className, classes.get(className)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 165 | label.classList.add('monospace'); |
| 166 | label.checkboxElement.addEventListener('click', this._onClick.bind(this, className), false); |
| 167 | this._classesContainer.appendChild(label); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @param {string} className |
| 173 | * @param {!Event} event |
| 174 | */ |
| 175 | _onClick(className, event) { |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 176 | const node = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 177 | if (!node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 178 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 179 | } |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 180 | const enabled = /** @type {!HTMLInputElement} */ (event.target).checked; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 181 | this._toggleClass(node, className, enabled); |
| 182 | this._installNodeClasses(node); |
| 183 | } |
| 184 | |
| 185 | /** |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 186 | * @param {!SDK.DOMModel.DOMNode} node |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 187 | * @return {!Map<string, boolean>} |
| 188 | */ |
| 189 | _nodeClasses(node) { |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 190 | let result = cachedClassesMap.get(node); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 191 | if (!result) { |
| 192 | const classAttribute = node.getAttribute('class') || ''; |
| 193 | const classes = classAttribute.split(/\s/); |
| 194 | result = new Map(); |
| 195 | for (let i = 0; i < classes.length; ++i) { |
| 196 | const className = classes[i].trim(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 197 | if (!className.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 198 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 199 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 200 | result.set(className, true); |
| 201 | } |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 202 | cachedClassesMap.set(node, result); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 203 | } |
| 204 | return result; |
| 205 | } |
| 206 | |
| 207 | /** |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 208 | * @param {!SDK.DOMModel.DOMNode} node |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 209 | * @param {string} className |
| 210 | * @param {boolean} enabled |
| 211 | */ |
| 212 | _toggleClass(node, className, enabled) { |
| 213 | const classes = this._nodeClasses(node); |
| 214 | classes.set(className, enabled); |
| 215 | } |
| 216 | |
| 217 | /** |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 218 | * @param {!SDK.DOMModel.DOMNode} node |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 219 | */ |
| 220 | _installNodeClasses(node) { |
| 221 | const classes = this._nodeClasses(node); |
| 222 | const activeClasses = new Set(); |
| 223 | for (const className of classes.keys()) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 224 | if (classes.get(className)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 225 | activeClasses.add(className); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 226 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | const additionalClasses = this._splitTextIntoClasses(this._prompt.textWithCurrentSuggestion()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 230 | for (const className of additionalClasses) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 231 | activeClasses.add(className); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 232 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 233 | |
Simon Zünd | a0d4062 | 2020-02-12 13:16:42 | [diff] [blame] | 234 | const newClasses = [...activeClasses.values()].sort(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 235 | |
| 236 | this._pendingNodeClasses.set(node, newClasses.join(' ')); |
| 237 | this._updateNodeThrottler.schedule(this._flushPendingClasses.bind(this)); |
| 238 | } |
| 239 | |
| 240 | /** |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 241 | * @return {!Promise<void>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 242 | */ |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 243 | async _flushPendingClasses() { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 244 | const promises = []; |
| 245 | for (const node of this._pendingNodeClasses.keys()) { |
| 246 | this._mutatingNodes.add(node); |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 247 | const promise = node.setAttributeValuePromise('class', /** @type {string} */ (this._pendingNodeClasses.get(node))) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 248 | .then(onClassValueUpdated.bind(this, node)); |
| 249 | promises.push(promise); |
| 250 | } |
| 251 | this._pendingNodeClasses.clear(); |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 252 | await Promise.all(promises); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 253 | |
| 254 | /** |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 255 | * @param {!SDK.DOMModel.DOMNode} node |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 256 | * @this {ClassesPaneWidget} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 257 | */ |
| 258 | function onClassValueUpdated(node) { |
| 259 | this._mutatingNodes.delete(node); |
| 260 | } |
| 261 | } |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 262 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 263 | |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 264 | /** @type {!WeakMap<!SDK.DOMModel.DOMNode, !Map<string, boolean>>} */ |
| 265 | const cachedClassesMap = new WeakMap(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 266 | |
| 267 | /** |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 268 | * @implements {UI.Toolbar.Provider} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 269 | */ |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 270 | export class ButtonProvider { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 271 | constructor() { |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 272 | this._button = new UI.Toolbar.ToolbarToggle(Common.UIString.UIString('Element Classes'), ''); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 273 | this._button.setText('.cls'); |
| 274 | this._button.element.classList.add('monospace'); |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 275 | this._button.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this._clicked, this); |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 276 | this._view = new ClassesPaneWidget(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | _clicked() { |
Tim van der Lippe | aabc830 | 2019-12-10 15:34:45 | [diff] [blame] | 280 | ElementsPanel.instance().showToolbarPane(!this._view.isShowing() ? this._view : null, this._button); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
| 284 | * @override |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 285 | * @return {!UI.Toolbar.ToolbarItem} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 286 | */ |
| 287 | item() { |
| 288 | return this._button; |
| 289 | } |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 290 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 291 | |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 292 | export class ClassNamePrompt extends UI.TextPrompt.TextPrompt { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 293 | /** |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 294 | * @param {function(!SDK.DOMModel.DOMNode):!Map<string, boolean>} nodeClasses |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 295 | */ |
| 296 | constructor(nodeClasses) { |
| 297 | super(); |
| 298 | this._nodeClasses = nodeClasses; |
| 299 | this.initialize(this._buildClassNameCompletions.bind(this), ' '); |
| 300 | this.disableDefaultSuggestionForEmptyInput(); |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 301 | /** @type {?string} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 302 | this._selectedFrameId = ''; |
| 303 | this._classNamesPromise = null; |
| 304 | } |
| 305 | |
| 306 | /** |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 307 | * @param {!SDK.DOMModel.DOMNode} selectedNode |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 308 | * @return {!Promise.<!Array.<string>>} |
| 309 | */ |
Mathias Bynens | 3abc095 | 2020-04-20 14:15:52 | [diff] [blame] | 310 | async _getClassNames(selectedNode) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 311 | const promises = []; |
| 312 | const completions = new Set(); |
| 313 | this._selectedFrameId = selectedNode.frameId(); |
| 314 | |
| 315 | const cssModel = selectedNode.domModel().cssModel(); |
| 316 | const allStyleSheets = cssModel.allStyleSheets(); |
| 317 | for (const stylesheet of allStyleSheets) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 318 | if (stylesheet.frameId !== this._selectedFrameId) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 319 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 320 | } |
Mathias Bynens | 3abc095 | 2020-04-20 14:15:52 | [diff] [blame] | 321 | const cssPromise = cssModel.classNamesPromise(stylesheet.id).then(classes => { |
| 322 | for (const className of classes) { |
| 323 | completions.add(className); |
| 324 | } |
| 325 | }); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 326 | promises.push(cssPromise); |
| 327 | } |
| 328 | |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 329 | const ownerDocumentId = /** @type {number} */ ( |
| 330 | /** @type {!SDK.DOMModel.DOMDocument} */ (selectedNode.ownerDocument).id); |
| 331 | |
| 332 | const domPromise = selectedNode.domModel().classNamesPromise(ownerDocumentId).then(classes => { |
Mathias Bynens | 3abc095 | 2020-04-20 14:15:52 | [diff] [blame] | 333 | for (const className of classes) { |
| 334 | completions.add(className); |
| 335 | } |
| 336 | }); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 337 | promises.push(domPromise); |
Mathias Bynens | 3abc095 | 2020-04-20 14:15:52 | [diff] [blame] | 338 | await Promise.all(promises); |
| 339 | return [...completions]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /** |
| 343 | * @param {string} expression |
| 344 | * @param {string} prefix |
| 345 | * @param {boolean=} force |
| 346 | * @return {!Promise<!UI.SuggestBox.Suggestions>} |
| 347 | */ |
Mathias Bynens | 41ea263 | 2020-12-24 05:52:49 | [diff] [blame] | 348 | async _buildClassNameCompletions(expression, prefix, force) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 349 | if (!prefix || force) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 350 | this._classNamesPromise = null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 351 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 352 | |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 353 | const selectedNode = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 354 | if (!selectedNode || (!prefix && !force && !expression.trim())) { |
Mathias Bynens | 41ea263 | 2020-12-24 05:52:49 | [diff] [blame] | 355 | return []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 356 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 357 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 358 | if (!this._classNamesPromise || this._selectedFrameId !== selectedNode.frameId()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 359 | this._classNamesPromise = this._getClassNames(selectedNode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 360 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 361 | |
Mathias Bynens | 41ea263 | 2020-12-24 05:52:49 | [diff] [blame] | 362 | let completions = await this._classNamesPromise; |
| 363 | const classesMap = this._nodeClasses(/** @type {!SDK.DOMModel.DOMNode} */ (selectedNode)); |
| 364 | completions = completions.filter(value => !classesMap.get(value)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 365 | |
Mathias Bynens | 41ea263 | 2020-12-24 05:52:49 | [diff] [blame] | 366 | if (prefix[0] === '.') { |
| 367 | completions = completions.map(value => '.' + value); |
| 368 | } |
| 369 | return completions.filter(value => value.startsWith(prefix)).sort().map(completion => { |
| 370 | return { |
| 371 | text: completion, |
| 372 | title: undefined, |
| 373 | subtitle: undefined, |
| 374 | iconType: undefined, |
| 375 | priority: undefined, |
| 376 | isSecondary: undefined, |
| 377 | subtitleRenderer: undefined, |
| 378 | selectionRange: undefined, |
| 379 | hideGhostText: undefined, |
| 380 | iconElement: undefined, |
| 381 | }; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 382 | }); |
| 383 | } |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 384 | } |