blob: 101eb221a2f241d88345d86e1fcffadda310b690 [file] [log] [blame]
Erik Luo0b789b62018-11-21 19:31:461/*
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
7Sources.BreakpointEditDialog = class extends UI.Widget {
8 /**
9 * @param {number} editorLineNumber
10 * @param {string} oldCondition
Erik Luo5b2b7522018-12-03 20:35:0611 * @param {boolean} preferLogpoint
Erik Luo0b789b62018-11-21 19:31:4612 * @param {function({committed: boolean, condition: string})} onFinish
13 */
Erik Luo5b2b7522018-12-03 20:35:0614 constructor(editorLineNumber, oldCondition, preferLogpoint, onFinish) {
Erik Luo0b789b62018-11-21 19:31:4615 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 Luo5b2b7522018-12-03 20:35:0622 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 Luo0b789b62018-11-21 19:31:4629 const labelElement = this.contentElement.createChild('label', 'source-frame-breakpoint-message');
30 labelElement.htmlFor = 'source-frame-breakpoint-condition';
Erik Luo5b2b7522018-12-03 20:35:0631 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 Luo0b789b62018-11-21 19:31:4634
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 Luo5b2b7522018-12-03 20:35:0654 * @param {string} condition
55 * @return {string}
56 */
57 static _conditionForLogpoint(condition) {
58 return `${Sources.BreakpointEditDialog._LogpointPrefix}${condition}${Sources.BreakpointEditDialog._LogpointSuffix}`;
59 }
60
61 /**
Erik Luo0b789b62018-11-21 19:31:4662 * @param {boolean} committed
63 */
64 _finishEditing(committed) {
65 if (this._finished)
66 return;
67 this._finished = true;
68 this._editor.widget().detach();
Erik Luo5b2b7522018-12-03 20:35:0669 let condition = this._editor.text();
70 if (this._isLogpoint)
71 condition = Sources.BreakpointEditDialog._conditionForLogpoint(condition);
Erik Luo0b789b62018-11-21 19:31:4672 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 Luo5b2b7522018-12-03 20:35:0691
92Sources.BreakpointEditDialog._LogpointPrefix = '/** DEVTOOLS_LOGPOINT */ console.log(';
93Sources.BreakpointEditDialog._LogpointSuffix = ')';