-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
176 lines (127 loc) · 4.66 KB
/
script.js
File metadata and controls
176 lines (127 loc) · 4.66 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
document.getElementById('export-html').addEventListener('click', function() {
const htmlCode = htmlEditor.getValue();
downloadFile('code.html', htmlCode);
});
document.getElementById('export-css').addEventListener('click', function() {
const cssCode = cssEditor.getValue();
downloadFile('style.css', cssCode);
});
document.getElementById('export-js').addEventListener('click', function() {
const jsCode = jsEditor.getValue();
downloadFile('script.js', jsCode);
});
document.getElementById('import-html').addEventListener('change', function(event) {
const file = event.target.files[0];
importFile(file, htmlEditor);
});
document.getElementById('import-css').addEventListener('change', function(event) {
const file = event.target.files[0];
importFile(file, cssEditor);
});
document.getElementById('import-js').addEventListener('change', function(event) {
const file = event.target.files[0];
importFile(file, jsEditor);
});
// Functions for downloadFile and importFile would be the same as before
function downloadFile(filename, content) {
const element = document.createElement('a'); // Create a temporary <a> element
const file = new Blob([content], { type: 'text/plain' }); // Create a Blob object with the file content
element.href = URL.createObjectURL(file); // Create a downloadable URL from the Blob
element.download = filename; // Set the filename for download
document.body.appendChild(element); // Append the element to the body
element.click(); // Programmatically click the element to trigger the download
document.body.removeChild(element); // Remove the element from the DOM after download
}
function importFile(file, editor) {
const reader = new FileReader(); // Create a FileReader object
reader.onload = function(e) {
editor.setValue(e.target.result); // Set the content of the file into the editor
};
reader.readAsText(file); // Read the file as text
}
const htmlEditor = CodeMirror.fromTextArea(document.getElementById('html-code'), {
mode: 'htmlmixed',
theme: 'dracula',
lineNumbers: true,
matchBrackets: true,
autoCloseBrackets: true
});
const cssEditor = CodeMirror.fromTextArea(document.getElementById('css-code'), {
mode: 'css',
theme: 'dracula',
lineNumbers: true,
matchBrackets: true,
autoCloseBrackets: true
});
const jsEditor = CodeMirror.fromTextArea(document.getElementById('js-code'), {
mode: 'javascript',
theme: 'dracula',
lineNumbers: true,
matchBrackets: true,
autoCloseBrackets: true
});
function run() {
let htmlcode = htmlEditor.getValue();
let csscode = cssEditor.getValue();
let jscode = jsEditor.getValue();
let iframe = document.getElementById("output");
const blob = new Blob([`
<html>
<head>
<style>
${csscode}
</style>
</head>
<body>
${htmlcode}
<script>
${jscode}
</script>
</body>
</html>
`], { type: 'text/html' });
iframe.src = URL.createObjectURL(blob);
}
// Attach the run function to the CodeMirror change events
function attachChangeListeners() {
htmlEditor.on('change', run);
cssEditor.on('change', run);
jsEditor.on('change', run);
}
// Initialize change listeners
attachChangeListeners();
// Function to store the code in localStorage
function storeCode() {
localStorage.setItem('htmlCode', htmlEditor.getValue());
localStorage.setItem('cssCode', cssEditor.getValue());
localStorage.setItem('jsCode', jsEditor.getValue());
}
// Function to retrieve the code from localStorage
function loadCode() {
const storedHtml = localStorage.getItem('htmlCode') || '';
const storedCss = localStorage.getItem('cssCode') || '';
const storedJs = localStorage.getItem('jsCode') || '';
htmlEditor.setValue(storedHtml);
cssEditor.setValue(storedCss);
jsEditor.setValue(storedJs);
}
// Attach the run function to the CodeMirror change events
function attachChangeListeners() {
htmlEditor.on('change', () => {
run();
storeCode();
});
cssEditor.on('change', () => {
run();
storeCode();
});
jsEditor.on('change', () => {
run();
storeCode();
});
}
// Load code from localStorage on page load
window.onload = function() {
loadCode();
attachChangeListeners();
};