Caret

Places and inspects the caret inside elements, handling inline tags, blocks, links, and non-editable inline blocks.

Access:

app.getService('caret')

App code:

const caret = app.getService('caret');

Plugin:

const caret = this.app.getService('caret');

set#

Collapses the selection to start, end, before, or after the element with tag-specific caret logic and optional block focus.

Parameters:

  • element (HTMLElement | string) — element
  • where (any) — target position: 'start', 'end', 'before', or 'after'
  • options (object) — object with the keys below
    • focusBlock (any) — also apply block-level focus/selection state after placing the caret

Returns: boolean

caret.set(paragraphEl, 'end', { focusBlock: true });

get#

Returns whether the caret is inside the element and at its normalized start or end boundary.

Parameters:

  • element (HTMLElement | string) — element
  • options (object) — object with the keys below
    • shift (number) — number of characters to subtract from the measured start/end offsets before evaluating atStart/atEnd
    • excludeNestedLists (any) — exclude the text length of nested <ul>/<ol> lists when measuring position (list/todo items)

Returns: { inside: boolean, atStart: boolean, atEnd: boolean }

caret.get(blockEl, { shift: 0 });

is#

Returns true when the caret is inside the element at the specified edge (start or end).

Parameters:

  • element (HTMLElement | string) — element
  • edge (string) — edge to test: 'start' or 'end'
  • options (object) — object with the keys below
    • shift (number) — number of characters to subtract from the measured start/end offsets before evaluating the edge
    • excludeNestedLists (any) — exclude the text length of nested <ul>/<ol> lists when measuring position (list/todo items)

Returns: boolean

caret.is(paragraphEl, 'start');