Skip to content

Commit 8dc0a41

Browse files
committed
feat: dynamically update colors when user toggles the preference
1 parent e6b2a37 commit 8dc0a41

File tree

1 file changed

+80
-36
lines changed
  • src/extensionsIntegrated/CSSColorPreview

1 file changed

+80
-36
lines changed

src/extensionsIntegrated/CSSColorPreview/main.js

Lines changed: 80 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ define(function (require, exports, module) {
3131
ColorUtils = require('utils/ColorUtils'),
3232
AppInit = require("utils/AppInit"),
3333
PreferencesManager = require("preferences/PreferencesManager"),
34+
MainViewManager = require("view/MainViewManager"),
3435
Strings = require("strings");
3536

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

50+
/**
51+
* Responsible to get all the colors and their respective line numbers.
52+
*
53+
* @param {Editor} editor
54+
* @return {Array.<Object>} an array of objects with all the line nos and,
55+
* the colors to be added on those lines
56+
*/
57+
function _getAllColorsAndLineNums(editor) {
58+
59+
const cm = editor._codeMirror;
60+
const nLen = cm.lineCount();
61+
const aColors = [];
62+
63+
// match colors and push into an array
64+
for (let i = 0; i < nLen; i++) {
65+
let lineText = cm.getLine(i);
66+
67+
if ((lineText.indexOf('/*') !== -1) || (lineText.indexOf('*/') !== -1)) {
68+
continue;
69+
} else {
70+
let regx = /:[^;]*;/g;
71+
72+
lineText = lineText.match(regx);
73+
if (lineText) {
74+
let tempColors = lineText[0].match(COLOR_REGEX);
75+
// Support up to 4 colors
76+
if (tempColors && tempColors.length > 0) {
77+
let colors = tempColors.slice(0, 4);
78+
aColors.push({
79+
lineNumber: i,
80+
colorValues: colors
81+
});
82+
}
83+
}
84+
}
85+
}
86+
87+
return aColors;
88+
}
89+
4990
/**
5091
* Gets all the colors that are to be displayed
5192
*
@@ -61,47 +102,47 @@ define(function (require, exports, module) {
61102
const editor = EditorManager.getActiveEditor();
62103
if (editor) {
63104

64-
const cm = editor._codeMirror;
65-
const nLen = cm.lineCount();
66-
const aColors = [];
67-
68-
// match colors and push into an array
69-
for (let i = 0; i < nLen; i++) {
70-
let lineText = cm.getLine(i);
71-
72-
if ((lineText.indexOf('/*') !== -1) || (lineText.indexOf('*/') !== -1)) {
73-
continue;
74-
} else {
75-
let regx = /:[^;]*;/g;
76-
77-
lineText = lineText.match(regx);
78-
if (lineText) {
79-
let tempColors = lineText[0].match(COLOR_REGEX);
80-
// Support up to 4 colors
81-
if (tempColors && tempColors.length > 0) {
82-
let colors = tempColors.slice(0, 4);
83-
aColors.push({
84-
lineNumber: i,
85-
colorValues: colors
86-
});
87-
}
88-
}
89-
}
90-
}
91-
105+
const aColors = _getAllColorsAndLineNums(editor);
92106
showGutters(editor, aColors);
107+
93108
}
94109
}
95110

111+
112+
/**
113+
* To add the color marks on the gutter of all the active editors
114+
* This function is called when the user toggles the
115+
* CssColorPreview preference and set it to true
116+
*/
117+
function addColorMarksToAllEditors() {
118+
119+
const allActiveEditors = MainViewManager.getAllViewedEditors();
120+
121+
allActiveEditors.forEach((activeEditor) => {
122+
const currEditor = activeEditor.editor;
123+
if(currEditor) {
124+
125+
const aColors = _getAllColorsAndLineNums(currEditor);
126+
showGutters(currEditor, aColors);
127+
128+
}
129+
});
130+
}
131+
96132
/**
97-
* To remove the color marks from the gutter
133+
* To remove the color marks from the gutter of all the active editors
98134
*/
99135
function removeColorMarks() {
100-
const editor = EditorManager.getActiveEditor();
101-
if (editor) {
102-
const cm = editor._codeMirror;
103-
cm.clearGutter(gutterName);
104-
}
136+
137+
const allActiveEditors = MainViewManager.getAllViewedEditors();
138+
139+
allActiveEditors.forEach((activeEditor) => {
140+
const currEditor = activeEditor.editor;
141+
if(currEditor) {
142+
const cm = currEditor._codeMirror;
143+
cm.clearGutter(gutterName);
144+
}
145+
});
105146
}
106147

107148
/**
@@ -225,9 +266,11 @@ define(function (require, exports, module) {
225266
const value = PreferencesManager.get(PREFERENCES_CSS_COLOR_PREVIEW);
226267
enabled = value;
227268
if (!value) {
269+
// to dynamically remove color to all active editors
228270
removeColorMarks();
229271
} else {
230-
showColorMarks();
272+
// to dynamically add color to all active editors
273+
addColorMarksToAllEditors();
231274
}
232275
}
233276

@@ -242,7 +285,8 @@ define(function (require, exports, module) {
242285
* Driver function, runs at the start of the program
243286
*/
244287
function init() {
245-
showColorMarks();
288+
// preferenceChanged calls 'showColorMarks' or 'removeColorMarks'
289+
preferenceChanged();
246290
registerHandlers();
247291
}
248292

0 commit comments

Comments
 (0)