Skip to content

Commit 18a7578

Browse files
devvaannshabose
authored andcommitted
feat: set cursor pos and opens quick edit when color icon is clicked
1 parent 245b3a6 commit 18a7578

File tree

1 file changed

+45
-0
lines changed
  • src/extensionsIntegrated/CSSColorPreview

1 file changed

+45
-0
lines changed

src/extensionsIntegrated/CSSColorPreview/main.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ define(function (require, exports, module) {
3333
Editor = require("editor/Editor").Editor,
3434
PreferencesManager = require("preferences/PreferencesManager"),
3535
MainViewManager = require("view/MainViewManager"),
36+
Commands = require("command/Commands"),
37+
CommandManager = require("command/CommandManager"),
3638
Strings = require("strings");
3739

3840
// Extension variables.
@@ -142,6 +144,45 @@ define(function (require, exports, module) {
142144
});
143145
}
144146

147+
148+
/**
149+
* To move the cursor to the color text and display the color quick edit
150+
* @param {Editor} codem the codemirror instance
151+
* @param {Number} lineNumber the line number that is clicked
152+
* @param {String} gutter the gutter name
153+
*/
154+
function colorIconClicked(codem, lineNumber, gutter) {
155+
const editor = EditorManager.getActiveEditor();
156+
const cm = editor._codeMirror;
157+
158+
if(gutter === 'CodeMirror-linenumbers') {
159+
160+
let colorValue;
161+
162+
for(let i of codem.colorGutters) {
163+
if(i.lineNumber === lineNumber) {
164+
colorValue = i.colorValues[0];
165+
}
166+
}
167+
168+
const lineText = cm.getLine(lineNumber);
169+
const colorIndex = lineText.indexOf(colorValue);
170+
171+
if (colorIndex !== -1) {
172+
// Place cursor at the start of the color text
173+
if (editor) {
174+
editor.setCursorPos(lineNumber, colorIndex);
175+
176+
// Added a 50ms delay with setTimeout to make sure the quick edit menu toggles correctly.
177+
// Without it, closing the menu trigger text selection, reopening the menu.
178+
setTimeout(() => {
179+
CommandManager.execute(Commands.TOGGLE_QUICK_EDIT);
180+
}, 50);
181+
}
182+
}
183+
}
184+
}
185+
145186
/**
146187
* To display the color marks on the gutter
147188
* @param {activeEditor} editor
@@ -155,6 +196,10 @@ define(function (require, exports, module) {
155196
editor.clearGutter(GUTTER_NAME); // clear color markers
156197
_addDummyGutterMarkerIfNotExist(editor, editor.getCursorPos().line);
157198

199+
cm.on("gutterClick", (codem, lineNumber, gutter) => {
200+
colorIconClicked(codem, lineNumber, gutter);
201+
});
202+
158203
// Only add markers if enabled
159204
if (enabled) {
160205
cm.colorGutters = _.sortBy(_results, "lineNumber");

0 commit comments

Comments
 (0)