-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
70 lines (52 loc) · 1.69 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
define(function (require, exports, module) {
"use strict";
var DocumentManager = brackets.getModule('document/DocumentManager');
var CommandManager = brackets.getModule("command/CommandManager");
var ExtensionUtils = brackets.getModule('utils/ExtensionUtils');
var EditorManager = brackets.getModule("editor/EditorManager");
var Menus = brackets.getModule("command/Menus");
var PREFIX = 'brackets-polisher';
var COMMAND_ID = PREFIX + '.format';
var KEY_BINDINGS = [
{
key: 'Ctrl-Shift-F',
platform: 'win'
}, {
key: 'Ctrl-Alt-F',
platform: 'win'
}, {
key: 'Cmd-Shift-F',
platform: 'mac'
}, {
key: 'Ctrl-Alt-F'
}
];
ExtensionUtils.loadStyleSheet(module, 'styles/styles.css');
function format() {
var editor = EditorManager.getCurrentFullEditor();
if (editor.hasSelection()) {
var selection = editor.getSelection();
var htmlUnformatted = editor.getSelectedText();
var htmlFormatted = '';
var document = DocumentManager.getCurrentDocument();
var rgx = /\s{2,}/gi;
htmlFormatted = htmlUnformatted.replace(rgx, ' ');
document.replaceRange(htmlFormatted, selection.start, selection.end);
} else {
//console.log('no selection');
}
}
CommandManager.register('Polish up!', COMMAND_ID, format);
$(document.createElement('a'))
.attr('id', 'polisher-icon')
.attr('href', '#')
.attr('title', 'Polish up!')
.on('click', function () {
format();
})
.appendTo($('#main-toolbar .buttons'));
var editMenu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
editMenu.addMenuDivider();
editMenu.addMenuItem(COMMAND_ID, KEY_BINDINGS);
Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU).addMenuItem(COMMAND_ID);
});