-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
150 lines (133 loc) · 6.15 KB
/
script.js
File metadata and controls
150 lines (133 loc) · 6.15 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
document.addEventListener('DOMContentLoaded', () => {
const bodyEl = document.body;
const htmlCodeEl = document.getElementById('html-code');
const cssCodeEl = document.getElementById('css-code');
const jsCodeEl = document.getElementById('javascript-code');
const outputEl = document.getElementById('output');
const consoleEl = document.getElementById('console');
const editorEl = document.getElementById('editor');
const resizerEl = document.getElementById('resizer');
const leftPaneEl = document.getElementById('left-pane');
const rightPaneEl = document.getElementById('right-pane');
const btnVertical = document.getElementById('btn-vertical');
const btnHorizontal = document.getElementById('btn-horizontal');
const themeToggleBtn = document.getElementById('theme-toggle');
const tabBtns = document.querySelectorAll('.tab-btn');
function debounce(func, delay) {
let timeout;
return function(...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), delay);
};
}
function run() {
const htmlCode = htmlCodeEl.value;
const cssCode = cssCodeEl.value;
const jsCode = jsCodeEl.value;
const fullHtml = `<!DOCTYPE html><html lang="en"><head><style>${cssCode}</style></head><body>${htmlCode}</body></html>`;
outputEl.srcdoc = fullHtml;
consoleEl.innerHTML = '';
setTimeout(() => {
const iframe = outputEl.contentWindow;
iframe.console = {
log: (...args) => {
const logOutput = args.map(arg => JSON.stringify(arg, null, 2)).join(' ');
const logEntry = document.createElement('div');
logEntry.textContent = logOutput;
consoleEl.appendChild(logEntry);
consoleEl.scrollTop = consoleEl.scrollHeight;
},
error: (...args) => {
const errorOutput = args.map(arg => String(arg)).join(' ');
const logEntry = document.createElement('div');
logEntry.style.color = 'red';
logEntry.textContent = `Error: ${errorOutput}`;
consoleEl.appendChild(logEntry);
consoleEl.scrollTop = consoleEl.scrollHeight;
}
};
try { iframe.eval(jsCode); } catch (e) { iframe.console.error(e.message); }
}, 50);
}
function setTheme(theme) {
if (theme === 'light') {
bodyEl.classList.add('light-mode');
themeToggleBtn.innerHTML = "<i class='bx bxs-moon'></i>";
} else {
bodyEl.classList.remove('light-mode');
themeToggleBtn.innerHTML = "<i class='bx bxs-sun'></i>";
}
localStorage.setItem('editor-theme', theme);
}
themeToggleBtn.addEventListener('click', () => {
const currentTheme = bodyEl.classList.contains('light-mode') ? 'light' : 'dark';
const newTheme = currentTheme === 'light' ? 'dark' : 'light';
setTheme(newTheme);
});
function setLayout(layout) {
editorEl.className = 'editor';
editorEl.classList.add(`layout-${layout}`);
btnVertical.classList.toggle('active-view', layout === 'vertical');
btnHorizontal.classList.toggle('active-view', layout === 'horizontal');
localStorage.setItem('editor-layout', layout);
}
btnVertical.addEventListener('click', () => setLayout('vertical'));
btnHorizontal.addEventListener('click', () => setLayout('horizontal'));
resizerEl.addEventListener('mousedown', (e) => {
e.preventDefault();
window.addEventListener('mousemove', handleMouseMove);
window.addEventListener('mouseup', () => { window.removeEventListener('mousemove', handleMouseMove); });
});
function handleMouseMove(e) {
const layout = localStorage.getItem('editor-layout') || 'vertical';
if (layout === 'vertical') {
let newLeftWidth = (e.clientX / editorEl.offsetWidth) * 100;
if (newLeftWidth < 10) newLeftWidth = 10;
if (newLeftWidth > 90) newLeftWidth = 90;
leftPaneEl.style.flexBasis = `${newLeftWidth}%`;
rightPaneEl.style.flexBasis = `${100 - newLeftWidth}%`;
} else {
let newTopHeight = ((e.clientY - editorEl.offsetTop) / editorEl.offsetHeight) * 100;
if (newTopHeight < 10) newTopHeight = 10;
if (newTopHeight > 90) newTopHeight = 90;
leftPaneEl.style.flexBasis = `${newTopHeight}%`;
rightPaneEl.style.flexBasis = `${100 - newTopHeight}%`;
}
}
tabBtns.forEach(btn => {
btn.addEventListener('click', () => {
tabBtns.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
const targetId = btn.dataset.tab;
document.querySelectorAll('.output-pane').forEach(pane => {
pane.classList.toggle('active', pane.id === targetId);
});
});
});
editorEl.addEventListener('click', (e) => {
if (e.target.classList.contains('copy-btn')) {
const targetId = e.target.dataset.target;
const textElement = document.getElementById(targetId);
textElement.select();
navigator.clipboard.writeText(textElement.value)
.then(() => console.log(`${targetId} copied.`))
.catch(err => console.error('Copy failed', err));
}
});
const debouncedRun = debounce(run, 300);
htmlCodeEl.addEventListener('keyup', debouncedRun);
cssCodeEl.addEventListener('keyup', debouncedRun);
jsCodeEl.addEventListener('keyup', debouncedRun);
const savedLayout = localStorage.getItem('editor-layout') || 'vertical';
setLayout(savedLayout);
const savedTheme = localStorage.getItem('editor-theme');
const prefersLight = window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches;
if (savedTheme) {
setTheme(savedTheme);
} else if (prefersLight) {
setTheme('light');
} else {
setTheme('dark');
}
run();
});