-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
55 lines (44 loc) · 1.79 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
define(function (require, exports, module) {
'use strict';
var CommandManager = brackets.getModule('command/CommandManager'),
EditorManager = brackets.getModule('editor/EditorManager'),
PreferencesManager = brackets.getModule('preferences/PreferencesManager'),
Menus = brackets.getModule('command/Menus');
function reval(s) {
try {
return window.eval('(function(){with(Math){ return ' + s + '; }})()');
}
catch (e) {
return undefined;
}
}
var MY_COMMAND_ID = 'inline-calc',
precision,
_preferences = PreferencesManager.getExtensionPrefs(MY_COMMAND_ID),
reloadSettings = function () {
precision = Math.pow(10, Math.min(_preferences.get('precision'), 15));
}
_preferences.definePreference("precision", "number", 2).on('change', reloadSettings);
reloadSettings();
function calcselectedtext() {
var editor = EditorManager.getCurrentFullEditor(),
currentSelection,
originalText,
processedText;
if (editor.hasSelection()) {
currentSelection = editor.getSelection();
originalText = editor.getSelectedText().trim();
if (originalText) {
var res = reval(originalText);
if (res !== undefined) {
if (typeof res == 'number') {
res = Math.round(res * precision) / precision;
}
editor.document.replaceRange(res.toString(), currentSelection.start, currentSelection.end);
}
}
}
}
CommandManager.register('Inline Calc', MY_COMMAND_ID, calcselectedtext);
Menus.getMenu(Menus.AppMenuBar.EDIT_MENU).addMenuItem(MY_COMMAND_ID, 'Alt-C');
});