Skip to content

Commit a96fa5c

Browse files
committed
Refactor tree operations and add addChild method
1 parent 9499ca8 commit a96fa5c

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

src/index.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@ import { computed, isReactive, reactive } from "vue";
66

77
export type unObject = Record<string, unknown>;
88

9+
/* -------------------------------------------------------------------------- */
10+
/* Значения по-умолчанию */
11+
/* -------------------------------------------------------------------------- */
12+
13+
const configurable = true;
14+
15+
/* -------------------------------------------------------------------------- */
16+
/* Служебные функции */
17+
/* -------------------------------------------------------------------------- */
18+
19+
const getItems = (nodes: unObject[], node?: unObject) =>
20+
nodes.toReversed().map((child) => ({
21+
node: child,
22+
parent: { configurable, value: node },
23+
siblings: { configurable, value: nodes },
24+
})),
25+
uid = () => {
26+
const url = URL.createObjectURL(new Blob()),
27+
uid = url.split("/").pop() ?? crypto.randomUUID();
28+
URL.revokeObjectURL(url);
29+
return uid;
30+
};
31+
932
/* -------------------------------------------------------------------------- */
1033
/* Композабл для работы с древовидным объектом */
1134
/* -------------------------------------------------------------------------- */
@@ -62,15 +85,9 @@ export default (
6285
/* Формирование массива элементов дерева простого и ассоциативного */
6386
/* -------------------------------------------------------------------------- */
6487

65-
const getItems = (nodes: unObject[], node?: unObject) =>
66-
nodes.toReversed().map((child) => ({
67-
node: child,
68-
parent: { configurable: true, value: node },
69-
siblings: { configurable: true, value: nodes },
70-
})),
71-
getNodes = function* (nodes: unObject[]) {
88+
const getNodes = function* (nodes: unObject[]) {
7289
const stack = getItems(nodes);
73-
while (stack.length > 0) {
90+
while (stack.length) {
7491
const { node, parent, siblings } = stack.pop() ?? {};
7592
if (node && parent && siblings) {
7693
Object.defineProperties(node, {
@@ -102,7 +119,6 @@ export default (
102119
const the = nodesMap.value[pId];
103120
if (the) {
104121
const [root] = nodes.value,
105-
children = the[keyChildren] as undefined | unObject[],
106122
index = the[keyIndex] as number,
107123
next = the[keyNext] as undefined | unObject,
108124
nextIndex = index + 1,
@@ -112,12 +128,14 @@ export default (
112128
siblings = the[keySiblings] as unObject[];
113129
switch (action) {
114130
case "add": {
115-
const url = URL.createObjectURL(new Blob()),
116-
id = url.split("/").pop();
117-
URL.revokeObjectURL(url);
118-
if (parent && Array.isArray(children))
119-
children.unshift({ [keyId]: id });
120-
else siblings.splice(index + 1, 0, { [keyId]: id });
131+
const id = uid();
132+
siblings.splice(nextIndex, 0, { [keyId]: id });
133+
return id;
134+
}
135+
case "addChild": {
136+
const id = uid();
137+
if (!Array.isArray(the[keyChildren])) the[keyChildren] = [];
138+
(the[keyChildren] as unObject[]).unshift({ [keyId]: id });
121139
return id;
122140
}
123141
case "down":
@@ -141,16 +159,14 @@ export default (
141159
return parent[keyId] as string;
142160
}
143161
break;
144-
case "remove":
145-
if (parent) {
146-
const id = (next?.[keyId] ??
147-
prev?.[keyId] ??
148-
parent[keyId] ??
149-
root?.[keyId]) as string | undefined;
150-
siblings.splice(index, 1);
151-
return id;
152-
}
153-
break;
162+
case "remove": {
163+
const id = (next?.[keyId] ??
164+
prev?.[keyId] ??
165+
parent?.[keyId] ??
166+
root?.[keyId]) as string | undefined;
167+
siblings.splice(index, 1);
168+
return id;
169+
}
154170
case "right":
155171
if (prev) {
156172
const children = (prev[keyChildren] ?? []) as unObject[],
@@ -176,6 +192,7 @@ export default (
176192

177193
return {
178194
add: (pId: string) => run(pId, "add"),
195+
addChild: (pId: string) => run(pId, "addChild"),
179196
down: (pId: string) => run(pId, "down"),
180197
left: (pId: string) => run(pId, "left"),
181198
nodes,

0 commit comments

Comments
 (0)