-
Notifications
You must be signed in to change notification settings - Fork 0
/
code-editor.html
209 lines (197 loc) · 6.88 KB
/
code-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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>Spacefate Editor</title>
<script type="application/javascript" src='https://codemirror.net/lib/codemirror.js'></script>
<script type="application/javascript" src='https://codemirror.net/mode/xml/xml.js'></script>
<script type="application/javascript" src='https://codemirror.net/mode/javascript/javascript.js'></script>
<script type="application/javascript" src='https://codemirror.net/mode/css/css.js'></script>
<script type="application/javascript" src='https://codemirror.net/mode/htmlmixed/htmlmixed.js'></script>
<link rel='stylesheet' href='https://codemirror.net/lib/codemirror.css'>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script type="application/javascript" rel='https://codemirror.net/addon/edit/matchbrackets.js'></script>
<script type="application/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<style type='text/css'>
.CodeMirror {
height: auto;
width: auto;
}
iframe {
border: 1px solid black;
height: auto;
width: auto;
}
.hide {
display: none;
}
input[type="file"] {
display: none;
}
#goToBtn {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
z-index: 99;
}
</style>
</head>
<body>
<!-- Top nav -->
<div class="w3-container">
<div class="w3-bar w3-border w3-card-4 w3-light-grey w3-margin-bottom">
<button class="w3-bar-item w3-button" onclick="SideBar()" id="sideBarBtn">☰</button>
<a href="#" class="w3-bar-item w3-button">Home</a>
<a href="#" class="w3-bar-item w3-button w3-right">Link</a>
</div>
</div>
<!-- Sidebar -->
<div class="w3-container">
<div class="w3-sidebar w3-bar-block w3-card-2 w3-animate-left" style="display:none;" id="Sidebar">
<a href="#" class="w3-bar-item w3-padding-16 w3-large" style="cursor:default!important;"><u>Mode</u></a>
<a href="#" class="w3-bar-item w3-button w3-right-align">Visual Editor</a>
<a href="#" class="w3-bar-item w3-button w3-right-align">Terminal</a>
<a href="#" class="w3-bar-item w3-button w3-right-align">Server Info Page</a>
</div>
</div>
<div id="main">
<div class="w3-container">
<div class="w3-bar w3-margin-bottom w3-green">
<label class="w3-bar-item w3-button">
<input type="file" onchange="loadfile(this)">
Upload
</label>
<a class="w3-bar-item w3-button" href="#download" onclick='saveTextAsFile()'>Download</a>
<input id="fileName" class="w3-bar-item w3-input" placeholder="Save file as ...">
</div>
<!-- _______________________________________________Code_Editor____________________________________________ -->
<textarea id="code" name="code"><!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://www.w3schools.com/lib/w3.js"></script>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<body>
</body>
<html></textarea>
<!-- ___________________________________________________________________________________________________ -->
<!--
<div class="w3-container hide">
<iframe id="preview"></iframe>
</div>
-->
<button class="w3-button w3-red" onclick="goToBtnFunc()" id="goToBtn" title="Go to top">Top</button>
</div>
</div>
<script>
var delay;
// Initialize CodeMirror editor
var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
mode: 'text/html',
tabMode: 'indent',
lineNumbers: true,
lineWrapping: true,
autoCloseTags: true,
matchBrackets: true
});
// Live preview
/* editor.on("change", function () {
clearTimeout(delay);
delay = setTimeout(updatePreview, 300);
});
setTimeout(updatePreview, 300);
*/
function saveTextAsFile() {
var fileName = document.getElementById("fileName").value;
if (fileName !== "") {
var textToWrite = document.getElementById("code").value;
var textFileAsBlob = new Blob([textToWrite], {
type: 'text/html'
});
var downloadLink = document.createElement("a");
downloadLink.download = fileName;
downloadLink.innerHTML = "Download File";
if (window.webkitURL != null) {
// Chrome allows the link to be clicked
// without actually adding it to the DOM.
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
}
else {
// Firefox requires the link to be added to the DOM
// before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
downloadLink.click();
}
else {
alert("Please type a file name.")
}
}
function loadfile(input) {
var reader = new FileReader();
reader.onload = function (e) {
editor.setValue(e.target.result);
}
reader.readAsText(input.files[0]);
document.getElementById("fileName").value = input.files[0].name
}
var sideBarBtn = document.getElementById("sideBarBtn");
var sideBarBtnState = false; // false == open
function SideBar() {
// if open close
if (sideBarBtnState == true) {
sideBarClose();
sideBarBtnState = false;
}
// if closed open
else {
sideBarOpen();
sideBarBtnState = true;
}
}
function sideBarOpen() {
document.getElementById("main").style.marginLeft = "15.9%";
document.getElementById("Sidebar").style.width = "15%";
document.getElementById("Sidebar").style.display = "block";
}
function sideBarClose() {
document.getElementById("main").style.marginLeft = "0%";
document.getElementById("Sidebar").style.display = "none";
}
window.onscroll = function () {scrollFunc()};
var goToBtn = document.getElementById("goToBtn");
var goToBtnState = false;
// When the user scrolls down 20px from the top of the document, show the button
function scrollFunc() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
goToBtn.style.display = "block";
goToBtn.innerHTML = "▲";
goToBtnState = false;
}
else if (document.body.scrollTop < 20 || document.documentElement.scrollTop < 20) {
goToBtn.innerHTML = "▼";
goToBtnState = true;
}
else {
document.getElementById("goToBtn").style.display = "none";
}
}
// When the user clicks on the button, scroll to the top of the document
function goToBtnFunc() {
if (goToBtnState === false) {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
} else {
document.body.scrollTop = $("body").height();
document.documentElement.scrollTop = $("body").height();
}
}
</script>
</body>
</html>