Skip to main content

@lexical/extension/BlockIdExtension

Interfaces​

BlockIdConfig​

Defined in: packages/lexical-extension/src/BlockIdExtension.ts:178

Experimental

Properties​

$isIdentifiedBlock​

$isIdentifiedBlock: (node) => boolean

Defined in: packages/lexical-extension/src/BlockIdExtension.ts:190

Experimental

Which nodes get an id. Evaluated when a node is marked dirty, so it should depend only on the node and its ancestors. Default: $defaultIsIdentifiedBlock (the children of the root).

Parameters​
node​

LexicalNode

Returns​

boolean

disabled​

disabled: boolean

Defined in: packages/lexical-extension/src/BlockIdExtension.ts:184

Experimental

Set to true to stop assigning, repairing, and indexing ids. Ids already stored on nodes are kept. While disabled, $getBlockById falls back to a scan of the document.

generateId​

generateId: () => string

Defined in: packages/lexical-extension/src/BlockIdExtension.ts:199

Experimental

Makes a new id. It must return a non-empty string of at most 128 characters. A collision with an id in use in this editor is retried, but ids made concurrently by collaborators cannot see each other, so a replacement must stay unique across peers (random ids of 120 bits or more, or a per-peer prefix). Tests can inject a deterministic counter. Default: generateBlockId.

Returns​

string


BlockIdOutput​

Defined in: packages/lexical-extension/src/BlockIdExtension.ts:203

Experimental

Properties​

$getBlockById​

$getBlockById: (id) => LexicalNode | null

Defined in: packages/lexical-extension/src/BlockIdExtension.ts:207

Experimental

The same as $getBlockById for this editor.

Parameters​
id​

string

Returns​

LexicalNode | null

$isIdentifiedBlock​

$isIdentifiedBlock: (node) => boolean

Defined in: packages/lexical-extension/src/BlockIdExtension.ts:209

Experimental

The configured BlockIdConfig.$isIdentifiedBlock.

Parameters​
node​

LexicalNode

Returns​

boolean

disabled​

disabled: Signal<boolean>

Defined in: packages/lexical-extension/src/BlockIdExtension.ts:205

Experimental

See BlockIdConfig.disabled.

Variables​

BlockIdExtension​

const BlockIdExtension: LexicalExtension<BlockIdConfig, "@lexical/extension/BlockId", BlockIdOutput, BlockIdRuntime>

Defined in: packages/lexical-extension/src/BlockIdExtension.ts:1323

Experimental

Gives blocks stable ids, stored as NodeState (blockIdState), so that code outside the editor (an AI agent, a server pipeline, a comment system) can address a block by id across sessions, collaborators, and serializations.

  • Every identified block (see BlockIdConfig.$isIdentifiedBlock) gets an id in the same update that creates it, whether it was typed, pasted, or produced by importJSON, importDOM, or a markdown import.
  • A document stored without ids gets positional ids when it is loaded: derived from the document and each block's place in it, so every reader and every collaborator that loads the same document names its blocks alike, without writing first. The later holders of a repeated id get ids derived from that id and their ordinal. Blocks created by edits get random ids (generateBlockId).
  • Ids are unique: when several identified blocks share one, a block that already had it keeps it and blocks that got it in this update (a paste, a duplicate, an import) get fresh ids, so a reference never jumps to a copy. Between blocks that all had it before (a duplicate made by merging concurrent edits), the earliest in document order keeps it, so collaborators repairing the same duplicate concurrently converge; between blocks that all got it in one local update, the first one created does.
  • Blocks inside slots are covered wherever the predicate identifies them.
  • Splits, joins, type changes, moves, and undo/redo keep ids with the content the user sees; see the documentation for the exact rules.
  • $getBlockById finds a block from an index that is maintained from the dirty nodes of each update, never from a scan of the document.

blockIdState​

const blockIdState: StateConfig<"blockId", string | undefined>

Defined in: packages/lexical-extension/src/BlockIdExtension.ts:93

Experimental

The NodeState that stores a block's id, serialized in node JSON as "$": {"blockId": "..."}.

It is ordinary NodeState, so it survives clones and undo, round-trips through exportJSON/importJSON, and is synchronized by any collaboration binding that synchronizes NodeState. It is reset by $copyNode, so a node split off another one (e.g. the new ListItemNode that Enter creates) starts without an id instead of with a duplicate.

Any non-empty string of at most 128 characters is accepted; anything else reads as "no id".

Functions​

$defaultIsIdentifiedBlock()​

$defaultIsIdentifiedBlock(node): boolean

Defined in: packages/lexical-extension/src/BlockIdExtension.ts:140

Experimental

The default BlockIdConfig.$isIdentifiedBlock: the element and decorator children of the root. Compose it to identify nested blocks too:

configExtension(BlockIdExtension, {
$isIdentifiedBlock: (node) =>
$defaultIsIdentifiedBlock(node) || $isListItemNode(node),
});

Parameters​

node​

LexicalNode

Returns​

boolean


$getBlockById()​

$getBlockById(id): LexicalNode | null

Defined in: packages/lexical-extension/src/BlockIdExtension.ts:123

Experimental

Returns the identified block with the given id in the active editor state, or null. Requires BlockIdExtension.

The lookup goes through an index kept up to date from the dirty nodes of each commit and is verified against the active state, so it never returns a stale node. A block created or re-keyed earlier in the same update is found once that update has committed.

Parameters​

id​

string

Returns​

LexicalNode | null


$getBlockId()​

$getBlockId(node): string | null

Defined in: packages/lexical-extension/src/BlockIdExtension.ts:107

Experimental

Returns the id stored on node, or null if it has none.

This reads the NodeState directly, so it also returns an id left on a node that is not an identified block (one moved into a list item, say); such ids are not indexed and $getBlockById does not find them.

Parameters​

node​

LexicalNode

Returns​

string | null


generateBlockId()​

generateBlockId(): string

Defined in: packages/lexical-extension/src/BlockIdExtension.ts:161

Experimental

The default BlockIdConfig.generateId: 21 characters of the base64url alphabet (126 random bits, more than the 122 of a version 4 UUID), from crypto.getRandomValues. Ids need no coordination between collaborators or servers: ids made independently practically never collide, and a mistyped id (from a language model, say) almost never names another block.

There is no weaker fallback: where crypto.getRandomValues is missing (React Native without a polyfill, for instance), it throws, and the editor must be given a generateId of its own.

Returns​

string