11// @flow
22
33import 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'
510import { withPluginContext } from '@djsp/core'
611import type { PluginProps } from '@djsp/core'
712import { 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'
919
1020class CheckableList extends Component < PluginProps > {
1121 _unregister : ( ) => void
1222
23+ updateEditorState = ( newEditorState : EditorState ) => {
24+ const { setEditorState, editorState } = this . props
25+ setEditorState (
26+ EditorState . forceSelection ( newEditorState , editorState . getSelection ( ) )
27+ )
28+ }
29+
1330 onClick = event => {
1431 event . stopPropagation ( )
1532
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 )
1839 }
1940
2041 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 )
2848 }
2949
3050 componentDidMount ( ) {
@@ -47,16 +67,34 @@ class CheckableList extends Component<PluginProps> {
4767 if ( block . getType ( ) === CHECKABLE_LIST_ITEM ) {
4868 return CHECKABLE_LIST_ITEM
4969 }
50- }
70+ } ,
5171 } )
5272 }
5373
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+
5490 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+ )
6098 }
6199}
62100
0 commit comments