-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
166 lines (148 loc) · 5.35 KB
/
script.js
File metadata and controls
166 lines (148 loc) · 5.35 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
// Initialize CodeMirror editors
// Initialize CodeMirror editors with enhanced auto-completion
const htmlEditor = CodeMirror(document.getElementById("html-container"), {
mode: "htmlmixed", // for HTML, CSS, and JS mixed content
lineNumbers: true,
theme: "dracula",
placeholder: "Write HTML code here...",
extraKeys: { "Ctrl-Space": "autocomplete" },
matchBrackets: true, // Auto-close brackets
autoCloseBrackets: true, // Close brackets automatically
autoCloseTags: true, // Auto-close HTML tags
hintOptions: {
schemaInfo: true,
},
lint: true, // Optionally enable linting for validation
htmlMode: true, // Enable HTML-specific features
lineWrapping: true, // Optional for line wrapping
});
const cssEditor = CodeMirror(document.getElementById("css-container"), {
mode: "css",
lineNumbers: true,
theme: "dracula",
placeholder: "Write CSS code here...",
extraKeys: { "Ctrl-Space": "autocomplete" },
matchBrackets: true,
autoCloseBrackets: true,
lint: true, // Optionally enable linting for validation
});
const jsEditor = CodeMirror(document.getElementById("js-container"), {
mode: "javascript",
lineNumbers: true,
theme: "dracula",
placeholder: "Write JavaScript code here...",
extraKeys: { "Ctrl-Space": "autocomplete" },
matchBrackets: true,
autoCloseBrackets: true,
lint: true, // Optionally enable linting for validation
});
// Theme toggle functionality
let isLightMode = false;
// let darkTheme = true;
document.getElementById("theme-toggle-button").addEventListener("click", () => {
isLightMode = !isLightMode;
const newTheme = isLightMode ? "neo" : "dracula";
// const body = document.body;
// Update theme for each editor
htmlEditor.setOption("theme", newTheme);
cssEditor.setOption("theme", newTheme);
jsEditor.setOption("theme", newTheme);
});
function showEditor(editorType) {
// Get all editor containers
const containers = document.querySelectorAll(".container");
// Hide all containers
containers.forEach((container) => container.classList.remove("active"));
// Show the selected container
const editor = document.getElementById(`${editorType}-editor`);
if (editor) {
editor.classList.add("active");
}
// Update button states
const buttons = document.querySelectorAll(".editor-buttons button");
buttons.forEach((button) => button.classList.remove("active"));
const activeButton = document.getElementById(`${editorType}-button`);
if (activeButton) {
activeButton.classList.add("active");
}
}
// Function to update the iframe preview
function runCode() {
const htmlCode = htmlEditor.getValue();
const cssCode = `<style>${cssEditor.getValue()}</style>`;
const jsCode = `<script>${jsEditor.getValue()}</script>`;
const output = htmlCode + cssCode + jsCode;
const iframe = document.getElementById("output");
iframe.srcdoc = output;
}
// Listen to changes in all editors and update the preview automatically
htmlEditor.on("change", runCode);
cssEditor.on("change", runCode);
jsEditor.on("change", runCode);
// Save Code to localStorage
function saveCode() {
localStorage.setItem("htmlCode", htmlEditor.getValue());
localStorage.setItem("cssCode", cssEditor.getValue());
localStorage.setItem("jsCode", jsEditor.getValue());
alert("Code saved successfully!");
}
// Load Code from localStorage
function loadCode() {
htmlEditor.setValue(localStorage.getItem("htmlCode") || "");
cssEditor.setValue(localStorage.getItem("cssCode") || "");
jsEditor.setValue(localStorage.getItem("jsCode") || "");
alert("Code loaded successfully!");
}
// Delete saved code from localStorage
function deleteCode() {
localStorage.removeItem("htmlCode");
localStorage.removeItem("cssCode");
localStorage.removeItem("jsCode");
htmlEditor.setValue("");
cssEditor.setValue("");
jsEditor.setValue("");
alert("Saved code deleted successfully!");
}
// Download Code as files
function downloadCode() {
const download = (filename, text) => {
const link = document.createElement("a");
link.setAttribute(
"href",
"data:text/plain;charset=utf-8," + encodeURIComponent(text)
);
link.setAttribute("download", filename);
link.style.display = "none";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
download("index.html", htmlEditor.getValue());
download("style.css", cssEditor.getValue());
download("script.js", jsEditor.getValue());
}
// Set Preview Size Function
function setPreviewSize(size) {
const iframe = document.getElementById("output");
switch (size) {
case "desktop":
iframe.style.width = "100%";
iframe.style.height = "100svh";
break;
case "tablet":
iframe.style.width = "768px";
iframe.style.height = "100svh";
break;
case "mobile":
iframe.style.width = "375px";
iframe.style.height = "100svh";
break;
}
}
document.getElementById("run-button").addEventListener("click", runCode);
document.getElementById("save-button").addEventListener("click", saveCode);
document.getElementById("load-button").addEventListener("click", loadCode);
document.getElementById("delete-button").addEventListener("click", deleteCode);
document
.getElementById("download-button")
.addEventListener("click", downloadCode);