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?
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
There's Editor.string function that can be used to get the text at the selection.
const selectedText = Editor.string(editor, editor.selection);
Most helpful comment
It's now
value.fragment.text, wherevalueis fromchange.value.It seems to be
Node.texts(root: Node, options?): Iterable<NodeEntry<Text>>nowhttps://docs.slatejs.org/api/nodes#static-methods