@stacksjs/components 0.2.855 → 0.2.858
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,665 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A value inspector in the DOM.
|
|
3
|
+
*
|
|
4
|
+
* Draws the rows of a `TreeModel` that are on screen and no others: every row
|
|
5
|
+
* is the same height, so which rows those are is arithmetic on the scroll
|
|
6
|
+
* position, and a tree of fifty thousand lines costs what thirty do.
|
|
7
|
+
*
|
|
8
|
+
* Focus stays on the tree itself and the current row is named with
|
|
9
|
+
* `aria-activedescendant`. A virtualized row cannot hold focus, because it
|
|
10
|
+
* stops existing when it scrolls away.
|
|
11
|
+
*/
|
|
12
|
+
import type { TreeNode, TreeOpenKind, TreePath, TreeRow } from './core'
|
|
13
|
+
import { formatPath, nodeToJson, previewOf, TreeModel } from './core'
|
|
14
|
+
|
|
15
|
+
export interface JsonTreeOptions {
|
|
16
|
+
value?: TreeNode | TreeNode[] | null
|
|
17
|
+
/** Containers nearer the root than this start expanded. Default 1. */
|
|
18
|
+
depth?: number
|
|
19
|
+
/** Pixels. Default 22. */
|
|
20
|
+
rowHeight?: number
|
|
21
|
+
/** Rows drawn beyond each edge of what is visible. Default 6. */
|
|
22
|
+
overscan?: number
|
|
23
|
+
/** Says where a node can be opened, overriding the node's own `openable`. */
|
|
24
|
+
openable?: ((node: TreeNode, path: TreePath) => TreeOpenKind | TreeOpenKind[] | null | undefined) | null
|
|
25
|
+
/** Keep the browser's own context menu from opening, because the host shows one. */
|
|
26
|
+
contextMenu?: boolean
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface TreeSelectDetail {
|
|
30
|
+
id: string
|
|
31
|
+
node: TreeNode
|
|
32
|
+
path: TreePath
|
|
33
|
+
/** The path as text: `$.users[0].name`. */
|
|
34
|
+
pathText: string
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface TreeToggleDetail extends TreeSelectDetail {
|
|
38
|
+
expanded: boolean
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface TreeCopyDetail {
|
|
42
|
+
what: 'value' | 'path'
|
|
43
|
+
text: string
|
|
44
|
+
path: TreePath
|
|
45
|
+
node: TreeNode
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface TreeOpenDetail extends TreeSelectDetail {
|
|
49
|
+
as: TreeOpenKind
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface TreeContextDetail extends TreeSelectDetail {
|
|
53
|
+
x: number
|
|
54
|
+
y: number
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface JsonTreeCallbacks {
|
|
58
|
+
onSelect?: (detail: TreeSelectDetail) => void
|
|
59
|
+
onToggle?: (detail: TreeToggleDetail) => void
|
|
60
|
+
onCopy?: (detail: TreeCopyDetail) => void
|
|
61
|
+
onOpen?: (detail: TreeOpenDetail) => void
|
|
62
|
+
onLoadMore?: (detail: TreeSelectDetail) => void
|
|
63
|
+
onContext?: (detail: TreeContextDetail) => void
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** What a pooled row element remembers about the row it last drew. */
|
|
67
|
+
interface RowElement extends HTMLElement {
|
|
68
|
+
__jt?: { index: number, version: number, opens: string }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface RowParts {
|
|
72
|
+
twisty: HTMLElement
|
|
73
|
+
label: HTMLElement
|
|
74
|
+
separator: HTMLElement
|
|
75
|
+
preview: HTMLElement
|
|
76
|
+
actions: HTMLElement
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const TYPEAHEAD_RESET = 600
|
|
80
|
+
/** A row's text is one line; nothing longer than this is worth laying out. */
|
|
81
|
+
const TEXT_LIMIT = 400
|
|
82
|
+
const OPEN_LABELS: Record<string, string> = { table: 'Table', graph: 'Graph', preview: 'Preview' }
|
|
83
|
+
|
|
84
|
+
function clip(text: string): string {
|
|
85
|
+
return text.length > TEXT_LIMIT ? `${text.slice(0, TEXT_LIMIT)}…` : text
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function listOf(value: TreeOpenKind | TreeOpenKind[] | null | undefined): TreeOpenKind[] {
|
|
89
|
+
if (value == null || value === '')
|
|
90
|
+
return []
|
|
91
|
+
return Array.isArray(value) ? value : [value]
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export class JsonTreeView {
|
|
95
|
+
readonly model: TreeModel
|
|
96
|
+
|
|
97
|
+
private readonly scroller: HTMLElement
|
|
98
|
+
private readonly content: HTMLElement
|
|
99
|
+
private readonly rowsEl: HTMLElement
|
|
100
|
+
private readonly status: HTMLElement | null
|
|
101
|
+
private readonly prefix: string
|
|
102
|
+
private rowHeight: number
|
|
103
|
+
private overscan: number
|
|
104
|
+
private openable: JsonTreeOptions['openable']
|
|
105
|
+
private contextMenu: boolean
|
|
106
|
+
private pool: RowElement[] = []
|
|
107
|
+
private parts = new WeakMap<HTMLElement, RowParts>()
|
|
108
|
+
private active = -1
|
|
109
|
+
private activeId = ''
|
|
110
|
+
/** What has been typed towards a label, and when the last character arrived. */
|
|
111
|
+
private typed = ''
|
|
112
|
+
private typedAt = 0
|
|
113
|
+
private observer: ResizeObserver | null = null
|
|
114
|
+
private readonly disposers: Array<() => void> = []
|
|
115
|
+
|
|
116
|
+
constructor(private readonly root: HTMLElement, options: JsonTreeOptions = {}, private readonly callbacks: JsonTreeCallbacks = {}) {
|
|
117
|
+
const part = (name: string): HTMLElement | null => root.querySelector<HTMLElement>(`[data-jt="${name}"]`)
|
|
118
|
+
this.scroller = part('scroller') ?? root
|
|
119
|
+
this.content = part('content') ?? this.scroller
|
|
120
|
+
this.rowsEl = part('rows') ?? this.content
|
|
121
|
+
this.status = part('status')
|
|
122
|
+
this.prefix = `${root.id || `json-tree-${Math.random().toString(36).slice(2, 8)}`}-row-`
|
|
123
|
+
this.rowHeight = options.rowHeight && options.rowHeight > 0 ? options.rowHeight : 22
|
|
124
|
+
this.overscan = options.overscan ?? 6
|
|
125
|
+
this.openable = options.openable ?? null
|
|
126
|
+
this.contextMenu = Boolean(options.contextMenu)
|
|
127
|
+
this.model = new TreeModel(options.value, { depth: options.depth ?? 1 })
|
|
128
|
+
|
|
129
|
+
// Everything drawn in here is data. A value that happens to contain `{{ }}`
|
|
130
|
+
// is text to show, never an expression for the stx runtime to evaluate.
|
|
131
|
+
;(this.rowsEl as unknown as { __stx_inert_content: boolean }).__stx_inert_content = true
|
|
132
|
+
root.style.setProperty('--stx-tree-row-height', `${this.rowHeight}px`)
|
|
133
|
+
|
|
134
|
+
const listen = <K extends keyof HTMLElementEventMap>(target: HTMLElement, type: K, handler: (event: HTMLElementEventMap[K]) => void, options?: AddEventListenerOptions): void => {
|
|
135
|
+
target.addEventListener(type, handler, options)
|
|
136
|
+
this.disposers.push(() => target.removeEventListener(type, handler, options))
|
|
137
|
+
}
|
|
138
|
+
listen(this.scroller, 'scroll', () => this.render(), { passive: true })
|
|
139
|
+
listen(this.scroller, 'keydown', event => this.onKeyDown(event))
|
|
140
|
+
listen(this.scroller, 'focus', () => {
|
|
141
|
+
if (this.active === -1 && this.model.rows.length > 0)
|
|
142
|
+
this.setActive(0, false)
|
|
143
|
+
})
|
|
144
|
+
listen(this.rowsEl, 'mousedown', (event) => {
|
|
145
|
+
// A click on a row must not start a text selection or move focus off the tree.
|
|
146
|
+
if ((event.target as HTMLElement).closest('[data-jt-action]'))
|
|
147
|
+
event.preventDefault()
|
|
148
|
+
})
|
|
149
|
+
listen(this.rowsEl, 'click', event => this.onClick(event))
|
|
150
|
+
listen(this.rowsEl, 'dblclick', event => this.onDoubleClick(event))
|
|
151
|
+
listen(this.rowsEl, 'contextmenu', event => this.onContextMenu(event))
|
|
152
|
+
|
|
153
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
154
|
+
this.observer = new ResizeObserver(() => this.render())
|
|
155
|
+
this.observer.observe(this.scroller)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
this.render()
|
|
159
|
+
root.setAttribute('data-jt-ready', '')
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Show another value. The cursor and what was expanded are forgotten. */
|
|
163
|
+
setValue(value: TreeNode | TreeNode[] | null | undefined): void {
|
|
164
|
+
this.model.setValue(value)
|
|
165
|
+
this.active = -1
|
|
166
|
+
this.activeId = ''
|
|
167
|
+
this.scroller.scrollTop = 0
|
|
168
|
+
this.render()
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
setDepth(depth: number): void {
|
|
172
|
+
this.model.setDepth(depth)
|
|
173
|
+
this.afterChange()
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
setOpenable(openable: JsonTreeOptions['openable']): void {
|
|
177
|
+
this.openable = openable ?? null
|
|
178
|
+
this.model.version++
|
|
179
|
+
this.render()
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
setContextMenu(on: boolean): void {
|
|
183
|
+
this.contextMenu = on
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
expandAll(id?: string): void {
|
|
187
|
+
this.model.expandAll(id)
|
|
188
|
+
this.afterChange()
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
collapseAll(): void {
|
|
192
|
+
this.model.collapseAll()
|
|
193
|
+
this.afterChange()
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
expand(id: string): boolean {
|
|
197
|
+
const changed = this.model.expand(id)
|
|
198
|
+
if (changed)
|
|
199
|
+
this.afterChange()
|
|
200
|
+
return changed
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
collapse(id: string): boolean {
|
|
204
|
+
const changed = this.model.collapse(id)
|
|
205
|
+
if (changed)
|
|
206
|
+
this.afterChange()
|
|
207
|
+
return changed
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Expand whatever hides a row, move the cursor to it and bring it on screen. */
|
|
211
|
+
reveal(id: string): boolean {
|
|
212
|
+
const index = this.model.reveal(id)
|
|
213
|
+
if (index === -1) {
|
|
214
|
+
this.afterChange()
|
|
215
|
+
return false
|
|
216
|
+
}
|
|
217
|
+
this.active = index
|
|
218
|
+
this.activeId = id
|
|
219
|
+
this.scrollToRow(index)
|
|
220
|
+
this.render()
|
|
221
|
+
this.emitSelect()
|
|
222
|
+
return true
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
appendChildren(id: string, nodes: TreeNode[], more = 0): boolean {
|
|
226
|
+
const done = this.model.appendChildren(id, nodes, more)
|
|
227
|
+
if (done)
|
|
228
|
+
this.afterChange()
|
|
229
|
+
return done
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** The row the cursor is on, or null. */
|
|
233
|
+
getSelected(): TreeSelectDetail | null {
|
|
234
|
+
return this.active === -1 ? null : this.detail(this.active)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Move the cursor to a visible row by id. */
|
|
238
|
+
select(id: string): boolean {
|
|
239
|
+
const index = this.model.indexOf(id)
|
|
240
|
+
if (index === -1)
|
|
241
|
+
return false
|
|
242
|
+
this.setActive(index, true)
|
|
243
|
+
return true
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
focus(): void {
|
|
247
|
+
this.scroller.focus()
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** How many row elements exist. A handful more than fit on screen, whatever the tree holds. */
|
|
251
|
+
get drawn(): number {
|
|
252
|
+
return this.pool.filter(row => row.style.display !== 'none').length
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
destroy(): void {
|
|
256
|
+
this.observer?.disconnect()
|
|
257
|
+
for (const dispose of this.disposers) dispose()
|
|
258
|
+
this.rowsEl.textContent = ''
|
|
259
|
+
this.pool = []
|
|
260
|
+
this.root.removeAttribute('data-jt-ready')
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** The rows changed under the cursor: find it again by id, then redraw. */
|
|
264
|
+
private afterChange(): void {
|
|
265
|
+
if (this.activeId) {
|
|
266
|
+
const index = this.model.indexOf(this.activeId)
|
|
267
|
+
this.active = index
|
|
268
|
+
if (index === -1)
|
|
269
|
+
this.activeId = ''
|
|
270
|
+
}
|
|
271
|
+
this.render()
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private detail(index: number): TreeSelectDetail {
|
|
275
|
+
const row = this.model.rows[index]
|
|
276
|
+
const path = this.model.pathOf(index)
|
|
277
|
+
return { id: row.id, node: row.node, path, pathText: formatPath(path) }
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
private emitSelect(): void {
|
|
281
|
+
if (this.active !== -1)
|
|
282
|
+
this.callbacks.onSelect?.(this.detail(this.active))
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private setActive(index: number, scroll: boolean): void {
|
|
286
|
+
if (index < 0 || index >= this.model.rows.length)
|
|
287
|
+
return
|
|
288
|
+
const moved = index !== this.active
|
|
289
|
+
this.active = index
|
|
290
|
+
this.activeId = this.model.rows[index].id
|
|
291
|
+
if (scroll)
|
|
292
|
+
this.scrollToRow(index)
|
|
293
|
+
this.render()
|
|
294
|
+
if (moved)
|
|
295
|
+
this.emitSelect()
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
private scrollToRow(index: number): void {
|
|
299
|
+
const top = index * this.rowHeight
|
|
300
|
+
const height = this.scroller.clientHeight
|
|
301
|
+
if (top < this.scroller.scrollTop)
|
|
302
|
+
this.scroller.scrollTop = top
|
|
303
|
+
else if (top + this.rowHeight > this.scroller.scrollTop + height)
|
|
304
|
+
this.scroller.scrollTop = top + this.rowHeight - height
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
private pageSize(): number {
|
|
308
|
+
return Math.max(1, Math.floor(this.scroller.clientHeight / this.rowHeight) - 1)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
private createRow(): RowElement {
|
|
312
|
+
const row = document.createElement('div') as RowElement
|
|
313
|
+
row.setAttribute('role', 'treeitem')
|
|
314
|
+
row.className = 'stx-jt-row'
|
|
315
|
+
|
|
316
|
+
const span = (className: string, hidden: boolean): HTMLElement => {
|
|
317
|
+
const element = document.createElement('span')
|
|
318
|
+
element.className = className
|
|
319
|
+
if (hidden)
|
|
320
|
+
element.setAttribute('aria-hidden', 'true')
|
|
321
|
+
row.appendChild(element)
|
|
322
|
+
return element
|
|
323
|
+
}
|
|
324
|
+
const parts: RowParts = {
|
|
325
|
+
twisty: span('stx-jt-twisty', true),
|
|
326
|
+
label: span('stx-jt-label', false),
|
|
327
|
+
separator: span('stx-jt-separator', true),
|
|
328
|
+
preview: span('stx-jt-preview', false),
|
|
329
|
+
actions: span('stx-jt-actions', false),
|
|
330
|
+
}
|
|
331
|
+
parts.separator.textContent = ': '
|
|
332
|
+
this.parts.set(row, parts)
|
|
333
|
+
this.rowsEl.appendChild(row)
|
|
334
|
+
return row
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
private button(action: string, text: string, label: string, as?: string): HTMLButtonElement {
|
|
338
|
+
const button = document.createElement('button')
|
|
339
|
+
button.type = 'button'
|
|
340
|
+
// Reached with the keyboard through the tree's own keys, not by tabbing
|
|
341
|
+
// through every row.
|
|
342
|
+
button.tabIndex = -1
|
|
343
|
+
button.className = 'stx-jt-action'
|
|
344
|
+
button.setAttribute('data-jt-action', action)
|
|
345
|
+
if (as)
|
|
346
|
+
button.setAttribute('data-jt-as', as)
|
|
347
|
+
button.setAttribute('aria-label', label)
|
|
348
|
+
button.textContent = text
|
|
349
|
+
return button
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
private opensOf(row: TreeRow, index: number): TreeOpenKind[] {
|
|
353
|
+
if (row.isMore)
|
|
354
|
+
return []
|
|
355
|
+
if (this.openable) {
|
|
356
|
+
const answer = this.openable(row.node, this.model.pathOf(index))
|
|
357
|
+
if (answer !== undefined)
|
|
358
|
+
return listOf(answer)
|
|
359
|
+
}
|
|
360
|
+
return listOf(row.node.openable)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** Draw the rows on screen. Rows already showing the right thing are left alone. */
|
|
364
|
+
private render(): void {
|
|
365
|
+
const rows = this.model.rows
|
|
366
|
+
const count = rows.length
|
|
367
|
+
const height = this.scroller.clientHeight
|
|
368
|
+
this.content.style.height = `${count * this.rowHeight}px`
|
|
369
|
+
// A tree that just got shorter still reports the old scroll position until
|
|
370
|
+
// the browser lays out again, and rows drawn from it would be off the end.
|
|
371
|
+
const top = Math.max(0, Math.min(this.scroller.scrollTop, count * this.rowHeight - height))
|
|
372
|
+
|
|
373
|
+
const first = Math.max(0, Math.floor(top / this.rowHeight) - this.overscan)
|
|
374
|
+
const last = Math.min(count, Math.ceil((top + height) / this.rowHeight) + this.overscan)
|
|
375
|
+
const needed = Math.max(0, last - first)
|
|
376
|
+
|
|
377
|
+
// A row index always lands on the same pooled element, so scrolling by one
|
|
378
|
+
// row rewrites one element and leaves the rest where they are.
|
|
379
|
+
while (this.pool.length < needed)
|
|
380
|
+
this.pool.push(this.createRow())
|
|
381
|
+
const size = this.pool.length
|
|
382
|
+
const used = new Uint8Array(size)
|
|
383
|
+
|
|
384
|
+
for (let index = first; index < last; index++) {
|
|
385
|
+
const slot = index % size
|
|
386
|
+
used[slot] = 1
|
|
387
|
+
this.paint(this.pool[slot], rows[index], index)
|
|
388
|
+
}
|
|
389
|
+
for (let slot = 0; slot < size; slot++) {
|
|
390
|
+
if (!used[slot] && this.pool[slot].style.display !== 'none') {
|
|
391
|
+
this.pool[slot].style.display = 'none'
|
|
392
|
+
this.pool[slot].__jt = undefined
|
|
393
|
+
this.pool[slot].removeAttribute('id')
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const activeDrawn = this.active >= first && this.active < last
|
|
398
|
+
if (activeDrawn)
|
|
399
|
+
this.scroller.setAttribute('aria-activedescendant', this.prefix + this.active)
|
|
400
|
+
else
|
|
401
|
+
this.scroller.removeAttribute('aria-activedescendant')
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
private paint(element: RowElement, row: TreeRow, index: number): void {
|
|
405
|
+
const version = this.model.version
|
|
406
|
+
const selected = index === this.active
|
|
407
|
+
const drawn = element.__jt
|
|
408
|
+
|
|
409
|
+
if (!drawn || drawn.index !== index || drawn.version !== version) {
|
|
410
|
+
const parts = this.parts.get(element) as RowParts
|
|
411
|
+
const node = row.node
|
|
412
|
+
element.style.display = ''
|
|
413
|
+
element.style.transform = `translateY(${index * this.rowHeight}px)`
|
|
414
|
+
element.style.setProperty('--stx-tree-depth', String(row.depth))
|
|
415
|
+
element.id = this.prefix + index
|
|
416
|
+
element.setAttribute('data-jt-index', String(index))
|
|
417
|
+
element.setAttribute('aria-level', String(row.depth + 1))
|
|
418
|
+
element.setAttribute('aria-posinset', String(row.posInSet))
|
|
419
|
+
element.setAttribute('aria-setsize', String(row.setSize))
|
|
420
|
+
if (row.expandable)
|
|
421
|
+
element.setAttribute('aria-expanded', row.expanded ? 'true' : 'false')
|
|
422
|
+
else
|
|
423
|
+
element.removeAttribute('aria-expanded')
|
|
424
|
+
|
|
425
|
+
if (row.isMore) {
|
|
426
|
+
element.setAttribute('data-kind', 'more')
|
|
427
|
+
parts.twisty.textContent = ''
|
|
428
|
+
parts.label.textContent = ''
|
|
429
|
+
parts.separator.style.display = 'none'
|
|
430
|
+
parts.preview.textContent = `… ${node.more} more`
|
|
431
|
+
parts.preview.setAttribute('data-kind', 'more')
|
|
432
|
+
}
|
|
433
|
+
else {
|
|
434
|
+
element.setAttribute('data-kind', node.kind)
|
|
435
|
+
parts.twisty.textContent = row.expandable ? (row.expanded ? '▾' : '▸') : ''
|
|
436
|
+
// An item of a list that was not given a label is called by its position.
|
|
437
|
+
const label = node.label ?? (typeof row.key === 'number' && row.parent !== -1 ? String(row.key) : '')
|
|
438
|
+
parts.label.textContent = clip(label)
|
|
439
|
+
parts.separator.style.display = label === '' ? 'none' : ''
|
|
440
|
+
parts.preview.textContent = clip(previewOf(node))
|
|
441
|
+
parts.preview.setAttribute('data-kind', node.kind)
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
const opens = this.opensOf(row, index)
|
|
445
|
+
const signature = row.isMore ? '+' : opens.join('|')
|
|
446
|
+
if (!drawn || drawn.opens !== signature) {
|
|
447
|
+
parts.actions.textContent = ''
|
|
448
|
+
if (row.isMore) {
|
|
449
|
+
parts.actions.appendChild(this.button('more', 'Load', 'Load more'))
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
for (const kind of opens)
|
|
453
|
+
parts.actions.appendChild(this.button('open', OPEN_LABELS[kind] ?? kind, `Open as ${OPEN_LABELS[kind]?.toLowerCase() ?? kind}`, kind))
|
|
454
|
+
parts.actions.appendChild(this.button('copy-value', 'Copy', 'Copy value'))
|
|
455
|
+
parts.actions.appendChild(this.button('copy-path', 'Path', 'Copy path'))
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
element.__jt = { index, version, opens: signature }
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (selected) {
|
|
462
|
+
element.setAttribute('aria-selected', 'true')
|
|
463
|
+
element.setAttribute('data-active', '')
|
|
464
|
+
}
|
|
465
|
+
else {
|
|
466
|
+
element.setAttribute('aria-selected', 'false')
|
|
467
|
+
element.removeAttribute('data-active')
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
private rowIndexOf(target: EventTarget | null): number {
|
|
472
|
+
const element = target instanceof Element ? target.closest('[data-jt-index]') : null
|
|
473
|
+
if (!element || (element as HTMLElement).style.display === 'none')
|
|
474
|
+
return -1
|
|
475
|
+
return Number(element.getAttribute('data-jt-index'))
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
private say(text: string): void {
|
|
479
|
+
if (this.status)
|
|
480
|
+
this.status.textContent = text
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
private toggleRow(index: number): void {
|
|
484
|
+
const row = this.model.rows[index]
|
|
485
|
+
if (!row)
|
|
486
|
+
return
|
|
487
|
+
if (row.isMore) {
|
|
488
|
+
this.loadMore(index)
|
|
489
|
+
return
|
|
490
|
+
}
|
|
491
|
+
if (row.node.ref !== undefined) {
|
|
492
|
+
this.follow(index)
|
|
493
|
+
return
|
|
494
|
+
}
|
|
495
|
+
if (!row.expandable)
|
|
496
|
+
return
|
|
497
|
+
const id = row.id
|
|
498
|
+
this.model.toggle(id)
|
|
499
|
+
this.afterChange()
|
|
500
|
+
const at = this.model.indexOf(id)
|
|
501
|
+
if (at !== -1)
|
|
502
|
+
this.callbacks.onToggle?.({ ...this.detail(at), expanded: this.model.rows[at].expanded })
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/** Go to the node a cycle points back to. */
|
|
506
|
+
private follow(index: number): void {
|
|
507
|
+
const target = this.model.rows[index].node.ref
|
|
508
|
+
if (target === undefined)
|
|
509
|
+
return
|
|
510
|
+
if (!this.reveal(target))
|
|
511
|
+
this.say('The value this points back to is not part of what was sent.')
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
private loadMore(index: number): void {
|
|
515
|
+
const row = this.model.rows[index]
|
|
516
|
+
const parent = row.parent
|
|
517
|
+
if (parent === -1)
|
|
518
|
+
return
|
|
519
|
+
this.callbacks.onLoadMore?.(this.detail(parent))
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
private copy(index: number, what: 'value' | 'path'): void {
|
|
523
|
+
const row = this.model.rows[index]
|
|
524
|
+
if (!row)
|
|
525
|
+
return
|
|
526
|
+
const at = row.isMore ? row.parent : index
|
|
527
|
+
const path = this.model.pathOf(at)
|
|
528
|
+
const node = this.model.rows[at].node
|
|
529
|
+
const text = what === 'path' ? formatPath(path) : nodeToJson(node)
|
|
530
|
+
try {
|
|
531
|
+
// Refused without a user gesture or off a secure origin; the event still
|
|
532
|
+
// carries the text, so a host can put it on the clipboard its own way.
|
|
533
|
+
const written = navigator.clipboard?.writeText(text)
|
|
534
|
+
written?.catch(() => {})
|
|
535
|
+
}
|
|
536
|
+
catch {}
|
|
537
|
+
this.say(what === 'path' ? `Copied path ${text}` : 'Copied value')
|
|
538
|
+
this.callbacks.onCopy?.({ what, text, path, node })
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
private open(index: number, as?: TreeOpenKind): void {
|
|
542
|
+
const row = this.model.rows[index]
|
|
543
|
+
if (!row || row.isMore)
|
|
544
|
+
return
|
|
545
|
+
const kind = as ?? this.opensOf(row, index)[0]
|
|
546
|
+
if (kind === undefined)
|
|
547
|
+
return
|
|
548
|
+
this.callbacks.onOpen?.({ ...this.detail(index), as: kind })
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
private onClick(event: MouseEvent): void {
|
|
552
|
+
const index = this.rowIndexOf(event.target)
|
|
553
|
+
if (index === -1)
|
|
554
|
+
return
|
|
555
|
+
const action = event.target instanceof Element ? event.target.closest('[data-jt-action]') : null
|
|
556
|
+
this.setActive(index, false)
|
|
557
|
+
|
|
558
|
+
if (action) {
|
|
559
|
+
const name = action.getAttribute('data-jt-action')
|
|
560
|
+
if (name === 'copy-value')
|
|
561
|
+
this.copy(index, 'value')
|
|
562
|
+
else if (name === 'copy-path')
|
|
563
|
+
this.copy(index, 'path')
|
|
564
|
+
else if (name === 'open')
|
|
565
|
+
this.open(index, action.getAttribute('data-jt-as') ?? undefined)
|
|
566
|
+
else if (name === 'more')
|
|
567
|
+
this.loadMore(index)
|
|
568
|
+
return
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
const row = this.model.rows[index]
|
|
572
|
+
// The twisty toggles. So does anywhere on a row that has nothing else to do
|
|
573
|
+
// with a click, which is every row: there is no inline editing here.
|
|
574
|
+
if (row.expandable || row.isMore || row.node.ref !== undefined)
|
|
575
|
+
this.toggleRow(index)
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
private onDoubleClick(event: MouseEvent): void {
|
|
579
|
+
const index = this.rowIndexOf(event.target)
|
|
580
|
+
if (index === -1 || (event.target instanceof Element && event.target.closest('[data-jt-action]')))
|
|
581
|
+
return
|
|
582
|
+
const row = this.model.rows[index]
|
|
583
|
+
if (!row.expandable && !row.isMore && row.node.ref === undefined)
|
|
584
|
+
this.open(index)
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
private onContextMenu(event: MouseEvent): void {
|
|
588
|
+
const index = this.rowIndexOf(event.target)
|
|
589
|
+
if (index === -1)
|
|
590
|
+
return
|
|
591
|
+
this.setActive(index, false)
|
|
592
|
+
if (this.contextMenu)
|
|
593
|
+
event.preventDefault()
|
|
594
|
+
this.callbacks.onContext?.({ ...this.detail(index), x: event.clientX, y: event.clientY })
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
private onKeyDown(event: KeyboardEvent): void {
|
|
598
|
+
const rows = this.model.rows
|
|
599
|
+
if (rows.length === 0)
|
|
600
|
+
return
|
|
601
|
+
const mod = event.metaKey || event.ctrlKey
|
|
602
|
+
const key = event.key
|
|
603
|
+
|
|
604
|
+
if (mod && (key === 'c' || key === 'C')) {
|
|
605
|
+
if (this.active === -1)
|
|
606
|
+
return
|
|
607
|
+
event.preventDefault()
|
|
608
|
+
this.copy(this.active, event.shiftKey ? 'path' : 'value')
|
|
609
|
+
return
|
|
610
|
+
}
|
|
611
|
+
if (mod || event.altKey)
|
|
612
|
+
return
|
|
613
|
+
|
|
614
|
+
if (key === 'Enter' && event.shiftKey) {
|
|
615
|
+
event.preventDefault()
|
|
616
|
+
if (this.active !== -1)
|
|
617
|
+
this.open(this.active)
|
|
618
|
+
return
|
|
619
|
+
}
|
|
620
|
+
// A space in the middle of typing a label is part of the label.
|
|
621
|
+
const typing = this.typed !== '' && Date.now() - this.typedAt <= TYPEAHEAD_RESET
|
|
622
|
+
if (key === 'Enter' || (key === ' ' && !typing)) {
|
|
623
|
+
event.preventDefault()
|
|
624
|
+
if (this.active !== -1)
|
|
625
|
+
this.toggleRow(this.active)
|
|
626
|
+
return
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
if (key === 'ArrowDown' || key === 'ArrowUp' || key === 'ArrowLeft' || key === 'ArrowRight' || key === 'Home' || key === 'End' || key === 'PageDown' || key === 'PageUp' || key === '*') {
|
|
630
|
+
event.preventDefault()
|
|
631
|
+
this.typed = ''
|
|
632
|
+
const before = this.active === -1 ? null : rows[this.active]
|
|
633
|
+
const wasExpanded = before?.expanded
|
|
634
|
+
const move = this.model.move(this.active, key, this.pageSize())
|
|
635
|
+
if (move.changed) {
|
|
636
|
+
this.active = move.index
|
|
637
|
+
this.activeId = this.model.rows[move.index]?.id ?? ''
|
|
638
|
+
this.scrollToRow(move.index)
|
|
639
|
+
this.render()
|
|
640
|
+
if (before && key !== '*') {
|
|
641
|
+
const at = this.model.indexOf(before.id)
|
|
642
|
+
if (at !== -1 && this.model.rows[at].expanded !== wasExpanded)
|
|
643
|
+
this.callbacks.onToggle?.({ ...this.detail(at), expanded: this.model.rows[at].expanded })
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
else {
|
|
647
|
+
this.setActive(move.index, true)
|
|
648
|
+
}
|
|
649
|
+
return
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
if (key.length === 1) {
|
|
653
|
+
const now = Date.now()
|
|
654
|
+
if (!typing)
|
|
655
|
+
this.typed = ''
|
|
656
|
+
this.typedAt = now
|
|
657
|
+
this.typed += key
|
|
658
|
+
const found = this.model.typeahead(this.active, this.typed)
|
|
659
|
+
if (found !== -1) {
|
|
660
|
+
event.preventDefault()
|
|
661
|
+
this.setActive(found, true)
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|