similiquedicta
Version:
A Plugin Architecture on top of Draft.JS
39 lines (35 loc) • 1.25 kB
text/typescript
import { SelectionState, EditorState, ContentBlock } from 'draft-js';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import DraftOffsetKey from 'draft-js/lib/DraftOffsetKey';
// Set selection of editor to next/previous block
export default (
getEditorState: () => EditorState,
setEditorState: (state: EditorState) => void,
newActiveBlock: ContentBlock
): void => {
const editorState = getEditorState();
// TODO verify that always a key-0-0 exists
const offsetKey = DraftOffsetKey.encode(newActiveBlock.getKey(), 0, 0);
const node = document.querySelectorAll(`[data-offset-key="${offsetKey}"]`)[0];
// set the native selection to the node so the caret is not in the text and
// the selectionState matches the native selection
const selection = window.getSelection()!;
const range = document.createRange();
range.setStart(node, 0);
range.setEnd(node, 0);
selection.removeAllRanges();
selection.addRange(range);
setEditorState(
EditorState.forceSelection(
editorState,
new SelectionState({
anchorKey: newActiveBlock.getKey(),
anchorOffset: 0,
focusKey: newActiveBlock.getKey(),
focusOffset: 0,
isBackward: false,
})
)
);
};