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); |
Jack Franklin | 84442c6 | 2021-06-28 15:02:06 | [diff] [blame] | 50 | this.registerRequiredCSS('panels/elements/classesPaneWidget.css'); |
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 { |
Kriti Sapra | b2b29f2 | 2021-06-29 12:59:56 | [diff] [blame^] | 155 | super.wasShown(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 156 | this._update(); |
| 157 | } |
| 158 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 159 | _update(): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 160 | if (!this.isShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 161 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 162 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 163 | |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 164 | let node = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 165 | if (node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 166 | node = node.enclosingElementOrSelf(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 167 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 168 | |
| 169 | this._classesContainer.removeChildren(); |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 170 | // @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] | 171 | this._input.disabled = !node; |
| 172 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 173 | if (!node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 174 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 175 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 176 | |
| 177 | const classes = this._nodeClasses(node); |
Simon Zünd | f27be3d | 2020-02-11 14:46:27 | [diff] [blame] | 178 | const keys = [...classes.keys()]; |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 179 | keys.sort(Platform.StringUtilities.caseInsensetiveComparator); |
| 180 | for (const className of keys) { |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 181 | const label = UI.UIUtils.CheckboxLabel.create(className, classes.get(className)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 182 | label.classList.add('monospace'); |
| 183 | label.checkboxElement.addEventListener('click', this._onClick.bind(this, className), false); |
| 184 | this._classesContainer.appendChild(label); |
| 185 | } |
| 186 | } |
| 187 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 188 | _onClick(className: string, event: Event): void { |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 189 | const node = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 190 | if (!node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 191 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 192 | } |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 193 | const enabled = (event.target as HTMLInputElement).checked; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 194 | this._toggleClass(node, className, enabled); |
| 195 | this._installNodeClasses(node); |
| 196 | } |
| 197 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 198 | _nodeClasses(node: SDK.DOMModel.DOMNode): Map<string, boolean> { |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 199 | let result = cachedClassesMap.get(node); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 200 | if (!result) { |
| 201 | const classAttribute = node.getAttribute('class') || ''; |
| 202 | const classes = classAttribute.split(/\s/); |
| 203 | result = new Map(); |
| 204 | for (let i = 0; i < classes.length; ++i) { |
| 205 | const className = classes[i].trim(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 206 | if (!className.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 207 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 208 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 209 | result.set(className, true); |
| 210 | } |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 211 | cachedClassesMap.set(node, result); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 212 | } |
| 213 | return result; |
| 214 | } |
| 215 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 216 | _toggleClass(node: SDK.DOMModel.DOMNode, className: string, enabled: boolean): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 217 | const classes = this._nodeClasses(node); |
| 218 | classes.set(className, enabled); |
| 219 | } |
| 220 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 221 | _installNodeClasses(node: SDK.DOMModel.DOMNode): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 222 | const classes = this._nodeClasses(node); |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 223 | const activeClasses = new Set<string>(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 224 | for (const className of classes.keys()) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 225 | if (classes.get(className)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 226 | activeClasses.add(className); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 227 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | const additionalClasses = this._splitTextIntoClasses(this._prompt.textWithCurrentSuggestion()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 231 | for (const className of additionalClasses) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 232 | activeClasses.add(className); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 233 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 234 | |
Simon Zünd | a0d4062 | 2020-02-12 13:16:42 | [diff] [blame] | 235 | const newClasses = [...activeClasses.values()].sort(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 236 | |
| 237 | this._pendingNodeClasses.set(node, newClasses.join(' ')); |
| 238 | this._updateNodeThrottler.schedule(this._flushPendingClasses.bind(this)); |
| 239 | } |
| 240 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 241 | async _flushPendingClasses(): Promise<void> { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 242 | const promises = []; |
| 243 | for (const node of this._pendingNodeClasses.keys()) { |
| 244 | this._mutatingNodes.add(node); |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 245 | const promise = node.setAttributeValuePromise('class', (this._pendingNodeClasses.get(node) as string)) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 246 | .then(onClassValueUpdated.bind(this, node)); |
| 247 | promises.push(promise); |
| 248 | } |
| 249 | this._pendingNodeClasses.clear(); |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 250 | await Promise.all(promises); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 251 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 252 | function onClassValueUpdated(this: ClassesPaneWidget, node: SDK.DOMModel.DOMNode): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 253 | this._mutatingNodes.delete(node); |
| 254 | } |
| 255 | } |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 256 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 257 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 258 | const cachedClassesMap = new WeakMap<SDK.DOMModel.DOMNode, Map<string, boolean>>(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 259 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 260 | let buttonProviderInstance: ButtonProvider; |
Andres Olivares | 43c2d9f | 2021-02-10 16:48:39 | [diff] [blame] | 261 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 262 | export class ButtonProvider implements UI.Toolbar.Provider { |
| 263 | _button: UI.Toolbar.ToolbarToggle; |
| 264 | _view: ClassesPaneWidget; |
| 265 | private constructor() { |
Vidal Guillermo Diazleal Ortega | 0cfa0ed | 2021-02-17 20:35:49 | [diff] [blame] | 266 | this._button = new UI.Toolbar.ToolbarToggle(i18nString(UIStrings.elementClasses), ''); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 267 | this._button.setText('.cls'); |
| 268 | this._button.element.classList.add('monospace'); |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 269 | this._button.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this._clicked, this); |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 270 | this._view = new ClassesPaneWidget(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 271 | } |
| 272 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 273 | static instance(opts: { |
| 274 | forceNew: boolean|null, |
| 275 | } = {forceNew: null}): ButtonProvider { |
Andres Olivares | 43c2d9f | 2021-02-10 16:48:39 | [diff] [blame] | 276 | const {forceNew} = opts; |
| 277 | if (!buttonProviderInstance || forceNew) { |
| 278 | buttonProviderInstance = new ButtonProvider(); |
| 279 | } |
| 280 | |
| 281 | return buttonProviderInstance; |
| 282 | } |
| 283 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 284 | _clicked(): void { |
Tim van der Lippe | aabc830 | 2019-12-10 15:34:45 | [diff] [blame] | 285 | ElementsPanel.instance().showToolbarPane(!this._view.isShowing() ? this._view : null, this._button); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 286 | } |
| 287 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 288 | item(): UI.Toolbar.ToolbarItem { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 289 | return this._button; |
| 290 | } |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 291 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 292 | |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 293 | export class ClassNamePrompt extends UI.TextPrompt.TextPrompt { |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 294 | _nodeClasses: (arg0: SDK.DOMModel.DOMNode) => Map<string, boolean>; |
| 295 | _selectedFrameId: string|null; |
| 296 | _classNamesPromise: Promise<string[]>|null; |
| 297 | constructor(nodeClasses: (arg0: SDK.DOMModel.DOMNode) => Map<string, boolean>) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 298 | super(); |
| 299 | this._nodeClasses = nodeClasses; |
| 300 | this.initialize(this._buildClassNameCompletions.bind(this), ' '); |
| 301 | this.disableDefaultSuggestionForEmptyInput(); |
| 302 | this._selectedFrameId = ''; |
| 303 | this._classNamesPromise = null; |
| 304 | } |
| 305 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 306 | async _getClassNames(selectedNode: SDK.DOMModel.DOMNode): Promise<string[]> { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 307 | const promises = []; |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 308 | const completions = new Set<string>(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 309 | this._selectedFrameId = selectedNode.frameId(); |
| 310 | |
| 311 | const cssModel = selectedNode.domModel().cssModel(); |
| 312 | const allStyleSheets = cssModel.allStyleSheets(); |
| 313 | for (const stylesheet of allStyleSheets) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 314 | if (stylesheet.frameId !== this._selectedFrameId) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 315 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 316 | } |
Mathias Bynens | 3abc095 | 2020-04-20 14:15:52 | [diff] [blame] | 317 | const cssPromise = cssModel.classNamesPromise(stylesheet.id).then(classes => { |
| 318 | for (const className of classes) { |
| 319 | completions.add(className); |
| 320 | } |
| 321 | }); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 322 | promises.push(cssPromise); |
| 323 | } |
| 324 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 325 | const ownerDocumentId = ((selectedNode.ownerDocument as SDK.DOMModel.DOMDocument).id); |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 326 | |
| 327 | const domPromise = selectedNode.domModel().classNamesPromise(ownerDocumentId).then(classes => { |
Mathias Bynens | 3abc095 | 2020-04-20 14:15:52 | [diff] [blame] | 328 | for (const className of classes) { |
| 329 | completions.add(className); |
| 330 | } |
| 331 | }); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 332 | promises.push(domPromise); |
Mathias Bynens | 3abc095 | 2020-04-20 14:15:52 | [diff] [blame] | 333 | await Promise.all(promises); |
| 334 | return [...completions]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 335 | } |
| 336 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 337 | async _buildClassNameCompletions(expression: string, prefix: string, force?: boolean): |
| 338 | Promise<UI.SuggestBox.Suggestions> { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 339 | if (!prefix || force) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 340 | this._classNamesPromise = null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 341 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 342 | |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 343 | const selectedNode = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 344 | if (!selectedNode || (!prefix && !force && !expression.trim())) { |
Mathias Bynens | 41ea263 | 2020-12-24 05:52:49 | [diff] [blame] | 345 | return []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 346 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 347 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 348 | if (!this._classNamesPromise || this._selectedFrameId !== selectedNode.frameId()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 349 | this._classNamesPromise = this._getClassNames(selectedNode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 350 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 351 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 352 | let completions: string[] = await this._classNamesPromise; |
| 353 | const classesMap = this._nodeClasses((selectedNode as SDK.DOMModel.DOMNode)); |
Mathias Bynens | 41ea263 | 2020-12-24 05:52:49 | [diff] [blame] | 354 | completions = completions.filter(value => !classesMap.get(value)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 355 | |
Mathias Bynens | 41ea263 | 2020-12-24 05:52:49 | [diff] [blame] | 356 | if (prefix[0] === '.') { |
| 357 | completions = completions.map(value => '.' + value); |
| 358 | } |
| 359 | return completions.filter(value => value.startsWith(prefix)).sort().map(completion => { |
| 360 | return { |
| 361 | text: completion, |
| 362 | title: undefined, |
| 363 | subtitle: undefined, |
| 364 | iconType: undefined, |
| 365 | priority: undefined, |
| 366 | isSecondary: undefined, |
| 367 | subtitleRenderer: undefined, |
| 368 | selectionRange: undefined, |
| 369 | hideGhostText: undefined, |
| 370 | iconElement: undefined, |
| 371 | }; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 372 | }); |
| 373 | } |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 374 | } |