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

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

Issue 2763833004: [DO NOT LAND] For Konrad: classes pane widget (Closed)
Patch Set: Created 3 years, 9 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/sdk/DOMModel.js » ('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 * @unrestricted 5 * @unrestricted
6 */ 6 */
7 Elements.ClassesPaneWidget = class extends UI.Widget { 7 Elements.ClassesPaneWidget = class extends UI.Widget {
8 constructor() { 8 constructor() {
9 super(); 9 super();
10 this.element.className = 'styles-element-classes-pane'; 10 this.element.className = 'styles-element-classes-pane';
11 var container = this.element.createChild('div', 'title-container'); 11 var container = this.element.createChild('div', 'title-container');
12 this._input = container.createChild('div', 'new-class-input monospace'); 12 this._input = container.createChild('div', 'new-class-input monospace');
13 this.setDefaultFocusedElement(this._input); 13 this.setDefaultFocusedElement(this._input);
14 this._classesContainer = this.element.createChild('div', 'source-code'); 14 this._classesContainer = this.element.createChild('div', 'source-code');
15 this._classesContainer.classList.add('styles-element-classes-container'); 15 this._classesContainer.classList.add('styles-element-classes-container');
16 this._prompt = new Elements.ClassesPaneWidget.ClassNamePrompt(); 16 this._prompt = new Elements.ClassesPaneWidget.ClassNamePrompt();
17 this._prompt.setAutocompletionTimeout(0); 17 this._prompt.setAutocompletionTimeout(0);
18 this._prompt.renderAsBlock(); 18 this._prompt.renderAsBlock();
19 19
20 var proxyElement = this._prompt.attach(this._input); 20 var proxyElement = this._prompt.attach(this._input);
21 this._prompt.setPlaceholder(Common.UIString('Add new class')); 21 this._prompt.setPlaceholder(Common.UIString('Add new class'));
22 this._prompt.on(UI.TextPrompt.TextChangedEvent, this._onTextChanged, this);
22 proxyElement.addEventListener('keydown', this._onKeyDown.bind(this), false); 23 proxyElement.addEventListener('keydown', this._onKeyDown.bind(this), false);
23 24
24 SDK.targetManager.addModelListener(SDK.DOMModel, SDK.DOMModel.Events.DOMMuta ted, this._onDOMMutated, this); 25 SDK.targetManager.addModelListener(SDK.DOMModel, SDK.DOMModel.Events.DOMMuta ted, this._onDOMMutated, this);
25 /** @type {!Set<!SDK.DOMNode>} */ 26 /** @type {!Set<!SDK.DOMNode>} */
26 this._mutatingNodes = new Set(); 27 this._mutatingNodes = new Set();
27 UI.context.addFlavorChangeListener(SDK.DOMNode, this._update, this); 28 /** @type {!Map<!SDK.DOMNode, string>} */
29 this._pendingNodeClasses = new Map();
30 this._updateNodeThrottler = new Common.Throttler(0);
31 /** @type {?SDK.DOMNode} */
32 this._previousTarget = null;
33 UI.context.addFlavorChangeListener(SDK.DOMNode, this._onFlavorChange, this);
34 }
35
36 /**
37 * @param {string} text
38 * @return {!Array.<string>}
39 */
40 _splitTextIntoClasses(text) {
41 return text.split(/[.,\s]/)
42 .map(className => className.trim())
43 .filter(className => className.length);
28 } 44 }
29 45
30 /** 46 /**
31 * @param {!Event} event 47 * @param {!Event} event
32 */ 48 */
33 _onKeyDown(event) { 49 _onKeyDown(event) {
50 if (!isEnterKey(event) && !isEscKey(event))
51 return;
52
34 var text = event.target.textContent; 53 var text = event.target.textContent;
35 if (isEscKey(event)) { 54 if (isEscKey(event)) {
36 event.target.textContent = '';
37 if (!text.isWhitespace()) 55 if (!text.isWhitespace())
38 event.consume(true); 56 event.consume(true);
39 return; 57 text = '';
40 } 58 }
41 59
42 if (!isEnterKey(event)) 60 if (isEnterKey(event))
43 return; 61 event.consume(true);
62
63 this._prompt.clearAutocomplete();
64 event.target.textContent = '';
65
44 var node = UI.context.flavor(SDK.DOMNode); 66 var node = UI.context.flavor(SDK.DOMNode);
45 if (!node) 67 if (!node)
46 return; 68 return;
47 69
48 this._prompt.clearAutocomplete(); 70 var classNames = this._splitTextIntoClasses(text);
49 event.target.textContent = ''; 71 for (var className of classNames)
50 var classNames = text.split(/[.,\s]/);
51 for (var className of classNames) {
52 var className = className.trim();
53 if (!className.length)
54 continue;
55 this._toggleClass(node, className, true); 72 this._toggleClass(node, className, true);
56 }
57 this._installNodeClasses(node); 73 this._installNodeClasses(node);
58 this._update(); 74 this._update();
59 event.consume(true); 75 }
76
77 _onTextChanged() {
78 var node = UI.context.flavor(SDK.DOMNode);
79 if (!node)
80 return;
81 this._installNodeClasses(node);
60 } 82 }
61 83
62 /** 84 /**
63 * @param {!Common.Event} event 85 * @param {!Common.Event} event
64 */ 86 */
65 _onDOMMutated(event) { 87 _onDOMMutated(event) {
66 var node = /** @type {!SDK.DOMNode} */ (event.data); 88 var node = /** @type {!SDK.DOMNode} */ (event.data);
67 if (this._mutatingNodes.has(node)) 89 if (this._mutatingNodes.has(node))
68 return; 90 return;
69 delete node[Elements.ClassesPaneWidget._classesSymbol]; 91 delete node[Elements.ClassesPaneWidget._classesSymbol];
70 this._update(); 92 this._update();
71 } 93 }
72 94
73 /** 95 /**
96 * @param {!Common.Event} event
97 */
98 _onFlavorChange(event) {
99 if (this._previousTarget && this._prompt.text()) {
100 this._input.textContent = '';
101 this._installNodeClasses(this._previousTarget);
102 }
103 this._previousTarget = /** @type {?SDK.DOMNode} */ (event.data);
104 this._update();
105 }
106
107 /**
74 * @override 108 * @override
75 */ 109 */
76 wasShown() { 110 wasShown() {
77 this._update(); 111 this._update();
78 } 112 }
79 113
80 _update() { 114 _update() {
81 if (!this.isShowing()) 115 if (!this.isShowing())
82 return; 116 return;
83 117
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 * @param {!SDK.DOMNode} node 186 * @param {!SDK.DOMNode} node
153 */ 187 */
154 _installNodeClasses(node) { 188 _installNodeClasses(node) {
155 var classes = this._nodeClasses(node); 189 var classes = this._nodeClasses(node);
156 var activeClasses = new Set(); 190 var activeClasses = new Set();
157 for (var className of classes.keys()) { 191 for (var className of classes.keys()) {
158 if (classes.get(className)) 192 if (classes.get(className))
159 activeClasses.add(className); 193 activeClasses.add(className);
160 } 194 }
161 195
196 var additionalClasses = this._splitTextIntoClasses(this._prompt.textWithCurr entSuggestion());
197 for (className of additionalClasses)
198 activeClasses.add(className);
199
162 var newClasses = activeClasses.valuesArray(); 200 var newClasses = activeClasses.valuesArray();
163 newClasses.sort(); 201 newClasses.sort();
164 this._mutatingNodes.add(node); 202
165 node.setAttributeValue('class', newClasses.join(' '), onClassNameUpdated.bin d(this)); 203 this._pendingNodeClasses.set(node, newClasses.join(' '));
204 this._updateNodeThrottler.schedule(this._flushPendingClasses.bind(this));
205 }
206
207 /**
208 * @return {!Promise}
209 */
210 _flushPendingClasses() {
211 var promises = [];
212 for (var node of this._pendingNodeClasses.keys()) {
213 this._mutatingNodes.add(node);
214 var promise = node.setAttributeValuePromise('class', this._pendingNodeClas ses.get(node)).then(onClassValueUpdated.bind(this, node));
215 promises.push(promise);
216 }
217 this._pendingNodeClasses.clear();
218 return Promise.all(promises);
166 219
167 /** 220 /**
221 * @param {!SDK.DOMNode} node
168 * @this {Elements.ClassesPaneWidget} 222 * @this {Elements.ClassesPaneWidget}
169 */ 223 */
170 function onClassNameUpdated() { 224 function onClassValueUpdated(node) {
171 this._mutatingNodes.delete(node); 225 this._mutatingNodes.delete(node);
172 } 226 }
227
173 } 228 }
174 }; 229 };
175 230
176 Elements.ClassesPaneWidget._classesSymbol = Symbol('Elements.ClassesPaneWidget._ classesSymbol'); 231 Elements.ClassesPaneWidget._classesSymbol = Symbol('Elements.ClassesPaneWidget._ classesSymbol');
177 232
178 /** 233 /**
179 * @implements {UI.ToolbarItem.Provider} 234 * @implements {UI.ToolbarItem.Provider}
180 * @unrestricted 235 * @unrestricted
181 */ 236 */
182 Elements.ClassesPaneWidget.ButtonProvider = class { 237 Elements.ClassesPaneWidget.ButtonProvider = class {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 if (!this._classNamesPromise || this._selectedFrameId !== selectedNode.frame Id()) 310 if (!this._classNamesPromise || this._selectedFrameId !== selectedNode.frame Id())
256 this._classNamesPromise = this._getClassNames(selectedNode); 311 this._classNamesPromise = this._getClassNames(selectedNode);
257 312
258 return this._classNamesPromise.then(completions => { 313 return this._classNamesPromise.then(completions => {
259 if (prefix[0] === '.') 314 if (prefix[0] === '.')
260 completions = completions.map(value => '.' + value); 315 completions = completions.map(value => '.' + value);
261 return completions.filter(value => value.startsWith(prefix)).map(completio n => ({text: completion})); 316 return completions.filter(value => value.startsWith(prefix)).map(completio n => ({text: completion}));
262 }); 317 });
263 } 318 }
264 }; 319 };
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/devtools/front_end/sdk/DOMModel.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698