-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathdemo.js
670 lines (606 loc) · 20.2 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
"use strict";
require("ace/ext/rtl");
require("ace/multi_select");
require("./inline_editor");
var devUtil = require("./dev_util");
require("./file_drop");
var config = require("ace/config");
config.setLoader(function(moduleName, cb) {
require([moduleName], function(module) {
cb(null, module);
});
});
var env = {};
var dom = require("ace/lib/dom");
var net = require("ace/lib/net");
var lang = require("ace/lib/lang");
var event = require("ace/lib/event");
var theme = require("ace/theme/textmate");
var EditSession = require("ace/edit_session").EditSession;
var UndoManager = require("ace/undomanager").UndoManager;
var HashHandler = require("ace/keyboard/hash_handler").HashHandler;
var Renderer = require("ace/virtual_renderer").VirtualRenderer;
var Editor = require("ace/editor").Editor;
var Range = require("ace/range").Range;
var whitespace = require("ace/ext/whitespace");
var doclist = require("./doclist");
var layout = require("./layout");
var util = require("./util");
var saveOption = util.saveOption;
require("ace/ext/elastic_tabstops_lite");
require("ace/incremental_search");
var TokenTooltip = require("./token_tooltip").TokenTooltip;
require("ace/config").defineOptions(Editor.prototype, "editor", {
showTokenInfo: {
set: function(val) {
if (val) {
this.tokenTooltip = this.tokenTooltip || new TokenTooltip(this);
}
else if (this.tokenTooltip) {
this.tokenTooltip.destroy();
delete this.tokenTooltip;
}
},
get: function() {
return !!this.tokenTooltip;
},
handlesSet: true
}
});
require("ace/config").defineOptions(Editor.prototype, "editor", {
useAceLinters: {
set: function(val) {
if (val && !window.languageProvider) {
loadLanguageProvider(editor);
}
else if (val) {
window.languageProvider.registerEditor(this);
} else {
// todo unregister
}
}
}
});
var {HoverTooltip} = require("ace/tooltip");
var MarkerGroup = require("ace/marker_group").MarkerGroup;
var docTooltip = new HoverTooltip();
function loadLanguageProvider(editor) {
function loadScript(cb) {
if (define.amd) {
require([
"https://mkslanc.github.io/ace-linters/build/ace-linters.js"
], function(m) {
cb(m.LanguageProvider);
});
} else {
net.loadScript([
"https://mkslanc.github.io/ace-linters/build/ace-linters.js"
], function() {
cb(window.LanguageProvider);
});
}
}
loadScript(function(LanguageProvider) {
var languageProvider = LanguageProvider.fromCdn("https://mkslanc.github.io/ace-linters/build", {
functionality: {
hover: true,
completion: {
overwriteCompleters: true
},
completionResolve: true,
format: true,
documentHighlights: true,
signatureHelp: false
}
});
window.languageProvider = languageProvider;
languageProvider.registerEditor(editor);
});
}
var workerModule = require("ace/worker/worker_client");
if (location.href.indexOf("noworker") !== -1) {
workerModule.WorkerClient = workerModule.UIWorkerClient;
}
/*********** create editor ***************************/
var container = document.getElementById("editor-container");
// Splitting.
var Split = require("ace/split").Split;
var split = new Split(container, theme, 1);
env.editor = split.getEditor(0);
split.on("focus", function(editor) {
env.editor = editor;
updateUIEditorOptions();
});
env.split = split;
window.env = env;
var consoleEl = dom.createElement("div");
container.parentNode.appendChild(consoleEl);
consoleEl.style.cssText = "position:fixed; bottom:1px; right:0;\
border:1px solid #baf; z-index:100";
var cmdLine = new layout.singleLineEditor(consoleEl);
cmdLine.setOption("placeholder", "Enter a command...");
cmdLine.editor = env.editor;
env.editor.cmdLine = cmdLine;
env.editor.showCommandLine = function(val) {
this.cmdLine.focus();
if (typeof val == "string")
this.cmdLine.setValue(val, 1);
};
/**
* This demonstrates how you can define commands and bind shortcuts to them.
*/
env.editor.commands.addCommands([{
name: "snippet",
bindKey: {win: "Alt-C", mac: "Command-Alt-C"},
exec: function(editor, needle) {
if (typeof needle == "object") {
editor.cmdLine.setValue("snippet ", 1);
editor.cmdLine.focus();
return;
}
var s = snippetManager.getSnippetByName(needle, editor);
if (s)
snippetManager.insertSnippet(editor, s.content);
},
readOnly: true
}, {
name: "focusCommandLine",
bindKey: "shift-esc|ctrl-`",
exec: function(editor, needle) { editor.cmdLine.focus(); },
readOnly: true
}, {
name: "nextFile",
bindKey: "Ctrl-tab",
exec: function(editor) { doclist.cycleOpen(editor, 1); },
readOnly: true
}, {
name: "previousFile",
bindKey: "Ctrl-shift-tab",
exec: function(editor) { doclist.cycleOpen(editor, -1); },
readOnly: true
}, {
name: "execute",
bindKey: "ctrl+enter",
exec: function(editor) {
try {
var r = window.eval(editor.getCopyText() || editor.getValue());
} catch(e) {
r = e;
}
editor.cmdLine.setValue(r + "");
},
readOnly: true
}, {
name: "showKeyboardShortcuts",
bindKey: {win: "Ctrl-Alt-h", mac: "Command-Alt-h"},
exec: function(editor) {
config.loadModule("ace/ext/keybinding_menu", function(module) {
module.init(editor);
editor.showKeyboardShortcuts();
});
}
}, {
name: "increaseFontSize",
bindKey: "Ctrl-=|Ctrl-+",
exec: function(editor) {
var size = parseInt(editor.getFontSize(), 10) || 12;
editor.setFontSize(size + 1);
}
}, {
name: "decreaseFontSize",
bindKey: "Ctrl+-|Ctrl-_",
exec: function(editor) {
var size = parseInt(editor.getFontSize(), 10) || 12;
editor.setFontSize(Math.max(size - 1 || 1));
}
}, {
name: "resetFontSize",
bindKey: "Ctrl+0|Ctrl-Numpad0",
exec: function(editor) {
editor.setFontSize(12);
}
}]);
env.editor.commands.addCommands(whitespace.commands);
cmdLine.commands.bindKeys({
"Shift-Return|Ctrl-Return|Alt-Return": function(cmdLine) { cmdLine.insert("\n"); },
"Esc|Shift-Esc": function(cmdLine){ cmdLine.editor.focus(); },
"Return": function(cmdLine){
var command = cmdLine.getValue().split(/\s+/);
var editor = cmdLine.editor;
editor.commands.exec(command[0], editor, command[1]);
editor.focus();
}
});
cmdLine.commands.removeCommands(["find", "gotoline", "findall", "replace", "replaceall"]);
var commands = env.editor.commands;
commands.addCommand({
name: "save",
bindKey: {win: "Ctrl-S", mac: "Command-S"},
exec: function(arg) {
var session = env.editor.session;
var name = session.name.match(/[^\/]+$/);
localStorage.setItem(
"saved_file:" + name,
session.getValue()
);
env.editor.cmdLine.setValue("saved "+ name);
}
});
commands.addCommand({
name: "load",
bindKey: {win: "Ctrl-O", mac: "Command-O"},
exec: function(arg) {
var session = env.editor.session;
var name = session.name.match(/[^\/]+$/);
var value = localStorage.getItem("saved_file:" + name);
if (typeof value == "string") {
session.setValue(value);
env.editor.cmdLine.setValue("loaded "+ name);
} else {
env.editor.cmdLine.setValue("no previuos value saved for "+ name);
}
}
});
/*********** manage layout ***************************/
function handleToggleActivate(target) {
if (dom.hasCssClass(sidePanelContainer, "closed"))
onResize(null, false);
else if (dom.hasCssClass(target, "toggleButton"))
onResize(null, true);
};
var sidePanelContainer = document.getElementById("sidePanel");
sidePanelContainer.onclick = function(e) {
handleToggleActivate(e.target);
};
var optionToggle = document.getElementById("optionToggle");
optionToggle.onkeydown = function(e) {
if (e.code === "Space" || e.code === "Enter") {
handleToggleActivate(e.target);
}
};
var consoleHeight = 20;
function onResize(e, closeSidePanel) {
var left = 280;
var width = document.documentElement.clientWidth;
var height = document.documentElement.clientHeight;
if (closeSidePanel == null)
closeSidePanel = width < 2 * left;
if (closeSidePanel) {
left = 20;
document.getElementById("optionToggle").setAttribute("aria-label", "Show Options");
} else
document.getElementById("optionToggle").setAttribute("aria-label", "Hide Options");
width -= left;
container.style.width = width + "px";
container.style.height = height - consoleHeight + "px";
container.style.left = left + "px";
env.split.resize();
consoleEl.style.width = width + "px";
consoleEl.style.left = left + "px";
cmdLine.resize();
sidePanel.style.width = left + "px";
sidePanel.style.height = height + "px";
dom.setCssClass(sidePanelContainer, "closed", closeSidePanel);
}
window.onresize = onResize;
onResize();
/*********** options panel ***************************/
doclist.history = doclist.docs.map(function(doc) {
return doc.name;
});
doclist.history.index = 0;
doclist.cycleOpen = function(editor, dir) {
var h = this.history;
h.index += dir;
if (h.index >= h.length)
h.index = 0;
else if (h.index <= 0)
h.index = h.length - 1;
var s = h[h.index];
doclist.pickDocument(s);
};
doclist.addToHistory = function(name) {
var h = this.history;
var i = h.indexOf(name);
if (i != h.index) {
if (i != -1)
h.splice(i, 1);
h.index = h.push(name);
}
};
doclist.pickDocument = function(name) {
doclist.loadDoc(name, function(session) {
if (!session)
return;
doclist.addToHistory(session.name);
session = env.split.setSession(session);
whitespace.detectIndentation(session);
optionsPanel.render();
env.editor.focus();
});
};
var OptionPanel = require("ace/ext/options").OptionPanel;
var optionsPanel = new OptionPanel(env.editor);
var originalAutocompleteCommand = null;
optionsPanel.add({
Main: {
Document: {
type: "select",
path: "doc",
items: doclist.all,
position: -101,
onchange: doclist.pickDocument,
getValue: function() {
return env.editor.session.name || "javascript";
}
},
Split: {
type: "buttonBar",
path: "split",
values: ["None", "Below", "Beside"],
position: -100,
onchange: function(value) {
var sp = env.split;
if (value == "Below" || value == "Beside") {
var newEditor = (sp.getSplits() == 1);
sp.setOrientation(value == "Below" ? sp.BELOW : sp.BESIDE);
sp.setSplits(2);
if (newEditor) {
var session = sp.getEditor(0).session;
var newSession = sp.setSession(session, 1);
newSession.name = session.name;
}
} else {
sp.setSplits(1);
}
},
getValue: function() {
var sp = env.split;
return sp.getSplits() == 1
? "None"
: sp.getOrientation() == sp.BELOW
? "Below"
: "Beside";
}
}
},
More: {
"RTL": {
path: "rtl",
position: 900
},
"Line based RTL switching": {
path: "rtlText",
position: 900
},
"Show token info": {
path: "showTokenInfo",
position: 2000
},
"Inline preview for autocomplete": {
path: "inlineEnabledForAutocomplete",
position: 2000,
onchange: function(value) {
var Autocomplete = require("ace/autocomplete").Autocomplete;
if (value && !originalAutocompleteCommand) {
originalAutocompleteCommand = Autocomplete.startCommand.exec;
Autocomplete.startCommand.exec = function(editor) {
var autocomplete = Autocomplete.for(editor);
autocomplete.inlineEnabled = true;
originalAutocompleteCommand(...arguments);
}
} else if (!value) {
var autocomplete = Autocomplete.for(editor);
autocomplete.destroy();
if (originalAutocompleteCommand)
Autocomplete.startCommand.exec = originalAutocompleteCommand;
originalAutocompleteCommand = null;
}
},
getValue: function() {
return !!originalAutocompleteCommand;
}
},
"Use Ace Linters": {
position: 3000,
path: "useAceLinters"
},
"Show Textarea Position": devUtil.textPositionDebugger,
"Text Input Debugger": devUtil.textInputDebugger,
}
});
var optionsPanelContainer = document.getElementById("optionsPanel");
optionsPanel.render();
optionsPanelContainer.insertBefore(optionsPanel.container, optionsPanelContainer.firstChild);
optionsPanel.container.style.width = "80%";
optionsPanel.on("setOption", function(e) {
util.saveOption(e.name, e.value);
});
function updateUIEditorOptions() {
optionsPanel.editor = env.editor;
optionsPanel.render();
}
optionsPanel.setOption("doc", util.getOption("doc") || "JavaScript");
for (var i in optionsPanel.options) {
var value = util.getOption(i);
if (value != undefined) {
if ((i == "mode" || i == "theme") && !/[/]/.test(value))
value = "ace/" + i + "/" + value;
optionsPanel.setOption(i, value);
}
}
function synchroniseScrolling() {
var s1 = env.split.$editors[0].session;
var s2 = env.split.$editors[1].session;
s1.on('changeScrollTop', function(pos) {s2.setScrollTop(pos)});
s2.on('changeScrollTop', function(pos) {s1.setScrollTop(pos)});
s1.on('changeScrollLeft', function(pos) {s2.setScrollLeft(pos)});
s2.on('changeScrollLeft', function(pos) {s1.setScrollLeft(pos)});
}
var StatusBar = require("ace/ext/statusbar").StatusBar;
new StatusBar(env.editor, cmdLine.container);
require("ace/placeholder").PlaceHolder;
var snippetManager = require("ace/snippets").snippetManager;
env.editSnippets = function() {
var sp = env.split;
if (sp.getSplits() == 2) {
sp.setSplits(1);
return;
}
sp.setSplits(1);
sp.setSplits(2);
sp.setOrientation(sp.BESIDE);
var editor = sp.$editors[1];
var id = sp.$editors[0].session.$mode.$id || "";
var m = snippetManager.files[id];
if (!doclist["snippets/" + id]) {
var text = m.snippetText;
var s = doclist.initDoc(text, "", {});
s.setMode("ace/mode/snippets");
doclist["snippets/" + id] = s;
}
editor.on("blur", function() {
m.snippetText = editor.getValue();
snippetManager.unregister(m.snippets);
m.snippets = snippetManager.parseSnippetFile(m.snippetText, m.scope);
snippetManager.register(m.snippets);
});
sp.$editors[0].once("changeMode", function() {
sp.setSplits(1);
});
editor.setSession(doclist["snippets/" + id], 1);
editor.focus();
};
optionsPanelContainer.insertBefore(
dom.buildDom(["div", {style: "text-align:right;width: 80%"},
["div", {},
["button", {onclick: env.editSnippets}, "Edit Snippets"]],
["div", {},
["button", {onclick: function() {
var info = navigator.platform + "\n" + navigator.userAgent;
if (env.editor.getValue() == info)
return env.editor.undo();
env.editor.setValue(info, -1);
env.editor.setOption("wrap", 80);
}}, "Show Browser Info"]],
devUtil.getUI(),
["div", {},
"Open Dialog ",
["button", {onclick: openTestDialog.bind(null, false)}, "Scale"],
["button", {onclick: openTestDialog.bind(null, true)}, "Height"]
]
]),
optionsPanelContainer.children[1]
);
function openTestDialog(animateHeight) {
if (window.dialogEditor)
window.dialogEditor.destroy();
var editor = ace.edit(null, {
value: "test editor",
mode: "ace/mode/javascript"
});
window.dialogEditor = editor;
var dialog = dom.buildDom(["div", {
style: "transition: all 1s; position: fixed; z-index: 100000;"
+ "background: darkblue; border: solid 1px black; display: flex; flex-direction: column"
},
["div", {}, "test dialog"],
editor.container
], document.body);
editor.container.style.flex = "1";
if (animateHeight) {
dialog.style.width = "0vw";
dialog.style.height = "0vh";
dialog.style.left = "20vw";
dialog.style.top = "20vh";
setTimeout(function() {
dialog.style.width = "80vw";
dialog.style.height = "80vh";
dialog.style.left = "10vw";
dialog.style.top = "10vh";
}, 0);
} else {
dialog.style.width = "80vw";
dialog.style.height = "80vh";
dialog.style.left = "10vw";
dialog.style.top = "10vh";
dialog.style.transform = "scale(0)";
setTimeout(function() {
dialog.style.transform = "scale(1)"
}, 0);
}
function close(e) {
if (!e || !dialog.contains(e.target)) {
if (animateHeight) {
dialog.style.width = "0vw";
dialog.style.height = "0vh";
dialog.style.left = "80vw";
dialog.style.top = "80vh";
} else {
dialog.style.transform = "scale(0)"
}
window.removeEventListener("mousedown", close);
dialog.addEventListener("transitionend", function() {
dialog.remove();
editor.destroy();
});
}
}
window.addEventListener("mousedown", close);
editor.focus()
editor.commands.bindKey("Esc", function() { close(); });
}
require("ace/ext/language_tools");
require("ace/ext/inline_autocomplete");
env.editor.setOptions({
enableBasicAutocompletion: true,
enableInlineAutocompletion: true,
enableSnippets: true
});
var beautify = require("ace/ext/beautify");
env.editor.commands.addCommands(beautify.commands);
// global keybindings
var KeyBinding = require("ace/keyboard/keybinding").KeyBinding;
var CommandManager = require("ace/commands/command_manager").CommandManager;
var commandManager = new CommandManager();
var kb = new KeyBinding({
commands: commandManager,
fake: true
});
event.addCommandKeyListener(document.documentElement, kb.onCommandKey.bind(kb));
event.addListener(document.documentElement, "keyup", function(e) {
if (e.keyCode === 18) // do not trigger browser menu on windows
e.preventDefault();
});
commandManager.addCommands([{
name: "window-left",
bindKey: {win: "cmd-alt-left", mac: "ctrl-cmd-left"},
exec: function() {
moveFocus();
}
}, {
name: "window-right",
bindKey: {win: "cmd-alt-right", mac: "ctrl-cmd-right"},
exec: function() {
moveFocus();
}
}, {
name: "window-up",
bindKey: {win: "cmd-alt-up", mac: "ctrl-cmd-up"},
exec: function() {
moveFocus();
}
}, {
name: "window-down",
bindKey: {win: "cmd-alt-down", mac: "ctrl-cmd-down"},
exec: function() {
moveFocus();
}
}]);
function moveFocus() {
var el = document.activeElement;
if (el == env.editor.textInput.getElement())
env.editor.cmdLine.focus();
else
env.editor.focus();
}