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, |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 75 | TextEditor.Config.autocompletion, |
| 76 | CodeMirror.EditorView.lineWrapping, |
| 77 | TextEditor.Config.showCompletionHint, |
| 78 | TextEditor.JavaScript.completion(), |
| 79 | TextEditor.JavaScript.argumentHints(), |
| 80 | ]; |
| 81 | |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 82 | this.onFinish = onFinish; |
| 83 | this.finished = false; |
Erik Luo | 8f91a0e | 2018-12-22 21:46:37 | [diff] [blame] | 84 | this.element.tabIndex = -1; |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 85 | |
Tim van der Lippe | 8987f8f | 2020-01-03 15:03:16 | [diff] [blame] | 86 | const logpointPrefix = LogpointPrefix; |
Paul Lewis | 3994495 | 2020-01-22 15:45:18 | [diff] [blame] | 87 | const logpointSuffix = LogpointSuffix; |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 88 | this.isLogpoint = oldCondition.startsWith(logpointPrefix) && oldCondition.endsWith(logpointSuffix); |
| 89 | if (this.isLogpoint) { |
Erik Luo | 5b2b752 | 2018-12-03 20:35:06 | [diff] [blame] | 90 | oldCondition = oldCondition.substring(logpointPrefix.length, oldCondition.length - logpointSuffix.length); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 91 | } |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 92 | this.isLogpoint = this.isLogpoint || preferLogpoint; |
Erik Luo | 5b2b752 | 2018-12-03 20:35:06 | [diff] [blame] | 93 | |
Pavel Feldman | db31091 | 2019-01-30 00:31:20 | [diff] [blame] | 94 | this.element.classList.add('sources-edit-breakpoint-dialog'); |
Tim van der Lippe | fbbf981 | 2020-02-13 14:43:46 | [diff] [blame] | 95 | const toolbar = new UI.Toolbar.Toolbar('source-frame-breakpoint-toolbar', this.contentElement); |
Pavel Feldman | db31091 | 2019-01-30 00:31:20 | [diff] [blame] | 96 | toolbar.appendText(`Line ${editorLineNumber + 1}:`); |
Erik Luo | 0941a44 | 2018-12-08 05:55:22 | [diff] [blame] | 97 | |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 98 | this.typeSelector = |
| 99 | new UI.Toolbar.ToolbarComboBox(this.onTypeChanged.bind(this), i18nString(UIStrings.breakpointType)); |
| 100 | this.typeSelector.createOption(i18nString(UIStrings.breakpoint), BreakpointType.Breakpoint); |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 101 | const conditionalOption = |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 102 | this.typeSelector.createOption(i18nString(UIStrings.conditionalBreakpoint), BreakpointType.Conditional); |
| 103 | const logpointOption = this.typeSelector.createOption(i18nString(UIStrings.logpoint), BreakpointType.Logpoint); |
| 104 | this.typeSelector.select(this.isLogpoint ? logpointOption : conditionalOption); |
| 105 | toolbar.appendToolbarItem(this.typeSelector); |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 106 | |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 107 | const content = oldCondition || ''; |
Marijn Haverbeke | 9a58ba7 | 2021-10-15 12:59:29 | [diff] [blame] | 108 | const finishIfComplete = (view: CodeMirror.EditorView): boolean => { |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 109 | if (TextEditor.JavaScript.isExpressionComplete(view.state)) { |
Marijn Haverbeke | 9a58ba7 | 2021-10-15 12:59:29 | [diff] [blame] | 110 | this.finishEditing(true, this.editor.state.doc.toString()); |
| 111 | return true; |
| 112 | } |
| 113 | return false; |
| 114 | }; |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 115 | const keymap = [ |
| 116 | { |
| 117 | key: 'Mod-Enter', |
Marijn Haverbeke | 9a58ba7 | 2021-10-15 12:59:29 | [diff] [blame] | 118 | run: finishIfComplete, |
| 119 | }, |
| 120 | { |
| 121 | key: 'Enter', |
| 122 | run: finishIfComplete, |
| 123 | }, |
| 124 | { |
Marijn Haverbeke | 9a58ba7 | 2021-10-15 12:59:29 | [diff] [blame] | 125 | key: 'Shift-Enter', |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 126 | run: CodeMirror.insertNewlineAndIndent, |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 127 | }, |
| 128 | { |
| 129 | key: 'Escape', |
| 130 | run: (): boolean => { |
| 131 | this.finishEditing(false, ''); |
| 132 | return true; |
| 133 | }, |
| 134 | }, |
| 135 | ]; |
| 136 | |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 137 | this.placeholderCompartment = new CodeMirror.Compartment(); |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 138 | |
Marijn Haverbeke | 9a58ba7 | 2021-10-15 12:59:29 | [diff] [blame] | 139 | const editorWrapper = this.contentElement.appendChild(document.createElement('div')); |
| 140 | editorWrapper.classList.add('condition-editor'); |
| 141 | |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 142 | this.editor = new TextEditor.TextEditor.TextEditor(CodeMirror.EditorState.create({ |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 143 | doc: content, |
| 144 | selection: {anchor: 0, head: content.length}, |
| 145 | extensions: [ |
| 146 | this.placeholderCompartment.of(this.getPlaceholder()), |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 147 | CodeMirror.keymap.of(keymap), |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 148 | editorConfig, |
| 149 | ], |
| 150 | })); |
Marijn Haverbeke | 9a58ba7 | 2021-10-15 12:59:29 | [diff] [blame] | 151 | editorWrapper.appendChild(this.editor); |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 152 | |
| 153 | this.updateTooltip(); |
Marijn Haverbeke | 9a58ba7 | 2021-10-15 12:59:29 | [diff] [blame] | 154 | |
Andres Olivares | 0ed4bef | 2021-03-15 17:15:51 | [diff] [blame] | 155 | this.element.addEventListener('blur', event => { |
Olivia Flynn | 989d272 | 2021-07-07 01:06:56 | [diff] [blame] | 156 | if (!event.relatedTarget || |
| 157 | (event.relatedTarget && !(event.relatedTarget as Node).isSelfOrDescendant(this.element))) { |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 158 | this.finishEditing(true, this.editor.state.doc.toString()); |
Andres Olivares | 0ed4bef | 2021-03-15 17:15:51 | [diff] [blame] | 159 | } |
| 160 | }, true); |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 161 | } |
| 162 | |
Jan Scheffler | 35199b9 | 2021-03-17 09:51:15 | [diff] [blame] | 163 | focusEditor(): void { |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 164 | this.editor.editor.focus(); |
Andres Olivares | 0ed4bef | 2021-03-15 17:15:51 | [diff] [blame] | 165 | } |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 166 | private static conditionForLogpoint(condition: string): string { |
Paul Lewis | 3994495 | 2020-01-22 15:45:18 | [diff] [blame] | 167 | return `${LogpointPrefix}${condition}${LogpointSuffix}`; |
Erik Luo | 5b2b752 | 2018-12-03 20:35:06 | [diff] [blame] | 168 | } |
| 169 | |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 170 | private onTypeChanged(): void { |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 171 | const type = this.breakpointType; |
| 172 | if (type === BreakpointType.Breakpoint) { |
| 173 | this.finishEditing(true, ''); |
| 174 | } else { |
Tim van der Lippe | c1ab112 | 2021-11-12 16:10:31 | [diff] [blame] | 175 | this.editor.dispatch({effects: this.placeholderCompartment.reconfigure(this.getPlaceholder())}); |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 176 | this.updateTooltip(); |
Erik Luo | 0941a44 | 2018-12-08 05:55:22 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 180 | private get breakpointType(): string|null { |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 181 | const option = this.typeSelector.selectedOption(); |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 182 | return option ? option.value : null; |
| 183 | } |
| 184 | |
| 185 | private getPlaceholder(): CodeMirror.Extension { |
| 186 | const type = this.breakpointType; |
| 187 | if (type === BreakpointType.Conditional) { |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 188 | return CodeMirror.placeholder(i18nString(UIStrings.expressionToCheckBeforePausingEg)); |
Philip Pfaffe | f2ef8f3 | 2020-10-26 13:06:06 | [diff] [blame] | 189 | } |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 190 | if (type === BreakpointType.Logpoint) { |
Marijn Haverbeke | b0e3e7c | 2021-11-15 09:51:52 | [diff] [blame] | 191 | return CodeMirror.placeholder(i18nString(UIStrings.logMessageEgXIsX)); |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 192 | } |
| 193 | return []; |
| 194 | } |
| 195 | |
| 196 | private updateTooltip(): void { |
| 197 | const type = this.breakpointType; |
| 198 | if (type === BreakpointType.Conditional) { |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 199 | UI.Tooltip.Tooltip.install((this.typeSelector.element), i18nString(UIStrings.pauseOnlyWhenTheConditionIsTrue)); |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 200 | } else if (type === BreakpointType.Logpoint) { |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 201 | UI.Tooltip.Tooltip.install((this.typeSelector.element), i18nString(UIStrings.logAMessageToConsoleDoNotBreak)); |
Erik Luo | 0941a44 | 2018-12-08 05:55:22 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 205 | private finishEditing(committed: boolean, condition: string): void { |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 206 | if (this.finished) { |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 207 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 208 | } |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 209 | this.finished = true; |
Marijn Haverbeke | cf7dcfa | 2021-10-06 15:35:02 | [diff] [blame] | 210 | this.editor.remove(); |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 211 | if (this.isLogpoint) { |
| 212 | condition = BreakpointEditDialog.conditionForLogpoint(condition); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 213 | } |
Jan Scheffler | a222c63 | 2021-08-13 12:46:15 | [diff] [blame] | 214 | this.onFinish({committed, condition}); |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 215 | } |
| 216 | |
Kriti Sapra | ae8f17e | 2021-08-18 10:28:02 | [diff] [blame] | 217 | wasShown(): void { |
| 218 | super.wasShown(); |
| 219 | this.registerCSSFiles([breakpointEditDialogStyles]); |
| 220 | } |
Tim van der Lippe | 8987f8f | 2020-01-03 15:03:16 | [diff] [blame] | 221 | } |
Erik Luo | 5b2b752 | 2018-12-03 20:35:06 | [diff] [blame] | 222 | |
Tim van der Lippe | 8987f8f | 2020-01-03 15:03:16 | [diff] [blame] | 223 | export const LogpointPrefix = '/** DEVTOOLS_LOGPOINT */ console.log('; |
Paul Lewis | 3994495 | 2020-01-22 15:45:18 | [diff] [blame] | 224 | export const LogpointSuffix = ')'; |
Erik Luo | 0941a44 | 2018-12-08 05:55:22 | [diff] [blame] | 225 | |
Tim van der Lippe | 8987f8f | 2020-01-03 15:03:16 | [diff] [blame] | 226 | export const BreakpointType = { |
Erik Luo | 0941a44 | 2018-12-08 05:55:22 | [diff] [blame] | 227 | Breakpoint: 'Breakpoint', |
| 228 | Conditional: 'Conditional', |
| 229 | Logpoint: 'Logpoint', |
| 230 | }; |