-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.js
137 lines (115 loc) · 4.63 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4 */
/*global define, $, brackets, window */
define(function (require, exports, module) {
"use strict";
var CommandManager = brackets.getModule("command/CommandManager"),
Menus = brackets.getModule("command/Menus"),
EditorManager = brackets.getModule("editor/EditorManager"),
Editor = brackets.getModule("editor/Editor").Editor,
AppInit = brackets.getModule("utils/AppInit"),
NodeConnection = brackets.getModule("utils/NodeConnection"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
DocumentManager = brackets.getModule("document/DocumentManager"),
COMMAND_ID = "csscomb.csscomb",
nodeConnection,
sortedCSS,
start,
end,
isSelection = false;
// Helper function that chains a series of promise-returning
// functions together via their done callbacks.
function chain() {
var functions = Array.prototype.slice.call(arguments, 0);
if (functions.length > 0) {
var firstFunction = functions.shift();
var firstPromise = firstFunction.call();
firstPromise.done(function () {
chain.apply(null, functions);
});
}
}
// Helper function to connect to node
function connect() {
var connectionPromise = nodeConnection.connect(true);
connectionPromise.fail(function () {
console.error("failed to connect to node");
});
return connectionPromise;
}
// Helper function that loads our domain into the node server
function loadDomain() {
var path = ExtensionUtils.getModulePath(module, "node/CSScombDomain"),
loadPromise = nodeConnection.loadDomains([path], true);
loadPromise.fail(function (err) {
console.log("failed to load domain: ", err);
});
loadPromise.done(function () {
console.log("csscomb domain successfully loaded");
});
return loadPromise;
}
function replaceCSS(css) {
var editor = EditorManager.getCurrentFullEditor(),
doc = DocumentManager.getCurrentDocument(),
cursor = editor.getCursorPos(),
scroll = editor.getScrollPos();
doc.batchOperation(function () {
if (isSelection) {
doc.replaceRange(css, start, end);
} else {
doc.setText(css);
}
editor.setCursorPos(cursor);
editor.setScrollPos(scroll.x, scroll.y);
});
console.log('csscomb replace finished');
}
function sortCSS(cssToSort) {
var config = JSON.parse(require("text!csscomb.json")),
doc = DocumentManager.getCurrentDocument(),
ext = doc.file.fullPath.split('.').pop().toLowerCase(),
supportedExts = ['css', 'scss', 'sass', 'less', 'styl'],
sortPromise,
data;
if (supportedExts.indexOf(ext) === -1) {
console.log('Unsupported extension', ext);
ext = 'css';
}
data = { css: cssToSort, ext: ext, config: config };
sortPromise = nodeConnection.domains.csscomb.runCommand(data, function (css) {
console.log("css: ", css);
sortPromise.resolve(css);
});
sortPromise.fail(function (err) {
console.error("failed to run csscomb domain", err);
});
sortPromise.done(function (css) {
console.log("csscomb domain work is done", css);
replaceCSS(css);
});
return sortPromise;
}
AppInit.appReady(function () {
nodeConnection = new NodeConnection();
chain(connect, loadDomain);
});
function csscombSort() {
var editor = EditorManager.getCurrentFullEditor(),
selectedText = editor.getSelectedText(),
selection = editor.getSelection(),
cssToSort;
start = selection.start;
end = selection.end;
if (selectedText.length > 0) {
isSelection = true;
cssToSort = selectedText;
} else {
cssToSort = DocumentManager.getCurrentDocument().getText();
}
sortCSS(cssToSort);
}
CommandManager.register("Sort with CSScomb", COMMAND_ID, csscombSort);
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
menu.addMenuItem(COMMAND_ID, [{key: "Ctrl-Shift-С", platform: "win"},
{key: "Ctrl-Shift-С", platform: "mac"}]);
});