Skip to content

Commit 122404a

Browse files
committed
chore: add option to toggle this feature in preferences
1 parent f5248a5 commit 122404a

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

src/extensions/default/CSSColorPreview/main.js

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,26 @@ define(function (require, exports, module) {
2525

2626
// Brackets modules.
2727
var _ = brackets.getModule("thirdparty/lodash"),
28-
EditorManager = brackets.getModule('editor/EditorManager'),
29-
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
30-
ColorUtils = brackets.getModule('utils/ColorUtils'),
28+
EditorManager = brackets.getModule('editor/EditorManager'),
29+
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
30+
ColorUtils = brackets.getModule('utils/ColorUtils'),
3131
AppInit = brackets.getModule("utils/AppInit"),
32+
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
33+
Strings = brackets.getModule("strings"),
3234

3335
// Extension variables.
3436
COLOR_REGEX = ColorUtils.COLOR_REGEX, // used to match color
3537
gutterName = "CodeMirror-colorGutter";
3638

3739
ExtensionUtils.loadStyleSheet(module, "main.css");
3840

41+
const PREFERENCES_CSS_COLOR_PREVIEW = "CSSColorPreview";
42+
let enabled = true;
43+
44+
PreferencesManager.definePreference(PREFERENCES_CSS_COLOR_PREVIEW, "boolean", enabled, {
45+
description: Strings.DESCRIPTION_CSS_COLOR_PREVIEW
46+
});
47+
3948
var CssColorPreview = {
4049

4150
// Get editor
@@ -45,6 +54,11 @@ define(function (require, exports, module) {
4554

4655
// show color preview
4756
showColorMarks: function() {
57+
// Check if the extension is enabled
58+
if (!enabled) {
59+
CssColorPreview.removeColorMarks();
60+
return;
61+
}
4862

4963
var editor = CssColorPreview.getEditor();
5064
if(editor) {
@@ -127,26 +141,48 @@ define(function (require, exports, module) {
127141
},
128142

129143
showGutters: function(editor, _results) {
130-
131-
if(editor){
144+
if(editor && enabled){
132145
CssColorPreview.initGutter(editor);
133146
var cm = editor._codeMirror;
134147
cm.clearGutter(gutterName); // clear color markers
135-
cm.colorGutters = _.sortBy(_results, "lineNumber");
136148

137-
cm.colorGutters.forEach(function (obj) {
138-
var $marker = $("<i>")
139-
.addClass("ico-cssColorPreview")
140-
.html("&nbsp;").css('background-color', obj.colorValue);
141-
cm.setGutterMarker(obj.lineNumber, gutterName, $marker[0]);
142-
});
149+
// Only add markers if enabled
150+
if (enabled) {
151+
cm.colorGutters = _.sortBy(_results, "lineNumber");
152+
153+
cm.colorGutters.forEach(function (obj) {
154+
var $marker = $("<i>")
155+
.addClass("ico-cssColorPreview")
156+
.html("&nbsp;").css('background-color', obj.colorValue);
157+
cm.setGutterMarker(obj.lineNumber, gutterName, $marker[0]);
158+
});
159+
}
160+
}
161+
},
143162

163+
// Method to remove colors when disabled
164+
removeColorMarks: function() {
165+
var editor = CssColorPreview.getEditor();
166+
if (editor) {
167+
var cm = editor._codeMirror;
168+
cm.clearGutter(gutterName);
144169
}
145170
}
146171
};
147172

173+
function preferenceChanged() {
174+
let value = PreferencesManager.get(PREFERENCES_CSS_COLOR_PREVIEW);
175+
enabled = value;
176+
if (!value) {
177+
CssColorPreview.removeColorMarks();
178+
} else {
179+
CssColorPreview.showColorMarks();
180+
}
181+
}
182+
148183
// init after appReady
149184
AppInit.appReady(function() {
185+
PreferencesManager.on("change", PREFERENCES_CSS_COLOR_PREVIEW, preferenceChanged);
150186
setTimeout(CssColorPreview.init, 1000);
151187
});
152188

src/nls/root/strings.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1250,5 +1250,6 @@ define({
12501250
"BEAUTIFY_OPTION_PRINT_TRAILING_COMMAS": "Print trailing commas wherever possible in multi-line comma-separated syntactic structures",
12511251
// indent guides extension
12521252
"DESCRIPTION_INDENT_GUIDES_ENABLED": "true to show indent guide lines, else false.",
1253-
"DESCRIPTION_HIDE_FIRST": "true to show the first Indent Guide line else false."
1253+
"DESCRIPTION_HIDE_FIRST": "true to show the first Indent Guide line else false.",
1254+
"DESCRIPTION_CSS_COLOR_PREVIEW": "true to display color previews in the gutter, else false."
12541255
});

0 commit comments

Comments
 (0)