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