Custom Tool - Editing content onExec resets selection

2 posts, 0 answers
  1. Andre Beauchamp
    Andre Beauchamp avatar
    14 posts
    Member since:
    Jan 2004

    Posted 07 Oct 2014 Link to this post

    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
  2. Andre Beauchamp
    Andre Beauchamp avatar
    14 posts
    Member since:
    Jan 2004

    Posted 07 Oct 2014 in reply to Andre Beauchamp Link to this post

    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());

  3. Kendo UI is VS 2017 Ready
Back to Top