Skip to content

Commit

Permalink
fix: preference for color inline preview not working
Browse files Browse the repository at this point in the history
  • Loading branch information
abose committed Dec 10, 2024
1 parent a0b0ead commit c3da137
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/extensionsIntegrated/CSSColorPreview/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ define(function (require, exports, module) {
MULTI_COLOR_PREVIEW_CLASS = "ico-multiple-cssColorPreview",
COLOR_PREVIEW_GUTTER_PRIORITY = 200,
COLOR_LANGUAGES= ["css", "scss", "less", "sass", "stylus", "html", "svg", "jsx", "tsx",
"php", "ejs", "erb_html", "pug"];
"php", "ejs", "erb_html", "pug"],
COLOR_LANGUAGES_SET = new Set(COLOR_LANGUAGES);


// For preferences settings, to toggle this feature on/off
Expand All @@ -59,7 +60,7 @@ define(function (require, exports, module) {
description: Strings.DESCRIPTION_CSS_COLOR_PREVIEW
});

PreferencesManager.definePreference(PREFERENCES_INLINE_COLOR_PREVIEW, "boolean", enabled, {
PreferencesManager.definePreference(PREFERENCES_INLINE_COLOR_PREVIEW, "boolean", inlinePreviewEnabled, {
description: Strings.DESCRIPTION_CSS_COLOR_PREVIEW_INLINE
});

Expand All @@ -76,7 +77,7 @@ define(function (require, exports, module) {
}

const editor = EditorManager.getActiveEditor();
if (editor) {
if (editor && editor.isGutterActive(GUTTER_NAME)) {
showGutters(editor, _getAllColorsAndLineNums(editor));
}
}
Expand Down Expand Up @@ -217,6 +218,10 @@ define(function (require, exports, module) {
function _applyInlineColor(editor, line) {
editor._currentlyColorMarkedLine = line;
editor.clearAllMarks(COLOR_MARK_NAME);
const editorLanguage = editor.document.getLanguage().getId();
if(!COLOR_LANGUAGES_SET.has(editorLanguage)) {
return;
}
const colors = detectValidColorsInLine(editor, line);
for(let color of colors){
_colorMark(editor, {line, ch: color.index}, {line, ch: color.index + color.color.length},
Expand All @@ -230,10 +235,7 @@ define(function (require, exports, module) {
if(enabled){
_addDummyGutterMarkerIfNotExist(editor, line);
}
if(!inlinePreviewEnabled){
return;
}
if(editor.hasSelection()){
if(editor.hasSelection() || !inlinePreviewEnabled){
if(editor._currentlyColorMarkedLine === line){
editor._currentlyColorMarkedLine = null;
editor.clearAllMarks(COLOR_MARK_NAME);
Expand All @@ -255,7 +257,7 @@ define(function (require, exports, module) {

// Add listener for all editor changes
EditorManager.on("activeEditorChange", function (event, newEditor, oldEditor) {
if (newEditor && newEditor.isGutterActive(GUTTER_NAME)) {
if (newEditor) {
newEditor.off("cursorActivity.colorPreview");
newEditor.on("cursorActivity.colorPreview", _cursorActivity);
// Unbind the previous editor's change event if it exists
Expand Down Expand Up @@ -435,14 +437,17 @@ define(function (require, exports, module) {
*/
function onChanged(_evt, instance, changeList) {
// for insertion and deletion, update the changed lines
if(!changeList || !changeList.length || !enabled) {
if(!changeList || !changeList.length) {
return;
}
const changeObj = changeList[0];
instance._currentlyColorMarkedLine = null;
if(inlinePreviewEnabled && changeObj.origin && changeObj.origin.startsWith("+InlineColorEditor")){
_applyInlineColor(instance, instance.getCursorPos().line);
}
if(!enabled){
return;
}
if(changeList.length === 1 && changeObj.origin === '+input' || changeObj.origin === '+delete') {
// we only do the diff updates on single key type input/delete and not bulk changes
// somehow the performance degrades if we do the diff logic on large blocks.
Expand Down

0 comments on commit c3da137

Please sign in to comment.