blob: babea392dd4282131805dba449b56db1b980a515 [file] [log] [blame]
Jan Scheffler35199b92021-03-17 09:51:151// Copyright 2021 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.
4
Blink Reformat4c46d092018-04-07 15:32:375/*
6 * Copyright (C) IBM Corp. 2009 All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 * * Neither the name of IBM Corp. nor the names of its
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
Tim van der Lippe8987f8f2020-01-03 15:03:1634
Jan Scheffler35199b92021-03-17 09:51:1535/* eslint-disable rulesdir/no_underscored_properties */
36
Tim van der Lippe021c7572021-04-19 10:49:4337import * as Components from '../../components/components.js';
38import * as Common from '../../core/common/common.js';
39import * as Host from '../../core/host/host.js';
40import * as i18n from '../../core/i18n/i18n.js';
41import * as Platform from '../../core/platform/platform.js';
42import * as SDK from '../../core/sdk/sdk.js';
43import * as ObjectUI from '../../ui/legacy/components/object_ui/object_ui.js';
44import * as UI from '../../ui/legacy/legacy.js';
Tim van der Lippefbbf9812020-02-13 14:43:4645
Paul Lewis39944952020-01-22 15:45:1846import {UISourceCodeFrame} from './UISourceCodeFrame.js';
47
Simon Zünd697fb0b2021-03-01 10:12:4248const UIStrings = {
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:3249 /**
50 *@description A context menu item in the Watch Expressions Sidebar Pane of the Sources panel
51 */
52 addWatchExpression: 'Add watch expression',
53 /**
54 *@description Tooltip/screen reader label of a button in the Sources panel that refreshes all watch expressions.
55 */
56 refreshWatchExpressions: 'Refresh watch expressions',
57 /**
58 *@description Empty element text content in Watch Expressions Sidebar Pane of the Sources panel
59 */
60 noWatchExpressions: 'No watch expressions',
61 /**
62 *@description A context menu item in the Watch Expressions Sidebar Pane of the Sources panel
63 */
64 deleteAllWatchExpressions: 'Delete all watch expressions',
65 /**
66 *@description A context menu item in the Watch Expressions Sidebar Pane of the Sources panel
67 */
68 addPropertyPathToWatch: 'Add property path to watch',
69 /**
70 *@description A context menu item in the Watch Expressions Sidebar Pane of the Sources panel
71 */
72 deleteWatchExpression: 'Delete watch expression',
73 /**
74 *@description Value element text content in Watch Expressions Sidebar Pane of the Sources panel
75 */
76 notAvailable: '<not available>',
77 /**
78 *@description A context menu item in the Watch Expressions Sidebar Pane of the Sources panel and Network pane request.
79 */
80 copyValue: 'Copy value',
81};
Tim van der Lippe021c7572021-04-19 10:49:4382const str_ = i18n.i18n.registerUIStrings('panels/sources/WatchExpressionsSidebarPane.ts', UIStrings);
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:3283const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
Jan Scheffler35199b92021-03-17 09:51:1584let watchExpressionsSidebarPaneInstance: WatchExpressionsSidebarPane;
Andres Olivaresfd431fb2020-12-10 15:30:0485
Jan Scheffler35199b92021-03-17 09:51:1586export class WatchExpressionsSidebarPane extends UI.ThrottledWidget.ThrottledWidget implements
87 UI.ActionRegistration.ActionDelegate, UI.Toolbar.ItemsProvider, UI.ContextMenu.Provider {
88 _watchExpressions: WatchExpression[];
89 _emptyElement!: HTMLElement;
90 // TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration)
91 // eslint-disable-next-line @typescript-eslint/no-explicit-any
92 _watchExpressionsSetting: Common.Settings.Setting<any>;
93 _addButton: UI.Toolbar.ToolbarButton;
94 _refreshButton: UI.Toolbar.ToolbarButton;
95 _treeOutline: ObjectUI.ObjectPropertiesSection.ObjectPropertiesSectionsTreeOutline;
96 _expandController: ObjectUI.ObjectPropertiesSection.ObjectPropertiesSectionsTreeExpandController;
97 _linkifier: Components.Linkifier.Linkifier;
98 private constructor() {
Blink Reformat4c46d092018-04-07 15:32:3799 super(true);
Tim van der Lippe0dd7a052021-04-12 16:44:23100 this.registerRequiredCSS('ui/legacy/components/object_ui/objectValue.css', {enableLegacyPatching: true});
Tim van der Lippe021c7572021-04-19 10:49:43101 this.registerRequiredCSS('panels/sources/watchExpressionsSidebarPane.css', {enableLegacyPatching: true});
Blink Reformat4c46d092018-04-07 15:32:37102
Simon Zünd0243c602020-03-10 06:51:42103 // TODO(szuend): Replace with a Set once the web test
Tim van der Lippe021c7572021-04-19 10:49:43104 // panels/sources/debugger-ui/watch-expressions-preserve-expansion.js is either converted
Jan Scheffler35199b92021-03-17 09:51:15105 // to an e2e test or no longer accesses this variable directly.
Blink Reformat4c46d092018-04-07 15:32:37106 this._watchExpressions = [];
Paul Lewis2d7d65c2020-03-16 17:26:30107 this._watchExpressionsSetting = Common.Settings.Settings.instance().createLocalSetting('watchExpressions', []);
Blink Reformat4c46d092018-04-07 15:32:37108
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:32109 this._addButton = new UI.Toolbar.ToolbarButton(i18nString(UIStrings.addWatchExpression), 'largeicon-add');
Jan Scheffler35199b92021-03-17 09:51:15110 this._addButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, _event => {
Tim van der Lippe37a35ff2020-03-03 13:49:02111 this._addButtonClicked();
112 });
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:32113 this._refreshButton =
114 new UI.Toolbar.ToolbarButton(i18nString(UIStrings.refreshWatchExpressions), 'largeicon-refresh');
Tim van der Lippefbbf9812020-02-13 14:43:46115 this._refreshButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this.update, this);
Blink Reformat4c46d092018-04-07 15:32:37116
117 this.contentElement.classList.add('watch-expressions');
118 this.contentElement.addEventListener('contextmenu', this._contextMenu.bind(this), false);
Tim van der Lippefbbf9812020-02-13 14:43:46119 this._treeOutline = new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSectionsTreeOutline();
Tim van der Lippe021c7572021-04-19 10:49:43120 this._treeOutline.registerRequiredCSS(
121 'panels/sources/watchExpressionsSidebarPane.css', {enableLegacyPatching: true});
Jack Lynch5db2a3e2019-11-05 19:12:31122 this._treeOutline.setShowSelectionOnKeyboardFocus(/* show */ true);
Tim van der Lippefbbf9812020-02-13 14:43:46123 this._expandController =
124 new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSectionsTreeExpandController(this._treeOutline);
Blink Reformat4c46d092018-04-07 15:32:37125
Tim van der Lipped1a00aa2020-08-19 16:03:56126 UI.Context.Context.instance().addFlavorChangeListener(SDK.RuntimeModel.ExecutionContext, this.update, this);
127 UI.Context.Context.instance().addFlavorChangeListener(SDK.DebuggerModel.CallFrame, this.update, this);
Tim van der Lippefbbf9812020-02-13 14:43:46128 this._linkifier = new Components.Linkifier.Linkifier();
Blink Reformat4c46d092018-04-07 15:32:37129 this.update();
130 }
131
Jan Scheffler35199b92021-03-17 09:51:15132 static instance(): WatchExpressionsSidebarPane {
Andres Olivaresfd431fb2020-12-10 15:30:04133 if (!watchExpressionsSidebarPaneInstance) {
134 watchExpressionsSidebarPaneInstance = new WatchExpressionsSidebarPane();
135 }
136 return watchExpressionsSidebarPaneInstance;
137 }
138
Jan Scheffler35199b92021-03-17 09:51:15139 toolbarItems(): UI.Toolbar.ToolbarItem[] {
Blink Reformat4c46d092018-04-07 15:32:37140 return [this._addButton, this._refreshButton];
141 }
142
Jan Scheffler35199b92021-03-17 09:51:15143 focus(): void {
Jack Lynch5db2a3e2019-11-05 19:12:31144 if (this.hasFocus()) {
145 return;
146 }
147 if (this._watchExpressions.length > 0) {
148 this._treeOutline.forceSelect();
149 }
150 }
151
Jan Scheffler35199b92021-03-17 09:51:15152 hasExpressions(): boolean {
Tim van der Lipped7cfd142021-01-07 12:17:24153 return Boolean(this._watchExpressionsSetting.get().length);
Blink Reformat4c46d092018-04-07 15:32:37154 }
155
Jan Scheffler35199b92021-03-17 09:51:15156 _saveExpressions(): void {
Blink Reformat4c46d092018-04-07 15:32:37157 const toSave = [];
158 for (let i = 0; i < this._watchExpressions.length; i++) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34159 if (this._watchExpressions[i].expression()) {
Blink Reformat4c46d092018-04-07 15:32:37160 toSave.push(this._watchExpressions[i].expression());
Tim van der Lippe1d6e57a2019-09-30 11:55:34161 }
Blink Reformat4c46d092018-04-07 15:32:37162 }
163
164 this._watchExpressionsSetting.set(toSave);
165 }
166
Jan Scheffler35199b92021-03-17 09:51:15167 async _addButtonClicked(): Promise<void> {
Paul Lewis75c7d0d2020-03-19 12:17:26168 await UI.ViewManager.ViewManager.instance().showView('sources.watch');
Jack Lynch1752e622020-08-25 23:50:56169 this._emptyElement.classList.add('hidden');
Blink Reformat4c46d092018-04-07 15:32:37170 this._createWatchExpression(null).startEditing();
171 }
172
Jan Scheffler35199b92021-03-17 09:51:15173 // TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration)
174 // eslint-disable-next-line @typescript-eslint/no-explicit-any
175 doUpdate(): Promise<any> {
Blink Reformat4c46d092018-04-07 15:32:37176 this._linkifier.reset();
177 this.contentElement.removeChildren();
Jack Lynch5db2a3e2019-11-05 19:12:31178 this._treeOutline.removeChildren();
Blink Reformat4c46d092018-04-07 15:32:37179 this._watchExpressions = [];
Jan Scheffler35199b92021-03-17 09:51:15180 this._emptyElement = (this.contentElement.createChild('div', 'gray-info-message') as HTMLElement);
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:32181 this._emptyElement.textContent = i18nString(UIStrings.noWatchExpressions);
Jack Lynch5db2a3e2019-11-05 19:12:31182 this._emptyElement.tabIndex = -1;
Blink Reformat4c46d092018-04-07 15:32:37183 const watchExpressionStrings = this._watchExpressionsSetting.get();
Jack Lynch338317a2020-08-19 21:59:49184 if (watchExpressionStrings.length) {
185 this._emptyElement.classList.add('hidden');
186 }
Blink Reformat4c46d092018-04-07 15:32:37187 for (let i = 0; i < watchExpressionStrings.length; ++i) {
188 const expression = watchExpressionStrings[i];
Tim van der Lippe1d6e57a2019-09-30 11:55:34189 if (!expression) {
Blink Reformat4c46d092018-04-07 15:32:37190 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34191 }
Blink Reformat4c46d092018-04-07 15:32:37192
193 this._createWatchExpression(expression);
194 }
195 return Promise.resolve();
196 }
197
Jan Scheffler35199b92021-03-17 09:51:15198 _createWatchExpression(expression: string|null): WatchExpression {
Jack Lynch5db2a3e2019-11-05 19:12:31199 this.contentElement.appendChild(this._treeOutline.element);
Tim van der Lippe8987f8f2020-01-03 15:03:16200 const watchExpression = new WatchExpression(expression, this._expandController, this._linkifier);
201 watchExpression.addEventListener(WatchExpression.Events.ExpressionUpdated, this._watchExpressionUpdated, this);
Jack Lynch5db2a3e2019-11-05 19:12:31202 this._treeOutline.appendChild(watchExpression.treeElement());
Blink Reformat4c46d092018-04-07 15:32:37203 this._watchExpressions.push(watchExpression);
204 return watchExpression;
205 }
206
Jan Scheffler35199b92021-03-17 09:51:15207 _watchExpressionUpdated(event: Common.EventTarget.EventTargetEvent): void {
208 const watchExpression = (event.data as WatchExpression);
Blink Reformat4c46d092018-04-07 15:32:37209 if (!watchExpression.expression()) {
Simon Zünd0243c602020-03-10 06:51:42210 Platform.ArrayUtilities.removeElement(this._watchExpressions, watchExpression);
Jack Lynch5db2a3e2019-11-05 19:12:31211 this._treeOutline.removeChild(watchExpression.treeElement());
Tim van der Lipped7cfd142021-01-07 12:17:24212 this._emptyElement.classList.toggle('hidden', Boolean(this._watchExpressions.length));
Jack Lynch5db2a3e2019-11-05 19:12:31213 if (this._watchExpressions.length === 0) {
214 this._treeOutline.element.remove();
215 }
Blink Reformat4c46d092018-04-07 15:32:37216 }
217
218 this._saveExpressions();
219 }
220
Jan Scheffler35199b92021-03-17 09:51:15221 _contextMenu(event: MouseEvent): void {
Tim van der Lippefbbf9812020-02-13 14:43:46222 const contextMenu = new UI.ContextMenu.ContextMenu(event);
Blink Reformat4c46d092018-04-07 15:32:37223 this._populateContextMenu(contextMenu, event);
224 contextMenu.show();
225 }
226
Jan Scheffler35199b92021-03-17 09:51:15227 _populateContextMenu(contextMenu: UI.ContextMenu.ContextMenu, event: MouseEvent): void {
Blink Reformat4c46d092018-04-07 15:32:37228 let isEditing = false;
Tim van der Lippe1d6e57a2019-09-30 11:55:34229 for (const watchExpression of this._watchExpressions) {
Tim van der Lippe0ad77da2020-10-01 16:38:00230 isEditing = isEditing || watchExpression.isEditing();
Tim van der Lippe1d6e57a2019-09-30 11:55:34231 }
Blink Reformat4c46d092018-04-07 15:32:37232
Tim van der Lippe1d6e57a2019-09-30 11:55:34233 if (!isEditing) {
Tim van der Lippefbbf9812020-02-13 14:43:46234 contextMenu.debugSection().appendItem(
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:32235 i18nString(UIStrings.addWatchExpression), this._addButtonClicked.bind(this));
Tim van der Lippe1d6e57a2019-09-30 11:55:34236 }
Blink Reformat4c46d092018-04-07 15:32:37237
238 if (this._watchExpressions.length > 1) {
239 contextMenu.debugSection().appendItem(
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:32240 i18nString(UIStrings.deleteAllWatchExpressions), this._deleteAllButtonClicked.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37241 }
242
Jack Lynch5db2a3e2019-11-05 19:12:31243 const treeElement = this._treeOutline.treeElementFromEvent(event);
244 if (!treeElement) {
Blink Reformat4c46d092018-04-07 15:32:37245 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34246 }
Jack Lynch5db2a3e2019-11-05 19:12:31247 const currentWatchExpression =
248 this._watchExpressions.find(watchExpression => treeElement.hasAncestorOrSelf(watchExpression.treeElement()));
Tim van der Lippe0ad77da2020-10-01 16:38:00249 if (currentWatchExpression) {
250 currentWatchExpression._populateContextMenu(contextMenu, event);
251 }
Blink Reformat4c46d092018-04-07 15:32:37252 }
253
Jan Scheffler35199b92021-03-17 09:51:15254 _deleteAllButtonClicked(): void {
Blink Reformat4c46d092018-04-07 15:32:37255 this._watchExpressions = [];
256 this._saveExpressions();
257 this.update();
258 }
259
Benedikt Meurer61f1eef2021-03-17 10:38:29260 async _focusAndAddExpressionToWatch(expression: string): Promise<void> {
261 await UI.ViewManager.ViewManager.instance().showView('sources.watch');
PhistucK065e1362018-05-05 09:51:09262 this._createWatchExpression(expression);
263 this._saveExpressions();
Jack Lynch338317a2020-08-19 21:59:49264 this.update();
PhistucK065e1362018-05-05 09:51:09265 }
266
Jan Scheffler35199b92021-03-17 09:51:15267 handleAction(_context: UI.Context.Context, _actionId: string): boolean {
Tim van der Lipped1a00aa2020-08-19 16:03:56268 const frame = UI.Context.Context.instance().flavor(UISourceCodeFrame);
Tim van der Lippe1d6e57a2019-09-30 11:55:34269 if (!frame) {
Blink Reformat4c46d092018-04-07 15:32:37270 return false;
Tim van der Lippe1d6e57a2019-09-30 11:55:34271 }
Blink Reformat4c46d092018-04-07 15:32:37272 const text = frame.textEditor.text(frame.textEditor.selection());
PhistucK065e1362018-05-05 09:51:09273 this._focusAndAddExpressionToWatch(text);
Blink Reformat4c46d092018-04-07 15:32:37274 return true;
275 }
276
Jan Scheffler35199b92021-03-17 09:51:15277 appendApplicableItems(event: Event, contextMenu: UI.ContextMenu.ContextMenu, target: Object): void {
Tim van der Lippefbbf9812020-02-13 14:43:46278 if (target instanceof ObjectUI.ObjectPropertiesSection.ObjectPropertyTreeElement && !target.property.synthetic) {
PhistucK065e1362018-05-05 09:51:09279 contextMenu.debugSection().appendItem(
Benedikt Meurer61f1eef2021-03-17 10:38:29280 i18nString(UIStrings.addPropertyPathToWatch), () => this._focusAndAddExpressionToWatch(target.path()));
PhistucK065e1362018-05-05 09:51:09281 }
282
Tim van der Lipped1a00aa2020-08-19 16:03:56283 const frame = UI.Context.Context.instance().flavor(UISourceCodeFrame);
Tim van der Lippe1d6e57a2019-09-30 11:55:34284 if (!frame || frame.textEditor.selection().isEmpty()) {
Blink Reformat4c46d092018-04-07 15:32:37285 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34286 }
Blink Reformat4c46d092018-04-07 15:32:37287
288 contextMenu.debugSection().appendAction('sources.add-to-watch');
289 }
Tim van der Lippe8987f8f2020-01-03 15:03:16290}
Blink Reformat4c46d092018-04-07 15:32:37291
Tim van der Lippefbbf9812020-02-13 14:43:46292export class WatchExpression extends Common.ObjectWrapper.ObjectWrapper {
Jan Scheffler35199b92021-03-17 09:51:15293 _treeElement!: UI.TreeOutline.TreeElement;
294 _nameElement!: Element;
295 _valueElement!: Element;
296 _expression: string|null;
297 _expandController: ObjectUI.ObjectPropertiesSection.ObjectPropertiesSectionsTreeExpandController;
298 _element: HTMLDivElement;
299 _editing: boolean;
300 _linkifier: Components.Linkifier.Linkifier;
301 _textPrompt?: ObjectUI.ObjectPropertiesSection.ObjectPropertyPrompt;
302 _result?: SDK.RemoteObject.RemoteObject|null;
303 _preventClickTimeout?: number;
304 constructor(
305 expression: string|null,
306 expandController: ObjectUI.ObjectPropertiesSection.ObjectPropertiesSectionsTreeExpandController,
307 linkifier: Components.Linkifier.Linkifier) {
Blink Reformat4c46d092018-04-07 15:32:37308 super();
Tim van der Lippe0ad77da2020-10-01 16:38:00309
Blink Reformat4c46d092018-04-07 15:32:37310 this._expression = expression;
311 this._expandController = expandController;
Tim van der Lippee7f27052020-05-01 15:15:28312 this._element = document.createElement('div');
313 this._element.classList.add('watch-expression');
314 this._element.classList.add('monospace');
Blink Reformat4c46d092018-04-07 15:32:37315 this._editing = false;
316 this._linkifier = linkifier;
317
318 this._createWatchExpression();
319 this.update();
320 }
321
Jan Scheffler35199b92021-03-17 09:51:15322 treeElement(): UI.TreeOutline.TreeElement {
Jack Lynch5db2a3e2019-11-05 19:12:31323 return this._treeElement;
Blink Reformat4c46d092018-04-07 15:32:37324 }
325
Jan Scheffler35199b92021-03-17 09:51:15326 expression(): string|null {
Blink Reformat4c46d092018-04-07 15:32:37327 return this._expression;
328 }
329
Jan Scheffler35199b92021-03-17 09:51:15330 update(): void {
Tim van der Lipped1a00aa2020-08-19 16:03:56331 const currentExecutionContext = UI.Context.Context.instance().flavor(SDK.RuntimeModel.ExecutionContext);
Blink Reformat4c46d092018-04-07 15:32:37332 if (currentExecutionContext && this._expression) {
333 currentExecutionContext
334 .evaluate(
335 {
336 expression: this._expression,
Tim van der Lippe8987f8f2020-01-03 15:03:16337 objectGroup: WatchExpression._watchObjectGroupId,
Blink Reformat4c46d092018-04-07 15:32:37338 includeCommandLineAPI: false,
339 silent: true,
340 returnByValue: false,
Tim van der Lippe0ad77da2020-10-01 16:38:00341 generatePreview: false,
342 allowUnsafeEvalBlockedByCSP: undefined,
343 disableBreaks: undefined,
344 replMode: undefined,
345 throwOnSideEffect: undefined,
346 timeout: undefined,
Blink Reformat4c46d092018-04-07 15:32:37347 },
348 /* userGesture */ false,
349 /* awaitPromise */ false)
Tim van der Lippe0ad77da2020-10-01 16:38:00350 .then(result => {
351 if ('object' in result) {
352 this._createWatchExpression(result.object, result.exceptionDetails);
353 }
354 });
Blink Reformat4c46d092018-04-07 15:32:37355 }
356 }
357
Jan Scheffler35199b92021-03-17 09:51:15358 startEditing(): void {
Blink Reformat4c46d092018-04-07 15:32:37359 this._editing = true;
Jack Lynch5db2a3e2019-11-05 19:12:31360 this._element.removeChildren();
Blink Reformat4c46d092018-04-07 15:32:37361 const newDiv = this._element.createChild('div');
362 newDiv.textContent = this._nameElement.textContent;
Tim van der Lippefbbf9812020-02-13 14:43:46363 this._textPrompt = new ObjectUI.ObjectPropertiesSection.ObjectPropertyPrompt();
Blink Reformat4c46d092018-04-07 15:32:37364 this._textPrompt.renderAsBlock();
Tim van der Lippebcd6b5c2021-01-13 12:31:51365 const proxyElement =
Jan Scheffler35199b92021-03-17 09:51:15366 (this._textPrompt.attachAndStartEditing(newDiv, this._finishEditing.bind(this)) as HTMLElement);
Jack Lynch5db2a3e2019-11-05 19:12:31367 this._treeElement.listItemElement.classList.add('watch-expression-editing');
Michael Liaoced83952020-05-30 22:57:03368 this._treeElement.collapse();
Blink Reformat4c46d092018-04-07 15:32:37369 proxyElement.classList.add('watch-expression-text-prompt-proxy');
370 proxyElement.addEventListener('keydown', this._promptKeyDown.bind(this), false);
Tim van der Lippe0ad77da2020-10-01 16:38:00371 const selection = this._element.getComponentSelection();
372 if (selection) {
373 selection.selectAllChildren(newDiv);
374 }
Blink Reformat4c46d092018-04-07 15:32:37375 }
376
Jan Scheffler35199b92021-03-17 09:51:15377 isEditing(): boolean {
Tim van der Lipped7cfd142021-01-07 12:17:24378 return Boolean(this._editing);
Blink Reformat4c46d092018-04-07 15:32:37379 }
380
Jan Scheffler35199b92021-03-17 09:51:15381 _finishEditing(event: Event, canceled?: boolean): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:34382 if (event) {
Blink Reformat4c46d092018-04-07 15:32:37383 event.consume(canceled);
Tim van der Lippe1d6e57a2019-09-30 11:55:34384 }
Blink Reformat4c46d092018-04-07 15:32:37385
386 this._editing = false;
Jack Lynch5db2a3e2019-11-05 19:12:31387 this._treeElement.listItemElement.classList.remove('watch-expression-editing');
Tim van der Lippe0ad77da2020-10-01 16:38:00388 if (this._textPrompt) {
389 this._textPrompt.detach();
390 const newExpression = canceled ? this._expression : this._textPrompt.text();
391 this._textPrompt = undefined;
392 this._element.removeChildren();
393 this._updateExpression(newExpression);
394 }
Blink Reformat4c46d092018-04-07 15:32:37395 }
396
Jan Scheffler35199b92021-03-17 09:51:15397 _dblClickOnWatchExpression(event: Event): void {
Blink Reformat4c46d092018-04-07 15:32:37398 event.consume();
Tim van der Lippe1d6e57a2019-09-30 11:55:34399 if (!this.isEditing()) {
Blink Reformat4c46d092018-04-07 15:32:37400 this.startEditing();
Tim van der Lippe1d6e57a2019-09-30 11:55:34401 }
Blink Reformat4c46d092018-04-07 15:32:37402 }
403
Jan Scheffler35199b92021-03-17 09:51:15404 _updateExpression(newExpression: string|null): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:34405 if (this._expression) {
Blink Reformat4c46d092018-04-07 15:32:37406 this._expandController.stopWatchSectionsWithId(this._expression);
Tim van der Lippe1d6e57a2019-09-30 11:55:34407 }
Blink Reformat4c46d092018-04-07 15:32:37408 this._expression = newExpression;
409 this.update();
Tim van der Lippe8987f8f2020-01-03 15:03:16410 this.dispatchEventToListeners(WatchExpression.Events.ExpressionUpdated, this);
Blink Reformat4c46d092018-04-07 15:32:37411 }
412
Jan Scheffler35199b92021-03-17 09:51:15413 _deleteWatchExpression(event: Event): void {
Blink Reformat4c46d092018-04-07 15:32:37414 event.consume(true);
415 this._updateExpression(null);
416 }
417
Jan Scheffler35199b92021-03-17 09:51:15418 _createWatchExpression(result?: SDK.RemoteObject.RemoteObject, exceptionDetails?: Protocol.Runtime.ExceptionDetails):
419 void {
Blink Reformat4c46d092018-04-07 15:32:37420 this._result = result || null;
421
Jack Lynch5db2a3e2019-11-05 19:12:31422 this._element.removeChildren();
423 const oldTreeElement = this._treeElement;
424 this._createWatchExpressionTreeElement(result, exceptionDetails);
425 if (oldTreeElement && oldTreeElement.parent) {
426 const root = oldTreeElement.parent;
427 const index = root.indexOfChild(oldTreeElement);
428 root.removeChild(oldTreeElement);
429 root.insertChild(this._treeElement, index);
430 }
431 this._treeElement.select();
432 }
433
Jan Scheffler35199b92021-03-17 09:51:15434 _createWatchExpressionHeader(
435 expressionValue?: SDK.RemoteObject.RemoteObject, exceptionDetails?: Protocol.Runtime.ExceptionDetails): Element {
Jack Lynch5db2a3e2019-11-05 19:12:31436 const headerElement = this._element.createChild('div', 'watch-expression-header');
Tim van der Lippefbbf9812020-02-13 14:43:46437 const deleteButton = UI.Icon.Icon.create('smallicon-cross', 'watch-expression-delete-button');
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:32438 UI.Tooltip.Tooltip.install(deleteButton, i18nString(UIStrings.deleteWatchExpression));
Blink Reformat4c46d092018-04-07 15:32:37439 deleteButton.addEventListener('click', this._deleteWatchExpression.bind(this), false);
440
Jack Lynch5db2a3e2019-11-05 19:12:31441 const titleElement = headerElement.createChild('div', 'watch-expression-title tree-element-title');
Jack Lynche1767de2020-04-22 02:43:55442 titleElement.appendChild(deleteButton);
Tim van der Lippefbbf9812020-02-13 14:43:46443 this._nameElement = ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.createNameElement(this._expression);
Tim van der Lipped7cfd142021-01-07 12:17:24444 if (Boolean(exceptionDetails) || !expressionValue) {
Tim van der Lippee7f27052020-05-01 15:15:28445 this._valueElement = document.createElement('span');
446 this._valueElement.classList.add('watch-expression-error');
447 this._valueElement.classList.add('value');
Blink Reformat4c46d092018-04-07 15:32:37448 titleElement.classList.add('dimmed');
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:32449 this._valueElement.textContent = i18nString(UIStrings.notAvailable);
Philip Pfaffe7426b002020-11-30 15:59:22450 if (exceptionDetails !== undefined && exceptionDetails.exception !== undefined &&
451 exceptionDetails.exception.description !== undefined) {
452 UI.Tooltip.Tooltip.install(this._valueElement, exceptionDetails.exception.description);
453 }
Blink Reformat4c46d092018-04-07 15:32:37454 } else {
Tim van der Lippefbbf9812020-02-13 14:43:46455 const propertyValue =
456 ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.createPropertyValueWithCustomSupport(
Tim van der Lipped7cfd142021-01-07 12:17:24457 expressionValue, Boolean(exceptionDetails), false /* showPreview */, titleElement, this._linkifier);
Connor Moody1a5c0d32019-12-19 07:23:36458 this._valueElement = propertyValue.element;
Blink Reformat4c46d092018-04-07 15:32:37459 }
Tim van der Lippef49e2322020-05-01 15:03:09460 const separatorElement = document.createElement('span');
461 separatorElement.classList.add('watch-expressions-separator');
Blink Reformat4c46d092018-04-07 15:32:37462 separatorElement.textContent = ': ';
Tim van der Lippe0ad77da2020-10-01 16:38:00463 titleElement.append(this._nameElement, separatorElement, this._valueElement);
Blink Reformat4c46d092018-04-07 15:32:37464
Jack Lynch5db2a3e2019-11-05 19:12:31465 return headerElement;
466 }
Blink Reformat4c46d092018-04-07 15:32:37467
Jan Scheffler35199b92021-03-17 09:51:15468 _createWatchExpressionTreeElement(
469 expressionValue?: SDK.RemoteObject.RemoteObject, exceptionDetails?: Protocol.Runtime.ExceptionDetails): void {
Jack Lynch5db2a3e2019-11-05 19:12:31470 const headerElement = this._createWatchExpressionHeader(expressionValue, exceptionDetails);
471
472 if (!exceptionDetails && expressionValue && expressionValue.hasChildren && !expressionValue.customPreview()) {
473 headerElement.classList.add('watch-expression-object-header');
474 this._treeElement = new ObjectUI.ObjectPropertiesSection.RootElement(expressionValue, this._linkifier);
Tim van der Lippe0ad77da2020-10-01 16:38:00475 this._expandController.watchSection(
Jan Scheffler35199b92021-03-17 09:51:15476 (this._expression as string), (this._treeElement as ObjectUI.ObjectPropertiesSection.RootElement));
Jack Lynch5db2a3e2019-11-05 19:12:31477 this._treeElement.toggleOnClick = false;
478 this._treeElement.listItemElement.addEventListener('click', this._onSectionClick.bind(this), false);
479 this._treeElement.listItemElement.addEventListener('dblclick', this._dblClickOnWatchExpression.bind(this));
480 } else {
481 headerElement.addEventListener('dblclick', this._dblClickOnWatchExpression.bind(this));
Tim van der Lippefbbf9812020-02-13 14:43:46482 this._treeElement = new UI.TreeOutline.TreeElement();
Jack Lynch5db2a3e2019-11-05 19:12:31483 }
484 this._treeElement.title = this._element;
485 this._treeElement.listItemElement.classList.add('watch-expression-tree-item');
486 this._treeElement.listItemElement.addEventListener('keydown', event => {
Tim van der Lippebcd6b5c2021-01-13 12:31:51487 if (event.key === 'Enter' && !this.isEditing()) {
Jack Lynch5db2a3e2019-11-05 19:12:31488 this.startEditing();
489 event.consume(true);
490 }
491 });
Blink Reformat4c46d092018-04-07 15:32:37492 }
493
Jan Scheffler35199b92021-03-17 09:51:15494 _onSectionClick(event: Event): void {
Blink Reformat4c46d092018-04-07 15:32:37495 event.consume(true);
Jan Scheffler35199b92021-03-17 09:51:15496 const mouseEvent = (event as MouseEvent);
Tim van der Lippe0ad77da2020-10-01 16:38:00497 if (mouseEvent.detail === 1) {
Jan Scheffler35199b92021-03-17 09:51:15498 this._preventClickTimeout = window.setTimeout(handleClick.bind(this), 333);
Tim van der Lippe0ad77da2020-10-01 16:38:00499 } else if (this._preventClickTimeout !== undefined) {
500 window.clearTimeout(this._preventClickTimeout);
501 this._preventClickTimeout = undefined;
Blink Reformat4c46d092018-04-07 15:32:37502 }
503
Jan Scheffler35199b92021-03-17 09:51:15504 function handleClick(this: WatchExpression): void {
Jack Lynch5db2a3e2019-11-05 19:12:31505 if (!this._treeElement) {
Blink Reformat4c46d092018-04-07 15:32:37506 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34507 }
Blink Reformat4c46d092018-04-07 15:32:37508
Jack Lynch5db2a3e2019-11-05 19:12:31509 if (this._treeElement.expanded) {
510 this._treeElement.collapse();
Tim van der Lippe1d6e57a2019-09-30 11:55:34511 } else {
Jack Lynch5db2a3e2019-11-05 19:12:31512 this._treeElement.expand();
Tim van der Lippe1d6e57a2019-09-30 11:55:34513 }
Blink Reformat4c46d092018-04-07 15:32:37514 }
515 }
516
Jan Scheffler35199b92021-03-17 09:51:15517 _promptKeyDown(event: KeyboardEvent): void {
Tim van der Lippebcd6b5c2021-01-13 12:31:51518 if (event.key === 'Enter' || isEscKey(event)) {
Blink Reformat4c46d092018-04-07 15:32:37519 this._finishEditing(event, isEscKey(event));
Tim van der Lippe1d6e57a2019-09-30 11:55:34520 }
Blink Reformat4c46d092018-04-07 15:32:37521 }
522
Jan Scheffler35199b92021-03-17 09:51:15523 _populateContextMenu(contextMenu: UI.ContextMenu.ContextMenu, event: Event): void {
Blink Reformat4c46d092018-04-07 15:32:37524 if (!this.isEditing()) {
525 contextMenu.editSection().appendItem(
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:32526 i18nString(UIStrings.deleteWatchExpression), this._updateExpression.bind(this, null));
Blink Reformat4c46d092018-04-07 15:32:37527 }
528
Tim van der Lippe1d6e57a2019-09-30 11:55:34529 if (!this.isEditing() && this._result && (this._result.type === 'number' || this._result.type === 'string')) {
Tim van der Lippefbbf9812020-02-13 14:43:46530 contextMenu.clipboardSection().appendItem(
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:32531 i18nString(UIStrings.copyValue), this._copyValueButtonClicked.bind(this));
Tim van der Lippe1d6e57a2019-09-30 11:55:34532 }
Blink Reformat4c46d092018-04-07 15:32:37533
Sigurd Schneider090c3b62020-11-10 10:26:49534 const target = UI.UIUtils.deepElementFromEvent(event);
Tim van der Lippe0ad77da2020-10-01 16:38:00535 if (target && this._valueElement.isSelfOrAncestor(target) && this._result) {
Blink Reformat4c46d092018-04-07 15:32:37536 contextMenu.appendApplicableItems(this._result);
Tim van der Lippe1d6e57a2019-09-30 11:55:34537 }
Blink Reformat4c46d092018-04-07 15:32:37538 }
539
Jan Scheffler35199b92021-03-17 09:51:15540 _copyValueButtonClicked(): void {
Tim van der Lippefbbf9812020-02-13 14:43:46541 Host.InspectorFrontendHost.InspectorFrontendHostInstance.copyText(this._valueElement.textContent);
Blink Reformat4c46d092018-04-07 15:32:37542 }
Jan Scheffler35199b92021-03-17 09:51:15543
544 // TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration)
545 // eslint-disable-next-line @typescript-eslint/naming-convention
546 static readonly _watchObjectGroupId = 'watch-group';
Tim van der Lippe8987f8f2020-01-03 15:03:16547}
Blink Reformat4c46d092018-04-07 15:32:37548
Jan Scheffler35199b92021-03-17 09:51:15549export namespace WatchExpression {
550 // TODO(crbug.com/1167717): Make this a const enum again
551 // eslint-disable-next-line rulesdir/const_enum
552 export const Events = {
553 ExpressionUpdated: Symbol('ExpressionUpdated'),
554 };
555}