Trilium Frontend API
    Preparing search index...

    Class EditorState

    The editor state class is a persistent (immutable) data structure. To update a state, you create a transaction, which produces a new state instance, without modifying the original object.

    As such, never mutate properties of a state directly. That'll just break things.

    Index

    Properties

    doc: Text

    The current document.

    selection: EditorSelection

    The current selection.

    allowMultipleSelections: Facet<boolean, boolean>

    A facet that, when enabled, causes the editor to allow multiple ranges to be selected. Be careful though, because by default the editor relies on the native DOM selection, which cannot handle multiple selections. An extension like drawSelection can be used to make secondary selections visible to the user.

    changeFilter: Facet<
        (tr: Transaction) => boolean | readonly number[],
        readonly ((tr: Transaction) => boolean | readonly number[])[],
    >

    Facet used to register change filters, which are called for each transaction (unless explicitly disabled), and can suppress part of the transaction's changes.

    Such a function can return true to indicate that it doesn't want to do anything, false to completely stop the changes in the transaction, or a set of ranges in which changes should be suppressed. Such ranges are represented as an array of numbers, with each pair of two numbers indicating the start and end of a range. So for example [10, 20, 100, 110] suppresses changes between 10 and 20, and between 100 and 110.

    languageData: Facet<
        (
            state: EditorState,
            pos: number,
            side: -1 | 0 | 1,
        ) => readonly { [name: string]: any }[],
        readonly (
            (
                state: EditorState,
                pos: number,
                side: -1 | 0 | 1,
            ) => readonly { [name: string]: any }[]
        )[],
    >

    A facet used to register language data providers.

    lineSeparator: Facet<string, string>

    The line separator to use. By default, any of "\n", "\r\n" and "\r" is treated as a separator when splitting lines, and lines are joined with "\n".

    When you configure a value here, only that precise separator will be used, allowing you to round-trip documents through the editor without normalizing line separators.

    phrases: Facet<{ [key: string]: string }, readonly { [key: string]: string }[]>

    Registers translation phrases. The phrase method will look through all objects registered with this facet to find translations for its argument.

    readOnly: Facet<boolean, boolean>

    This facet controls the value of the readOnly getter, which is consulted by commands and extensions that implement editing functionality to determine whether they should apply. It defaults to false, but when its highest-precedence value is true, such functionality disables itself.

    Not to be confused with EditorView.editable, which controls whether the editor's DOM is set to be editable (and thus focusable).

    tabSize: Facet<number, number>

    Configures the tab size to use in this state. The first (highest-precedence) value of the facet is used. If no value is given, this defaults to 4.

    transactionExtender: Facet<
        (tr: Transaction) => Pick<TransactionSpec, "effects" | "annotations">,
        readonly (
            (tr: Transaction) => Pick<TransactionSpec, "effects" | "annotations">
        )[],
    >

    This is a more limited form of transactionFilter, which can only add annotations and effects. But, this type of filter runs even if the transaction has disabled regular filtering, making it suitable for effects that don't need to touch the changes or selection, but do want to process every transaction.

    Extenders run after filters, when both are present.

    transactionFilter: Facet<
        (tr: Transaction) => TransactionSpec | readonly TransactionSpec[],
        readonly (
            (tr: Transaction) => TransactionSpec | readonly TransactionSpec[]
        )[],
    >

    Facet used to register a hook that gets a chance to update or replace transaction specs before they are applied. This will only be applied for transactions that don't have filter set to false. You can either return a single transaction spec (possibly the input transaction), or an array of specs (which will be combined in the same way as the arguments to EditorState.update).

    When possible, it is recommended to avoid accessing Transaction.state in a filter, since it will force creation of a state that will then be discarded again, if the transaction is actually filtered.

    (This functionality should be used with care. Indiscriminately modifying transaction is likely to break something or degrade the user experience.)

    Accessors

    • get lineBreak(): string

      Get the proper line-break string for this state.

      Returns string

    • get readOnly(): boolean

      Returns true when the editor is configured to be read-only.

      Returns boolean

    • get tabSize(): number

      The size (in columns) of a tab in the document, determined by the tabSize facet.

      Returns number

    Methods

    • Create a set of changes and a new selection by running the given function for each range in the active selection. The function can return an optional set of changes (in the coordinate space of the start document), plus an updated range (in the coordinate space of the document produced by the call's own changes). This method will merge all the changes and ranges into a single changeset and selection, and return it as a transaction spec, which can be passed to update.

      Parameters

      Returns {
          changes: ChangeSet;
          effects: readonly StateEffect<any>[];
          selection: EditorSelection;
      }

    • Create a change set from the given change description, taking the state's document length and line separator into account.

      Parameters

      Returns ChangeSet

    • Return a function that can categorize strings (expected to represent a single grapheme cluster) into one of:

      • Word (contains an alphanumeric character or a character explicitly listed in the local language's "wordChars" language data, which should be a string)
      • Space (contains only whitespace)
      • Other (anything else)

      Parameters

      • at: number

      Returns (char: string) => CharCategory

    • Retrieve the value of a state field. Throws an error when the state doesn't have that field, unless you pass false as second parameter.

      Type Parameters

      • T

      Parameters

      Returns T

    • Retrieve the value of a state field. Throws an error when the state doesn't have that field, unless you pass false as second parameter.

      Type Parameters

      • T

      Parameters

      Returns T

    • Find the values for a given language data field, provided by the the languageData facet.

      Examples of language data fields are...

      Type Parameters

      • T

      Parameters

      • name: string
      • pos: number
      • Optionalside: -1 | 0 | 1

      Returns readonly T[]

    • Look up a translation for the given phrase (via the phrases facet), or return the original string if no translation is found.

      If additional arguments are passed, they will be inserted in place of markers like $1 (for the first value) and $2, etc. A single $ is equivalent to $1, and $$ will produce a literal dollar sign.

      Parameters

      • phrase: string
      • ...insert: any[]

      Returns string

    • Return the given range of the document as a string.

      Parameters

      • Optionalfrom: number
      • Optionalto: number

      Returns string

    • Convert this state to a JSON-serializable object. When custom fields should be serialized, you can pass them in as an object mapping property names (in the resulting object, which should not use doc or selection) to fields.

      Parameters

      • Optionalfields: { [prop: string]: StateField<any> }

      Returns any

    • Using the state's line separator, create a Text instance from the given string.

      Parameters

      • string: string

      Returns Text

    • Create a transaction that updates this state. Any number of transaction specs can be passed. Unless sequential is set, the changes (if any) of each spec are assumed to start in the current document (not the document produced by previous specs), and its selection and effects are assumed to refer to the document created by its own changes. The resulting transaction contains the combined effect of all the different specs. For selection, later specs take precedence over earlier ones.

      Parameters

      Returns Transaction

    • Find the word at the given position, meaning the range containing all word characters around it. If no word characters are adjacent to the position, this returns null.

      Parameters

      • pos: number

      Returns SelectionRange

    • Create a new state. You'll usually only need this when initializing an editor—updated states are created by applying transactions.

      Parameters

      Returns EditorState

    • Deserialize a state from its JSON representation. When custom fields should be deserialized, pass the same object you passed to toJSON when serializing as third argument.

      Parameters

      Returns EditorState