I am trying to get current selection text or even block, I've found following code inside docs
var selectionState = editorState.getSelection();
var anchorKey = selectionState.getAnchorKey();
var currentContent = editorState.getCurrentContent();
var currentContentBlock = currentContent.getBlockForKey(anchorKey);
var start = selectionState.getStartOffset();
var end = selectionState.getEndOffset();
var selectedText = currentContentBlock.getText();
but selectedText
is previous selected block meaning
block 1 (user clicks first time)
block 2 (user clicks second time)
it will show block 1 and then
clicking block 1 (third time) will show block 2
it's like you have to double click to show correct block
any solutions to this? thank you
The issue was I was using previous state instead of latest state, this is working solution
const getCurrentBlock = (editorState) => {
const currentSelection = editorState.getSelection();
const blockKey = currentSelection.getStartKey();
return(editorState.getCurrentContent().getBlockForKey(blockKey));
}
const getCurrentText = (editorState) => {
const currentBlock = getCurrentBlock(editorState);
const blockText = currentBlock.getText();
return blockText;
}
const changeEditorState = (state) => {console.log(getCurrentText(state));}