Skip to content

Commit 5c6e086

Browse files
committed
feat: inline color preview on hover on the color box
1 parent 42e31f2 commit 5c6e086

File tree

1 file changed

+41
-6
lines changed
  • src/extensionsIntegrated/CSSColorPreview

1 file changed

+41
-6
lines changed

src/extensionsIntegrated/CSSColorPreview/main.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ define(function (require, exports, module) {
4343
DUMMY_GUTTER_CLASS = "CodeMirror-colorGutter-none",
4444
SINGLE_COLOR_PREVIEW_CLASS = "ico-cssColorPreview",
4545
MULTI_COLOR_PREVIEW_CLASS = "ico-multiple-cssColorPreview",
46+
COLOR_MARK_NAME = "colorMarker",
4647
COLOR_PREVIEW_GUTTER_PRIORITY = 200,
4748
COLOR_LANGUAGES= ["css", "scss", "less", "sass", "stylus", "html", "svg", "jsx", "tsx",
4849
"php", "ejs", "erb_html", "pug"];
@@ -69,7 +70,7 @@ define(function (require, exports, module) {
6970
}
7071

7172
const editor = EditorManager.getActiveEditor();
72-
if (editor) {
73+
if (editor && editor.isGutterActive(GUTTER_NAME)) {
7374
showGutters(editor, _getAllColorsAndLineNums(editor));
7475
}
7576
}
@@ -143,13 +144,13 @@ define(function (require, exports, module) {
143144
// Single color preview
144145
$marker = $("<i>")
145146
.addClass(SINGLE_COLOR_PREVIEW_CLASS)
146-
.css('background-color', obj.colorValues[0]);
147+
.css('background-color', obj.colorValues[0].color);
147148

148149
editor.setGutterMarker(obj.lineNumber, GUTTER_NAME, $marker[0]);
149150
$marker.click((event)=>{
150151
event.preventDefault();
151152
event.stopPropagation();
152-
_colorIconClicked(editor, obj.lineNumber, obj.colorValues[0]);
153+
_colorIconClicked(editor, obj.lineNumber, obj.colorValues[0].color);
153154
});
154155
} else {
155156
// Multiple colors preview
@@ -168,20 +169,30 @@ define(function (require, exports, module) {
168169
const $colorBox = $("<div>")
169170
.addClass("color-box")
170171
.css({
171-
'background-color': color,
172+
'background-color': color.color,
172173
...positions[index]
173174
});
174175
$colorBox.click((event)=>{
175176
event.preventDefault();
176177
event.stopPropagation();
177-
_colorIconClicked(editor, obj.lineNumber, color);
178+
_colorIconClicked(editor, obj.lineNumber, color.color);
178179
});
179180
$marker.append($colorBox);
180181
}
181182
});
182183

183184
editor.setGutterMarker(obj.lineNumber, GUTTER_NAME, $marker[0]);
184185
}
186+
$marker.mouseenter(event=>{
187+
event.preventDefault();
188+
event.stopPropagation();
189+
_applyInlineColor(editor, obj.lineNumber);
190+
});
191+
$marker.mouseleave(event=>{
192+
event.preventDefault();
193+
event.stopPropagation();
194+
editor.clearAllMarks(COLOR_MARK_NAME);
195+
});
185196
});
186197
}
187198
});
@@ -200,6 +211,10 @@ define(function (require, exports, module) {
200211
function _cursorActivity(_evt, editor){
201212
// this is to prevent a gutter gap in the active line if there is no color on this line.
202213
_addDummyGutterMarkerIfNotExist(editor, editor.getCursorPos().line);
214+
if(editor._currentlyColorMarkedLine){
215+
editor.clearAllMarks(COLOR_MARK_NAME);
216+
editor._currentlyColorMarkedLine = null;
217+
}
203218
}
204219

205220
/**
@@ -235,6 +250,26 @@ define(function (require, exports, module) {
235250
}
236251
}
237252

253+
function _colorMark(editor, from, to, color) {
254+
editor.markText(COLOR_MARK_NAME, from, to, {
255+
css: `
256+
--bg-color-mark: ${color};
257+
background: var(--bg-color-mark);
258+
color: lch(from var(--bg-color-mark) calc((50 - l) * infinity) 0 0);
259+
`
260+
});
261+
}
262+
263+
function _applyInlineColor(editor, line) {
264+
editor._currentlyColorMarkedLine = line;
265+
editor.clearAllMarks(COLOR_MARK_NAME);
266+
const colors = detectValidColorsInLine(editor, line);
267+
for(let color of colors){
268+
_colorMark(editor, {line, ch: color.index}, {line, ch: color.index + color.color.length},
269+
color.color);
270+
}
271+
}
272+
238273
/**
239274
* Checks for preference changes, to enable/disable the feature
240275
*/
@@ -321,7 +356,7 @@ define(function (require, exports, module) {
321356
}
322357

323358
// Return up to 4 colors
324-
return validColors.slice(0, 4).map(item => item.color);
359+
return validColors;
325360
}
326361

327362
/**

0 commit comments

Comments
 (0)