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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/sources/CSSSourceFrame.js

Issue 2157533003: DevTools: Source color picker experiment. Make color swatches clickable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 21 matching lines...) Expand all
32 * @constructor 32 * @constructor
33 * @extends {WebInspector.UISourceCodeFrame} 33 * @extends {WebInspector.UISourceCodeFrame}
34 * @param {!WebInspector.UISourceCode} uiSourceCode 34 * @param {!WebInspector.UISourceCode} uiSourceCode
35 */ 35 */
36 WebInspector.CSSSourceFrame = function(uiSourceCode) 36 WebInspector.CSSSourceFrame = function(uiSourceCode)
37 { 37 {
38 WebInspector.UISourceCodeFrame.call(this, uiSourceCode); 38 WebInspector.UISourceCodeFrame.call(this, uiSourceCode);
39 this.textEditor.setAutocompleteDelegate(new WebInspector.CSSSourceFrame.Auto completeDelegate()); 39 this.textEditor.setAutocompleteDelegate(new WebInspector.CSSSourceFrame.Auto completeDelegate());
40 this._registerShortcuts(); 40 this._registerShortcuts();
41 this._colorBookmarks = []; 41 this._colorBookmarks = [];
42 this._iconPopoverHelper = new WebInspector.IconPopoverHelper();
43 this._analyzeTextColors = true;
42 } 44 }
43 45
44 /** @type {number} */ 46 /** @type {number} */
45 WebInspector.CSSSourceFrame.UpdateTimeout = 200; 47 WebInspector.CSSSourceFrame.UpdateTimeout = 200;
46 48
47 WebInspector.CSSSourceFrame.prototype = { 49 WebInspector.CSSSourceFrame.prototype = {
48 _registerShortcuts: function() 50 _registerShortcuts: function()
49 { 51 {
50 var shortcutKeys = WebInspector.ShortcutsScreen.SourcesPanelShortcuts; 52 var shortcutKeys = WebInspector.ShortcutsScreen.SourcesPanelShortcuts;
51 for (var i = 0; i < shortcutKeys.IncreaseCSSUnitByOne.length; ++i) 53 for (var i = 0; i < shortcutKeys.IncreaseCSSUnitByOne.length; ++i)
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 * @param {?Array<!WebInspector.CSSSourceFrame.ColorPosition>} colorPosition s 147 * @param {?Array<!WebInspector.CSSSourceFrame.ColorPosition>} colorPosition s
146 */ 148 */
147 _putColorSwatchesInline: function(colorPositions) 149 _putColorSwatchesInline: function(colorPositions)
148 { 150 {
149 this._clearColorBookmarks(); 151 this._clearColorBookmarks();
150 if (!colorPositions) 152 if (!colorPositions)
151 return; 153 return;
152 154
153 for (var i = 0; i < colorPositions.length; i++) { 155 for (var i = 0; i < colorPositions.length; i++) {
154 var colorPosition = colorPositions[i]; 156 var colorPosition = colorPositions[i];
155 157 var swatch = new WebInspector.CSSSourceFrame.ColorSwatch(this, color Position);
156 var swatch = WebInspector.ColorSwatch.create(); 158 var bookmark = this.textEditor.addBookmark(colorPosition.textRange.s tartLine, colorPosition.textRange.startColumn, swatch.getElement());
157 swatch.setColorText(colorPosition.color.asString(WebInspector.Color. Format.Original));
158 swatch.hideText(true);
159
160 var bookmark = this.textEditor.addBookmark(colorPosition.textRange.s tartLine, colorPosition.textRange.startColumn, swatch);
161 this._colorBookmarks.push(bookmark); 159 this._colorBookmarks.push(bookmark);
162 } 160 }
163 }, 161 },
164 162
165 _clearColorBookmarks: function() 163 _clearColorBookmarks: function()
166 { 164 {
167 for (var i = 0; i < this._colorBookmarks.length; i++) 165 for (var i = 0; i < this._colorBookmarks.length; i++)
168 this._colorBookmarks[i].clear(); 166 this._colorBookmarks[i].clear();
169 this._colorBookmarks = []; 167 this._colorBookmarks = [];
170 }, 168 },
171 169
172 /** 170 /**
171 * @param {!WebInspector.ColorSwatch} swatch
172 */
173 _showSpectrum: function(swatch)
174 {
175 if (this._iconPopoverHelper.isShowing()) {
176 this._iconPopoverHelper.hide();
177 return;
178 }
179 var color = swatch.color();
180 var format = swatch.format();
181 if (format === WebInspector.Color.Format.Original)
182 format = color.format();
183
184 this._spectrum = new WebInspector.Spectrum();
185 this._spectrum.setColor(color, format);
186 this._spectrum.addEventListener(WebInspector.Spectrum.Events.SizeChanged , this._spectrumResized, this);
187 this._spectrum.addEventListener(WebInspector.Spectrum.Events.ColorChange d, this._spectrumChanged, this);
188
189 var anchorElement = swatch.iconElement();
190 this._iconPopoverHelper.show(this._spectrum, anchorElement, null, this._ spectrumHidden.bind(this));
191 },
192
193 /**
194 * @param {!WebInspector.Event} event
195 */
196 _spectrumResized: function(event)
197 {
198 this._iconPopoverHelper.reposition();
199 },
200
201 /**
202 * @param {!WebInspector.Event} event
203 */
204 _spectrumChanged: function(event)
205 {
206 this._analyzeTextColors = false;
207 var colorString = /** @type {string} */ (event.data);
208 this._currentSwatch.setColorText(colorString);
209 this._replaceColorText(this._currentSwatch.getColorPosition(), colorStri ng);
210 },
211
212 _spectrumHidden: function()
213 {
214 this._analyzeTextColors = true;
215 this._spectrum.removeEventListener(WebInspector.Spectrum.Events.SizeChan ged, this._spectrumResized, this);
216 this._spectrum.removeEventListener(WebInspector.Spectrum.Events.ColorCha nged, this._spectrumChanged, this);
217 delete this._spectrum;
218 },
219
220 /**
221 * @param {!WebInspector.CSSSourceFrame.ColorPosition} colorPosition
222 * @param {string} colorString
223 */
224 _replaceColorText: function(colorPosition, colorString)
225 {
226 this._textEditor.editRange(colorPosition.textRange, colorString);
227 colorPosition.color = WebInspector.Color.parse(colorString);
228 colorPosition.textRange.endColumn = colorPosition.textRange.startColumn + colorString.length;
229 },
230
231 /**
173 * @override 232 * @override
174 */ 233 */
175 onTextEditorContentSet: function() 234 onTextEditorContentSet: function()
176 { 235 {
177 WebInspector.UISourceCodeFrame.prototype.onTextEditorContentSet.call(thi s); 236 WebInspector.UISourceCodeFrame.prototype.onTextEditorContentSet.call(thi s);
178 if (Runtime.experiments.isEnabled("sourceColorPicker")) 237 if (Runtime.experiments.isEnabled("sourceColorPicker") && this._analyzeT extColors)
179 this._updateColorSwatches(); 238 this._updateColorSwatches();
180 }, 239 },
181 240
182 /** 241 /**
183 * @override 242 * @override
184 * @param {!WebInspector.TextRange} oldRange 243 * @param {!WebInspector.TextRange} oldRange
185 * @param {!WebInspector.TextRange} newRange 244 * @param {!WebInspector.TextRange} newRange
186 */ 245 */
187 onTextChanged: function(oldRange, newRange) 246 onTextChanged: function(oldRange, newRange)
188 { 247 {
189 WebInspector.UISourceCodeFrame.prototype.onTextChanged.call(this, oldRan ge, newRange); 248 WebInspector.UISourceCodeFrame.prototype.onTextChanged.call(this, oldRan ge, newRange);
190 if (Runtime.experiments.isEnabled("sourceColorPicker")) { 249 if (Runtime.experiments.isEnabled("sourceColorPicker") && this._analyzeT extColors) {
191 if (this._updateTimeout) 250 if (this._updateTimeout)
192 clearTimeout(this._updateTimeout); 251 clearTimeout(this._updateTimeout);
193 this._updateTimeout = setTimeout(this._updateColorSwatches.bind(this ), WebInspector.CSSSourceFrame.UpdateTimeout); 252 this._updateTimeout = setTimeout(this._updateColorSwatches.bind(this ), WebInspector.CSSSourceFrame.UpdateTimeout);
194 } 253 }
195 }, 254 },
196 255
256 /**
257 * @override
258 * @param {number} lineNumber
259 */
260 scrollChanged: function(lineNumber)
261 {
262 WebInspector.UISourceCodeFrame.prototype.scrollChanged.call(this, lineNu mber);
263 if (this._iconPopoverHelper.isShowing())
264 this._iconPopoverHelper.hide();
265 },
266
197 __proto__: WebInspector.UISourceCodeFrame.prototype 267 __proto__: WebInspector.UISourceCodeFrame.prototype
198 } 268 }
199 269
200 /** 270 /**
201 * @constructor 271 * @constructor
202 * @param {!WebInspector.Color} color 272 * @param {!WebInspector.Color} color
203 * @param {number} lineNumber 273 * @param {number} lineNumber
204 * @param {number} startColumn 274 * @param {number} startColumn
205 * @param {number} textLength 275 * @param {number} textLength
206 */ 276 */
207 WebInspector.CSSSourceFrame.ColorPosition = function(color, lineNumber, startCol umn, textLength) 277 WebInspector.CSSSourceFrame.ColorPosition = function(color, lineNumber, startCol umn, textLength)
208 { 278 {
209 this.color = color; 279 this.color = color;
210 this.textRange = new WebInspector.TextRange(lineNumber, startColumn, lineNum ber, startColumn + textLength); 280 this.textRange = new WebInspector.TextRange(lineNumber, startColumn, lineNum ber, startColumn + textLength);
211 } 281 }
212 282
213 /** 283 /**
214 * @constructor 284 * @constructor
285 * @param {!WebInspector.CSSSourceFrame} cssSourceFrame
286 * @param {!WebInspector.CSSSourceFrame.ColorPosition} colorPosition
287 */
288 WebInspector.CSSSourceFrame.ColorSwatch = function(cssSourceFrame, colorPosition )
289 {
290 this._cssSourceFrame = cssSourceFrame;
291 this._colorPosition = colorPosition;
292
293 this._swatch = WebInspector.ColorSwatch.create();
294 this._swatch.setColorText(colorPosition.color.asString(WebInspector.Color.Fo rmat.Original) || "white");
295 var shiftClickMessage = WebInspector.UIString("Shift + Click to change color format.");
296 this._swatch.iconElement().title = WebInspector.UIString("Open color picker. %s", shiftClickMessage);
297 this._swatch.removeClickListener();
lushnikov 2016/07/16 00:30:26 this is hackish! We need to come up with something
flandy 2016/07/19 17:52:19 How about adding a function to ColorSwatch to set
298 this._swatch.iconElement().addEventListener("click", this._handleSwatchClick .bind(this), true);
299 this._swatch.hideText(true);
300 }
301
302 WebInspector.CSSSourceFrame.ColorSwatch.prototype = {
303 /**
304 * @param {!Event} event
305 */
306 _handleSwatchClick: function(event)
307 {
308 this._cssSourceFrame._currentSwatch = this;
309
310 if (event.shiftKey) {
311 this._swatch.toggleNextFormat();
312 this._cssSourceFrame._analyzeTextColors = false;
313 this._cssSourceFrame._replaceColorText(this._colorPosition, this._sw atch.getColorText());
314 this._cssSourceFrame._analyzeTextColors = true;
315 } else {
316 this._cssSourceFrame._showSpectrum(this._swatch);
317 }
318 event.consume(true);
319 },
320
321 /**
322 * @return {!WebInspector.ColorSwatch}
323 */
324 getElement: function()
325 {
326 return this._swatch;
327 },
328
329 /**
330 * @return {!WebInspector.CSSSourceFrame.ColorPosition}
331 */
332 getColorPosition: function()
333 {
334 return this._colorPosition;
335 },
336
337 /**
338 * @param {string} colorString
339 */
340 setColorText: function(colorString)
341 {
342 this._swatch.setColorText(colorString);
343 }
344 }
345
346 /**
347 * @constructor
215 * @implements {WebInspector.TextEditorAutocompleteDelegate} 348 * @implements {WebInspector.TextEditorAutocompleteDelegate}
216 */ 349 */
217 WebInspector.CSSSourceFrame.AutocompleteDelegate = function() 350 WebInspector.CSSSourceFrame.AutocompleteDelegate = function()
218 { 351 {
219 this._simpleDelegate = new WebInspector.SimpleAutocompleteDelegate(".-$"); 352 this._simpleDelegate = new WebInspector.SimpleAutocompleteDelegate(".-$");
220 } 353 }
221 354
222 WebInspector.CSSSourceFrame._backtrackDepth = 10; 355 WebInspector.CSSSourceFrame._backtrackDepth = 10;
223 356
224 WebInspector.CSSSourceFrame.AutocompleteDelegate.prototype = { 357 WebInspector.CSSSourceFrame.AutocompleteDelegate.prototype = {
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 var propertyToken = this._backtrackPropertyToken(editor, prefixRange.sta rtLine, prefixRange.startColumn - 1); 431 var propertyToken = this._backtrackPropertyToken(editor, prefixRange.sta rtLine, prefixRange.startColumn - 1);
299 if (!propertyToken) 432 if (!propertyToken)
300 return this._simpleDelegate.wordsWithPrefix(editor, prefixRange, sub stituteRange); 433 return this._simpleDelegate.wordsWithPrefix(editor, prefixRange, sub stituteRange);
301 434
302 var line = editor.line(prefixRange.startLine); 435 var line = editor.line(prefixRange.startLine);
303 var tokenContent = line.substring(propertyToken.startColumn, propertyTok en.endColumn); 436 var tokenContent = line.substring(propertyToken.startColumn, propertyTok en.endColumn);
304 var keywords = WebInspector.CSSMetadata.keywordsForProperty(tokenContent ); 437 var keywords = WebInspector.CSSMetadata.keywordsForProperty(tokenContent );
305 return keywords.startsWith(prefix); 438 return keywords.startsWith(prefix);
306 }, 439 },
307 } 440 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698