1
- import type { Reactive } from "vue" ;
1
+ /* -------------------------------------------------------------------------- */
2
+ /* Imports */
3
+ /* -------------------------------------------------------------------------- */
4
+
5
+ import type { ComputedRef , Reactive } from "vue" ;
2
6
3
7
import { v4 } from "uuid" ;
4
8
import { computed , isReactive , reactive } from "vue" ;
5
9
10
+ /* -------------------------------------------------------------------------- */
11
+ /* Constants */
12
+ /* -------------------------------------------------------------------------- */
13
+
6
14
const configurable = true ;
7
- export default (
15
+
16
+ /* -------------------------------------------------------------------------- */
17
+ /* Functions */
18
+ /* -------------------------------------------------------------------------- */
19
+
20
+ const useFlatJsonTree : (
8
21
tree : Reactive < Record < string , unknown > [ ] > | Record < string , unknown > [ ] ,
22
+ {
23
+ branch,
24
+ children,
25
+ id,
26
+ index,
27
+ next,
28
+ parent,
29
+ prev,
30
+ siblings,
31
+ } ?: {
32
+ branch ?: string ;
33
+ children ?: string ;
34
+ id ?: string ;
35
+ index ?: string ;
36
+ next ?: string ;
37
+ parent ?: string ;
38
+ prev ?: string ;
39
+ siblings ?: string ;
40
+ } ,
41
+ ) => {
42
+ add : ( pId : string ) => null | string ;
43
+ down : ( pId : string ) => void ;
44
+ leaves : ComputedRef < Record < string , unknown > [ ] > ;
45
+ left : ( pId : string ) => null | string ;
46
+ remove : ( pId : string ) => null | string ;
47
+ right : ( pId : string ) => null | string ;
48
+ up : ( pId : string ) => void ;
49
+ } = (
50
+ tree ,
9
51
{
10
52
branch : keyBranch = "branch" ,
11
53
children : keyChildren = "children" ,
@@ -17,7 +59,11 @@ export default (
17
59
siblings : keySiblings = "siblings" ,
18
60
} = { } ,
19
61
) => {
20
- const properties = {
62
+ /* -------------------------------------------------------------------------- */
63
+ /* Constants */
64
+ /* -------------------------------------------------------------------------- */
65
+
66
+ const properties : PropertyDescriptorMap = {
21
67
[ keyBranch ] : {
22
68
get ( this : Record < string , unknown > ) {
23
69
const ret = [ this ] ;
@@ -48,10 +94,15 @@ export default (
48
94
} ,
49
95
} ,
50
96
} ;
97
+
98
+ /* -------------------------------------------------------------------------- */
99
+ /* Functions */
100
+ /* -------------------------------------------------------------------------- */
101
+
51
102
const getLeaves : (
52
103
siblings : { configurable ?: boolean ; value : Record < string , unknown > [ ] } ,
53
- parent ?: { configurable ?: boolean ; value ?: Record < string , unknown > } ,
54
- ) => Record < string , unknown > [ ] = ( siblings , parent = { value : undefined } ) =>
104
+ parent ?: { configurable ?: boolean ; value : null | Record < string , unknown > } ,
105
+ ) => Record < string , unknown > [ ] = ( siblings , parent = { value : null } ) =>
55
106
siblings . value . flatMap ( ( value ) => {
56
107
Object . defineProperties ( value , {
57
108
...properties ,
@@ -69,11 +120,28 @@ export default (
69
120
) ,
70
121
] ;
71
122
} ) ;
72
- const value = ( isReactive ( tree ) ? tree : reactive ( tree ) ) as Reactive <
73
- Record < string , unknown > [ ]
74
- > ;
75
- const leaves = computed ( ( ) => getLeaves ( { value } ) ) ;
76
- const up = ( pId : string ) => {
123
+
124
+ /* -------------------------------------------------------------------------- */
125
+ /* Constants */
126
+ /* -------------------------------------------------------------------------- */
127
+
128
+ const value : Reactive < Record < string , unknown > [ ] > = isReactive ( tree )
129
+ ? tree
130
+ : reactive ( tree ) ;
131
+
132
+ /* -------------------------------------------------------------------------- */
133
+ /* Computations */
134
+ /* -------------------------------------------------------------------------- */
135
+
136
+ const leaves : ComputedRef < Record < string , unknown > [ ] > = computed ( ( ) =>
137
+ getLeaves ( { value } ) ,
138
+ ) ;
139
+
140
+ /* -------------------------------------------------------------------------- */
141
+ /* Functions */
142
+ /* -------------------------------------------------------------------------- */
143
+
144
+ const up : ( pId : string ) => void = ( pId ) => {
77
145
const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
78
146
if ( the ) {
79
147
const index = the [ keyIndex ] as number ;
@@ -85,7 +153,10 @@ export default (
85
153
] ;
86
154
}
87
155
} ;
88
- const down = ( pId : string ) => {
156
+
157
+ /* -------------------------------------------------------------------------- */
158
+
159
+ const down : ( pId : string ) => void = ( pId ) => {
89
160
const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
90
161
if ( the ) {
91
162
const index = the [ keyIndex ] as number ;
@@ -97,10 +168,13 @@ export default (
97
168
] ;
98
169
}
99
170
} ;
100
- const right = ( pId : string ) => {
101
- const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
171
+
172
+ /* -------------------------------------------------------------------------- */
173
+
174
+ const right : ( pId : string ) => null | string = ( pId : string ) => {
175
+ const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ?? null ;
102
176
if ( the ) {
103
- const prev = the [ keyPrev ] as Record < string , unknown > | undefined ;
177
+ const prev = ( the [ keyPrev ] ?? null ) as null | Record < string , unknown > ;
104
178
if ( prev ) {
105
179
const children = ( prev [ keyChildren ] ?? [ ] ) as Record < string , unknown > [ ] ;
106
180
const id = prev [ keyId ] as string ;
@@ -114,12 +188,15 @@ export default (
114
188
return id ;
115
189
}
116
190
}
117
- return undefined ;
191
+ return null ;
118
192
} ;
119
- const left = ( pId : string ) => {
193
+
194
+ /* -------------------------------------------------------------------------- */
195
+
196
+ const left : ( pId : string ) => null | string = ( pId ) => {
120
197
const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
121
198
if ( the ) {
122
- const parent = the [ keyParent ] as Record < string , unknown > | undefined ;
199
+ const parent = ( the [ keyParent ] ?? null ) as null | Record < string , unknown > ;
123
200
if ( parent ) {
124
201
const siblings = parent [ keySiblings ] as Record < string , unknown > [ ] ;
125
202
if ( parent [ keyParent ] ) {
@@ -134,14 +211,17 @@ export default (
134
211
}
135
212
}
136
213
}
137
- return undefined ;
214
+ return null ;
138
215
} ;
139
- const add = ( pId : string ) => {
216
+
217
+ /* -------------------------------------------------------------------------- */
218
+
219
+ const add : ( pId : string ) => null | string = ( pId ) => {
140
220
const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
141
221
if ( the ) {
142
- const children = the [ keyChildren ] as
143
- | Record < string , unknown > [ ]
144
- | undefined ;
222
+ const children = ( the [ keyChildren ] ?? null ) as
223
+ | null
224
+ | Record < string , unknown > [ ] ;
145
225
const index = the [ keyIndex ] as number ;
146
226
const siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
147
227
const id = v4 ( ) ;
@@ -158,14 +238,17 @@ export default (
158
238
}
159
239
return id ;
160
240
}
161
- return undefined ;
241
+ return null ;
162
242
} ;
163
- const remove = ( pId : string ) => {
243
+
244
+ /* -------------------------------------------------------------------------- */
245
+
246
+ const remove : ( pId : string ) => null | string = ( pId ) => {
164
247
const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
165
248
if ( the ) {
166
- const next = the [ keyNext ] as Record < string , unknown > | undefined ;
167
- const parent = the [ keyParent ] as Record < string , unknown > | undefined ;
168
- const prev = the [ keyPrev ] as Record < string , unknown > | undefined ;
249
+ const next = ( the [ keyNext ] ?? null ) as null | Record < string , unknown > ;
250
+ const parent = ( the [ keyParent ] ?? null ) as null | Record < string , unknown > ;
251
+ const prev = ( the [ keyPrev ] ?? null ) as null | Record < string , unknown > ;
169
252
if ( parent ) {
170
253
let id : string ;
171
254
switch ( true ) {
@@ -186,7 +269,22 @@ export default (
186
269
return id ;
187
270
}
188
271
}
189
- return undefined ;
272
+ return null ;
190
273
} ;
274
+
275
+ /* -------------------------------------------------------------------------- */
276
+ /* Main */
277
+ /* -------------------------------------------------------------------------- */
278
+
191
279
return { add, down, leaves, left, remove, right, up } ;
280
+
281
+ /* -------------------------------------------------------------------------- */
192
282
} ;
283
+
284
+ /* -------------------------------------------------------------------------- */
285
+ /* Exports */
286
+ /* -------------------------------------------------------------------------- */
287
+
288
+ export default useFlatJsonTree ;
289
+
290
+ /* -------------------------------------------------------------------------- */
0 commit comments