Selection

This module provides several helpful methods working with browser selection.

findParentTag — looks ahead from selection and finds passed tag with class name

expandToTag — expands selection to the passed tag

findParentTag

Finds parent tag with passed class name

Parameters

String tag's name that will be found
String tag's class name must match this argument

Return value

?HTMLElement found HTML element or null if not found
findParentTag(tagName: string, className?: string): HTMLElement|null

Example

class MyInlineTool { constructor({api}) { this.api = api; } surround(range) { if (range) { const parentAnchor = this.selection.findParentTag('A'); if (parentAnchor) { // found closest 'A' tag that wraps current selection // do something } } } }

expandToTag

Wraps current selection with passed tag

Parameters

HTMLElement HTML element that will wrap the selection

Return value

Method does not return anything

expandToTag(node: HTMLElement): void

Example

class MyInlineTool { constructor({api}) { this.api = api; } surround(range) { if (range) { const parentAnchor = this.api.selection.findParentTag('A'); if (parentAnchor) { // found closest 'A' tag that wraps current selection this.api.selection.expandToTag(parentAnchor); } } } }