Skip to content

Commit 75d8567

Browse files
authored
- fix
1 parent c4f0340 commit 75d8567

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

tools/code_editor.html

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,49 @@
167167
<script>${js}<\/script>
168168
<script>
169169
(function() {
170+
function captureFormStates() {
171+
const clone = document.body.cloneNode(true);
172+
173+
// Update checkbox and radio states
174+
const inputs = document.querySelectorAll('input[type="checkbox"], input[type="radio"]');
175+
const cloneInputs = clone.querySelectorAll('input[type="checkbox"], input[type="radio"]');
176+
177+
for (let i = 0; i < inputs.length; i++) {
178+
if (inputs[i].checked) {
179+
cloneInputs[i].setAttribute('checked', 'checked');
180+
} else {
181+
cloneInputs[i].removeAttribute('checked');
182+
}
183+
}
184+
185+
// Update text input, textarea and select values
186+
const formElements = document.querySelectorAll('input:not([type="checkbox"]):not([type="radio"]), textarea, select');
187+
const cloneFormElements = clone.querySelectorAll('input:not([type="checkbox"]):not([type="radio"]), textarea, select');
188+
189+
for (let i = 0; i < formElements.length; i++) {
190+
cloneFormElements[i].value = formElements[i].value;
191+
if (formElements[i].tagName === 'SELECT') {
192+
const options = formElements[i].options;
193+
const cloneOptions = cloneFormElements[i].options;
194+
195+
for (let j = 0; j < options.length; j++) {
196+
if (options[j].selected) {
197+
cloneOptions[j].setAttribute('selected', 'selected');
198+
} else {
199+
cloneOptions[j].removeAttribute('selected');
200+
}
201+
}
202+
}
203+
}
204+
205+
return clone.innerHTML;
206+
}
207+
170208
function handleInputChange(e) {
171209
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.tagName === 'SELECT') {
172210
window.parent.postMessage({
173211
type: 'inputChanged',
174-
html: document.body.innerHTML
212+
html: captureFormStates()
175213
}, '*');
176214
}
177215
}

0 commit comments

Comments
 (0)