Skip to content

Commit

Permalink
feat: dynamically update colors when user toggles the preference
Browse files Browse the repository at this point in the history
  • Loading branch information
devvaannsh committed Nov 30, 2024
1 parent e6b2a37 commit 8dc0a41
Showing 1 changed file with 80 additions and 36 deletions.
116 changes: 80 additions & 36 deletions src/extensionsIntegrated/CSSColorPreview/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ define(function (require, exports, module) {
ColorUtils = require('utils/ColorUtils'),
AppInit = require("utils/AppInit"),
PreferencesManager = require("preferences/PreferencesManager"),
MainViewManager = require("view/MainViewManager"),
Strings = require("strings");

// Extension variables.
Expand All @@ -46,6 +47,46 @@ define(function (require, exports, module) {
description: Strings.DESCRIPTION_CSS_COLOR_PREVIEW
});

/**
* Responsible to get all the colors and their respective line numbers.
*
* @param {Editor} editor
* @return {Array.<Object>} an array of objects with all the line nos and,
* the colors to be added on those lines
*/
function _getAllColorsAndLineNums(editor) {

const cm = editor._codeMirror;
const nLen = cm.lineCount();
const aColors = [];

// match colors and push into an array
for (let i = 0; i < nLen; i++) {
let lineText = cm.getLine(i);

if ((lineText.indexOf('/*') !== -1) || (lineText.indexOf('*/') !== -1)) {
continue;
} else {
let regx = /:[^;]*;/g;

lineText = lineText.match(regx);
if (lineText) {
let tempColors = lineText[0].match(COLOR_REGEX);
// Support up to 4 colors
if (tempColors && tempColors.length > 0) {
let colors = tempColors.slice(0, 4);
aColors.push({
lineNumber: i,
colorValues: colors
});
}
}
}
}

return aColors;
}

/**
* Gets all the colors that are to be displayed
*
Expand All @@ -61,47 +102,47 @@ define(function (require, exports, module) {
const editor = EditorManager.getActiveEditor();
if (editor) {

const cm = editor._codeMirror;
const nLen = cm.lineCount();
const aColors = [];

// match colors and push into an array
for (let i = 0; i < nLen; i++) {
let lineText = cm.getLine(i);

if ((lineText.indexOf('/*') !== -1) || (lineText.indexOf('*/') !== -1)) {
continue;
} else {
let regx = /:[^;]*;/g;

lineText = lineText.match(regx);
if (lineText) {
let tempColors = lineText[0].match(COLOR_REGEX);
// Support up to 4 colors
if (tempColors && tempColors.length > 0) {
let colors = tempColors.slice(0, 4);
aColors.push({
lineNumber: i,
colorValues: colors
});
}
}
}
}

const aColors = _getAllColorsAndLineNums(editor);
showGutters(editor, aColors);

}
}


/**
* To add the color marks on the gutter of all the active editors
* This function is called when the user toggles the
* CssColorPreview preference and set it to true
*/
function addColorMarksToAllEditors() {

const allActiveEditors = MainViewManager.getAllViewedEditors();

allActiveEditors.forEach((activeEditor) => {
const currEditor = activeEditor.editor;
if(currEditor) {

const aColors = _getAllColorsAndLineNums(currEditor);
showGutters(currEditor, aColors);

}
});
}

/**
* To remove the color marks from the gutter
* To remove the color marks from the gutter of all the active editors
*/
function removeColorMarks() {
const editor = EditorManager.getActiveEditor();
if (editor) {
const cm = editor._codeMirror;
cm.clearGutter(gutterName);
}

const allActiveEditors = MainViewManager.getAllViewedEditors();

allActiveEditors.forEach((activeEditor) => {
const currEditor = activeEditor.editor;
if(currEditor) {
const cm = currEditor._codeMirror;
cm.clearGutter(gutterName);
}
});
}

/**
Expand Down Expand Up @@ -225,9 +266,11 @@ define(function (require, exports, module) {
const value = PreferencesManager.get(PREFERENCES_CSS_COLOR_PREVIEW);
enabled = value;
if (!value) {
// to dynamically remove color to all active editors
removeColorMarks();
} else {
showColorMarks();
// to dynamically add color to all active editors
addColorMarksToAllEditors();
}
}

Expand All @@ -242,7 +285,8 @@ define(function (require, exports, module) {
* Driver function, runs at the start of the program
*/
function init() {
showColorMarks();
// preferenceChanged calls 'showColorMarks' or 'removeColorMarks'
preferenceChanged();
registerHandlers();
}

Expand Down

0 comments on commit 8dc0a41

Please sign in to comment.