@@ -2,9 +2,7 @@ import { toReactive } from "@vueuse/core";
22import { v4 } from "uuid" ;
33import { computed , isReactive , reactive } from "vue" ;
44
5- /* -------------------------------------------------------------------------- */
6-
7- export default (
5+ export default function (
86 tree : Record < string , unknown > [ ] ,
97 {
108 branch : keyBranch = "branch" ,
@@ -16,8 +14,10 @@ export default (
1614 prev : keyPrev = "prev" ,
1715 siblings : keySiblings = "siblings" ,
1816 } = { } ,
19- ) => {
20- const configurable = true ,
17+ ) {
18+ const atlas = toReactive ( computed ( getAtlas ) ) ,
19+ configurable = true ,
20+ leaves = computed ( getLeaves ) ,
2121 properties = {
2222 [ keyBranch ] : {
2323 get ( this : Record < string , unknown > ) {
@@ -30,7 +30,7 @@ export default (
3030 [ keyIndex ] : {
3131 get ( this : Record < string , unknown > ) {
3232 return ( this [ keySiblings ] as Record < string , unknown > [ ] ) . findIndex (
33- ( { id } ) => this [ keyId ] === id ,
33+ ( sibling ) => this [ keyId ] === sibling [ keyId ] ,
3434 ) ;
3535 } ,
3636 } ,
@@ -48,157 +48,160 @@ export default (
4848 ] ;
4949 } ,
5050 } ,
51- } ;
51+ } ,
52+ value = isReactive ( tree ) ? tree : reactive ( tree ) ;
53+
54+ function getLeaves ( ) {
55+ return getSiblingLeaves ( { value } ) ;
56+ }
5257
53- const getLeaves = (
58+ function getSiblingLeaves (
5459 siblings : { configurable ?: boolean ; value : Record < string , unknown > [ ] } ,
5560 parent = { } ,
56- ) =>
57- siblings . value . flatMap ( ( value ) : Record < string , unknown > [ ] => {
61+ ) {
62+ function defineProperties (
63+ value : Record < string , unknown > ,
64+ ) : Record < string , unknown > [ ] {
5865 Object . defineProperties ( value , {
5966 ...properties ,
6067 [ keyParent ] : parent ,
6168 [ keySiblings ] : siblings ,
6269 } ) ;
6370 return [
6471 value ,
65- ...getLeaves (
72+ ...getSiblingLeaves (
6673 {
6774 configurable,
6875 value : ( value [ keyChildren ] ?? [ ] ) as Record < string , unknown > [ ] ,
6976 } ,
7077 { configurable, value } ,
7178 ) ,
7279 ] ;
73- } ) ;
80+ }
81+ return siblings . value . flatMap ( defineProperties ) ;
82+ }
7483
75- const value = isReactive ( tree ) ? tree : reactive ( tree ) ;
84+ function getAtlas ( ) {
85+ return Object . fromEntries ( leaves . value . map ( getLeafEntry ) ) ;
86+ }
7687
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+ }
7893
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+ }
86118
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 ;
108149 }
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 ,
116166 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 ;
184169 }
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 ,
192181 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 ;
198187 }
199- } ;
188+ }
189+ return undefined ;
190+ }
200191
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+ }
202205
203206 return { add, atlas, down, leaves, left, remove, right, up } ;
204- } ;
207+ }
0 commit comments