Example:
I have a rich text editor to edit a letter body.
With a button, I want to insert a paragraph into the rich text editor at cursor position.
I didn’t find any component action to do this. Is it possible to add it?
If not, is there a way to do that in js?
BTW, what component is the weweb rich text editor using?
I thought of a variable that stores the position of the cursor you clicked (I believe that in JS you can do this) and then create an action that would place the default text in the position of the variable (and then it would be a copy and paste).
You can try to use WeWeb’s AI to try, via JavaScript, to capture the cursor position within the Rich Text…
There’s no way to get the cursor position.
And even if it was possible, the position would be in a HTML text…
But ALL the rich text editor have a insertText or even insertHTML function available.
The clue is doing that inside weweb.
function insertTextIntoRichTextInput(text) {
const richTextInput = document.querySelector(‘[contenteditable=“true”]’);
if (!richTextInput) {
alert('Rich Text Input element not found');
return;
}
// Insert the text at the cursor position or at the end if no selection
const selection = window.getSelection();
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
range.deleteContents();
range.insertNode(document.createTextNode(text));
} else {
richTextInput.appendChild(document.createTextNode(text));
}
// Trigger an input event to ensure WeWeb recognizes the change
richTextInput.dispatchEvent(new Event('input', { bubbles: true }));
alert('Text inserted successfully');
}
insertTextIntoRichTextInput(‘Hello World’);
Hi, that’s weird. It should work with that code. You can set it up a JS action on click of a button. Can you send me the link to your project in private, please? Are you using the code like this?
You need to execute the function insertTextIntoRichTextInput(‘Hello World’); at the end of the code in the JS action. I’ve done it now and it seems to work. I don’t know which double quote syntax error you’re talking about, JS is fine.