@@ -2,9 +2,7 @@ import { toReactive } from "@vueuse/core";
2
2
import { v4 } from "uuid" ;
3
3
import { computed , isReactive , reactive } from "vue" ;
4
4
5
- /* -------------------------------------------------------------------------- */
6
-
7
- export default (
5
+ export default function (
8
6
tree : Record < string , unknown > [ ] ,
9
7
{
10
8
branch : keyBranch = "branch" ,
@@ -16,8 +14,10 @@ export default (
16
14
prev : keyPrev = "prev" ,
17
15
siblings : keySiblings = "siblings" ,
18
16
} = { } ,
19
- ) => {
20
- const configurable = true ,
17
+ ) {
18
+ const atlas = toReactive ( computed ( getAtlas ) ) ,
19
+ configurable = true ,
20
+ leaves = computed ( getLeaves ) ,
21
21
properties = {
22
22
[ keyBranch ] : {
23
23
get ( this : Record < string , unknown > ) {
@@ -30,7 +30,7 @@ export default (
30
30
[ keyIndex ] : {
31
31
get ( this : Record < string , unknown > ) {
32
32
return ( this [ keySiblings ] as Record < string , unknown > [ ] ) . findIndex (
33
- ( { id } ) => this [ keyId ] === id ,
33
+ ( sibling ) => this [ keyId ] === sibling [ keyId ] ,
34
34
) ;
35
35
} ,
36
36
} ,
@@ -48,157 +48,160 @@ export default (
48
48
] ;
49
49
} ,
50
50
} ,
51
- } ;
51
+ } ,
52
+ value = isReactive ( tree ) ? tree : reactive ( tree ) ;
53
+
54
+ function getLeaves ( ) {
55
+ return getSiblingLeaves ( { value } ) ;
56
+ }
52
57
53
- const getLeaves = (
58
+ function getSiblingLeaves (
54
59
siblings : { configurable ?: boolean ; value : Record < string , unknown > [ ] } ,
55
60
parent = { } ,
56
- ) =>
57
- siblings . value . flatMap ( ( value ) : Record < string , unknown > [ ] => {
61
+ ) {
62
+ function defineProperties (
63
+ value : Record < string , unknown > ,
64
+ ) : Record < string , unknown > [ ] {
58
65
Object . defineProperties ( value , {
59
66
...properties ,
60
67
[ keyParent ] : parent ,
61
68
[ keySiblings ] : siblings ,
62
69
} ) ;
63
70
return [
64
71
value ,
65
- ...getLeaves (
72
+ ...getSiblingLeaves (
66
73
{
67
74
configurable,
68
75
value : ( value [ keyChildren ] ?? [ ] ) as Record < string , unknown > [ ] ,
69
76
} ,
70
77
{ configurable, value } ,
71
78
) ,
72
79
] ;
73
- } ) ;
80
+ }
81
+ return siblings . value . flatMap ( defineProperties ) ;
82
+ }
74
83
75
- const value = isReactive ( tree ) ? tree : reactive ( tree ) ;
84
+ function getAtlas ( ) {
85
+ return Object . fromEntries ( leaves . value . map ( getLeafEntry ) ) ;
86
+ }
76
87
77
- const leaves = computed ( ( ) => getLeaves ( { value } ) ) ;
88
+ function getLeafEntry (
89
+ leaf : Record < string , unknown > ,
90
+ ) : [ string , Record < string , unknown > ] {
91
+ return [ leaf [ keyId ] as string , leaf ] ;
92
+ }
78
93
79
- const atlas : Record < string , Record < string , unknown > > = toReactive (
80
- computed ( ( ) =>
81
- Object . fromEntries (
82
- leaves . value . map ( ( leaf ) => [ leaf [ keyId ] as string , leaf ] ) ,
83
- ) ,
84
- ) ,
85
- ) ;
94
+ function add ( pId : string ) {
95
+ const the = atlas [ pId ] ;
96
+ if ( the ) {
97
+ const children = the [ keyChildren ] as
98
+ | Record < string , unknown > [ ]
99
+ | undefined ,
100
+ index = the [ keyIndex ] as number ,
101
+ siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
102
+ const id = v4 ( ) ;
103
+ switch ( true ) {
104
+ case ! ! the [ keyParent ] :
105
+ siblings . splice ( index + 1 , 0 , { [ keyId ] : id } ) ;
106
+ break ;
107
+ case ! ! children :
108
+ children . unshift ( { [ keyId ] : id } ) ;
109
+ break ;
110
+ default :
111
+ siblings . splice ( index + 1 , 0 , { [ keyId ] : id } ) ;
112
+ break ;
113
+ }
114
+ return id ;
115
+ }
116
+ return undefined ;
117
+ }
86
118
87
- const add = ( pId : string ) => {
88
- const the = atlas [ pId ] ;
89
- if ( the ) {
90
- const children = the [ keyChildren ] as
91
- | Record < string , unknown > [ ]
92
- | undefined ,
93
- index = the [ keyIndex ] as number ,
94
- siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
95
- const id = v4 ( ) ;
96
- switch ( true ) {
97
- case ! ! the [ keyParent ] :
98
- siblings . splice ( index + 1 , 0 , { id } ) ;
99
- break ;
100
- case ! ! children :
101
- children . unshift ( { id } ) ;
102
- break ;
103
- default :
104
- siblings . splice ( index + 1 , 0 , { id } ) ;
105
- break ;
106
- }
107
- return id ;
119
+ function down ( pId : string ) {
120
+ const the = atlas [ pId ] ;
121
+ if ( the ) {
122
+ const index = the [ keyIndex ] as number ,
123
+ nextIndex = index + 1 ,
124
+ siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
125
+ if ( index < siblings . length - 1 && siblings [ index ] && siblings [ nextIndex ] )
126
+ [ siblings [ index ] , siblings [ nextIndex ] ] = [
127
+ siblings [ nextIndex ] ,
128
+ siblings [ index ] ,
129
+ ] ;
130
+ }
131
+ }
132
+
133
+ function left ( pId : string ) {
134
+ const the = atlas [ pId ] ;
135
+ if ( the ) {
136
+ const parent = the [ keyParent ] as Record < string , unknown > | undefined ;
137
+ if ( parent ?. [ keyParent ] ) {
138
+ const children = ( parent [ keyChildren ] ?? [ ] ) as Record <
139
+ string ,
140
+ unknown
141
+ > [ ] ,
142
+ siblings = parent [ keySiblings ] as Record < string , unknown > [ ] ;
143
+ siblings . splice (
144
+ ( parent [ keyIndex ] as number ) + 1 ,
145
+ 0 ,
146
+ ...children . splice ( the [ keyIndex ] as number , 1 ) ,
147
+ ) ;
148
+ return parent [ keyId ] as string ;
108
149
}
109
- return undefined ;
110
- } ,
111
- down = ( pId : string ) => {
112
- const the = atlas [ pId ] ;
113
- if ( the ) {
114
- const index = the [ keyIndex ] as number ,
115
- nextIndex = index + 1 ,
150
+ }
151
+ return undefined ;
152
+ }
153
+
154
+ function remove ( pId : string ) {
155
+ const the = atlas [ pId ] ;
156
+ if ( the ) {
157
+ const parent = the [ keyParent ] as Record < string , unknown > | undefined ;
158
+ if ( parent ) {
159
+ const [ root ] = leaves . value ,
160
+ next = the [ keyNext ] as Record < string , unknown > | undefined ,
161
+ prev = the [ keyPrev ] as Record < string , unknown > | undefined ,
162
+ id = ( next ?. [ keyId ] ??
163
+ prev ?. [ keyId ] ??
164
+ parent [ keyId ] ??
165
+ root ?. [ keyId ] ) as string ,
116
166
siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
117
- if (
118
- index < siblings . length - 1 &&
119
- siblings [ index ] &&
120
- siblings [ nextIndex ]
121
- )
122
- [ siblings [ index ] , siblings [ nextIndex ] ] = [
123
- siblings [ nextIndex ] ,
124
- siblings [ index ] ,
125
- ] ;
126
- }
127
- } ,
128
- left = ( pId : string ) => {
129
- const the = atlas [ pId ] ;
130
- if ( the ) {
131
- const parent = the [ keyParent ] as Record < string , unknown > | undefined ;
132
- if ( parent ?. [ keyParent ] ) {
133
- const children = ( parent [ keyChildren ] ?? [ ] ) as Record <
134
- string ,
135
- unknown
136
- > [ ] ,
137
- siblings = parent [ keySiblings ] as Record < string , unknown > [ ] ;
138
- siblings . splice (
139
- ( parent [ keyIndex ] as number ) + 1 ,
140
- 0 ,
141
- ...children . splice ( the [ keyIndex ] as number , 1 ) ,
142
- ) ;
143
- return parent [ keyId ] as string ;
144
- }
145
- }
146
- return undefined ;
147
- } ,
148
- remove = ( pId : string ) => {
149
- const the = atlas [ pId ] ;
150
- if ( the ) {
151
- const parent = the [ keyParent ] as Record < string , unknown > | undefined ;
152
- if ( parent ) {
153
- const [ root ] = leaves . value ,
154
- next = the [ keyNext ] as Record < string , unknown > | undefined ,
155
- prev = the [ keyPrev ] as Record < string , unknown > | undefined ,
156
- id = ( next ?. [ keyId ] ??
157
- prev ?. [ keyId ] ??
158
- parent [ keyId ] ??
159
- root ?. [ keyId ] ) as string ,
160
- siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
161
- siblings . splice ( the [ keyIndex ] as number , 1 ) ;
162
- return id ;
163
- }
164
- }
165
- return undefined ;
166
- } ,
167
- right = ( pId : string ) => {
168
- const the = atlas [ pId ] ;
169
- if ( the ) {
170
- const prev = the [ keyPrev ] as Record < string , unknown > | undefined ;
171
- if ( prev ) {
172
- const children = ( prev [ keyChildren ] ?? [ ] ) as Record <
173
- string ,
174
- unknown
175
- > [ ] ,
176
- id = prev [ keyId ] as string ,
177
- siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
178
- prev [ keyChildren ] = [
179
- ...children ,
180
- ...siblings . splice ( the [ keyIndex ] as number , 1 ) ,
181
- ] ;
182
- return id ;
183
- }
167
+ siblings . splice ( the [ keyIndex ] as number , 1 ) ;
168
+ return id ;
184
169
}
185
- return undefined ;
186
- } ,
187
- up = ( pId : string ) => {
188
- const the = atlas [ pId ] ;
189
- if ( the ) {
190
- const index = the [ keyIndex ] as number ,
191
- prevIndex = index - 1 ,
170
+ }
171
+ return undefined ;
172
+ }
173
+
174
+ function right ( pId : string ) {
175
+ const the = atlas [ pId ] ;
176
+ if ( the ) {
177
+ const prev = the [ keyPrev ] as Record < string , unknown > | undefined ;
178
+ if ( prev ) {
179
+ const children = ( prev [ keyChildren ] ?? [ ] ) as Record < string , unknown > [ ] ,
180
+ id = prev [ keyId ] as string ,
192
181
siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
193
- if ( index && siblings [ index ] && siblings [ prevIndex ] )
194
- [ siblings [ prevIndex ] , siblings [ index ] ] = [
195
- siblings [ index ] ,
196
- siblings [ prevIndex ] ,
197
- ] ;
182
+ prev [ keyChildren ] = [
183
+ ... children ,
184
+ ... siblings . splice ( the [ keyIndex ] as number , 1 ) ,
185
+ ] ;
186
+ return id ;
198
187
}
199
- } ;
188
+ }
189
+ return undefined ;
190
+ }
200
191
201
- /* -------------------------------------------------------------------------- */
192
+ function up ( pId : string ) {
193
+ const the = atlas [ pId ] ;
194
+ if ( the ) {
195
+ const index = the [ keyIndex ] as number ,
196
+ prevIndex = index - 1 ,
197
+ siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
198
+ if ( index && siblings [ index ] && siblings [ prevIndex ] )
199
+ [ siblings [ prevIndex ] , siblings [ index ] ] = [
200
+ siblings [ index ] ,
201
+ siblings [ prevIndex ] ,
202
+ ] ;
203
+ }
204
+ }
202
205
203
206
return { add, atlas, down, leaves, left, remove, right, up } ;
204
- } ;
207
+ }
0 commit comments