@@ -15,7 +15,7 @@ import {
15
15
regex ,
16
16
fontSize ,
17
17
placeHolder ,
18
- listComponent ,
18
+ listBlockType ,
19
19
} from '@utils/blockContent' ;
20
20
import { useCommand , useManager } from '@/hooks' ;
21
21
import { focusState } from '@/stores/page' ;
@@ -61,6 +61,7 @@ function BlockContent(blockDTO: Block) {
61
61
const [ blockMap , setBlockMap ] = useRecoilState ( blockMapState ) ;
62
62
const focusId = useRecoilValue ( focusState ) ;
63
63
const caretRef = useRef ( 0 ) ;
64
+ const listCnt = useRef ( 1 ) ;
64
65
const [ Dispatcher ] = useCommand ( ) ;
65
66
const draggingBlock = useRecoilValue ( draggingBlockState ) ;
66
67
const [ { blockIndex } ] = useManager ( blockDTO . id ) ;
@@ -85,6 +86,33 @@ function BlockContent(blockDTO: Block) {
85
86
selection . collapse ( selection . focusNode , caretRef . current ) ;
86
87
} , [ blockDTO . value ] ) ;
87
88
89
+ const indexInSibling : number = blockMap [
90
+ blockDTO . parentId
91
+ ] . childIdList . findIndex ( ( childId : string ) => childId === blockDTO . id ) ;
92
+
93
+ const upperBlocks : Array < Block > = blockMap [ blockDTO . parentId ] . childIdList
94
+ . map ( ( childId : any ) => blockMap [ childId ] )
95
+ . slice ( 0 , indexInSibling )
96
+ . reverse ( ) ;
97
+
98
+ const isUpperBlockEqualToNumberList = ( ) : boolean => {
99
+ if ( upperBlocks . length ) {
100
+ return upperBlocks [ 0 ] . type === BlockType . NUMBERED_LIST ;
101
+ }
102
+ return false ;
103
+ } ;
104
+
105
+ const cntOfUpperNumberListBlock = ( ) : number => {
106
+ let cnt = 0 ;
107
+ for ( const upperblock of upperBlocks ) {
108
+ if ( upperblock . type !== BlockType . NUMBERED_LIST ) break ;
109
+ cnt += 1 ;
110
+ }
111
+ return cnt ;
112
+ } ;
113
+
114
+ const FIRST_LIST_NUMBER = '1' ;
115
+
88
116
const handleBlock = ( value : string , type ?: BlockType ) =>
89
117
type
90
118
? setBlockMap ( {
@@ -103,6 +131,19 @@ function BlockContent(blockDTO: Block) {
103
131
) ;
104
132
105
133
if ( newType ) {
134
+ if ( newType [ 0 ] === BlockType . NUMBERED_LIST ) {
135
+ if ( ! indexInSibling && content [ 0 ] !== FIRST_LIST_NUMBER ) return ;
136
+ if ( indexInSibling ) {
137
+ const numberListUpperBlock = isUpperBlockEqualToNumberList ( ) ;
138
+ if ( ! numberListUpperBlock && content [ 0 ] !== FIRST_LIST_NUMBER ) return ;
139
+ if (
140
+ numberListUpperBlock &&
141
+ cntOfUpperNumberListBlock ( ) + 1 !== + content [ 0 ]
142
+ )
143
+ return ;
144
+ }
145
+ }
146
+
106
147
handleBlock (
107
148
content . slice ( content . indexOf ( ' ' ) + 1 , content . length ) ,
108
149
newType [ 0 ] as BlockType ,
@@ -152,6 +193,37 @@ function BlockContent(blockDTO: Block) {
152
193
}
153
194
} ;
154
195
196
+ useEffect ( ( ) => {
197
+ blockRefState [ blockDTO . id ] = contentEditableRef ;
198
+ return ( ) => {
199
+ blockRefState [ blockDTO . id ] = null ;
200
+ } ;
201
+ } , [ ] ) ;
202
+
203
+ useEffect ( ( ) => {
204
+ if ( focusId === blockDTO . id ) contentEditableRef . current . focus ( ) ;
205
+ } , [ focusId ] ) ;
206
+
207
+ useEffect ( ( ) => {
208
+ const selection = window . getSelection ( ) ;
209
+ const nodeLength = selection . focusNode ?. nodeValue ?. length ?? 0 ;
210
+ if ( caretRef . current > nodeLength ) {
211
+ caretRef . current = nodeLength ;
212
+ }
213
+ selection . collapse ( selection . focusNode , caretRef . current ) ;
214
+
215
+ if ( blockDTO . type === BlockType . NUMBERED_LIST ) {
216
+ const numberListUpperBlock = isUpperBlockEqualToNumberList ( ) ;
217
+ if ( ! indexInSibling || ! numberListUpperBlock ) {
218
+ listCnt . current = 1 ;
219
+ return ;
220
+ }
221
+ if ( numberListUpperBlock ) {
222
+ listCnt . current = cntOfUpperNumberListBlock ( ) + 1 ;
223
+ }
224
+ }
225
+ } , [ blockDTO . value ] ) ;
226
+
155
227
const dropDraggingBlockHandler = async ( ) => {
156
228
const blockId = draggingBlock ?. id ;
157
229
if ( ! blockId || blockId === blockDTO . id ) {
@@ -174,7 +246,7 @@ function BlockContent(blockDTO: Block) {
174
246
175
247
return (
176
248
< div css = { blockContentCSS } onMouseUp = { dropDraggingBlockHandler } >
177
- { listComponent [ blockDTO . type ] }
249
+ { listBlockType ( blockDTO , listCnt . current ) }
178
250
< div
179
251
ref = { contentEditableRef }
180
252
css = { editableDivCSS ( blockDTO ) }
0 commit comments