Chromium Code Reviews| OLD | NEW |
|---|---|
| 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._uncommitedValues = new Map(); | |
| 30 /** @type {?SDK.DOMNode} */ | |
| 31 this._previousTarget = null; | |
| 32 UI.context.addFlavorChangeListener(SDK.DOMNode, this._onFlavorChange, this); | |
| 28 } | 33 } |
| 29 | 34 |
| 30 /** | 35 /** |
| 36 * @param {string} text | |
| 37 * @return {!Array.<string>} | |
| 38 */ | |
| 39 _splitTextIntoClasses(text) { | |
| 40 return text.split(/[.,\s]/) | |
| 41 .map(className => className.trim()) | |
| 42 .filter(className => className.length); | |
| 43 } | |
| 44 | |
| 45 /** | |
| 31 * @param {!Event} event | 46 * @param {!Event} event |
| 32 */ | 47 */ |
| 33 _onKeyDown(event) { | 48 _onKeyDown(event) { |
| 34 var text = event.target.textContent; | 49 var text = event.target.textContent; |
| 35 if (isEscKey(event)) { | 50 if (isEscKey(event)) { |
| 36 event.target.textContent = ''; | 51 event.target.textContent = ''; |
| 37 if (!text.isWhitespace()) | 52 if (!text.isWhitespace()) |
| 38 event.consume(true); | 53 event.consume(true); |
| 39 return; | 54 return; |
| 40 } | 55 } |
| 41 | 56 |
| 42 if (!isEnterKey(event)) | 57 if (!isEnterKey(event)) |
| 43 return; | 58 return; |
| 44 var node = UI.context.flavor(SDK.DOMNode); | 59 var node = UI.context.flavor(SDK.DOMNode); |
| 45 if (!node) | 60 if (!node) |
| 46 return; | 61 return; |
| 47 | 62 |
| 48 this._prompt.clearAutocomplete(); | 63 this._prompt.clearAutocomplete(); |
| 49 event.target.textContent = ''; | 64 event.target.textContent = ''; |
| 50 var classNames = text.split(/[.,\s]/); | 65 var classNames = this._splitTextIntoClasses(text); |
| 51 for (var className of classNames) { | 66 for (var className of classNames) |
| 52 var className = className.trim(); | |
| 53 if (!className.length) | |
| 54 continue; | |
| 55 this._toggleClass(node, className, true); | 67 this._toggleClass(node, className, true); |
| 56 } | |
| 57 this._installNodeClasses(node); | 68 this._installNodeClasses(node); |
| 58 this._update(); | 69 this._update(); |
| 59 event.consume(true); | 70 event.consume(true); |
| 60 } | 71 } |
| 61 | 72 |
| 73 _onTextChanged() { | |
| 74 var node = UI.context.flavor(SDK.DOMNode); | |
| 75 if (!node) | |
| 76 return; | |
| 77 var text = this._prompt.textWithCurrentSuggestion(); | |
| 78 var classes = this._splitTextIntoClasses(text); | |
| 79 | |
| 80 this._installNodeClasses(node, classes); | |
| 81 this._update(); | |
| 82 } | |
| 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._prompt.setText(''); | |
|
lushnikov
2017/03/07 02:55:12
to avoid that issue with selection you had, let's
kdzwinel
2017/03/15 15:23:45
We will avoid the error this way, but introduce a
| |
| 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 * @param {string} className | 177 * @param {string} className |
| 144 * @param {boolean} enabled | 178 * @param {boolean} enabled |
| 145 */ | 179 */ |
| 146 _toggleClass(node, className, enabled) { | 180 _toggleClass(node, className, enabled) { |
| 147 var classes = this._nodeClasses(node); | 181 var classes = this._nodeClasses(node); |
| 148 classes.set(className, enabled); | 182 classes.set(className, enabled); |
| 149 } | 183 } |
| 150 | 184 |
| 151 /** | 185 /** |
| 152 * @param {!SDK.DOMNode} node | 186 * @param {!SDK.DOMNode} node |
| 187 * @param {!Array.<string>=} additionalClasses | |
| 153 */ | 188 */ |
| 154 _installNodeClasses(node) { | 189 _installNodeClasses(node, additionalClasses) { |
| 155 var classes = this._nodeClasses(node); | 190 var classes = this._nodeClasses(node); |
| 156 var activeClasses = new Set(); | 191 var activeClasses = new Set(); |
| 157 for (var className of classes.keys()) { | 192 for (var className of classes.keys()) { |
| 158 if (classes.get(className)) | 193 if (classes.get(className)) |
| 159 activeClasses.add(className); | 194 activeClasses.add(className); |
| 160 } | 195 } |
| 161 | 196 |
| 197 if (additionalClasses) { | |
| 198 for (className of additionalClasses) | |
| 199 activeClasses.add(className); | |
| 200 } | |
| 162 var newClasses = activeClasses.valuesArray(); | 201 var newClasses = activeClasses.valuesArray(); |
| 163 newClasses.sort(); | 202 newClasses.sort(); |
| 203 | |
| 204 if (this._mutatingNodes.has(node)) { | |
| 205 this._uncommitedValues.set(node, newClasses.join(' ')); | |
| 206 return; | |
| 207 } | |
| 208 | |
| 164 this._mutatingNodes.add(node); | 209 this._mutatingNodes.add(node); |
| 165 node.setAttributeValue('class', newClasses.join(' '), onClassNameUpdated.bin d(this)); | 210 node.setAttributeValue('class', newClasses.join(' '), onClassNameUpdated.bin d(this)); |
| 166 | 211 |
| 167 /** | 212 /** |
| 168 * @this {Elements.ClassesPaneWidget} | 213 * @this {Elements.ClassesPaneWidget} |
| 169 */ | 214 */ |
| 170 function onClassNameUpdated() { | 215 function onClassNameUpdated() { |
| 171 this._mutatingNodes.delete(node); | 216 if (this._uncommitedValues.has(node)) { |
|
lushnikov
2017/03/07 02:55:12
this complicates the _installNodeClasses method, m
kdzwinel
2017/03/15 15:23:45
Thanks, I haven't seen common.Throttler before - u
| |
| 217 node.setAttributeValue('class', this._uncommitedValues.get(node), onClas sNameUpdated.bind(this)); | |
| 218 this._uncommitedValues.delete(node); | |
| 219 } else { | |
| 220 this._mutatingNodes.delete(node); | |
| 221 } | |
| 172 } | 222 } |
| 173 } | 223 } |
| 174 }; | 224 }; |
| 175 | 225 |
| 176 Elements.ClassesPaneWidget._classesSymbol = Symbol('Elements.ClassesPaneWidget._ classesSymbol'); | 226 Elements.ClassesPaneWidget._classesSymbol = Symbol('Elements.ClassesPaneWidget._ classesSymbol'); |
| 177 | 227 |
| 178 /** | 228 /** |
| 179 * @implements {UI.ToolbarItem.Provider} | 229 * @implements {UI.ToolbarItem.Provider} |
| 180 * @unrestricted | 230 * @unrestricted |
| 181 */ | 231 */ |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 255 if (!this._classNamesPromise || this._selectedFrameId !== selectedNode.frame Id()) | 305 if (!this._classNamesPromise || this._selectedFrameId !== selectedNode.frame Id()) |
| 256 this._classNamesPromise = this._getClassNames(selectedNode); | 306 this._classNamesPromise = this._getClassNames(selectedNode); |
| 257 | 307 |
| 258 return this._classNamesPromise.then(completions => { | 308 return this._classNamesPromise.then(completions => { |
| 259 if (prefix[0] === '.') | 309 if (prefix[0] === '.') |
| 260 completions = completions.map(value => '.' + value); | 310 completions = completions.map(value => '.' + value); |
| 261 return completions.filter(value => value.startsWith(prefix)).map(completio n => ({text: completion})); | 311 return completions.filter(value => value.startsWith(prefix)).map(completio n => ({text: completion})); |
| 262 }); | 312 }); |
| 263 } | 313 } |
| 264 }; | 314 }; |
| OLD | NEW |