Plate 存储的 API 参考文档。

Plate 使用 jotai-x 来存储编辑器的状态。

Plate 存储

PlateStoreState 对象存储了 Plate 编辑器的状态。它包含了编辑器的 ID、当前值、插件以及其他设置的信息。

State

  • editor PlateEditor

    Plate 编辑器的引用。

    • 默认值: createPlateFallbackEditor()
  • id string

    用作 provider 作用域的唯一 ID。如果在同一个 React 树中有多个 Plate,请使用它。

    • 默认值: 随机 ID
  • containerRef React.RefObject<HTMLDivElement>

    编辑器容器元素的引用。

  • decorate optional function

    用于装饰编辑器中范围的函数。

    (options: { editor: PlateEditor; entry: NodeEntry }) => TRange[]
  • isMounted optional boolean

    Editable 是否已渲染,以便 slate DOM 可解析。

  • onChange optional function

    编辑器状态变化时的受控回调函数。

    (options: { editor: PlateEditor; value: ValueOf<PlateEditor> }) => void
  • onSelectionChange optional function

    编辑器选区变化时的受控回调函数。

    (options: { editor: PlateEditor; selection: TSelection }) => void
  • onValueChange optional function

    编辑器子节点变化时的受控回调函数。

    (options: { editor: PlateEditor; value: ValueOf<PlateEditor> }) => void
  • primary optional boolean

    编辑器是否是主要的。如果没有活跃的编辑器,PlateController 将使用第一个挂载的主要编辑器。

    • 默认值: true
  • readOnly optional boolean

    编辑器是否为只读模式。

  • renderElement optional function

    渲染编辑器中元素的函数。

  • renderLeaf optional function

    渲染编辑器中叶子节点的函数。

  • versionDecorate optional number

    调用 redecorate 时递增的版本号。这是 decorate 函数的依赖项。

  • versionEditor optional number

    每次编辑器变化时递增的版本号。

  • versionSelection optional number

    每次编辑器选区变化时递增的版本号。

  • versionValue optional number

    每次编辑器子节点变化时递增的版本号。

访问存储

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'

usePlateState

获取和设置存储属性的值。

const [readOnly, setReadOnly] = usePlateState('readOnly', id?)

usePlateValue

订阅存储属性的值。

const readOnly = usePlateValue('readOnly', id?)

usePlateSet

设置存储属性的值。

const setReadOnly = usePlateSet('readOnly', id?)

事件编辑器存储

该存储是一个对象,其属性键是事件名称(例如 'focus'),属性值是编辑器 ID

  • 这在有多个编辑器时非常有用,可以根据 DOM 事件(例如最近聚焦的编辑器)获取一个编辑器。
  • Plate 的核心插件之一将存储以下事件。

State

Collapse all
  • blur string | null

    最近失去焦点的编辑器 ID。

  • focus string | null

    当前正在聚焦的编辑器 ID。

  • last string | null

    最近的编辑器 ID。

import { EventEditorStore, useEventEditorValue } from 'platejs'
 
// 获取值
const focusedId = EventEditorStore.get('focus')
 
// 设置值
EventEditorStore.set('focus', editorId)
 
// 订阅变化
const focusedId = useEventEditorValue('focus')

useEventPlateId

获取最近的事件编辑器 ID。

Parameters

Collapse all
  • id string | null

    如果定义了,则返回该 ID。

Returnsstring

    如果可用,则返回上下文中的 plate id,否则返回最近的事件编辑器 ID 或 PLATE_SCOPE