Skip to content

Commit 4f99ad2

Browse files
committed
feat: add multiple color support
1 parent 122404a commit 4f99ad2

File tree

2 files changed

+80
-27
lines changed

2 files changed

+80
-27
lines changed
Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1+
/* Single color preview */
12
.ico-cssColorPreview {
2-
border: solid 1px rgba(0,0,0,.2);
3+
border: solid 1px rgba(0, 0, 0, 0.2);
34
display: inline-block;
4-
height: 7px;
5-
width: 7px;
6-
margin-left: 3px;
7-
vertical-align: middle;
5+
height: 14px;
6+
width: 14px;
7+
margin-left: 6px;
8+
position: absolute;
9+
top: 8px;
10+
z-index: 99999;
11+
}
12+
13+
/* Multiple colors preview container (this is a div) */
14+
.ico-multiple-cssColorPreview {
815
position: relative;
16+
display: inline-block;
17+
width: 14px;
18+
height: 14px;
19+
margin-left: 6px;
20+
border: solid 1px rgba(0, 0, 0, 0.2);
921
z-index: 99999;
1022
}
23+
24+
/* For individual color boxes, they will be added inside the main .ico-multiple-cssColorPreview div */
25+
.ico-multiple-cssColorPreview .color-box {
26+
position: absolute;
27+
width: 6px;
28+
height: 6px;
29+
box-sizing: border-box;
30+
}

src/extensions/default/CSSColorPreview/main.js

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*
2020
*/
2121

22+
2223
/* Displays a color preview in the gutter for any file containing color values */
2324

2425
define(function (require, exports, module) {
@@ -48,42 +49,42 @@ define(function (require, exports, module) {
4849
var CssColorPreview = {
4950

5051
// Get editor
51-
getEditor: function() {
52+
getEditor: function () {
5253
return EditorManager.getActiveEditor();
5354
},
5455

5556
// show color preview
56-
showColorMarks: function() {
57+
showColorMarks: function () {
5758
// Check if the extension is enabled
5859
if (!enabled) {
5960
CssColorPreview.removeColorMarks();
6061
return;
6162
}
6263

6364
var editor = CssColorPreview.getEditor();
64-
if(editor) {
65+
if (editor) {
6566

6667
var cm = editor._codeMirror;
6768
var nLen = cm.lineCount();
6869
var aColors = [];
6970

7071
// match colors and push into an array
71-
for(var i = 0; i < nLen; i++) {
72+
for (var i = 0; i < nLen; i++) {
7273
var lineText = cm.getLine(i);
7374

74-
if( (lineText.indexOf('/*')!==-1) || (lineText.indexOf('*/')!==-1) ){
75+
if ((lineText.indexOf('/*') !== -1) || (lineText.indexOf('*/') !== -1)) {
7576
continue;
7677
} else {
7778
var regx = /:.*?;/g;
7879
lineText = lineText.match(regx);
79-
if(lineText){
80-
var tempItem = lineText[0].match(COLOR_REGEX);
81-
// todo current support one color to show only
82-
if(tempItem){
83-
var tempColor = tempItem[0];
80+
if (lineText) {
81+
var tempColors = lineText[0].match(COLOR_REGEX);
82+
// Support up to 4 colors
83+
if (tempColors && tempColors.length > 0) {
84+
var colors = tempColors.slice(0, 4);
8485
aColors.push({
8586
lineNumber: i,
86-
colorValue: tempColor
87+
colorValues: colors
8788
});
8889
}
8990
}
@@ -98,7 +99,7 @@ define(function (require, exports, module) {
9899
CssColorPreview.showColorMarks();
99100
},
100101

101-
init: function() {
102+
init: function () {
102103
CssColorPreview.showColorMarks();
103104
CssColorPreview.registerHandlers();
104105
},
@@ -108,7 +109,7 @@ define(function (require, exports, module) {
108109
EditorManager.off("activeEditorChange", CssColorPreview.onChanged);
109110

110111
// Add listener for all editor changes
111-
EditorManager.on("activeEditorChange", function(event, newEditor, oldEditor) {
112+
EditorManager.on("activeEditorChange", function (event, newEditor, oldEditor) {
112113
if (newEditor) {
113114
// Unbind the previous editor's change event if it exists
114115
if (oldEditor) {
@@ -129,7 +130,7 @@ define(function (require, exports, module) {
129130
});
130131
},
131132

132-
initGutter: function(editor) {
133+
initGutter: function (editor) {
133134

134135
var cm = editor._codeMirror;
135136
var gutters = cm.getOption("gutters").slice(0);
@@ -140,8 +141,8 @@ define(function (require, exports, module) {
140141
}
141142
},
142143

143-
showGutters: function(editor, _results) {
144-
if(editor && enabled){
144+
showGutters: function (editor, _results) {
145+
if (editor && enabled) {
145146
CssColorPreview.initGutter(editor);
146147
var cm = editor._codeMirror;
147148
cm.clearGutter(gutterName); // clear color markers
@@ -151,17 +152,49 @@ define(function (require, exports, module) {
151152
cm.colorGutters = _.sortBy(_results, "lineNumber");
152153

153154
cm.colorGutters.forEach(function (obj) {
154-
var $marker = $("<i>")
155-
.addClass("ico-cssColorPreview")
156-
.html("&nbsp;").css('background-color', obj.colorValue);
157-
cm.setGutterMarker(obj.lineNumber, gutterName, $marker[0]);
155+
let $marker;
156+
157+
if (obj.colorValues.length === 1) {
158+
// Single color preview
159+
$marker = $("<i>")
160+
.addClass("ico-cssColorPreview")
161+
.css('background-color', obj.colorValues[0]);
162+
163+
cm.setGutterMarker(obj.lineNumber, gutterName, $marker[0]);
164+
} else {
165+
// Multiple colors preview
166+
$marker = $("<div>").addClass("ico-multiple-cssColorPreview");
167+
168+
// Positions for up to 4 colors in grid
169+
const positions = [
170+
{ top: 0, left: 0 },
171+
{ top: 0, right: 0 },
172+
{ bottom: 0, right: 0 },
173+
{ bottom: 0, left: 0 }
174+
];
175+
176+
obj.colorValues.forEach((color, index) => {
177+
if (index < 4) {
178+
const $colorBox = $("<div>")
179+
.addClass("color-box")
180+
.css({
181+
'background-color': color,
182+
...positions[index]
183+
});
184+
$marker.append($colorBox);
185+
}
186+
});
187+
188+
cm.setGutterMarker(obj.lineNumber, gutterName, $marker[0]);
189+
}
158190
});
159191
}
192+
160193
}
161194
},
162195

163196
// Method to remove colors when disabled
164-
removeColorMarks: function() {
197+
removeColorMarks: function () {
165198
var editor = CssColorPreview.getEditor();
166199
if (editor) {
167200
var cm = editor._codeMirror;
@@ -181,7 +214,7 @@ define(function (require, exports, module) {
181214
}
182215

183216
// init after appReady
184-
AppInit.appReady(function() {
217+
AppInit.appReady(function () {
185218
PreferencesManager.on("change", PREFERENCES_CSS_COLOR_PREVIEW, preferenceChanged);
186219
setTimeout(CssColorPreview.init, 1000);
187220
});

0 commit comments

Comments
 (0)