-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
101 lines (74 loc) · 3.02 KB
/
script.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
function saveDataToLocalStorage() {
const htmlCode = document.getElementById("html-code").value;
const cssCode = document.getElementById("css-code").value;
const jsCode = document.getElementById("js-code").value;
localStorage.setItem("savedHTMLCode", htmlCode);
localStorage.setItem("savedCSSCode", cssCode);
localStorage.setItem("savedJSCode", jsCode);
}
function loadSavedData() {
const savedHTMLCode = localStorage.getItem("savedHTMLCode");
const savedCSSCode = localStorage.getItem("savedCSSCode");
const savedJSCode = localStorage.getItem("savedJSCode");
if (savedHTMLCode !== null) {
document.getElementById("html-code").value = savedHTMLCode;
}
if (savedCSSCode !== null) {
document.getElementById("css-code").value = savedCSSCode;
}
if (savedJSCode !== null) {
document.getElementById("js-code").value = savedJSCode;
}
}
function run() {
saveDataToLocalStorage();
let htmlCode = document.getElementById("html-code").value;
let cssCode = document.getElementById("css-code").value;
let jsCode = document.getElementById("js-code").value;
let output = document.getElementById("output");
output.contentDocument.body.innerHTML = htmlCode + "<style>"+cssCode+"</style>";
output.contentWindow.eval(jsCode);
}
function downloadCode(codeType) {
const code = document.getElementById(codeType).value;
const blob = new Blob([code], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
if (codeType === 'html-code') {
link.download = "html-code.txt";
} else if (codeType === 'css-code') {
link.download = "css-code.txt";
} else if (codeType === 'js-code') {
link.download = "js-code.txt";
}
link.click();
URL.revokeObjectURL(url);
}
// JavaScript to toggle the visibility of textareas and adjust their size
function toggleTextarea(codeType) {
const textarea = document.getElementById(codeType);
textarea.classList.toggle('collapsed');
const visibleTextareas = document.querySelectorAll('.code-textarea:not(.collapsed)').length;
const newHeight = 85 / visibleTextareas + '%';
document.querySelectorAll('.code-textarea:not(.collapsed)').forEach(textarea => {
textarea.style.height = newHeight;
});
saveDataToLocalStorage();
}
function loadAndApplyState() {
const codeTypes = ["html-code", "css-code", "js-code"];
for (const codeType of codeTypes) {
const textarea = document.getElementById(codeType);
const isCollapsed = localStorage.getItem(codeType + "-state") === "collapsed";
const isMobile = window.innerWidth < 768;
if ((isMobile && codeType !== "html-code") || isCollapsed) {
textarea.classList.add("collapsed");
}
}
loadSavedData();
run();
}
window.onload = function () {
loadAndApplyState();
};