@@ -77,14 +77,18 @@ export default (
77
77
78
78
const leaves = computed ( ( ) => getLeaves ( { value } ) ) ;
79
79
80
+ const atlas = computed (
81
+ ( ) => new Map ( leaves . value . map ( ( leaf ) => [ leaf [ keyId ] , leaf ] ) ) ,
82
+ ) ;
83
+
80
84
const add = ( pId : string ) => {
81
- const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
85
+ const the = atlas . value . get ( pId ) ;
82
86
if ( the ) {
83
87
const children = the [ keyChildren ] as
84
- | Record < string , unknown > [ ]
85
- | undefined ;
86
- const index = the [ keyIndex ] as number ;
87
- const siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
88
+ | Record < string , unknown > [ ]
89
+ | undefined ,
90
+ index = the [ keyIndex ] as number ,
91
+ siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
88
92
const id = v4 ( ) ;
89
93
switch ( true ) {
90
94
case ! ! the [ keyParent ] :
@@ -102,11 +106,11 @@ export default (
102
106
return undefined ;
103
107
} ,
104
108
down = ( pId : string ) => {
105
- const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
109
+ const the = atlas . value . get ( pId ) ;
106
110
if ( the ) {
107
- const index = the [ keyIndex ] as number ;
108
- const nextIndex = index + 1 ;
109
- const siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
111
+ const index = the [ keyIndex ] as number ,
112
+ nextIndex = index + 1 ,
113
+ siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
110
114
if (
111
115
index < siblings . length - 1 &&
112
116
siblings [ index ] &&
@@ -119,81 +123,70 @@ export default (
119
123
}
120
124
} ,
121
125
left = ( pId : string ) => {
122
- const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
126
+ const the = atlas . value . get ( pId ) ;
123
127
if ( the ) {
124
128
const parent = the [ keyParent ] as Record < string , unknown > | undefined ;
125
- if ( parent ) {
126
- const siblings = parent [ keySiblings ] as Record < string , unknown > [ ] ;
127
- if ( parent [ keyParent ] ) {
128
- siblings . splice (
129
- ( parent [ keyIndex ] as number ) + 1 ,
130
- 0 ,
131
- ... (
132
- ( parent [ keyChildren ] ?? [ ] ) as Record < string , unknown > [ ]
133
- ) . splice ( the [ keyIndex ] as number , 1 ) ,
134
- ) ;
135
- return parent [ keyId ] as string ;
136
- }
129
+ if ( parent ?. [ keyParent ] ) {
130
+ const children = ( parent [ keyChildren ] ?? [ ] ) as Record <
131
+ string ,
132
+ unknown
133
+ > [ ] ,
134
+ siblings = parent [ keySiblings ] as Record < string , unknown > [ ] ;
135
+ siblings . splice (
136
+ ( parent [ keyIndex ] as number ) + 1 ,
137
+ 0 ,
138
+ ... children . splice ( the [ keyIndex ] as number , 1 ) ,
139
+ ) ;
140
+ return parent [ keyId ] as string ;
137
141
}
138
142
}
139
143
return undefined ;
140
144
} ,
141
145
remove = ( pId : string ) => {
142
- const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
146
+ const the = atlas . value . get ( pId ) ;
143
147
if ( the ) {
144
- const next = the [ keyNext ] ;
145
- const parent = the [ keyParent ] ;
146
- const prev = the [ keyPrev ] ;
148
+ const parent = the [ keyParent ] as Record < string , unknown > | undefined ;
147
149
if ( parent ) {
148
- let id : string ;
149
- switch ( true ) {
150
- case ! ! next :
151
- ( { id } = next as { id : string } ) ;
152
- break ;
153
- case ! ! prev :
154
- ( { id } = prev as { id : string } ) ;
155
- break ;
156
- default :
157
- ( { id } = parent as { id : string } ) ;
158
- }
159
- ( the [ keySiblings ] as Record < string , unknown > [ ] ) . splice (
160
- the [ keyIndex ] as number ,
161
- 1 ,
162
- ) ;
163
- if ( ! id ) [ { id } ] = leaves . value as [ { id : string } ] ;
150
+ const [ root ] = leaves . value ,
151
+ next = the [ keyNext ] as Record < string , unknown > | undefined ,
152
+ prev = the [ keyPrev ] as Record < string , unknown > | undefined ,
153
+ id = ( next ?. [ keyId ] ??
154
+ prev ?. [ keyId ] ??
155
+ parent [ keyId ] ??
156
+ root ?. [ keyId ] ) as string ,
157
+ siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
158
+ siblings . splice ( the [ keyIndex ] as number , 1 ) ;
164
159
return id ;
165
160
}
166
161
}
167
162
return undefined ;
168
163
} ,
169
164
right = ( pId : string ) => {
170
- const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
165
+ const the = atlas . value . get ( pId ) ;
171
166
if ( the ) {
172
167
const prev = the [ keyPrev ] as Record < string , unknown > | undefined ;
173
168
if ( prev ) {
174
169
const children = ( prev [ keyChildren ] ?? [ ] ) as Record <
175
- string ,
176
- unknown
177
- > [ ] ;
178
- const id = prev [ keyId ] as string ;
170
+ string ,
171
+ unknown
172
+ > [ ] ,
173
+ id = prev [ keyId ] as string ,
174
+ siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
179
175
prev [ keyChildren ] = [
180
176
...children ,
181
- ...( the [ keySiblings ] as Record < string , unknown > [ ] ) . splice (
182
- the [ keyIndex ] as number ,
183
- 1 ,
184
- ) ,
177
+ ...siblings . splice ( the [ keyIndex ] as number , 1 ) ,
185
178
] ;
186
179
return id ;
187
180
}
188
181
}
189
182
return undefined ;
190
183
} ,
191
184
up = ( pId : string ) => {
192
- const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
185
+ const the = atlas . value . get ( pId ) ;
193
186
if ( the ) {
194
- const index = the [ keyIndex ] as number ;
195
- const prevIndex = index - 1 ;
196
- const siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
187
+ const index = the [ keyIndex ] as number ,
188
+ prevIndex = index - 1 ,
189
+ siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
197
190
if ( index && siblings [ index ] && siblings [ prevIndex ] )
198
191
[ siblings [ prevIndex ] , siblings [ index ] ] = [
199
192
siblings [ index ] ,
@@ -204,5 +197,5 @@ export default (
204
197
205
198
/* -------------------------------------------------------------------------- */
206
199
207
- return { add, down, leaves, left, remove, right, up } ;
200
+ return { add, atlas , down, leaves, left, remove, right, up } ;
208
201
} ;
0 commit comments