-
Notifications
You must be signed in to change notification settings - Fork 4
/
ui.js
196 lines (170 loc) · 6.08 KB
/
ui.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
(function(__tjs){
__tjs.allAceEditors = [];
var isMac = global.navigator.appVersion.indexOf("Mac")!=-1;
var commandSymbolHTML = isMac ? '⌘' : 'Ctrl';
var mdnSearchShortcutKey = ';';
var runShortcutKey = 'R';
var maxLogsCount = 100;
shortcut.add(
(isMac ? 'Meta' : 'Ctrl')+'+'+mdnSearchShortcutKey,
function(event) {
var allFocused = __tjs.allAceEditors.filter(function(ace){
return ace.$isFocused;
});
if(allFocused.length) {
__tjs.mdnSearchPopup(allFocused[0].getSelectedText());
} else {
__tjs.mdnSearchPopup(window.getSelection().toString());
}
event.preventDefault();
},
{
'type':'keydown',
'propagate':true,
'target':document
}
);
var storedFileKey = '__tjs.storedFile';
var storedFile = localStorage[storedFileKey];
global.setInterval(function(){
localStorage[storedFileKey] = __tjs.editor.getValue();
}, 3000);
__tjs.editor = ace.edit("__editor");
__tjs.editor.setTheme("ace/theme/tomorrow_night");
var editSession = __tjs.editor.getSession();
editSession.setMode("ace/mode/javascript");
editSession.setTabSize(2);
if(storedFile) {
//__tjs.editor.$blockScrolling = Infinity;
__tjs.editor.setValue(storedFile, -1);
}
__tjs.editor.focus();
__tjs.allAceEditors.push(__tjs.editor);
for(var i = 1; i <= 3; i++) {
var helpContentAce = ace.edit("__helpContent"+i);
helpContentAce.setTheme("ace/theme/tomorrow_night");
helpContentAce.getSession().setMode("ace/mode/javascript");
helpContentAce.setOptions({
readOnly: true,
highlightActiveLine: false,
highlightGutterLine: false
});
helpContentAce.renderer.setShowGutter(false);
helpContentAce.renderer.$cursorLayer.element.style.opacity=0;
helpContentAce.textInput.getElement().tabIndex=-1;
helpContentAce.commands.commmandKeyBinding={};
__tjs.allAceEditors.push(helpContentAce);
}
var runButton = global.document.getElementById('__runButton');
if(runButton){
runButton.innerHTML = "Run ( "+commandSymbolHTML+" "+runShortcutKey+" )";
runButton.onclick = function () {
runTurtleJS();
};
}
var hotkeyHelp = global.document.getElementById('__hotkeyHelp');
if(hotkeyHelp){
hotkeyHelp.innerHTML =
"ProTip: Quick search Mozilla Developer Network (MDN) for the "
+ "selected text with "+commandSymbolHTML+" + "+mdnSearchShortcutKey+" ";
}
global.document.getElementById('__helpButton').onclick = function () {
global.document.getElementById('__helpContainer').style.display = 'block';
};
global.document.getElementById('__closeHelpButton').onclick = function () {
global.document.getElementById('__helpContainer').style.display = 'none';
};
global.document.getElementById('__closeMdnButton').onclick = function () {
global.document.getElementById('__mdnIframeContainer').style.display = 'none';
};
global.document.getElementById('__consoleBar').onclick = function () {
global.document.getElementById('__console').style.display = 'block';
};
global.document.getElementById('__closeConsoleButton').onclick = function () {
global.document.getElementById('__console').style.display = 'none';
};
var KEYCODE_ESCAPE = 27;
window.addEventListener("keydown", (event) => {
if(event.keyCode == KEYCODE_ESCAPE) {
if(document.getElementById('__mdnIframeContainer').style.display == 'block') {
document.getElementById('__mdnIframeContainer').style.display = 'none';
} else if(document.getElementById('__helpContainer').style.display == 'block') {
document.getElementById('__helpContainer').style.display = 'none';
}
}
}, false);
shortcut.add(
(isMac ? 'Meta' : 'Ctrl')+'+'+runShortcutKey,
function(event) {
event.preventDefault();
runTurtleJS();
},
{
'type':'keydown',
'propagate':true,
'target':document
}
);
__tjs.addLog = function (logObject) {
var container = global.document.getElementById('__consoleContainer');
if(__tjs.logs.length >= maxLogsCount) {
container.removeChild(container.children[0]);
__tjs.logs.unshift();
}
__tjs.logs.push(logObject);
var newLogElement = global.document.createElement('div');
newLogElement.setAttribute('class', 'logElement ' + logObject.type);
newLogElement.innerHTML = logObject.message;
newLogElement.ondblclick = gotoLine.bind(this, logObject);
container.appendChild(newLogElement);
setBar(logObject);
pushRecentLog(logObject);
if(logObject.type === 'error') {
gotoLine(logObject);
}
}
var setAnnotationsAfterDebounce = debounce(function() {
__tjs.editor.getSession().setAnnotations(__tjs.recentLogs.map(function(log){
return {
row: log.lineNumber-1,
column: log.charOffset,
text: log.message,
type: log.type
};
}));
}, 250);
var clearNewAnnotationsAfterDebounce = debounce(function(){
__tjs.recentLogs = [];
}, 2000);
__tjs.recentLogs = [];
function pushRecentLog(logObject) {
__tjs.recentLogs.push(logObject);
setAnnotationsAfterDebounce();
clearNewAnnotationsAfterDebounce();
}
function gotoLine(logObject) {
if(logObject.lineNumber) {
__tjs.editor.scrollToLine(logObject.lineNumber, true, true, function () {});
__tjs.editor.gotoLine(logObject.lineNumber, logObject.charOffset, true);
}
}
function setBar(logObject) {
var bar = global.document.getElementById('__consoleBar');
bar.innerHTML = logObject ? logObject.message : '';
bar.setAttribute('class',
logObject ? 'logElement ' + logObject.type : '');
}
function runTurtleJS () {
global.document.getElementById('__loadingScreen').style.display = 'block';
setTimeout(function(){
__tjs.runTurtleJS();
global.document.getElementById('__loadingScreen').style.display = 'none';
},50);
}
global.document.getElementById('__clearLogsButton').onclick = function () {
setBar(null);
var container = global.document.getElementById('__consoleContainer');
container.innerHTML = '';
__tjs.logs = [];
};
})(__tjs);