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

Commit 1872741

Browse files
committed
fix(checkable-list-example): use custom mergeBlockData to avoid update
issue and add Draft.css
1 parent 72f5e14 commit 1872741

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

examples/checkable-list-example/src/CheckableList.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
ContentBlock,
77
DefaultDraftBlockRenderMap,
88
RichUtils,
9-
Modifier,
109
} from 'draft-js'
1110
import { withPluginContext } from '@djsp/core'
1211
import type { PluginProps } from '@djsp/core'
@@ -18,40 +17,53 @@ import {
1817
CheckableListItemUtils,
1918
} from 'draft-js-checkable-list-item'
2019

20+
function mergeBlockData(
21+
editorState: EditorState,
22+
block: ContentBlock,
23+
data: { [id: string]: any }
24+
): EditorState {
25+
const content = editorState.getCurrentContent()
26+
const updatedBlock = block.mergeIn(['data'], data)
27+
const blockKey = block.getKey()
28+
const blockMap = content.getBlockMap().merge({ [blockKey]: updatedBlock })
29+
return EditorState.push(
30+
editorState,
31+
content.merge({ blockMap }),
32+
'change-block-data'
33+
)
34+
}
35+
2136
class CheckableList extends Component<PluginProps> {
2237
_unregister: () => void
2338

39+
updateEditorState = (newEditorState: EditorState) => {
40+
const { setEditorState, editorState } = this.props
41+
setEditorState(
42+
EditorState.forceSelection(newEditorState, editorState.getSelection())
43+
)
44+
}
45+
2446
onClick = event => {
2547
event.stopPropagation()
2648

27-
const { setEditorState, editorState } = this.props
49+
const { editorState } = this.props
2850
const newEditorState = RichUtils.toggleBlockType(
2951
editorState,
3052
CHECKABLE_LIST_ITEM
3153
)
32-
setEditorState(
33-
EditorState.forceSelection(newEditorState, editorState.getSelection())
34-
)
54+
this.updateEditorState(newEditorState)
3555
}
3656

3757
toggleChecked = (block: ContentBlock) => {
38-
const { setEditorState, editorState } = this.props
39-
let newContentState = Modifier.mergeBlockData(
40-
editorState.getCurrentContent(),
41-
editorState.getSelection(),
42-
{
43-
checked: !block.getData().get('checked'),
44-
}
45-
)
46-
let newEditorState = EditorState.push(
47-
editorState,
48-
newContentState,
49-
'change-block-data'
50-
)
51-
setEditorState(newEditorState)
58+
const { editorState } = this.props
59+
let newEditorState = mergeBlockData(editorState, block, {
60+
checked: !block.getData().get('checked'),
61+
})
62+
this.updateEditorState(newEditorState)
5263
}
5364

5465
handleTab = (event: SyntheticKeyboardEvent): ?boolean => {
66+
// debugger
5567
if (this.adjustBlockDepth(event)) {
5668
return true
5769
}
@@ -93,7 +105,7 @@ class CheckableList extends Component<PluginProps> {
93105
return CHECKABLE_LIST_ITEM
94106
}
95107
},
96-
// handleTab: this.handleTab
108+
onTab: this.handleTab,
97109
})
98110
}
99111

examples/checkable-list-example/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ReactDOM from 'react-dom'
44
import { EditorState, convertFromRaw } from 'draft-js'
55
import { EditorContainer, Editor } from '@djsp/core'
66
import CheckableList from './CheckableList'
7+
import 'draft-js/dist/Draft.css'
78
import 'draft-js-checkable-list-item/lib/CheckableListItem.css'
89
import './styles.css'
910

0 commit comments

Comments
 (0)