import { SelectionState } from 'draft-js'; class SelectionStateUtil { constructor() {} createEmpty(blockKey) { return SelectionState.createEmpty(blockKey); } getOffsets(selectionState) { return { startOffset: selectionState.getStartOffset(), endOffset: selectionState.getEndOffset(), }; } updateRange(selectionState, newStartOffset, newEndOffset) { return new SelectionState({ anchorKey: selectionState.anchorKey, anchorOffset: newStartOffset, focusKey: selectionState.focusKey, focusOffset: newEndOffset, hasFocus: selectionState.hasFocus, isBackward: false, }) } selectTextRange(blockKey, anchorOffset, focusOffset) { return new SelectionState({ anchorKey: blockKey, anchorOffset, focusKey: blockKey, focusOffset, hasFocus: false, isBackward: false, }) } selectEntireBlock(selectionState, block) { // Used to apply default inline styles to any blocks that have them return new SelectionState({ anchorKey: selectionState.anchorKey, anchorOffset: 0, focusKey: selectionState.focusKey, focusOffset: block.getCharacterList().size, hasFocus: selectionState.hasFocus, isBackward: false, }); } selectBlockByKey(blockKey) { return new SelectionState({ anchorKey: blockKey, anchorOffset: 0, focusKey: blockKey, focusOffset: 1, hasFocus: true, isBackward: false }); } } export default new SelectionStateUtil();