This repository has been archived by the owner on May 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.js
385 lines (330 loc) · 12.8 KB
/
demo.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Skywriter.
*
* The Initial Developer of the Original Code is
* Mozilla.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Fabian Jakobs <fabian AT ajax DOT org>
* Kevin Dangoor ([email protected])
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
exports.launch = function(env) {
var event = require("pilot/event");
var Editor = require("ace/editor").Editor;
var Renderer = require("ace/virtual_renderer").VirtualRenderer;
var theme = require("ace/theme/textmate");
var EditSession = require("ace/edit_session").EditSession;
var JavaScriptMode = require("ace/mode/javascript").Mode;
var CssMode = require("ace/mode/css").Mode;
var HtmlMode = require("ace/mode/html").Mode;
var XmlMode = require("ace/mode/xml").Mode;
var PythonMode = require("ace/mode/python").Mode;
var PhpMode = require("ace/mode/php").Mode;
var JavaMode = require("ace/mode/java").Mode;
var RubyMode = require("ace/mode/ruby").Mode;
var CCPPMode = require("ace/mode/c_cpp").Mode;
var CoffeeMode = require("ace/mode/coffee").Mode;
var PerlMode = require("ace/mode/perl").Mode;
var TextMode = require("ace/mode/text").Mode;
var UndoManager = require("ace/undomanager").UndoManager;
var vim = require("ace/keyboard/keybinding/vim").Vim;
var emacs = require("ace/keyboard/keybinding/emacs").Emacs;
var HashHandler = require("ace/keyboard/hash_handler").HashHandler;
var keybindings = {
// Null = use "default" keymapping
ace: null,
vim: vim,
emacs: emacs
}
var docs = {};
// Make the lorem ipsum text a little bit longer.
var loreIpsum = document.getElementById("plaintext").innerHTML;
for (var i = 0; i < 5; i++) {
loreIpsum += loreIpsum;
}
docs.plain = new EditSession(loreIpsum);
docs.plain.setUseWrapMode(true);
docs.plain.setWrapLimitRange(80, 80)
docs.plain.setMode(new TextMode());
docs.plain.setUndoManager(new UndoManager());
docs.js = new EditSession(document.getElementById("jstext").innerHTML);
docs.js.setMode(new JavaScriptMode());
docs.js.setUndoManager(new UndoManager());
docs.css = new EditSession(document.getElementById("csstext").innerHTML);
docs.css.setMode(new CssMode());
docs.css.setUndoManager(new UndoManager());
docs.html = new EditSession(document.getElementById("htmltext").innerHTML);
docs.html.setMode(new HtmlMode());
docs.html.setUndoManager(new UndoManager());
docs.python = new EditSession(document.getElementById("pythontext").innerHTML);
docs.python.setMode(new PythonMode());
docs.python.setUndoManager(new UndoManager());
docs.php = new EditSession(document.getElementById("phptext").innerHTML);
docs.php.setMode(new PhpMode());
docs.php.setUndoManager(new UndoManager());
docs.java = new EditSession(document.getElementById("javatext").innerHTML);
docs.java.setMode(new JavaMode());
docs.java.setUndoManager(new UndoManager());
docs.ruby = new EditSession(document.getElementById("rubytext").innerHTML);
docs.ruby.setMode(new RubyMode());
docs.ruby.setUndoManager(new UndoManager());
docs.c_cpp = new EditSession(document.getElementById("cpptext").innerHTML);
docs.c_cpp.setMode(new CCPPMode());
docs.c_cpp.setUndoManager(new UndoManager());
docs.coffee = new EditSession(document.getElementById("coffeetext").innerHTML);
docs.coffee.setMode(new CoffeeMode());
docs.coffee.setUndoManager(new UndoManager());
docs.perl = new EditSession(document.getElementById("perltext").innerHTML);
docs.perl.setMode(new PerlMode());
docs.perl.setUndoManager(new UndoManager());
var container = document.getElementById("editor");
env.editor = new Editor(new Renderer(container, theme));
var modes = {
text: new TextMode(),
xml: new XmlMode(),
html: new HtmlMode(),
css: new CssMode(),
javascript: new JavaScriptMode(),
python: new PythonMode(),
php: new PhpMode(),
java: new JavaMode(),
ruby: new RubyMode(),
c_cpp: new CCPPMode(),
coffee: new CoffeeMode(),
perl: new PerlMode()
};
function getMode() {
return modes[modeEl.value];
}
var modeEl = document.getElementById("mode");
var wrapModeEl = document.getElementById("soft_wrap");
bindDropdown("doc", function(value) {
var doc = docs[value];
env.editor.setSession(doc);
var mode = doc.getMode();
if (mode instanceof JavaScriptMode) {
modeEl.value = "javascript";
}
else if (mode instanceof CssMode) {
modeEl.value = "css";
}
else if (mode instanceof HtmlMode) {
modeEl.value = "html";
}
else if (mode instanceof XmlMode) {
modeEl.value = "xml";
}
else if (mode instanceof PythonMode) {
modeEl.value = "python";
}
else if (mode instanceof PhpMode) {
modeEl.value = "php";
}
else if (mode instanceof JavaMode) {
modeEl.value = "java";
}
else if (mode instanceof RubyMode) {
modeEl.value = "ruby";
}
else if (mode instanceof CCPPMode) {
modeEl.value = "c_cpp";
}
else if (mode instanceof CoffeeMode) {
modeEl.value = "coffee";
}
else if (mode instanceof PerlMode) {
modeEl.value = "perl";
}
else {
modeEl.value = "text";
}
if (!doc.getUseWrapMode()) {
wrapModeEl.value = "off";
} else {
wrapModeEl.value = doc.getWrapLimitRange().min || "free";
}
env.editor.focus();
});
bindDropdown("mode", function(value) {
env.editor.getSession().setMode(modes[value] || modes.text);
});
bindDropdown("theme", function(value) {
env.editor.setTheme(value);
});
bindDropdown("keybinding", function(value) {
env.editor.setKeyboardHandler(keybindings[value]);
});
bindDropdown("fontsize", function(value) {
document.getElementById("editor").style.fontSize = value;
});
bindDropdown("soft_wrap", function(value) {
var session = env.editor.getSession();
var renderer = env.editor.renderer;
switch (value) {
case "off":
session.setUseWrapMode(false);
renderer.setPrintMarginColumn(80);
break;
case "40":
session.setUseWrapMode(true);
session.setWrapLimitRange(40, 40);
renderer.setPrintMarginColumn(40);
break;
case "80":
session.setUseWrapMode(true);
session.setWrapLimitRange(80, 80);
renderer.setPrintMarginColumn(80);
break;
case "free":
session.setUseWrapMode(true);
session.setWrapLimitRange(null, null);
renderer.setPrintMarginColumn(80);
break;
}
});
bindCheckbox("select_style", function(checked) {
env.editor.setSelectionStyle(checked ? "line" : "text");
});
bindCheckbox("highlight_active", function(checked) {
env.editor.setHighlightActiveLine(checked);
});
bindCheckbox("show_hidden", function(checked) {
env.editor.setShowInvisibles(checked);
});
bindCheckbox("show_gutter", function(checked) {
env.editor.renderer.setShowGutter(checked);
});
bindCheckbox("show_print_margin", function(checked) {
env.editor.renderer.setShowPrintMargin(checked);
});
bindCheckbox("highlight_selected_word", function(checked) {
env.editor.setHighlightSelectedWord(checked);
});
bindCheckbox("show_hscroll", function(checked) {
env.editor.renderer.setHScrollBarAlwaysVisible(checked);
});
bindButton("load", function() {
var fp = filePicker.FilePicker("Open file");
fp.show(function(path) {
var contents = file.read(path);
env.editor.getSession().setValue(contents);
/* docs.js = new EditSession(contents);
docs.js.setMode(new JavaScriptMode());
docs.js.setUndoManager(new UndoManager());
var container = document.getElementById("editor");
env.editor = new Editor(new Renderer(container, theme));
env.editor.setSession(docs);*/
});
});
bindButton("save", function() {
var fp = filePicker.FilePicker("Save file", "save");
fp.show(function(path) {
file.write(path, env.editor.getSession().getValue());
});
});
function bindCheckbox(id, callback) {
var el = document.getElementById(id);
var onCheck = function() {
callback(!!el.checked);
};
el.onclick = onCheck;
onCheck();
}
function bindDropdown(id, callback) {
var el = document.getElementById(id);
var onChange = function() {
callback(el.value);
};
el.onchange = onChange;
onChange();
}
function bindButton(id, callback) {
var el = document.getElementById(id);
var onClick = function() {
callback(el.value);
};
el.onclick = onClick;
}
function onResize() {
container.style.width = (document.documentElement.clientWidth) + "px";
container.style.height = (document.documentElement.clientHeight - 60 - 22) + "px";
env.editor.resize();
};
window.onresize = onResize;
onResize();
event.addListener(container, "dragover", function(e) {
return event.preventDefault(e);
});
event.addListener(container, "drop", function(e) {
try {
var file = e.dataTransfer.files[0];
} catch(e) {
return event.stopEvent();
}
if (window.FileReader) {
var reader = new FileReader();
reader.onload = function(e) {
env.editor.getSelection().selectAll();
var mode = "text";
if (/^.*\.js$/i.test(file.name)) {
mode = "javascript";
} else if (/^.*\.xml$/i.test(file.name)) {
mode = "xml";
} else if (/^.*\.html$/i.test(file.name)) {
mode = "html";
} else if (/^.*\.css$/i.test(file.name)) {
mode = "css";
} else if (/^.*\.py$/i.test(file.name)) {
mode = "python";
} else if (/^.*\.php$/i.test(file.name)) {
mode = "php";
} else if (/^.*\.java$/i.test(file.name)) {
mode = "java";
} else if (/^.*\.rb$/i.test(file.name)) {
mode = "ruby";
} else if (/^.*\.(c|cpp|h|hpp|cxx)$/i.test(file.name)) {
mode = "c_cpp";
} else if (/^.*\.coffee$/i.test(file.name)) {
mode = "coffee";
} else if (/^.*\.(pl|pm)$/i.test(file.name)) {
mode = "perl";
}
env.editor.onTextInput(reader.result);
modeEl.value = mode;
env.editor.getSession().setMode(modes[mode]);
};
reader.readAsText(file);
}
return event.preventDefault(e);
});
window.env = env;
};
});