-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonTreeCore.js
More file actions
173 lines (138 loc) · 3.9 KB
/
jsonTreeCore.js
File metadata and controls
173 lines (138 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
function inferType(value) {
if (Array.isArray(value)) return "array";
if (value === null) return "null";
return typeof value;
}
function createIdGenerator(start = 1) {
let id = start;
return function nextId() {
id += 1;
return String(id);
};
}
function buildNode(value, key = null, parent = null, nextId = createIdGenerator()) {
const type = inferType(value);
const node = {
id: nextId(),
key,
type,
parent,
collapsed: false,
children: [],
value: null
};
if (type === "object") {
node.children = Object.entries(value).map(([k, v]) => buildNode(v, k, node, nextId));
} else if (type === "array") {
node.children = value.map((item, index) => buildNode(item, String(index), node, nextId));
} else {
node.value = value;
}
return node;
}
function nodeToPlain(node) {
if (node.type === "object") {
const out = {};
for (const child of node.children) {
out[child.key] = nodeToPlain(child);
}
return out;
}
if (node.type === "array") {
return node.children.map((child) => nodeToPlain(child));
}
return node.value;
}
function parseJsonToTree(raw) {
if (typeof raw !== "string") {
throw new Error("输入必须是字符串");
}
const text = raw.trim();
if (!text) {
throw new Error("输入不能为空");
}
const parsed = JSON.parse(text);
const nextId = createIdGenerator(1);
return buildNode(parsed, "root", null, nextId);
}
function treeToJsonString(root, spaces = 2) {
return JSON.stringify(nodeToPlain(root), null, spaces);
}
function findNodeById(node, id) {
if (!node) return null;
if (node.id === id) return node;
for (const child of node.children) {
const found = findNodeById(child, id);
if (found) return found;
}
return null;
}
function isDescendant(ancestor, maybeChild) {
if (!ancestor || !maybeChild) return false;
for (const child of ancestor.children) {
if (child.id === maybeChild.id || isDescendant(child, maybeChild)) return true;
}
return false;
}
function detachNode(node) {
const parent = node.parent;
if (!parent) return;
const idx = parent.children.findIndex((item) => item.id === node.id);
if (idx >= 0) {
parent.children.splice(idx, 1);
}
}
function ensureUniqueObjectKey(targetParent, desiredKey) {
let key = desiredKey || "newKey";
const used = new Set(targetParent.children.map((child) => child.key));
if (!used.has(key)) return key;
let i = 1;
while (used.has(`${key}_${i}`)) {
i += 1;
}
return `${key}_${i}`;
}
function normalizeArrayKeys(parentNode) {
if (!parentNode || parentNode.type !== "array") return;
parentNode.children.forEach((child, index) => {
child.key = String(index);
});
}
function moveNode(rootNode, sourceId, targetId) {
const source = findNodeById(rootNode, sourceId);
const target = findNodeById(rootNode, targetId);
if (!source || !target || source.id === rootNode.id) return false;
if (source.id === target.id) return false;
if (isDescendant(source, target)) return false;
const sourceOldParent = source.parent;
detachNode(source);
if (target.type === "object" || target.type === "array") {
source.parent = target;
if (target.type === "object") {
source.key = ensureUniqueObjectKey(target, source.key || "movedKey");
}
target.children.push(source);
} else {
const parent = target.parent;
if (!parent) return false;
source.parent = parent;
const targetIndex = parent.children.findIndex((item) => item.id === target.id);
if (parent.type === "object") {
source.key = ensureUniqueObjectKey(parent, source.key || target.key || "movedKey");
}
parent.children.splice(targetIndex + 1, 0, source);
}
normalizeArrayKeys(sourceOldParent);
normalizeArrayKeys(target.parent);
normalizeArrayKeys(target);
return true;
}
export {
inferType,
buildNode,
nodeToPlain,
parseJsonToTree,
treeToJsonString,
findNodeById,
moveNode
};