Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Commit

Permalink
use mergeBlockData of Modifier instead of adding new one in utils
Browse files Browse the repository at this point in the history
  • Loading branch information
freedomlang committed Jan 17, 2019
1 parent 7bb8e7c commit 1e5ef13
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 37 deletions.
25 changes: 10 additions & 15 deletions examples/checkable-list-example/src/CheckableList.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from 'draft-js'
import { withPluginContext } from '@djsp/core'
import type { PluginProps } from '@djsp/core'
import { mergeBlockData } from '@djsp/utils'
import { Modifier } from 'draft-js'
import {
CheckableListItem,
CheckableListItemBlock,
Expand All @@ -20,31 +20,26 @@ import {
class CheckableList extends Component<PluginProps> {
_unregister: () => void

updateEditorState = (newEditorState: EditorState) => {
const { setEditorState, editorState } = this.props
setEditorState(
EditorState.forceSelection(newEditorState, editorState.getSelection())
)
}

onClick = event => {
event.stopPropagation()

const { editorState } = this.props
const { setEditorState, editorState } = this.props
const newEditorState = RichUtils.toggleBlockType(
editorState,
CHECKABLE_LIST_ITEM
)
this.updateEditorState(newEditorState)
setEditorState(
EditorState.forceSelection(newEditorState, editorState.getSelection())
)
}

toggleChecked = (block: ContentBlock) => {
const { editorState } = this.props

let newEditorState = mergeBlockData(editorState, block, {
const { setEditorState, editorState } = this.props
let newContentState = Modifier.mergeBlockData(editorState.getCurrentContent(), editorState.getSelection(), {
checked: !block.getData().get('checked'),
})
this.updateEditorState(newEditorState)
});
let newEditorState = EditorState.push(editorState, newContentState, 'change-block-data');
setEditorState(newEditorState)
}

componentDidMount() {
Expand Down
6 changes: 0 additions & 6 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,6 @@ getWordCount(editorState: EditorState): number
mergeEntityData(editorState: EditorState, entityKey: string, data: Object): EditorState
```

### mergeBlockData

```javascript
mergeBlockData(editorState: EditorState, block: ContentBlock, data: { [id: string]: any }): EditorState
```

## License

MIT © [juliankrispel](https://github.com/juliankrispel)
16 changes: 0 additions & 16 deletions packages/utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,19 +278,3 @@ export function mergeEntityData(

return EditorState.push(editorState, newContentState, 'apply-entity')
}

export function mergeBlockData(
editorState: EditorState,
block: ContentBlock,
data: { [id: string]: any }
): EditorState {
const content = editorState.getCurrentContent()
const updatedBlock = block.mergeIn(['data'], data)
const blockKey = block.getKey()
const blockMap = content.getBlockMap().merge({ [blockKey]: updatedBlock })
return EditorState.push(
editorState,
content.merge({ blockMap }),
'change-block-data'
)
}

0 comments on commit 1e5ef13

Please sign in to comment.