javascript - filterDescendants and findDescendant with slate.js - Stack Overflow

I'm working on making a wysiwyg editor using slate.jsI'm in a situation where I'm tryin

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.

This picture below shows what I'm talking about:

Slate.js find first text pic

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.

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

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.

Does anyone have any ideas or examples?

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.

This picture below shows what I'm talking about:

Slate.js find first text pic

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.

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

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.

Does anyone have any ideas or examples?

Share Improve this question edited Oct 26, 2017 at 15:58 Ian Storm Taylor 8,72012 gold badges57 silver badges75 bronze badges asked May 5, 2017 at 21:56 Jeff MaximJeff Maxim 931 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Slate.js author here.

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. In this case getBlocks() returns an immutable List, which has a find method.

Hope that helps!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745395736a4625867.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信