@@ -2,11 +2,15 @@ import { Block, BlockType, BlockFamily, FamilyFunc, BlockMap } from '@/schemes';
2
2
import { useFamily } from '@/hooks' ;
3
3
4
4
interface ManagerFunc {
5
- addChild : ( option ?: any , insertIndex ?: number ) => Block ;
6
- addSibling : ( option ?: any , insertIndex ?: number ) => Block ;
7
- setBlock : ( option ?: any ) => void ;
5
+ insertNewChild : ( option ?: any , insertIndex ?: number ) => Block ;
6
+ insertNewSibling : ( option ?: any , insertIndex ?: number ) => Block ;
7
+ insertSibling : ( id : string , inserIndex ?: number ) => Block ;
8
+ setBlock : ( id : string , option ?: any ) => Block ;
8
9
startTransaction : ( ) => void ;
9
10
commitTransaction : ( ) => void ;
11
+ pullIn : ( ) => Block ;
12
+ pullOut : ( ) => Block ;
13
+ deleteBlock : ( ) => string [ ] ;
10
14
}
11
15
12
16
const useManger = (
@@ -36,13 +40,15 @@ const useManger = (
36
40
familyFunc . setBlockMap ( transaction ) ;
37
41
} ;
38
42
39
- const setBlock = ( option : any = { } ) => {
40
- transaction [ block . id ] = {
41
- ...block ,
43
+ const setBlock = ( id : string , option : any = { } ) => {
44
+ transaction [ id ] = {
45
+ ...transaction [ id ] ,
42
46
...option ,
43
47
} ;
48
+ return transaction [ id ] ;
44
49
} ;
45
- const addChild = ( option : any = { } , insertIndex = 0 ) : Block => {
50
+
51
+ const insertNewChild = ( option : any = { } , insertIndex = 0 ) : Block => {
46
52
const id = `${ block . id } ${ children . length + 1 } _${ Date . now ( ) } ` ;
47
53
const newBlock : Block = {
48
54
id,
@@ -53,17 +59,28 @@ const useManger = (
53
59
pageId : page . id ,
54
60
...option ,
55
61
} ;
56
- transaction [ id ] = newBlock ;
62
+ setBlock ( id , newBlock ) ;
57
63
const copyChildIdList = [ ...childrenIdList ] ;
58
64
copyChildIdList . splice ( insertIndex , 0 , id ) ;
59
- transaction [ block . id ] = {
60
- ...transaction [ block . id ] ,
65
+ setBlock ( block . id , {
61
66
childIdList : copyChildIdList ,
62
- } ;
67
+ } ) ;
63
68
return newBlock ;
64
69
} ;
65
70
66
- const addSibling = (
71
+ const insertSibling = ( id : string , insertIndex : number = 0 ) => {
72
+ const copySiblingsIdList = transaction [ parent . id ] . childIdList ;
73
+ copySiblingsIdList . splice ( insertIndex , 0 , id ) ;
74
+ setBlock ( parent . id , {
75
+ childrenIdList : copySiblingsIdList ,
76
+ } ) ;
77
+ setBlock ( id , {
78
+ parentId : parent . id ,
79
+ } ) ;
80
+ return transaction [ id ] ;
81
+ } ;
82
+
83
+ const insertNewSibling = (
67
84
option : any = { } ,
68
85
insertIndex = blockIndex + 1 ,
69
86
) : Block => {
@@ -80,22 +97,58 @@ const useManger = (
80
97
transaction [ id ] = newBlock ;
81
98
const copySiblingsIdList = [ ...siblingsIdList ] ;
82
99
copySiblingsIdList . splice ( insertIndex , 0 , id ) ;
83
- transaction [ parent . id ] = {
84
- ...parent ,
100
+ setBlock ( parent . id , {
85
101
childIdList : copySiblingsIdList ,
86
- } ;
102
+ } ) ;
87
103
return newBlock ;
88
104
} ;
89
105
106
+ const deleteBlock = ( ) => {
107
+ const filteredSiblingsIdList = siblingsIdList . filter (
108
+ ( id ) => id !== block . id ,
109
+ ) ;
110
+ setBlock ( parent . id , {
111
+ childIdList : filteredSiblingsIdList ,
112
+ } ) ;
113
+ return [ ...transaction [ block . id ] . childIdList ] ;
114
+ } ;
115
+
116
+ const pullIn = ( ) => {
117
+ if ( blockIndex ) {
118
+ const targetSibling = siblings [ blockIndex - 1 ] ;
119
+ setBlock ( siblingsIdList [ blockIndex - 1 ] , {
120
+ childIdList : [ ...targetSibling . childIdList , block . id ] ,
121
+ } ) ;
122
+ deleteBlock ( ) ;
123
+ setBlock ( block . id , { parentId : siblingsIdList [ blockIndex - 1 ] } ) ;
124
+ }
125
+ return block ;
126
+ } ;
127
+
128
+ const pullOut = ( ) => {
129
+ if ( grandParent && grandParent . type !== BlockType . GRID ) {
130
+ deleteBlock ( ) ;
131
+ const copyParentsIdList = [ ...parentsIdList ] ;
132
+ copyParentsIdList . splice ( parentIndex + 1 , 0 , block . id ) ;
133
+ setBlock ( block . id , { parentId : grandParent . id } ) ;
134
+ setBlock ( grandParent . id , { childIdList : copyParentsIdList } ) ;
135
+ }
136
+ return block ;
137
+ } ;
138
+
90
139
return [
91
140
family ,
92
141
{
93
142
...familyFunc ,
94
- addChild,
95
- addSibling,
143
+ insertNewChild,
144
+ insertNewSibling,
145
+ insertSibling,
96
146
setBlock,
97
147
startTransaction,
98
148
commitTransaction,
149
+ pullIn,
150
+ pullOut,
151
+ deleteBlock,
99
152
} ,
100
153
] ;
101
154
} ;
0 commit comments