1
1
import { toReactive } from "@vueuse/core" ;
2
2
import { v4 } from "uuid" ;
3
3
import { computed , isReactive , reactive } from "vue" ;
4
-
5
4
export default (
6
5
tree : Record < string , unknown > [ ] ,
7
6
{
@@ -16,47 +15,39 @@ export default (
16
15
} = { } ,
17
16
) => {
18
17
const configurable : PropertyDescriptor [ "configurable" ] = true ,
19
- properties : PropertyDescriptorMap = {
20
- [ keyBranch ] : {
21
- get ( this : Record < string , unknown > ) {
22
- const ret = [ this ] ;
23
- while ( ret [ 0 ] ?. [ keyParent ] )
24
- ret . unshift ( ret [ 0 ] [ keyParent ] as Record < string , unknown > ) ;
25
- return ret ;
26
- } ,
27
- } ,
28
- [ keyIndex ] : {
29
- get ( this : Record < string , unknown > ) {
30
- return ( this [ keySiblings ] as Record < string , unknown > [ ] ) . findIndex (
31
- ( sibling ) => this [ keyId ] === sibling [ keyId ] ,
32
- ) ;
33
- } ,
34
- } ,
35
- [ keyNext ] : {
36
- get ( this : Record < string , unknown > ) {
37
- return ( this [ keySiblings ] as Record < string , unknown > [ ] ) [
38
- ( this [ keyIndex ] as number ) + 1
39
- ] ;
40
- } ,
41
- } ,
42
- [ keyPrev ] : {
43
- get ( this : Record < string , unknown > ) {
44
- return ( this [ keySiblings ] as Record < string , unknown > [ ] ) [
45
- ( this [ keyIndex ] as number ) - 1
46
- ] ;
47
- } ,
48
- } ,
49
- } ,
50
- value = isReactive ( tree ) ? tree : reactive ( tree ) ;
51
-
52
- const getLeaves = (
18
+ getLeaves = (
53
19
siblings : { configurable ?: boolean ; value : Record < string , unknown > [ ] } ,
54
20
parent = { } ,
55
21
) =>
56
22
siblings . value . flatMap ( ( value ) : Record < string , unknown > [ ] => {
57
23
Object . defineProperties ( value , {
58
- ...properties ,
24
+ [ keyBranch ] : {
25
+ get : ( ) => {
26
+ const ret = [ value ] ;
27
+ while ( ret [ 0 ] ?. [ keyParent ] )
28
+ ret . unshift ( ret [ 0 ] [ keyParent ] as Record < string , unknown > ) ;
29
+ return ret ;
30
+ } ,
31
+ } ,
32
+ [ keyIndex ] : {
33
+ get : ( ) =>
34
+ ( value [ keySiblings ] as Record < string , unknown > [ ] ) . findIndex (
35
+ ( sibling ) => value [ keyId ] === sibling [ keyId ] ,
36
+ ) ,
37
+ } ,
38
+ [ keyNext ] : {
39
+ get : ( ) =>
40
+ ( value [ keySiblings ] as Record < string , unknown > [ ] ) [
41
+ ( value [ keyIndex ] as number ) + 1
42
+ ] ,
43
+ } ,
59
44
[ keyParent ] : parent ,
45
+ [ keyPrev ] : {
46
+ get : ( ) =>
47
+ ( value [ keySiblings ] as Record < string , unknown > [ ] ) [
48
+ ( value [ keyIndex ] as number ) - 1
49
+ ] ,
50
+ } ,
60
51
[ keySiblings ] : siblings ,
61
52
} ) ;
62
53
return [
@@ -70,18 +61,18 @@ export default (
70
61
) ,
71
62
] ;
72
63
} ) ,
73
- leaves = computed ( ( ) => getLeaves ( { value } ) ) ;
74
-
75
- const arrLeaves = toReactive ( leaves ) ,
64
+ leaves = computed ( ( ) =>
65
+ getLeaves ( { value : isReactive ( tree ) ? tree : reactive ( tree ) } ) ,
66
+ ) ,
76
67
objLeaves = toReactive (
77
68
computed ( ( ) =>
78
69
Object . fromEntries (
79
70
leaves . value . map ( ( leaf ) => [ leaf [ keyId ] as string , leaf ] ) ,
80
71
) ,
81
72
) ,
82
73
) ;
83
-
84
- const add = ( pId : string ) => {
74
+ return {
75
+ add : ( pId : string ) => {
85
76
const the = objLeaves [ pId ] ;
86
77
if ( the ) {
87
78
const children = the [ keyChildren ] as
@@ -105,7 +96,8 @@ export default (
105
96
}
106
97
return undefined ;
107
98
} ,
108
- down = ( pId : string ) => {
99
+ arrLeaves : toReactive ( leaves ) ,
100
+ down : ( pId : string ) => {
109
101
const the = objLeaves [ pId ] ;
110
102
if ( the ) {
111
103
const index = the [ keyIndex ] as number ,
@@ -122,7 +114,8 @@ export default (
122
114
] ;
123
115
}
124
116
} ,
125
- left = ( pId : string ) => {
117
+ leaves,
118
+ left : ( pId : string ) => {
126
119
const the = objLeaves [ pId ] ;
127
120
if ( the ) {
128
121
const parent = the [ keyParent ] as Record < string , unknown > | undefined ;
@@ -142,7 +135,8 @@ export default (
142
135
}
143
136
return undefined ;
144
137
} ,
145
- remove = ( pId : string ) => {
138
+ objLeaves,
139
+ remove : ( pId : string ) => {
146
140
const the = objLeaves [ pId ] ;
147
141
if ( the ) {
148
142
const parent = the [ keyParent ] as Record < string , unknown > | undefined ;
@@ -161,7 +155,7 @@ export default (
161
155
}
162
156
return undefined ;
163
157
} ,
164
- right = ( pId : string ) => {
158
+ right : ( pId : string ) => {
165
159
const the = objLeaves [ pId ] ;
166
160
if ( the ) {
167
161
const prev = the [ keyPrev ] as Record < string , unknown > | undefined ;
@@ -181,7 +175,7 @@ export default (
181
175
}
182
176
return undefined ;
183
177
} ,
184
- up = ( pId : string ) => {
178
+ up : ( pId : string ) => {
185
179
const the = objLeaves [ pId ] ;
186
180
if ( the ) {
187
181
const index = the [ keyIndex ] as number ,
@@ -193,7 +187,6 @@ export default (
193
187
siblings [ prevIndex ] ,
194
188
] ;
195
189
}
196
- } ;
197
-
198
- return { add, arrLeaves, down, leaves, left, objLeaves, remove, right, up } ;
190
+ } ,
191
+ } ;
199
192
} ;
0 commit comments