-
Notifications
You must be signed in to change notification settings - Fork 1
/
editor.html
191 lines (142 loc) · 5.77 KB
/
editor.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
<!DOCTYPE html>
<html>
<head>
<title>Beginner JavaScript</title>
</head>
<body>
<script src="allPages.js"></script>
<!-- HTML Mixed CodeMirror-->
<script src="codemirror-5.42.0/lib/codemirror.js"></script>
<link rel="stylesheet" href="codemirror-5.42.0/lib/codemirror.css">
<script src="codemirror-5.42.0/mode/htmlmixed/htmlmixed.js"></script>
<script src="codemirror-5.42.0/mode/xml/xml.js"></script>
<script src="codemirror-5.42.0/mode/javascript/javascript.js"></script>
<script src="codemirror-5.42.0/mode/css/css.js"></script>
<script>
//HTML that is in editor at start
//.split("\n").join("\\n").split("\t").join("\\t").split("\"").join("\\\"")
window.initialHTML = localStorage.getItem("htmlcode")
if (window.initialHTML === null) {
window.initialHTML = "<!--<!DOCTYPE html>\n<html>\n\t<head>\n\t</head>\n\t<body>\n <p>Type HTML in the editor on the left. When you click run, the HTML page will be displayed here</p>\n <p>This editor saves your changes every second - so it you close the tab and come back, your code will still be here (unless you clear browsing data)</p>\n <style>\n /*CSS goes here*/\n body {\n background-color:yellow;\n }\n </style>\n <script>\n \t//JavaScript goes here\n </script>\n\t</body>\n</html>-->";
window.initialHTML = window.initialHTML.slice(4)
window.initialHTML = window.initialHTML.slice(0, -3)
}
</script>
<!-- All the stuff below is CodeMirror-->
<!-- window.initialHTML is the HTML that they should begin with-->
<div id="editorwrapper">
<textarea id="editor"></textarea>
<button id="run">Run!</button>
<button id="download">Download!</button>
</div>
<iframe id="result" src="javascript:document.write('Click run - the HTML page will display here')"></iframe>
<script>
function download(content, name, mimetype) {
if (!window.Blob) {
//IE <10
var ifd = document.getElementById('dummy').contentDocument;
ifd.open('text/plain', 'replace');
ifd.write(content);
ifd.close();
ifd.execCommand('SaveAs', true, name);
}
else {
content = new Blob([content], {type:mimetype})
//IE >=10
if (navigator.msSaveBlob) {
navigator.msSaveBlob(content, name)
}
else {
//Non-IE
var link = document.createElement("a");
document.body.appendChild(link);
link.download = name
link.href = URL.createObjectURL(content);
link.click();
document.body.removeChild(link);
}
}
}
document.getElementById("editor").value = window.initialHTML
var editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
name: "htmlmixed",
autoCloseTags: true,
styleActiveLine: true,
lineWrapping: false,
lineNumbers:true,
tabSize:2
});
function runcode() {
var htmlcode = editor.getValue()
var iframe = document.getElementById("result")
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(htmlcode);
iframe.contentWindow.document.close();
}
runcode()
document.getElementById("run").addEventListener("click", runcode)
document.getElementById("download").addEventListener("click", function(){
download(editor.getValue(), "website.html", "text/html")
})
var previoustext = editor.getValue()
function save() {
if (editor.getValue() !== previoustext) {
localStorage.setItem("htmlcode", editor.getValue())
}
}
setInterval(save, 1000)
</script>
<style>
#editorwrapper {
position:fixed;
bottom:0;
left:0;
height:100%;
width:60%;
display:inline-block;
float:left;
padding:0;
border-right:1px solid black;
border-top:1px solid black;
}
#result {
position:fixed;
bottom:0;
right:0;
height:100%;
width:40%;
border:0;
border-top:1px solid black;
border-left:1px solid black;
display:inline-block;
float:right;
padding:0;
}
.CodeMirror {
width:100%;
height:96%;
}
#run {
width:60%;
height:4%;
font-size:16px;
background-color:#99e0ff;
border:0;
display:block;
float:left;
}
#download {
width:40%;
height:4%;
font-size:16px;
background-color:#99ffe0;
border:0;
display:block;
}
html, body {
width:100%;
height:100%;
}
</style>
</body>
</html>