OLD | NEW |
---|---|
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 29 matching lines...) Expand all Loading... | |
40 this._swatchPopoverHelper = new WebInspector.SwatchPopoverHelper(); | 40 this._swatchPopoverHelper = new WebInspector.SwatchPopoverHelper(); |
41 this._muteColorProcessing = false; | 41 this._muteColorProcessing = false; |
42 this.configureAutocomplete({ | 42 this.configureAutocomplete({ |
43 suggestionsCallback: this._cssSuggestions.bind(this), | 43 suggestionsCallback: this._cssSuggestions.bind(this), |
44 isWordChar: this._isWordChar.bind(this) | 44 isWordChar: this._isWordChar.bind(this) |
45 }); | 45 }); |
46 } | 46 } |
47 | 47 |
48 /** @type {number} */ | 48 /** @type {number} */ |
49 WebInspector.CSSSourceFrame.maxSwatchProcessingLength = 300; | 49 WebInspector.CSSSourceFrame.maxSwatchProcessingLength = 300; |
50 | 50 /** @type {symbol} */ |
51 WebInspector.CSSSourceFrame.SwatchBookmark = Symbol("swatch"); | 51 WebInspector.CSSSourceFrame.SwatchBookmark = Symbol("swatch"); |
52 | 52 |
53 WebInspector.CSSSourceFrame.prototype = { | 53 WebInspector.CSSSourceFrame.prototype = { |
54 _registerShortcuts: function() | 54 _registerShortcuts: function() |
55 { | 55 { |
56 var shortcutKeys = WebInspector.ShortcutsScreen.SourcesPanelShortcuts; | 56 var shortcutKeys = WebInspector.ShortcutsScreen.SourcesPanelShortcuts; |
57 for (var i = 0; i < shortcutKeys.IncreaseCSSUnitByOne.length; ++i) | 57 for (var i = 0; i < shortcutKeys.IncreaseCSSUnitByOne.length; ++i) |
58 this.addShortcut(shortcutKeys.IncreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, 1)); | 58 this.addShortcut(shortcutKeys.IncreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, 1)); |
59 for (var i = 0; i < shortcutKeys.DecreaseCSSUnitByOne.length; ++i) | 59 for (var i = 0; i < shortcutKeys.DecreaseCSSUnitByOne.length; ++i) |
60 this.addShortcut(shortcutKeys.DecreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, -1)); | 60 this.addShortcut(shortcutKeys.DecreaseCSSUnitByOne[i].key, this._han dleUnitModification.bind(this, -1)); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 selection.startColumn = token.startColumn; | 104 selection.startColumn = token.startColumn; |
105 selection.endColumn = selection.startColumn + newUnitText.length; | 105 selection.endColumn = selection.startColumn + newUnitText.length; |
106 this.textEditor.setSelection(selection); | 106 this.textEditor.setSelection(selection); |
107 return true; | 107 return true; |
108 }, | 108 }, |
109 | 109 |
110 /** | 110 /** |
111 * @param {number} startLine | 111 * @param {number} startLine |
112 * @param {number} endLine | 112 * @param {number} endLine |
113 */ | 113 */ |
114 _updateColorSwatches: function(startLine, endLine) | 114 _updateSwatches: function(startLine, endLine) |
115 { | 115 { |
116 var colorPositions = []; | 116 var swatches = []; |
117 var swatchPositions = []; | |
117 | 118 |
118 var colorRegex = /[\s:;,(){}]((?:rgb|hsl)a?\([^)]+\)|#[0-9a-f]{8}|#[0-9a -f]{6}|#[0-9a-f]{3,4}|[a-z]+)(?=[\s;,(){}])/gi; | 119 var regexes = [WebInspector.CSSMetadata.VariableRegex, WebInspector.CSSM etadata.URLRegex, WebInspector.Geometry.CubicBezier.Regex, WebInspector.Color.Re gex]; |
119 for (var lineNumber = startLine; lineNumber <= endLine; lineNumber++) { | 120 for (var lineNumber = startLine; lineNumber <= endLine; lineNumber++) { |
120 var line = "\n" + this.textEditor.line(lineNumber).substring(0, WebI nspector.CSSSourceFrame.maxSwatchProcessingLength) + "\n"; | 121 var line = this.textEditor.line(lineNumber).substring(0, WebInspecto r.CSSSourceFrame.maxSwatchProcessingLength); |
121 var match; | 122 var results = WebInspector.TextUtils.splitStringByRegexes(line, rege xes); |
122 while ((match = colorRegex.exec(line)) !== null) { | 123 for (var i = 0; i < results.length; i++) { |
123 if (match.length < 2) | 124 var result = results[i]; |
125 if (result.regexIndex !== 2 && result.regexIndex !== 3) | |
lushnikov
2016/08/31 20:25:39
Let's rewrite this with Handlers:
var regexes =
flandy
2016/09/01 21:18:54
Done.
| |
124 continue; | 126 continue; |
125 var colorText = match[1]; | 127 var delimiters = /[\s:;,(){}]/; |
126 var color = WebInspector.Color.parse(colorText); | 128 var positionBefore = result.position - 1; |
127 if (color) | 129 var positionAfter = result.position + result.value.length; |
128 colorPositions.push(new WebInspector.CSSSourceFrame.ColorPos ition(color, lineNumber, match.index, colorText.length)); | 130 if (positionBefore >= 0 && !delimiters.test(line.charAt(position Before)) |
131 || positionAfter < line.length && !delimiters.test(line. charAt(positionAfter))) | |
132 continue; | |
133 | |
134 var swatch; | |
135 var swatchPosition = WebInspector.TextRange.createFromLocation(l ineNumber, result.position); | |
136 if (result.regexIndex === 2) { | |
137 if (!WebInspector.Geometry.CubicBezier.parse(result.value)) | |
lushnikov
2016/08/31 20:25:39
let's extract this logic into a handler
flandy
2016/09/01 21:18:54
Done.
| |
138 continue; | |
139 swatch = WebInspector.BezierSwatch.create(); | |
140 swatch.setText(result.value); | |
141 swatch.iconElement().title = WebInspector.UIString("Open cub ic bezier editor."); | |
142 swatch.setTextHidden(true); | |
143 } else if (result.regexIndex === 3) { | |
144 if (!WebInspector.Color.parse(result.value)) | |
lushnikov
2016/08/31 20:25:39
let's extract this logic into a handler as well
flandy
2016/09/01 21:18:54
Done.
| |
145 continue; | |
146 swatch = WebInspector.ColorSwatch.create(); | |
147 swatch.setColorText(result.value); | |
148 swatch.iconElement().title = WebInspector.UIString("Open col or picker."); | |
149 swatch.hideText(true); | |
150 } | |
151 swatches.push(swatch); | |
152 swatchPositions.push(swatchPosition); | |
129 } | 153 } |
130 } | 154 } |
131 | 155 this.textEditor.operation(putSwatchesInline.bind(this)); |
132 this.textEditor.operation(putColorSwatchesInline.bind(this)); | |
133 | 156 |
134 /** | 157 /** |
135 * @this {WebInspector.CSSSourceFrame} | 158 * @this {WebInspector.CSSSourceFrame} |
136 */ | 159 */ |
137 function putColorSwatchesInline() | 160 function putSwatchesInline() |
138 { | 161 { |
139 this._clearBookmarks(startLine, endLine); | 162 var clearRange = new WebInspector.TextRange(startLine, 0, endLine, t his.textEditor.line(endLine).length); |
163 this.textEditor.bookmarks(clearRange, WebInspector.CSSSourceFrame.Sw atchBookmark).forEach(marker => marker.clear()); | |
140 | 164 |
141 for (var i = 0; i < colorPositions.length; i++) { | 165 for (var i = 0; i < swatches.length; i++) { |
142 var colorPosition = colorPositions[i]; | 166 var swatch = swatches[i]; |
143 var swatch = WebInspector.ColorSwatch.create(); | 167 var swatchPosition = swatchPositions[i]; |
144 swatch.setColorText(colorPosition.color.asString(WebInspector.Co lor.Format.Original)); | 168 var bookmark = this.textEditor.addBookmark(swatchPosition.startL ine, swatchPosition.startColumn, swatch, WebInspector.CSSSourceFrame.SwatchBookm ark); |
145 swatch.iconElement().title = WebInspector.UIString("Open color p icker."); | 169 if (swatch instanceof WebInspector.BezierSwatch) |
146 swatch.hideText(true); | 170 swatch.iconElement().addEventListener("click", this._showBez ierEditor.bind(this, swatch, bookmark), false); |
147 var bookmark = this.textEditor.addBookmark(colorPosition.textRan ge.startLine, colorPosition.textRange.startColumn, swatch, WebInspector.CSSSourc eFrame.SwatchBookmark); | 171 else if (swatch instanceof WebInspector.ColorSwatch) |
148 swatch.iconElement().addEventListener("click", this._showSpectru m.bind(this, swatch, bookmark), true); | 172 swatch.iconElement().addEventListener("click", this._showSpe ctrum.bind(this, swatch, bookmark), false); |
149 } | 173 } |
150 } | 174 } |
151 }, | 175 }, |
152 | 176 |
153 /** | 177 /** |
154 * @param {number} startLine | |
155 * @param {number} endLine | |
156 */ | |
157 _clearBookmarks: function(startLine, endLine) | |
158 { | |
159 var range = new WebInspector.TextRange(startLine, 0, endLine, this.textE ditor.line(endLine).length); | |
160 this.textEditor.bookmarks(range, WebInspector.CSSSourceFrame.SwatchBookm ark).forEach(marker => marker.clear()); | |
161 }, | |
162 | |
163 /** | |
164 * @param {!WebInspector.ColorSwatch} swatch | 178 * @param {!WebInspector.ColorSwatch} swatch |
165 * @param {!WebInspector.TextEditorBookMark} bookmark | 179 * @param {!WebInspector.TextEditorBookMark} bookmark |
166 * @param {!Event} event | 180 * @param {!Event} event |
167 */ | 181 */ |
168 _showSpectrum: function(swatch, bookmark, event) | 182 _showSpectrum: function(swatch, bookmark, event) |
169 { | 183 { |
170 event.consume(true); | 184 this._swatchIconClicked(swatch, bookmark, event); |
171 if (this._swatchPopoverHelper.isShowing()) { | 185 if (!this._spectrum) { |
172 this._swatchPopoverHelper.hide(true); | 186 this._spectrum = new WebInspector.Spectrum(); |
173 return; | 187 this._spectrum.addEventListener(WebInspector.Spectrum.Events.SizeCha nged, this._spectrumResized, this); |
188 this._spectrum.addEventListener(WebInspector.Spectrum.Events.ColorCh anged, this._spectrumChanged, this); | |
174 } | 189 } |
175 this._hadSpectrumChange = false; | |
176 var position = bookmark.position(); | |
177 var colorText = swatch.color().asString(WebInspector.Color.Format.Origin al); | |
178 this.textEditor.setSelection(position); | |
179 this._currentColorTextRange = position.clone(); | |
180 this._currentColorTextRange.endColumn += colorText.length; | |
181 this._currentSwatch = swatch; | |
182 | |
183 this._spectrum = new WebInspector.Spectrum(); | |
184 this._spectrum.setColor(swatch.color(), swatch.format()); | 190 this._spectrum.setColor(swatch.color(), swatch.format()); |
185 this._spectrum.addEventListener(WebInspector.Spectrum.Events.SizeChanged , this._spectrumResized, this); | 191 this._swatchPopoverHelper.show(this._spectrum, swatch.iconElement(), thi s._swatchPopoverHidden.bind(this)); |
186 this._spectrum.addEventListener(WebInspector.Spectrum.Events.ColorChange d, this._spectrumChanged, this); | |
187 this._swatchPopoverHelper.show(this._spectrum, swatch.iconElement(), thi s._spectrumHidden.bind(this)); | |
188 }, | 192 }, |
189 | 193 |
190 /** | 194 /** |
191 * @param {!WebInspector.Event} event | 195 * @param {!WebInspector.Event} event |
192 */ | 196 */ |
193 _spectrumResized: function(event) | 197 _spectrumResized: function(event) |
194 { | 198 { |
195 this._swatchPopoverHelper.reposition(); | 199 this._swatchPopoverHelper.reposition(); |
196 }, | 200 }, |
197 | 201 |
198 /** | 202 /** |
199 * @param {!WebInspector.Event} event | 203 * @param {!WebInspector.Event} event |
200 */ | 204 */ |
201 _spectrumChanged: function(event) | 205 _spectrumChanged: function(event) |
202 { | 206 { |
203 this._muteColorProcessing = true; | |
204 this._hadSpectrumChange = true; | |
205 var colorString = /** @type {string} */ (event.data); | 207 var colorString = /** @type {string} */ (event.data); |
206 this._currentSwatch.setColorText(colorString); | 208 this._currentSwatch.setColorText(colorString); |
207 this._textEditor.editRange(this._currentColorTextRange, colorString, "*c olor-text-changed"); | 209 this._changeSwatchText(colorString); |
208 this._currentColorTextRange.endColumn = this._currentColorTextRange.star tColumn + colorString.length; | 210 }, |
211 | |
212 /** | |
213 * @param {!WebInspector.BezierSwatch} swatch | |
214 * @param {!WebInspector.TextEditorBookMark} bookmark | |
215 * @param {!Event} event | |
216 */ | |
217 _showBezierEditor: function(swatch, bookmark, event) | |
218 { | |
219 this._swatchIconClicked(swatch, bookmark, event); | |
220 if (!this._bezierEditor) { | |
221 this._bezierEditor = new WebInspector.BezierEditor(); | |
222 this._bezierEditor.addEventListener(WebInspector.BezierEditor.Events .BezierChanged, this._bezierChanged, this); | |
223 } | |
224 var cubicBezier = swatch.cubicBezier(); | |
225 if (!cubicBezier) | |
226 cubicBezier = /** @type {!WebInspector.Geometry.CubicBezier} */ (Web Inspector.Geometry.CubicBezier.parse("linear")); | |
227 this._bezierEditor.setBezier(cubicBezier); | |
228 this._swatchPopoverHelper.show(this._bezierEditor, swatch.iconElement(), this._swatchPopoverHidden.bind(this)); | |
229 }, | |
230 | |
231 /** | |
232 * @param {!WebInspector.Event} event | |
233 */ | |
234 _bezierChanged: function(event) | |
235 { | |
236 var bezierString = /** @type {string} */ (event.data); | |
237 this._currentSwatch.setText(bezierString); | |
238 this._changeSwatchText(bezierString); | |
239 }, | |
240 | |
241 /** | |
242 * @param {!Element} swatch | |
243 * @param {!WebInspector.TextEditorBookMark} bookmark | |
244 * @param {!Event} event | |
245 */ | |
246 _swatchIconClicked: function(swatch, bookmark, event) | |
lushnikov
2016/08/31 20:25:39
let's make this function a click listener, and let
flandy
2016/09/01 21:18:54
Done.
| |
247 { | |
248 event.consume(true); | |
249 this._hadSwatchChange = false; | |
250 var swatchPosition = bookmark.position(); | |
251 this.textEditor.setSelection(swatchPosition); | |
252 this._currentSwatchTextRange = swatchPosition.clone(); | |
253 this._currentSwatchTextRange.endColumn += swatch.textContent.length; | |
254 this._currentSwatch = swatch; | |
255 }, | |
256 | |
257 /** | |
258 * @param {string} text | |
259 */ | |
260 _changeSwatchText: function(text) | |
261 { | |
262 this._muteColorProcessing = true; | |
263 this._hadSwatchChange = true; | |
264 this._textEditor.editRange(this._currentSwatchTextRange, text, "*swatch- text-changed"); | |
265 this._currentSwatchTextRange.endColumn = this._currentSwatchTextRange.st artColumn + text.length; | |
209 }, | 266 }, |
210 | 267 |
211 /** | 268 /** |
212 * @param {boolean} commitEdit | 269 * @param {boolean} commitEdit |
213 */ | 270 */ |
214 _spectrumHidden: function(commitEdit) | 271 _swatchPopoverHidden: function(commitEdit) |
215 { | 272 { |
216 this._muteColorProcessing = false; | 273 this._muteColorProcessing = false; |
217 this._spectrum.removeEventListener(WebInspector.Spectrum.Events.SizeChan ged, this._spectrumResized, this); | 274 if (!commitEdit && this._hadSwatchChange) |
218 this._spectrum.removeEventListener(WebInspector.Spectrum.Events.ColorCha nged, this._spectrumChanged, this); | |
219 if (!commitEdit && this._hadSpectrumChange) | |
220 this.textEditor.undo(); | 275 this.textEditor.undo(); |
221 delete this._spectrum; | |
222 delete this._currentSwatch; | |
223 delete this._currentColorTextRange; | |
224 }, | 276 }, |
225 | 277 |
226 /** | 278 /** |
227 * @override | 279 * @override |
228 */ | 280 */ |
229 onTextEditorContentSet: function() | 281 onTextEditorContentSet: function() |
230 { | 282 { |
231 WebInspector.UISourceCodeFrame.prototype.onTextEditorContentSet.call(thi s); | 283 WebInspector.UISourceCodeFrame.prototype.onTextEditorContentSet.call(thi s); |
232 if (!this._muteColorProcessing) | 284 if (!this._muteColorProcessing) |
233 this._updateColorSwatches(0, this.textEditor.linesCount - 1); | 285 this._updateSwatches(0, this.textEditor.linesCount - 1); |
234 }, | 286 }, |
235 | 287 |
236 /** | 288 /** |
237 * @override | 289 * @override |
238 * @param {!WebInspector.TextRange} oldRange | 290 * @param {!WebInspector.TextRange} oldRange |
239 * @param {!WebInspector.TextRange} newRange | 291 * @param {!WebInspector.TextRange} newRange |
240 */ | 292 */ |
241 onTextChanged: function(oldRange, newRange) | 293 onTextChanged: function(oldRange, newRange) |
242 { | 294 { |
243 WebInspector.UISourceCodeFrame.prototype.onTextChanged.call(this, oldRan ge, newRange); | 295 WebInspector.UISourceCodeFrame.prototype.onTextChanged.call(this, oldRan ge, newRange); |
244 if (!this._muteColorProcessing) | 296 if (!this._muteColorProcessing) |
245 this._updateColorSwatches(newRange.startLine, newRange.endLine); | 297 this._updateSwatches(newRange.startLine, newRange.endLine); |
246 }, | 298 }, |
247 | 299 |
248 /** | 300 /** |
249 * @override | 301 * @override |
250 * @param {number} lineNumber | 302 * @param {number} lineNumber |
251 */ | 303 */ |
252 scrollChanged: function(lineNumber) | 304 scrollChanged: function(lineNumber) |
253 { | 305 { |
254 WebInspector.UISourceCodeFrame.prototype.scrollChanged.call(this, lineNu mber); | 306 WebInspector.UISourceCodeFrame.prototype.scrollChanged.call(this, lineNu mber); |
255 if (this._swatchPopoverHelper.isShowing()) | 307 if (this._swatchPopoverHelper.isShowing()) |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
313 else | 365 else |
314 return null; | 366 return null; |
315 } | 367 } |
316 tokenPosition = token.startColumn - 1; | 368 tokenPosition = token.startColumn - 1; |
317 } | 369 } |
318 return null; | 370 return null; |
319 }, | 371 }, |
320 | 372 |
321 __proto__: WebInspector.UISourceCodeFrame.prototype | 373 __proto__: WebInspector.UISourceCodeFrame.prototype |
322 } | 374 } |
323 | |
324 /** | |
325 * @constructor | |
326 * @param {!WebInspector.Color} color | |
327 * @param {number} lineNumber | |
328 * @param {number} startColumn | |
329 * @param {number} textLength | |
330 */ | |
331 WebInspector.CSSSourceFrame.ColorPosition = function(color, lineNumber, startCol umn, textLength) | |
332 { | |
333 this.color = color; | |
334 this.textRange = new WebInspector.TextRange(lineNumber, startColumn, lineNum ber, startColumn + textLength); | |
335 } | |
OLD | NEW |