-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
82 lines (73 loc) · 2.69 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
// Get DOM elements
const htmlCode = document.getElementById('htmlCode');
const cssCode = document.getElementById('cssCode');
const jsCode = document.getElementById('jsCode');
const preview = document.getElementById('preview');
// Function to safely update the preview with proper sandboxing
function updatePreview() {
try {
const previewDoc = preview.contentDocument || preview.contentWindow.document;
// Create a complete HTML document structure
const htmlTemplate = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
/* Reset default styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* User CSS */
${cssCode.value}
</style>
</head>
<body>
${htmlCode.value}
<script>
// Wrap JS in try-catch for error handling
try {
${jsCode.value}
} catch (error) {
console.error('JavaScript Error:', error);
}
</script>
</body>
</html>
`;
// Write the complete document to the iframe
previewDoc.open();
previewDoc.write(htmlTemplate);
previewDoc.close();
// Add error handling for the preview window
preview.contentWindow.onerror = function(msg, url, lineNo, columnNo, error) {
console.error('Preview Error:', msg, 'Line:', lineNo, 'Column:', columnNo);
return false;
};
} catch (error) {
console.error('Preview Update Error:', error);
}
}
// Add input event listeners
htmlCode.addEventListener('input', updatePreview);
cssCode.addEventListener('input', updatePreview);
jsCode.addEventListener('input', updatePreview);
// Initial preview update
updatePreview();
// Add tab support for better code editing
function handleTab(element, event) {
if (event.key === 'Tab') {
event.preventDefault();
const start = element.selectionStart;
const end = element.selectionEnd;
element.value = element.value.substring(0, start) + ' ' + element.value.substring(end);
element.selectionStart = element.selectionEnd = start + 4;
}
}
// Add tab handlers to all code areas
[htmlCode, cssCode, jsCode].forEach(editor => {
editor.addEventListener('keydown', (e) => handleTab(editor, e));
});