Chromium Code Reviews| 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.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 Loading... | |
| 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 if (this._updateTimeout) | |
| 114 clearTimeout(this._updateTimeout); | |
| 115 this._updateTimeout = setTimeout(this._updateColorSwatches.bind(this), W ebInspector.CSSSourceFrame.UpdateTimeout); | |
| 116 }, | |
| 117 | |
| 118 _updateColorSwatches: function(){ | |
| 119 if (this._updateTimeout) | |
| 120 clearTimeout(this._updateTimeout); | |
| 121 this._updateTimeout = null; | |
| 122 | |
| 123 var colorPositions = this._extractColorPositions(this._uiSourceCode.work ingCopy()); | |
| 124 this.textEditor.operation(this._putColorSwatchesInline.bind(this, colorP ositions)); | |
| 125 }, | |
| 126 | |
| 127 /** | |
| 128 * @param {string} content | |
| 129 * @return {?Array<!WebInspector.CSSSourceFrame.ColorPosition>} | |
| 130 */ | |
| 131 _extractColorPositions: function(content) | |
| 132 { | |
| 133 if (!content) | |
| 134 return null; | |
| 135 | |
| 136 var colorPositions = []; | |
| 137 | |
|
lushnikov
2016/07/13 21:21:53
lets kill this new line
flandy
2016/07/13 23:02:45
Done.
| |
| 138 var text = new WebInspector.Text(content); | |
| 139 var numberOfLines = text.lineCount(); | |
| 140 for (var lineNumber = 0; lineNumber < numberOfLines; lineNumber++) { | |
| 141 var line = text.lineAt(lineNumber) + "\n"; | |
|
lushnikov
2016/07/13 21:21:53
why do you need "\n" in the end?
flandy
2016/07/13 23:02:46
The Regex also makes sure that the character follo
| |
| 142 | |
|
lushnikov
2016/07/13 21:21:53
let's remove this new line
flandy
2016/07/13 23:02:46
Done.
| |
| 143 var colorRegex = /[\s:;,(){}]((?:rgb|hsl)a?\([^)]+\)|#[0-9a-f]{8}|#[ 0-9a-f]{6}|#[0-9a-f]{3,4}|[a-z]+)(?=[\s;,(){}])/gi; | |
| 144 var match; | |
| 145 | |
|
lushnikov
2016/07/13 21:21:53
let's remove this new line as well
flandy
2016/07/13 23:02:45
Done.
| |
| 146 while ((match = colorRegex.exec(line)) !== null) { | |
| 147 if (match.length < 2) | |
| 148 continue; | |
| 149 | |
| 150 var colorText = match[1]; | |
| 151 var color = WebInspector.Color.parse(colorText); | |
| 152 if (color) | |
| 153 colorPositions.push(new WebInspector.CSSSourceFrame.ColorPos ition(color, lineNumber, match.index + 1, colorText.length)); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 return colorPositions; | |
| 158 }, | |
| 159 | |
| 160 /** | |
| 161 * @param {?Array<!WebInspector.CSSSourceFrame.ColorPosition>} colorPosition s | |
| 162 */ | |
| 163 _putColorSwatchesInline: function(colorPositions) | |
| 164 { | |
| 165 this._clearColorBookmarks(); | |
| 166 if (!colorPositions) | |
| 167 return; | |
| 168 | |
| 169 for (var i = 0; i < colorPositions.length; i++) { | |
| 170 var colorPosition = colorPositions[i]; | |
| 171 | |
| 172 var swatch = WebInspector.ColorSwatch.create(); | |
| 173 swatch.setColorText(colorPosition.color.asString(WebInspector.Color. Format.Original)); | |
| 174 swatch.hideText(true); | |
| 175 | |
| 176 var bookmark = this.textEditor.addBookmark(colorPosition.textRange.s tartLine, colorPosition.textRange.startColumn, swatch); | |
| 177 this._colorBookmarks.push(bookmark); | |
| 178 } | |
| 179 }, | |
| 180 | |
| 181 _clearColorBookmarks: function() { | |
| 182 for (var i = 0; i < this._colorBookmarks.length; i++) | |
| 183 this._colorBookmarks[i].clear(); | |
| 184 this._colorBookmarks = []; | |
| 185 }, | |
| 186 | |
| 187 /** | |
| 188 * @override | |
| 189 */ | |
| 190 onTextEditorContentLoaded: function() | |
|
lushnikov
2016/07/13 21:21:53
let's rather override onTextChanged method from UI
flandy
2016/07/13 23:02:45
Switched to override onTextEditorContentLoaded and
| |
| 191 { | |
| 192 WebInspector.UISourceCodeFrame.prototype.onTextEditorContentLoaded.call( this); | |
| 193 if (Runtime.experiments.isEnabled("sourceColorPicker")) | |
| 194 this._updateColorSwatches(); | |
| 195 }, | |
| 196 | |
| 197 dispose: function() | |
|
lushnikov
2016/07/13 21:21:53
you will not need this method as you'll stop liste
flandy
2016/07/13 23:02:46
Done. Removed. It should clean up appropriately wi
| |
| 198 { | |
| 199 this.uiSourceCode().removeEventListener(WebInspector.UISourceCode.Events .WorkingCopyChanged, this._workingCopyChanged, this); | |
| 200 WebInspector.UISourceCodeFrame.prototype.dispose.call(this); | |
| 201 }, | |
| 202 | |
| 100 __proto__: WebInspector.UISourceCodeFrame.prototype | 203 __proto__: WebInspector.UISourceCodeFrame.prototype |
| 101 } | 204 } |
| 102 | 205 |
| 103 /** | 206 /** |
| 104 * @constructor | 207 * @constructor |
| 208 * @param {!WebInspector.Color} color | |
| 209 * @param {number} lineNumber | |
| 210 * @param {number} startColumn | |
| 211 * @param {number} textLength | |
| 212 */ | |
| 213 WebInspector.CSSSourceFrame.ColorPosition = function(color, lineNumber, startCol umn, textLength) | |
| 214 { | |
| 215 this.color = color; | |
| 216 this.textRange = new WebInspector.TextRange(lineNumber, startColumn, lineNum ber, startColumn + textLength); | |
| 217 } | |
| 218 | |
| 219 /** | |
| 220 * @constructor | |
| 105 * @implements {WebInspector.TextEditorAutocompleteDelegate} | 221 * @implements {WebInspector.TextEditorAutocompleteDelegate} |
| 106 */ | 222 */ |
| 107 WebInspector.CSSSourceFrame.AutocompleteDelegate = function() | 223 WebInspector.CSSSourceFrame.AutocompleteDelegate = function() |
| 108 { | 224 { |
| 109 this._simpleDelegate = new WebInspector.SimpleAutocompleteDelegate(".-$"); | 225 this._simpleDelegate = new WebInspector.SimpleAutocompleteDelegate(".-$"); |
| 110 } | 226 } |
| 111 | 227 |
| 112 WebInspector.CSSSourceFrame._backtrackDepth = 10; | 228 WebInspector.CSSSourceFrame._backtrackDepth = 10; |
| 113 | 229 |
| 114 WebInspector.CSSSourceFrame.AutocompleteDelegate.prototype = { | 230 WebInspector.CSSSourceFrame.AutocompleteDelegate.prototype = { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 var propertyToken = this._backtrackPropertyToken(editor, prefixRange.sta rtLine, prefixRange.startColumn - 1); | 304 var propertyToken = this._backtrackPropertyToken(editor, prefixRange.sta rtLine, prefixRange.startColumn - 1); |
| 189 if (!propertyToken) | 305 if (!propertyToken) |
| 190 return this._simpleDelegate.wordsWithPrefix(editor, prefixRange, sub stituteRange); | 306 return this._simpleDelegate.wordsWithPrefix(editor, prefixRange, sub stituteRange); |
| 191 | 307 |
| 192 var line = editor.line(prefixRange.startLine); | 308 var line = editor.line(prefixRange.startLine); |
| 193 var tokenContent = line.substring(propertyToken.startColumn, propertyTok en.endColumn); | 309 var tokenContent = line.substring(propertyToken.startColumn, propertyTok en.endColumn); |
| 194 var keywords = WebInspector.CSSMetadata.keywordsForProperty(tokenContent ); | 310 var keywords = WebInspector.CSSMetadata.keywordsForProperty(tokenContent ); |
| 195 return keywords.startsWith(prefix); | 311 return keywords.startsWith(prefix); |
| 196 }, | 312 }, |
| 197 } | 313 } |
| OLD | NEW |