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

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: Inline color swatches 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
43 WebInspector.CSSSourceFrame.prototype = { 48 WebInspector.CSSSourceFrame.prototype = {
44 _registerShortcuts: function() 49 _registerShortcuts: function()
45 { 50 {
46 var shortcutKeys = WebInspector.ShortcutsScreen.SourcesPanelShortcuts; 51 var shortcutKeys = WebInspector.ShortcutsScreen.SourcesPanelShortcuts;
47 for (var i = 0; i < shortcutKeys.IncreaseCSSUnitByOne.length; ++i) 52 for (var i = 0; i < shortcutKeys.IncreaseCSSUnitByOne.length; ++i)
48 this.addShortcut(shortcutKeys.IncreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, 1)); 53 this.addShortcut(shortcutKeys.IncreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, 1));
49 for (var i = 0; i < shortcutKeys.DecreaseCSSUnitByOne.length; ++i) 54 for (var i = 0; i < shortcutKeys.DecreaseCSSUnitByOne.length; ++i)
50 this.addShortcut(shortcutKeys.DecreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, -1)); 55 this.addShortcut(shortcutKeys.DecreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, -1));
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 var newUnitText = this._modifyUnit(cssUnitText, change); 95 var newUnitText = this._modifyUnit(cssUnitText, change);
91 if (!newUnitText) 96 if (!newUnitText)
92 return false; 97 return false;
93 this.textEditor.editRange(cssUnitRange, newUnitText); 98 this.textEditor.editRange(cssUnitRange, newUnitText);
94 selection.startColumn = token.startColumn; 99 selection.startColumn = token.startColumn;
95 selection.endColumn = selection.startColumn + newUnitText.length; 100 selection.endColumn = selection.startColumn + newUnitText.length;
96 this.textEditor.setSelection(selection); 101 this.textEditor.setSelection(selection);
97 return true; 102 return true;
98 }, 103 },
99 104
100 __proto__: WebInspector.UISourceCodeFrame.prototype 105 __proto__: WebInspector.UISourceCodeFrame.prototype,
lushnikov 2016/07/12 03:48:02 nit: __proto__ should be the last thing in the obj
flandy 2016/07/12 21:17:15 Done.
106
107 _workingCopyChanged: function(event)
lushnikov 2016/07/12 03:48:02 let's add jsdoc here
flandy 2016/07/12 21:17:15 Done.
108 {
109 this._addColorSwatches();
110 },
111
112 _addColorSwatches: function()
113 {
114 var colorPositions = this._extractColorPositions(this._uiSourceCode.work ingCopy());
115 this._putColorSwatchesInline(colorPositions);
116 },
117
118 /**
119 * @param {string} content
120 * @return {?Array<!WebInspector.CSSSourceFrame.ColorPosition>}
121 */
122 _extractColorPositions: function(content)
123 {
124 if (!content)
125 return null;
126
127 var colorPositions = [];
128
129 var lines = content.split(/\r?\n/);
130 for (var lineNum = 0; lineNum < lines.length; lineNum++) {
131 var line = lines[lineNum] + "\n";
132
133 var colorRegex = /[\s:;,(){}]((?:rgb|hsl)a?\([^)]+\)|#[0-9a-f]{8}|#[ 0-9a-f]{6}|#[0-9a-f]{3,4}|[a-z]+)(?=[\s;,(){}])/gi;
134 var arrResult;
135
136 while ((arrResult = colorRegex.exec(line)) !== null) {
137 if (arrResult.length >= 1) {
138 var colorText = arrResult[1];
139 var color = WebInspector.Color.parse(colorText);
140 if (color) {
141 var startIndex = arrResult[0].search(/[a-z]|#/i);
142 var startColumn = arrResult.index + startIndex;
143
144 colorPositions.push(new WebInspector.CSSSourceFrame.Colo rPosition(color, lineNum, startColumn, colorText.length));
145 }
146 }
147 }
148 }
149
150 return colorPositions;
151 },
152
153
154 /**
155 * @param {?Array<!WebInspector.CSSSourceFrame.ColorPosition>} colorPosition s
156 */
157 _putColorSwatchesInline: function(colorPositions)
lushnikov 2016/07/12 03:48:02 this whole function should be a codemirror operati
flandy 2016/07/12 21:17:15 Awesome! Done.
158 {
159 if (colorPositions) {
160 this._clearColorBookmarks();
lushnikov 2016/07/12 03:48:02 i guess you want clear color bookmarks outside of
flandy 2016/07/12 21:17:15 Done.
161
162 for (var i = 0; i < colorPositions.length; i++) {
163 var colorPos = colorPositions[i];
164
165 var swatch = WebInspector.ColorSwatch.create();
166 swatch.setColorText(colorPos.color.asString(WebInspector.Color.F ormat.Original));
167 swatch.setFormat(WebInspector.Color.detectColorFormat(colorPos.c olor));
168 swatch.iconElement().title = WebInspector.UIString("Open color p icker");
169
170 var bookmark = this.textEditor.addBookmark(colorPos.textRange.st artLine, colorPos.textRange.startColumn, swatch);
171 this._colorBookmarks.push(bookmark);
172 }
173 }
174 },
175
176 _clearColorBookmarks: function() {
177 for (var i = 0; i < this._colorBookmarks.length; i++) {
178 this._colorBookmarks[i].clear();
179 }
180 this._colorBookmarks = [];
181 },
182
183 /**
184 * @override
185 */
186 onTextEditorContentLoaded: function()
187 {
188 WebInspector.UISourceCodeFrame.prototype.onTextEditorContentLoaded.call( this);
189 if (Runtime.experiments.isEnabled("sourceColorPicker")) {
190 this._addColorSwatches();
191 }
192 },
193
194 dispose: function()
195 {
196 this.uiSourceCode().removeEventListener(WebInspector.UISourceCode.Events .WorkingCopyChanged, this._workingCopyChanged, this);
197 WebInspector.UISourceCodeFrame.prototype.dispose.call(this);
198 }
101 } 199 }
102 200
103 /** 201 /**
202 * @constructor
203 * @param {!WebInspector.Color} color
204 * @param {number} lineNumber
205 * @param {number} startColumn
206 * @param {number} textLength
207 */
208 WebInspector.CSSSourceFrame.ColorPosition = function(color, lineNumber, startCol umn, textLength)
209 {
210 this.color = color;
211 this.textRange = new WebInspector.TextRange(lineNumber, startColumn, lineNum ber, startColumn + textLength);
212 }
213
214 /**
104 * @constructor 215 * @constructor
105 * @implements {WebInspector.TextEditorAutocompleteDelegate} 216 * @implements {WebInspector.TextEditorAutocompleteDelegate}
106 */ 217 */
107 WebInspector.CSSSourceFrame.AutocompleteDelegate = function() 218 WebInspector.CSSSourceFrame.AutocompleteDelegate = function()
108 { 219 {
109 this._simpleDelegate = new WebInspector.SimpleAutocompleteDelegate(".-$"); 220 this._simpleDelegate = new WebInspector.SimpleAutocompleteDelegate(".-$");
110 } 221 }
111 222
112 WebInspector.CSSSourceFrame._backtrackDepth = 10; 223 WebInspector.CSSSourceFrame._backtrackDepth = 10;
113 224
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 var propertyToken = this._backtrackPropertyToken(editor, prefixRange.sta rtLine, prefixRange.startColumn - 1); 299 var propertyToken = this._backtrackPropertyToken(editor, prefixRange.sta rtLine, prefixRange.startColumn - 1);
189 if (!propertyToken) 300 if (!propertyToken)
190 return this._simpleDelegate.wordsWithPrefix(editor, prefixRange, sub stituteRange); 301 return this._simpleDelegate.wordsWithPrefix(editor, prefixRange, sub stituteRange);
191 302
192 var line = editor.line(prefixRange.startLine); 303 var line = editor.line(prefixRange.startLine);
193 var tokenContent = line.substring(propertyToken.startColumn, propertyTok en.endColumn); 304 var tokenContent = line.substring(propertyToken.startColumn, propertyTok en.endColumn);
194 var keywords = WebInspector.CSSMetadata.keywordsForProperty(tokenContent ); 305 var keywords = WebInspector.CSSMetadata.keywordsForProperty(tokenContent );
195 return keywords.startsWith(prefix); 306 return keywords.startsWith(prefix);
196 }, 307 },
197 } 308 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698