Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The Chromium Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
| 7 | Sources.BreakpointEditDialog = class extends UI.Widget { |
| 8 | /** |
| 9 | * @param {number} editorLineNumber |
| 10 | * @param {string} oldCondition |
Erik Luo | 5b2b752 | 2018-12-03 20:35:06 | [diff] [blame^] | 11 | * @param {boolean} preferLogpoint |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 12 | * @param {function({committed: boolean, condition: string})} onFinish |
| 13 | */ |
Erik Luo | 5b2b752 | 2018-12-03 20:35:06 | [diff] [blame^] | 14 | constructor(editorLineNumber, oldCondition, preferLogpoint, onFinish) { |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 15 | super(true); |
| 16 | this.registerRequiredCSS('sources/breakpointEditDialog.css'); |
| 17 | this._onFinish = onFinish; |
| 18 | this._finished = false; |
| 19 | /** @type {?UI.TextEditor} */ |
| 20 | this._editor = null; |
| 21 | |
Erik Luo | 5b2b752 | 2018-12-03 20:35:06 | [diff] [blame^] | 22 | const logpointPrefix = Sources.BreakpointEditDialog._LogpointPrefix; |
| 23 | const logpointSuffix = Sources.BreakpointEditDialog._LogpointSuffix; |
| 24 | this._isLogpoint = oldCondition.startsWith(logpointPrefix) && oldCondition.endsWith(logpointSuffix); |
| 25 | if (this._isLogpoint) |
| 26 | oldCondition = oldCondition.substring(logpointPrefix.length, oldCondition.length - logpointSuffix.length); |
| 27 | this._isLogpoint = this._isLogpoint || preferLogpoint; |
| 28 | |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 29 | const labelElement = this.contentElement.createChild('label', 'source-frame-breakpoint-message'); |
| 30 | labelElement.htmlFor = 'source-frame-breakpoint-condition'; |
Erik Luo | 5b2b752 | 2018-12-03 20:35:06 | [diff] [blame^] | 31 | const labelText = this._isLogpoint ? ls`On line ${editorLineNumber + 1}, log to the Console:` : ls |
| 32 | `The breakpoint on line ${editorLineNumber + 1} will stop only if this expression is true:`; |
| 33 | labelElement.createTextChild(labelText); |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 34 | |
| 35 | self.runtime.extension(UI.TextEditorFactory).instance().then(factory => { |
| 36 | this._editor = |
| 37 | factory.createEditor({lineNumbers: false, lineWrapping: true, mimeType: 'javascript', autoHeight: true}); |
| 38 | this._editor.configureAutocomplete(ObjectUI.JavaScriptAutocompleteConfig.createConfigForEditor(this._editor)); |
| 39 | if (oldCondition) |
| 40 | this._editor.setText(oldCondition); |
| 41 | this._editor.widget().show(this.contentElement); |
| 42 | this._editor.widget().element.id = 'source-frame-breakpoint-condition'; |
| 43 | this._editor.setSelection(this._editor.fullRange()); |
| 44 | this._editor.widget().focus(); |
| 45 | this._editor.widget().element.addEventListener('keydown', this._onKeyDown.bind(this), true); |
| 46 | this._editor.widget().element.addEventListener('blur', event => { |
| 47 | if (event.relatedTarget && !event.relatedTarget.isSelfOrDescendant(this._editor.widget().element)) |
| 48 | this._finishEditing(true); |
| 49 | }, true); |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | /** |
Erik Luo | 5b2b752 | 2018-12-03 20:35:06 | [diff] [blame^] | 54 | * @param {string} condition |
| 55 | * @return {string} |
| 56 | */ |
| 57 | static _conditionForLogpoint(condition) { |
| 58 | return `${Sources.BreakpointEditDialog._LogpointPrefix}${condition}${Sources.BreakpointEditDialog._LogpointSuffix}`; |
| 59 | } |
| 60 | |
| 61 | /** |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 62 | * @param {boolean} committed |
| 63 | */ |
| 64 | _finishEditing(committed) { |
| 65 | if (this._finished) |
| 66 | return; |
| 67 | this._finished = true; |
| 68 | this._editor.widget().detach(); |
Erik Luo | 5b2b752 | 2018-12-03 20:35:06 | [diff] [blame^] | 69 | let condition = this._editor.text(); |
| 70 | if (this._isLogpoint) |
| 71 | condition = Sources.BreakpointEditDialog._conditionForLogpoint(condition); |
Erik Luo | 0b789b6 | 2018-11-21 19:31:46 | [diff] [blame] | 72 | this._onFinish({committed, condition}); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param {!Event} event |
| 77 | */ |
| 78 | async _onKeyDown(event) { |
| 79 | if (isEnterKey(event) && !event.shiftKey) { |
| 80 | event.consume(true); |
| 81 | const expression = this._editor.text(); |
| 82 | if (event.ctrlKey || await ObjectUI.JavaScriptAutocomplete.isExpressionComplete(expression)) |
| 83 | this._finishEditing(true); |
| 84 | else |
| 85 | this._editor.newlineAndIndent(); |
| 86 | } |
| 87 | if (isEscKey(event)) |
| 88 | this._finishEditing(false); |
| 89 | } |
| 90 | }; |
Erik Luo | 5b2b752 | 2018-12-03 20:35:06 | [diff] [blame^] | 91 | |
| 92 | Sources.BreakpointEditDialog._LogpointPrefix = '/** DEVTOOLS_LOGPOINT */ console.log('; |
| 93 | Sources.BreakpointEditDialog._LogpointSuffix = ')'; |