-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.html
202 lines (178 loc) · 6.54 KB
/
app.html
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
<html>
<head>
<title>Hello World!</title>
<script type='text/javascript' src='src/vendor/jquery-1.9.1.min.js'></script>
<!-- These coffeescript files need the .js extension so we don't get mimetype errors
on page reload. Only coffeescript files placed here need the .js -->
<link href="src/vendor/codemirror.css" rel="stylesheet">
<link class='theme-css' href="src/vendor/theme/vibrant-ink.css" rel="stylesheet">
<script type='text/coffeescript' src="src/shims.coffee.js"></script>
<script type='text/coffeescript' src="src/application.coffee.js"></script>
<script src="src/vendor/coffee-script.js"></script>
<script src="src/vendor/codemirror.js"></script>
<script src="src/vendor/runmode.js"></script>
<script src="src/vendor/mode/javascript/javascript.js"></script>
<style type="text/css">
body {
margin: 0;
}
iframe {
width: 100%; height: 400px;
}
.draggable {
-webkit-app-region: drag;
background-color: #eee;
width: 100%;
height: 20px;
cursor: move;
}
</style>
</head>
<body>
<div class='draggable'></div>
<select class='syntax'></select>
<select class='theme'></select>
<label for='font-input'>Font</label>
<input id='font-input' type='text'/>
<input id='font-size-input' type='number'/>
<div class='console'></div>
<iframe></iframe>
<script>
var iframeDoc = $('iframe')[0].contentDocument
var iframeBody = $(iframeDoc).find('body')
var fs = require('fs')
var syntaxList = $('.syntax')
var themeList = $('.theme')
var currentSyntax = window.localStorage['currentSyntax'] || 'javascript'
var currentTheme = window.localStorage['currentTheme'] || 'vibrant-ink'
var currentFont = window.localStorage['currentFont'] || 'monospace'
var currentFontSize = window.localStorage['currentFontSize'] || '12'
var modeList = fs.readdirSync('src/vendor/mode')
var head = document.getElementsByTagName('head')[0];
$('#font-input').val(currentFont);
$('#font-size-input').val(currentFontSize);
$("<div style='white-space: pre; overflow: auto; font-family: "+currentFont+"; padding: 5px; font-size: "+currentFontSize+"px;' class='output CodeMirror cm-s-"+currentTheme+" CodeMirror-wrap'></div>").appendTo(iframeBody)
$(iframeDoc).find('head').append("<link class='theme-css' href='src/vendor/theme/"+currentTheme+".css' rel='stylesheet'>")
$(iframeDoc).find('body').css({'padding': '0', 'margin': 0})
$('head .theme-css').attr('href', 'src/vendor/theme/' + currentTheme + '.css')
$.each(modeList, function(index, mode){
if(mode != '.DS_Store') {
if(mode.match(/\.js/)) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'src/vendor/mode/' + mode
head.appendChild(script);
}else{
$.each(fs.readdirSync('src/vendor/mode/'+mode), function(index, file){
if(file.match(/\.js$/) && !file.match(/test\.js$/)) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "src/vendor/mode/"+mode+"/"+file+""
head.appendChild(script);
}
});
}
if(mode === currentSyntax) {
el = $("<option value='"+mode+"' selected='selected'>"+mode+"</option>");
} else {
el = $("<option value='"+mode+"'>"+mode+"</option>");
}
syntaxList.append(el);
}
});
syntaxList.prepend($("<option value=''>None</option>"));
var themeFileList = fs.readdirSync('src/vendor/theme')
$.each(themeFileList, function(index, theme){
if(theme != '.DS_Store') {
theme = theme.replace(/\.css/, '')
if(theme === currentTheme) {
el = $("<option value='"+theme+"' selected='selected'>"+theme+"</option>")
}else{
el = $("<option value='"+theme+"'>"+theme+"</option>")
}
themeList.append(el)
}
});
themeList.prepend($("<option value=''>None</option>"))
var renderHtml = function(syntax, theme){
editorContent = editor.getValue();
CodeMirror.runMode(editorContent, syntax || 'javascript', iframeBody.find('.output')[0])
iframeBody.find('.output')[0].className = "output CodeMirror CodeMirror-wrap cm-s-" + theme
}
var editor = CodeMirror($('.console')[0], {
mode: {
name: currentTheme,
useCPP: true
},
gutter: false,
lineNumbers: false,
theme: currentTheme,
indentUnit: 2,
tabSize: 2,
lineWrapping: true,
autoFocus: true,
extraKeys: {
"Tab": function(cm) {
var spaces = Array(cm.getOption("indentUnit") + 1).join(" ");
cm.replaceSelection(spaces, "end", "+input");
}
},
onKeyEvent: function(){
for (var i = 0; i < editor.lineCount(); i++) {
var line = editor.getLine(i);
if(line.match(/[\u201C\u201D\u2018\u2019]/)) {
editor.setLine(i, line.replace(/[\u2018\u2019]/g, "'").replace(/[\u201C\u201D]/g, '"'));
}
if(line.match(/\t/)) {
editor.setLine(i, line.replace(/\t/g, " "));
}
};
renderHtml(currentSyntax, currentTheme);
}
});
// Not sure why this is needed... its not picking up the mode for some reason in
// the initial editor creation
setTimeout(function(){
editor.setOption('mode',{
name: currentSyntax,
useCPP: true
});
editor.refresh();
}, 300);
$('.syntax').on('change', function(evt){
currentSyntax = $(this).find('option:selected').val()
editor.setOption('mode',{
name: currentSyntax,
useCPP: true
});
editor.refresh();
renderHtml(currentSyntax, currentTheme);
window.localStorage['currentSyntax'] = currentSyntax
});
$('.theme').on('change', function(evt){
currentTheme = $(this).find('option:selected').val()
editor.setOption('theme',currentTheme);
renderHtml(currentSyntax, currentTheme)
$('head .theme-css').attr('href', 'src/vendor/theme/' + currentTheme + '.css')
$(iframeDoc).find('head .theme-css').attr('href', 'src/vendor/theme/' + currentTheme + '.css')
editor.refresh()
window.localStorage['currentTheme'] = currentTheme
});
$('#font-input').on('keyup', function(){
fontInput = $(this).val();
if(fontInput === '') {
fontInput = 'monospace';
}
iframeBody.find('.output').css({'font-family': fontInput});
window.localStorage['currentFontSize'] = fontInput;
});
updateFontSize = function(){
fontSize = $(this).val();
iframeBody.find('.output').css({'font-size': fontSize + "px"});
window.localStorage['currentFontSize'] = fontSize;
}
$('#font-size-input').on('change', updateFontSize);
$('#font-size-input').on('keyup', updateFontSize);
</script>
</body>
</html>