Skip to content

Commit f5248a5

Browse files
committed
feat: add CSSColorPreview feature to extension dir & fix event issues
1 parent 37cb11a commit f5248a5

File tree

6 files changed

+183
-12
lines changed

6 files changed

+183
-12
lines changed

package-lock.json

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-node/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.ico-cssColorPreview {
2+
border: solid 1px rgba(0,0,0,.2);
3+
display: inline-block;
4+
height: 7px;
5+
width: 7px;
6+
margin-left: 3px;
7+
vertical-align: middle;
8+
position: relative;
9+
z-index: 99999;
10+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* GNU AGPL-3.0 License
3+
*
4+
* Copyright (c) 2021 - present core.ai . All rights reserved.
5+
* Original work Copyright (c) 2013 - 2021 Adobe Systems Incorporated. All rights reserved.
6+
*
7+
* This program is free software: you can redistribute it and/or modify it
8+
* under the terms of the GNU Affero General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
15+
* for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
19+
*
20+
*/
21+
22+
/* Displays a color preview in the gutter for any file containing color values */
23+
24+
define(function (require, exports, module) {
25+
26+
// Brackets modules.
27+
var _ = brackets.getModule("thirdparty/lodash"),
28+
EditorManager = brackets.getModule('editor/EditorManager'),
29+
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
30+
ColorUtils = brackets.getModule('utils/ColorUtils'),
31+
AppInit = brackets.getModule("utils/AppInit"),
32+
33+
// Extension variables.
34+
COLOR_REGEX = ColorUtils.COLOR_REGEX, // used to match color
35+
gutterName = "CodeMirror-colorGutter";
36+
37+
ExtensionUtils.loadStyleSheet(module, "main.css");
38+
39+
var CssColorPreview = {
40+
41+
// Get editor
42+
getEditor: function() {
43+
return EditorManager.getActiveEditor();
44+
},
45+
46+
// show color preview
47+
showColorMarks: function() {
48+
49+
var editor = CssColorPreview.getEditor();
50+
if(editor) {
51+
52+
var cm = editor._codeMirror;
53+
var nLen = cm.lineCount();
54+
var aColors = [];
55+
56+
// match colors and push into an array
57+
for(var i = 0; i < nLen; i++) {
58+
var lineText = cm.getLine(i);
59+
60+
if( (lineText.indexOf('/*')!==-1) || (lineText.indexOf('*/')!==-1) ){
61+
continue;
62+
} else {
63+
var regx = /:.*?;/g;
64+
lineText = lineText.match(regx);
65+
if(lineText){
66+
var tempItem = lineText[0].match(COLOR_REGEX);
67+
// todo current support one color to show only
68+
if(tempItem){
69+
var tempColor = tempItem[0];
70+
aColors.push({
71+
lineNumber: i,
72+
colorValue: tempColor
73+
});
74+
}
75+
}
76+
}
77+
}
78+
79+
CssColorPreview.showGutters(editor, aColors);
80+
}
81+
},
82+
83+
onChanged: function () {
84+
CssColorPreview.showColorMarks();
85+
},
86+
87+
init: function() {
88+
CssColorPreview.showColorMarks();
89+
CssColorPreview.registerHandlers();
90+
},
91+
92+
registerHandlers: function () {
93+
// Remove previous listeners to avoid multiple binding issue
94+
EditorManager.off("activeEditorChange", CssColorPreview.onChanged);
95+
96+
// Add listener for all editor changes
97+
EditorManager.on("activeEditorChange", function(event, newEditor, oldEditor) {
98+
if (newEditor) {
99+
// Unbind the previous editor's change event if it exists
100+
if (oldEditor) {
101+
var oldCM = oldEditor._codeMirror;
102+
if (oldCM) {
103+
oldCM.off("change", CssColorPreview.onChanged);
104+
}
105+
}
106+
107+
// Bind change event to the new editor
108+
var cm = newEditor._codeMirror;
109+
if (cm) {
110+
cm.on("change", CssColorPreview.onChanged);
111+
}
112+
113+
CssColorPreview.showColorMarks();
114+
}
115+
});
116+
},
117+
118+
initGutter: function(editor) {
119+
120+
var cm = editor._codeMirror;
121+
var gutters = cm.getOption("gutters").slice(0);
122+
var str = gutters.join('');
123+
if (str.indexOf(gutterName) === -1) {
124+
gutters.unshift(gutterName);
125+
cm.setOption("gutters", gutters);
126+
}
127+
},
128+
129+
showGutters: function(editor, _results) {
130+
131+
if(editor){
132+
CssColorPreview.initGutter(editor);
133+
var cm = editor._codeMirror;
134+
cm.clearGutter(gutterName); // clear color markers
135+
cm.colorGutters = _.sortBy(_results, "lineNumber");
136+
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+
});
143+
144+
}
145+
}
146+
};
147+
148+
// init after appReady
149+
AppInit.appReady(function() {
150+
setTimeout(CssColorPreview.init, 1000);
151+
});
152+
153+
});
154+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "CSSColorPreview",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
}
6+
}

src/extensions/default/DefaultExtensions.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"CodeFolding",
77
"CSSAtRuleCodeHints",
88
"CSSCodeHints",
9+
"CSSColorPreview",
910
"CSSPseudoSelectorHints",
1011
"DebugCommands",
1112
"HandlebarsSupport",

0 commit comments

Comments
 (0)