This is a migrated thread and some comments may be shown as answers.

Custom Tool - Editing content onExec resets selection

1 Answer 62 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Andre Beauchamp
Top achievements
Rank 1
Andre Beauchamp asked on 07 Oct 2014, 03:38 PM
Hi, I have a custom tool that adds an html element inside the editor's body at the cursor position.
This works great but I wish this element to be unique so that if a user clicks the tool again, it deletes the existing element and then add it again at selection (Cursor position).

My problem is that editing the editor's value also resets the selection.
I tried saving the selection and range into a temporary variable and re-assign them or to copy the editor's selectionRestorePoint but to no avail.

Is there a way to edit the editor's content and also keep the cursor position ?

Thanks

1 Answer, 1 is accepted

Sort by
0
Andre Beauchamp
Top achievements
Rank 1
answered on 07 Oct 2014, 03:55 PM
I found a solution to this issue by changing the order of operations.
Using an identifying class called 'New', I first add the element into the editor's content at cursor's position using 
editor.exec("inserthtml", { value: elemHTML });

Then after insertion, I look for any other not 'New' elements and remove them from the body.
And finally, I remove the 'New' class from the remaining element.
var elemExist = $("<p></p>").html(editor.body.innerHTML);
$(elemExist).find(".MyClass").not(".New").remove();
$(elemExist).find(".MyClass").removeClass("New");
editor.value($(elemExist).html());

Tags
Editor
Asked by
Andre Beauchamp
Top achievements
Rank 1
Answers by
Andre Beauchamp
Top achievements
Rank 1
Share this question
or