简体   繁体   English

filterDescendants和findDescendant with slate.js

[英]filterDescendants and findDescendant with slate.js

I'm working on making a wysiwyg editor using slate.js I'm in a situation where I'm trying to find the first node with text. 我正在使用slate.js制作一个所见即所得的编辑器。我正处于这样一种情况,即我正在尝试找到带有文本的第一个节点。

This picture below shows what I'm talking about: 下面的图片显示了我在说什么:

Slate.js find first text pic Slate.js找到第一个文本图片

In my picture, I'd want to find the node that contains "this is my title.", even if there's several empty lines before it. 在我的图片中,我想找到包含“这是我的标题。”的节点,即使它前面有几个空行。

Basically if I have a bunch of text written in the editor, how do I find the first text that's not an empty string? 基本上如果我在编辑器中写了一堆文本,我如何找到第一个不是空字符串的文本?

Looking through the docs, I've found the filterDescendants and findDescendants functions which seem to do what I'm looking for. 通过文档查看,我发现了filterDescendants和findDescendants函数,它们似乎正在寻找我正在寻找的东西。

However, I'm unclear how to use them. 但是,我不清楚如何使用它们。

I've tried something like this: 我尝试过这样的事情:

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

But this just returns null 但这只是返回null

The docs say that findDescendant will "Deeply find a descendant node by iterator", where iterator is a function, but there's no examples provided for what sort of function you'd pass here. 文档说findDescendant将“通过迭代器深深地找到一个后代节点”,其中iterator是一个函数,但是没有为你在这里传递什么类型的函数提供的例子。

Does anyone have any ideas or examples? 有没有人有任何想法或例子?

Slate.js author here. Slate.js的作者在这里。

You'll like want to do something like: 你想要做的事情如下:

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

This will search through the leaf block nodes in the document (in this case your paragraphs, headers, etc.) and find the first one that isn't empty. 这将搜索文档中的叶块节点(在本例中为您的段落,标题等),并找到第一个非空的节点。

The Slate data model is built with Immutable.js , so reading up on how that library works is very helpful for using Slate. Slate数据模型是使用Immutable.js构建的,因此阅读该库的工作方式对于使用Slate非常有帮助。 In this case getBlocks() returns an immutable List , which has a find method. 在这种情况下, getBlocks()返回一个不可变的List ,它有一个find方法。

Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM