-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
142 lines (124 loc) · 4.04 KB
/
script.js
File metadata and controls
142 lines (124 loc) · 4.04 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
// CodeMirror Editors
let htmlEditor, cssEditor, jsEditor;
// Initialize CodeMirror
function initEditors() {
// HTML Editor
htmlEditor = CodeMirror.fromTextArea(document.getElementById('htmlInput'), {
mode: 'xml',
theme: 'dracula',
lineNumbers: true,
autoCloseTags: true,
autoCloseBrackets: true,
lineWrapping: true,
indentUnit: 4,
tabSize: 4,
matchBrackets: true,
autoRefresh: true
});
// CSS Editor
cssEditor = CodeMirror.fromTextArea(document.getElementById('cssInput'), {
mode: 'css',
theme: 'dracula',
lineNumbers: true,
autoCloseBrackets: true,
lineWrapping: true,
indentUnit: 4,
tabSize: 4,
matchBrackets: true
});
// JavaScript Editor
jsEditor = CodeMirror.fromTextArea(document.getElementById('jsInput'), {
mode: 'javascript',
theme: 'dracula',
lineNumbers: true,
autoCloseBrackets: true,
lineWrapping: true,
indentUnit: 4,
tabSize: 4,
matchBrackets: true
});
// Add change event for preview
htmlEditor.on('change', debounceUpdatePreview);
cssEditor.on('change', debounceUpdatePreview);
jsEditor.on('change', debounceUpdatePreview);
// Update cursor position
htmlEditor.on('cursorActivity', updateCodeMirrorCursor);
cssEditor.on('cursorActivity', updateCodeMirrorCursor);
jsEditor.on('cursorActivity', updateCodeMirrorCursor);
}
// Debounce function for preview
function debounceUpdatePreview() {
clearTimeout(previewTimeout);
previewTimeout = setTimeout(updatePreview, 1000);
}
// Update cursor for CodeMirror
function updateCodeMirrorCursor(editor) {
const pos = editor.getCursor();
lineColSpan.textContent = `Ln ${pos.line + 1}, Col ${pos.ch + 1}`;
updateWordCount();
}
// Override updatePreview for CodeMirror
function updatePreview() {
const html = htmlEditor.getValue();
const css = cssEditor.getValue();
const js = jsEditor.getValue();
const combinedCode = `
<!DOCTYPE html>
<html>
<head>
<style>${css}</style>
</head>
<body>
${html}
<script>${js}<\/script>
</body>
</html>
`;
const preview = previewFrame.contentDocument || previewFrame.contentWindow.document;
preview.open();
preview.write(combinedCode);
preview.close();
document.getElementById('fileStatus').textContent = 'Preview Updated';
setTimeout(() => {
document.getElementById('fileStatus').textContent = 'Ready';
}, 2000);
}
// Override save function for CodeMirror
function saveFile() {
const content = {
html: htmlEditor.getValue(),
css: cssEditor.getValue(),
js: jsEditor.getValue(),
timestamp: new Date().toISOString()
};
const blob = new Blob([JSON.stringify(content, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `html-project-${Date.now()}.json`;
a.click();
URL.revokeObjectURL(url);
document.getElementById('fileStatus').textContent = 'Saved!';
setTimeout(() => {
document.getElementById('fileStatus').textContent = 'Ready';
}, 2000);
}
// Initialize when DOM loaded
document.addEventListener('DOMContentLoaded', () => {
initEditors();
setupEventListeners();
updatePreview();
});
// Update word count for CodeMirror
function updateWordCount() {
let text = '';
if (document.querySelector('.tab-btn.active').dataset.tab === 'html') {
text = htmlEditor.getValue();
} else if (document.querySelector('.tab-btn.active').dataset.tab === 'css') {
text = cssEditor.getValue();
} else if (document.querySelector('.tab-btn.active').dataset.tab === 'js') {
text = jsEditor.getValue();
}
const words = text.trim() ? text.trim().split(/\s+/).length : 0;
wordCountSpan.textContent = `${words} words`;
}