-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
238 lines (208 loc) · 6.74 KB
/
index.html
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Live preview/editor with Debounce</title>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/codemirror.min.css"
/>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
height: 100vh;
overflow: hidden;
background-color: #1e1e1e;
color: #ccc;
}
h2 {
margin-top: 10px;
margin-bottom: 5px;
font-size: 18px;
}
.main {
display: flex;
height: 100%;
max-height: 100%;
}
.code {
flex: 40;
height: calc(100%-40px);
display: flex;
flex-direction: column;
justify-content: flex-start;
padding: 10px;
}
.block {
flex: 1;
display: flex;
flex-direction: column;
}
.editor {
flex: 1;
}
.monaco-editor {
border: 1px solid #333;
}
#preview-wrapper {
flex: 60;
height: 100%;
display: flex;
flex-direction: column;
}
#preview {
flex: 1;
background-color: #fff;
}
#hide-editors-link {
padding: 10px;
text-align: right;
}
.hide-editors .code,
.hide-editors #hide-editors-link {
display: none;
}
</style>
</head>
<body class="hide-editors">
<div class="main">
<div class="code">
<div class="block" style="flex: 1">
<h2>HTML</h2>
<div class="editor" id="html-editor"></div>
</div>
<div class="block" style="flex: 1">
<h2>CSS</h2>
<div class="editor" id="css-editor"></div>
</div>
</div>
<aside id="preview-wrapper">
<a id="hide-editors-link" href="#">Open Preview Only</a>
<iframe id="preview"></iframe>
</aside>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.30.1/min/vs/loader.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lz-string/1.4.4/lz-string.min.js"></script>
<script>
// Helper function to encode string in compressed Base64
const encodeCompressedBase64 = (str) => LZString.compressToBase64(str);
// Helper function to decode string from compressed Base64
const decodeCompressedBase64 = (str) =>
LZString.decompressFromBase64(str);
// Helper function to get hash parameters
function getHashParam(param) {
const hash = window.location.hash.substr(1);
const params = new URLSearchParams(hash);
return params.get(param)
? decodeCompressedBase64(params.get(param))
: '';
}
// Helper function to set hash parameters
function setHashParam(param, value) {
const hash = window.location.hash.substr(1);
const params = new URLSearchParams(hash);
params.set(param, encodeCompressedBase64(value));
window.location.hash = params.toString();
}
// Debounce function
function debounce(func, delay) {
let timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => func.apply(this, args), delay);
};
}
// Function to generate preview content
function generatePreviewContent(html, css) {
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Preview</title>
<style>${css}</style>
</head>
<body>
${html}
</body>
</html>
`;
}
const previewIframe = document.getElementById('preview');
const htmlValue = getHashParam('html') || '<h1>Hello World</h1>';
const cssValue = getHashParam('css') || 'h1 { color: blue; }';
// Check if URL has hideEditors parameter in the hash
const hashParams = new URLSearchParams(window.location.hash.substr(1));
if (hashParams.get('hideEditors') === 'true') {
previewIframe.srcdoc = generatePreviewContent(htmlValue, cssValue);
} else {
// Show editors
document.body.classList.remove('hide-editors');
const hideEditorsLink = document.getElementById('hide-editors-link');
// Load Monaco Editor
require.config({
paths: {
vs: 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.30.1/min/vs',
},
});
require(['vs/editor/editor.main'], function () {
const htmlEditor = monaco.editor.create(
document.getElementById('html-editor'),
{
value: htmlValue,
language: 'html',
theme: 'vs-dark',
scrollBeyondLastLine: false,
automaticLayout: false,
minimap: { enabled: false },
}
);
const cssEditor = monaco.editor.create(
document.getElementById('css-editor'),
{
value: cssValue,
language: 'css',
theme: 'vs-dark',
scrollBeyondLastLine: false,
automaticLayout: false,
minimap: { enabled: false },
}
);
// Update iframe preview
const updatePreview = () => {
const html = htmlEditor.getValue();
const css = cssEditor.getValue();
previewIframe.srcdoc = generatePreviewContent(html, css);
};
// Debounced version of updatePreview
const debouncedUpdatePreview = debounce(updatePreview, 300);
// Update hash and preview when text changes
htmlEditor.onDidChangeModelContent(() => {
setHashParam('html', htmlEditor.getValue());
debouncedUpdatePreview();
});
cssEditor.onDidChangeModelContent(() => {
setHashParam('css', cssEditor.getValue());
debouncedUpdatePreview();
});
// Initial preview update
updatePreview();
// Link to open preview only version
hideEditorsLink.addEventListener('click', (event) => {
event.preventDefault();
const hash = window.location.hash.substr(1);
const params = new URLSearchParams(hash);
params.set('hideEditors', 'true');
const newUrl =
window.location.href.split('#')[0] + '#' + params.toString();
window.open(newUrl, '_blank');
});
});
}
</script>
</body>
</html>