-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditors.js
146 lines (134 loc) · 4.92 KB
/
editors.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
function createCodeEditor(node) {
var options = {
value: "// Enter javascript code here\nprintln(\"Hello, world!\");",
mode: "javascript",
indentUnit: 4, // Indent with 4 spaces
lineNumbers: true, // Show line numbers
matchBrackets: true,
extraKeys: {
"Enter": function(cm) { cm.autoInsertBraces(cm)},
"Ctrl-Enter": function(cm) { cm.autoInsertBraces(cm)},
"Shift-Cmd-S": function(cm) { jswb.saveProject(cm); return true; },
"Shift-Cmd-O": function(cm) { jswb.loadProject(cm); return true; },
"Ctrl-O": function (cm) { jswb.load(cm); return true; },
},
onDragEvent: function(cm, event) {
if (event.type === "drop") {
console.log(event);
event.stopPropagation();
event.preventDefault();
var dt = event.dataTransfer;
var files = dt.files;
jswb.loadFile(cm, files[0]);
return true;
} else if (event.type === "dragover") {
event.stopPropagation();
event.preventDefault();
event.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
}
};
var editor = CodeMirror(node, options);
editor.save = jswb.save;
return editor;
}
// ================================================================================
// REPL
// ================================================================================
function REPLHistoryManager(cm) {
this.editor = cm;
this.history = [];
this.pos = 0;
this.limit = 100; // TODO: make this configurable
this.currentLine = undefined;
}
REPLHistoryManager.prototype.setEditorValue = function (v) {
this.editor.setValue(v);
this.editor.refresh();
CodeMirror.commands.goLineEnd(this.editor);
};
REPLHistoryManager.prototype.resetPos = function () {
this.pos = this.history.length;
this.currentLine = undefined;
};
REPLHistoryManager.prototype.add = function (line) {
this.history.push(line);
while (this.history.length > this.limit) {
this.history.shift();
}
this.resetPos();
};
REPLHistoryManager.prototype.previous = function () {
var index = this.pos - 1;
if (this.pos === this.history.length) {
// Remember the current line to be able to restore it later, if needed
this.currentLine = this.editor.getValue();
}
if (index >= 0) {
// Restore previous history item
this.setEditorValue(this.history[index]);
this.pos = index;
}
};
REPLHistoryManager.prototype.next = function () {
var index = this.pos + 1;
if (index < this.history.length) {
this.setEditorValue(this.history[index]);
this.pos = index;
} else if (index === this.history.length) {
this.setEditorValue(this.currentLine);
this.resetPos();
}
};
function createREPL(node) {
var options = {
mode: "javascript",
indentUnit: 4, // Indent by 4 spaces
lineNumbers: false, // Show line numbers
matchBrackets: true,
extraKeys: {
"Ctrl-C": function (cm) { cm.setValue(""); cm.refresh(); cm.jswb.history.resetPos(); },
"Ctrl-L": function (cm) { jswb.clearConsole(); cm.jswb.history.resetPos(); },
"Ctrl-Enter": function(cm) { cm.autoInsertBraces(cm)},
"Enter": function(cm) {
var script = cm.getValue();
cm.setValue("");
cm.refresh();
jswb.addLineToConsole(script, "console-line");
if (script) {
try {
cm.busy = true;
var result = jswb.runScript(script);
if (result !== undefined) {
jswb.addLineToConsole(result, "console-result");
}
} catch (error) {
jswb.addLineToConsole(error, "console-error label label-important");
}
cm.jswb.history.add(script);
cm.busy = false;
}
return true;
},
"Up": function (cm) {
cm.jswb.history.previous();
return true;
},
"Down": function (cm) {
cm.jswb.history.next();
return true;
}
},
onKeyEvent: function (cm, event) {
if (cm.busy) {
event.stopPropagation();
event.preventDefault();
}
},
};
var editor = CodeMirror(node, options);
editor.jswb = {};
editor.jswb.history = new REPLHistoryManager(editor);
editor.busy = false;
return editor;
}