Skip to content

Commit 29f9c83

Browse files
author
Jerry Bruwes
committed
modified: index.ts
1 parent 38c6a42 commit 29f9c83

File tree

1 file changed

+49
-56
lines changed

1 file changed

+49
-56
lines changed

index.ts

Lines changed: 49 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,18 @@ export default (
7777

7878
const leaves = computed(() => getLeaves({ value }));
7979

80+
const atlas = computed(
81+
() => new Map(leaves.value.map((leaf) => [leaf[keyId], leaf])),
82+
);
83+
8084
const add = (pId: string) => {
81-
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
85+
const the = atlas.value.get(pId);
8286
if (the) {
8387
const children = the[keyChildren] as
84-
| Record<string, unknown>[]
85-
| undefined;
86-
const index = the[keyIndex] as number;
87-
const siblings = the[keySiblings] as Record<string, unknown>[];
88+
| Record<string, unknown>[]
89+
| undefined,
90+
index = the[keyIndex] as number,
91+
siblings = the[keySiblings] as Record<string, unknown>[];
8892
const id = v4();
8993
switch (true) {
9094
case !!the[keyParent]:
@@ -102,11 +106,11 @@ export default (
102106
return undefined;
103107
},
104108
down = (pId: string) => {
105-
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
109+
const the = atlas.value.get(pId);
106110
if (the) {
107-
const index = the[keyIndex] as number;
108-
const nextIndex = index + 1;
109-
const siblings = the[keySiblings] as Record<string, unknown>[];
111+
const index = the[keyIndex] as number,
112+
nextIndex = index + 1,
113+
siblings = the[keySiblings] as Record<string, unknown>[];
110114
if (
111115
index < siblings.length - 1 &&
112116
siblings[index] &&
@@ -119,81 +123,70 @@ export default (
119123
}
120124
},
121125
left = (pId: string) => {
122-
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
126+
const the = atlas.value.get(pId);
123127
if (the) {
124128
const parent = the[keyParent] as Record<string, unknown> | undefined;
125-
if (parent) {
126-
const siblings = parent[keySiblings] as Record<string, unknown>[];
127-
if (parent[keyParent]) {
128-
siblings.splice(
129-
(parent[keyIndex] as number) + 1,
130-
0,
131-
...(
132-
(parent[keyChildren] ?? []) as Record<string, unknown>[]
133-
).splice(the[keyIndex] as number, 1),
134-
);
135-
return parent[keyId] as string;
136-
}
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;
137141
}
138142
}
139143
return undefined;
140144
},
141145
remove = (pId: string) => {
142-
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
146+
const the = atlas.value.get(pId);
143147
if (the) {
144-
const next = the[keyNext];
145-
const parent = the[keyParent];
146-
const prev = the[keyPrev];
148+
const parent = the[keyParent] as Record<string, unknown> | undefined;
147149
if (parent) {
148-
let id: string;
149-
switch (true) {
150-
case !!next:
151-
({ id } = next as { id: string });
152-
break;
153-
case !!prev:
154-
({ id } = prev as { id: string });
155-
break;
156-
default:
157-
({ id } = parent as { id: string });
158-
}
159-
(the[keySiblings] as Record<string, unknown>[]).splice(
160-
the[keyIndex] as number,
161-
1,
162-
);
163-
if (!id) [{ id }] = leaves.value as [{ id: string }];
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);
164159
return id;
165160
}
166161
}
167162
return undefined;
168163
},
169164
right = (pId: string) => {
170-
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
165+
const the = atlas.value.get(pId);
171166
if (the) {
172167
const prev = the[keyPrev] as Record<string, unknown> | undefined;
173168
if (prev) {
174169
const children = (prev[keyChildren] ?? []) as Record<
175-
string,
176-
unknown
177-
>[];
178-
const id = prev[keyId] as string;
170+
string,
171+
unknown
172+
>[],
173+
id = prev[keyId] as string,
174+
siblings = the[keySiblings] as Record<string, unknown>[];
179175
prev[keyChildren] = [
180176
...children,
181-
...(the[keySiblings] as Record<string, unknown>[]).splice(
182-
the[keyIndex] as number,
183-
1,
184-
),
177+
...siblings.splice(the[keyIndex] as number, 1),
185178
];
186179
return id;
187180
}
188181
}
189182
return undefined;
190183
},
191184
up = (pId: string) => {
192-
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
185+
const the = atlas.value.get(pId);
193186
if (the) {
194-
const index = the[keyIndex] as number;
195-
const prevIndex = index - 1;
196-
const siblings = the[keySiblings] as Record<string, unknown>[];
187+
const index = the[keyIndex] as number,
188+
prevIndex = index - 1,
189+
siblings = the[keySiblings] as Record<string, unknown>[];
197190
if (index && siblings[index] && siblings[prevIndex])
198191
[siblings[prevIndex], siblings[index]] = [
199192
siblings[index],
@@ -204,5 +197,5 @@ export default (
204197

205198
/* -------------------------------------------------------------------------- */
206199

207-
return { add, down, leaves, left, remove, right, up };
200+
return { add, atlas, down, leaves, left, remove, right, up };
208201
};

0 commit comments

Comments
 (0)