Skip to content

Instantly share code, notes, and snippets.

@0on
Created April 25, 2017 09:09
Show Gist options
  • Save 0on/8a94bc346cee0134f78f2739971640a2 to your computer and use it in GitHub Desktop.
Save 0on/8a94bc346cee0134f78f2739971640a2 to your computer and use it in GitHub Desktop.
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