Skip to content
On this page

updateContentEditable

This is a helper function to mutate the content/html of a content editable div.

Types

ts
interface SelectionRange {
    start: number;
    end: number;
}

interface UpdateContentEditableOptions = {
  /**
   * Selection range of text to be replaced.
   * Leaving this empty replaces whole text.
   */
  replace?: SelectionRange;
  /**
   * Selection/Caret to be set after updating the content editable div.
   * Leaving this empty sets the caret at the end of the replaced text.
   */
  selectionAfter?: SelectionRange;
  /**
   * Optionally provide logic for updating the content editable div content.
   * Leaving this empty wil use a custom mutate function to update the content editable div.
   * For Range see: https://developer.mozilla.org/en-US/docs/Web/API/Range
   */
  setRange?(range: Range): void;
}

type UpdateContentEditable = (
    /**
     * The agent input field element.
     */
    element: HTMLElement,
    /**
     * The text being inserted into the agent input field.
     */
    insertText: string,
    /**
     * Options to control the insert behaviour.
     */
    options?: UpdateContentEditableOptions
) => void;

Usage example

Example content-editable element with text:

html
<div contenteditable="true" id="input">
    Hello Person, how do you do?</br>
</div>

Replace 'Person' with 'John':

ts
import { updateContentEditable } from '@deepdesk/deepdesk-sdk';

const element = document.getElementById("input");

updateContentEditable(element, "John", {
    replace: { start: 6, end: 12 },
});

Content-editable element state after:

html
<div contenteditable="true" id="input">
    Hello John, how do you do?</br>
</div>

Note that the caret will be placed right after 'John'.