Better support for type_text
This commit is contained in:
1 parent
2873da3c84
commit
5277357240
1 file changed
+20
-1
+20
-1
@@ -157,7 +157,26 @@ function typeText(id, text) {
|
|||||||
throw new Error(`Element with mcp-id ${id} not found`);
|
throw new Error(`Element with mcp-id ${id} not found`);
|
||||||
}
|
}
|
||||||
el.focus();
|
el.focus();
|
||||||
el.value = text;
|
|
||||||
|
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
|
// Dispatch input and change events so frameworks (React, Vue, etc.) notice
|
||||||
el.dispatchEvent(new Event("input", { bubbles: true }));
|
el.dispatchEvent(new Event("input", { bubbles: true }));
|
||||||
el.dispatchEvent(new Event("change", { bubbles: true }));
|
el.dispatchEvent(new Event("change", { bubbles: true }));
|
||||||
|
|||||||
Reference in new issue
Block a user