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

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"))
lushnikov 2016/07/13 23:31:42 let's not check for experiment here - having empty
flandy 2016/07/14 00:56:40 Done.
43 this._colorBookmarks = [];
41 } 44 }
42 45
46 /** @type {number} */
47 WebInspector.CSSSourceFrame.UpdateTimeout = 200;
48
43 WebInspector.CSSSourceFrame.prototype = { 49 WebInspector.CSSSourceFrame.prototype = {
44 _registerShortcuts: function() 50 _registerShortcuts: function()
45 { 51 {
46 var shortcutKeys = WebInspector.ShortcutsScreen.SourcesPanelShortcuts; 52 var shortcutKeys = WebInspector.ShortcutsScreen.SourcesPanelShortcuts;
47 for (var i = 0; i < shortcutKeys.IncreaseCSSUnitByOne.length; ++i) 53 for (var i = 0; i < shortcutKeys.IncreaseCSSUnitByOne.length; ++i)
48 this.addShortcut(shortcutKeys.IncreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, 1)); 54 this.addShortcut(shortcutKeys.IncreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, 1));
49 for (var i = 0; i < shortcutKeys.DecreaseCSSUnitByOne.length; ++i) 55 for (var i = 0; i < shortcutKeys.DecreaseCSSUnitByOne.length; ++i)
50 this.addShortcut(shortcutKeys.DecreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, -1)); 56 this.addShortcut(shortcutKeys.DecreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, -1));
51 for (var i = 0; i < shortcutKeys.IncreaseCSSUnitByTen.length; ++i) 57 for (var i = 0; i < shortcutKeys.IncreaseCSSUnitByTen.length; ++i)
52 this.addShortcut(shortcutKeys.IncreaseCSSUnitByTen[i].key, this._han dleUnitModification.bind(this, 10)); 58 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); 96 var newUnitText = this._modifyUnit(cssUnitText, change);
91 if (!newUnitText) 97 if (!newUnitText)
92 return false; 98 return false;
93 this.textEditor.editRange(cssUnitRange, newUnitText); 99 this.textEditor.editRange(cssUnitRange, newUnitText);
94 selection.startColumn = token.startColumn; 100 selection.startColumn = token.startColumn;
95 selection.endColumn = selection.startColumn + newUnitText.length; 101 selection.endColumn = selection.startColumn + newUnitText.length;
96 this.textEditor.setSelection(selection); 102 this.textEditor.setSelection(selection);
97 return true; 103 return true;
98 }, 104 },
99 105
106 _updateColorSwatches: function()
107 {
108 if (this._updateTimeout)
109 clearTimeout(this._updateTimeout);
110 this._updateTimeout = null;
111
112 var colorPositions = this._extractColorPositions(this._uiSourceCode.work ingCopy());
lushnikov 2016/07/13 23:31:42 don't go for content of uiSourceCode, use textEdit
flandy 2016/07/14 00:56:40 Done.
113 this.textEditor.operation(this._putColorSwatchesInline.bind(this, colorP ositions));
114 },
115
116 /**
117 * @param {string} content
118 * @return {?Array<!WebInspector.CSSSourceFrame.ColorPosition>}
119 */
120 _extractColorPositions: function(content)
121 {
122 if (!content)
123 return null;
124
125 var colorPositions = [];
126 var text = new WebInspector.Text(content);
127 var numberOfLines = text.lineCount();
128 for (var lineNumber = 0; lineNumber < numberOfLines; lineNumber++) {
129 var line = text.lineAt(lineNumber) + "\n";
130 var colorRegex = /[\s:;,(){}]((?:rgb|hsl)a?\([^)]+\)|#[0-9a-f]{8}|#[ 0-9a-f]{6}|#[0-9a-f]{3,4}|[a-z]+)(?=[\s;,(){}])/gi;
lushnikov 2016/07/13 23:31:42 would the \b work for you instead of "[\s:;,(){}]"
flandy 2016/07/14 00:56:40 \b is not strict enough because it would match the
131 var match;
132 while ((match = colorRegex.exec(line)) !== null) {
133 if (match.length < 2)
134 continue;
135
136 var colorText = match[1];
137 var color = WebInspector.Color.parse(colorText);
138 if (color)
139 colorPositions.push(new WebInspector.CSSSourceFrame.ColorPos ition(color, lineNumber, match.index + 1, colorText.length));
140 }
141 }
142
143 return colorPositions;
144 },
145
146 /**
147 * @param {?Array<!WebInspector.CSSSourceFrame.ColorPosition>} colorPosition s
148 */
149 _putColorSwatchesInline: function(colorPositions)
150 {
151 this._clearColorBookmarks();
152 if (!colorPositions)
153 return;
154
155 for (var i = 0; i < colorPositions.length; i++) {
156 var colorPosition = colorPositions[i];
157
158 var swatch = WebInspector.ColorSwatch.create();
159 swatch.setColorText(colorPosition.color.asString(WebInspector.Color. Format.Original));
160 swatch.hideText(true);
161
162 var bookmark = this.textEditor.addBookmark(colorPosition.textRange.s tartLine, colorPosition.textRange.startColumn, swatch);
163 this._colorBookmarks.push(bookmark);
164 }
165 },
166
167 _clearColorBookmarks: function()
168 {
169 for (var i = 0; i < this._colorBookmarks.length; i++)
170 this._colorBookmarks[i].clear();
171 this._colorBookmarks = [];
172 },
173
174 /**
175 * @override
176 */
177 onTextEditorContentLoaded: function()
178 {
179 WebInspector.UISourceCodeFrame.prototype.onTextEditorContentLoaded.call( this);
180 if (Runtime.experiments.isEnabled("sourceColorPicker"))
181 this._updateColorSwatches();
182 },
183
184 /**
185 * @override
186 * @param {!WebInspector.TextRange} oldRange
187 * @param {!WebInspector.TextRange} newRange
188 */
189 onTextChanged: function(oldRange, newRange)
190 {
191 WebInspector.UISourceCodeFrame.prototype.onTextChanged.call(this, oldRan ge, newRange);
192 if (Runtime.experiments.isEnabled("sourceColorPicker")) {
193 if (this._updateTimeout)
194 clearTimeout(this._updateTimeout);
195 this._updateTimeout = setTimeout(this._updateColorSwatches.bind(this ), WebInspector.CSSSourceFrame.UpdateTimeout);
196 }
197 },
198
100 __proto__: WebInspector.UISourceCodeFrame.prototype 199 __proto__: WebInspector.UISourceCodeFrame.prototype
101 } 200 }
102 201
103 /** 202 /**
104 * @constructor 203 * @constructor
204 * @param {!WebInspector.Color} color
205 * @param {number} lineNumber
206 * @param {number} startColumn
207 * @param {number} textLength
208 */
209 WebInspector.CSSSourceFrame.ColorPosition = function(color, lineNumber, startCol umn, textLength)
210 {
211 this.color = color;
212 this.textRange = new WebInspector.TextRange(lineNumber, startColumn, lineNum ber, startColumn + textLength);
213 }
214
215 /**
216 * @constructor
105 * @implements {WebInspector.TextEditorAutocompleteDelegate} 217 * @implements {WebInspector.TextEditorAutocompleteDelegate}
106 */ 218 */
107 WebInspector.CSSSourceFrame.AutocompleteDelegate = function() 219 WebInspector.CSSSourceFrame.AutocompleteDelegate = function()
108 { 220 {
109 this._simpleDelegate = new WebInspector.SimpleAutocompleteDelegate(".-$"); 221 this._simpleDelegate = new WebInspector.SimpleAutocompleteDelegate(".-$");
110 } 222 }
111 223
112 WebInspector.CSSSourceFrame._backtrackDepth = 10; 224 WebInspector.CSSSourceFrame._backtrackDepth = 10;
113 225
114 WebInspector.CSSSourceFrame.AutocompleteDelegate.prototype = { 226 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); 300 var propertyToken = this._backtrackPropertyToken(editor, prefixRange.sta rtLine, prefixRange.startColumn - 1);
189 if (!propertyToken) 301 if (!propertyToken)
190 return this._simpleDelegate.wordsWithPrefix(editor, prefixRange, sub stituteRange); 302 return this._simpleDelegate.wordsWithPrefix(editor, prefixRange, sub stituteRange);
191 303
192 var line = editor.line(prefixRange.startLine); 304 var line = editor.line(prefixRange.startLine);
193 var tokenContent = line.substring(propertyToken.startColumn, propertyTok en.endColumn); 305 var tokenContent = line.substring(propertyToken.startColumn, propertyTok en.endColumn);
194 var keywords = WebInspector.CSSMetadata.keywordsForProperty(tokenContent ); 306 var keywords = WebInspector.CSSMetadata.keywordsForProperty(tokenContent );
195 return keywords.startsWith(prefix); 307 return keywords.startsWith(prefix);
196 }, 308 },
197 } 309 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698