-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
268 lines (224 loc) · 9.31 KB
/
script.js
File metadata and controls
268 lines (224 loc) · 9.31 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
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
const tabGroup = document.querySelector('.tab-group');
const newTabButton = document.querySelector('.new-tab-button');
const codeEditor = document.getElementById('code-editor');
const livePreview = document.getElementById('live-preview');
const deleteTabButton = document.querySelector('.delete-tab');
const renameTabButton = document.querySelector('.rename-tab');
const copyContentButton = document.querySelector('.copy-content');
const downloadHTMLButton = document.querySelector('.download-html');
const resizer = document.querySelector('.resizer');
const panel1 = document.getElementById('panel1');
const panel2 = document.getElementById('panel2');
let tabCount = 1;
let tabs = [{ name: 'index', content: '' }]; // Store tab names and content
// Function to create a new tab
function createTab(tabName) {
if (tabs.length >= 5) {
alert("Maximum 5 tabs are allowed.");
return null;
}
const tabButton = document.createElement('button');
tabButton.classList.add('tab');
tabButton.dataset.tab = tabName;
tabButton.textContent = tabName;
tabButton.addEventListener('click', () => {
switchTab(tabName);
});
return tabButton;
}
// Function to switch to a tab
function switchTab(tabName) {
// Deactivate current active tab
document.querySelector('.tab.active').classList.remove('active');
// Activate the clicked tab
const tabButtons = document.querySelectorAll('.tab');
tabButtons.forEach(button => {
if (button.dataset.tab === tabName) {
button.classList.add('active');
}
});
// Load the content of the tab into the editor
const tab = tabs.find(tab => tab.name === tabName);
codeEditor.value = tab.content;
updatePreview();
}
// Function to add a new tab
function addTab() {
if (tabs.length >= 5) {
alert("Maximum 5 tabs are allowed.");
return;
}
tabCount++;
const tabName = `index${tabCount > 2 ? tabCount - 1 : ''}`;
if (tabs.find(tab => tab.name === tabName)) {
alert("Tab with this name already exists.");
return;
}
const newTab = { name: tabName, content: '' };
tabs.push(newTab);
const tabButton = createTab(tabName);
if (tabButton) {
tabGroup.insertBefore(tabButton, newTabButton);
switchTab(tabName);
}
}
// Function to delete a tab
function deleteTab() {
if (tabs.length <= 1) {
alert("Cannot delete the initial 'index' tab.");
return;
}
const activeTabName = document.querySelector('.tab.active').dataset.tab;
if (confirm(`Are you sure you want to delete tab "${activeTabName}"?`)) {
// Remove the tab from the array
tabs = tabs.filter(tab => tab.name !== activeTabName);
// Remove the tab button from the UI
const tabButtons = document.querySelectorAll('.tab');
tabButtons.forEach(button => {
if (button.dataset.tab === activeTabName) {
button.remove();
}
});
// Switch to the first tab
switchTab(tabs[0].name);
}
}
// Function to rename a tab
function renameTab() {
const activeTabName = document.querySelector('.tab.active').dataset.tab;
const newName = prompt("Enter new name for the tab:", activeTabName);
if (newName && newName !== activeTabName) {
if (tabs.some(tab => tab.name === newName)) {
alert("Tab with this name already exists.");
return;
}
// Update the tab name in the array
const tab = tabs.find(tab => tab.name === activeTabName);
tab.name = newName;
// Update the tab button text in the UI
const tabButtons = document.querySelectorAll('.tab');
tabButtons.forEach(button => {
if (button.dataset.tab === activeTabName) {
button.textContent = newName;
button.dataset.tab = newName;
}
});
}
}
// Function to copy content to clipboard
function copyContent() {
const content = codeEditor.value;
navigator.clipboard.writeText(content)
.then(() => alert('Content copied to clipboard!'))
.catch(err => console.error('Failed to copy: ', err));
}
// Function to download HTML
function downloadHTML() {
const content = codeEditor.value;
const filename = document.querySelector('.tab.active').dataset.tab + '.html';
const blob = new Blob([content], { type: 'text/html' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
// Function to update the live preview
function updatePreview() {
const content = codeEditor.value;
livePreview.contentDocument.body.innerHTML = content;
const activeTabName = document.querySelector('.tab.active').dataset.tab;
const tab = tabs.find(tab => tab.name === activeTabName);
tab.content = content;
}
// Event listeners
newTabButton.addEventListener('click', addTab);
deleteTabButton.addEventListener('click', deleteTab);
renameTabButton.addEventListener('click', renameTab);
copyContentButton.addEventListener('click', copyContent);
downloadHTMLButton.addEventListener('click', downloadHTML);
codeEditor.addEventListener('input', updatePreview);
// script.js (Resize Functionality - Refined)
// const panel1 = document.getElementById('panel1');
// const panel2 = document.getElementById('panel2');
// const resizer = document.querySelector('.resizer');
const container = document.querySelector('.container'); // Get the container
let originalX = 0;
let originalWidth1 = 0;
let originalWidth2 = 0;
let maxWidth1 = 0;
let minWidth1 = 50; // Minimum width for panel1 (adjust as needed)
resizer.addEventListener('mousedown', function (e) {
originalX = e.pageX;
originalWidth1 = panel1.offsetWidth;
originalWidth2 = panel2.offsetWidth;
maxWidth1 = container.offsetWidth - minWidth1; // Maximum width for panel1
panel1.classList.add('no-select');
panel2.classList.add('no-select');
document.addEventListener('mousemove', resize);
document.addEventListener('mouseup', stopResize);
});
function resize(e) {
let widthChange = e.pageX - originalX;
let newWidth1 = originalWidth1 + widthChange;
// Constrain the width of panel1
if (newWidth1 < minWidth1) {
newWidth1 = minWidth1;
} else if (newWidth1 > maxWidth1) {
newWidth1 = maxWidth1;
}
panel1.style.flexBasis = newWidth1 + 'px';
panel2.style.flexBasis = (container.offsetWidth - newWidth1) + 'px'; // Make panel2 adjust accordingly
updateDimensions(); // Update the dimensions display during resize
}
function stopResize() {
panel1.classList.remove('no-select');
panel2.classList.remove('no-select');
document.removeEventListener('mousemove', resize);
document.removeEventListener('mouseup', stopResize);
}
// Initialize - Select the first tab
switchTab('index');
//... (previous code) ...
const screenIcon = document.getElementById('screen-icon');
const widthDisplay = document.getElementById('width');
const heightDisplay = document.getElementById('height');
// Function to update dimensions display
function updateDimensions() {
const width = livePreview.offsetWidth;
const height = livePreview.offsetHeight;
widthDisplay.textContent = width;
heightDisplay.textContent = height;
// Update screen size icon
if (width < 480) {
screenIcon.className = 'fas fa-mobile-alt'; // Small mobile
} else if (width < 768) {
screenIcon.className = 'fas fa-tablet-alt'; // Tablet
} else if (width < 992) {
screenIcon.className = 'fas fa-laptop'; // Laptop
} else {
screenIcon.className = 'fas fa-desktop'; // Desktop / Wide screen
}
}
// Call updateDimensions on initial load and when the preview is updated.
updatePreview(); // Call it once on page load to initialize
codeEditor.addEventListener('input', () => {
updatePreview();
updateDimensions();
});
// Also update dimensions when the window resizes (in case the user resizes the whole browser window)
window.addEventListener('resize', updateDimensions);
// Modify the updatePreview function
function updatePreview() {
const content = codeEditor.value;
livePreview.contentDocument.body.innerHTML = content;
const activeTabName = document.querySelector('.tab.active').dataset.tab;
const tab = tabs.find(tab => tab.name === activeTabName);
tab.content = content;
// Update dimensions after the content has been rendered in the iframe.
setTimeout(updateDimensions, 0); // Use setTimeout to ensure iframe is updated.
}
//... (rest of your previous code) ...