Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/elements/ClassesPaneWidget.js

Issue 2343773002: DevTools: Autocomplete class names in ClassesPaneWidget (Closed)
Patch Set: Rebased Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | third_party/WebKit/Source/devtools/front_end/elements/elementsPanel.css » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * @constructor 6 * @constructor
7 * @extends {WebInspector.Widget} 7 * @extends {WebInspector.Widget}
8 */ 8 */
9 WebInspector.ClassesPaneWidget = function() 9 WebInspector.ClassesPaneWidget = function()
10 { 10 {
11 WebInspector.Widget.call(this); 11 WebInspector.Widget.call(this);
12 this.element.className = "styles-element-classes-pane"; 12 this.element.className = "styles-element-classes-pane";
13 var container = this.element.createChild("div", "title-container"); 13 var container = this.element.createChild("div", "title-container");
14 this._input = container.createChild("input", "new-class-input monospace"); 14 this._input = container.createChild("div", "new-class-input monospace");
15 this._input.placeholder = WebInspector.UIString("Add new class"); 15 this._input.setAttribute("placeholder", WebInspector.UIString("Add new class "));
16 this._input.addEventListener("keydown", this._onKeyDown.bind(this), false);
17 this.setDefaultFocusedElement(this._input); 16 this.setDefaultFocusedElement(this._input);
18 this._classesContainer = this.element.createChild("div", "source-code"); 17 this._classesContainer = this.element.createChild("div", "source-code");
19 this._classesContainer.classList.add("styles-element-classes-container"); 18 this._classesContainer.classList.add("styles-element-classes-container");
19 this._prompt = new WebInspector.ClassesPaneWidget.ClassNamePrompt();
20 this._prompt.setAutocompletionTimeout(0);
21 this._prompt.renderAsBlock();
22
23 var proxyElement = this._prompt.attach(this._input);
24 proxyElement.addEventListener("keydown", this._onKeyDown.bind(this), false);
20 25
21 WebInspector.targetManager.addModelListener(WebInspector.DOMModel, WebInspec tor.DOMModel.Events.DOMMutated, this._onDOMMutated, this); 26 WebInspector.targetManager.addModelListener(WebInspector.DOMModel, WebInspec tor.DOMModel.Events.DOMMutated, this._onDOMMutated, this);
22 /** @type {!Set<!WebInspector.DOMNode>} */ 27 /** @type {!Set<!WebInspector.DOMNode>} */
23 this._mutatingNodes = new Set(); 28 this._mutatingNodes = new Set();
24 WebInspector.context.addFlavorChangeListener(WebInspector.DOMNode, this._upd ate, this); 29 WebInspector.context.addFlavorChangeListener(WebInspector.DOMNode, this._upd ate, this);
25 } 30 }
26 31
27 WebInspector.ClassesPaneWidget._classesSymbol = Symbol("WebInspector.ClassesPane Widget._classesSymbol"); 32 WebInspector.ClassesPaneWidget._classesSymbol = Symbol("WebInspector.ClassesPane Widget._classesSymbol");
28 33
29 WebInspector.ClassesPaneWidget.prototype = { 34 WebInspector.ClassesPaneWidget.prototype = {
30 /** 35 /**
31 * @param {!Event} event 36 * @param {!Event} event
32 */ 37 */
33 _onKeyDown: function(event) 38 _onKeyDown: function(event)
34 { 39 {
35 var text = event.target.value; 40 var text = event.target.textContent;
36 if (isEscKey(event)) { 41 if (isEscKey(event)) {
37 event.target.value = ""; 42 event.target.textContent = "";
38 if (!text.isWhitespace()) 43 if (!text.isWhitespace())
39 event.consume(true); 44 event.consume(true);
40 return; 45 return;
41 } 46 }
42 47
43 if (!isEnterKey(event)) 48 if (!isEnterKey(event))
44 return; 49 return;
45 var node = WebInspector.context.flavor(WebInspector.DOMNode); 50 var node = WebInspector.context.flavor(WebInspector.DOMNode);
46 if (!node) 51 if (!node)
47 return; 52 return;
48 53
49 event.target.value = ""; 54 this._prompt.clearAutocomplete();
55 event.target.textContent = "";
50 var classNames = text.split(/[.,\s]/); 56 var classNames = text.split(/[.,\s]/);
51 for (var className of classNames) { 57 for (var className of classNames) {
52 var className = className.trim(); 58 var className = className.trim();
53 if (!className.length) 59 if (!className.length)
54 continue; 60 continue;
55 this._toggleClass(node, className, true); 61 this._toggleClass(node, className, true);
56 } 62 }
57 this._installNodeClasses(node); 63 this._installNodeClasses(node);
58 this._update(); 64 this._update();
59 event.consume(true); 65 event.consume(true);
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 210
205 /** 211 /**
206 * @override 212 * @override
207 * @return {!WebInspector.ToolbarItem} 213 * @return {!WebInspector.ToolbarItem}
208 */ 214 */
209 item: function() 215 item: function()
210 { 216 {
211 return this._button; 217 return this._button;
212 } 218 }
213 } 219 }
220
221 /**
222 * @constructor
223 * @extends {WebInspector.TextPrompt}
224 */
225 WebInspector.ClassesPaneWidget.ClassNamePrompt = function()
226 {
227 WebInspector.TextPrompt.call(this, this._buildClassNameCompletions.bind(this ), " ");
228 this.setSuggestBoxEnabled(true);
229 this.disableDefaultSuggestionForEmptyInput();
230 }
231
232 WebInspector.ClassesPaneWidget.ClassNamePrompt.prototype = {
233 /**
234 * @override
235 * @param {!Event} event
236 */
237 onKeyDown: function(event)
lushnikov 2016/09/28 21:19:39 why do you need to override onKeyDown? Looks like
ahmetemirercin 2016/09/28 23:40:56 Done.
238 {
239 switch (event.key) {
240 case "Enter":
241 // Accept any available autocompletions and advance to the next fiel d.
242 if (this.autoCompleteElement && this.autoCompleteElement.textContent .length) {
243 this.acceptAutoComplete();
244 return;
245 }
246 break;
247 }
248
249 WebInspector.TextPrompt.prototype.onKeyDown.call(this, event);
250 },
251
252 /**
253 * @param {!Element} proxyElement
254 * @param {!Range} wordRange
255 * @param {boolean} force
256 * @param {function(!Array.<string>, number=)} completionsReadyCallback
257 */
258 _buildClassNameCompletions: function(proxyElement, wordRange, force, complet ionsReadyCallback)
259 {
260 var prefix = wordRange.toString();
261 if (!prefix && !force && !proxyElement.textContent.length) {
262 completionsReadyCallback([]);
263 return;
264 }
265
266 var promises = [];
267 var allClasses = new Set();
268 var selectedNode = WebInspector.context.flavor(WebInspector.DOMNode);
lushnikov 2016/09/28 21:19:39 at times selectedNode is null - let's bail out in
ahmetemirercin 2016/09/28 23:40:56 Done.
269 var cssModel = WebInspector.CSSModel.fromTarget(selectedNode.target());
270 var allStyleSheets = cssModel.allStyleSheets();
271 var selectedFrameId = selectedNode.frameId() || WebInspector.ResourceTre eModel.fromTarget(selectedNode.target()).mainFrame.id;
272
273 for (var stylesheet of allStyleSheets) {
lushnikov 2016/09/28 21:19:40 this code runs for every keypress in ClassesPaneWi
ahmetemirercin 2016/09/28 23:40:56 I propose keeping old completions when user moving
lushnikov 2016/09/29 16:03:15 It's better, but as someone who explores completio
ahmetemirercin 2016/09/29 19:25:36 I was afraid that classnames can be ready when use
274 if (stylesheet.frameId !== selectedFrameId)
275 continue;
276 var cssPromise = cssModel.classNamesPromise(stylesheet.id).then(clas ses => allClasses.addAll(classes));
277 promises.push(cssPromise);
278 }
279
280 var domPromise = selectedNode.domModel().classNamesPromise(selectedNode. ownerDocument.id).then(classes => allClasses.addAll(classes));
281 promises.push(domPromise);
282
283 Promise.all(promises).then(() => {
284 var results = allClasses.valuesArray().filter((value) => value.start sWith(prefix));
285 completionsReadyCallback(results, 0);
286 });
287 },
288
289 __proto__: WebInspector.TextPrompt.prototype
290 }
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/devtools/front_end/elements/elementsPanel.css » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698