Node ID
Installation
npm install @udecode/plate-node-id
Usage
import { NodeIdPlugin } from '@udecode/plate-node-id';
const plugins = [
// ...otherPlugins,
NodeIdPlugin.configure({
options: {
idKey: 'id',
filterInline: true,
filterText: true,
idCreator: () => nanoid(10),
},
}),
];
Plugins
NodeIdPlugin
Plugin that automatically assigns and manages unique IDs for nodes in the editor.
Options
Collapse all
- When
false
: Keeps existing IDs if they don't exist in the document - When
true
: Always generates new IDs - Default:
false
- Default:
true
- Default:
true
- Default:
() => nanoid(10)
- Default:
'id'
- When
false
: Only checks first and last nodes - When
true
: Normalizes all nodes - Default:
false
- When
true
: Keeps IDs if they don't exist in document - When
false
: Always generates new IDs (safer across documents) - Default:
false
- Default:
() => true
Disable using existing IDs when inserting nodes.
Filter inline Element nodes from receiving IDs.
Filter Text nodes from receiving IDs.
Function to generate unique IDs.
Property key used to store the ID on nodes.
Whether to normalize all nodes in the initial value.
Reuse IDs on undo/redo and copy/paste operations.
List of node types that should receive IDs.
List of node types that should not receive IDs.
Custom filter function for nodes that should receive IDs.
Behavior
The plugin handles several scenarios:
- Node Insertion:
<editor>
<hp id="10">test</hp>
</editor>
// Insert node with existing ID (e.g. copy/paste)
editor.insertNode(<hp id="10">inserted</hp>);
// Results in:
<editor>
<hp id="10">test</hp>
<hp id="1">inserted</hp> {/* Gets new ID */}
</editor>
// Insert multiple nodes
editor.insertNodes([
<hp>inserted</hp>,
<hp>test</hp>,
]);
// Results in:
<editor>
<hp id="10">test</hp>
<hp id="1">inserted</hp>
<hp id="2">test</hp>
</editor>
- Node Splitting:
// Before split
<hp id="1">te|st</hp>
// After split:
<hp id="1">te</hp>
<hp id="2">st</hp>
- Filtering:
// With filterText=false
<hp id="1">
<htext id="2">text</htext>
</hp>
// With allow=['p'] exclude=['blockquote']
<hp id="1">text</hp>
<hblockquote>quote</hblockquote>
- Undo/Redo:
// With reuseId=true
editor.insertNode(<hp id="1">text</hp>);
editor.undo();
editor.redo();
// Node keeps id="1" if not in use
// With reuseId=false
editor.insertNode(<hp id="1">text</hp>);
editor.undo();
editor.redo();
// Node gets new id="2"