This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.js
executable file
·355 lines (334 loc) · 13 KB
/
init.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Copyright (c) Codiad & Andr3as, distributed
* as-is and without warranty under the MIT License.
* See http://opensource.org/licenses/MIT for more information.
* This information must remain intact.
*/
(function(global, $){
var codiad = global.codiad,
scripts = document.getElementsByTagName('script'),
path = scripts[scripts.length-1].src.split('?')[0],
curpath = path.split('/').slice(0, -1).join('/')+'/';
$(function() {
codiad.Beautify.init();
});
codiad.Beautify = {
path: curpath,
beautifyPhp: null,
lines: 0,
row: 0,
settings: {
js: false, json: false, html: false, css: false, auto: false
},
files: ["html", "htm", "js", "json", "css", "php"],
init: function() {
var _this = this;
//Load libs
$.getScript(this.path+"libs/beautify-css.js");
$.getScript(this.path+"libs/beautify-html.js");
$.getScript(this.path+"libs/beautify.js");
$.getScript(this.path+"libs/ext-beautify.js", function() {
_this.beautifyPhp = ace.require("ace/ext/beautify");
});
//Load settings
this.load();
//Set subscriptions
amplify.subscribe('active.onOpen', function(path){
if(codiad.editor.getActive() === null)
return;
var manager = codiad.editor.getActive().commands;
manager.addCommand({
name: "Beautify",
bindKey: {win: "Ctrl-Alt-B", mac: "Command-Alt-B"},
exec: function(){
_this.beautify();
}
});
});
amplify.subscribe('active.onSave', function(path){
path = path || codiad.active.getPath();
var ext = _this.getExtension(path);
if (_this.files.indexOf(ext) != -1) {
if (_this.check(path)) {
var content = codiad.editor.getContent();
_this.lines = _this.getLines();
_this.row = codiad.editor.getActive().getCursorPosition().row;
content = _this.beautifyContent(path, content);
if (typeof(content) !== 'string') {
return true;
}
codiad.editor.setContent(content);
_this.guessCursorPosition();
}
}
});
amplify.subscribe('context-menu.onShow', function(obj){
var ext = _this.getExtension(obj.path);
if (_this.files.indexOf(ext) != -1 && ext !== "php") {
$('#context-menu').append('<hr class="file-only beautify">');
$('#context-menu').append('<a class="file-only beautify" onclick="codiad.Beautify.contextMenu($(\'#context-menu\').attr(\'data-path\'));"><span class="icon-brush"></span>Beautify</a>');
}
});
amplify.subscribe('context-menu.onHide', function(){
$('.beautify').remove();
});
amplify.subscribe('settings.dialog.save', function(){
if ($('#beautify_form').length > 0) {
codiad.Beautify.save();
}
});
},
//////////////////////////////////////////////////////////
//
// Show settings dialog
//
//////////////////////////////////////////////////////////
showDialog: function() {
codiad.modal.load(200, this.path+"dialog.php");
},
//////////////////////////////////////////////////////////
//
// Save new settings
//
//////////////////////////////////////////////////////////
save: function() {
var _this = this;
this.checkSettings("js");
this.checkSettings("json");
this.checkSettings("html");
this.checkSettings("css");
this.checkSettings("php");
$.post(this.path+"controller.php?action=save", {settings: JSON.stringify(this.settings)}, function(data){
var json = JSON.parse(data);
if (json.status == "error") {
codiad.message.error(json.message);
} else {
codiad.message.success(json.message);
}
_this.load();
});
},
//////////////////////////////////////////////////////////
//
// Load existing settings
//
//////////////////////////////////////////////////////////
load: function() {
var _this = this;
$.getJSON(this.path+"controller.php?action=load", function(json){
_this.settings = json;
});
},
//////////////////////////////////////////////////////////
//
// Display settings in the dialog
//
//////////////////////////////////////////////////////////
get: function() {
this.setSettings("js");
this.setSettings("json");
this.setSettings("html");
this.setSettings("css");
this.setSettings("php");
},
//////////////////////////////////////////////////////////
//
// Get number of lines of current document
//
// Parameters
//
// content - {string} - (optional) Content of current document
//
//////////////////////////////////////////////////////////
getLines: function(content) {
content = content || codiad.editor.getContent();
return (content.match(/\n/g) || []).length + 1;
},
//////////////////////////////////////////////////////////
//
// Guess the cursor position after beautifying content
//
//////////////////////////////////////////////////////////
guessCursorPosition: function() {
if (localStorage.getItem("codiad.plugin.beautify.guessCursorPosition") == "true") {
var newLines= this.getLines();
var factor = newLines / this.lines;
var newRow = Math.floor(factor * this.row);
codiad.editor.getActive().clearSelection();
codiad.editor.getActive().moveCursorToPosition({"row":newRow, "column":0});
}
},
//////////////////////////////////////////////////////////
//
// Set checkbox of dialog given by extension
//
// Parameters
//
// ext - {string} - Extension of id of checkbox
//
//////////////////////////////////////////////////////////
setSettings: function(ext) {
if (this.settings.auto[ext] === true) {
$('#beautify_'+ext).attr("checked", "checked");
}
},
//////////////////////////////////////////////////////////
//
// Check checkboxes and store status
//
// Parameters
//
// ext - {string} - Extension of id of checkbox
//
//////////////////////////////////////////////////////////
checkSettings: function(ext) {
if ($('#beautify_'+ext).attr("checked") == "checked") {
this.settings.auto[ext] = true;
} else {
this.settings.auto[ext] = false;
}
},
//////////////////////////////////////////////////////////
//
// Function to handle context menu click
//
// Parameters
//
// path - {string} - File path of the clicked file
//
//////////////////////////////////////////////////////////
contextMenu: function(path) {
var _this = this;
$.get(this.path+"controller.php?action=getContent&path="+path, function(data){
var content = _this.beautifyContent(path, data);
$.post(_this.path+"controller.php?action=saveContent&path="+path, {"content": content}, function(result){
var json = JSON.parse(result);
if (json.status == "error") {
codiad.message.error(json.message);
} else {
codiad.message.success(json.message);
}
});
});
},
//////////////////////////////////////////////////////////
//
// Beautify content
//
// Parameters
//
// path - {string} - File path
// content - {string} - Content to beautify
// settings - {object} - Settings for beautify
//
//////////////////////////////////////////////////////////
beautifyContent: function(path, content, settings) {
this.checkBeautifySettings();
if (typeof(settings) == 'undefined') {
settings = this.settings.beautify;
}
var ext = this.getExtension(path);
if (ext == "html" || ext == "htm") {
return html_beautify(content, settings);
} else if (ext == "css") {
return css_beautify(content, settings);
} else if (ext == "js" || ext == "json") {
return js_beautify(content, settings);
} else if (ext == "php") {
this.beautifyPhp.beautify(codiad.editor.getActive().getSession());
return true;
} else {
return false;
}
},
//////////////////////////////////////////////////////////
//
// Beautify command to handle hotkey
//
//////////////////////////////////////////////////////////
beautify: function() {
var _this = this;
var settings = this.settings.beautify;
var path = codiad.active.getPath();
var editor = codiad.editor.getActive();
var session = editor.getSession();
var selText = codiad.editor.getSelectedText();
var range = editor.selection.getRange();
var fn = function(range, text) {
if (typeof(text) == 'undefined') {
settings.indent_level = "keep";
range.start.column = 0;
text = session.getTextRange(range);
}
text = _this.beautifyContent(path, text, settings);
if (typeof(text) == 'string') {
session.replace(range, text);
}
};
if (selText !== "") {
if (editor.selection.inMultiSelectMode) {
var multiRanges = editor.selection.getAllRanges();
for (var i = 0; i < multiRanges.length; i++) {
fn(multiRanges[i]);
}
} else {
//Single selection
fn(range);
}
} else {
this.row = codiad.editor.getActive().getCursorPosition().row;
this.lines = this.getLines();
var content = codiad.editor.getContent();
range = editor.selectAll() || editor.selection.getRange();
fn(range, content);
this.guessCursorPosition();
}
},
//////////////////////////////////////////////////////////
//
// Get settings for given file path
//
// Parameters
//
// path - {string} - File path
//
//////////////////////////////////////////////////////////
check: function(path) {
var ext = this.getExtension(path);
if (ext == "htm") {
ext = "html";
}
return this.settings.auto[ext];
},
//////////////////////////////////////////////////////////
//
// Check settings for beautify
//
//////////////////////////////////////////////////////////
checkBeautifySettings: function() {
var char = "";
var tab = 1;
if (codiad.editor.settings.softTabs) {
char = " ";
tab = 4;
} else {
char = "\t";
tab = 1;
}
this.settings.beautify.indent_char = char;
this.settings.beautify.indent_size = tab;
},
//////////////////////////////////////////////////////////
//
// Get extension of file
//
// Parameters
//
// path - {string} - File path
//
//////////////////////////////////////////////////////////
getExtension: function(path) {
return path.substring(path.lastIndexOf(".")+1);
}
};
})(this, jQuery);