Better support for type_text

This commit is contained in:
moosecrap committed 2026-11-22 17:32:38 -08:00
1 parent 2873da3c84
commit 5277357240
1 file changed
+19
+19
View File
@@ -157,7 +157,26 @@ function typeText(id, text) {
throw new Error(`Element with mcp-id ${id} not found`);
}
el.focus();
if (el.isContentEditable) {
// For contenteditable elements, try to replace the content using a selection and execCommand
// This is more likely to trigger framework state updates (like in Slate.js or Draft.js)
try {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(el);
selection.removeAllRanges();
selection.addRange(range);
document.execCommand('insertText', false, text);
} catch (e) {
// Fallback to innerText if execCommand fails
el.innerText = text;
}
} else {
// Standard input/textarea
el.value = text;
}
// Dispatch input and change events so frameworks (React, Vue, etc.) notice
el.dispatchEvent(new Event("input", { bubbles: true }));
el.dispatchEvent(new Event("change", { bubbles: true }));