@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,317 @@
|
|
|
1
|
+
<script server>
|
|
2
|
+
/*
|
|
3
|
+
* Either prop can arrive here as an object (a value known on the server) or
|
|
4
|
+
* not at all (a bound one, which the client script reads live).
|
|
5
|
+
*/
|
|
6
|
+
export const value = $props.value && typeof $props.value === 'object' ? $props.value : null
|
|
7
|
+
export const tinker = $props.tinker && typeof $props.tinker === 'object' ? $props.tinker : null
|
|
8
|
+
export const depth = $num($props.depth, 1)
|
|
9
|
+
export const rowHeight = $num($props.rowHeight) || 22
|
|
10
|
+
export const contextMenu = $bool($props.contextMenu)
|
|
11
|
+
/* A CSS length. `100%` fills a sized parent, which is what a pane wants. */
|
|
12
|
+
export const height = $props.height || '20rem'
|
|
13
|
+
export const label = $props.label || 'Value'
|
|
14
|
+
export const className = $props.className || ''
|
|
15
|
+
export const treeId = $props.id || `json-tree-${$uid}`
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
<script client>
|
|
19
|
+
/*
|
|
20
|
+
* <JsonTree>: an expandable inspector for a described value.
|
|
21
|
+
*
|
|
22
|
+
* This script is the wiring: props in, events out. Which rows are visible is
|
|
23
|
+
* in ./core, how a tinker result becomes nodes is in ./tinker, and how the
|
|
24
|
+
* rows on screen are drawn is in ./view.
|
|
25
|
+
*/
|
|
26
|
+
import { fromTinkerValue } from './tinker'
|
|
27
|
+
import { JsonTreeView } from './view'
|
|
28
|
+
|
|
29
|
+
const emit = defineEmits(['select', 'toggle', 'copy', 'open', 'load-more', 'context'])
|
|
30
|
+
|
|
31
|
+
function asBoolean(value) {
|
|
32
|
+
if (value == null) return false
|
|
33
|
+
if (typeof value === 'string') return !['false', '0', 'off', 'no'].includes(value.trim().toLowerCase())
|
|
34
|
+
return Boolean(value)
|
|
35
|
+
}
|
|
36
|
+
// A described value is an object or a list of them. As an attribute it is JSON.
|
|
37
|
+
function asDescribed(value) {
|
|
38
|
+
if (value && typeof value === 'object') return value
|
|
39
|
+
if (typeof value === 'string' && (value.charAt(0) === '{' || value.charAt(0) === '[')) {
|
|
40
|
+
try { return JSON.parse(value) }
|
|
41
|
+
catch (e) { return null }
|
|
42
|
+
}
|
|
43
|
+
return null
|
|
44
|
+
}
|
|
45
|
+
function asDepth(value) {
|
|
46
|
+
const number = Number(value)
|
|
47
|
+
return value == null || value === '' || Number.isNaN(number) ? 1 : number
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const liveValue = useReactiveProp('value', {{ value }}, { parse: asDescribed })
|
|
51
|
+
const liveTinker = useReactiveProp('tinker', {{ tinker }}, { parse: asDescribed })
|
|
52
|
+
const liveDepth = useReactiveProp('depth', {{ depth }}, { parse: asDepth })
|
|
53
|
+
const liveContextMenu = useReactiveProp('contextMenu', {{ contextMenu }}, { parse: asBoolean })
|
|
54
|
+
// A function can only arrive through a bound attribute, and is read as it is.
|
|
55
|
+
const liveOpenable = useReactiveProp('openable', null, { parse: v => v })
|
|
56
|
+
|
|
57
|
+
// The nodes to show: the described value, or the tinker result adapted.
|
|
58
|
+
function described() {
|
|
59
|
+
const nodes = asDescribed(liveValue())
|
|
60
|
+
if (nodes) return nodes
|
|
61
|
+
const result = asDescribed(liveTinker())
|
|
62
|
+
return result ? fromTinkerValue(result) : null
|
|
63
|
+
}
|
|
64
|
+
function asFunction(value) { return typeof value === 'function' ? value : null }
|
|
65
|
+
|
|
66
|
+
const rootRef = useRef('json-tree')
|
|
67
|
+
let tree = null
|
|
68
|
+
|
|
69
|
+
onMount(() => {
|
|
70
|
+
const root = rootRef()
|
|
71
|
+
if (!root) return
|
|
72
|
+
|
|
73
|
+
tree = new JsonTreeView(root, {
|
|
74
|
+
value: described(),
|
|
75
|
+
depth: asDepth(liveDepth()),
|
|
76
|
+
rowHeight: {{ rowHeight }},
|
|
77
|
+
openable: asFunction(liveOpenable()),
|
|
78
|
+
contextMenu: asBoolean(liveContextMenu()),
|
|
79
|
+
}, {
|
|
80
|
+
onSelect(detail) { emit('select', detail) },
|
|
81
|
+
onToggle(detail) { emit('toggle', detail) },
|
|
82
|
+
onCopy(detail) { emit('copy', detail) },
|
|
83
|
+
onOpen(detail) { emit('open', detail) },
|
|
84
|
+
onLoadMore(detail) { emit('load-more', detail) },
|
|
85
|
+
onContext(detail) { emit('context', detail) },
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
// The first run of each finds what the view was just built with. Setting it
|
|
89
|
+
// again would throw away nothing yet, but would build the rows twice.
|
|
90
|
+
let valueSeen = false
|
|
91
|
+
effect(() => {
|
|
92
|
+
const next = described()
|
|
93
|
+
if (valueSeen) tree.setValue(next)
|
|
94
|
+
valueSeen = true
|
|
95
|
+
})
|
|
96
|
+
let depthSeen = false
|
|
97
|
+
effect(() => {
|
|
98
|
+
const next = asDepth(liveDepth())
|
|
99
|
+
if (depthSeen) tree.setDepth(next)
|
|
100
|
+
depthSeen = true
|
|
101
|
+
})
|
|
102
|
+
effect(() => tree.setOpenable(asFunction(liveOpenable())))
|
|
103
|
+
effect(() => tree.setContextMenu(asBoolean(liveContextMenu())))
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
onDestroy(() => {
|
|
107
|
+
if (tree) tree.destroy()
|
|
108
|
+
tree = null
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
defineExpose({
|
|
112
|
+
expandAll: (id) => { if (tree) tree.expandAll(id) },
|
|
113
|
+
collapseAll: () => { if (tree) tree.collapseAll() },
|
|
114
|
+
expand: id => (tree ? tree.expand(id) : false),
|
|
115
|
+
collapse: id => (tree ? tree.collapse(id) : false),
|
|
116
|
+
reveal: id => (tree ? tree.reveal(id) : false),
|
|
117
|
+
appendChildren: (id, nodes, more) => (tree ? tree.appendChildren(id, nodes, more) : false),
|
|
118
|
+
setValue: (node) => { if (tree) tree.setValue(node) },
|
|
119
|
+
setTinker: (result) => { if (tree) tree.setValue(result ? fromTinkerValue(result) : null) },
|
|
120
|
+
getSelected: () => (tree ? tree.getSelected() : null),
|
|
121
|
+
select: id => (tree ? tree.select(id) : false),
|
|
122
|
+
focus: () => { if (tree) tree.focus() },
|
|
123
|
+
tree: () => tree,
|
|
124
|
+
})
|
|
125
|
+
</script>
|
|
126
|
+
|
|
127
|
+
<div
|
|
128
|
+
ref="json-tree"
|
|
129
|
+
id="{{ treeId }}"
|
|
130
|
+
data-stx-json-tree
|
|
131
|
+
class="stx-jt {{ className }}"
|
|
132
|
+
style="height: {{ height }}"
|
|
133
|
+
>
|
|
134
|
+
{{-- The tree is the thing with focus. Its rows are drawn by the view, and
|
|
135
|
+
the two wrappers between them carry no meaning of their own. --}}
|
|
136
|
+
<div data-jt="scroller" role="tree" tabindex="0" aria-label="{{ label }}" class="stx-jt-scroller">
|
|
137
|
+
<div data-jt="content" role="none" class="stx-jt-content">
|
|
138
|
+
<div data-jt="rows" role="none" class="stx-jt-rows"></div>
|
|
139
|
+
</div>
|
|
140
|
+
</div>
|
|
141
|
+
<div data-jt="status" role="status" aria-live="polite" class="stx-jt-status"></div>
|
|
142
|
+
</div>
|
|
143
|
+
|
|
144
|
+
<style>
|
|
145
|
+
/*
|
|
146
|
+
* Everything the tree is drawn with is a custom property. The surface colours
|
|
147
|
+
* fall back to the library's role tokens and the value colours to the syntax
|
|
148
|
+
* tokens <CodeBlock> and <CodeEditor> use, so a value reads like the code that
|
|
149
|
+
* produced it. Each has a literal behind it, because the stylesheet that
|
|
150
|
+
* defines the syntax tokens ships with those components and may not be here.
|
|
151
|
+
*/
|
|
152
|
+
:where([data-stx-json-tree]) {
|
|
153
|
+
--stx-tree-font: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, 'Liberation Mono', monospace;
|
|
154
|
+
--stx-tree-font-size: 12.5px;
|
|
155
|
+
--stx-tree-row-height: 22px;
|
|
156
|
+
--stx-tree-indent: 14px;
|
|
157
|
+
--stx-tree-bg: var(--stx-panel, #ffffff);
|
|
158
|
+
--stx-tree-fg: var(--stx-fg, #171717);
|
|
159
|
+
--stx-tree-muted: var(--stx-fg-soft, #737373);
|
|
160
|
+
--stx-tree-hover: color-mix(in srgb, var(--stx-fg, #171717) 6%, transparent);
|
|
161
|
+
--stx-tree-active: color-mix(in srgb, var(--stx-accent, #2563eb) 16%, transparent);
|
|
162
|
+
--stx-tree-focus: var(--stx-accent, #2563eb);
|
|
163
|
+
--stx-tree-line: var(--stx-line, #e5e5e5);
|
|
164
|
+
--stx-tree-key: var(--stx-syntax-attribute, #0550ae);
|
|
165
|
+
--stx-tree-string: var(--stx-syntax-string, #0a3069);
|
|
166
|
+
--stx-tree-number: var(--stx-syntax-numeric, #0550ae);
|
|
167
|
+
--stx-tree-keyword: var(--stx-syntax-keyword, #cf222e);
|
|
168
|
+
--stx-tree-type: var(--stx-syntax-type, #953800);
|
|
169
|
+
--stx-tree-function: var(--stx-syntax-function, #8250df);
|
|
170
|
+
--stx-tree-comment: var(--stx-syntax-comment, #6e7781);
|
|
171
|
+
--stx-tree-error: var(--stx-danger, #dc2626);
|
|
172
|
+
position: relative;
|
|
173
|
+
display: flex;
|
|
174
|
+
flex-direction: column;
|
|
175
|
+
min-width: 0;
|
|
176
|
+
background: var(--stx-tree-bg);
|
|
177
|
+
color: var(--stx-tree-fg);
|
|
178
|
+
}
|
|
179
|
+
/* Dark follows the `.dark` class, as the role tokens do: a surface from one
|
|
180
|
+
and text from the other would not be readable. */
|
|
181
|
+
:where(.dark) :where([data-stx-json-tree]) {
|
|
182
|
+
--stx-tree-bg: var(--stx-panel, #262626);
|
|
183
|
+
--stx-tree-fg: var(--stx-fg, #f5f5f5);
|
|
184
|
+
--stx-tree-muted: var(--stx-fg-soft, #a3a3a3);
|
|
185
|
+
--stx-tree-hover: color-mix(in srgb, var(--stx-fg, #f5f5f5) 8%, transparent);
|
|
186
|
+
--stx-tree-line: var(--stx-line, #404040);
|
|
187
|
+
--stx-tree-key: var(--stx-syntax-attribute, #79c0ff);
|
|
188
|
+
--stx-tree-string: var(--stx-syntax-string, #a5d6ff);
|
|
189
|
+
--stx-tree-number: var(--stx-syntax-numeric, #79c0ff);
|
|
190
|
+
--stx-tree-keyword: var(--stx-syntax-keyword, #ff7b72);
|
|
191
|
+
--stx-tree-type: var(--stx-syntax-type, #ffa657);
|
|
192
|
+
--stx-tree-function: var(--stx-syntax-function, #d2a8ff);
|
|
193
|
+
--stx-tree-comment: var(--stx-syntax-comment, #8b949e);
|
|
194
|
+
--stx-tree-error: var(--stx-danger, #f87171);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
[data-stx-json-tree] .stx-jt-scroller {
|
|
198
|
+
position: relative;
|
|
199
|
+
flex: 1 1 auto;
|
|
200
|
+
min-height: 0;
|
|
201
|
+
overflow: auto;
|
|
202
|
+
outline: none;
|
|
203
|
+
font-family: var(--stx-tree-font);
|
|
204
|
+
font-size: var(--stx-tree-font-size);
|
|
205
|
+
line-height: var(--stx-tree-row-height);
|
|
206
|
+
font-variant-ligatures: none;
|
|
207
|
+
}
|
|
208
|
+
[data-stx-json-tree] .stx-jt-scroller:focus-visible {
|
|
209
|
+
box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--stx-tree-focus) 55%, transparent);
|
|
210
|
+
}
|
|
211
|
+
[data-stx-json-tree] .stx-jt-content { position: relative; min-width: 100%; }
|
|
212
|
+
[data-stx-json-tree] .stx-jt-rows { position: absolute; top: 0; left: 0; right: 0; }
|
|
213
|
+
|
|
214
|
+
/* Every row is the same height and is placed by a transform, so which rows
|
|
215
|
+
are on screen is arithmetic and moving one costs no layout. */
|
|
216
|
+
[data-stx-json-tree] .stx-jt-row {
|
|
217
|
+
position: absolute;
|
|
218
|
+
top: 0;
|
|
219
|
+
left: 0;
|
|
220
|
+
right: 0;
|
|
221
|
+
box-sizing: border-box;
|
|
222
|
+
display: flex;
|
|
223
|
+
align-items: center;
|
|
224
|
+
height: var(--stx-tree-row-height);
|
|
225
|
+
padding-left: calc(6px + var(--stx-tree-indent) * var(--stx-tree-depth, 0));
|
|
226
|
+
padding-right: 6px;
|
|
227
|
+
white-space: nowrap;
|
|
228
|
+
cursor: default;
|
|
229
|
+
contain: layout style;
|
|
230
|
+
}
|
|
231
|
+
[data-stx-json-tree] .stx-jt-row:hover { background: var(--stx-tree-hover); }
|
|
232
|
+
[data-stx-json-tree] .stx-jt-row[data-active] { background: var(--stx-tree-active); }
|
|
233
|
+
[data-stx-json-tree] .stx-jt-scroller:focus-visible .stx-jt-row[data-active] {
|
|
234
|
+
box-shadow: inset 2px 0 0 var(--stx-tree-focus);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
[data-stx-json-tree] .stx-jt-twisty {
|
|
238
|
+
flex: 0 0 var(--stx-tree-indent);
|
|
239
|
+
color: var(--stx-tree-muted);
|
|
240
|
+
text-align: center;
|
|
241
|
+
user-select: none;
|
|
242
|
+
-webkit-user-select: none;
|
|
243
|
+
}
|
|
244
|
+
[data-stx-json-tree] .stx-jt-label { flex: 0 0 auto; color: var(--stx-tree-key); }
|
|
245
|
+
[data-stx-json-tree] .stx-jt-separator { flex: 0 0 auto; color: var(--stx-tree-muted); white-space: pre; }
|
|
246
|
+
[data-stx-json-tree] .stx-jt-preview {
|
|
247
|
+
flex: 1 1 auto;
|
|
248
|
+
min-width: 0;
|
|
249
|
+
overflow: hidden;
|
|
250
|
+
text-overflow: ellipsis;
|
|
251
|
+
color: var(--stx-tree-muted);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="string"],
|
|
255
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="regexp"],
|
|
256
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="url"],
|
|
257
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="date"] { color: var(--stx-tree-string); }
|
|
258
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="number"],
|
|
259
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="bigint"],
|
|
260
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="bytes"] { color: var(--stx-tree-number); }
|
|
261
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="boolean"],
|
|
262
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="null"],
|
|
263
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="undefined"] { color: var(--stx-tree-keyword); }
|
|
264
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="function"],
|
|
265
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="symbol"],
|
|
266
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="promise"] { color: var(--stx-tree-function); }
|
|
267
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="model"],
|
|
268
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="html"],
|
|
269
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="relation"] { color: var(--stx-tree-type); }
|
|
270
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="error"] { color: var(--stx-tree-error); }
|
|
271
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="ref"],
|
|
272
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="cut"],
|
|
273
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="more"],
|
|
274
|
+
[data-stx-json-tree] .stx-jt-preview[data-kind="hidden"] { color: var(--stx-tree-comment); font-style: italic; }
|
|
275
|
+
[data-stx-json-tree] .stx-jt-row[data-kind="ref"],
|
|
276
|
+
[data-stx-json-tree] .stx-jt-row[data-kind="more"] { cursor: pointer; }
|
|
277
|
+
|
|
278
|
+
/* The row's buttons: there for a pointer over the row, and for the row the
|
|
279
|
+
cursor is on. The keyboard reaches the same things through the tree's keys. */
|
|
280
|
+
[data-stx-json-tree] .stx-jt-actions {
|
|
281
|
+
flex: 0 0 auto;
|
|
282
|
+
display: none;
|
|
283
|
+
gap: 4px;
|
|
284
|
+
margin-left: 8px;
|
|
285
|
+
}
|
|
286
|
+
[data-stx-json-tree] .stx-jt-row:hover .stx-jt-actions,
|
|
287
|
+
[data-stx-json-tree] .stx-jt-row[data-active] .stx-jt-actions { display: inline-flex; }
|
|
288
|
+
[data-stx-json-tree] .stx-jt-action {
|
|
289
|
+
appearance: none;
|
|
290
|
+
box-sizing: border-box;
|
|
291
|
+
height: calc(var(--stx-tree-row-height) - 4px);
|
|
292
|
+
margin: 0;
|
|
293
|
+
padding: 0 6px;
|
|
294
|
+
border: 1px solid var(--stx-tree-line);
|
|
295
|
+
border-radius: 4px;
|
|
296
|
+
background: var(--stx-tree-bg);
|
|
297
|
+
color: var(--stx-tree-muted);
|
|
298
|
+
font: inherit;
|
|
299
|
+
font-size: 11px;
|
|
300
|
+
line-height: 1;
|
|
301
|
+
cursor: pointer;
|
|
302
|
+
}
|
|
303
|
+
[data-stx-json-tree] .stx-jt-action:hover { color: var(--stx-tree-fg); border-color: var(--stx-tree-focus); }
|
|
304
|
+
[data-stx-json-tree] .stx-jt-action[data-jt-action="open"] { color: var(--stx-tree-focus); }
|
|
305
|
+
|
|
306
|
+
[data-stx-json-tree] .stx-jt-status {
|
|
307
|
+
position: absolute;
|
|
308
|
+
width: 1px;
|
|
309
|
+
height: 1px;
|
|
310
|
+
margin: -1px;
|
|
311
|
+
padding: 0;
|
|
312
|
+
overflow: hidden;
|
|
313
|
+
clip: rect(0, 0, 0, 0);
|
|
314
|
+
white-space: nowrap;
|
|
315
|
+
border: 0;
|
|
316
|
+
}
|
|
317
|
+
</style>
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# JsonTree
|
|
2
|
+
|
|
3
|
+
An expandable inspector for a described value: a result, a parsed document, an
|
|
4
|
+
object graph. Keyboard navigation, copy as JSON, copy as a path, and a way to
|
|
5
|
+
say that a node can also be opened as a table, a graph or a preview. No
|
|
6
|
+
third-party tree underneath.
|
|
7
|
+
|
|
8
|
+
```html
|
|
9
|
+
<script client>
|
|
10
|
+
const result = state(null) // a TinkerValue, as @stacksjs/tinker sends it
|
|
11
|
+
|
|
12
|
+
function open(detail) {
|
|
13
|
+
// detail.as is 'table', 'graph' or 'preview'; detail.node is what to show.
|
|
14
|
+
}
|
|
15
|
+
</script>
|
|
16
|
+
|
|
17
|
+
<JsonTree
|
|
18
|
+
label="Result"
|
|
19
|
+
height="100%"
|
|
20
|
+
depth="2"
|
|
21
|
+
:tinker="result()"
|
|
22
|
+
@open="open($event)"
|
|
23
|
+
/>
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
A component's event hands its payload to the handler as `$event`, so `open`
|
|
27
|
+
above receives the detail object itself.
|
|
28
|
+
|
|
29
|
+
## How it works
|
|
30
|
+
|
|
31
|
+
The tree does not look at values. It is given a description of one: nodes that
|
|
32
|
+
each say what kind of thing they are, what the parent calls them and how they
|
|
33
|
+
read on one line. So it is not tied to a serializer, and a value that cannot
|
|
34
|
+
leave its process (a class instance, a cycle, a model) is shown as well as one
|
|
35
|
+
that can.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
interface TreeNode {
|
|
39
|
+
kind: string // 'object', 'array', 'string', 'model', 'error', ...
|
|
40
|
+
label?: string // what the parent calls it
|
|
41
|
+
preview?: string // the value on one line
|
|
42
|
+
value?: unknown // a scalar's value, for copy
|
|
43
|
+
children?: TreeNode[]
|
|
44
|
+
more?: number // children that exist but were not sent
|
|
45
|
+
ref?: string // the id of the node a cycle points back to
|
|
46
|
+
id?: string // needed only where a ref or the host points at it
|
|
47
|
+
key?: string | number // the path segment, when the label is not it
|
|
48
|
+
openable?: string | string[] // 'table' | 'graph' | 'preview' | your own
|
|
49
|
+
meta?: unknown // yours; never read
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Three layers of code, each usable without the ones above it:
|
|
54
|
+
|
|
55
|
+
| File | What it is |
|
|
56
|
+
|---|---|
|
|
57
|
+
| `core.ts` | The model: which rows are visible, what is expanded, what a key does, paths, JSON. Also `fromValue(value)`, which describes a plain JavaScript value. No DOM and no stx. |
|
|
58
|
+
| `tinker.ts` | `fromTinkerValue(value, label?, { idPrefix? })`: a `@stacksjs/tinker` result as tree nodes. A structural copy of its type, so there is no dependency on that package. |
|
|
59
|
+
| `view.ts` | The DOM: draws the rows on screen and no others, and turns keys and clicks into changes. |
|
|
60
|
+
| `JsonTree.stx` | The template, the styles, and the props and events. |
|
|
61
|
+
|
|
62
|
+
`@stacksjs/components/json-tree` exports the first three for a host that mounts
|
|
63
|
+
a tree itself.
|
|
64
|
+
|
|
65
|
+
Every row is the same height, so the rows on screen are arithmetic on the
|
|
66
|
+
scroll position. About thirty row elements exist whatever the tree holds, and
|
|
67
|
+
each row index always lands on the same element, so scrolling by one row
|
|
68
|
+
rewrites one element. The model only walks containers that are expanded.
|
|
69
|
+
|
|
70
|
+
## Props
|
|
71
|
+
|
|
72
|
+
| Prop | Default | |
|
|
73
|
+
|---|---|---|
|
|
74
|
+
| `value` | | A `TreeNode`, or a list of them shown as several roots. Bind it: `:value="node()"`. |
|
|
75
|
+
| `tinker` | | A tinker result, adapted on the client. Bind it: `:tinker="result()"`. Used when there is no `value`. |
|
|
76
|
+
| `depth` | `1` | Containers nearer the root than this start expanded. `1` opens the root and leaves its children closed; `0` closes everything. |
|
|
77
|
+
| `rowHeight` | `22` | Pixels. |
|
|
78
|
+
| `height` | `'20rem'` | Any CSS length. `100%` fills a sized parent. |
|
|
79
|
+
| `label` | `'Value'` | The tree's accessible name. |
|
|
80
|
+
| `openable` | | A function, bound with `:openable`, `(node, path) => string \| string[] \| null`. Its answer replaces the node's own `openable`; `undefined` falls back to it. |
|
|
81
|
+
| `contextMenu` | `false` | The host shows its own menu on `@context`, so the browser's is kept away. |
|
|
82
|
+
| `id`, `className` | | |
|
|
83
|
+
|
|
84
|
+
A bound object is also written onto the component's element as JSON by stx,
|
|
85
|
+
whatever the component does with it. For the 5,001-row tree below that is 250 KB
|
|
86
|
+
of attribute, a fraction of a millisecond to produce. For a much larger value, give the tree
|
|
87
|
+
nothing and call `setValue(node)` or `setTinker(result)` on it instead.
|
|
88
|
+
|
|
89
|
+
## Events
|
|
90
|
+
|
|
91
|
+
| Event | Payload |
|
|
92
|
+
|---|---|
|
|
93
|
+
| `@select` | `{ id, node, path, pathText }`. The cursor moved. `path` is `['users', 0, 'name']`, `pathText` is `$.users[0].name`. |
|
|
94
|
+
| `@toggle` | The same, with `expanded`. |
|
|
95
|
+
| `@copy` | `{ what: 'value' \| 'path', text, path, node }`. The text is also put on the clipboard where the browser allows it. |
|
|
96
|
+
| `@open` | The select payload, with `as`: which of the node's `openable` kinds was chosen. |
|
|
97
|
+
| `@load-more` | The select payload for a container whose `more` row was activated. Answer with `appendChildren(id, nodes, more)`. |
|
|
98
|
+
| `@context` | The select payload, with `x` and `y`. A context click on a row. |
|
|
99
|
+
|
|
100
|
+
## Methods
|
|
101
|
+
|
|
102
|
+
From `componentApi(target)` (`@stacksjs/components/component-api`), where
|
|
103
|
+
`target` is the component's element, its id or a selector: `setValue(node)`, `setTinker(result)`,
|
|
104
|
+
`expandAll(id?)`, `collapseAll()`, `expand(id)`, `collapse(id)`, `reveal(id)`
|
|
105
|
+
(expands whatever hides a node, moves the cursor to it and scrolls to it),
|
|
106
|
+
`appendChildren(id, nodes, more?)`, `select(id)`, `getSelected()`, `focus()`,
|
|
107
|
+
and `tree()` for the view itself, whose `model` is the `TreeModel`.
|
|
108
|
+
|
|
109
|
+
A row's id is the node's `id` when it has one and its position otherwise
|
|
110
|
+
(`0.2.1`: the second child of the third child of the first root).
|
|
111
|
+
|
|
112
|
+
## Keys
|
|
113
|
+
|
|
114
|
+
| Key | |
|
|
115
|
+
|---|---|
|
|
116
|
+
| Up, Down | The row above or below. |
|
|
117
|
+
| Right | Open a closed container; on an open one, go to its first child. |
|
|
118
|
+
| Left | Close an open container; otherwise go to the parent. |
|
|
119
|
+
| Home, End, PageUp, PageDown | |
|
|
120
|
+
| `*` | Open every sibling of the row. |
|
|
121
|
+
| Enter, Space | Toggle a container, load a `more` row, follow a cycle to the node it points at. |
|
|
122
|
+
| Shift+Enter | Open the node as the first of its `openable` kinds. |
|
|
123
|
+
| Cmd/Ctrl+C | Copy the value as JSON. |
|
|
124
|
+
| Shift+Cmd/Ctrl+C | Copy the path. |
|
|
125
|
+
| Any character | Jump to the next row whose label starts with what was typed. |
|
|
126
|
+
|
|
127
|
+
Focus stays on the tree and the current row is named with
|
|
128
|
+
`aria-activedescendant`, because a row that scrolls away stops existing and
|
|
129
|
+
cannot hold focus. Rows are `treeitem`s with their level, position, set size
|
|
130
|
+
and expanded state. The buttons on a row are for a pointer; each has a key
|
|
131
|
+
above.
|
|
132
|
+
|
|
133
|
+
## Copying
|
|
134
|
+
|
|
135
|
+
Copy value writes the JSON of what the tree holds for that node. Children that
|
|
136
|
+
were never sent (`more`) are not there and nothing is put in their place, so the
|
|
137
|
+
text is always valid JSON of what is present. A cycle is the text
|
|
138
|
+
`"[Circular]"`, a bigint or a non-finite number is a string, and `undefined` at
|
|
139
|
+
the top is the word `undefined`.
|
|
140
|
+
|
|
141
|
+
## Tinker results
|
|
142
|
+
|
|
143
|
+
`fromTinkerValue` covers every tag. A model shows its attributes, its loaded
|
|
144
|
+
relations as values, and two groups: `[hidden]` names the attributes the model
|
|
145
|
+
does not serialize, and `[relations]` lists what it declares. An error shows
|
|
146
|
+
its message, its frames, its cause and its own fields. Each container keeps
|
|
147
|
+
the serializer's object id as its node id (`t12`), which is what a `ref` names,
|
|
148
|
+
so following a cycle lands on the right node. Two results in one tree both
|
|
149
|
+
count from one: give each its own `idPrefix`.
|
|
150
|
+
|
|
151
|
+
By default a list of objects or models is `openable` as a `table`, a model or an
|
|
152
|
+
object or map holding other containers as a `graph`, and markup as a `preview`.
|
|
153
|
+
|
|
154
|
+
## Theming
|
|
155
|
+
|
|
156
|
+
Everything is a CSS custom property. Surfaces fall back to the library's role
|
|
157
|
+
tokens, and value colours to the syntax tokens `<CodeBlock>` and `<CodeEditor>`
|
|
158
|
+
draw with, so a value reads like the code that produced it. Dark follows the
|
|
159
|
+
`.dark` class, as the role tokens do.
|
|
160
|
+
|
|
161
|
+
- The tree: `--stx-tree-bg`, `--stx-tree-fg`, `--stx-tree-muted`,
|
|
162
|
+
`--stx-tree-hover`, `--stx-tree-active`, `--stx-tree-focus`, `--stx-tree-line`.
|
|
163
|
+
- The values: `--stx-tree-key`, `--stx-tree-string`, `--stx-tree-number`,
|
|
164
|
+
`--stx-tree-keyword` (booleans, null, undefined), `--stx-tree-type` (models,
|
|
165
|
+
markup), `--stx-tree-function`, `--stx-tree-comment` (cycles, cuts, more),
|
|
166
|
+
`--stx-tree-error`.
|
|
167
|
+
- The type: `--stx-tree-font`, `--stx-tree-font-size`, `--stx-tree-indent`.
|
|
168
|
+
The row height is the `rowHeight` prop, because the view needs the number.
|
|
169
|
+
|
|
170
|
+
## Measured
|
|
171
|
+
|
|
172
|
+
Headless WebKit on a Mac, 1,000 records of five rows each (5,001 rows open),
|
|
173
|
+
a 400px viewport, layout forced after every step. WebKit's clock ticks in whole
|
|
174
|
+
milliseconds, so the means are the better numbers.
|
|
175
|
+
|
|
176
|
+
| | |
|
|
177
|
+
|---|---|
|
|
178
|
+
| Show the value (1,001 rows) | 2 to 3 ms |
|
|
179
|
+
| Expand all (5,001 rows) | 3 to 5 ms |
|
|
180
|
+
| An arrow key | mean 0.2 ms, p95 1 ms |
|
|
181
|
+
| A scroll repaint, 730px at a time | mean 1.5 ms, p95 2 ms |
|
|
182
|
+
| Row elements in the document | 32 |
|
|
183
|
+
|
|
184
|
+
`test/json-tree/json-tree-browser.test.ts` prints these on every run.
|
|
185
|
+
|
|
186
|
+
## Limits of this version
|
|
187
|
+
|
|
188
|
+
- **One line per value.** A long string is cut on its row at 400 characters;
|
|
189
|
+
copy gives all of it. There is no multi-line string view.
|
|
190
|
+
- **One cursor, no multi-select.**
|
|
191
|
+
- **No search.** Type-ahead finds labels among the rows that are visible.
|
|
192
|
+
- **Expand all walks everything under the row.** Fine at tens of thousands of
|
|
193
|
+
nodes; a host with millions should send less and use `more`.
|