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