Skip to content

Commit b11dd8c

Browse files
committed
chore: ux improvement for interacting with color gutter
1 parent 7d865cd commit b11dd8c

File tree

2 files changed

+49
-34
lines changed

2 files changed

+49
-34
lines changed

src/extensionsIntegrated/CSSColorPreview/main.js

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -146,39 +146,29 @@ define(function (require, exports, module) {
146146

147147
/**
148148
* To move the cursor to the color text and display the color quick edit
149-
* @param {CodeMirror} codeMirror the codemirror instance
149+
* @param {Editor} editor the codemirror instance
150150
* @param {Number} lineNumber the line number that is clicked
151-
* @param {String} gutter the gutter name
151+
* @param {string} colorValue the color value clicked
152+
* @param {boolean} dontEdit if set to true,color editor won't be opened
152153
*/
153-
function colorIconClicked(codeMirror, lineNumber, gutter) {
154-
const editor = EditorManager.getActiveEditor();
155-
156-
if(gutter === GUTTER_NAME) {
157-
158-
let colorValue;
159-
160-
for(let i of codeMirror.colorGutters) {
161-
if(i.lineNumber === lineNumber) {
162-
colorValue = i.colorValues[0];
163-
}
164-
}
165-
166-
const lineText = editor.getLine(lineNumber);
167-
const colorIndex = lineText.indexOf(colorValue);
168-
169-
if (colorIndex !== -1) {
170-
// Place cursor at the start of the color text
171-
if (editor) {
172-
editor.setCursorPos(lineNumber, colorIndex);
154+
function _colorIconClicked(editor, lineNumber, colorValue, dontEdit) {
155+
const lineText = editor.getLine(lineNumber);
156+
const colorIndex = lineText.indexOf(colorValue);
157+
const currentPos = editor.getCursorPos(false, "start");
158+
if(!(currentPos.line === lineNumber && currentPos.ch === colorIndex)) {
159+
editor.setCursorPos(lineNumber, colorIndex);
160+
editor.focus();
161+
}
173162

174-
// Added a 50ms delay with setTimeout to make sure the quick edit menu toggles correctly.
175-
// Without it, closing the menu trigger text selection, reopening the menu.
176-
setTimeout(() => {
177-
CommandManager.execute(Commands.TOGGLE_QUICK_EDIT);
178-
}, 50);
179-
}
180-
}
163+
if(dontEdit){
164+
return;
181165
}
166+
167+
// Added a 50ms delay with setTimeout to make sure the quick edit menu toggles correctly.
168+
// Without it, closing the menu trigger text selection, reopening the menu.
169+
setTimeout(() => {
170+
CommandManager.execute(Commands.TOGGLE_QUICK_EDIT);
171+
}, 50);
182172
}
183173

184174
/**
@@ -194,24 +184,24 @@ define(function (require, exports, module) {
194184
editor.clearGutter(GUTTER_NAME); // clear color markers
195185
_addDummyGutterMarkerIfNotExist(editor, editor.getCursorPos().line);
196186

197-
cm.on("gutterClick", (codeMirror, lineNumber, gutter) => {
198-
colorIconClicked(codeMirror, lineNumber, gutter);
199-
});
200-
201187
// Only add markers if enabled
202188
if (enabled) {
203189
cm.colorGutters = _.sortBy(_results, "lineNumber");
204190

205191
cm.colorGutters.forEach(function (obj) {
206192
let $marker;
207-
208193
if (obj.colorValues.length === 1) {
209194
// Single color preview
210195
$marker = $("<i>")
211196
.addClass("ico-cssColorPreview")
212197
.css('background-color', obj.colorValues[0]);
213198

214199
editor.setGutterMarker(obj.lineNumber, GUTTER_NAME, $marker[0]);
200+
$marker.click((event)=>{
201+
event.preventDefault();
202+
event.stopPropagation();
203+
_colorIconClicked(editor, obj.lineNumber, obj.colorValues[0], false);
204+
});
215205
} else {
216206
// Multiple colors preview
217207
$marker = $("<div>").addClass("ico-multiple-cssColorPreview");
@@ -232,6 +222,11 @@ define(function (require, exports, module) {
232222
'background-color': color,
233223
...positions[index]
234224
});
225+
$colorBox.click((event)=>{
226+
event.preventDefault();
227+
event.stopPropagation();
228+
_colorIconClicked(editor, obj.lineNumber, color, false);
229+
});
235230
$marker.append($colorBox);
236231
}
237232
});

src/styles/Extn-CSSColorPreview.less

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
height:.7em;
1919
width: .7em;
2020
position: relative;
21+
pointer-events: auto;
2122
}
2223

2324
/* Multiple colors preview container (this is a div) */
@@ -31,6 +32,16 @@
3132
top: .2em;
3233
width: .9em;
3334
height:.9em;
35+
pointer-events: auto;
36+
transition: all 0.3s ease;
37+
}
38+
39+
.ico-multiple-cssColorPreview:hover {
40+
top: -.8em;
41+
width: 2em;
42+
height:2em;
43+
pointer-events: auto;
44+
transition: all 0.3s ease;
3445
}
3546

3647
/* For individual color boxes, they will be added inside the main .ico-multiple-cssColorPreview div */
@@ -39,4 +50,13 @@
3950
width: .4em;
4051
height: .4em;
4152
box-sizing: border-box;
53+
transition: all 0.3s ease;
4254
}
55+
56+
.ico-multiple-cssColorPreview:hover .color-box {
57+
position: absolute;
58+
width: calc(1em - 1px);
59+
height: calc(1em - 1px);
60+
box-sizing: border-box;
61+
transition: all 0.3s ease;
62+
}

0 commit comments

Comments
 (0)