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 20 matching lines...) Expand all Loading... |
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._registerShortcuts(); | 39 this._registerShortcuts(); |
40 this._swatchPopoverHelper = new WebInspector.SwatchPopoverHelper(); | 40 this._swatchPopoverHelper = new WebInspector.SwatchPopoverHelper(); |
41 this._muteColorProcessing = false; | 41 this._muteSwatchProcessing = 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 this._swatches = []; |
| 117 this._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, |
| 120 WebInspector.CSSShadowModel.PropertyRegex, WebInspector.Color.Regex]
; |
| 121 var handlers = new Map(); |
| 122 handlers.set(WebInspector.Color.Regex, this._processColor.bind(this)); |
| 123 handlers.set(WebInspector.Geometry.CubicBezier.Regex, this._processBezie
r.bind(this)); |
| 124 handlers.set(WebInspector.CSSShadowModel.PropertyRegex, this._processSha
dow.bind(this)); |
| 125 |
119 for (var lineNumber = startLine; lineNumber <= endLine; lineNumber++) { | 126 for (var lineNumber = startLine; lineNumber <= endLine; lineNumber++) { |
120 var line = "\n" + this.textEditor.line(lineNumber).substring(0, WebI
nspector.CSSSourceFrame.maxSwatchProcessingLength) + "\n"; | 127 var line = this.textEditor.line(lineNumber).substring(0, WebInspecto
r.CSSSourceFrame.maxSwatchProcessingLength) + "\n"; |
121 var match; | 128 var results = WebInspector.TextUtils.splitStringByRegexes(line, rege
xes); |
122 while ((match = colorRegex.exec(line)) !== null) { | 129 for (var i = 0; i < results.length; i++) { |
123 if (match.length < 2) | 130 var result = results[i]; |
| 131 if (result.regexIndex === -1 || !handlers.has(regexes[result.reg
exIndex])) |
124 continue; | 132 continue; |
125 var colorText = match[1]; | 133 var delimiters = /[\s:;,(){}]/; |
126 var color = WebInspector.Color.parse(colorText); | 134 var positionBefore = result.position - 1; |
127 if (color) | 135 var positionAfter = result.position + result.value.length; |
128 colorPositions.push(new WebInspector.CSSSourceFrame.ColorPos
ition(color, lineNumber, match.index, colorText.length)); | 136 if (positionBefore >= 0 && !delimiters.test(line.charAt(position
Before)) |
| 137 || positionAfter < line.length && !delimiters.test(line.char
At(positionAfter))) |
| 138 continue; |
| 139 handlers.get(regexes[result.regexIndex])(result.value, WebInspec
tor.TextRange.createFromLocation(lineNumber, result.position)); |
129 } | 140 } |
130 } | 141 } |
131 | 142 this.textEditor.operation(this._putSwatchesInline.bind(this, startLine,
endLine)); |
132 this.textEditor.operation(putColorSwatchesInline.bind(this)); | 143 delete this._swatches; |
133 | 144 delete this._swatchPositions; |
134 /** | |
135 * @this {WebInspector.CSSSourceFrame} | |
136 */ | |
137 function putColorSwatchesInline() | |
138 { | |
139 this._clearBookmarks(startLine, endLine); | |
140 | |
141 for (var i = 0; i < colorPositions.length; i++) { | |
142 var colorPosition = colorPositions[i]; | |
143 var swatch = WebInspector.ColorSwatch.create(); | |
144 swatch.setColorText(colorPosition.color.asString(WebInspector.Co
lor.Format.Original)); | |
145 swatch.iconElement().title = WebInspector.UIString("Open color p
icker."); | |
146 swatch.hideText(true); | |
147 var bookmark = this.textEditor.addBookmark(colorPosition.textRan
ge.startLine, colorPosition.textRange.startColumn, swatch, WebInspector.CSSSourc
eFrame.SwatchBookmark); | |
148 swatch.iconElement().addEventListener("click", this._showSpectru
m.bind(this, swatch, bookmark), true); | |
149 } | |
150 } | |
151 }, | 145 }, |
152 | 146 |
153 /** | 147 /** |
154 * @param {number} startLine | 148 * @param {number} startLine |
155 * @param {number} endLine | 149 * @param {number} endLine |
156 */ | 150 */ |
157 _clearBookmarks: function(startLine, endLine) | 151 _putSwatchesInline: function(startLine, endLine) |
158 { | 152 { |
159 var range = new WebInspector.TextRange(startLine, 0, endLine, this.textE
ditor.line(endLine).length); | 153 var clearRange = new WebInspector.TextRange(startLine, 0, endLine, this.
textEditor.line(endLine).length); |
160 this.textEditor.bookmarks(range, WebInspector.CSSSourceFrame.SwatchBookm
ark).forEach(marker => marker.clear()); | 154 this.textEditor.bookmarks(clearRange, WebInspector.CSSSourceFrame.Swatch
Bookmark).forEach(marker => marker.clear()); |
| 155 |
| 156 for (var i = 0; i < this._swatches.length; i++) { |
| 157 var swatch = this._swatches[i]; |
| 158 var swatchPosition = this._swatchPositions[i]; |
| 159 var bookmark = this.textEditor.addBookmark(swatchPosition.startLine,
swatchPosition.startColumn, swatch, WebInspector.CSSSourceFrame.SwatchBookmark)
; |
| 160 swatch[WebInspector.CSSSourceFrame.SwatchBookmark] = bookmark; |
| 161 } |
| 162 }, |
| 163 |
| 164 /** |
| 165 * @param {string} text |
| 166 * @param {!WebInspector.TextRange} position |
| 167 */ |
| 168 _processColor: function(text, position) |
| 169 { |
| 170 if (!WebInspector.Color.parse(text)) |
| 171 return; |
| 172 var swatch = WebInspector.ColorSwatch.create(); |
| 173 swatch.setColorText(text); |
| 174 swatch.iconElement().title = WebInspector.UIString("Open color picker.")
; |
| 175 swatch.iconElement().addEventListener("click", this._swatchIconClicked.b
ind(this, swatch), false); |
| 176 swatch.hideText(true); |
| 177 this._swatches.push(swatch); |
| 178 this._swatchPositions.push(position); |
| 179 }, |
| 180 |
| 181 /** |
| 182 * @param {string} text |
| 183 * @param {!WebInspector.TextRange} position |
| 184 */ |
| 185 _processBezier: function(text, position) |
| 186 { |
| 187 if (!WebInspector.Geometry.CubicBezier.parse(text)) |
| 188 return; |
| 189 var swatch = WebInspector.BezierSwatch.create(); |
| 190 swatch.setText(text); |
| 191 swatch.iconElement().title = WebInspector.UIString("Open cubic bezier ed
itor."); |
| 192 swatch.iconElement().addEventListener("click", this._swatchIconClicked.b
ind(this, swatch), false); |
| 193 swatch.hideText(true); |
| 194 this._swatches.push(swatch); |
| 195 this._swatchPositions.push(position); |
| 196 }, |
| 197 |
| 198 /** |
| 199 * @param {string} text |
| 200 * @param {!WebInspector.TextRange} position |
| 201 */ |
| 202 _processShadow: function(text, position) |
| 203 { |
| 204 console.log("Found shadow: " + text); |
| 205 var startIndex = text.indexOf(":") + 1; |
| 206 var propertyValue = text.substring(startIndex); |
| 207 if (propertyValue.charAt(propertyValue.length - 1) === ";") |
| 208 propertyValue = propertyValue.substring(0, propertyValue.length - 1)
; |
| 209 var shadows = text.startsWith("text") ? WebInspector.CSSShadowModel.pars
eTextShadow(propertyValue) : WebInspector.CSSShadowModel.parseBoxShadow(property
Value); |
| 210 if (shadows.length === 0) |
| 211 return; |
| 212 var shadowTexts = WebInspector.CSSShadowModel.splitShadows(propertyValue
); |
| 213 console.log(shadowTexts); |
| 214 for (var i = 0; i < shadowTexts.length; i++) { |
| 215 var shadowText = shadowTexts[i]; |
| 216 var swatch = WebInspector.CSSShadowSwatch.create(); |
| 217 var swatchPosition = WebInspector.TextRange.createFromLocation(posit
ion.startLine, position.startColumn + startIndex + shadowText.position); |
| 218 swatch.iconElement().title = WebInspector.UIString("Open shadow edit
or."); |
| 219 // swatch.iconElement().addEventListener("click", this._swatchIconCl
icked.bind(this, swatch), false); |
| 220 swatch.hideText(true); |
| 221 this._swatches.push(swatch); |
| 222 this._swatchPositions.push(swatchPosition); |
| 223 } |
| 224 }, |
| 225 |
| 226 /** |
| 227 * @param {!Element} swatch |
| 228 * @param {!Event} event |
| 229 */ |
| 230 _swatchIconClicked: function(swatch, event) |
| 231 { |
| 232 event.consume(true); |
| 233 this._hadSwatchChange = false; |
| 234 var swatchPosition = swatch[WebInspector.CSSSourceFrame.SwatchBookmark].
position(); |
| 235 this.textEditor.setSelection(swatchPosition); |
| 236 this._editedSwatchTextRange = swatchPosition.clone(); |
| 237 this._editedSwatchTextRange.endColumn += swatch.textContent.length; |
| 238 this._currentSwatch = swatch; |
| 239 |
| 240 if (swatch instanceof WebInspector.ColorSwatch) |
| 241 this._showSpectrum(swatch); |
| 242 else if (swatch instanceof WebInspector.BezierSwatch) |
| 243 this._showBezierEditor(swatch); |
161 }, | 244 }, |
162 | 245 |
163 /** | 246 /** |
164 * @param {!WebInspector.ColorSwatch} swatch | 247 * @param {!WebInspector.ColorSwatch} swatch |
165 * @param {!WebInspector.TextEditorBookMark} bookmark | |
166 * @param {!Event} event | |
167 */ | 248 */ |
168 _showSpectrum: function(swatch, bookmark, event) | 249 _showSpectrum: function(swatch) |
169 { | 250 { |
170 event.consume(true); | 251 if (!this._spectrum) { |
171 if (this._swatchPopoverHelper.isShowing()) { | 252 this._spectrum = new WebInspector.Spectrum(); |
172 this._swatchPopoverHelper.hide(true); | 253 this._spectrum.addEventListener(WebInspector.Spectrum.Events.SizeCha
nged, this._spectrumResized, this); |
173 return; | 254 this._spectrum.addEventListener(WebInspector.Spectrum.Events.ColorCh
anged, this._spectrumChanged, this); |
174 } | 255 } |
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()); | 256 this._spectrum.setColor(swatch.color(), swatch.format()); |
185 this._spectrum.addEventListener(WebInspector.Spectrum.Events.SizeChanged
, this._spectrumResized, this); | 257 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 }, | 258 }, |
189 | 259 |
190 /** | 260 /** |
191 * @param {!WebInspector.Event} event | 261 * @param {!WebInspector.Event} event |
192 */ | 262 */ |
193 _spectrumResized: function(event) | 263 _spectrumResized: function(event) |
194 { | 264 { |
195 this._swatchPopoverHelper.reposition(); | 265 this._swatchPopoverHelper.reposition(); |
196 }, | 266 }, |
197 | 267 |
198 /** | 268 /** |
199 * @param {!WebInspector.Event} event | 269 * @param {!WebInspector.Event} event |
200 */ | 270 */ |
201 _spectrumChanged: function(event) | 271 _spectrumChanged: function(event) |
202 { | 272 { |
203 this._muteColorProcessing = true; | |
204 this._hadSpectrumChange = true; | |
205 var colorString = /** @type {string} */ (event.data); | 273 var colorString = /** @type {string} */ (event.data); |
206 this._currentSwatch.setColorText(colorString); | 274 this._currentSwatch.setColorText(colorString); |
207 this._textEditor.editRange(this._currentColorTextRange, colorString, "*c
olor-text-changed"); | 275 this._changeSwatchText(colorString); |
208 this._currentColorTextRange.endColumn = this._currentColorTextRange.star
tColumn + colorString.length; | 276 }, |
| 277 |
| 278 /** |
| 279 * @param {!WebInspector.BezierSwatch} swatch |
| 280 */ |
| 281 _showBezierEditor: function(swatch) |
| 282 { |
| 283 if (!this._bezierEditor) { |
| 284 this._bezierEditor = new WebInspector.BezierEditor(); |
| 285 this._bezierEditor.addEventListener(WebInspector.BezierEditor.Events
.BezierChanged, this._bezierChanged, this); |
| 286 } |
| 287 var cubicBezier = WebInspector.Geometry.CubicBezier.parse(swatch.textCon
tent); |
| 288 if (!cubicBezier) |
| 289 cubicBezier = /** @type {!WebInspector.Geometry.CubicBezier} */ (Web
Inspector.Geometry.CubicBezier.parse("linear")); |
| 290 this._bezierEditor.setBezier(cubicBezier); |
| 291 this._swatchPopoverHelper.show(this._bezierEditor, swatch.iconElement(),
this._swatchPopoverHidden.bind(this)); |
| 292 }, |
| 293 |
| 294 /** |
| 295 * @param {!WebInspector.Event} event |
| 296 */ |
| 297 _bezierChanged: function(event) |
| 298 { |
| 299 var bezierString = /** @type {string} */ (event.data); |
| 300 this._currentSwatch.setText(bezierString); |
| 301 this._changeSwatchText(bezierString); |
| 302 }, |
| 303 |
| 304 /** |
| 305 * @param {string} text |
| 306 */ |
| 307 _changeSwatchText: function(text) |
| 308 { |
| 309 this._muteSwatchProcessing = true; |
| 310 this._hadSwatchChange = true; |
| 311 this._textEditor.editRange(this._editedSwatchTextRange, text, "*swatch-t
ext-changed"); |
| 312 this._editedSwatchTextRange.endColumn = this._editedSwatchTextRange.star
tColumn + text.length; |
209 }, | 313 }, |
210 | 314 |
211 /** | 315 /** |
212 * @param {boolean} commitEdit | 316 * @param {boolean} commitEdit |
213 */ | 317 */ |
214 _spectrumHidden: function(commitEdit) | 318 _swatchPopoverHidden: function(commitEdit) |
215 { | 319 { |
216 this._muteColorProcessing = false; | 320 this._muteSwatchProcessing = false; |
217 this._spectrum.removeEventListener(WebInspector.Spectrum.Events.SizeChan
ged, this._spectrumResized, this); | 321 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(); | 322 this.textEditor.undo(); |
221 delete this._spectrum; | |
222 delete this._currentSwatch; | |
223 delete this._currentColorTextRange; | |
224 }, | 323 }, |
225 | 324 |
226 /** | 325 /** |
227 * @override | 326 * @override |
228 */ | 327 */ |
229 onTextEditorContentSet: function() | 328 onTextEditorContentSet: function() |
230 { | 329 { |
231 WebInspector.UISourceCodeFrame.prototype.onTextEditorContentSet.call(thi
s); | 330 WebInspector.UISourceCodeFrame.prototype.onTextEditorContentSet.call(thi
s); |
232 if (!this._muteColorProcessing) | 331 if (!this._muteSwatchProcessing) |
233 this._updateColorSwatches(0, this.textEditor.linesCount - 1); | 332 this._updateSwatches(0, this.textEditor.linesCount - 1); |
234 }, | 333 }, |
235 | 334 |
236 /** | 335 /** |
237 * @override | 336 * @override |
238 * @param {!WebInspector.TextRange} oldRange | 337 * @param {!WebInspector.TextRange} oldRange |
239 * @param {!WebInspector.TextRange} newRange | 338 * @param {!WebInspector.TextRange} newRange |
240 */ | 339 */ |
241 onTextChanged: function(oldRange, newRange) | 340 onTextChanged: function(oldRange, newRange) |
242 { | 341 { |
243 WebInspector.UISourceCodeFrame.prototype.onTextChanged.call(this, oldRan
ge, newRange); | 342 WebInspector.UISourceCodeFrame.prototype.onTextChanged.call(this, oldRan
ge, newRange); |
244 if (!this._muteColorProcessing) | 343 if (!this._muteSwatchProcessing) |
245 this._updateColorSwatches(newRange.startLine, newRange.endLine); | 344 this._updateSwatches(newRange.startLine, newRange.endLine); |
246 }, | 345 }, |
247 | 346 |
248 /** | 347 /** |
249 * @override | 348 * @override |
250 * @param {number} lineNumber | 349 * @param {number} lineNumber |
251 */ | 350 */ |
252 scrollChanged: function(lineNumber) | 351 scrollChanged: function(lineNumber) |
253 { | 352 { |
254 WebInspector.UISourceCodeFrame.prototype.scrollChanged.call(this, lineNu
mber); | 353 WebInspector.UISourceCodeFrame.prototype.scrollChanged.call(this, lineNu
mber); |
255 if (this._swatchPopoverHelper.isShowing()) | 354 if (this._swatchPopoverHelper.isShowing()) |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 else | 412 else |
314 return null; | 413 return null; |
315 } | 414 } |
316 tokenPosition = token.startColumn - 1; | 415 tokenPosition = token.startColumn - 1; |
317 } | 416 } |
318 return null; | 417 return null; |
319 }, | 418 }, |
320 | 419 |
321 __proto__: WebInspector.UISourceCodeFrame.prototype | 420 __proto__: WebInspector.UISourceCodeFrame.prototype |
322 } | 421 } |
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 |