11import type { Reactive } from "vue" ;
22
33import { v4 } from "uuid" ;
4- import { computed , isReactive , nextTick , reactive , watch } from "vue" ;
4+ import { computed , isReactive , nextTick , reactive } from "vue" ;
55
66const configurable = true ;
77export default (
@@ -17,74 +17,62 @@ export default (
1717 siblings : keySiblings = "siblings" ,
1818 } = { } ,
1919) => {
20- const data = ( isReactive ( tree ) ? tree : reactive ( tree ) ) as Reactive <
21- Record < string , unknown > [ ]
22- > ;
23- {
24- const properties = {
25- [ keyBranch ] : {
26- get ( this : Record < string , unknown > ) {
27- const ret = [ this ] ;
28- while ( ret [ 0 ] [ keyParent ] )
29- ret . unshift ( ret [ 0 ] [ keyParent ] as Record < string , unknown > ) ;
30- return ret ;
31- } ,
20+ const properties = {
21+ [ keyBranch ] : {
22+ get ( this : Record < string , unknown > ) {
23+ const ret = [ this ] ;
24+ while ( ret [ 0 ] [ keyParent ] )
25+ ret . unshift ( ret [ 0 ] [ keyParent ] as Record < string , unknown > ) ;
26+ return ret ;
3227 } ,
33- [ keyIndex ] : {
34- get ( this : Record < string , unknown > ) {
35- return ( this [ keySiblings ] as Record < string , unknown > [ ] ) . findIndex (
36- ( { id } ) => this [ keyId ] === id ,
37- ) ;
38- } ,
39- } ,
40- [ keyNext ] : {
41- get ( this : Record < string , unknown > ) {
42- return ( this [ keySiblings ] as Record < string , unknown > [ ] ) [
43- ( this [ keyIndex ] as number ) + 1
44- ] ;
45- } ,
28+ } ,
29+ [ keyIndex ] : {
30+ get ( this : Record < string , unknown > ) {
31+ return ( this [ keySiblings ] as Record < string , unknown > [ ] ) . findIndex (
32+ ( { id } ) => this [ keyId ] === id ,
33+ ) ;
4634 } ,
47- [ keyPrev ] : {
48- get ( this : Record < string , unknown > ) {
49- return ( this [ keySiblings ] as Record < string , unknown > [ ] ) [
50- ( this [ keyIndex ] as number ) - 1
51- ] ;
52- } ,
35+ } ,
36+ [ keyNext ] : {
37+ get ( this : Record < string , unknown > ) {
38+ return ( this [ keySiblings ] as Record < string , unknown > [ ] ) [
39+ ( this [ keyIndex ] as number ) + 1
40+ ] ;
5341 } ,
54- } ;
55- const defineProperties = (
56- siblings : { configurable ?: boolean ; value : Record < string , unknown > [ ] } ,
57- parent : { configurable ?: boolean ; value ?: Record < string , unknown > } = {
58- value : undefined ,
42+ } ,
43+ [ keyPrev ] : {
44+ get ( this : Record < string , unknown > ) {
45+ return ( this [ keySiblings ] as Record < string , unknown > [ ] ) [
46+ ( this [ keyIndex ] as number ) - 1
47+ ] ;
5948 } ,
60- ) => {
61- siblings . value . forEach ( ( value ) => {
62- Object . defineProperties ( value , {
63- ...properties ,
64- [ keyParent ] : parent ,
65- [ keySiblings ] : siblings ,
66- } ) ;
67- defineProperties (
49+ } ,
50+ } ;
51+ const getLeaves : (
52+ siblings : { configurable ?: boolean ; value : Record < string , unknown > [ ] } ,
53+ parent ?: { configurable ?: boolean ; value ?: Record < string , unknown > } ,
54+ ) => Record < string , unknown > [ ] = ( siblings , parent = { value : undefined } ) =>
55+ siblings . value . flatMap ( ( value ) => {
56+ Object . defineProperties ( value , {
57+ ...properties ,
58+ [ keyParent ] : parent ,
59+ [ keySiblings ] : siblings ,
60+ } ) ;
61+ return [
62+ value ,
63+ ...getLeaves (
6864 {
6965 configurable,
7066 value : ( value [ keyChildren ] ?? [ ] ) as Record < string , unknown > [ ] ,
7167 } ,
7268 { configurable, value } ,
73- ) ;
74- } ) ;
75- } ;
76- watch ( data , ( value ) => {
77- defineProperties ( { value } ) ;
69+ ) ,
70+ ] ;
7871 } ) ;
79- }
80- const getLeaves = (
81- leaves : Record < string , unknown > [ ] ,
82- ) : Record < string , unknown > [ ] =>
83- leaves . flatMap ( ( element ) => [
84- element ,
85- ...getLeaves ( ( element [ keyChildren ] ?? [ ] ) as Record < string , unknown > [ ] ) ,
86- ] ) ;
87- const leaves = computed ( ( ) => getLeaves ( data ) ) ;
72+ const value = ( isReactive ( tree ) ? tree : reactive ( tree ) ) as Reactive <
73+ Record < string , unknown > [ ]
74+ > ;
75+ const leaves = computed ( ( ) => getLeaves ( { value } ) ) ;
8876 const up = ( pId : string | undefined ) => {
8977 const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
9078 if ( the ) {
0 commit comments