6
6
ContentBlock ,
7
7
DefaultDraftBlockRenderMap ,
8
8
RichUtils ,
9
- Modifier ,
10
9
} from 'draft-js'
11
10
import { withPluginContext } from '@djsp/core'
12
11
import type { PluginProps } from '@djsp/core'
@@ -18,40 +17,53 @@ import {
18
17
CheckableListItemUtils ,
19
18
} from 'draft-js-checkable-list-item'
20
19
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
+
21
36
class CheckableList extends Component < PluginProps > {
22
37
_unregister : ( ) => void
23
38
39
+ updateEditorState = ( newEditorState : EditorState ) => {
40
+ const { setEditorState, editorState } = this . props
41
+ setEditorState (
42
+ EditorState . forceSelection ( newEditorState , editorState . getSelection ( ) )
43
+ )
44
+ }
45
+
24
46
onClick = event => {
25
47
event . stopPropagation ( )
26
48
27
- const { setEditorState , editorState } = this . props
49
+ const { editorState } = this . props
28
50
const newEditorState = RichUtils . toggleBlockType (
29
51
editorState ,
30
52
CHECKABLE_LIST_ITEM
31
53
)
32
- setEditorState (
33
- EditorState . forceSelection ( newEditorState , editorState . getSelection ( ) )
34
- )
54
+ this . updateEditorState ( newEditorState )
35
55
}
36
56
37
57
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 )
52
63
}
53
64
54
65
handleTab = ( event : SyntheticKeyboardEvent ) : ?boolean => {
66
+ // debugger
55
67
if ( this . adjustBlockDepth ( event ) ) {
56
68
return true
57
69
}
@@ -93,7 +105,7 @@ class CheckableList extends Component<PluginProps> {
93
105
return CHECKABLE_LIST_ITEM
94
106
}
95
107
} ,
96
- // handleTab : this.handleTab
108
+ onTab : this . handleTab ,
97
109
} )
98
110
}
99
111
0 commit comments