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

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: Reviewed 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 this._selectedFrameId = "";
231 this._classNamesPromise = null;
232 }
233
234 WebInspector.ClassesPaneWidget.ClassNamePrompt.prototype = {
235 /**
236 * @param {!WebInspector.DOMNode} selectedNode
237 * @return {!Promise.<!Array.<string>>}
238 */
239 _getClassNames: function(selectedNode)
240 {
241 var promises = [];
242 var completions = new Set();
243 this._selectedFrameId = selectedNode.frameId();
244
245 var cssModel = WebInspector.CSSModel.fromTarget(selectedNode.target());
246 var allStyleSheets = cssModel.allStyleSheets();
247 for (var stylesheet of allStyleSheets) {
248 if (stylesheet.frameId !== this._selectedFrameId)
249 continue;
250 var cssPromise = cssModel.classNamesPromise(stylesheet.id).then(clas ses => completions.addAll(classes));
251 promises.push(cssPromise);
252 }
253
254 var domPromise = selectedNode.domModel().classNamesPromise(selectedNode. ownerDocument.id).then(classes => completions.addAll(classes));
255 promises.push(domPromise);
256 return Promise.all(promises).then(() => completions.valuesArray());
257 },
258
259 /**
260 * @param {!Element} proxyElement
261 * @param {!Range} wordRange
262 * @param {boolean} force
263 * @param {function(!Array.<string>, number=)} completionsReadyCallback
264 */
265 _buildClassNameCompletions: function(proxyElement, wordRange, force, complet ionsReadyCallback)
266 {
267 var prefix = wordRange.toString();
268 if (!prefix || force)
269 this._classNamesPromise = null;
270
271 var selectedNode = WebInspector.context.flavor(WebInspector.DOMNode);
272 if (!selectedNode || (!prefix && !force && !proxyElement.textContent.len gth)) {
273 completionsReadyCallback([]);
274 return;
275 }
276
277 if (!this._classNamesPromise || this._selectedFrameId !== selectedNode.f rameId())
278 this._classNamesPromise = this._getClassNames(selectedNode);
279
280 this._classNamesPromise.then(completions => {
281 if (prefix[0] === ".")
282 completions = completions.map(value => "." + value);
283 var results = completions.filter(value => value.startsWith(prefix));
284 completionsReadyCallback(results, 0);
285 });
286 },
287
288 __proto__: WebInspector.TextPrompt.prototype
289 }
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