1
1
import type { Reactive } from "vue" ;
2
2
3
3
import { v4 } from "uuid" ;
4
- import { computed , isReactive , nextTick , reactive , watch } from "vue" ;
4
+ import { computed , isReactive , nextTick , reactive } from "vue" ;
5
5
6
6
const configurable = true ;
7
7
export default (
@@ -17,74 +17,62 @@ export default (
17
17
siblings : keySiblings = "siblings" ,
18
18
} = { } ,
19
19
) => {
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 ;
32
27
} ,
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
+ ) ;
46
34
} ,
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
+ ] ;
53
41
} ,
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
+ ] ;
59
48
} ,
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 (
68
64
{
69
65
configurable,
70
66
value : ( value [ keyChildren ] ?? [ ] ) as Record < string , unknown > [ ] ,
71
67
} ,
72
68
{ configurable, value } ,
73
- ) ;
74
- } ) ;
75
- } ;
76
- watch ( data , ( value ) => {
77
- defineProperties ( { value } ) ;
69
+ ) ,
70
+ ] ;
78
71
} ) ;
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 } ) ) ;
88
76
const up = ( pId : string | undefined ) => {
89
77
const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
90
78
if ( the ) {
0 commit comments