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