1
1
import React from 'react' ;
2
2
import {
3
+ findNodePath ,
4
+ getPluginOptions ,
3
5
InjectComponentProps ,
4
6
InjectComponentReturnType ,
7
+ setNodes ,
5
8
} from '@udecode/plate-common' ;
6
9
import { clsx } from 'clsx' ;
7
10
8
- import { KEY_LIST_START , KEY_LIST_STYLE_TYPE } from './createIndentListPlugin' ;
11
+ import {
12
+ IndentListPlugin ,
13
+ KEY_LIST_CHECKED ,
14
+ KEY_LIST_START ,
15
+ KEY_LIST_STYLE_TYPE ,
16
+ KEY_TODO_STYLE_TYPE ,
17
+ } from './createIndentListPlugin' ;
9
18
import { ListStyleType } from './types' ;
10
19
11
20
export const injectIndentListComponent = (
@@ -16,7 +25,11 @@ export const injectIndentListComponent = (
16
25
const listStyleType = element [ KEY_LIST_STYLE_TYPE ] as string ;
17
26
const listStart = element [ KEY_LIST_START ] as number ;
18
27
19
- if ( listStyleType ) {
28
+ const isTodo =
29
+ element . hasOwnProperty ( KEY_LIST_CHECKED ) &&
30
+ listStyleType === KEY_TODO_STYLE_TYPE ;
31
+
32
+ if ( listStyleType && ! isTodo ) {
20
33
let className = clsx ( `slate-${ KEY_LIST_STYLE_TYPE } -${ listStyleType } ` ) ;
21
34
const style : React . CSSProperties = {
22
35
padding : 0 ,
@@ -50,4 +63,51 @@ export const injectIndentListComponent = (
50
63
) ;
51
64
} ;
52
65
}
66
+
67
+ if ( isTodo ) {
68
+ const className = clsx ( 'slate-list-todo' ) ;
69
+ const checked = element [ KEY_LIST_CHECKED ] as boolean ;
70
+ const style : React . CSSProperties = {
71
+ position : 'relative' ,
72
+ padding : 0 ,
73
+ margin : 0 ,
74
+ } ;
75
+ return function Ol ( { children, editor } ) {
76
+ const { markerComponent } = getPluginOptions < IndentListPlugin > (
77
+ editor ,
78
+ KEY_LIST_STYLE_TYPE
79
+ ) ;
80
+
81
+ return (
82
+ < div className = { `${ className } ` } style = { style } >
83
+ { markerComponent ? (
84
+ markerComponent ( {
85
+ checked : checked ,
86
+ onChange : ( v : boolean ) => {
87
+ const path = findNodePath ( editor , element ) ;
88
+ setNodes ( editor , { checked : v } , { at : path } ) ;
89
+ } ,
90
+ } )
91
+ ) : (
92
+ < input
93
+ contentEditable = { false }
94
+ data-slate-void
95
+ type = "checkbox"
96
+ style = { {
97
+ marginRight : 5 ,
98
+ marginLeft : - 17 ,
99
+ paddingTop : - 10 ,
100
+ } }
101
+ checked = { checked }
102
+ onChange = { ( v ) => {
103
+ const path = findNodePath ( editor , element ) ;
104
+ setNodes ( editor , { checked : v . target . checked } , { at : path } ) ;
105
+ } }
106
+ />
107
+ ) }
108
+ < span > { children } </ span >
109
+ </ div >
110
+ ) ;
111
+ } ;
112
+ }
53
113
} ;
0 commit comments