Marker

Temporary DOM markers for saving and restoring selection across DOM mutations and HTML processing.

Access:

app.getService('marker')

App code:

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

Plugin:

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

create#

Builds a span marker element with the configured marker character and optional text-fallback token.

Parameters:

  • pos (number) — which marker to build: 'start' or 'end'
  • options (object) — object with the keys below
    • textFallback (boolean) — append a zero-width text token alongside the marker so its position survives plain-text operations
    • doc (any) — Document to create the element in (defaults to the editor document)

Returns: HTMLElement

marker.create('start', { textFallback: true });

createHtml#

Returns the outerHTML string of a marker element created by create().

Parameters:

  • pos (number) — which marker to build: 'start' or 'end'
  • options (object) — object with the keys below
    • textFallback (boolean) — append a zero-width text token alongside the marker so its position survives plain-text operations
    • doc (any) — Document to create the element in (defaults to the editor document)

Returns: string

marker.createHtml('end');

insert#

Removes existing markers and inserts start/end marker spans around the current selection range.

Parameters:

  • options (object) — object with the keys below
    • textFallback (boolean) — append zero-width text tokens alongside the markers so their position survives plain-text operations
    • doc (any) — Document to create the marker elements in (defaults to the editor document)

Returns: void

marker.insert({ textFallback: true });

save#

Alias for insert() that bookmarks the current selection with marker spans.

Parameters:

  • options (object) — object with the keys below
    • textFallback (boolean) — append zero-width text tokens alongside the markers so their position survives plain-text operations
    • doc (any) — Document to create the marker elements in (defaults to the editor document)

Returns: void

marker.save();

restore#

Finds saved markers, rebuilds the selection range between them, removes the markers, and cleans text tokens.

Returns: void

marker.restore();

find#

Returns start and end marker elements, or a single marker when pos is start or end.

Parameters:

  • pos (number) — marker position (start or end)

Returns: HTMLElement|false|{ start: HTMLElement|false, end: HTMLElement|false }

marker.find('start');

replaceToPlaceholders#

Replaces marker span elements in HTML with ---marker-start--- and ---marker-end--- placeholder strings.

Parameters:

  • html (string) — HTML string

Returns: string

marker.replaceToPlaceholders(html);

replaceToMarkers#

Replaces placeholder strings in HTML with marker span outerHTML.

Parameters:

  • html (string) — HTML string

Returns: string

marker.replaceToMarkers(html);

remove#

Removes start and end marker elements from the editor DOM.

Returns: void

marker.remove();

cleanupTextTokens#

Strips marker text fallback tokens from text nodes in the editable root.

Returns: void

marker.cleanupTextTokens();