Skip to content

Commit 16febe9

Browse files
committed
Refactor tree object handling and update dependencies
1 parent dcb1110 commit 16febe9

File tree

4 files changed

+62
-62
lines changed

4 files changed

+62
-62
lines changed

eslint.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { defineConfig } from "eslint/config";
21
import shared from "@vuebro/configs/eslint";
2+
import { defineConfig } from "eslint/config";
33

44
export default defineConfig(shared, {
55
languageOptions: {

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@
3737
"prettier": "@vuebro/configs/prettierrc",
3838
"devDependencies": {
3939
"@types/node": "^24.9.1",
40-
"@vuebro/configs": "^1.1.49"
40+
"@vuebro/configs": "^1.1.51"
4141
}
4242
}

src/index.ts

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { isReactive, computed, reactive } from "vue";
21
import uid from "uuid-random";
2+
import { computed, isReactive, reactive } from "vue";
33

44
/* -------------------------------------------------------------------------- */
55
/* Тип универсального объекта */
@@ -18,7 +18,7 @@ const configurable = true;
1818
/* -------------------------------------------------------------------------- */
1919

2020
const getItems = (siblings: unObject[], parent?: unObject) =>
21-
[...siblings].reverse().map((node) => ({ siblings, parent, node }));
21+
[...siblings].reverse().map((node) => ({ node, parent, siblings }));
2222

2323
/* -------------------------------------------------------------------------- */
2424
/* Композабл для работы с древовидным объектом */
@@ -27,14 +27,14 @@ const getItems = (siblings: unObject[], parent?: unObject) =>
2727
export default (
2828
tree: unObject[],
2929
{
30-
children: keyChildren = "children",
31-
siblings: keySiblings = "siblings",
3230
branch: keyBranch = "branch",
33-
parent: keyParent = "parent",
31+
children: keyChildren = "children",
32+
id: keyId = "id",
3433
index: keyIndex = "index",
3534
next: keyNext = "next",
35+
parent: keyParent = "parent",
3636
prev: keyPrev = "prev",
37-
id: keyId = "id",
37+
siblings: keySiblings = "siblings",
3838
} = {},
3939
) => {
4040
/* -------------------------------------------------------------------------- */
@@ -57,18 +57,18 @@ export default (
5757
return ret;
5858
},
5959
},
60-
[keyPrev]: {
60+
[keyIndex]: {
6161
/**
62-
* A computed property that returns the previous sibling of the current
63-
* object.
62+
* A computed property that finds the index of the current object in its
63+
* siblings array.
6464
*
65-
* @returns {unObject | undefined} The previous sibling object or
66-
* undefined if there is no previous sibling.
65+
* @returns {number} The index of the current object in its siblings
66+
* array.
6767
*/
68-
get(this: unObject): undefined | unObject {
69-
return (this[keySiblings] as unObject[])[
70-
(this[keyIndex] as number) - 1
71-
];
68+
get(this: unObject): number {
69+
return (this[keySiblings] as unObject[]).findIndex(
70+
(sibling) => this[keyId] === sibling[keyId],
71+
);
7272
},
7373
},
7474
[keyNext]: {
@@ -85,18 +85,18 @@ export default (
8585
];
8686
},
8787
},
88-
[keyIndex]: {
88+
[keyPrev]: {
8989
/**
90-
* A computed property that finds the index of the current object in its
91-
* siblings array.
90+
* A computed property that returns the previous sibling of the current
91+
* object.
9292
*
93-
* @returns {number} The index of the current object in its siblings
94-
* array.
93+
* @returns {unObject | undefined} The previous sibling object or
94+
* undefined if there is no previous sibling.
9595
*/
96-
get(this: unObject): number {
97-
return (this[keySiblings] as unObject[]).findIndex(
98-
(sibling) => this[keyId] === sibling[keyId],
99-
);
96+
get(this: unObject): undefined | unObject {
97+
return (this[keySiblings] as unObject[])[
98+
(this[keyIndex] as number) - 1
99+
];
100100
},
101101
},
102102
};
@@ -116,17 +116,17 @@ export default (
116116
const getNodes = function* (nodes: unObject[]) {
117117
const stack = getItems(nodes);
118118
while (stack.length) {
119-
const { siblings, parent, node } = stack.pop() ?? {};
119+
const { node, parent, siblings } = stack.pop() ?? {};
120120
if (node) {
121121
if (node[keyParent] !== parent)
122122
Object.defineProperty(node, keyParent, {
123-
value: parent,
124123
configurable,
124+
value: parent,
125125
});
126126
if (node[keySiblings] !== siblings)
127127
Object.defineProperty(node, keySiblings, {
128-
value: siblings,
129128
configurable,
129+
value: siblings,
130130
});
131131
if (Object.keys(properties).some((key) => !(key in node)))
132132
Object.defineProperties(node, properties);
@@ -151,37 +151,26 @@ export default (
151151
const run = (pId: string, action: string) => {
152152
const the = nodesMap.value[pId];
153153
if (the) {
154-
const parent = the[keyParent] as undefined | unObject,
155-
next = the[keyNext] as undefined | unObject,
156-
prev = the[keyPrev] as undefined | unObject,
157-
siblings = the[keySiblings] as unObject[],
154+
const [root] = nodes.value,
158155
index = the[keyIndex] as number,
156+
next = the[keyNext] as undefined | unObject,
159157
nextIndex = index + 1,
158+
parent = the[keyParent] as undefined | unObject,
159+
prev = the[keyPrev] as undefined | unObject,
160160
prevIndex = index - 1,
161-
[root] = nodes.value;
161+
siblings = the[keySiblings] as unObject[];
162162
switch (action) {
163+
case "add": {
164+
const id = uid();
165+
siblings.splice(nextIndex, 0, { [keyId]: id });
166+
return id;
167+
}
163168
case "addChild": {
164169
const id = uid();
165170
if (!Array.isArray(the[keyChildren])) the[keyChildren] = [];
166171
(the[keyChildren] as unObject[]).unshift({ [keyId]: id });
167172
return id;
168173
}
169-
case "remove": {
170-
const id = (next?.[keyId] ??
171-
prev?.[keyId] ??
172-
parent?.[keyId] ??
173-
root?.[keyId]) as undefined | string;
174-
siblings.splice(index, 1);
175-
return id;
176-
}
177-
case "right":
178-
if (prev) {
179-
const children = (prev[keyChildren] ?? []) as unObject[],
180-
id = prev[keyId] as string;
181-
prev[keyChildren] = [...children, ...siblings.splice(index, 1)];
182-
return id;
183-
}
184-
break;
185174
case "down":
186175
if (
187176
index < siblings.length - 1 &&
@@ -203,11 +192,22 @@ export default (
203192
return parent[keyId] as string;
204193
}
205194
break;
206-
case "add": {
207-
const id = uid();
208-
siblings.splice(nextIndex, 0, { [keyId]: id });
195+
case "remove": {
196+
const id = (next?.[keyId] ??
197+
prev?.[keyId] ??
198+
parent?.[keyId] ??
199+
root?.[keyId]) as string | undefined;
200+
siblings.splice(index, 1);
209201
return id;
210202
}
203+
case "right":
204+
if (prev) {
205+
const children = (prev[keyChildren] ?? []) as unObject[],
206+
id = prev[keyId] as string;
207+
prev[keyChildren] = [...children, ...siblings.splice(index, 1)];
208+
return id;
209+
}
210+
break;
211211
case "up":
212212
if (index && siblings[index] && siblings[prevIndex])
213213
[siblings[prevIndex], siblings[index]] = [
@@ -225,14 +225,14 @@ export default (
225225
/* -------------------------------------------------------------------------- */
226226

227227
return {
228+
add: (pId: string) => run(pId, "add"),
228229
addChild: (pId: string) => run(pId, "addChild"),
229-
remove: (pId: string) => run(pId, "remove"),
230-
right: (pId: string) => run(pId, "right"),
231230
down: (pId: string) => run(pId, "down"),
232231
left: (pId: string) => run(pId, "left"),
233-
add: (pId: string) => run(pId, "add"),
234-
up: (pId: string) => run(pId, "up"),
235-
nodesMap,
236232
nodes,
233+
nodesMap,
234+
remove: (pId: string) => run(pId, "remove"),
235+
right: (pId: string) => run(pId, "right"),
236+
up: (pId: string) => run(pId, "up"),
237237
};
238238
};

0 commit comments

Comments
 (0)