This repository has been archived by the owner on Mar 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 187
/
main.js
268 lines (230 loc) · 7.44 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
define(function(require, exports, module) {
var fs = require('./brackets-fs');
var preferences = require('./preferences');
var editor = require('./editor');
var path = require('./path');
var prompt = require('./prompt');
var interactive = require('./interactive');
var emmet = require('emmet/emmet');
var resources = require('emmet/assets/resources');
var actions = require('emmet/action/main');
var keymap = require('text!keymap.json');
var snippets = require('text!emmet/snippets.json');
var ciu = require('text!emmet/caniuse.json');
var CommandManager = brackets.getModule('command/CommandManager');
var KeyBindingManager = brackets.getModule('command/KeyBindingManager');
var Menus = brackets.getModule('command/Menus');
var EditorManager = brackets.getModule('editor/EditorManager');
var Dialogs = brackets.getModule('widgets/Dialogs');
var FileSystem = brackets.getModule('filesystem/FileSystem');
var ExtensionUtils = brackets.getModule('utils/ExtensionUtils');
var AppInit = brackets.getModule('utils/AppInit');
var skippedActions = ['update_image_size', 'encode_decode_data_url'];
// actions that should be performed in single selection mode
var singleSelectionActions = [
'prev_edit_point', 'next_edit_point', 'merge_lines',
'reflect_css_value', 'select_next_item', 'select_previous_item',
'wrap_with_abbreviation', 'update_tag', 'insert_formatted_line_break_only'
];
var isEnabled = true;
var lineBreakSyntaxes = ['html', 'xml', 'xsl'];
/**
* Emmet action decorator: creates a command function
* for CodeMirror and executes Emmet action as single
* undo command
* @param {Object} action Action to perform
* @return {Function}
*/
function actionDecorator(action) {
return function() {
var df = new $.Deferred();
var bracketsEditor = EditorManager.getFocusedEditor();
if (!bracketsEditor) {
return df.reject();
}
editor.setup(bracketsEditor);
bracketsEditor.document.batchOperation(function() {
runAction(action, df);
});
return df.resolve().promise();
};
}
/**
* Same as `actionDecorator()` but executes action
* with multiple selections
* @param {Object} action Action to perform
* @return {Function}
*/
function multiSelectionActionDecorator(action) {
return function() {
var df = new $.Deferred();
var bracketsEditor = EditorManager.getFocusedEditor();
if (!bracketsEditor) {
return df.reject();
}
editor.setup(bracketsEditor);
bracketsEditor.document.batchOperation(function() {
editor.exec(function() {
runAction(action, df);
});
editor.applyBatchedSelections();
});
return df.resolve().promise();
};
}
function runAction(action, df) {
if (!isEnabled) {
return df.reject().promise();
}
// do not handle Tab key for unknown syntaxes
if (action == 'expand_abbreviation_with_tab') {
var syntax = editor.getSyntax();
var activeEditor = editor.editor;
// do not allow tab expander in JS/JSX since it breakes native
// snippets and indentation. Hardcode this exception for now
if (syntax === 'jsx' || !preferences.getPreference('tab') || !resources.hasSyntax(syntax)) {
return df.reject();
}
// do not expand abbreviation if there’s a selection
if (activeEditor.hasSelection()) {
if (activeEditor._handleTabKey) {
activeEditor._handleTabKey();
}
return df.resolve();
}
}
if (action == 'insert_formatted_line_break') {
var activeEditor = editor.editor;
var allowAction = !activeEditor.hasSelection() && ~lineBreakSyntaxes.indexOf(editor.getSyntax());
if (!allowAction) {
// handle Enter key for limited syntaxes only
return df.reject();
}
}
return emmet.run(action, editor);
}
function loadExtensions(callback) {
var extPath = preferences.getPreference('extPath');
if (extPath) {
var dir;
try {
dir = FileSystem.getDirectoryForPath(extPath);
} catch (e) {
console.error('Error while loading extensions:', e);
callback();
}
dir.exists(function(err, exists) {
if (exists) {
emmet.resetUserData();
dir.getContents(function(err, files) {
if (err) {
return callback(err);
}
files = files.filter(function(entry) {
return !entry.isDirectory;
});
var complete = function() {
emmet.loadExtensions(files);
callback();
};
var waitForKeymap = false;
// if extensions path contains keymap file —
// use it as current Emmet keymap
files = files.map(function(file) {
if (path.basename(file.fullPath) == 'keymap.json') {
waitForKeymap = true;
file.read({encoding: 'utf8'}, function(content) {
keymap = content;
complete();
});
}
return file.fullPath;
});
if (!waitForKeymap) {
complete();
}
});
}
});
} else {
callback();
}
}
/**
* Register special, interactive versions of some Emmet actions
*/
function registerInteractiveCommands(menu) {
actions.add('interactive_expand_abbreviation', function() {}, 'Expand Abbreviation (interactive)');
['wrap_with_abbreviation', 'update_tag', 'interactive_expand_abbreviation'].forEach(function(cmd) {
var action = actions.get(cmd);
CommandManager.register(actionLabel(action, cmd), 'io.emmet.' + cmd, function() {
editor.setup(EditorManager.getFocusedEditor());
interactive.run(cmd, editor);
});
});
}
function actionLabel(action, fallback) {
if (action && action.options.label) {
return action.options.label.split('/').pop().replace(/\\/g, '/');
}
return fallback.replace(/^[a-z]|_([a-z])/g, function(str, ch) {
return str.toUpperCase().replace('_', ' ');
});
}
function init() {
try {
if (typeof keymap == 'string') {
keymap = JSON.parse(keymap);
}
} catch(e) {
console.error(e);
}
emmet.loadSystemSnippets(snippets);
emmet.loadCIU(ciu);
var menu = Menus.addMenu('Emmet', 'io.emmet.EmmetMainMenu');
registerInteractiveCommands(menu);
// register all commands
actions.getList().forEach(function(action) {
if (~skippedActions.indexOf(action.name)) {
return;
}
var id = 'io.emmet.' + action.name;
if (!CommandManager.get(id)) {
// regiester new command only if wasn’t defined previously
var cmd = ~singleSelectionActions.indexOf(action.name)
? actionDecorator(action.name)
: multiSelectionActionDecorator(action.name);
CommandManager.register(actionLabel(action), id, cmd);
}
var shortcut = keymap[action.name];
if (!action.options.hidden) {
menu.addMenuItem(id, shortcut);
} else if (shortcut) {
KeyBindingManager.addBinding(id, shortcut);
}
});
menu.addMenuDivider();
// Allow enable and disable Emmet
var cmdEnable = CommandManager.register('Enable Emmet', 'io.emmet.enabled', function() {
this.setChecked(!this.getChecked());
});
cmdEnable.on('checkedStateChange', function() {
isEnabled = cmdEnable.getChecked();
});
menu.addMenuItem(cmdEnable);
cmdEnable.setChecked(isEnabled);
// Add Preferences
var cmdPreferences = CommandManager.register('Preferences...', 'io.emmet.preferences', function() {
preferences.showPreferencesDialog().done(function(id) {
if (id === Dialogs.DIALOG_BTN_OK) {
loadExtensions();
}
});
});
menu.addMenuItem(cmdPreferences);
}
AppInit.appReady(function() {
ExtensionUtils.loadStyleSheet(module, 'ui/style.css');
loadExtensions(init);
});
});