-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-editor.html
More file actions
198 lines (178 loc) · 5.79 KB
/
code-editor.html
File metadata and controls
198 lines (178 loc) · 5.79 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>HTML Viewer | tools.eliana.lol</title>
<link rel="stylesheet" href="global.css" />
<link rel="icon" type="image/x-icon" href="favicon.svg" />
<style>
/* Specific Tool UI */
.editor-container {
display: flex;
flex-direction: column;
gap: 10px;
margin-top: 20px;
}
#editor {
height: 350px;
border: 1px solid var(--puny);
}
iframe#preview {
width: 100%;
height: 350px;
background: white;
border: 1px solid var(--puny);
margin-top: 10px;
}
.toolbar {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 10px;
}
.stats {
display: flex;
justify-content: space-between;
margin-top: 5px;
}
.kbd {
font-size: 0.7rem;
color: var(--puny);
border: 1px solid var(--puny);
padding: 1px 3px;
border-radius: 3px;
}
</style>
</head>
<body>
<nav>
<div class="left">
<a href="index.html">home</a> | <a href="extra.html">extra</a> |
<a href="credits.html">credits</a> |
<a href="changelog.html">logs</a>
</div>
<div class="right">
<button id="theme-toggle">[theme]</button> |
<a href="https://eliana.lol">site</a>
</div>
</nav>
<main>
<h1>HTML Viewer</h1>
<p class="puny">
Live editor & previewer. <span class="kbd">Ctrl+Enter</span> to manual
refresh.
</p>
<div class="toolbar">
<button onclick="formatHTML()">✨ Format</button>
<button onclick="previewInTab()">📄 New Tab</button>
<button onclick="loadSample()">📝 Sample</button>
<button onclick="clearEditor()" style="border-color: var(--accent)">
🗑️ Clear
</button>
</div>
<div class="editor-container">
<div id="editor"></div>
<div class="stats puny">
<span
>Line: <span id="line">1</span>, Col:
<span id="column">1</span></span
>
<label style="cursor: pointer"
><input type="checkbox" id="autoPreview" checked />
Auto-update</label
>
</div>
<iframe id="preview"></iframe>
</div>
</main>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.32.0/ace.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.11/beautify-html.min.js"></script>
<script>
const html = document.documentElement;
const toggle = document.getElementById('theme-toggle');
const saved =
localStorage.getItem('theme') ||
(window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light');
html.setAttribute('data-theme', saved);
if (toggle) toggle.innerText = saved === 'dark' ? '[light]' : '[dark]';
if (toggle) {
toggle.onclick = () => {
const now = html.getAttribute('data-theme');
const next = now === 'dark' ? 'light' : 'dark';
html.setAttribute('data-theme', next);
localStorage.setItem('theme', next);
toggle.innerText = next === 'dark' ? '[light]' : '[dark]';
};
}
</script>
<script>
function initEditor() {
const editor = ace.edit('editor');
editor.setTheme('ace/theme/monokai');
editor.session.setMode('ace/mode/html');
editor.setOptions({
fontSize: '14px',
showPrintMargin: false,
wrap: true,
tabSize: 2,
});
const defaultHTML = `<!DOCTYPE html>\n<html>\n<body>\n <h1>Hello World</h1>\n <p>Ready to edit.</p>\n</body>\n</html>`;
editor.setValue(defaultHTML, -1);
window.aceEditor = editor;
editor.session.selection.on('changeCursor', () => {
const cursor = editor.getCursorPosition();
document.getElementById('line').textContent = cursor.row + 1;
document.getElementById('column').textContent = cursor.column + 1;
});
let previewTimeout;
editor.session.on('change', () => {
if (document.getElementById('autoPreview').checked) {
clearTimeout(previewTimeout);
previewTimeout = setTimeout(updatePreview, 500);
}
});
updatePreview();
}
function updatePreview() {
const code = window.aceEditor.getValue();
const preview = document.getElementById('preview');
const doc = preview.contentDocument || preview.contentWindow.document;
doc.open();
doc.write(code);
doc.close();
}
function formatHTML() {
const htmlCode = window.aceEditor.getValue();
const formatted = html_beautify(htmlCode, { indent_size: 2 });
window.aceEditor.setValue(formatted, -1);
}
function previewInTab() {
const code = window.aceEditor.getValue();
const newTab = window.open();
newTab.document.write(code);
newTab.document.close();
}
function clearEditor() {
if (confirm('Wipe the editor?')) {
window.aceEditor.setValue('', -1);
updatePreview();
}
}
function loadSample() {
const sample = '<h1>Monospace Test</h1><p>The layout is working.</p>';
window.aceEditor.setValue(sample, -1);
updatePreview();
}
if (typeof ace !== 'undefined') initEditor();
// Keyboard Shortcut (Ctrl/Cmd + Enter)
document.addEventListener('keydown', (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
updatePreview();
}
});
</script>
</body>
</html>