Skip to main content

LexicalEditorWithDispose

interface

A handle to the editor with an attached dispose function

interface LexicalEditorWithDispose

Import

import { LexicalEditorWithDispose } from '@modular/sdk/editor';

Members29

dispose
() => void

Dispose the editor and perform all clean-up (also available as Symbol.dispose via Disposable)

__@dispose@281474976712269()
() => void
blur()
() => void

Removes focus from the editor.

dispatchCommand()
<TCommand extends AnyLexicalCommand>(type: TCommand, ...args: CommandPayloadArgs<CommandPayloadType<TCommand>>) => boolean

Dispatches a command of the specified type with the specified payload. This triggers all command listeners (set by LexicalEditor.registerCommand) for this type, passing them the provided payload. The command listeners will be triggered in an implicit LexicalEditor.update, unless this was invoked from inside an update in which case that update context will be re-used (as if this was a dollar function itself). Do not call this from inside a read-only context such as LexicalEditor.read or EditorState.read. Command listeners usually change the editor, so Lexical runs them in a separate writable update and development builds log a warning when they detect this. Dispatch after the read returns instead.

focus()
(callbackFn?: () => void, options?: EditorFocusOptions) => void

Focuses the editor by marking the existing selection as dirty, or by creating a new selection at `defaultSelection` if one does not already exist. If you want to force a specific selection, you should call `root.selectStart()` or `root.selectEnd()` in an update.

getDecorators()
<T>() => Record<NodeKey, T>

Gets a map of all decorators in the editor.

getEditorState()
() => EditorState

Gets the active editor state.

getElementByKey()
(key: NodeKey) => HTMLElement | null

Gets the underlying HTMLElement associated with the LexicalNode for the given key.

getKey()
() => string

Gets the key of the editor

getRootElement()
() => null | HTMLElement
hasNode()
<T extends Klass<LexicalNode>>(node: T) => boolean

Used to assert that a certain node is registered, usually by plugins to ensure nodes that they depend on have been registered.

hasNodes()
<T extends Klass<LexicalNode>>(nodes: T[]) => boolean

Used to assert that certain nodes are registered, usually by plugins to ensure nodes that they depend on have been registered.

isComposing()
() => boolean
isEditable()
() => boolean

Returns true if the editor is editable, false otherwise.

parseEditorState()
(maybeStringifiedEditorState: string | SerializedEditorState | CompactSerializedEditorState | ParsableSerializedEditorState, updateFn?: () => void) => EditorState

Parses a SerializedEditorState (usually produced by EditorState.toJSON) and returns and EditorState object that can be, for example, passed to LexicalEditor.setEditorState. Typically, deserialization from JSON stored in a database uses this method. Either form is accepted: parsing restores what a compact document omitted, which is the whole reason it may omit it, so CompactSerializedEditorState — what `toJSON(true)` returns — goes back in without a cast. So does a document assembled from serialized nodes (ParsableSerializedEditorState), such as `@lexical/clipboard`'s.

read()
<T>(callbackFn: () => T) => T | <T>(mode: EditorReadMode, callbackFn: () => T) => T

Executes a read of the editor's state, with the editor context available (useful for exporting and read-only DOM operations). Much like update, but prevents any mutation of the editor's state. When called with a single argument the `mode` defaults to `'force-commit'`, which flushes any pending updates immediately before the read so it always observes a fully committed and reconciled state. See EditorReadMode for the behavior of the other modes (`'pending'` and `'latest'`). Executes a read of the editor's state in the given `mode`, with the editor context available. See EditorReadMode for the available modes.

registerCommand()
<P>(command: LexicalCommand<P>, listener: CommandListener<P>, priority: CommandListenerPriority | CommandListenerPriorityBefore) => () => void

Registers a listener that will trigger anytime the provided command is dispatched with LexicalEditor.dispatch, subject to priority. Listeners that run at a higher priority can "intercept" commands and prevent them from propagating to other handlers by returning true. Listeners are always invoked in an LexicalEditor.update and can call dollar functions. Listeners registered at the same priority level will run deterministically in the order of registration.

registerDecoratorListener()
<T>(listener: DecoratorListener<T>) => () => void

Registers a listener for when the editor's decorator object changes. The decorator object contains all DecoratorNode keys -> their decorated value. This is primarily used with external UI frameworks. Will trigger the provided callback each time the editor transitions between these states until the teardown function is called.

registerEditableListener()
(listener: EditableListener) => () => void

Registers a listener for when the editor changes between editable and non-editable states. Will trigger the provided callback each time the editor transitions between these states until the teardown function is called. If the listener returns a function, that function will be called before the next transition or teardown.

registerMutationListener()
(klass: Klass<LexicalNode>, listener: MutationListener, options?: MutationListenerOptions) => () => void

Registers a listener that will run when a Lexical node of the provided class is mutated. The listener will receive a list of nodes along with the type of mutation that was performed on each: created, destroyed, or updated. One common use case for this is to attach DOM event listeners to the underlying DOM nodes as Lexical nodes are created. LexicalEditor.getElementByKey can be used for this. If any existing nodes are in the DOM, and skipInitialization is not true, the listener will be called immediately with an updateTag of 'registerMutationListener' where all nodes have the 'created' NodeMutation. This can be controlled with the skipInitialization option (whose default was previously true for backwards compatibility with &lt;=0.16.1 but has been changed to false as of 0.21.0).

registerNodeTransform()
<T extends LexicalNode>(klass: Klass<T>, listener: Transform<T>) => () => void

Registers a listener that will run when a Lexical node of the provided class is marked dirty during an update. The listener will continue to run as long as the node is marked dirty. There are no guarantees around the order of transform execution! Watch out for infinite loops. See [Node Transforms](https://lexical.dev/docs/concepts/transforms)

registerRootListener()
(listener: RootListener) => () => void

Registers a listener for when the editor's root DOM element (the content editable Lexical attaches to) changes. This is primarily used to attach event listeners to the root element. The root listener function is executed directly upon registration and then on any subsequent update. Will trigger the provided callback each time the editor transitions between these states until the teardown function is called. If the listener returns a function, that function will be called before the next transition or teardown.

registerTextContentListener()
(listener: TextContentListener) => () => void

Registers a listener for when Lexical commits an update to the DOM and the text content of the editor changes from the previous state of the editor. If the text content is the same between updates, no notifications to the listeners will happen. Will trigger the provided callback each time the editor transitions between these states until the teardown function is called.

registerUpdateListener()
(listener: UpdateListener) => () => void

Registers a listener for Editor update event. Will trigger the provided callback each time the editor goes through an update (via LexicalEditor.update) until the teardown function is called.

setEditable()
(editable: boolean) => void

Sets the editable property of the editor. When false, the editor will not listen for user events on the underling contenteditable.

setEditorState()
(editorState: EditorState, options?: EditorSetOptions) => void

Imperatively set the EditorState. Triggers reconciliation like an update.

setRootElement()
(nextRootElement: null | HTMLElement) => void

Imperatively set the root contenteditable element that Lexical listens for events on.

toJSON()
() => SerializedEditor

Returns a JSON-serializable javascript object NOT a JSON string. You still must call JSON.stringify (or something else) to turn the state into a string you can transfer over the wire and store in a database. See LexicalNode.exportJSON This editor's serialized state, in whichever form the export around it is writing — which is how a nested editor (an image caption) stays in the same form as the document containing it. The form is passed on explicitly rather than picked up by the call below: `EditorState.toJSON()` with no argument always writes the legacy form, so that its return type is true of what it returns.

update()
(updateFn: () => void, options?: EditorUpdateOptions) => void

Executes an update to the editor state. The updateFn callback is the ONLY place where Lexical editor state can be safely mutated.