-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
144 lines (121 loc) · 5.07 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
138
139
140
141
142
143
144
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, brackets, window, $, Mustache, navigator */
define(function( require, exports, module ) {
"use strict";
// Brackets Modules
var Commands = brackets.getModule("command/Commands");
var CommandManager = brackets.getModule("command/CommandManager");
var Menus = brackets.getModule("command/Menus");
var ProjectManager = brackets.getModule("project/ProjectManager");
var EditorManager = brackets.getModule("editor/EditorManager");
var DocumentManager = brackets.getModule("document/DocumentManager");
var StatusBar = brackets.getModule("widgets/StatusBar");
var ExtensionUtils = brackets.getModule("utils/ExtensionUtils");
ExtensionUtils.loadStyleSheet(module, "css/style.css");
// Strings
var Strings = require("strings");
// var BracketsStrings = brackets.getModule("strings");
// NODE BRIDGE
var nodeBridge = require("node/nodebridge");
// CONSTS
var CSS_COMP_BRACKETS_RUN = 'csscomb.brackets.run';
var CSS_COMP_BRACKETS_RUN_INLINE = 'csscomb.brackets.run.inline';
var INDICATOR_ID = "csscomb-status-validation";
// MENUS
var editMenu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
var workingSetMenu = Menus.getContextMenu(Menus.ContextMenuIds.WORKING_SET_MENU);
var projectMenu = Menus.getContextMenu(Menus.ContextMenuIds.PROJECT_MENU);
// var inlineEditorMenu = Menus.getContextMenu(Menus.ContextMenuIds.INLINE_EDITOR_MENU);
var editorMenu = Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU);
/**
* Register menu entries
*/
function _registerMenuEntries() {
// Run on full path or file
CommandManager.register(Strings.CSS_COMP, CSS_COMP_BRACKETS_RUN, processPath);
// register menu entry for secondary click on sidebar
workingSetMenu.addMenuDivider();
workingSetMenu.addMenuItem(CSS_COMP_BRACKETS_RUN);
projectMenu.addMenuDivider();
projectMenu.addMenuItem(CSS_COMP_BRACKETS_RUN);
// Check first on selected text - if none run on full path or file
// register menu entry for inline editing in editor
CommandManager.register(Strings.CSS_COMP, CSS_COMP_BRACKETS_RUN_INLINE, processSelectedText);
editorMenu.addMenuDivider();
editorMenu.addMenuItem(CSS_COMP_BRACKETS_RUN_INLINE);
// register Edit -> CSScomb
editMenu.addMenuDivider();
editMenu.addMenuItem(CSS_COMP_BRACKETS_RUN_INLINE, "Cmd-alt-c", Menus.AFTER);
}
/**
* Call the CSScomp node script on a path or file
*/
function processPath() {
var dfd = $.Deferred();
var file = ProjectManager.getSelectedItem();
nodeBridge.processPath(file._path, function( err, resp ) {
if( err ) {
StatusBar.updateIndicator(INDICATOR_ID, true, "inspection-errors", err);
dfd.reject(err);
return;
}
StatusBar.updateIndicator(INDICATOR_ID, true, "inspection-valid", Strings.UPDATED + ' ' + file._path);
dfd.resolve(resp);
});
return dfd.promise();
}
/**
* call CSScomb node processString with the given text
* @param text
* @returns {*}
*/
function processString( text ) {
var dfd = $.Deferred();
nodeBridge.processString(text, function( err, resp ) {
if( err ) {
StatusBar.updateIndicator(INDICATOR_ID, true, "inspection-errors", err);
dfd.reject(err);
return;
}
StatusBar.updateIndicator(INDICATOR_ID, true, "inspection-valid", Strings.UPDATED + ' ' + resp);
dfd.resolve(resp);
});
return dfd.promise();
}
/**
* Get the current selected text and CSScomb it. If no text is selected process the file or folder
*/
function processSelectedText() {
var editor = EditorManager.getCurrentFullEditor();
var selectedText = editor.getSelectedText();
// get the current document
var doc = DocumentManager.getCurrentDocument();
// get the current text selection
var selection = editor.getSelection();
// if there is a text selection
if( selectedText.length > 0 ) {
// try to CSScomb it
processString(selectedText).then(function( text ) {
doc.replaceRange(text, selection.start, selection.end);
});
} else {
// otherwise try to process the current file or folder
processPath();
}
}
/**
* Setup plugin
* @private
*/
function _init() {
_registerMenuEntries();
var statusIconHtml = Mustache.render("<div id=\"csscomb-status-validation\"> </div>", Strings);
StatusBar.addIndicator(INDICATOR_ID, $(statusIconHtml), true, "", "CSScomb", "status-indent");
}
// INIT
_init();
// API
exports.processPath = processPath;
exports.processSelectedText = processSelectedText;
//brackets.app.showDeveloperTools();
});