Slate: How to get a selected text?

Created on 12 Jan 2017  路  5Comments  路  Source: ianstormtaylor/slate

If I have next text in the editor

Example text here
Another paragraph

And when I select word text and print to console state.focusText.text - I got whole Example text here sentence. Bu I wan to get text word instead. I tried bunch of methods - but no one seems to work.

I guess I can get selected work by using state.startOffset and state.endOffset properties ( or state.focusKey) - but I don't know how.

Slate should have some convenient method for this case. Am I right?

question

Most helpful comment

It's now value.fragment.text, where value is from change.value.


It seems to be Node.texts(root: Node, options?): Iterable<NodeEntry<Text>> now

https://docs.slatejs.org/api/nodes#static-methods

All 5 comments

let {startOffset, endOffset, focusText} = this.props.editorState
const text = focusText.text.slice(startOffset, endOffset)

This solution works.

But is seems a little cumbersome.

@mkozhukharenko You can get selected text from fragment salteState.fragment.text

I know this was closed, but wanted to add for historical purposes.

I'm creating a plugin and listening to the onSelect and if I do console.log(state.fragment.text) it comes back empty, if do the following, it's set to the selected text:

setTimeout(() => {
  console.log(state.fragment.text);
}, 0);

Not sure if this is a bug or if I should be using something other than onSelect.

It's now value.fragment.text, where value is from change.value.


It seems to be Node.texts(root: Node, options?): Iterable<NodeEntry<Text>> now

https://docs.slatejs.org/api/nodes#static-methods

There's Editor.string function that can be used to get the text at the selection.

const selectedText = Editor.string(editor, editor.selection);

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ianstormtaylor picture ianstormtaylor  路  3Comments

varoot picture varoot  路  3Comments

chrpeter picture chrpeter  路  3Comments

Slapbox picture Slapbox  路  3Comments

YurkaninRyan picture YurkaninRyan  路  3Comments