Skip to content

Commit 5984736

Browse files
authored
- track and sync input field changes from preview to code editor
1 parent 528226d commit 5984736

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

tools/code_editor.html

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,23 @@
115115
</div>
116116
</div>
117117
<script>
118-
// JavaScript updates for toggle functionality including dynamic arrow direction
119118
const container = document.getElementById('container');
120119
const toggle = document.getElementById('toggle');
121120
const iframe = document.getElementById('preview');
122121
const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
123-
122+
124123
function compressAndBase64Encode(str) {
125124
return btoa(String.fromCharCode.apply(null, pako.deflate(str))).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
126125
}
127-
126+
128127
function base64DecodeAndDecompress(base64EncodedData) {
129128
const adjustedData = base64EncodedData.replace(/-/g, '+').replace(/_/g, '/');
130129
const decodedData = atob(adjustedData);
131130
const charData = decodedData.split('').map(c => c.charCodeAt(0));
132131
const binData = new Uint8Array(charData);
133132
return pako.inflate(binData, { to: 'string' });
134133
}
135-
134+
136135
function updateURL() {
137136
console.log('url updated');
138137
const html = compressAndBase64Encode(document.getElementById('htmlCode').value);
@@ -141,41 +140,69 @@
141140
history.replaceState(null, '', `?html=${html}&css=${css}&js=${js}`);
142141
decompressAndInject();
143142
}
144-
143+
145144
function decompressAndInject() {
146145
let urlParams = new URLSearchParams(window.location.search);
147146
let html = urlParams.get('html') ? sanitizeEncodedValue(urlParams.get('html')) : '';
148147
let css = urlParams.get('css') ? sanitizeEncodedValue(urlParams.get('css')) : '';
149148
let js = urlParams.get('js') ? sanitizeEncodedValue(urlParams.get('js')) : '';
150-
149+
151150
html = html ? base64DecodeAndDecompress(html) : '';
152151
css = css ? base64DecodeAndDecompress(css) : '';
153152
js = js ? base64DecodeAndDecompress(js) : '';
154-
153+
155154
if (html) document.getElementById('htmlCode').value = html;
156155
if (css) document.getElementById('cssCode').value = css;
157156
if (js) document.getElementById('jsCode').value = js;
158-
157+
159158
iframeDoc.open();
160159
iframeDoc.write(`
161-
<html>
162-
<head><style>${css}</style></head>
163-
<body>${html}<script>${js}<\/script></body>
164-
</html>
165-
`);
160+
<html>
161+
<head>
162+
<style>${css}</style>
163+
</head>
164+
<body>${html}<script>${js}</script>
165+
<script>
166+
document.addEventListener('input', function(e) {
167+
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.tagName === 'SELECT') {
168+
window.parent.postMessage({
169+
type: 'inputChanged',
170+
html: document.body.innerHTML
171+
}, '*');
172+
}
173+
});
174+
document.addEventListener('change', function(e) {
175+
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.tagName === 'SELECT') {
176+
window.parent.postMessage({
177+
type: 'inputChanged',
178+
html: document.body.innerHTML
179+
}, '*');
180+
}
181+
});
182+
</script>
183+
</body>
184+
</html>
185+
`);
166186
iframeDoc.close();
167187
}
168-
188+
169189
function sanitizeEncodedValue(encodedValue) {
170190
return encodedValue.replace(/[\s'"]+$/g, '');
171191
}
172-
192+
173193
document.getElementById('htmlCode').addEventListener('input', updateURL);
174194
document.getElementById('cssCode').addEventListener('input', updateURL);
175195
document.getElementById('jsCode').addEventListener('input', updateURL);
176-
196+
197+
window.addEventListener('message', function(event) {
198+
if (event.data && event.data.type === 'inputChanged') {
199+
document.getElementById('htmlCode').value = event.data.html;
200+
updateURL();
201+
}
202+
});
203+
177204
decompressAndInject();
178-
205+
179206
toggle.addEventListener('click', () => {
180207
const codeEditor = document.getElementById('codeEditor');
181208
const preview = document.getElementById('preview');
@@ -192,4 +219,4 @@
192219
</script>
193220
<script src="../logo.js"></script>
194221
</body>
195-
</html>
222+
</html>

0 commit comments

Comments
 (0)