filterDescendants 和 findDescendant 与 slate.js

filterDescendants and findDescendant with slate.js

我正在使用 slate.js 制作一个所见即所得的编辑器 我正处于尝试找到第一个带有文本的节点的情况。

下图就是我所说的:

Slate.js find first text pic

在我的图片中,我想找到包含 "this is my title." 的节点,即使它之前有几个空行。

基本上如果我在编辑器中写了一堆文本,我如何找到第一个不是空字符串的文本?

查看文档后,我发现 filterDescendants 和 findDescendants 函数似乎可以满足我的需求。

但是,我不清楚如何使用它们。

我试过这样的事情:

this.state.state.startBlock.findDescendant((d) => d.text !== "")

但这只是returnsnull

文档说 findDescendant 将 "Deeply find a descendant node by iterator",其中 iterator 是一个函数,但是这里没有提供您要传递哪种函数的示例。

有没有人有任何想法或例子?

Slate.js作者在这里

你会喜欢做类似的事情:

state.document.getBlocks().find(block => block.text != '')

这将搜索文档中的叶块节点(在本例中是您的段落,headers,等等)并找到第一个不为空的节点。

Slate 数据模型是用 Immutable.js, so reading up on how that library works is very helpful for using Slate. In this case getBlocks() returns an immutable List 构建的,它有一个 find 方法。

希望对您有所帮助!