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 | |
Tim van der Lippe | 7696157 | 2021-04-06 10:48:07 | [diff] [blame] | 5 | import * as Common from '../../core/common/common.js'; |
Tim van der Lippe | bb352e6 | 2021-04-01 17:57:28 | [diff] [blame] | 6 | import * as i18n from '../../core/i18n/i18n.js'; |
Tim van der Lippe | aa1ed7a | 2021-03-31 14:38:27 | [diff] [blame] | 7 | import * as Platform from '../../core/platform/platform.js'; |
Tim van der Lippe | e00b92f | 2021-03-31 16:52:17 | [diff] [blame] | 8 | import * as SDK from '../../core/sdk/sdk.js'; |
Tim van der Lippe | aa61faf | 2021-04-07 15:32:07 | [diff] [blame] | 9 | import * as UI from '../../ui/legacy/legacy.js'; |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 10 | |
Kriti Sapra | 40ca8da | 2021-07-20 09:33:59 | [diff] [blame] | 11 | import classesPaneWidgetStyles from './classesPaneWidget.css.js'; |
Tim van der Lippe | aabc830 | 2019-12-10 15:34:45 | [diff] [blame] | 12 | import {ElementsPanel} from './ElementsPanel.js'; |
| 13 | |
Simon Zünd | fbfd107 | 2021-03-01 07:38:53 | [diff] [blame] | 14 | const UIStrings = { |
Vidal Guillermo Diazleal Ortega | 0cfa0ed | 2021-02-17 20:35:49 | [diff] [blame] | 15 | /** |
Peter Marshall | cc99541 | 2021-02-19 08:07:43 | [diff] [blame] | 16 | * @description Prompt text for a text field in the Classes Pane Widget of the Elements panel. |
| 17 | * Class refers to a CSS class. |
Vidal Guillermo Diazleal Ortega | 0cfa0ed | 2021-02-17 20:35:49 | [diff] [blame] | 18 | */ |
| 19 | addNewClass: 'Add new class', |
| 20 | /** |
Peter Marshall | cc99541 | 2021-02-19 08:07:43 | [diff] [blame] | 21 | * @description Screen reader announcement string when adding a CSS class via the Classes Pane Widget. |
| 22 | * @example {vbox flex-auto} PH1 |
Vidal Guillermo Diazleal Ortega | 0cfa0ed | 2021-02-17 20:35:49 | [diff] [blame] | 23 | */ |
Peter Marshall | cc99541 | 2021-02-19 08:07:43 | [diff] [blame] | 24 | classesSAdded: 'Classes {PH1} added', |
Vidal Guillermo Diazleal Ortega | 0cfa0ed | 2021-02-17 20:35:49 | [diff] [blame] | 25 | /** |
Peter Marshall | cc99541 | 2021-02-19 08:07:43 | [diff] [blame] | 26 | * @description Screen reader announcement string when adding a class via the Classes Pane Widget. |
| 27 | * @example {title-container} PH1 |
Vidal Guillermo Diazleal Ortega | 0cfa0ed | 2021-02-17 20:35:49 | [diff] [blame] | 28 | */ |
Peter Marshall | cc99541 | 2021-02-19 08:07:43 | [diff] [blame] | 29 | classSAdded: 'Class {PH1} added', |
Vidal Guillermo Diazleal Ortega | 0cfa0ed | 2021-02-17 20:35:49 | [diff] [blame] | 30 | /** |
Peter Marshall | cc99541 | 2021-02-19 08:07:43 | [diff] [blame] | 31 | * @description Accessible title read by screen readers for the Classes Pane Widget of the Elements |
| 32 | * 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] | 33 | */ |
| 34 | elementClasses: 'Element Classes', |
| 35 | }; |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 36 | const str_ = i18n.i18n.registerUIStrings('panels/elements/ClassesPaneWidget.ts', UIStrings); |
Vidal Guillermo Diazleal Ortega | 0cfa0ed | 2021-02-17 20:35:49 | [diff] [blame] | 37 | const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 38 | export class ClassesPaneWidget extends UI.Widget.Widget { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 39 | private input: HTMLElement; |
| 40 | private readonly classesContainer: HTMLElement; |
| 41 | private readonly prompt: ClassNamePrompt; |
| 42 | private readonly mutatingNodes: Set<SDK.DOMModel.DOMNode>; |
| 43 | private readonly pendingNodeClasses: Map<SDK.DOMModel.DOMNode, string>; |
| 44 | private readonly updateNodeThrottler: Common.Throttler.Throttler; |
| 45 | private previousTarget: SDK.DOMModel.DOMNode|null; |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 46 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 47 | constructor() { |
| 48 | super(true); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 49 | this.contentElement.className = 'styles-element-classes-pane'; |
| 50 | const container = this.contentElement.createChild('div', 'title-container'); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 51 | this.input = container.createChild('div', 'new-class-input monospace'); |
| 52 | this.setDefaultFocusedElement(this.input); |
| 53 | this.classesContainer = this.contentElement.createChild('div', 'source-code'); |
| 54 | this.classesContainer.classList.add('styles-element-classes-container'); |
| 55 | this.prompt = new ClassNamePrompt(this.nodeClasses.bind(this)); |
| 56 | this.prompt.setAutocompletionTimeout(0); |
| 57 | this.prompt.renderAsBlock(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 58 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 59 | const proxyElement = (this.prompt.attach(this.input) as HTMLElement); |
| 60 | this.prompt.setPlaceholder(i18nString(UIStrings.addNewClass)); |
| 61 | this.prompt.addEventListener(UI.TextPrompt.Events.TextChanged, this.onTextChanged, this); |
| 62 | proxyElement.addEventListener('keydown', this.onKeyDown.bind(this), false); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 63 | |
Sigurd Schneider | b9f6c79 | 2021-05-31 10:57:24 | [diff] [blame] | 64 | SDK.TargetManager.TargetManager.instance().addModelListener( |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 65 | SDK.DOMModel.DOMModel, SDK.DOMModel.Events.DOMMutated, this.onDOMMutated, this); |
| 66 | this.mutatingNodes = new Set(); |
| 67 | this.pendingNodeClasses = new Map(); |
| 68 | this.updateNodeThrottler = new Common.Throttler.Throttler(0); |
| 69 | this.previousTarget = null; |
| 70 | UI.Context.Context.instance().addFlavorChangeListener(SDK.DOMModel.DOMNode, this.onSelectedNodeChanged, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 71 | } |
| 72 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 73 | private splitTextIntoClasses(text: string): string[] { |
Mathias Bynens | 3abc095 | 2020-04-20 14:15:52 | [diff] [blame] | 74 | return text.split(/[,\s]/).map(className => className.trim()).filter(className => className.length); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 75 | } |
| 76 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 77 | private onKeyDown(event: KeyboardEvent): void { |
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(); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [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 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 89 | const eventTarget = (event.target as HTMLElement); |
| 90 | let text: ''|string = (eventTarget.textContent as string); |
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 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 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 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 106 | const classNames = this.splitTextIntoClasses(text); |
Michael Liao | cafccfd | 2020-04-02 17:11:59 | [diff] [blame] | 107 | if (!classNames.length) { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [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) { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [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 | 7322dee | 2021-04-07 18:33:30 | [diff] [blame] | 120 | UI.ARIAUtils.alert(announcementString); |
Michael Liao | cafccfd | 2020-04-02 17:11:59 | [diff] [blame] | 121 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 122 | this.installNodeClasses(node); |
| 123 | this.update(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 124 | } |
| 125 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 126 | private onTextChanged(): void { |
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 | } |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 131 | this.installNodeClasses(node); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 132 | } |
| 133 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 134 | private onDOMMutated(event: Common.EventTarget.EventTargetEvent<SDK.DOMModel.DOMNode>): void { |
Simon Zünd | 37cf1f5 | 2021-07-30 09:53:35 | [diff] [blame] | 135 | const node = event.data; |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 136 | if (this.mutatingNodes.has(node)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 137 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 138 | } |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 139 | cachedClassesMap.delete(node); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 140 | this.update(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 141 | } |
| 142 | |
Simon Zünd | f0aa2b4 | 2021-09-20 09:18:02 | [diff] [blame] | 143 | private onSelectedNodeChanged(event: Common.EventTarget.EventTargetEvent<SDK.DOMModel.DOMNode|null>): void { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 144 | if (this.previousTarget && this.prompt.text()) { |
| 145 | this.input.textContent = ''; |
| 146 | this.installNodeClasses(this.previousTarget); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 147 | } |
Simon Zünd | f0aa2b4 | 2021-09-20 09:18:02 | [diff] [blame] | 148 | this.previousTarget = event.data; |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 149 | this.update(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 150 | } |
| 151 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 152 | wasShown(): void { |
Kriti Sapra | b2b29f2 | 2021-06-29 12:59:56 | [diff] [blame] | 153 | super.wasShown(); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 154 | this.update(); |
Kriti Sapra | 40ca8da | 2021-07-20 09:33:59 | [diff] [blame] | 155 | this.registerCSSFiles([classesPaneWidgetStyles]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 156 | } |
| 157 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 158 | private 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 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 168 | this.classesContainer.removeChildren(); |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 169 | // @ts-ignore this.input is a div, not an input element. So this line makes no sense at all |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 170 | this.input.disabled = !node; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 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 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 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'); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 182 | label.checkboxElement.addEventListener('click', this.onClick.bind(this, className), false); |
| 183 | this.classesContainer.appendChild(label); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 187 | private 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; |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 193 | this.toggleClass(node, className, enabled); |
| 194 | this.installNodeClasses(node); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 195 | } |
| 196 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 197 | private 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 | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 215 | private toggleClass(node: SDK.DOMModel.DOMNode, className: string, enabled: boolean): void { |
| 216 | const classes = this.nodeClasses(node); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 217 | classes.set(className, enabled); |
| 218 | } |
| 219 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 220 | private installNodeClasses(node: SDK.DOMModel.DOMNode): void { |
| 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 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 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 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 236 | this.pendingNodeClasses.set(node, newClasses.join(' ')); |
Tim van der Lippe | 2d9a95c | 2022-01-04 15:18:03 | [diff] [blame] | 237 | void this.updateNodeThrottler.schedule(this.flushPendingClasses.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 238 | } |
| 239 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 240 | private async flushPendingClasses(): Promise<void> { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 241 | const promises = []; |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 242 | for (const node of this.pendingNodeClasses.keys()) { |
| 243 | this.mutatingNodes.add(node); |
| 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 | } |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 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 { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 252 | this.mutatingNodes.delete(node); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 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 { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 262 | private readonly button: UI.Toolbar.ToolbarToggle; |
| 263 | private view: ClassesPaneWidget; |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 264 | private constructor() { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 265 | this.button = new UI.Toolbar.ToolbarToggle(i18nString(UIStrings.elementClasses), ''); |
| 266 | this.button.setText('.cls'); |
| 267 | this.button.element.classList.add('monospace'); |
| 268 | this.button.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this.clicked, this); |
| 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 | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 283 | private clicked(): void { |
| 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 { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 288 | return this.button; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 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 | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 293 | private readonly nodeClasses: (arg0: SDK.DOMModel.DOMNode) => Map<string, boolean>; |
| 294 | private selectedFrameId: string|null; |
| 295 | private classNamesPromise: Promise<string[]>|null; |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 296 | constructor(nodeClasses: (arg0: SDK.DOMModel.DOMNode) => Map<string, boolean>) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 297 | super(); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 298 | this.nodeClasses = nodeClasses; |
| 299 | this.initialize(this.buildClassNameCompletions.bind(this), ' '); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 300 | this.disableDefaultSuggestionForEmptyInput(); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 301 | this.selectedFrameId = ''; |
| 302 | this.classNamesPromise = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 303 | } |
| 304 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 305 | private 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>(); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 308 | this.selectedFrameId = selectedNode.frameId(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 309 | |
| 310 | const cssModel = selectedNode.domModel().cssModel(); |
| 311 | const allStyleSheets = cssModel.allStyleSheets(); |
| 312 | for (const stylesheet of allStyleSheets) { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [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 | } |
Johan Bay | 04d50fc | 2022-03-01 12:07:04 | [diff] [blame^] | 316 | const cssPromise = cssModel.getClassNames(stylesheet.id).then(classes => { |
Mathias Bynens | 3abc095 | 2020-04-20 14:15:52 | [diff] [blame] | 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 | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 336 | private async buildClassNameCompletions(expression: string, prefix: string, force?: boolean): |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 337 | Promise<UI.SuggestBox.Suggestions> { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 338 | if (!prefix || force) { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [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 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 347 | if (!this.classNamesPromise || this.selectedFrameId !== selectedNode.frameId()) { |
| 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 | 77646c7 | 2021-08-13 18:40:38 | [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 | } |