1
1
// @flow
2
2
3
3
import React , { Component , Fragment } from 'react'
4
- import { ContentBlock , RichUtils } from 'draft-js'
4
+ import { EditorState , ContentBlock , DefaultDraftBlockRenderMap } from 'draft-js'
5
5
import { Plugin , withPluginContext } from '@djsp/core'
6
- import { insertEntityBlock } from '@djsp/utils'
7
- import AtomicBlock from '@djsp/atomic-block'
8
- import { CheckableListItem , CheckableListItemBlock , CHECKABLE_LIST_ITEM , CheckableListItemUtils } from 'draft-js-checkable-list-item'
6
+ import { insertEntityBlock , mergeEntityData } from '@djsp/utils'
7
+ import { CheckableListItem , CheckableListItemBlock , CHECKABLE_LIST_ITEM , blockRenderMap } from 'draft-js-checkable-list-item'
9
8
10
9
11
10
type Props = {
@@ -24,20 +23,25 @@ class CheckableList extends Component<Props> {
24
23
25
24
const { setEditorState, editorState } = this . props
26
25
setEditorState ( insertEntityBlock ( editorState , CHECKABLE_LIST_ITEM , {
27
- checked : true
26
+ checked : false
28
27
} ) )
29
28
}
30
29
31
30
toggleChecked = ( block ) = > {
32
31
const { setEditorState, editorState } = this . props
33
- console . log ( CheckableListItemUtils . toggleChecked ( editorState , block ) )
34
- setEditorState (
35
- CheckableListItemUtils . toggleChecked ( editorState , block )
36
- ) ;
32
+ const content = editorState . getCurrentContent ( ) ;
33
+ const entityKey = block . getEntityAt ( 0 ) ;
34
+ const data = content . getEntity ( entityKey ) . getData ( ) ;
35
+
36
+ let newEditorState = mergeEntityData ( editorState , entityKey , { checked : ! data . checked } )
37
+ setEditorState ( EditorState . forceSelection (
38
+ newEditorState ,
39
+ editorState . getSelection ( )
40
+ ) ) ;
37
41
}
38
42
39
43
blockRendererFn = ( block : ContentBlock ) : ?CheckableListItemBlock => {
40
- const { setEditorState , editorState } = this . props ;
44
+ const { editorState } = this . props ;
41
45
var content = editorState . getCurrentContent ( ) ;
42
46
43
47
if ( block . getType ( ) === 'atomic' ) {
@@ -51,28 +55,25 @@ class CheckableList extends Component<Props> {
51
55
component : CheckableListItem ,
52
56
props : {
53
57
onChangeChecked : ( ) => this . toggleChecked ( block ) ,
54
- checked : ! ! block . getData ( ) . get ( ' checked' ) ,
58
+ checked : ! ! data . checked ,
55
59
} ,
56
60
}
57
61
}
58
62
}
59
63
}
60
64
65
+ blockStyleFn ( block : ContentBlock ) : ?string {
66
+ if ( block . getType ( ) === CHECKABLE_LIST_ITEM ) {
67
+ return CHECKABLE_LIST_ITEM
68
+ }
69
+ }
70
+
61
71
render ( ) {
62
72
return ( < Fragment >
63
- { /* <AtomicBlock type={CHECKABLE_LIST_ITEM}>
64
- {({ isFocused, blockProps: { checked }, ...otherProps }) => {
65
- const props = {
66
- checked,
67
- onChangeChecked: () => this.toggleChecked(otherProps.block)
68
- }
69
-
70
- return (
71
- <CheckableListItem blockProps={props} {...otherProps} />
72
- )
73
- }}
74
- </AtomicBlock> */ }
75
- < Plugin blockRendererFn = { this . blockRendererFn } />
73
+ < Plugin
74
+ blockRendererFn = { this . blockRendererFn }
75
+ blockRenderMap = { DefaultDraftBlockRenderMap . merge ( blockRenderMap ) }
76
+ blockStyleFn = { this . blockStyleFn } />
76
77
< button type = "button" onClick = { this . onClick } >
77
78
Insert checkable-list
78
79
</ button >
0 commit comments