Set Rich Text Editor Cursor Position

Hi,

I have a requirement to build an email template designer, where the user will build a template base on a table so the system can automate sending the emails and it will replace with record value, the idea of it, the user will type the email and where it will require to add value, he has a list of fields and he can click, I'm doing

emailTemplateBox.executeCommand("insertText", `{{${id}}}`

to insert text but the problem is sometimes for some reason the cursor is set back to the beginning so that the text will be inserted in the wrong location, check the attached video for more details



Attachment: RichTextEditor_Issue_b9a9d892.rar


3 Replies 1 reply marked as answer

VJ Vinitha Jeyakumar Syncfusion Team August 15, 2022 07:28 AM UTC

Hi Abdul,


Your reported issue can be resolved by saving the cursor location in the blur event and restoring the cursor locating before inserting the HTML using ExecuteCommand. Please check the code below,

Code snippet:
let rangeRange;
let selectionNodeSelection = new NodeSelection();
let saveSelection;
let defaultAPIRichTextEditor = new RichTextEditor({
  showCharCount: true,
  maxLength: 1000,
  blur: blur,
});
defaultAPI.appendTo('#defaultRTE');

function blur() {
  range = selection.getRange(document);
  saveSelection = selection.save(rangedocument);
}
document.getElementById('getVal').onclick = () => {
  saveSelection.restore();
  defaultAPI.executeCommand('insertHTML''inserted an html');
};



Regards,
Vinitha

Marked as answer

AM Abdul Mounem August 15, 2022 11:23 AM UTC

Thanks a lot for the solution, it's working now as expected



VJ Vinitha Jeyakumar Syncfusion Team August 16, 2022 05:25 AM UTC

Hi Abdul,


You are always welcome.

Regards,
Vinitha

Loader.
Up arrow icon