Skip to content

Commit e44fd18

Browse files
author
Jerry Bruwes
committed
modified: index.ts
1 parent 39cf8a6 commit e44fd18

File tree

1 file changed

+42
-49
lines changed

1 file changed

+42
-49
lines changed

index.ts

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { toReactive } from "@vueuse/core";
22
import { v4 } from "uuid";
33
import { computed, isReactive, reactive } from "vue";
4-
54
export default (
65
tree: Record<string, unknown>[],
76
{
@@ -16,47 +15,39 @@ export default (
1615
} = {},
1716
) => {
1817
const configurable: PropertyDescriptor["configurable"] = true,
19-
properties: PropertyDescriptorMap = {
20-
[keyBranch]: {
21-
get(this: Record<string, unknown>) {
22-
const ret = [this];
23-
while (ret[0]?.[keyParent])
24-
ret.unshift(ret[0][keyParent] as Record<string, unknown>);
25-
return ret;
26-
},
27-
},
28-
[keyIndex]: {
29-
get(this: Record<string, unknown>) {
30-
return (this[keySiblings] as Record<string, unknown>[]).findIndex(
31-
(sibling) => this[keyId] === sibling[keyId],
32-
);
33-
},
34-
},
35-
[keyNext]: {
36-
get(this: Record<string, unknown>) {
37-
return (this[keySiblings] as Record<string, unknown>[])[
38-
(this[keyIndex] as number) + 1
39-
];
40-
},
41-
},
42-
[keyPrev]: {
43-
get(this: Record<string, unknown>) {
44-
return (this[keySiblings] as Record<string, unknown>[])[
45-
(this[keyIndex] as number) - 1
46-
];
47-
},
48-
},
49-
},
50-
value = isReactive(tree) ? tree : reactive(tree);
51-
52-
const getLeaves = (
18+
getLeaves = (
5319
siblings: { configurable?: boolean; value: Record<string, unknown>[] },
5420
parent = {},
5521
) =>
5622
siblings.value.flatMap((value): Record<string, unknown>[] => {
5723
Object.defineProperties(value, {
58-
...properties,
24+
[keyBranch]: {
25+
get: () => {
26+
const ret = [value];
27+
while (ret[0]?.[keyParent])
28+
ret.unshift(ret[0][keyParent] as Record<string, unknown>);
29+
return ret;
30+
},
31+
},
32+
[keyIndex]: {
33+
get: () =>
34+
(value[keySiblings] as Record<string, unknown>[]).findIndex(
35+
(sibling) => value[keyId] === sibling[keyId],
36+
),
37+
},
38+
[keyNext]: {
39+
get: () =>
40+
(value[keySiblings] as Record<string, unknown>[])[
41+
(value[keyIndex] as number) + 1
42+
],
43+
},
5944
[keyParent]: parent,
45+
[keyPrev]: {
46+
get: () =>
47+
(value[keySiblings] as Record<string, unknown>[])[
48+
(value[keyIndex] as number) - 1
49+
],
50+
},
6051
[keySiblings]: siblings,
6152
});
6253
return [
@@ -70,18 +61,18 @@ export default (
7061
),
7162
];
7263
}),
73-
leaves = computed(() => getLeaves({ value }));
74-
75-
const arrLeaves = toReactive(leaves),
64+
leaves = computed(() =>
65+
getLeaves({ value: isReactive(tree) ? tree : reactive(tree) }),
66+
),
7667
objLeaves = toReactive(
7768
computed(() =>
7869
Object.fromEntries(
7970
leaves.value.map((leaf) => [leaf[keyId] as string, leaf]),
8071
),
8172
),
8273
);
83-
84-
const add = (pId: string) => {
74+
return {
75+
add: (pId: string) => {
8576
const the = objLeaves[pId];
8677
if (the) {
8778
const children = the[keyChildren] as
@@ -105,7 +96,8 @@ export default (
10596
}
10697
return undefined;
10798
},
108-
down = (pId: string) => {
99+
arrLeaves: toReactive(leaves),
100+
down: (pId: string) => {
109101
const the = objLeaves[pId];
110102
if (the) {
111103
const index = the[keyIndex] as number,
@@ -122,7 +114,8 @@ export default (
122114
];
123115
}
124116
},
125-
left = (pId: string) => {
117+
leaves,
118+
left: (pId: string) => {
126119
const the = objLeaves[pId];
127120
if (the) {
128121
const parent = the[keyParent] as Record<string, unknown> | undefined;
@@ -142,7 +135,8 @@ export default (
142135
}
143136
return undefined;
144137
},
145-
remove = (pId: string) => {
138+
objLeaves,
139+
remove: (pId: string) => {
146140
const the = objLeaves[pId];
147141
if (the) {
148142
const parent = the[keyParent] as Record<string, unknown> | undefined;
@@ -161,7 +155,7 @@ export default (
161155
}
162156
return undefined;
163157
},
164-
right = (pId: string) => {
158+
right: (pId: string) => {
165159
const the = objLeaves[pId];
166160
if (the) {
167161
const prev = the[keyPrev] as Record<string, unknown> | undefined;
@@ -181,7 +175,7 @@ export default (
181175
}
182176
return undefined;
183177
},
184-
up = (pId: string) => {
178+
up: (pId: string) => {
185179
const the = objLeaves[pId];
186180
if (the) {
187181
const index = the[keyIndex] as number,
@@ -193,7 +187,6 @@ export default (
193187
siblings[prevIndex],
194188
];
195189
}
196-
};
197-
198-
return { add, arrLeaves, down, leaves, left, objLeaves, remove, right, up };
190+
},
191+
};
199192
};

0 commit comments

Comments
 (0)