Plate 中节点的 API 参考。

节点是 Plate 文档的基本构建块。它可以是编辑器根节点(最高层级)、Element 节点或 Text 节点(最低层级)。此 API 提供了与节点交互的工具,包括遍历、查询和提取数据。

type TNode = Editor | TElement | TText;
 
type Descendant = Element | Text
type Ancestor = Editor | Element

NodeAPI

ancestor

获取特定路径的节点,并断言它是祖先节点。

Parameters

  • root TNode

    起始的根节点。

  • path Path

    到祖先节点的路径。

ReturnsAncestor | undefined

    如果找到则返回祖先节点,否则返回 undefined

ancestors

返回特定路径上方所有祖先节点的生成器。

Parameters

Collapse all
  • root TNode

    起始的根节点。

  • path Path

    获取祖先的路径。

  • options optional NodeAncestorsOptions

    获取祖先的选项。

OptionsNodeAncestorsOptions

Collapse all
  • reverse optional boolean

    如果为 true,则从上到下返回祖先而非从下到上。

ReturnsGenerator<NodeEntry<Ancestor>, void, undefined>

    祖先节点条目的生成器。

child

获取节点在特定索引的子节点。

Parameters

Collapse all
  • root TNode

    父节点。

  • index number

    子节点的索引。

ReturnsTNode | undefined

    如果找到则返回子节点,否则返回 undefined

children

遍历特定路径节点的子节点。

Parameters

Collapse all
  • root TNode

    根节点。

  • path Path

    到父节点的路径。

  • options optional NodeChildrenOptions

    遍历子节点的选项。

OptionsNodeChildrenOptions

Collapse all
  • reverse optional boolean

    如果为 true,则反向迭代。

  • from optional number

    起始索引(包含)。

  • to optional number

    结束索引(不包含)。

ReturnsGenerator<NodeEntry<TNode>, void, undefined>

    子节点条目的生成器。

common

获取两个路径公共祖先节点的条目。

Parameters

Collapse all
  • root TNode

    根节点。

  • path Path

    第一个路径。

  • another Path

    第二个路径。

ReturnsNodeEntry<N> | undefined

    如果找到则返回公共祖先条目,否则返回 undefined

descendant

获取特定路径的节点,并断言它是后代节点。

Parameters

Collapse all
  • root TNode

    根节点。

  • path Path

    到后代的路径。

ReturnsDescendant | undefined

    如果找到则返回后代节点,否则返回 undefined

descendants

返回根节点内所有后代节点条目的生成器。

Parameters

Collapse all
  • root TNode

    根节点。

  • options optional NodeDescendantsOptions

    获取后代的选项。

OptionsNodeDescendantsOptions

Collapse all
  • from optional Path

    起始路径。

  • to optional Path

    结束路径。

  • reverse optional boolean

    如果为 true,则反向迭代。

  • pass optional (node: Descendant) => boolean

    过滤后代的函数。

ReturnsGenerator<NodeEntry<Descendant>, void, undefined>

    后代节点条目的生成器。

elements

返回根节点内所有 Element 节点的生成器。

Parameters

Collapse all
  • root TNode

    根节点。

  • options optional NodeElementsOptions

    获取 Element 的选项。

OptionsNodeElementsOptions

Collapse all
  • pass optional (node: Element) => boolean

    过滤 Element 的函数。

  • reverse optional boolean

    如果为 true,则反向迭代。

  • from optional Path

    起始路径。

  • to optional Path

    结束路径。

ReturnsGenerator<NodeEntry<Element>, void, undefined>

    Element 条目的生成器。

first

从路径获取根节点中的第一个节点条目。

Parameters

Collapse all
  • root TNode

    根节点。

  • path Path

    到节点的路径。

ReturnsNodeEntry<N> | undefined

    如果找到则返回第一个节点条目,否则返回 undefined

firstChild

获取节点的第一个子节点条目。

Parameters

Collapse all
  • root TNode

    父节点。

  • path Path

    到父节点的路径。

ReturnsNodeEntry<N> | undefined

    如果找到则返回第一个子节点条目,否则返回 undefined

firstText

获取节点的第一个 Text 节点条目。

Parameters

Collapse all
  • root TNode

    父节点。

  • path Path

    到父节点的路径。

ReturnsNodeEntry<N> | undefined

    如果找到则返回第一个 Text 节点条目,否则返回 undefined

fragment

获取根节点内范围所表示的切片片段。

Parameters

Collapse all
  • root TNode

    根节点。

  • range TRange

    要切片的范围。

ReturnsN[]

    切片后的片段。

get

获取特定路径引用的后代节点。

Parameters

Collapse all
  • root TNode

    根节点。

  • path Path

    到节点的路径。

ReturnsTNode | undefined

    如果找到则返回节点,否则返回 undefined

last

从路径获取根节点中的最后一个节点条目。

Parameters

Collapse all
  • root TNode

    根节点。

  • path Path

    到节点的路径。

ReturnsNodeEntry<N> | undefined

    如果找到则返回最后一个节点条目,否则返回 undefined

lastChild

获取节点的最后一个子节点条目。

Parameters

Collapse all
  • root TNode

    父节点。

  • path Path

    到父节点的路径。

ReturnsNodeEntry<N> | undefined

    如果找到则返回最后一个子节点条目,否则返回 undefined

leaf

获取特定路径的节点,并确保它是叶子 Text 节点。

Parameters

Collapse all
  • root TNode

    根节点。

  • path Path

    到节点的路径。

ReturnsN | undefined

    如果找到则返回叶子节点,否则返回 undefined

levels

返回树分支中从特定路径开始的节点生成器。

Parameters

Collapse all
  • root TNode

    根节点。

  • path Path

    到节点的路径。

ReturnsGenerator<NodeEntry<N>, void, undefined>

    从特定路径开始的树分支中节点条目的生成器。

nodes

返回根节点所有节点条目的生成器。

Parameters

Collapse all
  • root TNode

    根节点。

  • options optional NodeTextsOptions

    类似 descendants 的选项。

ReturnsGenerator<NodeEntry<N>, void, undefined>

    节点条目的生成器。

parent

获取特定路径节点的父节点。

Parameters

Collapse all
  • root TNode

    根节点。

  • path Path

    到节点的路径。

ReturnsAncestor | undefined

    如果找到则返回父节点,否则返回 undefined

texts

返回根节点中所有叶子 Text 节点的生成器。

Parameters

Collapse all
  • root TNode

    根节点。

  • options optional NodeTextsOptions

    获取 Text 节点的选项。

ReturnsGenerator<NodeEntry<Text>, void, undefined>

    Text 节点条目的生成器。

extractProps

获取节点的属性。

Parameters

Collapse all
  • node TNode

    要提取属性的节点。

ReturnsNodeProps<N>

    节点的属性。

has

检查特定路径是否存在后代节点。

Parameters

Collapse all
  • root TNode

    根节点。

  • path Path

    要检查的路径。

Returnsboolean

    如果指定路径存在节点则返回 true,否则返回 false

hasSingleChild

检查节点是否只有一个子节点。

Parameters

Collapse all
  • node TNode

    要检查的节点。

Returnsboolean

    如果节点只有一个子节点则返回 true

isAncestor

检查值是否实现了 Ancestor 接口。

Parameters

Collapse all
  • value any

    要检查的值。

Returnsboolean

    如果值实现了 Ancestor 接口则返回 true

isDescendant

检查值是否实现了 Descendant 接口。

Parameters

Collapse all
  • value any

    要检查的值。

Returnsboolean

    如果值实现了 Descendant 接口则返回 true

isLastChild

检查节点是否是父节点的最后一个子节点。

Parameters

Collapse all
  • root TNode

    根节点。

  • path Path

    到节点的路径。

Returnsboolean

    如果节点是父节点的最后一个子节点则返回 true

isNode

检查值是否实现了 TNode 接口。

Parameters

Collapse all
  • value any

    要检查的值。

Returnsboolean

    如果值实现了 TNode 接口则返回 true

isNodeList

检查值是否是 Descendant 对象的列表。

Parameters

Collapse all
  • value any

    要检查的值。

Returnsboolean

    如果值是 Descendant 对象的列表则返回 true

matches

检查节点是否匹配一组属性。

Parameters

Collapse all
  • node Descendant

    要检查的节点。

  • props Partial<Descendant>

    要匹配的属性。

Returnsboolean

    如果节点匹配提供的属性则返回 true

string

获取节点内容的连接文本字符串。

Parameters

Collapse all
  • node TNode

    要获取文本的节点。

Returnsstring

    连接后的文本内容。

类型

TNode

NodeTNode 的类型别名。

type TNode = Editor | TElement | TText;

NodeEntry

NodeEntry 对象在遍历 Plate 文档树中的节点时返回。它们由两个元素的数组组成:TNode 及其相对于文档中根节点的 Path

Attributes

Collapse all
  • 0 TNode

    节点本身。

  • 1 Path

    到节点的路径。

Descendant

Descendant 联合类型表示树中的后代节点。

type Descendant = TElement | TText;

Ancestor

Ancestor 联合类型表示树中的祖先节点。

type Ancestor = Editor | TElement;

NodeOf<N>

Parameters

Collapse all
  • node TNode

    要获取类型的节点。

ReturnsN

    节点类型。

NodeIn<V>

Parameters

Collapse all
  • value Value

    要获取节点类型的值。

ReturnsNodeOf<V[number]>

    指定值中所有可能的节点类型。

TNodeMatch<N>

Parameters

Collapse all
  • node N

    要匹配的节点。

Returnsboolean

    如果节点匹配谓词则为 true

DescendantOf<N>

Parameters

Collapse all
  • node N

    要获取后代类型的节点。

ReturnsDescendantOf<N>

    指定根节点中所有可能的后代节点类型。

DescendantIn<V>

Parameters

Collapse all
  • value Value

    要获取后代类型的值。

ReturnsDescendantIn<V>

    指定值中所有可能的后代节点类型。

ChildOf<N>

Parameters

Collapse all
  • node N

    要获取子节点类型的节点。

ReturnsChildOf<N>

    子节点类型。

AncestorOf<N>

Parameters

Collapse all
  • node N

    要获取祖先类型的节点。

ReturnsAncestorOf<N>

    指定根节点中所有可能的祖先节点类型。

AncestorIn<V>

Parameters

Collapse all
  • value Value

    要获取祖先类型的值。

ReturnsAncestorIn<V>

    指定值中所有可能的祖先节点类型。

AncestorEntry

祖先条目表示一个祖先节点(Editor 或 Element)及其路径。

Attributes

Collapse all
  • 0 Ancestor

    Editor 或 Element 节点。

  • 1 Path

    到祖先的路径。

DescendantEntry

后代条目表示一个后代节点(Element 或 Text)及其路径。

Attributes

Collapse all
  • 0 Descendant

    Element 或 Text 节点。

  • 1 Path

    到后代的路径。

NodeChildEntry

节点子条目表示一个子节点及其相对于父节点的路径。

Attributes

Collapse all
  • 0 TNode

    子节点。

  • 1 Path

    到子节点的路径。