-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTML-EDITOR.html
More file actions
243 lines (206 loc) · 7.6 KB
/
Copy pathHTML-EDITOR.html
File metadata and controls
243 lines (206 loc) · 7.6 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body style="height: 100vh; margin: 0;"> <!-- Set height of body to 100% of viewport height -->
<button onclick="encodeToData();">Run</button>
<button onclick="openNewTab();">Run In New Tab</button>
<button onclick="formatCode();">Format Code</button>
<button onclick="saveToLocalStorage();">Quick Save</button>
<button onclick="loadFromLocalStorage();">Quick Load</button>
<button onclick="slotToLocal();">Save to Slot</button>
<button onclick="slotFromLocal();">Load from Slot</button>
<button onclick="deleteSlot();">Delete Slot</button>
<button onclick="listSlots();">List slots</button>
<button onclick="copyDATA();">Copy data: URL</button>
<button onclick="downloadCode();">Download Code</button>
<button onclick="resetSlotList();">Reset Slot List</button>
<textarea id="code" spellcheck="false" style="width: 100vw; resize: vertical; height: 100px;" onkeydown="handleTab(event)"></textarea>
<iframe src="" id="frame" style="width: 100vw; height: 50%;"></iframe> <!-- Set height in percentage -->
</body>
<script>
function updateIframeHeight() {
var textareaHeight = document.getElementById('code').scrollHeight;
var buttonHeight = document.getElementsByTagName('button')[0].offsetHeight; // Assuming all buttons have the same height
var buttonPadding = 10; // Adjust this value according to your button padding
var windowHeight = window.innerHeight;
var windowWidth = window.innerWidth;
var textareaPadding = 20; // Adjust this value according to your padding
var totalHeight = textareaHeight + buttonHeight + buttonPadding + textareaPadding;
var remainingHeight = windowHeight - totalHeight;
var minIframeHeight = 100; // Minimum height for the iframe
var iframeHeight = Math.max(remainingHeight, minIframeHeight);
document.getElementById('frame').style.height = iframeHeight + "px";
document.getElementById('frame').style.width = windowWidth + "px";
}
function openNewTab() {
var frameCode = document.getElementById("code").value;
var encodedCode = encodeURIComponent(frameCode);
var dataURL = "data:text/html;charset=utf-8," + encodedCode;
window.open(dataURL, "_blank")
}
function slotToLocal() {
var code = document.getElementById("code").value;
var slot = prompt("Enter the slot name (It will be your load code for advancedLoad)","my_project")
var new_slot = "my_htmledit_" + slot
localStorage.setItem(new_slot, code);
// Update slot list
var slotList = JSON.parse(localStorage.getItem("slotList")) || [];
slotList.push(slot);
localStorage.setItem("slotList", JSON.stringify(slotList));
localStorage.setItem("prevSlot", slot);
alert("Saved into slot " + slot);
}
function slotFromLocal() {
var slot = prompt("Enter your slot name (What you put in for the save code)",localStorage.getItem("prevSlot"))
var new_slot = "my_htmledit_" + slot
var savedCode = localStorage.getItem(new_slot);
if (savedCode !== null) {
document.getElementById("code").value = savedCode;
alert("Loaded from slot " + slot)
} else {
alert("Slot not found");
}
}
function deleteSlot() {
var slot = prompt("Slot to delete","my_project")
var new_slot = "my_htmledit_" + slot
localStorage.removeItem(new_slot);
// Update slot list
var slotList = JSON.parse(localStorage.getItem("slotList")) || [];
var index = slotList.indexOf(slot);
if (index !== -1) {
slotList.splice(index, 1);
localStorage.setItem("slotList", JSON.stringify(slotList));
alert("Deleted slot " + slot);
} else {
alert("Slot not found");
}
}
function listSlots(){
var slotList = JSON.parse(localStorage.getItem("slotList")) || [];
alert(slotList.join(", "));
}
function saveToLocalStorage() {
var code = document.getElementById("code").value;
localStorage.setItem("savedCode", code);
alert("Saved!")
}
function loadFromLocalStorage() {
var savedCode = localStorage.getItem("savedCode");
if (savedCode !== null) {
document.getElementById("code").value = savedCode;
alert("Loaded!")
}
}
function encodeToData() {
var frameCode = document.getElementById("code").value;
var encodedCode = encodeURIComponent(frameCode);
var dataURL = "data:text/html;charset=utf-8," + encodedCode;
document.getElementById('frame').src = dataURL;
}
function copyDATA() {
var frameCode = document.getElementById("code").value;
var encodedCode = encodeURIComponent(frameCode);
var dataURL = "data:text/html;charset=utf-8," + encodedCode;
navigator.clipboard.writeText(dataURL)
}
function resetSlotList(){
var ver = prompt("Are you sure? Type in 'yes' to accept, anything else for no.", "No");
if (ver === "yes"){
localStorage.setItem("slotList", JSON.stringify([]));
alert("Success");
}
else{
alert("Aborted");
}
}
function formatCode() {
var codeTextArea = document.getElementById('code');
var unformattedCode = codeTextArea.value;
// Split code into lines
var lines = unformattedCode.split('\n');
// Indentation variables
var indentation = 0;
var indentString = ' '; // Two spaces for each level of indentation
// Regular expression patterns for detecting opening and closing tags
var openTagPattern = /<[^\/>][^>]*>/;
var closeTagPattern = /<\/[^>]+>/;
// Formatted code string
var formattedCode = '';
// Iterate over each line of the code
lines.forEach(function(line) {
// Trim leading and trailing whitespace
line = line.trim();
// Check if the line contains an opening or closing tag
var isOpenTag = openTagPattern.test(line);
var isCloseTag = closeTagPattern.test(line);
// Adjust indentation based on the tag type
if (isCloseTag) {
indentation--;
}
// Add indentation
for (var i = 0; i < indentation; i++) {
formattedCode += indentString;
}
// Add the line to the formatted code string
formattedCode += line + '\n';
// Adjust indentation for the next line
if (isOpenTag && !isCloseTag) {
indentation++;
}
});
// Update the content of the textarea with the formatted code
codeTextArea.value = formattedCode;
}
function downloadCode() {
var code = document.getElementById("code").value;
var blob = new Blob([code], { type: "text/html" });
var url = URL.createObjectURL(blob);
var a = document.createElement("a");
a.href = url;
a.download = prompt("File Name (without extension)", "code") + ".html";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
function handleTab(event) {
if (event.keyCode === 9) { // Tab key
event.preventDefault();
var textarea = event.target;
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
var tab = " "; // Two spaces for indentation
var content = textarea.value;
textarea.value = content.substring(0, start) + tab + content.substring(end);
textarea.setSelectionRange(start + tab.length, start + tab.length);
}
}
document.addEventListener("keydown", function(event) {
if (event.ctrlKey && event.key === 's') {
event.preventDefault(); // Prevent default browser save action
saveToLocalStorage(); // Call your save function
}
});
document.addEventListener("keydown", function(event) {
if (event.ctrlKey && event.key === 'l') {
event.preventDefault(); // Prevent default browser save action
loadFromLocalStorage(); // Call your save function
}
});
document.addEventListener("keydown", function(event) {
if (event.ctrlKey && event.key === 'r') {
event.preventDefault(); // Prevent default browser save action
encodeToData();
}
});
setInterval(function(){updateIframeHeight()}, 10);
</script>
</html>