1
1
// @flow
2
2
3
3
import React , { Component , Fragment } from 'react'
4
- import { EditorState , ContentBlock , DefaultDraftBlockRenderMap , RichUtils } from 'draft-js'
4
+ import {
5
+ EditorState ,
6
+ ContentBlock ,
7
+ DefaultDraftBlockRenderMap ,
8
+ RichUtils ,
9
+ } from 'draft-js'
5
10
import { withPluginContext } from '@djsp/core'
6
11
import type { PluginProps } from '@djsp/core'
7
12
import { mergeBlockData } from '@djsp/utils'
8
- import { CheckableListItem , CheckableListItemBlock , CHECKABLE_LIST_ITEM , blockRenderMap } from 'draft-js-checkable-list-item'
13
+ import {
14
+ CheckableListItem ,
15
+ CheckableListItemBlock ,
16
+ CHECKABLE_LIST_ITEM ,
17
+ blockRenderMap ,
18
+ } from 'draft-js-checkable-list-item'
9
19
10
20
class CheckableList extends Component < PluginProps > {
11
21
_unregister : ( ) => void
12
22
23
+ updateEditorState = ( newEditorState : EditorState ) => {
24
+ const { setEditorState, editorState } = this . props
25
+ setEditorState (
26
+ EditorState . forceSelection ( newEditorState , editorState . getSelection ( ) )
27
+ )
28
+ }
29
+
13
30
onClick = event => {
14
31
event . stopPropagation ( )
15
32
16
- const { setEditorState, editorState } = this . props
17
- setEditorState ( RichUtils . toggleBlockType ( editorState , CHECKABLE_LIST_ITEM ) )
33
+ const { editorState } = this . props
34
+ const newEditorState = RichUtils . toggleBlockType (
35
+ editorState ,
36
+ CHECKABLE_LIST_ITEM
37
+ )
38
+ this . updateEditorState ( newEditorState )
18
39
}
19
40
20
41
toggleChecked = ( block : ContentBlock ) => {
21
- const { setEditorState, editorState } = this . props
22
-
23
- let newEditorState = mergeBlockData ( editorState , block , { checked : ! block . getData ( ) . get ( 'checked' ) } )
24
- setEditorState ( EditorState . forceSelection (
25
- newEditorState ,
26
- editorState . getSelection ( )
27
- ) ) ;
42
+ const { editorState } = this . props
43
+
44
+ let newEditorState = mergeBlockData ( editorState , block , {
45
+ checked : ! block . getData ( ) . get ( 'checked' ) ,
46
+ } )
47
+ this . updateEditorState ( newEditorState )
28
48
}
29
49
30
50
componentDidMount ( ) {
@@ -47,16 +67,34 @@ class CheckableList extends Component<PluginProps> {
47
67
if ( block . getType ( ) === CHECKABLE_LIST_ITEM ) {
48
68
return CHECKABLE_LIST_ITEM
49
69
}
50
- }
70
+ } ,
51
71
} )
52
72
}
53
73
74
+ getStyle = ( ) : object => {
75
+ const { editorState } = this . props
76
+ const selection = editorState . getSelection ( )
77
+ const currentBlockType = editorState
78
+ . getCurrentContent ( )
79
+ . getBlockForKey ( selection . getStartKey ( ) )
80
+ . getType ( )
81
+
82
+ return currentBlockType === CHECKABLE_LIST_ITEM
83
+ ? {
84
+ fontWeight : 'BOLD' ,
85
+ cursor : 'pointer' ,
86
+ }
87
+ : { cursor : 'pointer' }
88
+ }
89
+
54
90
render ( ) {
55
- return ( < Fragment >
56
- < button type = "button" onClick = { this . onClick } >
57
- Toggle checkable-list
58
- </ button >
59
- </ Fragment > )
91
+ return (
92
+ < Fragment >
93
+ < span style = { this . getStyle ( ) } onClick = { this . onClick } >
94
+ ✓
95
+ </ span >
96
+ </ Fragment >
97
+ )
60
98
}
61
99
}
62
100
0 commit comments