@stacksjs/components 0.2.854 → 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/CodeEditor-jg4w7e15.stx +497 -0
- 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/code-editor/editor.js +2166 -0
- package/dist/component-api/component-api.js +36 -0
- package/dist/components/CodeBlock.d.ts +12 -5
- 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 +10 -0
- package/dist/index.js +34 -15
- 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/code-editor/CodeEditor.d.ts +46 -0
- package/dist/ui/code-editor/controller.d.ts +103 -0
- package/dist/ui/code-editor/core/commands.d.ts +55 -0
- package/dist/ui/code-editor/core/completion.d.ts +40 -0
- package/dist/ui/code-editor/core/editor.d.ts +69 -0
- package/dist/ui/code-editor/core/highlight.d.ts +61 -0
- package/dist/ui/code-editor/core/history.d.ts +27 -0
- package/dist/ui/code-editor/core/index.d.ts +13 -0
- package/dist/ui/code-editor/core/keymap.d.ts +38 -0
- package/dist/ui/code-editor/core/selection.d.ts +30 -0
- package/dist/ui/code-editor/core/text.d.ts +64 -0
- package/dist/ui/code-editor/editor.d.ts +15 -0
- package/dist/ui/code-editor/index.d.ts +29 -0
- package/dist/ui/code-editor/tokenizer.d.ts +18 -0
- package/dist/ui/code-editor/view.d.ts +59 -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/dist/utils/highlighter.d.ts +19 -1
- package/dist/utils/highlighter.js +24 -7
- package/dist/utils/syntax-theme.d.ts +44 -0
- package/package.json +44 -4
- package/src/components/CodeBlock.stx +195 -17
- package/src/index.ts +10 -0
- package/src/ui/code-editor/CodeEditor.stx +497 -0
- package/src/ui/code-editor/README.md +135 -0
- package/src/ui/code-editor/controller.ts +517 -0
- package/src/ui/code-editor/core/commands.ts +381 -0
- package/src/ui/code-editor/core/completion.ts +212 -0
- package/src/ui/code-editor/core/editor.ts +319 -0
- package/src/ui/code-editor/core/highlight.ts +203 -0
- package/src/ui/code-editor/core/history.ts +190 -0
- package/src/ui/code-editor/core/index.ts +14 -0
- package/src/ui/code-editor/core/keymap.ts +125 -0
- package/src/ui/code-editor/core/selection.ts +104 -0
- package/src/ui/code-editor/core/text.ts +252 -0
- package/src/ui/code-editor/editor.ts +16 -0
- package/src/ui/code-editor/index.ts +46 -0
- package/src/ui/code-editor/tokenizer.ts +74 -0
- package/src/ui/code-editor/view.ts +1066 -0
- 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
- package/src/utils/highlighter.ts +121 -218
- package/src/utils/syntax-theme.ts +144 -0
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The model of a value inspector. No DOM.
|
|
3
|
+
*
|
|
4
|
+
* A value is described as a tree of nodes, each saying what kind of thing it
|
|
5
|
+
* is, what to call it and how to show it on one line. The shape is generic on
|
|
6
|
+
* purpose: nothing here knows which serializer produced the description, so
|
|
7
|
+
* the same tree shows a tinker result, a parsed JSON document or a debugger
|
|
8
|
+
* frame. `./tinker` adapts one such serializer; `fromValue` below describes a
|
|
9
|
+
* plain JavaScript value.
|
|
10
|
+
*
|
|
11
|
+
* The model turns that tree into the list of rows currently visible, which is
|
|
12
|
+
* what a virtualized view draws. Only expanded containers are walked, so the
|
|
13
|
+
* cost of every operation follows what is on show, not what exists.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** The ways a node can be opened outside the tree. Any other string is the host's own. */
|
|
17
|
+
export type TreeOpenKind = 'table' | 'graph' | 'preview' | (string & {})
|
|
18
|
+
|
|
19
|
+
export interface TreeNode {
|
|
20
|
+
/**
|
|
21
|
+
* A stable name for the node. Left out, the row is named by its position.
|
|
22
|
+
* Needed only for a node that a `ref` points at, or that the host addresses.
|
|
23
|
+
*/
|
|
24
|
+
id?: string
|
|
25
|
+
/** What the value is: `object`, `array`, `string`, `number`, `error`, ... Styles the row. */
|
|
26
|
+
kind: string
|
|
27
|
+
/** What the parent calls it: a property name, an index, a map key. */
|
|
28
|
+
label?: string
|
|
29
|
+
/**
|
|
30
|
+
* The segment this node adds to a copied path. Left out, it is the index
|
|
31
|
+
* under an array or a set and the label under anything else.
|
|
32
|
+
*/
|
|
33
|
+
key?: string | number
|
|
34
|
+
/** The value on one line. Left out, one is made from `value` or the children. */
|
|
35
|
+
preview?: string
|
|
36
|
+
/** The value itself, for a scalar. What copy puts on the clipboard. */
|
|
37
|
+
value?: unknown
|
|
38
|
+
children?: TreeNode[]
|
|
39
|
+
/** How many children exist beyond the ones in `children`. Shown as a row that asks for them. */
|
|
40
|
+
more?: number
|
|
41
|
+
/** The `id` of the node this one points back to, for a cycle. */
|
|
42
|
+
ref?: string
|
|
43
|
+
/** Where else this node can be shown. The view draws a button for each. */
|
|
44
|
+
openable?: TreeOpenKind | TreeOpenKind[]
|
|
45
|
+
/** Anything the host wants to find again on the node. Never read here. */
|
|
46
|
+
meta?: unknown
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** One visible line of the tree. */
|
|
50
|
+
export interface TreeRow {
|
|
51
|
+
id: string
|
|
52
|
+
/** The node, or for a "more" row the node whose children are missing. */
|
|
53
|
+
node: TreeNode
|
|
54
|
+
/** Zero for a root. */
|
|
55
|
+
depth: number
|
|
56
|
+
/** The path segment this row adds. */
|
|
57
|
+
key: string | number
|
|
58
|
+
/** The index of the parent row, or -1 for a root. */
|
|
59
|
+
parent: number
|
|
60
|
+
expandable: boolean
|
|
61
|
+
expanded: boolean
|
|
62
|
+
/** One-based position among its siblings, and how many siblings there are. */
|
|
63
|
+
posInSet: number
|
|
64
|
+
setSize: number
|
|
65
|
+
/** The row standing for children that were not sent. */
|
|
66
|
+
isMore: boolean
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type TreePath = Array<string | number>
|
|
70
|
+
|
|
71
|
+
export interface TreeModelOptions {
|
|
72
|
+
/** Containers nearer the root than this start expanded. Default 1: the root is open, its children closed. */
|
|
73
|
+
depth?: number
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const INDEXED = new Set(['array', 'set'])
|
|
77
|
+
const IDENTIFIER = /^[A-Z_$][\w$]*$/i
|
|
78
|
+
|
|
79
|
+
/** Whether a node's children are numbered rather than named. */
|
|
80
|
+
export function isIndexed(node: TreeNode): boolean {
|
|
81
|
+
return INDEXED.has(node.kind)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Whether a node can be opened to show something under it. */
|
|
85
|
+
export function isExpandable(node: TreeNode): boolean {
|
|
86
|
+
return node.ref === undefined && ((node.children !== undefined && node.children.length > 0) || (node.more ?? 0) > 0)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A path as JavaScript would write it: `$.users[0]["first name"]`.
|
|
91
|
+
*
|
|
92
|
+
* `$` is the value itself. A name that is a plain identifier is dotted, a
|
|
93
|
+
* number is an index, and anything else is quoted in brackets.
|
|
94
|
+
*/
|
|
95
|
+
export function formatPath(path: readonly (string | number)[], root = '$'): string {
|
|
96
|
+
let text = root
|
|
97
|
+
for (const segment of path) {
|
|
98
|
+
if (typeof segment === 'number')
|
|
99
|
+
text += `[${segment}]`
|
|
100
|
+
else if (IDENTIFIER.test(segment))
|
|
101
|
+
text += `.${segment}`
|
|
102
|
+
else
|
|
103
|
+
text += `[${JSON.stringify(segment)}]`
|
|
104
|
+
}
|
|
105
|
+
return text
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The plain value a node describes, as far as the description goes.
|
|
110
|
+
*
|
|
111
|
+
* A container becomes an array or an object of what is present: children that
|
|
112
|
+
* were never sent (`more`) are simply not there, and nothing is put in their
|
|
113
|
+
* place, so the result is always the honest JSON of what the tree holds. A
|
|
114
|
+
* cycle becomes the text `[Circular]`. A scalar is its `value` when it has
|
|
115
|
+
* one and its preview otherwise.
|
|
116
|
+
*/
|
|
117
|
+
export function nodeToPlain(node: TreeNode): unknown {
|
|
118
|
+
if (node.ref !== undefined)
|
|
119
|
+
return '[Circular]'
|
|
120
|
+
|
|
121
|
+
if (node.children) {
|
|
122
|
+
if (isIndexed(node))
|
|
123
|
+
return node.children.map(child => nodeToPlain(child))
|
|
124
|
+
|
|
125
|
+
const object: Record<string, unknown> = {}
|
|
126
|
+
node.children.forEach((child, index) => {
|
|
127
|
+
object[child.key !== undefined ? String(child.key) : (child.label ?? String(index))] = nodeToPlain(child)
|
|
128
|
+
})
|
|
129
|
+
return object
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if ('value' in node) {
|
|
133
|
+
const value = node.value
|
|
134
|
+
if (typeof value === 'bigint')
|
|
135
|
+
return value.toString()
|
|
136
|
+
if (typeof value === 'number' && !Number.isFinite(value))
|
|
137
|
+
return String(value)
|
|
138
|
+
if (typeof value === 'function' || typeof value === 'symbol')
|
|
139
|
+
return node.preview ?? String(value)
|
|
140
|
+
return value
|
|
141
|
+
}
|
|
142
|
+
return node.preview ?? null
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The node as indented JSON. `undefined`, which JSON has no word for, is the text `undefined`. */
|
|
146
|
+
export function nodeToJson(node: TreeNode, indent = 2): string {
|
|
147
|
+
const plain = nodeToPlain(node)
|
|
148
|
+
return plain === undefined ? 'undefined' : JSON.stringify(plain, null, indent)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const PREVIEW_LIMIT = 80
|
|
152
|
+
|
|
153
|
+
/** One line for a node that did not bring its own. */
|
|
154
|
+
export function previewOf(node: TreeNode): string {
|
|
155
|
+
if (node.preview !== undefined)
|
|
156
|
+
return node.preview
|
|
157
|
+
if (node.ref !== undefined)
|
|
158
|
+
return '[Circular]'
|
|
159
|
+
|
|
160
|
+
if (node.children || node.more) {
|
|
161
|
+
const count = (node.children?.length ?? 0) + (node.more ?? 0)
|
|
162
|
+
if (isIndexed(node))
|
|
163
|
+
return `${node.kind === 'set' ? 'Set' : 'Array'}(${count})`
|
|
164
|
+
return count === 0 ? '{}' : `{${count}}`
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if ('value' in node) {
|
|
168
|
+
const value = node.value
|
|
169
|
+
if (typeof value === 'string')
|
|
170
|
+
return JSON.stringify(value.length > PREVIEW_LIMIT ? `${value.slice(0, PREVIEW_LIMIT)}…` : value)
|
|
171
|
+
if (typeof value === 'bigint')
|
|
172
|
+
return `${value}n`
|
|
173
|
+
return String(value)
|
|
174
|
+
}
|
|
175
|
+
return node.kind
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface FromValueOptions {
|
|
179
|
+
/** Entries kept per container. The rest are counted in `more`. Default 1000. */
|
|
180
|
+
maxItems?: number
|
|
181
|
+
/** Levels described. Deeper containers are shown closed and empty. Default 32. */
|
|
182
|
+
maxDepth?: number
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Describe a plain JavaScript value: what `JSON.parse` returns, or any object
|
|
187
|
+
* graph. Cycles become `ref` nodes rather than a loop.
|
|
188
|
+
*/
|
|
189
|
+
export function fromValue(value: unknown, label?: string, options: FromValueOptions = {}): TreeNode {
|
|
190
|
+
const maxItems = options.maxItems ?? 1000
|
|
191
|
+
const maxDepth = options.maxDepth ?? 32
|
|
192
|
+
const seen = new Map<object, string>()
|
|
193
|
+
let nextId = 1
|
|
194
|
+
|
|
195
|
+
const named = (node: TreeNode, name: string | undefined): TreeNode => {
|
|
196
|
+
if (name !== undefined)
|
|
197
|
+
node.label = name
|
|
198
|
+
return node
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const walk = (current: unknown, name: string | undefined, depth: number): TreeNode => {
|
|
202
|
+
if (current === null)
|
|
203
|
+
return named({ kind: 'null', value: null, preview: 'null' }, name)
|
|
204
|
+
|
|
205
|
+
switch (typeof current) {
|
|
206
|
+
case 'undefined':
|
|
207
|
+
return named({ kind: 'undefined', value: undefined, preview: 'undefined' }, name)
|
|
208
|
+
case 'string':
|
|
209
|
+
case 'number':
|
|
210
|
+
case 'boolean':
|
|
211
|
+
case 'bigint':
|
|
212
|
+
return named({ kind: typeof current, value: current }, name)
|
|
213
|
+
case 'symbol':
|
|
214
|
+
return named({ kind: 'symbol', preview: current.toString() }, name)
|
|
215
|
+
case 'function':
|
|
216
|
+
return named({ kind: 'function', preview: `ƒ ${current.name || '(anonymous)'}()` }, name)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const object = current as object
|
|
220
|
+
const known = seen.get(object)
|
|
221
|
+
if (known !== undefined)
|
|
222
|
+
return named({ kind: 'ref', ref: known }, name)
|
|
223
|
+
|
|
224
|
+
if (object instanceof Date)
|
|
225
|
+
return named({ kind: 'date', value: Number.isNaN(object.getTime()) ? null : object.toISOString(), preview: Number.isNaN(object.getTime()) ? 'Invalid Date' : object.toISOString() }, name)
|
|
226
|
+
if (object instanceof RegExp)
|
|
227
|
+
return named({ kind: 'regexp', value: object.toString(), preview: object.toString() }, name)
|
|
228
|
+
|
|
229
|
+
const id = `v${nextId++}`
|
|
230
|
+
seen.set(object, id)
|
|
231
|
+
const array = Array.isArray(object)
|
|
232
|
+
const set = object instanceof Set
|
|
233
|
+
const map = object instanceof Map
|
|
234
|
+
const node: TreeNode = named({ id, kind: array ? 'array' : set ? 'set' : map ? 'map' : 'object', children: [] }, name)
|
|
235
|
+
if (depth >= maxDepth) {
|
|
236
|
+
node.preview = array ? 'Array' : 'Object'
|
|
237
|
+
delete node.children
|
|
238
|
+
return node
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const children = node.children as TreeNode[]
|
|
242
|
+
let total = 0
|
|
243
|
+
if (array || set) {
|
|
244
|
+
for (const item of object as Iterable<unknown>) {
|
|
245
|
+
if (total < maxItems)
|
|
246
|
+
children.push(walk(item, String(total), depth + 1))
|
|
247
|
+
total++
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
else if (map) {
|
|
251
|
+
for (const [key, inner] of object as Map<unknown, unknown>) {
|
|
252
|
+
if (total < maxItems)
|
|
253
|
+
children.push(walk(inner, typeof key === 'string' ? key : previewOf(walk(key, undefined, maxDepth)), depth + 1))
|
|
254
|
+
total++
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
for (const key of Object.keys(object)) {
|
|
259
|
+
if (total < maxItems)
|
|
260
|
+
children.push(walk((object as Record<string, unknown>)[key], key, depth + 1))
|
|
261
|
+
total++
|
|
262
|
+
}
|
|
263
|
+
const ctor = object.constructor?.name
|
|
264
|
+
if (ctor && ctor !== 'Object')
|
|
265
|
+
node.preview = `${ctor} {${total}}`
|
|
266
|
+
}
|
|
267
|
+
if (total > children.length)
|
|
268
|
+
node.more = total - children.length
|
|
269
|
+
if (map)
|
|
270
|
+
node.preview = `Map(${total})`
|
|
271
|
+
return node
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return walk(value, label, 0)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** What a key does to a tree: where the cursor goes, and whether the rows changed. */
|
|
278
|
+
export interface TreeMove {
|
|
279
|
+
index: number
|
|
280
|
+
/** True when rows were expanded or collapsed, so the view must redraw them all. */
|
|
281
|
+
changed: boolean
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* The visible rows of a tree, and what is expanded.
|
|
286
|
+
*/
|
|
287
|
+
export class TreeModel {
|
|
288
|
+
/** The visible rows, top to bottom. Replaced, never edited, when the tree changes. */
|
|
289
|
+
rows: TreeRow[] = []
|
|
290
|
+
/** Goes up every time `rows` is rebuilt. */
|
|
291
|
+
version = 0
|
|
292
|
+
|
|
293
|
+
private roots: TreeNode[] = []
|
|
294
|
+
private depth: number
|
|
295
|
+
/** Rows the user opened below the default depth, and closed above it. */
|
|
296
|
+
private readonly opened = new Set<string>()
|
|
297
|
+
private readonly closed = new Set<string>()
|
|
298
|
+
private index: Map<string, number> | null = null
|
|
299
|
+
|
|
300
|
+
constructor(value?: TreeNode | TreeNode[] | null, options: TreeModelOptions = {}) {
|
|
301
|
+
this.depth = options.depth ?? 1
|
|
302
|
+
this.setValue(value)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** Show another value. Forgets what was expanded. */
|
|
306
|
+
setValue(value: TreeNode | TreeNode[] | null | undefined): void {
|
|
307
|
+
this.roots = value == null ? [] : (Array.isArray(value) ? value : [value])
|
|
308
|
+
this.opened.clear()
|
|
309
|
+
this.closed.clear()
|
|
310
|
+
this.flatten()
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** Change the default depth, forgetting what was expanded by hand. */
|
|
314
|
+
setDepth(depth: number): void {
|
|
315
|
+
this.depth = depth
|
|
316
|
+
this.opened.clear()
|
|
317
|
+
this.closed.clear()
|
|
318
|
+
this.flatten()
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** The value on show, as the list of its roots. */
|
|
322
|
+
getRoots(): readonly TreeNode[] {
|
|
323
|
+
return this.roots
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** The index of a row among the visible ones, or -1. */
|
|
327
|
+
indexOf(id: string): number {
|
|
328
|
+
if (!this.index) {
|
|
329
|
+
this.index = new Map()
|
|
330
|
+
for (let i = 0; i < this.rows.length; i++)
|
|
331
|
+
this.index.set(this.rows[i].id, i)
|
|
332
|
+
}
|
|
333
|
+
return this.index.get(id) ?? -1
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** The path from the value to a row, one segment per ancestor. */
|
|
337
|
+
pathOf(index: number): TreePath {
|
|
338
|
+
const path: TreePath = []
|
|
339
|
+
// With several roots each is addressed by its position; a single root is `$` itself.
|
|
340
|
+
for (let at = index; at >= 0; at = this.rows[at].parent) {
|
|
341
|
+
const row = this.rows[at]
|
|
342
|
+
if (row.isMore)
|
|
343
|
+
continue
|
|
344
|
+
if (row.parent !== -1 || this.roots.length > 1)
|
|
345
|
+
path.push(row.key)
|
|
346
|
+
}
|
|
347
|
+
return path.reverse()
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
isExpanded(id: string, depth: number): boolean {
|
|
351
|
+
return this.opened.has(id) || (depth < this.depth && !this.closed.has(id))
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
expand(id: string): boolean {
|
|
355
|
+
return this.set(id, true)
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
collapse(id: string): boolean {
|
|
359
|
+
return this.set(id, false)
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
toggle(id: string): boolean {
|
|
363
|
+
const index = this.indexOf(id)
|
|
364
|
+
if (index === -1)
|
|
365
|
+
return false
|
|
366
|
+
return this.set(id, !this.rows[index].expanded)
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** Expand a row and everything under it, or the whole tree when no row is named. */
|
|
370
|
+
expandAll(id?: string): void {
|
|
371
|
+
const open = (node: TreeNode, rowId: string, depth: number): void => {
|
|
372
|
+
if (!isExpandable(node))
|
|
373
|
+
return
|
|
374
|
+
if (depth >= this.depth)
|
|
375
|
+
this.opened.add(rowId)
|
|
376
|
+
this.closed.delete(rowId)
|
|
377
|
+
node.children?.forEach((child, index) => open(child, child.id ?? `${rowId}.${index}`, depth + 1))
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
if (id === undefined) {
|
|
381
|
+
this.roots.forEach((root, index) => open(root, root.id ?? String(index), 0))
|
|
382
|
+
}
|
|
383
|
+
else {
|
|
384
|
+
const found = this.find(id)
|
|
385
|
+
if (!found)
|
|
386
|
+
return
|
|
387
|
+
open(found.node, id, found.ancestors.length)
|
|
388
|
+
}
|
|
389
|
+
this.flatten()
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** Close everything, the roots included. */
|
|
393
|
+
collapseAll(): void {
|
|
394
|
+
this.opened.clear()
|
|
395
|
+
this.closed.clear()
|
|
396
|
+
const close = (node: TreeNode, rowId: string, depth: number): void => {
|
|
397
|
+
if (depth >= this.depth || !isExpandable(node))
|
|
398
|
+
return
|
|
399
|
+
this.closed.add(rowId)
|
|
400
|
+
node.children?.forEach((child, index) => close(child, child.id ?? `${rowId}.${index}`, depth + 1))
|
|
401
|
+
}
|
|
402
|
+
this.roots.forEach((root, index) => close(root, root.id ?? String(index), 0))
|
|
403
|
+
this.flatten()
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Make a row visible by expanding everything above it, wherever it is in the
|
|
408
|
+
* tree. Returns its index, or -1 when no node has that id. This is how a
|
|
409
|
+
* cycle is followed back to the node it points at.
|
|
410
|
+
*/
|
|
411
|
+
reveal(id: string): number {
|
|
412
|
+
const found = this.find(id)
|
|
413
|
+
if (!found)
|
|
414
|
+
return -1
|
|
415
|
+
found.ancestors.forEach((ancestor, depth) => {
|
|
416
|
+
this.closed.delete(ancestor)
|
|
417
|
+
if (depth >= this.depth)
|
|
418
|
+
this.opened.add(ancestor)
|
|
419
|
+
})
|
|
420
|
+
this.flatten()
|
|
421
|
+
return this.indexOf(id)
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Add children that were asked for through a "more" row. `more` is how many
|
|
426
|
+
* are still missing afterwards.
|
|
427
|
+
*/
|
|
428
|
+
appendChildren(id: string, nodes: TreeNode[], more = 0): boolean {
|
|
429
|
+
const found = this.find(id)
|
|
430
|
+
if (!found)
|
|
431
|
+
return false
|
|
432
|
+
const node = found.node
|
|
433
|
+
node.children = [...(node.children ?? []), ...nodes]
|
|
434
|
+
if (more > 0)
|
|
435
|
+
node.more = more
|
|
436
|
+
else
|
|
437
|
+
delete node.more
|
|
438
|
+
this.flatten()
|
|
439
|
+
return true
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* What a navigation key does from a row. The keys are the ones the WAI-ARIA
|
|
444
|
+
* tree pattern names; anything else leaves the cursor where it is.
|
|
445
|
+
*/
|
|
446
|
+
move(index: number, key: string, pageSize = 10): TreeMove {
|
|
447
|
+
const last = this.rows.length - 1
|
|
448
|
+
if (last < 0)
|
|
449
|
+
return { index: -1, changed: false }
|
|
450
|
+
const at = Math.min(Math.max(index, 0), last)
|
|
451
|
+
const row = this.rows[at]
|
|
452
|
+
const still = (next: number): TreeMove => ({ index: Math.min(Math.max(next, 0), last), changed: false })
|
|
453
|
+
|
|
454
|
+
switch (key) {
|
|
455
|
+
case 'ArrowDown':
|
|
456
|
+
return still(index < 0 ? 0 : at + 1)
|
|
457
|
+
case 'ArrowUp':
|
|
458
|
+
return still(at - 1)
|
|
459
|
+
case 'Home':
|
|
460
|
+
return still(0)
|
|
461
|
+
case 'End':
|
|
462
|
+
return still(last)
|
|
463
|
+
case 'PageDown':
|
|
464
|
+
return still(at + pageSize)
|
|
465
|
+
case 'PageUp':
|
|
466
|
+
return still(at - pageSize)
|
|
467
|
+
case 'ArrowRight':
|
|
468
|
+
if (!row.expandable || row.isMore)
|
|
469
|
+
return still(at)
|
|
470
|
+
if (!row.expanded)
|
|
471
|
+
return { index: at, changed: this.expand(row.id) }
|
|
472
|
+
return still(at + 1)
|
|
473
|
+
case 'ArrowLeft':
|
|
474
|
+
if (row.expanded && !row.isMore)
|
|
475
|
+
return { index: at, changed: this.collapse(row.id) }
|
|
476
|
+
return still(row.parent === -1 ? at : row.parent)
|
|
477
|
+
case '*': {
|
|
478
|
+
// Every sibling at once, the row itself included.
|
|
479
|
+
let changed = false
|
|
480
|
+
const parent = row.parent
|
|
481
|
+
const siblings = this.rows.filter(candidate => candidate.parent === parent && candidate.depth === row.depth && candidate.expandable && !candidate.expanded && !candidate.isMore)
|
|
482
|
+
for (const sibling of siblings) {
|
|
483
|
+
this.mark(sibling.id, sibling.depth, true)
|
|
484
|
+
changed = true
|
|
485
|
+
}
|
|
486
|
+
if (changed)
|
|
487
|
+
this.flatten()
|
|
488
|
+
return { index: this.indexOf(row.id), changed }
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
return still(at)
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* The next row whose label starts with what was typed. A single character
|
|
496
|
+
* moves on from the current row, so pressing it again cycles; a longer run
|
|
497
|
+
* stays on the current row while it still matches.
|
|
498
|
+
*/
|
|
499
|
+
typeahead(index: number, text: string): number {
|
|
500
|
+
const needle = text.toLowerCase()
|
|
501
|
+
const count = this.rows.length
|
|
502
|
+
if (needle === '' || count === 0)
|
|
503
|
+
return -1
|
|
504
|
+
const from = needle.length === 1 ? index + 1 : Math.max(index, 0)
|
|
505
|
+
for (let offset = 0; offset < count; offset++) {
|
|
506
|
+
const at = (from + offset + count) % count
|
|
507
|
+
const row = this.rows[at]
|
|
508
|
+
if (!row.isMore && (row.node.label ?? '').toLowerCase().startsWith(needle))
|
|
509
|
+
return at
|
|
510
|
+
}
|
|
511
|
+
return -1
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
private set(id: string, expanded: boolean): boolean {
|
|
515
|
+
const index = this.indexOf(id)
|
|
516
|
+
if (index === -1)
|
|
517
|
+
return false
|
|
518
|
+
const row = this.rows[index]
|
|
519
|
+
if (!row.expandable || row.isMore || row.expanded === expanded)
|
|
520
|
+
return false
|
|
521
|
+
this.mark(id, row.depth, expanded)
|
|
522
|
+
this.flatten()
|
|
523
|
+
return true
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
private mark(id: string, depth: number, expanded: boolean): void {
|
|
527
|
+
if (expanded) {
|
|
528
|
+
this.closed.delete(id)
|
|
529
|
+
if (depth >= this.depth)
|
|
530
|
+
this.opened.add(id)
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
this.opened.delete(id)
|
|
534
|
+
if (depth < this.depth)
|
|
535
|
+
this.closed.add(id)
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/** Find a node anywhere in the tree by row id, with the ids of the rows above it. */
|
|
540
|
+
private find(id: string): { node: TreeNode, ancestors: string[] } | null {
|
|
541
|
+
const stack: Array<{ node: TreeNode, id: string, ancestors: string[] }> = []
|
|
542
|
+
for (let index = this.roots.length - 1; index >= 0; index--)
|
|
543
|
+
stack.push({ node: this.roots[index], id: this.roots[index].id ?? String(index), ancestors: [] })
|
|
544
|
+
|
|
545
|
+
while (stack.length > 0) {
|
|
546
|
+
const current = stack.pop() as { node: TreeNode, id: string, ancestors: string[] }
|
|
547
|
+
if (current.id === id)
|
|
548
|
+
return { node: current.node, ancestors: current.ancestors }
|
|
549
|
+
const children = current.node.children
|
|
550
|
+
if (!children || current.node.ref !== undefined)
|
|
551
|
+
continue
|
|
552
|
+
const ancestors = [...current.ancestors, current.id]
|
|
553
|
+
for (let index = children.length - 1; index >= 0; index--)
|
|
554
|
+
stack.push({ node: children[index], id: children[index].id ?? `${current.id}.${index}`, ancestors })
|
|
555
|
+
}
|
|
556
|
+
return null
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** Rebuild the visible rows. Walks expanded containers only. */
|
|
560
|
+
private flatten(): void {
|
|
561
|
+
const rows: TreeRow[] = []
|
|
562
|
+
|
|
563
|
+
const visit = (node: TreeNode, id: string, depth: number, parent: number, key: string | number, posInSet: number, setSize: number): void => {
|
|
564
|
+
const expandable = isExpandable(node)
|
|
565
|
+
const expanded = expandable && this.isExpanded(id, depth)
|
|
566
|
+
const at = rows.length
|
|
567
|
+
rows.push({ id, node, depth, key, parent, expandable, expanded, posInSet, setSize, isMore: false })
|
|
568
|
+
if (!expanded)
|
|
569
|
+
return
|
|
570
|
+
|
|
571
|
+
const children = node.children ?? []
|
|
572
|
+
const more = node.more ?? 0
|
|
573
|
+
const indexed = isIndexed(node)
|
|
574
|
+
const size = children.length + (more > 0 ? 1 : 0)
|
|
575
|
+
for (let index = 0; index < children.length; index++) {
|
|
576
|
+
const child = children[index]
|
|
577
|
+
visit(child, child.id ?? `${id}.${index}`, depth + 1, at, child.key ?? (indexed ? index : (child.label ?? index)), index + 1, size)
|
|
578
|
+
}
|
|
579
|
+
if (more > 0)
|
|
580
|
+
rows.push({ id: `${id}.+`, node, depth: depth + 1, key: '', parent: at, expandable: false, expanded: false, posInSet: size, setSize: size, isMore: true })
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
this.roots.forEach((root, index) => visit(root, root.id ?? String(index), 0, -1, root.key ?? index, index + 1, this.roots.length))
|
|
584
|
+
this.rows = rows
|
|
585
|
+
this.index = null
|
|
586
|
+
this.version++
|
|
587
|
+
}
|
|
588
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { TreeNode, TreeOpenKind, TreePath } from './core'
|
|
2
|
+
import type { TinkerValue } from './tinker'
|
|
3
|
+
|
|
4
|
+
export * from './core'
|
|
5
|
+
export { default as JsonTree } from './JsonTree.stx'
|
|
6
|
+
export * from './tinker'
|
|
7
|
+
export * from './view'
|
|
8
|
+
|
|
9
|
+
export interface JsonTreeProps {
|
|
10
|
+
/** The value, described as tree nodes. Bind it: `:value="node()"`. */
|
|
11
|
+
value?: TreeNode | TreeNode[] | null
|
|
12
|
+
/** A tinker result, adapted to tree nodes on the client. Bind it: `:tinker="result()"`. */
|
|
13
|
+
tinker?: TinkerValue | null
|
|
14
|
+
/** Containers nearer the root than this start expanded. Default 1. */
|
|
15
|
+
depth?: number
|
|
16
|
+
/** Pixels per row. Default 22. */
|
|
17
|
+
rowHeight?: number
|
|
18
|
+
/** A CSS length. Default `20rem`. */
|
|
19
|
+
height?: string
|
|
20
|
+
/** The tree's accessible name. Default `Value`. */
|
|
21
|
+
label?: string
|
|
22
|
+
/** Says where a node can be opened, overriding the node's own `openable`. Bound: `:openable="fn"`. */
|
|
23
|
+
openable?: (node: TreeNode, path: TreePath) => TreeOpenKind | TreeOpenKind[] | null | undefined
|
|
24
|
+
/** The host shows its own context menu, so the browser's is suppressed. */
|
|
25
|
+
contextMenu?: boolean
|
|
26
|
+
id?: string
|
|
27
|
+
className?: string
|
|
28
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The value inspector without its stx template.
|
|
3
|
+
*
|
|
4
|
+
* `@stacksjs/components/json-tree`. The model (`./core`, no DOM), the adapter
|
|
5
|
+
* for tinker results (`./tinker`) and the view that draws a model into any
|
|
6
|
+
* element.
|
|
7
|
+
*/
|
|
8
|
+
export * from './core'
|
|
9
|
+
export * from './tinker'
|
|
10
|
+
export * from './view'
|