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