@@ -4,13 +4,13 @@ import { jsx, css, SerializedStyles } from '@emotion/react';
4
4
import { useEffect , useRef , FormEvent , KeyboardEvent } from 'react' ;
5
5
import { useRecoilState , useRecoilValue } from 'recoil' ;
6
6
7
- import { blockMapState , blockRefState , throttleState } from '@/stores' ;
7
+ import { blockRefState , throttleState , blockMapState } from '@/stores' ;
8
8
import { Block , BlockType } from '@/schemes' ;
9
9
import {
10
10
regex ,
11
11
fontSize ,
12
12
placeHolder ,
13
- listComponent ,
13
+ listBlockType ,
14
14
} from '@utils/blockContent' ;
15
15
import { useCommand } from '@/hooks' ;
16
16
import { focusState } from '@/stores/page' ;
@@ -55,8 +55,36 @@ function BlockContent(blockDTO: Block) {
55
55
const [ blockMap , setBlockMap ] = useRecoilState ( blockMapState ) ;
56
56
const focusId = useRecoilValue ( focusState ) ;
57
57
const caretRef = useRef ( 0 ) ;
58
+ const listCnt = useRef ( 1 ) ;
58
59
const [ Dispatcher ] = useCommand ( ) ;
59
60
61
+ const indexInSibling : number = blockMap [
62
+ blockDTO . parentId
63
+ ] . childIdList . findIndex ( ( childId : string ) => childId === blockDTO . id ) ;
64
+
65
+ const upperBlocks : Array < Block > = blockMap [ blockDTO . parentId ] . childIdList
66
+ . map ( ( childId : any ) => blockMap [ childId ] )
67
+ . slice ( 0 , indexInSibling )
68
+ . reverse ( ) ;
69
+
70
+ const isUpperBlockEqualToNumberList = ( ) : boolean => {
71
+ if ( upperBlocks . length ) {
72
+ return upperBlocks [ 0 ] . type === BlockType . NUMBERED_LIST ;
73
+ }
74
+ return false ;
75
+ } ;
76
+
77
+ const cntOfUpperNumberListBlock = ( ) : number => {
78
+ let cnt = 0 ;
79
+ for ( const upperblock of upperBlocks ) {
80
+ if ( upperblock . type !== BlockType . NUMBERED_LIST ) break ;
81
+ cnt += 1 ;
82
+ }
83
+ return cnt ;
84
+ } ;
85
+
86
+ const FIRST_LIST_NUMBER = '1' ;
87
+
60
88
const handleBlock = ( value : string , type ?: BlockType ) =>
61
89
type
62
90
? setBlockMap ( {
@@ -75,6 +103,19 @@ function BlockContent(blockDTO: Block) {
75
103
) ;
76
104
77
105
if ( newType ) {
106
+ if ( newType [ 0 ] === BlockType . NUMBERED_LIST ) {
107
+ if ( ! indexInSibling && content [ 0 ] !== FIRST_LIST_NUMBER ) return ;
108
+ if ( indexInSibling ) {
109
+ const numberListUpperBlock = isUpperBlockEqualToNumberList ( ) ;
110
+ if ( ! numberListUpperBlock && content [ 0 ] !== FIRST_LIST_NUMBER ) return ;
111
+ if (
112
+ numberListUpperBlock &&
113
+ cntOfUpperNumberListBlock ( ) + 1 !== + content [ 0 ]
114
+ )
115
+ return ;
116
+ }
117
+ }
118
+
78
119
handleBlock (
79
120
content . slice ( content . indexOf ( ' ' ) + 1 , content . length ) ,
80
121
newType [ 0 ] as BlockType ,
@@ -142,11 +183,22 @@ function BlockContent(blockDTO: Block) {
142
183
caretRef . current = nodeLength ;
143
184
}
144
185
selection . collapse ( selection . focusNode , caretRef . current ) ;
186
+
187
+ if ( blockDTO . type === BlockType . NUMBERED_LIST ) {
188
+ const numberListUpperBlock = isUpperBlockEqualToNumberList ( ) ;
189
+ if ( ! indexInSibling || ! numberListUpperBlock ) {
190
+ listCnt . current = 1 ;
191
+ return ;
192
+ }
193
+ if ( numberListUpperBlock ) {
194
+ listCnt . current = cntOfUpperNumberListBlock ( ) + 1 ;
195
+ }
196
+ }
145
197
} , [ blockDTO . value ] ) ;
146
198
147
199
return (
148
200
< div css = { blockContentCSS } >
149
- { listComponent [ blockDTO . type ] }
201
+ { listBlockType ( blockDTO , listCnt . current ) }
150
202
< div
151
203
ref = { contentEditableRef }
152
204
css = { editableDivCSS ( blockDTO ) }
0 commit comments