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/sources/CSSSourceFrame.js

Issue 2128093004: DevTools: Add color swatches to Sources panel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments 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 20 matching lines...) Expand all
31 /** 31 /**
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
42 if (Runtime.experiments.isEnabled("sourceColorPicker")) {
43 this._colorBookmarks = [];
44 this.uiSourceCode().addEventListener(WebInspector.UISourceCode.Events.Wo rkingCopyChanged, this._workingCopyChanged, this);
45 }
41 } 46 }
42 47
48 /** @type {number} */
49 WebInspector.CSSSourceFrame.UpdateTimeout = 200;
50
43 WebInspector.CSSSourceFrame.prototype = { 51 WebInspector.CSSSourceFrame.prototype = {
44 _registerShortcuts: function() 52 _registerShortcuts: function()
45 { 53 {
46 var shortcutKeys = WebInspector.ShortcutsScreen.SourcesPanelShortcuts; 54 var shortcutKeys = WebInspector.ShortcutsScreen.SourcesPanelShortcuts;
47 for (var i = 0; i < shortcutKeys.IncreaseCSSUnitByOne.length; ++i) 55 for (var i = 0; i < shortcutKeys.IncreaseCSSUnitByOne.length; ++i)
48 this.addShortcut(shortcutKeys.IncreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, 1)); 56 this.addShortcut(shortcutKeys.IncreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, 1));
49 for (var i = 0; i < shortcutKeys.DecreaseCSSUnitByOne.length; ++i) 57 for (var i = 0; i < shortcutKeys.DecreaseCSSUnitByOne.length; ++i)
50 this.addShortcut(shortcutKeys.DecreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, -1)); 58 this.addShortcut(shortcutKeys.DecreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, -1));
51 for (var i = 0; i < shortcutKeys.IncreaseCSSUnitByTen.length; ++i) 59 for (var i = 0; i < shortcutKeys.IncreaseCSSUnitByTen.length; ++i)
52 this.addShortcut(shortcutKeys.IncreaseCSSUnitByTen[i].key, this._han dleUnitModification.bind(this, 10)); 60 this.addShortcut(shortcutKeys.IncreaseCSSUnitByTen[i].key, this._han dleUnitModification.bind(this, 10));
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 var newUnitText = this._modifyUnit(cssUnitText, change); 98 var newUnitText = this._modifyUnit(cssUnitText, change);
91 if (!newUnitText) 99 if (!newUnitText)
92 return false; 100 return false;
93 this.textEditor.editRange(cssUnitRange, newUnitText); 101 this.textEditor.editRange(cssUnitRange, newUnitText);
94 selection.startColumn = token.startColumn; 102 selection.startColumn = token.startColumn;
95 selection.endColumn = selection.startColumn + newUnitText.length; 103 selection.endColumn = selection.startColumn + newUnitText.length;
96 this.textEditor.setSelection(selection); 104 this.textEditor.setSelection(selection);
97 return true; 105 return true;
98 }, 106 },
99 107
108 /**
109 * @param {!WebInspector.Event} event
110 */
111 _workingCopyChanged: function(event)
112 {
113 this.updateColorSwatchesWhenPossible();
114 },
115
116 updateColorSwatchesWhenPossible: function(){
lushnikov 2016/07/12 22:38:46 let inline this method into _workingCopyChanged me
flandy 2016/07/13 18:14:57 Done.
117 if (this._updateTimeout)
118 clearTimeout(this._updateTimeout);
119 this._updateTimeout = setTimeout(this.updateColorSwatchesImmediately.bin d(this), WebInspector.CSSSourceFrame.UpdateTimeout);
120 },
121
122 updateColorSwatchesImmediately: function(){
lushnikov 2016/07/12 22:38:46 _updateColorSwatches:
flandy 2016/07/13 18:14:57 Done.
123 if (this._updateTimeout)
124 clearTimeout(this._updateTimeout);
125 this._updateTimeout = null;
126
127 var colorPositions = this._extractColorPositions(this._uiSourceCode.work ingCopy());
128 this.textEditor.operation(this._putColorSwatchesInline.bind(this, colorP ositions));
129 },
130
131 /**
132 * @param {string} content
133 * @return {?Array<!WebInspector.CSSSourceFrame.ColorPosition>}
134 */
135 _extractColorPositions: function(content)
136 {
137 if (!content)
138 return null;
139
140 var colorPositions = [];
141
142 var lines = content.split(/\r?\n/);
lushnikov 2016/07/12 22:38:46 can we use WI.Text here? It has methods such as li
flandy 2016/07/13 18:14:57 Yes we can. Done.
143 for (var lineNum = 0; lineNum < lines.length; lineNum++) {
lushnikov 2016/07/12 22:38:46 s/lineNum/lineNumber/g
flandy 2016/07/13 18:14:57 Done.
144 var line = lines[lineNum] + "\n";
145
146 var colorRegex = /[\s:;,(){}]((?:rgb|hsl)a?\([^)]+\)|#[0-9a-f]{8}|#[ 0-9a-f]{6}|#[0-9a-f]{3,4}|[a-z]+)(?=[\s;,(){}])/gi;
147 var arrResult;
lushnikov 2016/07/12 22:38:46 var match;
flandy 2016/07/13 18:14:57 Done.
148
149 while ((arrResult = colorRegex.exec(line)) !== null) {
150 if (arrResult.length >= 1) {
lushnikov 2016/07/12 22:38:45 let's use fast-return where possible. The less ne
flandy 2016/07/13 18:14:57 Okay great! Done. Yes, I updated the Regex to use
151 var colorText = arrResult[1];
152 var color = WebInspector.Color.parse(colorText);
153 if (color) {
154 var startIndex = arrResult[0].search(/[a-z]|#/i);
lushnikov 2016/07/12 22:38:46 why the arrResult.index doesn't work for you?
flandy 2016/07/13 18:14:57 Since I use a capturing group (in parenthesis) for
155 var startColumn = arrResult.index + startIndex;
156
157 colorPositions.push(new WebInspector.CSSSourceFrame.Colo rPosition(color, lineNum, startColumn, colorText.length));
158 }
159 }
160 }
161 }
162
163 return colorPositions;
164 },
165
166 /**
167 * @param {?Array<!WebInspector.CSSSourceFrame.ColorPosition>} colorPosition s
168 */
169 _putColorSwatchesInline: function(colorPositions)
170 {
171 this._clearColorBookmarks();
172 if (colorPositions) {
lushnikov 2016/07/12 22:38:46 let's use fast-return here as well if (!colorPosi
flandy 2016/07/13 18:14:57 Done.
173
174 for (var i = 0; i < colorPositions.length; i++) {
175 var colorPos = colorPositions[i];
lushnikov 2016/07/12 22:38:46 var colorPosition = ...
flandy 2016/07/13 18:14:57 Done.
176
177 var swatch = WebInspector.ColorSwatch.create();
178 swatch.setColorText(colorPos.color.asString(WebInspector.Color.F ormat.Original));
179 swatch.setFormat(WebInspector.Color.detectColorFormat(colorPos.c olor));
lushnikov 2016/07/12 22:38:46 do you need to set it any format?
flandy 2016/07/13 18:14:57 No need. setColorText already sets the swatch's fo
180 swatch.iconElement().title = WebInspector.UIString("Open color p icker");
lushnikov 2016/07/12 22:38:46 let's kill this until we have it
flandy 2016/07/13 18:14:57 Done.
181 swatch.showText(false);
182
183 var bookmark = this.textEditor.addBookmark(colorPos.textRange.st artLine, colorPos.textRange.startColumn, swatch);
184 this._colorBookmarks.push(bookmark);
185 }
186 }
187 },
188
189 _clearColorBookmarks: function() {
190 for (var i = 0; i < this._colorBookmarks.length; i++) {
lushnikov 2016/07/12 22:38:46 remove {}
flandy 2016/07/13 18:14:57 Done.
191 this._colorBookmarks[i].clear();
192 }
193 this._colorBookmarks = [];
194 },
195
196 /**
197 * @override
198 */
199 onTextEditorContentLoaded: function()
200 {
201 WebInspector.UISourceCodeFrame.prototype.onTextEditorContentLoaded.call( this);
202 if (Runtime.experiments.isEnabled("sourceColorPicker")) {
lushnikov 2016/07/12 22:38:46 remove {}
flandy 2016/07/13 18:14:57 Done.
203 this.updateColorSwatchesImmediately();
204 }
205 },
206
207 dispose: function()
208 {
209 this.uiSourceCode().removeEventListener(WebInspector.UISourceCode.Events .WorkingCopyChanged, this._workingCopyChanged, this);
210 WebInspector.UISourceCodeFrame.prototype.dispose.call(this);
211 },
212
100 __proto__: WebInspector.UISourceCodeFrame.prototype 213 __proto__: WebInspector.UISourceCodeFrame.prototype
101 } 214 }
102 215
103 /** 216 /**
104 * @constructor 217 * @constructor
218 * @param {!WebInspector.Color} color
219 * @param {number} lineNumber
220 * @param {number} startColumn
221 * @param {number} textLength
222 */
223 WebInspector.CSSSourceFrame.ColorPosition = function(color, lineNumber, startCol umn, textLength)
224 {
225 this.color = color;
226 this.textRange = new WebInspector.TextRange(lineNumber, startColumn, lineNum ber, startColumn + textLength);
227 }
228
229 /**
230 * @constructor
105 * @implements {WebInspector.TextEditorAutocompleteDelegate} 231 * @implements {WebInspector.TextEditorAutocompleteDelegate}
106 */ 232 */
107 WebInspector.CSSSourceFrame.AutocompleteDelegate = function() 233 WebInspector.CSSSourceFrame.AutocompleteDelegate = function()
108 { 234 {
109 this._simpleDelegate = new WebInspector.SimpleAutocompleteDelegate(".-$"); 235 this._simpleDelegate = new WebInspector.SimpleAutocompleteDelegate(".-$");
110 } 236 }
111 237
112 WebInspector.CSSSourceFrame._backtrackDepth = 10; 238 WebInspector.CSSSourceFrame._backtrackDepth = 10;
113 239
114 WebInspector.CSSSourceFrame.AutocompleteDelegate.prototype = { 240 WebInspector.CSSSourceFrame.AutocompleteDelegate.prototype = {
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 var propertyToken = this._backtrackPropertyToken(editor, prefixRange.sta rtLine, prefixRange.startColumn - 1); 314 var propertyToken = this._backtrackPropertyToken(editor, prefixRange.sta rtLine, prefixRange.startColumn - 1);
189 if (!propertyToken) 315 if (!propertyToken)
190 return this._simpleDelegate.wordsWithPrefix(editor, prefixRange, sub stituteRange); 316 return this._simpleDelegate.wordsWithPrefix(editor, prefixRange, sub stituteRange);
191 317
192 var line = editor.line(prefixRange.startLine); 318 var line = editor.line(prefixRange.startLine);
193 var tokenContent = line.substring(propertyToken.startColumn, propertyTok en.endColumn); 319 var tokenContent = line.substring(propertyToken.startColumn, propertyTok en.endColumn);
194 var keywords = WebInspector.CSSMetadata.keywordsForProperty(tokenContent ); 320 var keywords = WebInspector.CSSMetadata.keywordsForProperty(tokenContent );
195 return keywords.startsWith(prefix); 321 return keywords.startsWith(prefix);
196 }, 322 },
197 } 323 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698