-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
127 lines (109 loc) · 3.72 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
/*jslint indent: 2,maxerr: 50 */
/*global define, brackets, Mustache, localStorage */
define(function (require, exports, module) {
"use strict";
var CommandManager = brackets.getModule("command/CommandManager"),
Menus = brackets.getModule("command/Menus"),
DocumentManager = brackets.getModule("document/DocumentManager"),
Strings = brackets.getModule("strings"),
Dialogs = brackets.getModule("widgets/Dialogs"),
PrefsTemplate = require("text!preferences.html");
var preferenceKeyId = 'charlimit.preferences';
var defaultPreferences = {
charLimitCols: 80,
charLimitColor: '#bbbbbb',
charLimitEnabled: true
};
function getStorage() {
var userData = localStorage.getItem(preferenceKeyId);
return userData ? JSON.parse(userData) : $.extend({}, defaultPreferences);
}
function getPreference(name) {
return getStorage()[name];
}
function saveStorage(storage) {
localStorage.setItem(preferenceKeyId, JSON.stringify(storage));
}
function setPreference(name, value) {
var storage = getStorage();
if (typeof name === 'object') {
for (var p in name) {
if (name.hasOwnProperty(p)) {
storage[p] = name[p];
} else {
storage[name] = value;
}
}
saveStorage(storage);
}
}
function charWidth() {
var o = $('<div>a</div>')
.css({'position': 'absolute',
'float': 'left',
'white-space': 'nowrap',
'visibility': 'hidden'})
.appendTo($('body'));
var w = o.width();
o.remove();
return w;
}
function getGutterWidth() {
var result = 15;
var max = 0;
$('.CodeMirror-gutters').each(function() {
if ($(this).width() > max) {
max = $(this).width();
}
});
result += max;
return result;
}
function drawCharLimit() {
$('#charlimit').remove();
if (getPreference('charLimitEnabled') && !!DocumentManager.getCurrentDocument()) {
var cols = getPreference('charLimitCols');
cols--;
var width = cols*charWidth() + getGutterWidth();
var editorHolder = $('#editor-holder');
editorHolder.append('<div id="charlimit" class="charlimit"></div>');
var charlimit = $('#charlimit');
charlimit.css('position', 'fixed');
charlimit.css('top', '0');
charlimit.css('height', '100%');
charlimit.css('width', width + 'px');
var color = getPreference('charLimitColor');
charlimit.css('border-right', 'thin solid ' + color);
charlimit.css('pointer-events', 'none');
}
}
function showCharLimitPreferences() {
var promise = Dialogs
.showModalDialogUsingTemplate(Mustache.render(PrefsTemplate, Strings))
.done(function (id) {
if (id === Dialogs.DIALOG_BTN_OK) {
setPreference({
charLimitCols: colsInput.val(),
charLimitColor: colorInput.val(),
charLimitEnabled: enabledInput[0].checked
});
drawCharLimit();
}
});
var dialog = $("#charlimit-settings");
var colsInput = dialog.find('#charlimit-cols');
var colorInput = dialog.find('#charlimit-color');
var enabledInput = dialog.find('#charlimit-enabled');
colsInput.val(getPreference('charLimitCols'));
colorInput.val(getPreference('charLimitColor'));
enabledInput[0].checked = !!getPreference('charLimitEnabled');
return promise;
}
$(DocumentManager).on('currentDocumentChange', drawCharLimit);
var CHARLIMIT_ID = "cezarywojcik.charlimitpref";
CommandManager.register("CharLimit Preferences", CHARLIMIT_ID,
showCharLimitPreferences);
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
menu.addMenuDivider();
menu.addMenuItem(CHARLIMIT_ID);
});