Saves and restores selection positions using DOM node paths or character offsets within a container.
Access:
app.getService('offset')
App code:
const offset = app.getService('offset');
Plugin:
const offset = this.app.getService('offset');
get#
Returns the current selection as node paths and offsets relative to the editor root, or null when outside the editor.
Returns: { isCollapsed: boolean, start: { nodePath: number[], offset: number }, end: { nodePath: number[], offset: number } }|null
const pos = offset.get();
set#
Restores a selection from a position object created by get(), clamping offsets to valid node lengths.
Parameters:
- pos (
number) — marker position (startorend)
Returns: void
offset.set(pos);
getCharOffset#
Returns character offsets for the selection start and end within the given or editor container.
Parameters:
- el (
HTMLElement | string) — DOM element
Returns: { start: number, end: number }|null
offset.getCharOffset(blockEl);
setCharOffset#
Places the selection at the given character start and end offsets by traversing text nodes in the container.
Parameters:
- offset (
any) — offset - el (
HTMLElement | string) — DOM element
Returns: boolean
offset.setCharOffset({ start: 5, end: 10 });