āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā š shadcn/directory/udecode/plate/(guides)/editor-methods ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
This guide covers the various methods available on the Plate editor instance.
How you access the editor instance depends on the context in which you need it.
Use one of these hooks:
useEditorRef: Never re-render.useEditorSelector: Re-render only when a specific editor property changes.useEditorState: Re-render on every change.import { useEditorRef, useEditorSelector, useEditorState } from 'platejs/react';
const MyComponent = () => {
const editor = useEditorRef();
const hasSelection = useEditorSelector((editor) => !!editor.selection, []);
const editorState = useEditorState();
// ...
};
useEditorRef is always sufficient for accessing the editor inside callbacks.useEditorRef will never cause your component to re-render, so it's the best choice for performance.const hasSelection = useEditorSelector((editor) => !!editor.selection, []);useEditorSelector to access the relevant property.===. In most cases, this means returning a primitive value, like a number, string or boolean.equalityFn in the options to useEditorSelector for cases where === isn't sufficient.useEditorState will cause your component to re-render every time the user presses a key or changes the selection.To access the editor outside the Plate component or work with multiple editors, use the PlateController component:
import { PlateController } from 'platejs/react';
const App = () => (
<PlateController>
<Toolbar />
<MyEditor />
</PlateController>
);
const Toolbar = () => {
const editor = useEditorState();
const isMounted = useEditorMounted();
// Use editor methods here
};
PlateController manages active editors:
primary={false} on Plate).Hooks like useEditorRef and useEditorSelector work with the active editor inside PlateController. If no editor is active, they return a fallback editor, which:
Fallback editor scenarios:
Plate components.Plate components are non-primary.PlateContent mounting.You can check if any editor is mounted using useEditorMounted:
const Toolbar = () => {
const editor = useEditorState();
const isMounted = useEditorMounted();
if (!isMounted) {
return <div>Editor not ready</div>;
}
return <div>{/* Toolbar content */}</div>;
};
You can also use editor.meta.isFallback to check if the editor is a fallback instance.
Find the path of a node. Default is findNodePath (traversal). Overridden by withPlate to use ReactEditor.findPath (memo).
const path = editor.findPath(node);
Retrieve the typed API for the editor:
const api = editor.getApi(TablePlugin);
api.api.create.tableCell(); // Type-safe API method
Get the typed transforms for the editor:
const tf = editor.getTransforms(TablePlugin);
tf.insert.tableRow(); // Type-safe transform method
Retrieve the editor plugin instance by its key or base plugin:
const codeBlockPlugin = editor.getPlugin(CodeBlockPlugin);
const headingPlugin = editor.getPlugin({ key: KEYS.heading });
Get the node type associated with a plugin:
const paragraphType = editor.getType(KEYS.p);
Get a specific option value for a plugin:
const search = editor.getOption(FindReplacePlugin, 'search');
To subscribe to options changes, use usePluginOption or usePluginOptions hooks.
Get all options for a plugin:
const linkOptions = editor.getOptions(LinkPlugin);
Set a specific option value for a plugin:
editor.setOption(FindReplacePlugin, 'search', 'hello');
Set multiple options for a plugin:
editor.setOptions(FindReplacePlugin, {
search: 'hello',
caseSensitive: true,
});
You can also use a function to update options using Immer:
editor.setOptions(FindReplacePlugin, (draft) => {
draft.search = 'hello';
draft.caseSensitive = true;
});
Get the zustand-x options store for a plugin:
const store = editor.getOptionsStore(FindReplacePlugin);
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā