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

Unified 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: Addressed further comments 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/devtools/front_end/sources/CSSSourceFrame.js
diff --git a/third_party/WebKit/Source/devtools/front_end/sources/CSSSourceFrame.js b/third_party/WebKit/Source/devtools/front_end/sources/CSSSourceFrame.js
index 1a9085987f308174302c2fff97730dee4c170a3d..0ca99811d5b847f70d232d5228f7686b55da3a29 100644
--- a/third_party/WebKit/Source/devtools/front_end/sources/CSSSourceFrame.js
+++ b/third_party/WebKit/Source/devtools/front_end/sources/CSSSourceFrame.js
@@ -38,8 +38,16 @@ WebInspector.CSSSourceFrame = function(uiSourceCode)
WebInspector.UISourceCodeFrame.call(this, uiSourceCode);
this.textEditor.setAutocompleteDelegate(new WebInspector.CSSSourceFrame.AutocompleteDelegate());
this._registerShortcuts();
+
+ if (Runtime.experiments.isEnabled("sourceColorPicker")) {
+ this._colorBookmarks = [];
+ this.uiSourceCode().addEventListener(WebInspector.UISourceCode.Events.WorkingCopyChanged, this._workingCopyChanged, this);
+ }
}
+/** @type {number} */
+WebInspector.CSSSourceFrame.UpdateTimeout = 200;
+
WebInspector.CSSSourceFrame.prototype = {
_registerShortcuts: function()
{
@@ -97,11 +105,119 @@ WebInspector.CSSSourceFrame.prototype = {
return true;
},
+ /**
+ * @param {!WebInspector.Event} event
+ */
+ _workingCopyChanged: function(event)
+ {
+ if (this._updateTimeout)
+ clearTimeout(this._updateTimeout);
+ this._updateTimeout = setTimeout(this._updateColorSwatches.bind(this), WebInspector.CSSSourceFrame.UpdateTimeout);
+ },
+
+ _updateColorSwatches: function(){
+ if (this._updateTimeout)
+ clearTimeout(this._updateTimeout);
+ this._updateTimeout = null;
+
+ var colorPositions = this._extractColorPositions(this._uiSourceCode.workingCopy());
+ this.textEditor.operation(this._putColorSwatchesInline.bind(this, colorPositions));
+ },
+
+ /**
+ * @param {string} content
+ * @return {?Array<!WebInspector.CSSSourceFrame.ColorPosition>}
+ */
+ _extractColorPositions: function(content)
+ {
+ if (!content)
+ return null;
+
+ var colorPositions = [];
+
lushnikov 2016/07/13 21:21:53 lets kill this new line
flandy 2016/07/13 23:02:45 Done.
+ var text = new WebInspector.Text(content);
+ var numberOfLines = text.lineCount();
+ for (var lineNumber = 0; lineNumber < numberOfLines; lineNumber++) {
+ 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
+
lushnikov 2016/07/13 21:21:53 let's remove this new line
flandy 2016/07/13 23:02:46 Done.
+ var colorRegex = /[\s:;,(){}]((?:rgb|hsl)a?\([^)]+\)|#[0-9a-f]{8}|#[0-9a-f]{6}|#[0-9a-f]{3,4}|[a-z]+)(?=[\s;,(){}])/gi;
+ var match;
+
lushnikov 2016/07/13 21:21:53 let's remove this new line as well
flandy 2016/07/13 23:02:45 Done.
+ while ((match = colorRegex.exec(line)) !== null) {
+ if (match.length < 2)
+ continue;
+
+ var colorText = match[1];
+ var color = WebInspector.Color.parse(colorText);
+ if (color)
+ colorPositions.push(new WebInspector.CSSSourceFrame.ColorPosition(color, lineNumber, match.index + 1, colorText.length));
+ }
+ }
+
+ return colorPositions;
+ },
+
+ /**
+ * @param {?Array<!WebInspector.CSSSourceFrame.ColorPosition>} colorPositions
+ */
+ _putColorSwatchesInline: function(colorPositions)
+ {
+ this._clearColorBookmarks();
+ if (!colorPositions)
+ return;
+
+ for (var i = 0; i < colorPositions.length; i++) {
+ var colorPosition = colorPositions[i];
+
+ var swatch = WebInspector.ColorSwatch.create();
+ swatch.setColorText(colorPosition.color.asString(WebInspector.Color.Format.Original));
+ swatch.hideText(true);
+
+ var bookmark = this.textEditor.addBookmark(colorPosition.textRange.startLine, colorPosition.textRange.startColumn, swatch);
+ this._colorBookmarks.push(bookmark);
+ }
+ },
+
+ _clearColorBookmarks: function() {
+ for (var i = 0; i < this._colorBookmarks.length; i++)
+ this._colorBookmarks[i].clear();
+ this._colorBookmarks = [];
+ },
+
+ /**
+ * @override
+ */
+ 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
+ {
+ WebInspector.UISourceCodeFrame.prototype.onTextEditorContentLoaded.call(this);
+ if (Runtime.experiments.isEnabled("sourceColorPicker"))
+ this._updateColorSwatches();
+ },
+
+ 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
+ {
+ this.uiSourceCode().removeEventListener(WebInspector.UISourceCode.Events.WorkingCopyChanged, this._workingCopyChanged, this);
+ WebInspector.UISourceCodeFrame.prototype.dispose.call(this);
+ },
+
__proto__: WebInspector.UISourceCodeFrame.prototype
}
/**
* @constructor
+ * @param {!WebInspector.Color} color
+ * @param {number} lineNumber
+ * @param {number} startColumn
+ * @param {number} textLength
+ */
+WebInspector.CSSSourceFrame.ColorPosition = function(color, lineNumber, startColumn, textLength)
+{
+ this.color = color;
+ this.textRange = new WebInspector.TextRange(lineNumber, startColumn, lineNumber, startColumn + textLength);
+}
+
+/**
+ * @constructor
* @implements {WebInspector.TextEditorAutocompleteDelegate}
*/
WebInspector.CSSSourceFrame.AutocompleteDelegate = function()

Powered by Google App Engine
This is Rietveld 408576698