66 ContentBlock ,
77 DefaultDraftBlockRenderMap ,
88 RichUtils ,
9- Modifier ,
109} from 'draft-js'
1110import { withPluginContext } from '@djsp/core'
1211import 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+
2136class 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
0 commit comments