API / @aionbuilders/nabu/extend

TextBehavior

Class Stability: stable Stable contract

The editing of a block's rich text: wraps a Loro text and offers the reactive text and delta, the selection in that text, and insertion, deletion, splitting, marks and links. Text blocks use it through TextBlock; a container with a header (list item, toggle) owns one.

Related guide: Extensions →

Import

TypeScript
import { TextBehavior } from '@aionbuilders/nabu/extend';

Constructor

TypeScript
new TextBehavior(block: Block, container?: LoroText)
Parameters of the constructor
ParameterTypeDescription
blockBlockn/a
containeroptionalLoroTextn/a

Properties

block

TypeScript
block: Block

container

TypeScript
container: LoroText

delta

TypeScript
delta: DeltaOp[]

positions

TypeScript
positions: number

selection

TypeScript
selection: {
    direction: 'none' | 'forward' | 'backward';
    from: number;
    isCollapsed: boolean;
    to: number;
  } | null

text

TypeScript
text: string

wordCount readonly

TypeScript
readonly wordCount: number

Number of words in this text: segmented with Intl.Segmenter in the language of the editor (nabu.lang) when the platform has it, so Chinese or Japanese text counts its words rather than one per sentence; whitespace-separated otherwise. See utils/words.js. Reactive in the text and in nabu.lang.

Methods

absorbs()

TypeScript
absorbs(other: Block): boolean
Parameters of absorbs
ParameterTypeDescription
otherBlockn/a

applyDelta()

TypeScript
applyDelta(data?: Delta<string>[]): void
Parameters of applyDelta
ParameterTypeDefaultDescription
dataoptionalDelta<string>[][]n/a

applyMark()

TypeScript
applyMark(
  markName: string,
  value: any,
  sel: Pick<{
      direction: 'none' | 'forward' | 'backward';
      from: number;
      isCollapsed: boolean;
      to: number;
    }, 'from' | 'isCollapsed' | 'to'> | null
): void
Parameters of applyMark
ParameterTypeDescription
markNamestringn/a
valueanyn/a
selPick<{ direction: 'none' | 'forward' | 'backward'; from: number; isCollapsed: boolean; to: number; }, 'from' | 'isCollapsed' | 'to'> | nulln/a

delete()

TypeScript
delete(
  deletion?: object,
  selection?: {
    direction: 'none' | 'forward' | 'backward';
    from: number;
    isCollapsed: boolean;
    to: number;
  } | null
): void
Parameters of delete
ParameterTypeDefaultDescription
deletionoptionalobjectn/an/a
deletion.fromoptionalnumbern/an/a
deletion.indexoptionalnumbern/an/a
deletion.lengthoptionalnumbern/an/a
deletion.tooptionalnumbern/an/a
selectionoptional{ direction: 'none' | 'forward' | 'backward'; from: number; isCollapsed: boolean; to: number; } | null...n/a

getDOMPoint()

TypeScript
getDOMPoint(targetOffset: number, element?: HTMLElement | null): { node: Node; offset: number } | null

Retrouve le nœud texte et l'offset DOM pour un offset Modèle donné

Parameters of getDOMPoint
ParameterTypeDefaultDescription
targetOffsetnumbern/an/a
elementoptionalHTMLElement | null...

handleBeforeInput()

TypeScript
handleBeforeInput(
  event: InputEvent,
  selection?: {
    direction: 'none' | 'forward' | 'backward';
    from: number;
    isCollapsed: boolean;
    to: number;
  } | null
): any
Parameters of handleBeforeInput
ParameterTypeDefaultDescription
eventInputEventn/an/a
selectionoptional{ direction: 'none' | 'forward' | 'backward'; from: number; isCollapsed: boolean; to: number; } | null...n/a

insert()

TypeScript
insert(index: number, text: string): void
Parameters of insert
ParameterTypeDescription
indexnumbern/a
textstringn/a

isMarkActive()

TypeScript
isMarkActive(
  markName: string,
  sel: Pick<{
      direction: 'none' | 'forward' | 'backward';
      from: number;
      isCollapsed: boolean;
      to: number;
    }, 'from' | 'isCollapsed' | 'to'> | null
): boolean
Parameters of isMarkActive
ParameterTypeDescription
markNamestringn/a
selPick<{ direction: 'none' | 'forward' | 'backward'; from: number; isCollapsed: boolean; to: number; }, 'from' | 'isCollapsed' | 'to'> | nulln/a

removeMark()

TypeScript
removeMark(
  markName: string,
  sel: Pick<{
      direction: 'none' | 'forward' | 'backward';
      from: number;
      isCollapsed: boolean;
      to: number;
    }, 'from' | 'isCollapsed' | 'to'> | null
): void
Parameters of removeMark
ParameterTypeDescription
markNamestringn/a
selPick<{ direction: 'none' | 'forward' | 'backward'; from: number; isCollapsed: boolean; to: number; }, 'from' | 'isCollapsed' | 'to'> | nulln/a

split()

TypeScript
split(
  options?: object,
  selection?: {
    direction: 'none' | 'forward' | 'backward';
    from: number;
    isCollapsed: boolean;
    to: number;
  } | null
): { block: Block } | null
Parameters of split
ParameterTypeDefaultDescription
optionsoptionalobjectn/an/a
options.fromoptionalnumbern/an/a
options.indexoptionalnumbern/an/a
options.lengthoptionalnumbern/an/a
options.offsetoptionalnumbern/an/a
options.tooptionalnumbern/an/a
selectionoptional{ direction: 'none' | 'forward' | 'backward'; from: number; isCollapsed: boolean; to: number; } | null...n/a

Returns { block: Block } | null

toClipboardBlock()

TypeScript
toClipboardBlock(context?: object): PasteBlock

Serializes a (partial) range of this block's text as a PasteBlock.

Parameters of toClipboardBlock
ParameterTypeDefaultDescription
contextoptionalobject{}n/a
context.fromoptionalnumber0n/a
context.partialoptionalfalse | 'both' | 'start' | 'end'falsen/a
context.propsoptionalRecord<string, any>{}n/a
context.tooptionalnumber | nullnulln/a

toggleMark()

TypeScript
toggleMark(
  markName: string,
  value: any,
  sel: Pick<{
      direction: 'none' | 'forward' | 'backward';
      from: number;
      isCollapsed: boolean;
      to: number;
    }, 'from' | 'isCollapsed' | 'to'> | null
): void
Parameters of toggleMark
ParameterTypeDescription
markNamestringn/a
valueanyn/a
selPick<{ direction: 'none' | 'forward' | 'backward'; from: number; isCollapsed: boolean; to: number; }, 'from' | 'isCollapsed' | 'to'> | nulln/a

toJSON()

TypeScript
toJSON(): { text: string; [mark: string]: any }[]

Converts the text delta to a Slate-like JSON format.

toMarkdown()

TypeScript
toMarkdown(options?: object): string
Parameters of toMarkdown
ParameterTypeDescription
optionsoptionalobject
options.rawBreaksoptionalbooleann/a

Source: src/lib/behaviors/text/text.behavior.svelte.js:188