Created
April 25, 2017 09:09
-
-
Save 0on/8a94bc346cee0134f78f2739971640a2 to your computer and use it in GitHub Desktop.
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
| import * as im from 'immutable' | |
| const removeBlock = (editorState, blockKey) => { | |
| const content = editorState.getCurrentContent() | |
| const selection = editorState.getSelection() | |
| const blockMap = content.getBlockMap().delete(blockKey) | |
| return content.merge( | |
| { blockMap, selectionAfter: selection }, | |
| ) | |
| } | |
| const addBlock = (state, content, block) => { | |
| const selection = SelectionState.createEmpty(block.getKey()) | |
| const blocks = content.getBlockMap() | |
| const blockKey = block.getKey() | |
| const blocksBefore = blocks.takeWhile(x => x.getKey() !== blockKey) | |
| const blocksAfter = blocks.skipUntil(x => x.getKey() === blockKey) | |
| const newKey = genKey() | |
| const data = im.fromJS({ | |
| [newKey]: new ContentBlock({ | |
| key: newKey, | |
| type: 'atomic', | |
| text: ' ', | |
| data: im.Map({ | |
| type: 'placeholder', | |
| }), | |
| }), | |
| }) | |
| const newBlocks = blocksBefore.concat(im.fromJS(data)).concat(blocksAfter) | |
| return content.merge({ | |
| blockMap: newBlocks, | |
| selectionAfter: selection, | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment