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 19, 2019
1 parent 7bb8e7c commit 1895bd8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 38 deletions.
35 changes: 19 additions & 16 deletions examples/checkable-list-example/src/CheckableList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
ContentBlock,
DefaultDraftBlockRenderMap,
RichUtils,
Modifier,
} from 'draft-js'
import { withPluginContext } from '@djsp/core'
import type { PluginProps } from '@djsp/core'
import { mergeBlockData } from '@djsp/utils'
import {
CheckableListItem,
CheckableListItemBlock,
Expand All @@ -20,31 +20,34 @@ 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, {
checked: !block.getData().get('checked'),
})
this.updateEditorState(newEditorState)
const { setEditorState, editorState } = this.props
let newContentState = Modifier.mergeBlockData(
editorState.getCurrentContent(),
editorState.getSelection(),
{
checked: !block.getData().get('checked'),
}
)
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 1895bd8

Please sign in to comment.