@stacksjs/components 0.2.855 → 0.2.857
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ContextMenu-43g3w5tp.stx +188 -0
- package/dist/DataGrid-7rt7v865.stx +376 -0
- package/dist/GraphView-0t3t9mec.stx +320 -0
- package/dist/JsonTree-satc4010.stx +317 -0
- package/dist/PreviewFrame-erbgvzj8.stx +257 -0
- package/dist/SplitPane-7sqfdwqa.stx +159 -0
- package/dist/component-api/component-api.js +36 -0
- package/dist/context-menu/context-menu.js +575 -0
- package/dist/data-grid/data-grid.js +1244 -0
- package/dist/graph-view/graph-view.js +1408 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +22 -20
- package/dist/json-tree/json-tree.js +1121 -0
- package/dist/preview-frame/preview-frame.js +356 -0
- package/dist/split-pane/split-pane.js +364 -0
- package/dist/ui/context-menu/ContextMenu.d.ts +14 -0
- package/dist/ui/context-menu/context-menu.d.ts +8 -0
- package/dist/ui/context-menu/core.d.ts +74 -0
- package/dist/ui/context-menu/index.d.ts +11 -0
- package/dist/ui/context-menu/view.d.ts +36 -0
- package/dist/ui/data-grid/DataGrid.d.ts +30 -0
- package/dist/ui/data-grid/core.d.ts +141 -0
- package/dist/ui/data-grid/data-grid.d.ts +9 -0
- package/dist/ui/data-grid/index.d.ts +20 -0
- package/dist/ui/data-grid/view.d.ts +56 -0
- package/dist/ui/graph-view/GraphView.d.ts +21 -0
- package/dist/ui/graph-view/core/edges.d.ts +34 -0
- package/dist/ui/graph-view/core/force.d.ts +10 -0
- package/dist/ui/graph-view/core/graph.d.ts +83 -0
- package/dist/ui/graph-view/core/index.d.ts +5 -0
- package/dist/ui/graph-view/core/layered.d.ts +24 -0
- package/dist/ui/graph-view/core/viewport.d.ts +34 -0
- package/dist/ui/graph-view/graph-view.d.ts +9 -0
- package/dist/ui/graph-view/index.d.ts +21 -0
- package/dist/ui/graph-view/view.d.ts +51 -0
- package/dist/ui/json-tree/JsonTree.d.ts +18 -0
- package/dist/ui/json-tree/core.d.ts +108 -0
- package/dist/ui/json-tree/index.d.ts +18 -0
- package/dist/ui/json-tree/json-tree.d.ts +10 -0
- package/dist/ui/json-tree/tinker.d.ts +84 -0
- package/dist/ui/json-tree/view.d.ts +59 -0
- package/dist/ui/preview-frame/PreviewFrame.d.ts +44 -0
- package/dist/ui/preview-frame/core.d.ts +81 -0
- package/dist/ui/preview-frame/index.d.ts +19 -0
- package/dist/ui/preview-frame/preview-frame.d.ts +8 -0
- package/dist/ui/preview-frame/view.d.ts +37 -0
- package/dist/ui/split-pane/SplitPane.d.ts +18 -0
- package/dist/ui/split-pane/core.d.ts +58 -0
- package/dist/ui/split-pane/index.d.ts +15 -0
- package/dist/ui/split-pane/split-pane.d.ts +8 -0
- package/dist/ui/split-pane/view.d.ts +27 -0
- package/dist/ui/virtual-table/index.d.ts +6 -0
- package/dist/utils/component-api.d.ts +4 -0
- package/package.json +38 -3
- package/src/index.ts +8 -0
- package/src/ui/code-editor/README.md +2 -1
- package/src/ui/context-menu/ContextMenu.stx +188 -0
- package/src/ui/context-menu/README.md +117 -0
- package/src/ui/context-menu/context-menu.ts +8 -0
- package/src/ui/context-menu/core.ts +250 -0
- package/src/ui/context-menu/index.ts +16 -0
- package/src/ui/context-menu/view.ts +540 -0
- package/src/ui/data-grid/DataGrid.stx +376 -0
- package/src/ui/data-grid/README.md +204 -0
- package/src/ui/data-grid/core.ts +813 -0
- package/src/ui/data-grid/data-grid.ts +9 -0
- package/src/ui/data-grid/index.ts +34 -0
- package/src/ui/data-grid/view.ts +806 -0
- package/src/ui/graph-view/GraphView.stx +320 -0
- package/src/ui/graph-view/README.md +210 -0
- package/src/ui/graph-view/core/edges.ts +180 -0
- package/src/ui/graph-view/core/force.ts +237 -0
- package/src/ui/graph-view/core/graph.ts +277 -0
- package/src/ui/graph-view/core/index.ts +5 -0
- package/src/ui/graph-view/core/layered.ts +256 -0
- package/src/ui/graph-view/core/viewport.ts +141 -0
- package/src/ui/graph-view/graph-view.ts +9 -0
- package/src/ui/graph-view/index.ts +35 -0
- package/src/ui/graph-view/view.ts +809 -0
- package/src/ui/json-tree/JsonTree.stx +317 -0
- package/src/ui/json-tree/README.md +193 -0
- package/src/ui/json-tree/core.ts +588 -0
- package/src/ui/json-tree/index.ts +28 -0
- package/src/ui/json-tree/json-tree.ts +10 -0
- package/src/ui/json-tree/tinker.ts +349 -0
- package/src/ui/json-tree/view.ts +665 -0
- package/src/ui/preview-frame/PreviewFrame.stx +257 -0
- package/src/ui/preview-frame/README.md +107 -0
- package/src/ui/preview-frame/core.ts +195 -0
- package/src/ui/preview-frame/index.ts +31 -0
- package/src/ui/preview-frame/preview-frame.ts +8 -0
- package/src/ui/preview-frame/view.ts +336 -0
- package/src/ui/split-pane/README.md +87 -0
- package/src/ui/split-pane/SplitPane.stx +159 -0
- package/src/ui/split-pane/core.ts +200 -0
- package/src/ui/split-pane/index.ts +24 -0
- package/src/ui/split-pane/split-pane.ts +8 -0
- package/src/ui/split-pane/view.ts +305 -0
- package/src/ui/virtual-table/index.ts +6 -0
- package/src/utils/component-api.ts +72 -0
|
@@ -0,0 +1,1121 @@
|
|
|
1
|
+
// src/ui/json-tree/core.ts
|
|
2
|
+
var INDEXED = new Set(["array", "set"]);
|
|
3
|
+
var IDENTIFIER = /^[A-Z_$][\w$]*$/i;
|
|
4
|
+
function isIndexed(node) {
|
|
5
|
+
return INDEXED.has(node.kind);
|
|
6
|
+
}
|
|
7
|
+
function isExpandable(node) {
|
|
8
|
+
return node.ref === undefined && (node.children !== undefined && node.children.length > 0 || (node.more ?? 0) > 0);
|
|
9
|
+
}
|
|
10
|
+
function formatPath(path, root = "$") {
|
|
11
|
+
let text = root;
|
|
12
|
+
for (const segment of path) {
|
|
13
|
+
if (typeof segment === "number")
|
|
14
|
+
text += `[${segment}]`;
|
|
15
|
+
else if (IDENTIFIER.test(segment))
|
|
16
|
+
text += `.${segment}`;
|
|
17
|
+
else
|
|
18
|
+
text += `[${JSON.stringify(segment)}]`;
|
|
19
|
+
}
|
|
20
|
+
return text;
|
|
21
|
+
}
|
|
22
|
+
function nodeToPlain(node) {
|
|
23
|
+
if (node.ref !== undefined)
|
|
24
|
+
return "[Circular]";
|
|
25
|
+
if (node.children) {
|
|
26
|
+
if (isIndexed(node))
|
|
27
|
+
return node.children.map((child) => nodeToPlain(child));
|
|
28
|
+
const object = {};
|
|
29
|
+
node.children.forEach((child, index) => {
|
|
30
|
+
object[child.key !== undefined ? String(child.key) : child.label ?? String(index)] = nodeToPlain(child);
|
|
31
|
+
});
|
|
32
|
+
return object;
|
|
33
|
+
}
|
|
34
|
+
if ("value" in node) {
|
|
35
|
+
const value = node.value;
|
|
36
|
+
if (typeof value === "bigint")
|
|
37
|
+
return value.toString();
|
|
38
|
+
if (typeof value === "number" && !Number.isFinite(value))
|
|
39
|
+
return String(value);
|
|
40
|
+
if (typeof value === "function" || typeof value === "symbol")
|
|
41
|
+
return node.preview ?? String(value);
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
return node.preview ?? null;
|
|
45
|
+
}
|
|
46
|
+
function nodeToJson(node, indent = 2) {
|
|
47
|
+
const plain = nodeToPlain(node);
|
|
48
|
+
return plain === undefined ? "undefined" : JSON.stringify(plain, null, indent);
|
|
49
|
+
}
|
|
50
|
+
var PREVIEW_LIMIT = 80;
|
|
51
|
+
function previewOf(node) {
|
|
52
|
+
if (node.preview !== undefined)
|
|
53
|
+
return node.preview;
|
|
54
|
+
if (node.ref !== undefined)
|
|
55
|
+
return "[Circular]";
|
|
56
|
+
if (node.children || node.more) {
|
|
57
|
+
const count = (node.children?.length ?? 0) + (node.more ?? 0);
|
|
58
|
+
if (isIndexed(node))
|
|
59
|
+
return `${node.kind === "set" ? "Set" : "Array"}(${count})`;
|
|
60
|
+
return count === 0 ? "{}" : `{${count}}`;
|
|
61
|
+
}
|
|
62
|
+
if ("value" in node) {
|
|
63
|
+
const value = node.value;
|
|
64
|
+
if (typeof value === "string")
|
|
65
|
+
return JSON.stringify(value.length > PREVIEW_LIMIT ? `${value.slice(0, PREVIEW_LIMIT)}…` : value);
|
|
66
|
+
if (typeof value === "bigint")
|
|
67
|
+
return `${value}n`;
|
|
68
|
+
return String(value);
|
|
69
|
+
}
|
|
70
|
+
return node.kind;
|
|
71
|
+
}
|
|
72
|
+
function fromValue(value, label, options = {}) {
|
|
73
|
+
const maxItems = options.maxItems ?? 1000;
|
|
74
|
+
const maxDepth = options.maxDepth ?? 32;
|
|
75
|
+
const seen = new Map;
|
|
76
|
+
let nextId = 1;
|
|
77
|
+
const named = (node, name) => {
|
|
78
|
+
if (name !== undefined)
|
|
79
|
+
node.label = name;
|
|
80
|
+
return node;
|
|
81
|
+
};
|
|
82
|
+
const walk = (current, name, depth) => {
|
|
83
|
+
if (current === null)
|
|
84
|
+
return named({ kind: "null", value: null, preview: "null" }, name);
|
|
85
|
+
switch (typeof current) {
|
|
86
|
+
case "undefined":
|
|
87
|
+
return named({ kind: "undefined", value: undefined, preview: "undefined" }, name);
|
|
88
|
+
case "string":
|
|
89
|
+
case "number":
|
|
90
|
+
case "boolean":
|
|
91
|
+
case "bigint":
|
|
92
|
+
return named({ kind: typeof current, value: current }, name);
|
|
93
|
+
case "symbol":
|
|
94
|
+
return named({ kind: "symbol", preview: current.toString() }, name);
|
|
95
|
+
case "function":
|
|
96
|
+
return named({ kind: "function", preview: `ƒ ${current.name || "(anonymous)"}()` }, name);
|
|
97
|
+
}
|
|
98
|
+
const object = current;
|
|
99
|
+
const known = seen.get(object);
|
|
100
|
+
if (known !== undefined)
|
|
101
|
+
return named({ kind: "ref", ref: known }, name);
|
|
102
|
+
if (object instanceof Date)
|
|
103
|
+
return named({ kind: "date", value: Number.isNaN(object.getTime()) ? null : object.toISOString(), preview: Number.isNaN(object.getTime()) ? "Invalid Date" : object.toISOString() }, name);
|
|
104
|
+
if (object instanceof RegExp)
|
|
105
|
+
return named({ kind: "regexp", value: object.toString(), preview: object.toString() }, name);
|
|
106
|
+
const id = `v${nextId++}`;
|
|
107
|
+
seen.set(object, id);
|
|
108
|
+
const array = Array.isArray(object);
|
|
109
|
+
const set = object instanceof Set;
|
|
110
|
+
const map = object instanceof Map;
|
|
111
|
+
const node = named({ id, kind: array ? "array" : set ? "set" : map ? "map" : "object", children: [] }, name);
|
|
112
|
+
if (depth >= maxDepth) {
|
|
113
|
+
node.preview = array ? "Array" : "Object";
|
|
114
|
+
delete node.children;
|
|
115
|
+
return node;
|
|
116
|
+
}
|
|
117
|
+
const children = node.children;
|
|
118
|
+
let total = 0;
|
|
119
|
+
if (array || set) {
|
|
120
|
+
for (const item of object) {
|
|
121
|
+
if (total < maxItems)
|
|
122
|
+
children.push(walk(item, String(total), depth + 1));
|
|
123
|
+
total++;
|
|
124
|
+
}
|
|
125
|
+
} else if (map) {
|
|
126
|
+
for (const [key, inner] of object) {
|
|
127
|
+
if (total < maxItems)
|
|
128
|
+
children.push(walk(inner, typeof key === "string" ? key : previewOf(walk(key, undefined, maxDepth)), depth + 1));
|
|
129
|
+
total++;
|
|
130
|
+
}
|
|
131
|
+
} else {
|
|
132
|
+
for (const key of Object.keys(object)) {
|
|
133
|
+
if (total < maxItems)
|
|
134
|
+
children.push(walk(object[key], key, depth + 1));
|
|
135
|
+
total++;
|
|
136
|
+
}
|
|
137
|
+
const ctor = object.constructor?.name;
|
|
138
|
+
if (ctor && ctor !== "Object")
|
|
139
|
+
node.preview = `${ctor} {${total}}`;
|
|
140
|
+
}
|
|
141
|
+
if (total > children.length)
|
|
142
|
+
node.more = total - children.length;
|
|
143
|
+
if (map)
|
|
144
|
+
node.preview = `Map(${total})`;
|
|
145
|
+
return node;
|
|
146
|
+
};
|
|
147
|
+
return walk(value, label, 0);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
class TreeModel {
|
|
151
|
+
rows = [];
|
|
152
|
+
version = 0;
|
|
153
|
+
roots = [];
|
|
154
|
+
depth;
|
|
155
|
+
opened = new Set;
|
|
156
|
+
closed = new Set;
|
|
157
|
+
index = null;
|
|
158
|
+
constructor(value, options = {}) {
|
|
159
|
+
this.depth = options.depth ?? 1;
|
|
160
|
+
this.setValue(value);
|
|
161
|
+
}
|
|
162
|
+
setValue(value) {
|
|
163
|
+
this.roots = value == null ? [] : Array.isArray(value) ? value : [value];
|
|
164
|
+
this.opened.clear();
|
|
165
|
+
this.closed.clear();
|
|
166
|
+
this.flatten();
|
|
167
|
+
}
|
|
168
|
+
setDepth(depth) {
|
|
169
|
+
this.depth = depth;
|
|
170
|
+
this.opened.clear();
|
|
171
|
+
this.closed.clear();
|
|
172
|
+
this.flatten();
|
|
173
|
+
}
|
|
174
|
+
getRoots() {
|
|
175
|
+
return this.roots;
|
|
176
|
+
}
|
|
177
|
+
indexOf(id) {
|
|
178
|
+
if (!this.index) {
|
|
179
|
+
this.index = new Map;
|
|
180
|
+
for (let i = 0;i < this.rows.length; i++)
|
|
181
|
+
this.index.set(this.rows[i].id, i);
|
|
182
|
+
}
|
|
183
|
+
return this.index.get(id) ?? -1;
|
|
184
|
+
}
|
|
185
|
+
pathOf(index) {
|
|
186
|
+
const path = [];
|
|
187
|
+
for (let at = index;at >= 0; at = this.rows[at].parent) {
|
|
188
|
+
const row = this.rows[at];
|
|
189
|
+
if (row.isMore)
|
|
190
|
+
continue;
|
|
191
|
+
if (row.parent !== -1 || this.roots.length > 1)
|
|
192
|
+
path.push(row.key);
|
|
193
|
+
}
|
|
194
|
+
return path.reverse();
|
|
195
|
+
}
|
|
196
|
+
isExpanded(id, depth) {
|
|
197
|
+
return this.opened.has(id) || depth < this.depth && !this.closed.has(id);
|
|
198
|
+
}
|
|
199
|
+
expand(id) {
|
|
200
|
+
return this.set(id, true);
|
|
201
|
+
}
|
|
202
|
+
collapse(id) {
|
|
203
|
+
return this.set(id, false);
|
|
204
|
+
}
|
|
205
|
+
toggle(id) {
|
|
206
|
+
const index = this.indexOf(id);
|
|
207
|
+
if (index === -1)
|
|
208
|
+
return false;
|
|
209
|
+
return this.set(id, !this.rows[index].expanded);
|
|
210
|
+
}
|
|
211
|
+
expandAll(id) {
|
|
212
|
+
const open = (node, rowId, depth) => {
|
|
213
|
+
if (!isExpandable(node))
|
|
214
|
+
return;
|
|
215
|
+
if (depth >= this.depth)
|
|
216
|
+
this.opened.add(rowId);
|
|
217
|
+
this.closed.delete(rowId);
|
|
218
|
+
node.children?.forEach((child, index) => open(child, child.id ?? `${rowId}.${index}`, depth + 1));
|
|
219
|
+
};
|
|
220
|
+
if (id === undefined) {
|
|
221
|
+
this.roots.forEach((root, index) => open(root, root.id ?? String(index), 0));
|
|
222
|
+
} else {
|
|
223
|
+
const found = this.find(id);
|
|
224
|
+
if (!found)
|
|
225
|
+
return;
|
|
226
|
+
open(found.node, id, found.ancestors.length);
|
|
227
|
+
}
|
|
228
|
+
this.flatten();
|
|
229
|
+
}
|
|
230
|
+
collapseAll() {
|
|
231
|
+
this.opened.clear();
|
|
232
|
+
this.closed.clear();
|
|
233
|
+
const close = (node, rowId, depth) => {
|
|
234
|
+
if (depth >= this.depth || !isExpandable(node))
|
|
235
|
+
return;
|
|
236
|
+
this.closed.add(rowId);
|
|
237
|
+
node.children?.forEach((child, index) => close(child, child.id ?? `${rowId}.${index}`, depth + 1));
|
|
238
|
+
};
|
|
239
|
+
this.roots.forEach((root, index) => close(root, root.id ?? String(index), 0));
|
|
240
|
+
this.flatten();
|
|
241
|
+
}
|
|
242
|
+
reveal(id) {
|
|
243
|
+
const found = this.find(id);
|
|
244
|
+
if (!found)
|
|
245
|
+
return -1;
|
|
246
|
+
found.ancestors.forEach((ancestor, depth) => {
|
|
247
|
+
this.closed.delete(ancestor);
|
|
248
|
+
if (depth >= this.depth)
|
|
249
|
+
this.opened.add(ancestor);
|
|
250
|
+
});
|
|
251
|
+
this.flatten();
|
|
252
|
+
return this.indexOf(id);
|
|
253
|
+
}
|
|
254
|
+
appendChildren(id, nodes, more = 0) {
|
|
255
|
+
const found = this.find(id);
|
|
256
|
+
if (!found)
|
|
257
|
+
return false;
|
|
258
|
+
const node = found.node;
|
|
259
|
+
node.children = [...node.children ?? [], ...nodes];
|
|
260
|
+
if (more > 0)
|
|
261
|
+
node.more = more;
|
|
262
|
+
else
|
|
263
|
+
delete node.more;
|
|
264
|
+
this.flatten();
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
move(index, key, pageSize = 10) {
|
|
268
|
+
const last = this.rows.length - 1;
|
|
269
|
+
if (last < 0)
|
|
270
|
+
return { index: -1, changed: false };
|
|
271
|
+
const at = Math.min(Math.max(index, 0), last);
|
|
272
|
+
const row = this.rows[at];
|
|
273
|
+
const still = (next) => ({ index: Math.min(Math.max(next, 0), last), changed: false });
|
|
274
|
+
switch (key) {
|
|
275
|
+
case "ArrowDown":
|
|
276
|
+
return still(index < 0 ? 0 : at + 1);
|
|
277
|
+
case "ArrowUp":
|
|
278
|
+
return still(at - 1);
|
|
279
|
+
case "Home":
|
|
280
|
+
return still(0);
|
|
281
|
+
case "End":
|
|
282
|
+
return still(last);
|
|
283
|
+
case "PageDown":
|
|
284
|
+
return still(at + pageSize);
|
|
285
|
+
case "PageUp":
|
|
286
|
+
return still(at - pageSize);
|
|
287
|
+
case "ArrowRight":
|
|
288
|
+
if (!row.expandable || row.isMore)
|
|
289
|
+
return still(at);
|
|
290
|
+
if (!row.expanded)
|
|
291
|
+
return { index: at, changed: this.expand(row.id) };
|
|
292
|
+
return still(at + 1);
|
|
293
|
+
case "ArrowLeft":
|
|
294
|
+
if (row.expanded && !row.isMore)
|
|
295
|
+
return { index: at, changed: this.collapse(row.id) };
|
|
296
|
+
return still(row.parent === -1 ? at : row.parent);
|
|
297
|
+
case "*": {
|
|
298
|
+
let changed = false;
|
|
299
|
+
const parent = row.parent;
|
|
300
|
+
const siblings = this.rows.filter((candidate) => candidate.parent === parent && candidate.depth === row.depth && candidate.expandable && !candidate.expanded && !candidate.isMore);
|
|
301
|
+
for (const sibling of siblings) {
|
|
302
|
+
this.mark(sibling.id, sibling.depth, true);
|
|
303
|
+
changed = true;
|
|
304
|
+
}
|
|
305
|
+
if (changed)
|
|
306
|
+
this.flatten();
|
|
307
|
+
return { index: this.indexOf(row.id), changed };
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return still(at);
|
|
311
|
+
}
|
|
312
|
+
typeahead(index, text) {
|
|
313
|
+
const needle = text.toLowerCase();
|
|
314
|
+
const count = this.rows.length;
|
|
315
|
+
if (needle === "" || count === 0)
|
|
316
|
+
return -1;
|
|
317
|
+
const from = needle.length === 1 ? index + 1 : Math.max(index, 0);
|
|
318
|
+
for (let offset = 0;offset < count; offset++) {
|
|
319
|
+
const at = (from + offset + count) % count;
|
|
320
|
+
const row = this.rows[at];
|
|
321
|
+
if (!row.isMore && (row.node.label ?? "").toLowerCase().startsWith(needle))
|
|
322
|
+
return at;
|
|
323
|
+
}
|
|
324
|
+
return -1;
|
|
325
|
+
}
|
|
326
|
+
set(id, expanded) {
|
|
327
|
+
const index = this.indexOf(id);
|
|
328
|
+
if (index === -1)
|
|
329
|
+
return false;
|
|
330
|
+
const row = this.rows[index];
|
|
331
|
+
if (!row.expandable || row.isMore || row.expanded === expanded)
|
|
332
|
+
return false;
|
|
333
|
+
this.mark(id, row.depth, expanded);
|
|
334
|
+
this.flatten();
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
mark(id, depth, expanded) {
|
|
338
|
+
if (expanded) {
|
|
339
|
+
this.closed.delete(id);
|
|
340
|
+
if (depth >= this.depth)
|
|
341
|
+
this.opened.add(id);
|
|
342
|
+
} else {
|
|
343
|
+
this.opened.delete(id);
|
|
344
|
+
if (depth < this.depth)
|
|
345
|
+
this.closed.add(id);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
find(id) {
|
|
349
|
+
const stack = [];
|
|
350
|
+
for (let index = this.roots.length - 1;index >= 0; index--)
|
|
351
|
+
stack.push({ node: this.roots[index], id: this.roots[index].id ?? String(index), ancestors: [] });
|
|
352
|
+
while (stack.length > 0) {
|
|
353
|
+
const current = stack.pop();
|
|
354
|
+
if (current.id === id)
|
|
355
|
+
return { node: current.node, ancestors: current.ancestors };
|
|
356
|
+
const children = current.node.children;
|
|
357
|
+
if (!children || current.node.ref !== undefined)
|
|
358
|
+
continue;
|
|
359
|
+
const ancestors = [...current.ancestors, current.id];
|
|
360
|
+
for (let index = children.length - 1;index >= 0; index--)
|
|
361
|
+
stack.push({ node: children[index], id: children[index].id ?? `${current.id}.${index}`, ancestors });
|
|
362
|
+
}
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
flatten() {
|
|
366
|
+
const rows = [];
|
|
367
|
+
const visit = (node, id, depth, parent, key, posInSet, setSize) => {
|
|
368
|
+
const expandable = isExpandable(node);
|
|
369
|
+
const expanded = expandable && this.isExpanded(id, depth);
|
|
370
|
+
const at = rows.length;
|
|
371
|
+
rows.push({ id, node, depth, key, parent, expandable, expanded, posInSet, setSize, isMore: false });
|
|
372
|
+
if (!expanded)
|
|
373
|
+
return;
|
|
374
|
+
const children = node.children ?? [];
|
|
375
|
+
const more = node.more ?? 0;
|
|
376
|
+
const indexed = isIndexed(node);
|
|
377
|
+
const size = children.length + (more > 0 ? 1 : 0);
|
|
378
|
+
for (let index = 0;index < children.length; index++) {
|
|
379
|
+
const child = children[index];
|
|
380
|
+
visit(child, child.id ?? `${id}.${index}`, depth + 1, at, child.key ?? (indexed ? index : child.label ?? index), index + 1, size);
|
|
381
|
+
}
|
|
382
|
+
if (more > 0)
|
|
383
|
+
rows.push({ id: `${id}.+`, node, depth: depth + 1, key: "", parent: at, expandable: false, expanded: false, posInSet: size, setSize: size, isMore: true });
|
|
384
|
+
};
|
|
385
|
+
this.roots.forEach((root, index) => visit(root, root.id ?? String(index), 0, -1, root.key ?? index, index + 1, this.roots.length));
|
|
386
|
+
this.rows = rows;
|
|
387
|
+
this.index = null;
|
|
388
|
+
this.version++;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
// src/ui/json-tree/tinker.ts
|
|
392
|
+
var CONTAINERS = new Set(["array", "object", "map", "set", "model", "error"]);
|
|
393
|
+
var STRING_PREVIEW = 120;
|
|
394
|
+
var SUMMARY_KEYS = 5;
|
|
395
|
+
function isRecordLike(value) {
|
|
396
|
+
return value.t === "object" || value.t === "model";
|
|
397
|
+
}
|
|
398
|
+
function isTabular(items) {
|
|
399
|
+
return items.length > 0 && items.slice(0, 20).every(isRecordLike);
|
|
400
|
+
}
|
|
401
|
+
function hasNestedContainer(values) {
|
|
402
|
+
return values.some((value) => CONTAINERS.has(value.t));
|
|
403
|
+
}
|
|
404
|
+
function quoted(text, truncated) {
|
|
405
|
+
const shown = text.length > STRING_PREVIEW ? `${text.slice(0, STRING_PREVIEW)}…` : text;
|
|
406
|
+
return truncated ? `${JSON.stringify(shown)} (${truncated} more characters)` : JSON.stringify(shown);
|
|
407
|
+
}
|
|
408
|
+
function frameLocation(frame) {
|
|
409
|
+
if (!frame.file)
|
|
410
|
+
return "";
|
|
411
|
+
return [frame.file, frame.line, frame.column].filter((part) => part !== undefined).join(":");
|
|
412
|
+
}
|
|
413
|
+
function fromTinkerValue(value, label, options = {}) {
|
|
414
|
+
const prefix = options.idPrefix ?? "t";
|
|
415
|
+
const idOf = (id) => `${prefix}${id}`;
|
|
416
|
+
const named = (node, name) => {
|
|
417
|
+
if (name !== undefined)
|
|
418
|
+
node.label = name;
|
|
419
|
+
return node;
|
|
420
|
+
};
|
|
421
|
+
const short = (inner) => walk(inner, undefined).preview ?? inner.t;
|
|
422
|
+
const open = (node, kinds) => {
|
|
423
|
+
if (kinds.length > 0)
|
|
424
|
+
node.openable = kinds.length === 1 ? kinds[0] : kinds;
|
|
425
|
+
return node;
|
|
426
|
+
};
|
|
427
|
+
function walk(current, name) {
|
|
428
|
+
switch (current.t) {
|
|
429
|
+
case "undefined":
|
|
430
|
+
return named({ kind: "undefined", value: undefined, preview: "undefined" }, name);
|
|
431
|
+
case "null":
|
|
432
|
+
return named({ kind: "null", value: null, preview: "null" }, name);
|
|
433
|
+
case "boolean":
|
|
434
|
+
return named({ kind: "boolean", value: current.v, preview: String(current.v) }, name);
|
|
435
|
+
case "number": {
|
|
436
|
+
const number = typeof current.v === "number" ? current.v : current.v === "-0" ? -0 : Number(current.v);
|
|
437
|
+
return named({ kind: "number", value: number, preview: String(current.v) }, name);
|
|
438
|
+
}
|
|
439
|
+
case "bigint":
|
|
440
|
+
return named({ kind: "bigint", value: current.v, preview: `${current.v}n` }, name);
|
|
441
|
+
case "string":
|
|
442
|
+
return named({ kind: "string", value: current.v, preview: quoted(current.v, current.truncated) }, name);
|
|
443
|
+
case "symbol":
|
|
444
|
+
return named({ kind: "symbol", preview: `Symbol(${current.v})` }, name);
|
|
445
|
+
case "function": {
|
|
446
|
+
const preview = current.kind === "class" ? `class ${current.name}` : `${current.kind === "async" ? "async " : ""}ƒ${current.kind === "generator" ? "*" : ""} ${current.name}()`;
|
|
447
|
+
return named({ kind: "function", preview }, name);
|
|
448
|
+
}
|
|
449
|
+
case "date":
|
|
450
|
+
return named({ kind: "date", value: current.v, preview: current.v ?? "Invalid Date" }, name);
|
|
451
|
+
case "regexp":
|
|
452
|
+
return named({ kind: "regexp", value: current.v, preview: current.v }, name);
|
|
453
|
+
case "url":
|
|
454
|
+
return named({ kind: "url", value: current.v, preview: current.v }, name);
|
|
455
|
+
case "promise":
|
|
456
|
+
return named({ kind: "promise", preview: "Promise" }, name);
|
|
457
|
+
case "bytes": {
|
|
458
|
+
const cut = current.length * 2 > current.hex.length;
|
|
459
|
+
return named({ kind: "bytes", value: current.hex, preview: `${current.ctor}(${current.length}) <${current.hex}${cut ? "…" : ""}>` }, name);
|
|
460
|
+
}
|
|
461
|
+
case "ref":
|
|
462
|
+
return named({ kind: "ref", ref: idOf(current.id), preview: "[Circular]" }, name);
|
|
463
|
+
case "cut":
|
|
464
|
+
return named({ kind: "cut", preview: `${current.ctor ?? "Object"} … (${current.reason === "depth" ? "too deep to show" : "result too large to show"})` }, name);
|
|
465
|
+
case "array": {
|
|
466
|
+
const node = {
|
|
467
|
+
id: idOf(current.id),
|
|
468
|
+
kind: "array",
|
|
469
|
+
preview: `${current.ctor ?? "Array"}(${current.length})`,
|
|
470
|
+
children: current.items.map((item, index) => walk(item, String(index)))
|
|
471
|
+
};
|
|
472
|
+
if (current.more)
|
|
473
|
+
node.more = current.more;
|
|
474
|
+
return named(open(node, isTabular(current.items) ? ["table"] : []), name);
|
|
475
|
+
}
|
|
476
|
+
case "set": {
|
|
477
|
+
const node = {
|
|
478
|
+
id: idOf(current.id),
|
|
479
|
+
kind: "set",
|
|
480
|
+
preview: `Set(${current.size})`,
|
|
481
|
+
children: current.items.map((item, index) => walk(item, String(index)))
|
|
482
|
+
};
|
|
483
|
+
if (current.more)
|
|
484
|
+
node.more = current.more;
|
|
485
|
+
return named(open(node, isTabular(current.items) ? ["table"] : []), name);
|
|
486
|
+
}
|
|
487
|
+
case "object": {
|
|
488
|
+
const keys = current.entries.slice(0, SUMMARY_KEYS).map(([key]) => key).join(", ");
|
|
489
|
+
const total = current.entries.length + (current.more ?? 0);
|
|
490
|
+
const body = total === 0 ? "{}" : `{${keys}${total > SUMMARY_KEYS ? ", …" : ""}}`;
|
|
491
|
+
const node = {
|
|
492
|
+
id: idOf(current.id),
|
|
493
|
+
kind: "object",
|
|
494
|
+
preview: current.ctor && current.ctor !== "Object" ? `${current.ctor} ${body}` : body,
|
|
495
|
+
children: current.entries.map(([key, inner]) => walk(inner, key))
|
|
496
|
+
};
|
|
497
|
+
if (current.more)
|
|
498
|
+
node.more = current.more;
|
|
499
|
+
return named(open(node, hasNestedContainer(current.entries.map((entry) => entry[1])) ? ["graph"] : []), name);
|
|
500
|
+
}
|
|
501
|
+
case "map": {
|
|
502
|
+
const node = {
|
|
503
|
+
id: idOf(current.id),
|
|
504
|
+
kind: "map",
|
|
505
|
+
preview: `Map(${current.size})`,
|
|
506
|
+
children: current.entries.map(([key, inner], index) => {
|
|
507
|
+
if (!CONTAINERS.has(key.t)) {
|
|
508
|
+
const child = walk(inner, short(key));
|
|
509
|
+
child.key = key.t === "string" ? key.v : short(key);
|
|
510
|
+
return child;
|
|
511
|
+
}
|
|
512
|
+
return { kind: "entry", label: String(index), key: index, preview: `${short(key)} => ${short(inner)}`, children: [walk(key, "key"), walk(inner, "value")] };
|
|
513
|
+
})
|
|
514
|
+
};
|
|
515
|
+
if (current.more)
|
|
516
|
+
node.more = current.more;
|
|
517
|
+
return named(open(node, hasNestedContainer(current.entries.map((entry) => entry[1])) ? ["graph"] : []), name);
|
|
518
|
+
}
|
|
519
|
+
case "model": {
|
|
520
|
+
const children = current.attributes.map(([key, inner]) => walk(inner, key));
|
|
521
|
+
for (const [relation, inner] of current.loaded ?? []) {
|
|
522
|
+
const child = walk(inner, relation);
|
|
523
|
+
child.meta = child.meta ?? { relation: true };
|
|
524
|
+
children.push(child);
|
|
525
|
+
}
|
|
526
|
+
if (current.hidden?.length) {
|
|
527
|
+
children.push({
|
|
528
|
+
kind: "hidden",
|
|
529
|
+
label: "[hidden]",
|
|
530
|
+
key: "[hidden]",
|
|
531
|
+
preview: current.hidden.join(", "),
|
|
532
|
+
children: current.hidden.map((hidden, index) => ({ kind: "hidden", label: String(index), value: hidden, preview: hidden }))
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
if (current.relations?.length) {
|
|
536
|
+
children.push({
|
|
537
|
+
kind: "relations",
|
|
538
|
+
label: "[relations]",
|
|
539
|
+
key: "[relations]",
|
|
540
|
+
preview: `${current.relations.length} declared`,
|
|
541
|
+
children: current.relations.map((relation) => ({
|
|
542
|
+
kind: "relation",
|
|
543
|
+
label: relation.name ?? relation.model,
|
|
544
|
+
value: relation,
|
|
545
|
+
preview: `${relation.kind} ${relation.model}`
|
|
546
|
+
}))
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
const meta = { model: current.model, hidden: current.hidden ?? [], relations: current.relations ?? [] };
|
|
550
|
+
if (current.table)
|
|
551
|
+
meta.table = current.table;
|
|
552
|
+
if (current.primaryKey)
|
|
553
|
+
meta.primaryKey = current.primaryKey;
|
|
554
|
+
const node = {
|
|
555
|
+
id: idOf(current.id),
|
|
556
|
+
kind: "model",
|
|
557
|
+
preview: current.key ? `${current.model} #${short(current.key).replace(/^"|"$/g, "")}` : current.model,
|
|
558
|
+
children,
|
|
559
|
+
meta
|
|
560
|
+
};
|
|
561
|
+
if (current.more)
|
|
562
|
+
node.more = current.more;
|
|
563
|
+
return named(open(node, ["graph"]), name);
|
|
564
|
+
}
|
|
565
|
+
case "html": {
|
|
566
|
+
const meta = { kind: current.kind };
|
|
567
|
+
if (current.title)
|
|
568
|
+
meta.title = current.title;
|
|
569
|
+
if (current.text)
|
|
570
|
+
meta.text = current.text;
|
|
571
|
+
if (current.truncated)
|
|
572
|
+
meta.truncated = current.truncated;
|
|
573
|
+
const cut = current.truncated ? `, ${current.truncated} more cut` : "";
|
|
574
|
+
const title = current.title ? `: ${current.title}` : "";
|
|
575
|
+
return named(open({ kind: "html", value: current.html, preview: `${current.kind}${title} (${current.html.length} characters${cut})`, meta }, ["preview"]), name);
|
|
576
|
+
}
|
|
577
|
+
case "error": {
|
|
578
|
+
const children = [{ kind: "string", label: "message", value: current.message, preview: quoted(current.message) }];
|
|
579
|
+
if (current.frames?.length) {
|
|
580
|
+
children.push({
|
|
581
|
+
kind: "stack",
|
|
582
|
+
label: "stack",
|
|
583
|
+
preview: `${current.frames.length} frames`,
|
|
584
|
+
value: current.stack,
|
|
585
|
+
children: current.frames.map((frame, index) => ({
|
|
586
|
+
kind: "frame",
|
|
587
|
+
label: String(index),
|
|
588
|
+
value: frame,
|
|
589
|
+
preview: [frame.function, frameLocation(frame)].filter(Boolean).join(" at ") || "(unknown)",
|
|
590
|
+
meta: frame
|
|
591
|
+
}))
|
|
592
|
+
});
|
|
593
|
+
} else if (current.stack) {
|
|
594
|
+
children.push({ kind: "string", label: "stack", value: current.stack, preview: quoted(current.stack) });
|
|
595
|
+
}
|
|
596
|
+
if (current.cause)
|
|
597
|
+
children.push(walk(current.cause, "cause"));
|
|
598
|
+
for (const [key, inner] of current.entries ?? [])
|
|
599
|
+
children.push(walk(inner, key));
|
|
600
|
+
return named({ kind: "error", preview: current.message ? `${current.name}: ${current.message}` : current.name, children }, name);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
return named({ kind: "unknown", preview: String(current.t ?? "unknown") }, name);
|
|
604
|
+
}
|
|
605
|
+
return walk(value, label);
|
|
606
|
+
}
|
|
607
|
+
// src/ui/json-tree/view.ts
|
|
608
|
+
var TYPEAHEAD_RESET = 600;
|
|
609
|
+
var TEXT_LIMIT = 400;
|
|
610
|
+
var OPEN_LABELS = { table: "Table", graph: "Graph", preview: "Preview" };
|
|
611
|
+
function clip(text) {
|
|
612
|
+
return text.length > TEXT_LIMIT ? `${text.slice(0, TEXT_LIMIT)}…` : text;
|
|
613
|
+
}
|
|
614
|
+
function listOf(value) {
|
|
615
|
+
if (value == null || value === "")
|
|
616
|
+
return [];
|
|
617
|
+
return Array.isArray(value) ? value : [value];
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
class JsonTreeView {
|
|
621
|
+
root;
|
|
622
|
+
callbacks;
|
|
623
|
+
model;
|
|
624
|
+
scroller;
|
|
625
|
+
content;
|
|
626
|
+
rowsEl;
|
|
627
|
+
status;
|
|
628
|
+
prefix;
|
|
629
|
+
rowHeight;
|
|
630
|
+
overscan;
|
|
631
|
+
openable;
|
|
632
|
+
contextMenu;
|
|
633
|
+
pool = [];
|
|
634
|
+
parts = new WeakMap;
|
|
635
|
+
active = -1;
|
|
636
|
+
activeId = "";
|
|
637
|
+
typed = "";
|
|
638
|
+
typedAt = 0;
|
|
639
|
+
observer = null;
|
|
640
|
+
disposers = [];
|
|
641
|
+
constructor(root, options = {}, callbacks = {}) {
|
|
642
|
+
this.root = root;
|
|
643
|
+
this.callbacks = callbacks;
|
|
644
|
+
const part = (name) => root.querySelector(`[data-jt="${name}"]`);
|
|
645
|
+
this.scroller = part("scroller") ?? root;
|
|
646
|
+
this.content = part("content") ?? this.scroller;
|
|
647
|
+
this.rowsEl = part("rows") ?? this.content;
|
|
648
|
+
this.status = part("status");
|
|
649
|
+
this.prefix = `${root.id || `json-tree-${Math.random().toString(36).slice(2, 8)}`}-row-`;
|
|
650
|
+
this.rowHeight = options.rowHeight && options.rowHeight > 0 ? options.rowHeight : 22;
|
|
651
|
+
this.overscan = options.overscan ?? 6;
|
|
652
|
+
this.openable = options.openable ?? null;
|
|
653
|
+
this.contextMenu = Boolean(options.contextMenu);
|
|
654
|
+
this.model = new TreeModel(options.value, { depth: options.depth ?? 1 });
|
|
655
|
+
this.rowsEl.__stx_inert_content = true;
|
|
656
|
+
root.style.setProperty("--stx-tree-row-height", `${this.rowHeight}px`);
|
|
657
|
+
const listen = (target, type, handler, options) => {
|
|
658
|
+
target.addEventListener(type, handler, options);
|
|
659
|
+
this.disposers.push(() => target.removeEventListener(type, handler, options));
|
|
660
|
+
};
|
|
661
|
+
listen(this.scroller, "scroll", () => this.render(), { passive: true });
|
|
662
|
+
listen(this.scroller, "keydown", (event) => this.onKeyDown(event));
|
|
663
|
+
listen(this.scroller, "focus", () => {
|
|
664
|
+
if (this.active === -1 && this.model.rows.length > 0)
|
|
665
|
+
this.setActive(0, false);
|
|
666
|
+
});
|
|
667
|
+
listen(this.rowsEl, "mousedown", (event) => {
|
|
668
|
+
if (event.target.closest("[data-jt-action]"))
|
|
669
|
+
event.preventDefault();
|
|
670
|
+
});
|
|
671
|
+
listen(this.rowsEl, "click", (event) => this.onClick(event));
|
|
672
|
+
listen(this.rowsEl, "dblclick", (event) => this.onDoubleClick(event));
|
|
673
|
+
listen(this.rowsEl, "contextmenu", (event) => this.onContextMenu(event));
|
|
674
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
675
|
+
this.observer = new ResizeObserver(() => this.render());
|
|
676
|
+
this.observer.observe(this.scroller);
|
|
677
|
+
}
|
|
678
|
+
this.render();
|
|
679
|
+
root.setAttribute("data-jt-ready", "");
|
|
680
|
+
}
|
|
681
|
+
setValue(value) {
|
|
682
|
+
this.model.setValue(value);
|
|
683
|
+
this.active = -1;
|
|
684
|
+
this.activeId = "";
|
|
685
|
+
this.scroller.scrollTop = 0;
|
|
686
|
+
this.render();
|
|
687
|
+
}
|
|
688
|
+
setDepth(depth) {
|
|
689
|
+
this.model.setDepth(depth);
|
|
690
|
+
this.afterChange();
|
|
691
|
+
}
|
|
692
|
+
setOpenable(openable) {
|
|
693
|
+
this.openable = openable ?? null;
|
|
694
|
+
this.model.version++;
|
|
695
|
+
this.render();
|
|
696
|
+
}
|
|
697
|
+
setContextMenu(on) {
|
|
698
|
+
this.contextMenu = on;
|
|
699
|
+
}
|
|
700
|
+
expandAll(id) {
|
|
701
|
+
this.model.expandAll(id);
|
|
702
|
+
this.afterChange();
|
|
703
|
+
}
|
|
704
|
+
collapseAll() {
|
|
705
|
+
this.model.collapseAll();
|
|
706
|
+
this.afterChange();
|
|
707
|
+
}
|
|
708
|
+
expand(id) {
|
|
709
|
+
const changed = this.model.expand(id);
|
|
710
|
+
if (changed)
|
|
711
|
+
this.afterChange();
|
|
712
|
+
return changed;
|
|
713
|
+
}
|
|
714
|
+
collapse(id) {
|
|
715
|
+
const changed = this.model.collapse(id);
|
|
716
|
+
if (changed)
|
|
717
|
+
this.afterChange();
|
|
718
|
+
return changed;
|
|
719
|
+
}
|
|
720
|
+
reveal(id) {
|
|
721
|
+
const index = this.model.reveal(id);
|
|
722
|
+
if (index === -1) {
|
|
723
|
+
this.afterChange();
|
|
724
|
+
return false;
|
|
725
|
+
}
|
|
726
|
+
this.active = index;
|
|
727
|
+
this.activeId = id;
|
|
728
|
+
this.scrollToRow(index);
|
|
729
|
+
this.render();
|
|
730
|
+
this.emitSelect();
|
|
731
|
+
return true;
|
|
732
|
+
}
|
|
733
|
+
appendChildren(id, nodes, more = 0) {
|
|
734
|
+
const done = this.model.appendChildren(id, nodes, more);
|
|
735
|
+
if (done)
|
|
736
|
+
this.afterChange();
|
|
737
|
+
return done;
|
|
738
|
+
}
|
|
739
|
+
getSelected() {
|
|
740
|
+
return this.active === -1 ? null : this.detail(this.active);
|
|
741
|
+
}
|
|
742
|
+
select(id) {
|
|
743
|
+
const index = this.model.indexOf(id);
|
|
744
|
+
if (index === -1)
|
|
745
|
+
return false;
|
|
746
|
+
this.setActive(index, true);
|
|
747
|
+
return true;
|
|
748
|
+
}
|
|
749
|
+
focus() {
|
|
750
|
+
this.scroller.focus();
|
|
751
|
+
}
|
|
752
|
+
get drawn() {
|
|
753
|
+
return this.pool.filter((row) => row.style.display !== "none").length;
|
|
754
|
+
}
|
|
755
|
+
destroy() {
|
|
756
|
+
this.observer?.disconnect();
|
|
757
|
+
for (const dispose of this.disposers)
|
|
758
|
+
dispose();
|
|
759
|
+
this.rowsEl.textContent = "";
|
|
760
|
+
this.pool = [];
|
|
761
|
+
this.root.removeAttribute("data-jt-ready");
|
|
762
|
+
}
|
|
763
|
+
afterChange() {
|
|
764
|
+
if (this.activeId) {
|
|
765
|
+
const index = this.model.indexOf(this.activeId);
|
|
766
|
+
this.active = index;
|
|
767
|
+
if (index === -1)
|
|
768
|
+
this.activeId = "";
|
|
769
|
+
}
|
|
770
|
+
this.render();
|
|
771
|
+
}
|
|
772
|
+
detail(index) {
|
|
773
|
+
const row = this.model.rows[index];
|
|
774
|
+
const path = this.model.pathOf(index);
|
|
775
|
+
return { id: row.id, node: row.node, path, pathText: formatPath(path) };
|
|
776
|
+
}
|
|
777
|
+
emitSelect() {
|
|
778
|
+
if (this.active !== -1)
|
|
779
|
+
this.callbacks.onSelect?.(this.detail(this.active));
|
|
780
|
+
}
|
|
781
|
+
setActive(index, scroll) {
|
|
782
|
+
if (index < 0 || index >= this.model.rows.length)
|
|
783
|
+
return;
|
|
784
|
+
const moved = index !== this.active;
|
|
785
|
+
this.active = index;
|
|
786
|
+
this.activeId = this.model.rows[index].id;
|
|
787
|
+
if (scroll)
|
|
788
|
+
this.scrollToRow(index);
|
|
789
|
+
this.render();
|
|
790
|
+
if (moved)
|
|
791
|
+
this.emitSelect();
|
|
792
|
+
}
|
|
793
|
+
scrollToRow(index) {
|
|
794
|
+
const top = index * this.rowHeight;
|
|
795
|
+
const height = this.scroller.clientHeight;
|
|
796
|
+
if (top < this.scroller.scrollTop)
|
|
797
|
+
this.scroller.scrollTop = top;
|
|
798
|
+
else if (top + this.rowHeight > this.scroller.scrollTop + height)
|
|
799
|
+
this.scroller.scrollTop = top + this.rowHeight - height;
|
|
800
|
+
}
|
|
801
|
+
pageSize() {
|
|
802
|
+
return Math.max(1, Math.floor(this.scroller.clientHeight / this.rowHeight) - 1);
|
|
803
|
+
}
|
|
804
|
+
createRow() {
|
|
805
|
+
const row = document.createElement("div");
|
|
806
|
+
row.setAttribute("role", "treeitem");
|
|
807
|
+
row.className = "stx-jt-row";
|
|
808
|
+
const span = (className, hidden) => {
|
|
809
|
+
const element = document.createElement("span");
|
|
810
|
+
element.className = className;
|
|
811
|
+
if (hidden)
|
|
812
|
+
element.setAttribute("aria-hidden", "true");
|
|
813
|
+
row.appendChild(element);
|
|
814
|
+
return element;
|
|
815
|
+
};
|
|
816
|
+
const parts = {
|
|
817
|
+
twisty: span("stx-jt-twisty", true),
|
|
818
|
+
label: span("stx-jt-label", false),
|
|
819
|
+
separator: span("stx-jt-separator", true),
|
|
820
|
+
preview: span("stx-jt-preview", false),
|
|
821
|
+
actions: span("stx-jt-actions", false)
|
|
822
|
+
};
|
|
823
|
+
parts.separator.textContent = ": ";
|
|
824
|
+
this.parts.set(row, parts);
|
|
825
|
+
this.rowsEl.appendChild(row);
|
|
826
|
+
return row;
|
|
827
|
+
}
|
|
828
|
+
button(action, text, label, as) {
|
|
829
|
+
const button = document.createElement("button");
|
|
830
|
+
button.type = "button";
|
|
831
|
+
button.tabIndex = -1;
|
|
832
|
+
button.className = "stx-jt-action";
|
|
833
|
+
button.setAttribute("data-jt-action", action);
|
|
834
|
+
if (as)
|
|
835
|
+
button.setAttribute("data-jt-as", as);
|
|
836
|
+
button.setAttribute("aria-label", label);
|
|
837
|
+
button.textContent = text;
|
|
838
|
+
return button;
|
|
839
|
+
}
|
|
840
|
+
opensOf(row, index) {
|
|
841
|
+
if (row.isMore)
|
|
842
|
+
return [];
|
|
843
|
+
if (this.openable) {
|
|
844
|
+
const answer = this.openable(row.node, this.model.pathOf(index));
|
|
845
|
+
if (answer !== undefined)
|
|
846
|
+
return listOf(answer);
|
|
847
|
+
}
|
|
848
|
+
return listOf(row.node.openable);
|
|
849
|
+
}
|
|
850
|
+
render() {
|
|
851
|
+
const rows = this.model.rows;
|
|
852
|
+
const count = rows.length;
|
|
853
|
+
const height = this.scroller.clientHeight;
|
|
854
|
+
this.content.style.height = `${count * this.rowHeight}px`;
|
|
855
|
+
const top = Math.max(0, Math.min(this.scroller.scrollTop, count * this.rowHeight - height));
|
|
856
|
+
const first = Math.max(0, Math.floor(top / this.rowHeight) - this.overscan);
|
|
857
|
+
const last = Math.min(count, Math.ceil((top + height) / this.rowHeight) + this.overscan);
|
|
858
|
+
const needed = Math.max(0, last - first);
|
|
859
|
+
while (this.pool.length < needed)
|
|
860
|
+
this.pool.push(this.createRow());
|
|
861
|
+
const size = this.pool.length;
|
|
862
|
+
const used = new Uint8Array(size);
|
|
863
|
+
for (let index = first;index < last; index++) {
|
|
864
|
+
const slot = index % size;
|
|
865
|
+
used[slot] = 1;
|
|
866
|
+
this.paint(this.pool[slot], rows[index], index);
|
|
867
|
+
}
|
|
868
|
+
for (let slot = 0;slot < size; slot++) {
|
|
869
|
+
if (!used[slot] && this.pool[slot].style.display !== "none") {
|
|
870
|
+
this.pool[slot].style.display = "none";
|
|
871
|
+
this.pool[slot].__jt = undefined;
|
|
872
|
+
this.pool[slot].removeAttribute("id");
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
const activeDrawn = this.active >= first && this.active < last;
|
|
876
|
+
if (activeDrawn)
|
|
877
|
+
this.scroller.setAttribute("aria-activedescendant", this.prefix + this.active);
|
|
878
|
+
else
|
|
879
|
+
this.scroller.removeAttribute("aria-activedescendant");
|
|
880
|
+
}
|
|
881
|
+
paint(element, row, index) {
|
|
882
|
+
const version = this.model.version;
|
|
883
|
+
const selected = index === this.active;
|
|
884
|
+
const drawn = element.__jt;
|
|
885
|
+
if (!drawn || drawn.index !== index || drawn.version !== version) {
|
|
886
|
+
const parts = this.parts.get(element);
|
|
887
|
+
const node = row.node;
|
|
888
|
+
element.style.display = "";
|
|
889
|
+
element.style.transform = `translateY(${index * this.rowHeight}px)`;
|
|
890
|
+
element.style.setProperty("--stx-tree-depth", String(row.depth));
|
|
891
|
+
element.id = this.prefix + index;
|
|
892
|
+
element.setAttribute("data-jt-index", String(index));
|
|
893
|
+
element.setAttribute("aria-level", String(row.depth + 1));
|
|
894
|
+
element.setAttribute("aria-posinset", String(row.posInSet));
|
|
895
|
+
element.setAttribute("aria-setsize", String(row.setSize));
|
|
896
|
+
if (row.expandable)
|
|
897
|
+
element.setAttribute("aria-expanded", row.expanded ? "true" : "false");
|
|
898
|
+
else
|
|
899
|
+
element.removeAttribute("aria-expanded");
|
|
900
|
+
if (row.isMore) {
|
|
901
|
+
element.setAttribute("data-kind", "more");
|
|
902
|
+
parts.twisty.textContent = "";
|
|
903
|
+
parts.label.textContent = "";
|
|
904
|
+
parts.separator.style.display = "none";
|
|
905
|
+
parts.preview.textContent = `… ${node.more} more`;
|
|
906
|
+
parts.preview.setAttribute("data-kind", "more");
|
|
907
|
+
} else {
|
|
908
|
+
element.setAttribute("data-kind", node.kind);
|
|
909
|
+
parts.twisty.textContent = row.expandable ? row.expanded ? "▾" : "▸" : "";
|
|
910
|
+
const label = node.label ?? (typeof row.key === "number" && row.parent !== -1 ? String(row.key) : "");
|
|
911
|
+
parts.label.textContent = clip(label);
|
|
912
|
+
parts.separator.style.display = label === "" ? "none" : "";
|
|
913
|
+
parts.preview.textContent = clip(previewOf(node));
|
|
914
|
+
parts.preview.setAttribute("data-kind", node.kind);
|
|
915
|
+
}
|
|
916
|
+
const opens = this.opensOf(row, index);
|
|
917
|
+
const signature = row.isMore ? "+" : opens.join("|");
|
|
918
|
+
if (!drawn || drawn.opens !== signature) {
|
|
919
|
+
parts.actions.textContent = "";
|
|
920
|
+
if (row.isMore) {
|
|
921
|
+
parts.actions.appendChild(this.button("more", "Load", "Load more"));
|
|
922
|
+
} else {
|
|
923
|
+
for (const kind of opens)
|
|
924
|
+
parts.actions.appendChild(this.button("open", OPEN_LABELS[kind] ?? kind, `Open as ${OPEN_LABELS[kind]?.toLowerCase() ?? kind}`, kind));
|
|
925
|
+
parts.actions.appendChild(this.button("copy-value", "Copy", "Copy value"));
|
|
926
|
+
parts.actions.appendChild(this.button("copy-path", "Path", "Copy path"));
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
element.__jt = { index, version, opens: signature };
|
|
930
|
+
}
|
|
931
|
+
if (selected) {
|
|
932
|
+
element.setAttribute("aria-selected", "true");
|
|
933
|
+
element.setAttribute("data-active", "");
|
|
934
|
+
} else {
|
|
935
|
+
element.setAttribute("aria-selected", "false");
|
|
936
|
+
element.removeAttribute("data-active");
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
rowIndexOf(target) {
|
|
940
|
+
const element = target instanceof Element ? target.closest("[data-jt-index]") : null;
|
|
941
|
+
if (!element || element.style.display === "none")
|
|
942
|
+
return -1;
|
|
943
|
+
return Number(element.getAttribute("data-jt-index"));
|
|
944
|
+
}
|
|
945
|
+
say(text) {
|
|
946
|
+
if (this.status)
|
|
947
|
+
this.status.textContent = text;
|
|
948
|
+
}
|
|
949
|
+
toggleRow(index) {
|
|
950
|
+
const row = this.model.rows[index];
|
|
951
|
+
if (!row)
|
|
952
|
+
return;
|
|
953
|
+
if (row.isMore) {
|
|
954
|
+
this.loadMore(index);
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
if (row.node.ref !== undefined) {
|
|
958
|
+
this.follow(index);
|
|
959
|
+
return;
|
|
960
|
+
}
|
|
961
|
+
if (!row.expandable)
|
|
962
|
+
return;
|
|
963
|
+
const id = row.id;
|
|
964
|
+
this.model.toggle(id);
|
|
965
|
+
this.afterChange();
|
|
966
|
+
const at = this.model.indexOf(id);
|
|
967
|
+
if (at !== -1)
|
|
968
|
+
this.callbacks.onToggle?.({ ...this.detail(at), expanded: this.model.rows[at].expanded });
|
|
969
|
+
}
|
|
970
|
+
follow(index) {
|
|
971
|
+
const target = this.model.rows[index].node.ref;
|
|
972
|
+
if (target === undefined)
|
|
973
|
+
return;
|
|
974
|
+
if (!this.reveal(target))
|
|
975
|
+
this.say("The value this points back to is not part of what was sent.");
|
|
976
|
+
}
|
|
977
|
+
loadMore(index) {
|
|
978
|
+
const row = this.model.rows[index];
|
|
979
|
+
const parent = row.parent;
|
|
980
|
+
if (parent === -1)
|
|
981
|
+
return;
|
|
982
|
+
this.callbacks.onLoadMore?.(this.detail(parent));
|
|
983
|
+
}
|
|
984
|
+
copy(index, what) {
|
|
985
|
+
const row = this.model.rows[index];
|
|
986
|
+
if (!row)
|
|
987
|
+
return;
|
|
988
|
+
const at = row.isMore ? row.parent : index;
|
|
989
|
+
const path = this.model.pathOf(at);
|
|
990
|
+
const node = this.model.rows[at].node;
|
|
991
|
+
const text = what === "path" ? formatPath(path) : nodeToJson(node);
|
|
992
|
+
try {
|
|
993
|
+
const written = navigator.clipboard?.writeText(text);
|
|
994
|
+
written?.catch(() => {});
|
|
995
|
+
} catch {}
|
|
996
|
+
this.say(what === "path" ? `Copied path ${text}` : "Copied value");
|
|
997
|
+
this.callbacks.onCopy?.({ what, text, path, node });
|
|
998
|
+
}
|
|
999
|
+
open(index, as) {
|
|
1000
|
+
const row = this.model.rows[index];
|
|
1001
|
+
if (!row || row.isMore)
|
|
1002
|
+
return;
|
|
1003
|
+
const kind = as ?? this.opensOf(row, index)[0];
|
|
1004
|
+
if (kind === undefined)
|
|
1005
|
+
return;
|
|
1006
|
+
this.callbacks.onOpen?.({ ...this.detail(index), as: kind });
|
|
1007
|
+
}
|
|
1008
|
+
onClick(event) {
|
|
1009
|
+
const index = this.rowIndexOf(event.target);
|
|
1010
|
+
if (index === -1)
|
|
1011
|
+
return;
|
|
1012
|
+
const action = event.target instanceof Element ? event.target.closest("[data-jt-action]") : null;
|
|
1013
|
+
this.setActive(index, false);
|
|
1014
|
+
if (action) {
|
|
1015
|
+
const name = action.getAttribute("data-jt-action");
|
|
1016
|
+
if (name === "copy-value")
|
|
1017
|
+
this.copy(index, "value");
|
|
1018
|
+
else if (name === "copy-path")
|
|
1019
|
+
this.copy(index, "path");
|
|
1020
|
+
else if (name === "open")
|
|
1021
|
+
this.open(index, action.getAttribute("data-jt-as") ?? undefined);
|
|
1022
|
+
else if (name === "more")
|
|
1023
|
+
this.loadMore(index);
|
|
1024
|
+
return;
|
|
1025
|
+
}
|
|
1026
|
+
const row = this.model.rows[index];
|
|
1027
|
+
if (row.expandable || row.isMore || row.node.ref !== undefined)
|
|
1028
|
+
this.toggleRow(index);
|
|
1029
|
+
}
|
|
1030
|
+
onDoubleClick(event) {
|
|
1031
|
+
const index = this.rowIndexOf(event.target);
|
|
1032
|
+
if (index === -1 || event.target instanceof Element && event.target.closest("[data-jt-action]"))
|
|
1033
|
+
return;
|
|
1034
|
+
const row = this.model.rows[index];
|
|
1035
|
+
if (!row.expandable && !row.isMore && row.node.ref === undefined)
|
|
1036
|
+
this.open(index);
|
|
1037
|
+
}
|
|
1038
|
+
onContextMenu(event) {
|
|
1039
|
+
const index = this.rowIndexOf(event.target);
|
|
1040
|
+
if (index === -1)
|
|
1041
|
+
return;
|
|
1042
|
+
this.setActive(index, false);
|
|
1043
|
+
if (this.contextMenu)
|
|
1044
|
+
event.preventDefault();
|
|
1045
|
+
this.callbacks.onContext?.({ ...this.detail(index), x: event.clientX, y: event.clientY });
|
|
1046
|
+
}
|
|
1047
|
+
onKeyDown(event) {
|
|
1048
|
+
const rows = this.model.rows;
|
|
1049
|
+
if (rows.length === 0)
|
|
1050
|
+
return;
|
|
1051
|
+
const mod = event.metaKey || event.ctrlKey;
|
|
1052
|
+
const key = event.key;
|
|
1053
|
+
if (mod && (key === "c" || key === "C")) {
|
|
1054
|
+
if (this.active === -1)
|
|
1055
|
+
return;
|
|
1056
|
+
event.preventDefault();
|
|
1057
|
+
this.copy(this.active, event.shiftKey ? "path" : "value");
|
|
1058
|
+
return;
|
|
1059
|
+
}
|
|
1060
|
+
if (mod || event.altKey)
|
|
1061
|
+
return;
|
|
1062
|
+
if (key === "Enter" && event.shiftKey) {
|
|
1063
|
+
event.preventDefault();
|
|
1064
|
+
if (this.active !== -1)
|
|
1065
|
+
this.open(this.active);
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
const typing = this.typed !== "" && Date.now() - this.typedAt <= TYPEAHEAD_RESET;
|
|
1069
|
+
if (key === "Enter" || key === " " && !typing) {
|
|
1070
|
+
event.preventDefault();
|
|
1071
|
+
if (this.active !== -1)
|
|
1072
|
+
this.toggleRow(this.active);
|
|
1073
|
+
return;
|
|
1074
|
+
}
|
|
1075
|
+
if (key === "ArrowDown" || key === "ArrowUp" || key === "ArrowLeft" || key === "ArrowRight" || key === "Home" || key === "End" || key === "PageDown" || key === "PageUp" || key === "*") {
|
|
1076
|
+
event.preventDefault();
|
|
1077
|
+
this.typed = "";
|
|
1078
|
+
const before = this.active === -1 ? null : rows[this.active];
|
|
1079
|
+
const wasExpanded = before?.expanded;
|
|
1080
|
+
const move = this.model.move(this.active, key, this.pageSize());
|
|
1081
|
+
if (move.changed) {
|
|
1082
|
+
this.active = move.index;
|
|
1083
|
+
this.activeId = this.model.rows[move.index]?.id ?? "";
|
|
1084
|
+
this.scrollToRow(move.index);
|
|
1085
|
+
this.render();
|
|
1086
|
+
if (before && key !== "*") {
|
|
1087
|
+
const at = this.model.indexOf(before.id);
|
|
1088
|
+
if (at !== -1 && this.model.rows[at].expanded !== wasExpanded)
|
|
1089
|
+
this.callbacks.onToggle?.({ ...this.detail(at), expanded: this.model.rows[at].expanded });
|
|
1090
|
+
}
|
|
1091
|
+
} else {
|
|
1092
|
+
this.setActive(move.index, true);
|
|
1093
|
+
}
|
|
1094
|
+
return;
|
|
1095
|
+
}
|
|
1096
|
+
if (key.length === 1) {
|
|
1097
|
+
const now = Date.now();
|
|
1098
|
+
if (!typing)
|
|
1099
|
+
this.typed = "";
|
|
1100
|
+
this.typedAt = now;
|
|
1101
|
+
this.typed += key;
|
|
1102
|
+
const found = this.model.typeahead(this.active, this.typed);
|
|
1103
|
+
if (found !== -1) {
|
|
1104
|
+
event.preventDefault();
|
|
1105
|
+
this.setActive(found, true);
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
export {
|
|
1111
|
+
JsonTreeView,
|
|
1112
|
+
TreeModel,
|
|
1113
|
+
formatPath,
|
|
1114
|
+
fromTinkerValue,
|
|
1115
|
+
fromValue,
|
|
1116
|
+
isExpandable,
|
|
1117
|
+
isIndexed,
|
|
1118
|
+
nodeToJson,
|
|
1119
|
+
nodeToPlain,
|
|
1120
|
+
previewOf
|
|
1121
|
+
};
|