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

Commit 1e5ef13

Browse files
committed
use mergeBlockData of Modifier instead of adding new one in utils
1 parent 7bb8e7c commit 1e5ef13

File tree

3 files changed

+10
-37
lines changed

3 files changed

+10
-37
lines changed

Diff for: examples/checkable-list-example/src/CheckableList.js

+10-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from 'draft-js'
1010
import { withPluginContext } from '@djsp/core'
1111
import type { PluginProps } from '@djsp/core'
12-
import { mergeBlockData } from '@djsp/utils'
12+
import { Modifier } from 'draft-js'
1313
import {
1414
CheckableListItem,
1515
CheckableListItemBlock,
@@ -20,31 +20,26 @@ import {
2020
class CheckableList extends Component<PluginProps> {
2121
_unregister: () => void
2222

23-
updateEditorState = (newEditorState: EditorState) => {
24-
const { setEditorState, editorState } = this.props
25-
setEditorState(
26-
EditorState.forceSelection(newEditorState, editorState.getSelection())
27-
)
28-
}
29-
3023
onClick = event => {
3124
event.stopPropagation()
3225

33-
const { editorState } = this.props
26+
const { setEditorState, editorState } = this.props
3427
const newEditorState = RichUtils.toggleBlockType(
3528
editorState,
3629
CHECKABLE_LIST_ITEM
3730
)
38-
this.updateEditorState(newEditorState)
31+
setEditorState(
32+
EditorState.forceSelection(newEditorState, editorState.getSelection())
33+
)
3934
}
4035

4136
toggleChecked = (block: ContentBlock) => {
42-
const { editorState } = this.props
43-
44-
let newEditorState = mergeBlockData(editorState, block, {
37+
const { setEditorState, editorState } = this.props
38+
let newContentState = Modifier.mergeBlockData(editorState.getCurrentContent(), editorState.getSelection(), {
4539
checked: !block.getData().get('checked'),
46-
})
47-
this.updateEditorState(newEditorState)
40+
});
41+
let newEditorState = EditorState.push(editorState, newContentState, 'change-block-data');
42+
setEditorState(newEditorState)
4843
}
4944

5045
componentDidMount() {

Diff for: packages/utils/README.md

-6
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,6 @@ getWordCount(editorState: EditorState): number
149149
mergeEntityData(editorState: EditorState, entityKey: string, data: Object): EditorState
150150
```
151151

152-
### mergeBlockData
153-
154-
```javascript
155-
mergeBlockData(editorState: EditorState, block: ContentBlock, data: { [id: string]: any }): EditorState
156-
```
157-
158152
## License
159153

160154
MIT © [juliankrispel](https://github.com/juliankrispel)

Diff for: packages/utils/src/index.js

-16
Original file line numberDiff line numberDiff line change
@@ -278,19 +278,3 @@ export function mergeEntityData(
278278

279279
return EditorState.push(editorState, newContentState, 'apply-entity')
280280
}
281-
282-
export function mergeBlockData(
283-
editorState: EditorState,
284-
block: ContentBlock,
285-
data: { [id: string]: any }
286-
): EditorState {
287-
const content = editorState.getCurrentContent()
288-
const updatedBlock = block.mergeIn(['data'], data)
289-
const blockKey = block.getKey()
290-
const blockMap = content.getBlockMap().merge({ [blockKey]: updatedBlock })
291-
return EditorState.push(
292-
editorState,
293-
content.merge({ blockMap }),
294-
'change-block-data'
295-
)
296-
}

0 commit comments

Comments
 (0)