Last active
April 1, 2016 13:01
-
-
Save SchDen/c155ec5b801d9b63a3e4788d8f13db6a to your computer and use it in GitHub Desktop.
Draft.js - getSelectedText
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Returns normalized selection values so you can be sure that `anchorKey` is the | |
| * first and `focusKey` the last block in your selection. | |
| * | |
| * @see https://gist.github.com/johanneslumpe/5865625e2992d0718cfb45d8a4c47879 | |
| * | |
| * @param {object} selection A draft.js `SelectionState` instance | |
| * | |
| * @return {object} An object containing normalized `anchorKey`, `anchorOffset`, | |
| * `focusKey` and `focusOffset` values. | |
| */ | |
| function getNormalizedSelectionValues(selection) { | |
| const { anchorKey, anchorOffset, focusKey, focusOffset, isBackward } = selection; | |
| return { | |
| anchorKey: isBackward ? focusKey : anchorKey, | |
| focusKey: isBackward ? anchorKey : focusKey, | |
| anchorOffset: isBackward ? focusOffset : anchorOffset, | |
| focusOffset: isBackward ? anchorOffset : focusOffset, | |
| }; | |
| } | |
| /** | |
| * Returns an array of all `ContentBlock` instances within two block keys | |
| * | |
| * @see https://gist.github.com/johanneslumpe/5865625e2992d0718cfb45d8a4c47879 | |
| * | |
| * @param {object} contentState A draft.js `ContentState` instance | |
| * @param {string} anchorKey The block key to start searching from | |
| * @param {string} focusKey The block key until which to search | |
| * | |
| * @return {array} An array containing the found content blocks | |
| */ | |
| function getSelectedBlocks(contentState, anchorKey, focusKey) { | |
| const isSameBlock = anchorKey === focusKey; | |
| const startingBlock = contentState.getBlockForKey(anchorKey); | |
| const selectedBlocks = [startingBlock]; | |
| if (!isSameBlock) { | |
| let blockKey = anchorKey; | |
| while (blockKey !== focusKey) { | |
| const nextBlock = contentState.getBlockAfter(blockKey); | |
| selectedBlocks.push(nextBlock); | |
| blockKey = nextBlock.key; | |
| } | |
| } | |
| return selectedBlocks; | |
| } | |
| export default editorState => { | |
| const contentState = editorState.getCurrentContent(); | |
| const selection = editorState.getSelection(); | |
| const { anchorKey, focusKey } = getNormalizedSelectionValues(selection); | |
| const blocks = getSelectedBlocks(contentState, anchorKey, focusKey); | |
| const countBlocks = blocks.length; | |
| let blockText = ''; | |
| // Only 1 block | |
| if (countBlocks === 1) { | |
| return blocks[0].getText().slice(selection.getAnchorOffset(), selection.getEndOffset()); | |
| } | |
| // Concat text for some blocks | |
| blockText = blocks.reduce((sumText, block, index) => { | |
| let text = block.getText(); | |
| // If it's first block - get selected part | |
| if (index === 0) { | |
| text = text.slice(selection.getAnchorOffset()); | |
| } | |
| // If it's last block - get selected part | |
| if (index === (countBlocks - 1)) { | |
| text = text.slice(0, selection.getEndOffset()); | |
| } | |
| return sumText + text; | |
| }, ''); | |
| return blockText; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment