docs/api/core/plate-store.cn.mdx
Plate 使用 jotai-x 来存储编辑器的状态。
createPlateFallbackEditor()
</APIItem>
(options: { editor: PlateEditor; entry: NodeEntry }) => TRange[]
(options: { editor: PlateEditor; value: ValueOf<PlateEditor> }) => void
(options: { editor: PlateEditor; selection: TSelection }) => void
(options: { editor: PlateEditor; value: ValueOf<PlateEditor> }) => void
(options: {
editor: PlateEditor;
node: Descendant;
operation: NodeOperation;
prevNode: Descendant
}) => void
参数:
editor: Plate 编辑器实例node: 操作后的节点operation: 发生的节点操作(insert、remove、set、merge、split、move)prevNode: 操作前的节点注意: 对于 insert_node 和 remove_node 操作,node 和 prevNode 包含相同的值,以避免空值情况。
</APIItem>
(options: {
editor: PlateEditor;
node: Descendant;
operation: TextOperation;
prevText: string;
text: string
}) => void
参数:
editor: Plate 编辑器实例node: 包含变化文本的父节点operation: 发生的文本操作(insert_text 或 remove_text)prevText: 操作前的文本内容text: 操作后的文本内容
</APIItem>
true
</APIItem>
import { usePlateStore, useEditorRef, useEditorPlugin } from 'platejs/react'
// 直接访问存储
const store = usePlateStore(id?)
// 通过编辑器引用访问
const store = useEditorRef().store
// 通过插件上下文访问
const store = useEditorPlugin(myPlugin).store
注意:id 参数是可选的,默认使用最近的编辑器。
以下钩子可用于与 Plate 存储交互:
import { usePlateState, usePlateValue, usePlateSet } from 'platejs/react'
获取和设置存储属性的值。
const [readOnly, setReadOnly] = usePlateState('readOnly', id?)
订阅存储属性的值。
const readOnly = usePlateValue('readOnly', id?)
设置存储属性的值。
const setReadOnly = usePlateSet('readOnly', id?)
该存储是一个对象,其属性键是事件名称(例如 'focus'),属性值是编辑器 ID。
最近失去焦点的编辑器 ID。
</APIItem> <APIItem name="focus" type="string | null">当前正在聚焦的编辑器 ID。
</APIItem> <APIItem name="last" type="string | null">最近的编辑器 ID。
</APIItem> </APIState> </API>import { EventEditorStore, useEventEditorValue } from 'platejs'
// 获取值
const focusedId = EventEditorStore.get('focus')
// 设置值
EventEditorStore.set('focus', editorId)
// 订阅变化
const focusedId = useEventEditorValue('focus')
useEventPlateId获取最近的事件编辑器 ID。
<API name="useEventPlateId"> <APIParameters> <APIItem name="id" type="string | null">如果定义了,则返回该 ID。
</APIItem> </APIParameters> <APIReturns type="string"> 如果可用,则返回上下文中的 plate id,否则返回最近的事件编辑器 ID 或 `PLATE_SCOPE`。 </APIReturns> </API>