-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.js
More file actions
135 lines (116 loc) · 4.41 KB
/
control.js
File metadata and controls
135 lines (116 loc) · 4.41 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
// control.js
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 screenIcon = document.getElementById('screen-icon');
const widthDisplay = document.getElementById('width');
const heightDisplay = document.getElementById('height');
const newTabButton = document.querySelector('.new-tab-button');
const tabGroup = document.querySelector('.tab-group');
// Tab Management Functions
function createTabElement(tab) {
const tabElement = document.createElement('button');
tabElement.classList.add('tab');
tabElement.dataset.tab = tab.name;
tabElement.textContent = tab.name;
tabElement.addEventListener('click', () => {
setActiveTab(tab);
});
return tabElement;
}
function renderTabs() {
tabGroup.innerHTML = '';
tabs.forEach(tab => {
const tabElement = createTabElement(tab);
tabGroup.appendChild(tabElement);
});
const activeTabElement = document.querySelector(`.tab[data-tab="${activeTab.name}"]`);
if (activeTabElement) {
activeTabElement.classList.add('active');
}
}
function setActiveTab(tab) {
activeTab = tab;
codeMirror.setValue(tab.content); // Update CodeMirror
renderTabs();
updatePreview();
}
function addTab() {
if (tabs.length >= 10) {
alert("Maximum 10 files allowed.");
return;
}
const newTabName = `File${tabs.length + 1}`;
const newTab = { name: newTabName, content: '' };
tabs.push(newTab);
setActiveTab(newTab);
}
function deleteTab() {
if (tabs.length <= 1) {
alert("Cannot delete the last tab.");
return;
}
const tabIndex = tabs.indexOf(activeTab);
tabs.splice(tabIndex, 1);
// Set active tab to the previous tab, or the next if deleting the first tab
if (tabIndex > 0) {
setActiveTab(tabs[tabIndex - 1]);
} else {
setActiveTab(tabs[0]);
}
}
function renameTab() {
const newName = prompt("Enter new name for the tab:", activeTab.name);
if (newName) {
// Check for uniqueness (case-insensitive)
const lowerNewName = newName.toLowerCase();
if (tabs.some(tab => tab !== activeTab && tab.name.toLowerCase() === lowerNewName)) {
alert("Tab name already exists. Please choose a different name.");
return;
}
activeTab.name = newName;
renderTabs(); // Re-render to update the tab name in the UI
}
}
// Screen Size Update function
function updateDimensions() {
const livePreview = document.getElementById('live-preview');
const width = livePreview.offsetWidth;
const height = livePreview.offsetHeight;
widthDisplay.textContent = width;
heightDisplay.textContent = height;
if (width < 480) {
screenIcon.className = 'fas fa-mobile-alt';
} else if (width < 768) {
screenIcon.className = 'fas fa-tablet-alt';
} else if (width < 992) {
screenIcon.className = 'fas fa-laptop';
} else {
screenIcon.className = 'fas fa-desktop';
}
}
// Download HTML (basic)
downloadHtmlButton.addEventListener('click', () => {
const content = codeMirror.getValue();
const blob = new Blob([content], { type: 'text/html' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${activeTab.name}.html`; // Use active tab's name
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
// Copy Content
copyContentButton.addEventListener('click', () => {
const content = codeMirror.getValue();
navigator.clipboard.writeText(content)
.then(() => alert('Content copied to clipboard!'))
.catch(err => console.error('Failed to copy: ', err));
});
// Event Listeners
newTabButton.addEventListener('click', addTab);
deleteTabButton.addEventListener('click', deleteTab);
renameTabButton.addEventListener('click', renameTab);