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'; |
Danil Somsikov | 8a65be0 | 2023-10-17 08:36:58 | [diff] [blame] | 10 | import * as VisualLogging from '../../ui/visual_logging/visual_logging.js'; |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 11 | |
Kriti Sapra | 40ca8da | 2021-07-20 09:33:59 | [diff] [blame] | 12 | import classesPaneWidgetStyles from './classesPaneWidget.css.js'; |
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 | /** |
Jack Franklin | fd72c07 | 2022-12-21 11:45:01 | [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. |
| 19 | */ |
Vidal Guillermo Diazleal Ortega | 0cfa0ed | 2021-02-17 20:35:49 | [diff] [blame] | 20 | addNewClass: 'Add new class', |
| 21 | /** |
Jack Franklin | fd72c07 | 2022-12-21 11:45:01 | [diff] [blame] | 22 | * @description Screen reader announcement string when adding a CSS class via the Classes Pane Widget. |
| 23 | * @example {vbox flex-auto} PH1 |
| 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 | /** |
Jack Franklin | fd72c07 | 2022-12-21 11:45:01 | [diff] [blame] | 27 | * @description Screen reader announcement string when adding a class via the Classes Pane Widget. |
| 28 | * @example {title-container} PH1 |
| 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 | /** |
Jack Franklin | fd72c07 | 2022-12-21 11:45:01 | [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. |
| 34 | */ |
Vidal Guillermo Diazleal Ortega | 0cfa0ed | 2021-02-17 20:35:49 | [diff] [blame] | 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 | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 40 | private input: HTMLElement; |
| 41 | private readonly classesContainer: HTMLElement; |
| 42 | private readonly prompt: ClassNamePrompt; |
| 43 | private readonly mutatingNodes: Set<SDK.DOMModel.DOMNode>; |
| 44 | private readonly pendingNodeClasses: Map<SDK.DOMModel.DOMNode, string>; |
| 45 | private readonly updateNodeThrottler: Common.Throttler.Throttler; |
| 46 | private previousTarget: SDK.DOMModel.DOMNode|null; |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 47 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 48 | constructor() { |
| 49 | super(true); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 50 | this.contentElement.className = 'styles-element-classes-pane'; |
Danil Somsikov | 0cb9746 | 2024-01-30 15:11:29 | [diff] [blame] | 51 | this.contentElement.setAttribute('jslog', `${VisualLogging.pane('elements-classes')}`); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 52 | const container = this.contentElement.createChild('div', 'title-container'); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 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'); |
| 57 | this.prompt = new ClassNamePrompt(this.nodeClasses.bind(this)); |
| 58 | this.prompt.setAutocompletionTimeout(0); |
| 59 | this.prompt.renderAsBlock(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 60 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 61 | const proxyElement = (this.prompt.attach(this.input) as HTMLElement); |
| 62 | this.prompt.setPlaceholder(i18nString(UIStrings.addNewClass)); |
| 63 | this.prompt.addEventListener(UI.TextPrompt.Events.TextChanged, this.onTextChanged, this); |
| 64 | proxyElement.addEventListener('keydown', this.onKeyDown.bind(this), false); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 65 | |
Sigurd Schneider | b9f6c79 | 2021-05-31 10:57:24 | [diff] [blame] | 66 | SDK.TargetManager.TargetManager.instance().addModelListener( |
Danil Somsikov | 29edf37 | 2023-03-03 16:18:59 | [diff] [blame] | 67 | SDK.DOMModel.DOMModel, SDK.DOMModel.Events.DOMMutated, this.onDOMMutated, this, {scoped: true}); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 68 | this.mutatingNodes = new Set(); |
| 69 | this.pendingNodeClasses = new Map(); |
| 70 | this.updateNodeThrottler = new Common.Throttler.Throttler(0); |
| 71 | this.previousTarget = null; |
| 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 | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 75 | private 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 | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 79 | private onKeyDown(event: KeyboardEvent): void { |
Jack Franklin | e24bc6f | 2022-10-07 10:36:18 | [diff] [blame] | 80 | if (!(event.key === 'Enter') && !Platform.KeyboardUtilities.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(); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [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); |
Jack Franklin | e24bc6f | 2022-10-07 10:36:18 | [diff] [blame] | 93 | if (Platform.KeyboardUtilities.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 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 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 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 108 | const classNames = this.splitTextIntoClasses(text); |
Michael Liao | cafccfd | 2020-04-02 17:11:59 | [diff] [blame] | 109 | if (!classNames.length) { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [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) { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [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 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 124 | this.installNodeClasses(node); |
| 125 | this.update(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 126 | } |
| 127 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 128 | private 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 | } |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 133 | this.installNodeClasses(node); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 134 | } |
| 135 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 136 | private onDOMMutated(event: Common.EventTarget.EventTargetEvent<SDK.DOMModel.DOMNode>): void { |
Simon Zünd | 37cf1f5 | 2021-07-30 09:53:35 | [diff] [blame] | 137 | const node = event.data; |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [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); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 142 | this.update(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 143 | } |
| 144 | |
Simon Zünd | f0aa2b4 | 2021-09-20 09:18:02 | [diff] [blame] | 145 | private onSelectedNodeChanged(event: Common.EventTarget.EventTargetEvent<SDK.DOMModel.DOMNode|null>): void { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 146 | if (this.previousTarget && this.prompt.text()) { |
| 147 | this.input.textContent = ''; |
| 148 | this.installNodeClasses(this.previousTarget); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 149 | } |
Simon Zünd | f0aa2b4 | 2021-09-20 09:18:02 | [diff] [blame] | 150 | this.previousTarget = event.data; |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 151 | this.update(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 152 | } |
| 153 | |
Randolf Jung | ffd1424 | 2023-04-19 00:32:25 | [diff] [blame] | 154 | override wasShown(): void { |
Kriti Sapra | b2b29f2 | 2021-06-29 12:59:56 | [diff] [blame] | 155 | super.wasShown(); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 156 | this.update(); |
Kriti Sapra | 40ca8da | 2021-07-20 09:33:59 | [diff] [blame] | 157 | this.registerCSSFiles([classesPaneWidgetStyles]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 158 | } |
| 159 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 160 | private update(): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 161 | if (!this.isShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 162 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 163 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 164 | |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 165 | let node = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 166 | if (node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 167 | node = node.enclosingElementOrSelf(); |
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 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 170 | this.classesContainer.removeChildren(); |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 171 | // @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] | 172 | this.input.disabled = !node; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 173 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 174 | if (!node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 175 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 176 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 177 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 178 | const classes = this.nodeClasses(node); |
Simon Zünd | f27be3d | 2020-02-11 14:46:27 | [diff] [blame] | 179 | const keys = [...classes.keys()]; |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 180 | keys.sort(Platform.StringUtilities.caseInsensetiveComparator); |
| 181 | for (const className of keys) { |
Kateryna Prokopenko | f895606 | 2024-06-19 08:30:37 | [diff] [blame] | 182 | const label = |
| 183 | UI.UIUtils.CheckboxLabel.create(className, classes.get(className), undefined, 'element-class', true); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 184 | label.classList.add('monospace'); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 185 | label.checkboxElement.addEventListener('click', this.onClick.bind(this, className), false); |
| 186 | this.classesContainer.appendChild(label); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 190 | private onClick(className: string, event: Event): void { |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 191 | const node = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 192 | if (!node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 193 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 194 | } |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 195 | const enabled = (event.target as HTMLInputElement).checked; |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 196 | this.toggleClass(node, className, enabled); |
| 197 | this.installNodeClasses(node); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 198 | } |
| 199 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 200 | private nodeClasses(node: SDK.DOMModel.DOMNode): Map<string, boolean> { |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 201 | let result = cachedClassesMap.get(node); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 202 | if (!result) { |
| 203 | const classAttribute = node.getAttribute('class') || ''; |
| 204 | const classes = classAttribute.split(/\s/); |
| 205 | result = new Map(); |
| 206 | for (let i = 0; i < classes.length; ++i) { |
| 207 | const className = classes[i].trim(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 208 | if (!className.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 209 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 210 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 211 | result.set(className, true); |
| 212 | } |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 213 | cachedClassesMap.set(node, result); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 214 | } |
| 215 | return result; |
| 216 | } |
| 217 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 218 | private toggleClass(node: SDK.DOMModel.DOMNode, className: string, enabled: boolean): void { |
| 219 | const classes = this.nodeClasses(node); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 220 | classes.set(className, enabled); |
Kateryna Prokopenko | 17cba89 | 2024-07-26 13:33:56 | [diff] [blame^] | 221 | ButtonProvider.instance().item().setChecked([...classes.values()].includes(true)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 222 | } |
| 223 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 224 | private installNodeClasses(node: SDK.DOMModel.DOMNode): void { |
| 225 | const classes = this.nodeClasses(node); |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 226 | const activeClasses = new Set<string>(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 227 | for (const className of classes.keys()) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 228 | if (classes.get(className)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 229 | activeClasses.add(className); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 230 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 231 | } |
| 232 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 233 | const additionalClasses = this.splitTextIntoClasses(this.prompt.textWithCurrentSuggestion()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 234 | for (const className of additionalClasses) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 235 | activeClasses.add(className); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 236 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 237 | |
Simon Zünd | a0d4062 | 2020-02-12 13:16:42 | [diff] [blame] | 238 | const newClasses = [...activeClasses.values()].sort(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 239 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 240 | this.pendingNodeClasses.set(node, newClasses.join(' ')); |
Tim van der Lippe | 2d9a95c | 2022-01-04 15:18:03 | [diff] [blame] | 241 | void this.updateNodeThrottler.schedule(this.flushPendingClasses.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 242 | } |
| 243 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 244 | private async flushPendingClasses(): Promise<void> { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 245 | const promises = []; |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 246 | for (const node of this.pendingNodeClasses.keys()) { |
| 247 | this.mutatingNodes.add(node); |
| 248 | const promise = node.setAttributeValuePromise('class', (this.pendingNodeClasses.get(node) as string)) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 249 | .then(onClassValueUpdated.bind(this, node)); |
| 250 | promises.push(promise); |
| 251 | } |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 252 | this.pendingNodeClasses.clear(); |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 253 | await Promise.all(promises); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 254 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 255 | function onClassValueUpdated(this: ClassesPaneWidget, node: SDK.DOMModel.DOMNode): void { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 256 | this.mutatingNodes.delete(node); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 257 | } |
| 258 | } |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 259 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 260 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 261 | const cachedClassesMap = new WeakMap<SDK.DOMModel.DOMNode, Map<string, boolean>>(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 262 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 263 | let buttonProviderInstance: ButtonProvider; |
Andres Olivares | 43c2d9f | 2021-02-10 16:48:39 | [diff] [blame] | 264 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 265 | export class ButtonProvider implements UI.Toolbar.Provider { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 266 | private readonly button: UI.Toolbar.ToolbarToggle; |
| 267 | private view: ClassesPaneWidget; |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 268 | private constructor() { |
Kateryna Prokopenko | 17cba89 | 2024-07-26 13:33:56 | [diff] [blame^] | 269 | this.button = new UI.Toolbar.ToolbarToggle(i18nString(UIStrings.elementClasses), 'class'); |
| 270 | this.button.element.style.setProperty('--dot-toggle-top', '12px'); |
| 271 | this.button.element.style.setProperty('--dot-toggle-left', '18px'); |
Danil Somsikov | 8a65be0 | 2023-10-17 08:36:58 | [diff] [blame] | 272 | this.button.element.setAttribute( |
Danil Somsikov | 0cb9746 | 2024-01-30 15:11:29 | [diff] [blame] | 273 | 'jslog', `${VisualLogging.toggleSubpane('elements-classes').track({click: true})}`); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 274 | this.button.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this.clicked, this); |
| 275 | this.view = new ClassesPaneWidget(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 276 | } |
| 277 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 278 | static instance(opts: { |
| 279 | forceNew: boolean|null, |
| 280 | } = {forceNew: null}): ButtonProvider { |
Andres Olivares | 43c2d9f | 2021-02-10 16:48:39 | [diff] [blame] | 281 | const {forceNew} = opts; |
| 282 | if (!buttonProviderInstance || forceNew) { |
| 283 | buttonProviderInstance = new ButtonProvider(); |
| 284 | } |
| 285 | |
| 286 | return buttonProviderInstance; |
| 287 | } |
| 288 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 289 | private clicked(): void { |
| 290 | ElementsPanel.instance().showToolbarPane(!this.view.isShowing() ? this.view : null, this.button); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 291 | } |
| 292 | |
Kateryna Prokopenko | 17cba89 | 2024-07-26 13:33:56 | [diff] [blame^] | 293 | item(): UI.Toolbar.ToolbarToggle { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 294 | return this.button; |
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 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 297 | |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 298 | export class ClassNamePrompt extends UI.TextPrompt.TextPrompt { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 299 | private readonly nodeClasses: (arg0: SDK.DOMModel.DOMNode) => Map<string, boolean>; |
| 300 | private selectedFrameId: string|null; |
| 301 | private classNamesPromise: Promise<string[]>|null; |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 302 | constructor(nodeClasses: (arg0: SDK.DOMModel.DOMNode) => Map<string, boolean>) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 303 | super(); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 304 | this.nodeClasses = nodeClasses; |
| 305 | this.initialize(this.buildClassNameCompletions.bind(this), ' '); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 306 | this.disableDefaultSuggestionForEmptyInput(); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 307 | this.selectedFrameId = ''; |
| 308 | this.classNamesPromise = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 309 | } |
| 310 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 311 | private async getClassNames(selectedNode: SDK.DOMModel.DOMNode): Promise<string[]> { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 312 | const promises = []; |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 313 | const completions = new Set<string>(); |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 314 | this.selectedFrameId = selectedNode.frameId(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 315 | |
| 316 | const cssModel = selectedNode.domModel().cssModel(); |
| 317 | const allStyleSheets = cssModel.allStyleSheets(); |
| 318 | for (const stylesheet of allStyleSheets) { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 319 | if (stylesheet.frameId !== this.selectedFrameId) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 320 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 321 | } |
Johan Bay | 04d50fc | 2022-03-01 12:07:04 | [diff] [blame] | 322 | const cssPromise = cssModel.getClassNames(stylesheet.id).then(classes => { |
Mathias Bynens | 3abc095 | 2020-04-20 14:15:52 | [diff] [blame] | 323 | for (const className of classes) { |
| 324 | completions.add(className); |
| 325 | } |
| 326 | }); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 327 | promises.push(cssPromise); |
| 328 | } |
| 329 | |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 330 | const ownerDocumentId = ((selectedNode.ownerDocument as SDK.DOMModel.DOMDocument).id); |
Tim van der Lippe | 32f760f | 2020-10-01 10:52:15 | [diff] [blame] | 331 | |
| 332 | const domPromise = selectedNode.domModel().classNamesPromise(ownerDocumentId).then(classes => { |
Mathias Bynens | 3abc095 | 2020-04-20 14:15:52 | [diff] [blame] | 333 | for (const className of classes) { |
| 334 | completions.add(className); |
| 335 | } |
| 336 | }); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 337 | promises.push(domPromise); |
Mathias Bynens | 3abc095 | 2020-04-20 14:15:52 | [diff] [blame] | 338 | await Promise.all(promises); |
| 339 | return [...completions]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 340 | } |
| 341 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 342 | private async buildClassNameCompletions(expression: string, prefix: string, force?: boolean): |
Jan Scheffler | 2990585 | 2021-04-14 11:30:31 | [diff] [blame] | 343 | Promise<UI.SuggestBox.Suggestions> { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 344 | if (!prefix || force) { |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 345 | this.classNamesPromise = null; |
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 | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 348 | const selectedNode = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 349 | if (!selectedNode || (!prefix && !force && !expression.trim())) { |
Mathias Bynens | 41ea263 | 2020-12-24 05:52:49 | [diff] [blame] | 350 | return []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 351 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 352 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 353 | if (!this.classNamesPromise || this.selectedFrameId !== selectedNode.frameId()) { |
| 354 | this.classNamesPromise = this.getClassNames(selectedNode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 355 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 356 | |
Jan Scheffler | 77646c7 | 2021-08-13 18:40:38 | [diff] [blame] | 357 | let completions: string[] = await this.classNamesPromise; |
| 358 | const classesMap = this.nodeClasses((selectedNode as SDK.DOMModel.DOMNode)); |
Mathias Bynens | 41ea263 | 2020-12-24 05:52:49 | [diff] [blame] | 359 | completions = completions.filter(value => !classesMap.get(value)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 360 | |
Mathias Bynens | 41ea263 | 2020-12-24 05:52:49 | [diff] [blame] | 361 | if (prefix[0] === '.') { |
| 362 | completions = completions.map(value => '.' + value); |
| 363 | } |
| 364 | return completions.filter(value => value.startsWith(prefix)).sort().map(completion => { |
| 365 | return { |
| 366 | text: completion, |
| 367 | title: undefined, |
| 368 | subtitle: undefined, |
Mathias Bynens | 41ea263 | 2020-12-24 05:52:49 | [diff] [blame] | 369 | priority: undefined, |
| 370 | isSecondary: undefined, |
| 371 | subtitleRenderer: undefined, |
| 372 | selectionRange: undefined, |
| 373 | hideGhostText: undefined, |
| 374 | iconElement: undefined, |
| 375 | }; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 376 | }); |
| 377 | } |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 378 | } |