-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.js
343 lines (295 loc) · 10.3 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
let editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/html");
editor.setOptions({
fontSize: "10pt",
showPrintMargin: false,
highlightActiveLine: false
});
// ---------------------------------------------------------------------------
let initialContent =
`<!-- This is an example -->
<!-- You can freely modify this code -->
<!-- Or create your own -->
<!-- By pressing the "+" icon -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Cube</title>
<style>
body, html {
height: 100%;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: #282c34;
}
.scene {
width: 150px;
height: 150px;
perspective: 600px;
}
.cube {
width: 100%;
height: 100%;
position: relative;
transform-style: preserve-3d;
transform: rotateX(30deg) rotateY(30deg);
animation: rotate 10s infinite linear;
}
.cube div {
position: absolute;
width: 150px;
height: 150px;
background: rgba(255, 255, 255, 0.9);
border: 2px solid #000;
box-sizing: border-box;
}
.front { transform: translateZ(75px); }
.back { transform: rotateY(180deg) translateZ(75px); }
.right { transform: rotateY(90deg) translateZ(75px); }
.left { transform: rotateY(-90deg) translateZ(75px); }
.top { transform: rotateX(90deg) translateZ(75px); }
.bottom { transform: rotateX(-90deg) translateZ(75px); }
@keyframes rotate {
0% { transform: rotateX(0deg) rotateY(0deg); }
100% { transform: rotateX(360deg) rotateY(360deg); }
}
</style>
</head>
<body>
<div class="scene">
<div class="cube">
<div class="front"></div>
<div class="back"></div>
<div class="right"></div>
<div class="left"></div>
<div class="top"></div>
<div class="bottom"></div>
</div>
</div>
</body>
</html>`;
// ---------------------------------------------------------------------------
let tabs = [{ id: 1, content: initialContent }];
let currentTab = 1;
const preview = document.getElementById('preview');
const runBtn = document.getElementById('runBtn');
const addTabBtn = document.getElementById('addTabBtn');
const startOverBtn = document.getElementById('startOverBtn');
const startFromScratchBtn = document.getElementById('startFromScratchBtn');
const fullScreenBtn = document.getElementById('fullScreenBtn');
const tabContainer = document.getElementById('tabContainer');
const downloadBtn = document.getElementById('downloadBtn');
const focusModeBtn = document.getElementById('focusModeBtn');
const darkModeToggle = document.getElementById('darkModeToggle');
const bgColorPicker = document.getElementById('bgColorPicker');
const resetColorBtn = document.getElementById('resetColorBtn');
// Function to run the code
function runCode() {
preview.srcdoc = editor.getValue();
}
// Switch tab and save content of the previous tab
function switchToTab(tabId) {
tabs[currentTab - 1].content = editor.getValue();
currentTab = tabId;
updateTabsUI();
editor.setValue(tabs[currentTab - 1].content, -1);
runCode();
}
// Update the tab UI to mark the active tab
function updateTabsUI() {
document.querySelectorAll('.tab').forEach(tab => {
tab.classList.toggle('active', parseInt(tab.dataset.tab) === currentTab);
});
}
// Add a new tab
function addNewTab() {
const newTabId = tabs.length + 1;
tabs.push({ id: newTabId, content:
`<!-- Tab ${newTabId} Content -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tab ${newTabId}</title>
</head>
<body>
</body>
</html>
`});
const newTab = createTabElement(newTabId);
tabContainer.insertBefore(newTab, addTabBtn);
switchToTab(newTabId);
}
// Create a new tab element
function createTabElement(tabId) {
const newTab = document.createElement('button');
newTab.className = 'tab';
newTab.dataset.tab = tabId;
newTab.innerHTML = `Tab ${tabId} <span class="tab-close">×</span>`;
return newTab;
}
// Handle tab click and close events
function handleTabClick(event) {
const tabElement = event.target.closest('.tab');
if (tabElement) {
if (event.target.classList.contains('tab-close')) {
closeTab(tabElement);
} else {
switchToTab(parseInt(tabElement.dataset.tab));
}
}
}
// Close a tab
function closeTab(tabElement) {
const tabId = parseInt(tabElement.dataset.tab);
// Ensure there's at least one tab left before closing
if (tabs.length > 1) {
// Remove the tab from the array
tabs = tabs.filter(tab => tab.id !== tabId);
// Remove the tab element from the DOM
tabElement.remove();
// Switch to the first tab after removing the current one
if (currentTab === tabId) {
switchToTab(tabs[0].id);
}
// Update the tab numbering after a tab is closed
updateTabNumbers();
}
}
// Update tab numbers after closing a tab
function updateTabNumbers() {
const tabElements = document.querySelectorAll('.tab');
tabElements.forEach((tab, index) => {
tab.textContent = `Tab ${index + 1}`;
tab.dataset.tab = index + 1;
tabs[index].id = index + 1;
// Re-add the close button to each tab after renumbering
const closeButton = document.createElement('span');
closeButton.classList.add('tab-close');
closeButton.innerHTML = '×';
closeButton.addEventListener('click', (e) => {
e.stopPropagation(); // Stop event bubbling
closeTab(tab);
});
tab.appendChild(closeButton);
});
}
// Reset the editor to its initial state
function startOver() {
tabs = [{ id: 1, content: initialContent }];
currentTab = 1;
tabContainer.innerHTML = '<button class="tab active" data-tab="1">Tab 1 <span class="tab-close">×</span></button>';
tabContainer.appendChild(addTabBtn);
editor.setValue(tabs[0].content, -1);
runCode();
}
function startFromScratch() {
// Reset the tabs array with a new, blank tab
tabs = [{
id: 1,
content: '<!-- Start your code here -->'
}];
// Reset the current tab to the first one
currentTab = 1;
// Update the tab container UI
tabContainer.innerHTML = `
<button class="tab active" data-tab="1">Tab 1 <span class="tab-close">×</span></button>
<button id="addTabBtn">+</button>
`;
// Re-assign the event listener to the new Add Tab button
const newAddTabBtn = document.getElementById('addTabBtn');
newAddTabBtn.addEventListener('click', addNewTab);
// Set the editor content to the new blank template
editor.setValue(tabs[0].content, -1);
// Run the code (which will clear the preview since it's empty)
runCode();
}
// Toggle full screen for preview
function toggleFullScreen() {
if (!document.fullscreenElement) {
preview.requestFullscreen().catch(err => console.error(`Error: ${err.message}`));
preview.classList.add('fullscreen-preview');
} else {
document.exitFullscreen();
}
}
// Focus mode toggle
function toggleFocusMode() {
document.body.classList.toggle('focus-mode');
editor.resize();
}
// Full focus mode toggle logic
function toggleFullFocus() {
const isFullFocus = document.body.classList.toggle('focus-mode');
focusModeBtn.innerText = isFullFocus ? 'Exit Full Focus Mode' : 'Full Focus Mode';
}
// Download the code
function downloadCode() {
const code = editor.getValue();
const blob = new Blob([code], { type: 'text/html' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'your_code.html';
link.click();
URL.revokeObjectURL(link.href);
}
// Dark mode toggle
function toggleDarkMode() {
document.body.classList.toggle('dark-mode');
localStorage.setItem('darkMode', document.body.classList.contains('dark-mode') ? 'enabled' : 'disabled');
}
// Background color functionality
function handleBackgroundColor() {
const savedColor = localStorage.getItem('backgroundColor');
if (savedColor) {
document.body.style.backgroundColor = savedColor;
bgColorPicker.value = savedColor;
}
}
bgColorPicker.addEventListener('input', event => {
const color = event.target.value;
document.body.style.backgroundColor = color;
localStorage.setItem('backgroundColor', color);
});
resetColorBtn.addEventListener('click', () => {
document.body.style.backgroundColor = '';
localStorage.removeItem('backgroundColor');
});
// Event Listeners
runBtn.addEventListener ('click', runCode);
addTabBtn.addEventListener ('click', addNewTab);
startOverBtn.addEventListener ('click', startOver);
startFromScratchBtn.addEventListener('click', startFromScratch);
fullScreenBtn.addEventListener ('click', toggleFullScreen);
tabContainer.addEventListener ('click', handleTabClick);
downloadBtn.addEventListener ('click', downloadCode);
focusModeBtn.addEventListener ('click', toggleFullFocus);
darkModeToggle.addEventListener ('click', toggleDarkMode);
// Load saved settings
window.addEventListener('DOMContentLoaded', () => {
if (localStorage.getItem('darkMode') === 'enabled') {
document.body.classList.add('dark-mode');
}
handleBackgroundColor();
runCode();
});
// Handle full-screen changes
document.addEventListener('fullscreenchange', () => {
if (!document.fullscreenElement) {
preview.classList.remove('fullscreen-preview');
}
});
// Initialize the editor with the first tab content
editor.setValue(tabs[0].content, -1);
// Alerts the user when you try to leave
window.addEventListener('beforeunload', function (e) {
const confirmationMessage = "The changes you made may not be saved.";
e.preventDefault();
e.returnValue = confirmationMessage;
return confirmationMessage
});