Tim van der Lippe | 83f02be | 2020-01-23 11:11:40 | [diff] [blame] | 1 | // Copyright 2018 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. |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 4 | |
Tim van der Lippe | 021c757 | 2021-04-19 10:49:43 | [diff] [blame] | 5 | import * as i18n from '../../core/i18n/i18n.js'; |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 6 | import * as CodeMirror from '../../third_party/codemirror.next/codemirror.next.js'; |
| 7 | import * as TextEditor from '../../ui/components/text_editor/text_editor.js'; |
Tim van der Lippe | 021c757 | 2021-04-19 10:49:43 | [diff] [blame] | 8 | import * as UI from '../../ui/legacy/legacy.js'; |
Tim van der Lippe | fbbf981 | 2020-02-13 14:43:46 | [diff] [blame] | 9 | |
Kriti Sapra | ae8f17e | 2021-08-18 10:28:02 | [diff] [blame] | 10 | import breakpointEditDialogStyles from './breakpointEditDialog.css.js'; |
| 11 | |
Simon Zünd | 697fb0b | 2021-03-01 10:12:42 | [diff] [blame] | 12 | const UIStrings = { |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 13 | /** |
| 14 | *@description Screen reader label for a select box that chooses the breakpoint type in the Sources panel when editing a breakpoint |
| 15 | */ |
| 16 | breakpointType: 'Breakpoint type', |
| 17 | /** |
| 18 | *@description Text in Breakpoint Edit Dialog of the Sources panel |
| 19 | */ |
| 20 | breakpoint: 'Breakpoint', |
| 21 | /** |
| 22 | *@description Text in Breakpoint Edit Dialog of the Sources panel |
| 23 | */ |
| 24 | conditionalBreakpoint: 'Conditional breakpoint', |
| 25 | /** |
| 26 | *@description Text in Breakpoint Edit Dialog of the Sources panel |
| 27 | */ |
| 28 | logpoint: 'Logpoint', |
| 29 | /** |
| 30 | *@description Text in Breakpoint Edit Dialog of the Sources panel |
| 31 | */ |
| 32 | expressionToCheckBeforePausingEg: 'Expression to check before pausing, e.g. x > 5', |
| 33 | /** |
| 34 | *@description Type selector element title in Breakpoint Edit Dialog of the Sources panel |
| 35 | */ |
| 36 | pauseOnlyWhenTheConditionIsTrue: 'Pause only when the condition is true', |
| 37 | /** |
Simon Zünd | 059aa76 | 2021-07-21 05:44:54 | [diff] [blame] | 38 | *@description Text in Breakpoint Edit Dialog of the Sources panel. It is used as |
| 39 | *the placeholder for a text input field before the user enters text. Provides the user with |
Simon Zünd | 4cb2aab | 2021-06-22 05:27:12 | [diff] [blame] | 40 | *an example on how to use Logpoints. 'Log' is a verb and 'message' is a noun. |
| 41 | *See: https://developer.chrome.com/blog/new-in-devtools-73/#logpoints |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 42 | */ |
Simon Zünd | 4cb2aab | 2021-06-22 05:27:12 | [diff] [blame] | 43 | logMessageEgXIsX: 'Log message, e.g. `\'x is\', x`', |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 44 | /** |
| 45 | *@description Type selector element title in Breakpoint Edit Dialog of the Sources panel |
| 46 | */ |
| 47 | logAMessageToConsoleDoNotBreak: 'Log a message to Console, do not break', |
| 48 | }; |
Tim van der Lippe | 021c757 | 2021-04-19 10:49:43 | [diff] [blame] | 49 | const str_ = i18n.i18n.registerUIStrings('panels/sources/BreakpointEditDialog.ts', UIStrings); |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 50 | const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 51 | |
Tim van der Lippe | fbbf981 | 2020-02-13 14:43:46 | [diff] [blame] | 52 | export class BreakpointEditDialog extends UI.Widget.Widget { |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 53 | private readonly onFinish: (arg0: { |
Jan Scheffler | 35199b9 | 2021-03-17 09:51:15 | [diff] [blame] | 54 | committed: boolean, |
| 55 | condition: string, |
| 56 | }) => Promise<void>; |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 57 | private finished: boolean; |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 58 | private editor: TextEditor.TextEditor.TextEditor; |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 59 | private isLogpoint: boolean; |
| 60 | private readonly typeSelector: UI.Toolbar.ToolbarComboBox; |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 61 | private placeholderCompartment: CodeMirror.Compartment; |
Jan Scheffler | 35199b9 | 2021-03-17 09:51:15 | [diff] [blame] | 62 | |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 63 | constructor( |
| 64 | editorLineNumber: number, |
| 65 | oldCondition: string, |
| 66 | preferLogpoint: boolean, |
| 67 | onFinish: (arg0: {committed: boolean, condition: string}) => Promise<void>, |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 68 | ) { |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 69 | super(true); |
Kriti Sapra | ae8f17e | 2021-08-18 10:28:02 | [diff] [blame] | 70 | |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 71 | const editorConfig = [ |
| 72 | CodeMirror.javascript.javascriptLanguage, |
| 73 | TextEditor.Config.baseConfiguration(oldCondition || ''), |
Marijn Haverbeke | f034f0b | 2021-11-22 14:20:52 | [diff] [blame] | 74 | TextEditor.Config.closeBrackets, |
Benedikt Meurer | 89f259d | 2022-12-13 07:03:55 | [diff] [blame^] | 75 | TextEditor.Config.autocompletion.instance(), |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 76 | CodeMirror.EditorView.lineWrapping, |
| 77 | TextEditor.Config.showCompletionHint, |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 78 | TextEditor.JavaScript.argumentHints(), |
| 79 | ]; |
| 80 | |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 81 | this.onFinish = onFinish; |
| 82 | this.finished = false; |
Erik Luo | 8f91a0e | 2018-12-22 21:46:37 | [diff] [blame] | 83 | this.element.tabIndex = -1; |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 84 | |
Tim van der Lippe | 8987f8f | 2020-01-03 15:03:16 | [diff] [blame] | 85 | const logpointPrefix = LogpointPrefix; |
Paul Lewis | 3994495 | 2020-01-22 15:45:18 | [diff] [blame] | 86 | const logpointSuffix = LogpointSuffix; |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 87 | this.isLogpoint = oldCondition.startsWith(logpointPrefix) && oldCondition.endsWith(logpointSuffix); |
| 88 | if (this.isLogpoint) { |
Erik Luo | 5b2b752 | 2018-12-03 20:35:06 | [diff] [blame] | 89 | oldCondition = oldCondition.substring(logpointPrefix.length, oldCondition.length - logpointSuffix.length); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 90 | } |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 91 | this.isLogpoint = this.isLogpoint || preferLogpoint; |
Erik Luo | 5b2b752 | 2018-12-03 20:35:06 | [diff] [blame] | 92 | |
Pavel Feldman | db31091 | 2019-01-30 00:31:20 | [diff] [blame] | 93 | this.element.classList.add('sources-edit-breakpoint-dialog'); |
Tim van der Lippe | fbbf981 | 2020-02-13 14:43:46 | [diff] [blame] | 94 | const toolbar = new UI.Toolbar.Toolbar('source-frame-breakpoint-toolbar', this.contentElement); |
Pavel Feldman | db31091 | 2019-01-30 00:31:20 | [diff] [blame] | 95 | toolbar.appendText(`Line ${editorLineNumber + 1}:`); |
Erik Luo | 0941a44 | 2018-12-08 05:55:22 | [diff] [blame] | 96 | |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 97 | this.typeSelector = |
| 98 | new UI.Toolbar.ToolbarComboBox(this.onTypeChanged.bind(this), i18nString(UIStrings.breakpointType)); |
| 99 | this.typeSelector.createOption(i18nString(UIStrings.breakpoint), BreakpointType.Breakpoint); |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 100 | const conditionalOption = |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 101 | this.typeSelector.createOption(i18nString(UIStrings.conditionalBreakpoint), BreakpointType.Conditional); |
| 102 | const logpointOption = this.typeSelector.createOption(i18nString(UIStrings.logpoint), BreakpointType.Logpoint); |
| 103 | this.typeSelector.select(this.isLogpoint ? logpointOption : conditionalOption); |
| 104 | toolbar.appendToolbarItem(this.typeSelector); |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 105 | |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 106 | const content = oldCondition || ''; |
Marijn Haverbeke | 9a58ba7 | 2021-10-15 12:59:29 | [diff] [blame] | 107 | const finishIfComplete = (view: CodeMirror.EditorView): boolean => { |
Tim van der Lippe | 2d9a95c | 2022-01-04 15:18:03 | [diff] [blame] | 108 | void TextEditor.JavaScript.isExpressionComplete(view.state.doc.toString()).then((complete): void => { |
Marijn Haverbeke | 0adc723 | 2021-12-08 16:56:57 | [diff] [blame] | 109 | if (complete) { |
| 110 | this.finishEditing(true, this.editor.state.doc.toString()); |
| 111 | } else { |
| 112 | CodeMirror.insertNewlineAndIndent(view); |
| 113 | } |
| 114 | }); |
| 115 | return true; |
Marijn Haverbeke | 9a58ba7 | 2021-10-15 12:59:29 | [diff] [blame] | 116 | }; |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 117 | const keymap = [ |
| 118 | { |
| 119 | key: 'Mod-Enter', |
Marijn Haverbeke | 9a58ba7 | 2021-10-15 12:59:29 | [diff] [blame] | 120 | run: finishIfComplete, |
| 121 | }, |
| 122 | { |
| 123 | key: 'Enter', |
| 124 | run: finishIfComplete, |
| 125 | }, |
| 126 | { |
Marijn Haverbeke | 9a58ba7 | 2021-10-15 12:59:29 | [diff] [blame] | 127 | key: 'Shift-Enter', |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 128 | run: CodeMirror.insertNewlineAndIndent, |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 129 | }, |
| 130 | { |
| 131 | key: 'Escape', |
| 132 | run: (): boolean => { |
| 133 | this.finishEditing(false, ''); |
| 134 | return true; |
| 135 | }, |
| 136 | }, |
| 137 | ]; |
| 138 | |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 139 | this.placeholderCompartment = new CodeMirror.Compartment(); |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 140 | |
Marijn Haverbeke | 9a58ba7 | 2021-10-15 12:59:29 | [diff] [blame] | 141 | const editorWrapper = this.contentElement.appendChild(document.createElement('div')); |
| 142 | editorWrapper.classList.add('condition-editor'); |
| 143 | |
Ergün Erdoğmuş | c08685a | 2022-10-06 07:24:32 | [diff] [blame] | 144 | this.editor = new TextEditor.TextEditor.TextEditor(CodeMirror.EditorState.create({ |
| 145 | doc: content, |
| 146 | selection: {anchor: 0, head: content.length}, |
| 147 | extensions: [ |
| 148 | this.placeholderCompartment.of(this.getPlaceholder()), |
| 149 | CodeMirror.keymap.of(keymap), |
| 150 | editorConfig, |
| 151 | ], |
| 152 | })); |
Marijn Haverbeke | 9a58ba7 | 2021-10-15 12:59:29 | [diff] [blame] | 153 | editorWrapper.appendChild(this.editor); |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 154 | |
| 155 | this.updateTooltip(); |
Marijn Haverbeke | 9a58ba7 | 2021-10-15 12:59:29 | [diff] [blame] | 156 | |
Andres Olivares | 0ed4bef | 2021-03-15 17:15:51 | [diff] [blame] | 157 | this.element.addEventListener('blur', event => { |
Olivia Flynn | 989d272 | 2021-07-07 01:06:56 | [diff] [blame] | 158 | if (!event.relatedTarget || |
| 159 | (event.relatedTarget && !(event.relatedTarget as Node).isSelfOrDescendant(this.element))) { |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 160 | this.finishEditing(true, this.editor.state.doc.toString()); |
Andres Olivares | 0ed4bef | 2021-03-15 17:15:51 | [diff] [blame] | 161 | } |
| 162 | }, true); |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 163 | } |
| 164 | |
Jan Scheffler | 35199b9 | 2021-03-17 09:51:15 | [diff] [blame] | 165 | focusEditor(): void { |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 166 | this.editor.editor.focus(); |
Andres Olivares | 0ed4bef | 2021-03-15 17:15:51 | [diff] [blame] | 167 | } |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 168 | private static conditionForLogpoint(condition: string): string { |
Paul Lewis | 3994495 | 2020-01-22 15:45:18 | [diff] [blame] | 169 | return `${LogpointPrefix}${condition}${LogpointSuffix}`; |
Erik Luo | 5b2b752 | 2018-12-03 20:35:06 | [diff] [blame] | 170 | } |
| 171 | |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 172 | private onTypeChanged(): void { |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 173 | const type = this.breakpointType; |
Jaroslav Sevcik | 895a875 | 2021-12-15 18:13:04 | [diff] [blame] | 174 | this.isLogpoint = type === BreakpointType.Logpoint; |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 175 | if (type === BreakpointType.Breakpoint) { |
| 176 | this.finishEditing(true, ''); |
Jaroslav Sevcik | 895a875 | 2021-12-15 18:13:04 | [diff] [blame] | 177 | return; |
Erik Luo | 0941a44 | 2018-12-08 05:55:22 | [diff] [blame] | 178 | } |
Jaroslav Sevcik | 895a875 | 2021-12-15 18:13:04 | [diff] [blame] | 179 | this.editor.dispatch({effects: this.placeholderCompartment.reconfigure(this.getPlaceholder())}); |
| 180 | this.updateTooltip(); |
Erik Luo | 0941a44 | 2018-12-08 05:55:22 | [diff] [blame] | 181 | } |
| 182 | |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 183 | private get breakpointType(): string|null { |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 184 | const option = this.typeSelector.selectedOption(); |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 185 | return option ? option.value : null; |
| 186 | } |
| 187 | |
| 188 | private getPlaceholder(): CodeMirror.Extension { |
| 189 | const type = this.breakpointType; |
| 190 | if (type === BreakpointType.Conditional) { |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 191 | return CodeMirror.placeholder(i18nString(UIStrings.expressionToCheckBeforePausingEg)); |
Philip Pfaffe | f2ef8f3 | 2020-10-26 13:06:06 | [diff] [blame] | 192 | } |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 193 | if (type === BreakpointType.Logpoint) { |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 194 | return CodeMirror.placeholder(i18nString(UIStrings.logMessageEgXIsX)); |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 195 | } |
| 196 | return []; |
| 197 | } |
| 198 | |
| 199 | private updateTooltip(): void { |
| 200 | const type = this.breakpointType; |
| 201 | if (type === BreakpointType.Conditional) { |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 202 | UI.Tooltip.Tooltip.install((this.typeSelector.element), i18nString(UIStrings.pauseOnlyWhenTheConditionIsTrue)); |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 203 | } else if (type === BreakpointType.Logpoint) { |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 204 | UI.Tooltip.Tooltip.install((this.typeSelector.element), i18nString(UIStrings.logAMessageToConsoleDoNotBreak)); |
Erik Luo | 0941a44 | 2018-12-08 05:55:22 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Jaroslav Sevcik | 59fdfc1 | 2022-01-12 17:44:26 | [diff] [blame] | 208 | finishEditing(committed: boolean, condition: string): void { |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 209 | if (this.finished) { |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 210 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 211 | } |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 212 | this.finished = true; |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 213 | this.editor.remove(); |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 214 | if (this.isLogpoint) { |
| 215 | condition = BreakpointEditDialog.conditionForLogpoint(condition); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 216 | } |
Tim van der Lippe | 2d9a95c | 2022-01-04 15:18:03 | [diff] [blame] | 217 | void this.onFinish({committed, condition}); |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 218 | } |
| 219 | |
Kriti Sapra | ae8f17e | 2021-08-18 10:28:02 | [diff] [blame] | 220 | wasShown(): void { |
| 221 | super.wasShown(); |
| 222 | this.registerCSSFiles([breakpointEditDialogStyles]); |
| 223 | } |
Tim van der Lippe | 8987f8f | 2020-01-03 15:03:16 | [diff] [blame] | 224 | } |
Erik Luo | 5b2b752 | 2018-12-03 20:35:06 | [diff] [blame] | 225 | |
Tim van der Lippe | 8987f8f | 2020-01-03 15:03:16 | [diff] [blame] | 226 | export const LogpointPrefix = '/** DEVTOOLS_LOGPOINT */ console.log('; |
Paul Lewis | 3994495 | 2020-01-22 15:45:18 | [diff] [blame] | 227 | export const LogpointSuffix = ')'; |
Erik Luo | 0941a44 | 2018-12-08 05:55:22 | [diff] [blame] | 228 | |
Tim van der Lippe | 8987f8f | 2020-01-03 15:03:16 | [diff] [blame] | 229 | export const BreakpointType = { |
Erik Luo | 0941a44 | 2018-12-08 05:55:22 | [diff] [blame] | 230 | Breakpoint: 'Breakpoint', |
| 231 | Conditional: 'Conditional', |
| 232 | Logpoint: 'Logpoint', |
| 233 | }; |