@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.
Files changed (100) hide show
  1. package/dist/ContextMenu-43g3w5tp.stx +188 -0
  2. package/dist/DataGrid-7rt7v865.stx +376 -0
  3. package/dist/GraphView-0t3t9mec.stx +320 -0
  4. package/dist/JsonTree-satc4010.stx +317 -0
  5. package/dist/PreviewFrame-erbgvzj8.stx +257 -0
  6. package/dist/SplitPane-7sqfdwqa.stx +159 -0
  7. package/dist/component-api/component-api.js +36 -0
  8. package/dist/context-menu/context-menu.js +575 -0
  9. package/dist/data-grid/data-grid.js +1244 -0
  10. package/dist/graph-view/graph-view.js +1408 -0
  11. package/dist/index.d.ts +8 -0
  12. package/dist/index.js +22 -20
  13. package/dist/json-tree/json-tree.js +1121 -0
  14. package/dist/preview-frame/preview-frame.js +356 -0
  15. package/dist/split-pane/split-pane.js +364 -0
  16. package/dist/ui/context-menu/ContextMenu.d.ts +14 -0
  17. package/dist/ui/context-menu/context-menu.d.ts +8 -0
  18. package/dist/ui/context-menu/core.d.ts +74 -0
  19. package/dist/ui/context-menu/index.d.ts +11 -0
  20. package/dist/ui/context-menu/view.d.ts +36 -0
  21. package/dist/ui/data-grid/DataGrid.d.ts +30 -0
  22. package/dist/ui/data-grid/core.d.ts +141 -0
  23. package/dist/ui/data-grid/data-grid.d.ts +9 -0
  24. package/dist/ui/data-grid/index.d.ts +20 -0
  25. package/dist/ui/data-grid/view.d.ts +56 -0
  26. package/dist/ui/graph-view/GraphView.d.ts +21 -0
  27. package/dist/ui/graph-view/core/edges.d.ts +34 -0
  28. package/dist/ui/graph-view/core/force.d.ts +10 -0
  29. package/dist/ui/graph-view/core/graph.d.ts +83 -0
  30. package/dist/ui/graph-view/core/index.d.ts +5 -0
  31. package/dist/ui/graph-view/core/layered.d.ts +24 -0
  32. package/dist/ui/graph-view/core/viewport.d.ts +34 -0
  33. package/dist/ui/graph-view/graph-view.d.ts +9 -0
  34. package/dist/ui/graph-view/index.d.ts +21 -0
  35. package/dist/ui/graph-view/view.d.ts +51 -0
  36. package/dist/ui/json-tree/JsonTree.d.ts +18 -0
  37. package/dist/ui/json-tree/core.d.ts +108 -0
  38. package/dist/ui/json-tree/index.d.ts +18 -0
  39. package/dist/ui/json-tree/json-tree.d.ts +10 -0
  40. package/dist/ui/json-tree/tinker.d.ts +84 -0
  41. package/dist/ui/json-tree/view.d.ts +59 -0
  42. package/dist/ui/preview-frame/PreviewFrame.d.ts +44 -0
  43. package/dist/ui/preview-frame/core.d.ts +81 -0
  44. package/dist/ui/preview-frame/index.d.ts +19 -0
  45. package/dist/ui/preview-frame/preview-frame.d.ts +8 -0
  46. package/dist/ui/preview-frame/view.d.ts +37 -0
  47. package/dist/ui/split-pane/SplitPane.d.ts +18 -0
  48. package/dist/ui/split-pane/core.d.ts +58 -0
  49. package/dist/ui/split-pane/index.d.ts +15 -0
  50. package/dist/ui/split-pane/split-pane.d.ts +8 -0
  51. package/dist/ui/split-pane/view.d.ts +27 -0
  52. package/dist/ui/virtual-table/index.d.ts +6 -0
  53. package/dist/utils/component-api.d.ts +4 -0
  54. package/package.json +38 -3
  55. package/src/index.ts +8 -0
  56. package/src/ui/code-editor/README.md +2 -1
  57. package/src/ui/context-menu/ContextMenu.stx +188 -0
  58. package/src/ui/context-menu/README.md +117 -0
  59. package/src/ui/context-menu/context-menu.ts +8 -0
  60. package/src/ui/context-menu/core.ts +250 -0
  61. package/src/ui/context-menu/index.ts +16 -0
  62. package/src/ui/context-menu/view.ts +540 -0
  63. package/src/ui/data-grid/DataGrid.stx +376 -0
  64. package/src/ui/data-grid/README.md +204 -0
  65. package/src/ui/data-grid/core.ts +813 -0
  66. package/src/ui/data-grid/data-grid.ts +9 -0
  67. package/src/ui/data-grid/index.ts +34 -0
  68. package/src/ui/data-grid/view.ts +806 -0
  69. package/src/ui/graph-view/GraphView.stx +320 -0
  70. package/src/ui/graph-view/README.md +210 -0
  71. package/src/ui/graph-view/core/edges.ts +180 -0
  72. package/src/ui/graph-view/core/force.ts +237 -0
  73. package/src/ui/graph-view/core/graph.ts +277 -0
  74. package/src/ui/graph-view/core/index.ts +5 -0
  75. package/src/ui/graph-view/core/layered.ts +256 -0
  76. package/src/ui/graph-view/core/viewport.ts +141 -0
  77. package/src/ui/graph-view/graph-view.ts +9 -0
  78. package/src/ui/graph-view/index.ts +35 -0
  79. package/src/ui/graph-view/view.ts +809 -0
  80. package/src/ui/json-tree/JsonTree.stx +317 -0
  81. package/src/ui/json-tree/README.md +193 -0
  82. package/src/ui/json-tree/core.ts +588 -0
  83. package/src/ui/json-tree/index.ts +28 -0
  84. package/src/ui/json-tree/json-tree.ts +10 -0
  85. package/src/ui/json-tree/tinker.ts +349 -0
  86. package/src/ui/json-tree/view.ts +665 -0
  87. package/src/ui/preview-frame/PreviewFrame.stx +257 -0
  88. package/src/ui/preview-frame/README.md +107 -0
  89. package/src/ui/preview-frame/core.ts +195 -0
  90. package/src/ui/preview-frame/index.ts +31 -0
  91. package/src/ui/preview-frame/preview-frame.ts +8 -0
  92. package/src/ui/preview-frame/view.ts +336 -0
  93. package/src/ui/split-pane/README.md +87 -0
  94. package/src/ui/split-pane/SplitPane.stx +159 -0
  95. package/src/ui/split-pane/core.ts +200 -0
  96. package/src/ui/split-pane/index.ts +24 -0
  97. package/src/ui/split-pane/split-pane.ts +8 -0
  98. package/src/ui/split-pane/view.ts +305 -0
  99. package/src/ui/virtual-table/index.ts +6 -0
  100. package/src/utils/component-api.ts +72 -0
@@ -0,0 +1,200 @@
1
+ /**
2
+ * The arithmetic of a split layout. No DOM.
3
+ *
4
+ * A layout is a list of fractions, one per pane, that add up to one. Fractions
5
+ * rather than pixels, so a window that changes size keeps its proportions and
6
+ * a saved layout means the same thing on another screen. Pixels come in only
7
+ * where a person does: a drag is so many pixels, and a pane must not get
8
+ * narrower than so many.
9
+ */
10
+
11
+ export type SplitDirection = 'horizontal' | 'vertical'
12
+
13
+ /** The pixel limits of one pane. `max` is `Infinity` when there is none. */
14
+ export interface PaneLimit {
15
+ min: number
16
+ max: number
17
+ }
18
+
19
+ // Written out: the formatter splits a negative exponent (1e-9) into a subtraction.
20
+ const EPSILON = 0.000000001
21
+
22
+ /** Equal shares for `count` panes. */
23
+ export function equalSizes(count: number): number[] {
24
+ return Array.from({ length: count }, () => 1 / count)
25
+ }
26
+
27
+ /**
28
+ * Fractions that add up to one, from any list of positive numbers.
29
+ *
30
+ * `[30, 70]`, `[0.3, 0.7]` and `[3, 7]` are the same layout. A list of the
31
+ * wrong length, or one holding anything that is not a positive finite number,
32
+ * is not a layout, and the panes are shared equally instead.
33
+ */
34
+ export function normalizeSizes(sizes: readonly number[] | null | undefined, count: number): number[] {
35
+ if (count <= 0)
36
+ return []
37
+ if (!sizes || sizes.length !== count || sizes.some(size => typeof size !== 'number' || !Number.isFinite(size) || size <= 0))
38
+ return equalSizes(count)
39
+
40
+ const total = sizes.reduce((sum, size) => sum + size, 0)
41
+ return sizes.map(size => size / total)
42
+ }
43
+
44
+ /**
45
+ * Read sizes as they arrive from an attribute, a prop or storage: an array, a
46
+ * JSON array, or `30,70`. Null when there is nothing usable.
47
+ */
48
+ export function parseSizes(value: unknown): number[] | null {
49
+ let list: unknown = value
50
+ if (typeof value === 'string') {
51
+ const text = value.trim()
52
+ if (text === '')
53
+ return null
54
+ if (text.charAt(0) === '[') {
55
+ try {
56
+ list = JSON.parse(text)
57
+ }
58
+ catch {
59
+ return null
60
+ }
61
+ }
62
+ else {
63
+ list = text.split(/[\s,]+/).map(part => Number.parseFloat(part))
64
+ }
65
+ }
66
+
67
+ if (!Array.isArray(list) || list.length === 0)
68
+ return null
69
+ const numbers = list.map(entry => (typeof entry === 'string' ? Number.parseFloat(entry) : entry))
70
+ return numbers.every(entry => typeof entry === 'number' && Number.isFinite(entry) && entry > 0) ? numbers as number[] : null
71
+ }
72
+
73
+ /** A pixel length from `120`, `'120'` or `'120px'`. The fallback for anything else. */
74
+ export function parseLength(value: unknown, fallback: number): number {
75
+ if (typeof value === 'number')
76
+ return Number.isFinite(value) && value >= 0 ? value : fallback
77
+ if (typeof value !== 'string' || value.trim() === '')
78
+ return fallback
79
+ const parsed = Number.parseFloat(value)
80
+ return Number.isFinite(parsed) && parsed >= 0 ? parsed : fallback
81
+ }
82
+
83
+ /**
84
+ * Bring a layout inside its limits for a container `total` pixels long.
85
+ *
86
+ * A pane under its minimum is raised to it and the difference is taken from
87
+ * the panes that have room, in proportion to the room they have. When the
88
+ * minimums do not fit at all the container is simply too small, and the panes
89
+ * share it in proportion to their minimums, which is the least wrong answer.
90
+ */
91
+ export function fitSizes(sizes: readonly number[], total: number, limits: readonly PaneLimit[]): number[] {
92
+ const count = sizes.length
93
+ if (count === 0 || total <= 0)
94
+ return [...sizes]
95
+
96
+ const minimums = limits.map(limit => Math.max(0, limit.min))
97
+ const minimumTotal = minimums.reduce((sum, min) => sum + min, 0)
98
+ if (minimumTotal >= total)
99
+ return minimumTotal === 0 ? [...sizes] : minimums.map(min => min / minimumTotal)
100
+
101
+ const pixels = sizes.map(size => size * total)
102
+
103
+ // Raise what is too small, lower what is too large, then hand the difference
104
+ // to the panes that can take it. Each pass pins at least one pane, so the
105
+ // number of panes bounds the number of passes.
106
+ for (let pass = 0; pass <= count; pass++) {
107
+ let surplus = 0
108
+ for (let index = 0; index < count; index++) {
109
+ const clamped = Math.min(Math.max(pixels[index], minimums[index]), limits[index].max)
110
+ surplus += pixels[index] - clamped
111
+ pixels[index] = clamped
112
+ }
113
+ if (Math.abs(surplus) < EPSILON)
114
+ break
115
+
116
+ const room = pixels.map((size, index) => (surplus > 0 ? limits[index].max - size : size - minimums[index]))
117
+ const roomTotal = room.reduce((sum, value) => sum + (Number.isFinite(value) ? value : total), 0)
118
+ if (roomTotal < EPSILON)
119
+ break
120
+ for (let index = 0; index < count; index++) {
121
+ const share = (Number.isFinite(room[index]) ? room[index] : total) / roomTotal
122
+ pixels[index] += surplus * share
123
+ }
124
+ }
125
+
126
+ const sum = pixels.reduce((a, b) => a + b, 0)
127
+ return pixels.map(size => size / sum)
128
+ }
129
+
130
+ /**
131
+ * Move the divider after pane `index` by `delta` pixels.
132
+ *
133
+ * A positive delta grows the pane before the divider. The pane next to the
134
+ * divider on the other side shrinks first; once it is at its minimum the one
135
+ * beyond it gives, and so on, which is what dragging a divider through a
136
+ * narrow pane should do. The pane that grows stops at its maximum.
137
+ */
138
+ export function resizeAt(sizes: readonly number[], index: number, delta: number, total: number, limits: readonly PaneLimit[]): number[] {
139
+ const count = sizes.length
140
+ if (index < 0 || index >= count - 1 || total <= 0 || delta === 0)
141
+ return [...sizes]
142
+
143
+ const pixels = sizes.map(size => size * total)
144
+ const forward = delta > 0
145
+ const grower = forward ? index : index + 1
146
+ const step = forward ? 1 : -1
147
+
148
+ let wanted = Math.min(Math.abs(delta), limits[grower].max - pixels[grower])
149
+ if (wanted <= 0)
150
+ return [...sizes]
151
+
152
+ let taken = 0
153
+ for (let pane = forward ? index + 1 : index; pane >= 0 && pane < count && wanted > EPSILON; pane += step) {
154
+ const give = Math.min(wanted, Math.max(0, pixels[pane] - Math.max(0, limits[pane].min)))
155
+ pixels[pane] -= give
156
+ taken += give
157
+ wanted -= give
158
+ }
159
+ pixels[grower] += taken
160
+
161
+ return pixels.map(size => size / total)
162
+ }
163
+
164
+ /** How far the divider after pane `index` can move each way, in pixels. */
165
+ export function dividerRange(sizes: readonly number[], index: number, total: number, limits: readonly PaneLimit[]): { back: number, forward: number } {
166
+ const pixels = sizes.map(size => size * total)
167
+ const room = (from: number, to: number): number => {
168
+ let sum = 0
169
+ for (let pane = from; pane <= to; pane++)
170
+ sum += Math.max(0, pixels[pane] - Math.max(0, limits[pane].min))
171
+ return sum
172
+ }
173
+
174
+ return {
175
+ back: Math.min(room(0, index), limits[index + 1].max - pixels[index + 1]),
176
+ forward: Math.min(room(index + 1, sizes.length - 1), limits[index].max - pixels[index]),
177
+ }
178
+ }
179
+
180
+ /** What a separator reports to assistive technology: the pane before it, in percent. */
181
+ export function separatorValue(sizes: readonly number[], index: number, total: number, limits: readonly PaneLimit[]): { now: number, min: number, max: number } {
182
+ const range = dividerRange(sizes, index, total, limits)
183
+ const now = sizes[index] * 100
184
+ const span = total > 0 ? 100 / total : 0
185
+ return {
186
+ now: Math.round(now),
187
+ min: Math.round(now - range.back * span),
188
+ max: Math.round(now + range.forward * span),
189
+ }
190
+ }
191
+
192
+ /** The storage name a layout is kept under. */
193
+ export function storageName(key: string): string {
194
+ return `stx:split-pane:${key}`
195
+ }
196
+
197
+ /** Sizes as they are stored: rounded, so the text is short and stable. */
198
+ export function serializeSizes(sizes: readonly number[]): string {
199
+ return JSON.stringify(sizes.map(size => Math.round(size * 1e4) / 1e4))
200
+ }
@@ -0,0 +1,24 @@
1
+ import type { SplitDirection } from './core'
2
+
3
+ export * from './core'
4
+ export { default as SplitPane } from './SplitPane.stx'
5
+ export * from './view'
6
+
7
+ export interface SplitPaneProps {
8
+ /** `horizontal` puts the panes side by side, `vertical` stacks them. Default `horizontal`. */
9
+ direction?: SplitDirection
10
+ /** Shares, one per pane: `[30, 70]`, `[1, 2, 1]`, or the text `30,70`. */
11
+ sizes?: number[] | string
12
+ /** The least a pane can be, in pixels. A pane overrides it with `data-min-size`. Default 48. */
13
+ minSize?: number
14
+ /** Pixels an arrow key moves a separator. Default 16. */
15
+ step?: number
16
+ /** Keeps the layout in local storage under this name. */
17
+ storageKey?: string
18
+ /** The accessible name of a separator. Default `Resize panes`. */
19
+ label?: string
20
+ /** A CSS length. Default `100%`. */
21
+ height?: string
22
+ id?: string
23
+ className?: string
24
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * The split layout without its stx template.
3
+ *
4
+ * `@stacksjs/components/split-pane`. The arithmetic (`./core`, no DOM) and the
5
+ * view that applies it to any element whose children are the panes.
6
+ */
7
+ export * from './core'
8
+ export * from './view'
@@ -0,0 +1,305 @@
1
+ /**
2
+ * A split layout in the DOM.
3
+ *
4
+ * The panes are whatever elements the host put inside the root. This puts a
5
+ * separator between each pair, sizes the panes with flex-grow (so the browser
6
+ * keeps the proportions when the container changes size, with no script), and
7
+ * turns a drag or a key on a separator into a new layout.
8
+ */
9
+ import type { PaneLimit, SplitDirection } from './core'
10
+ import { dividerRange, fitSizes, normalizeSizes, parseLength, parseSizes, resizeAt, separatorValue, serializeSizes, storageName } from './core'
11
+
12
+ export interface SplitPaneOptions {
13
+ direction?: SplitDirection
14
+ /** The starting layout, one number per pane. Any positive numbers; they are shares. */
15
+ sizes?: number[] | null
16
+ /** The least a pane can be, in pixels, unless the pane says otherwise. */
17
+ minSize?: number
18
+ /** A name to keep the layout under in local storage. Empty for none. */
19
+ storageKey?: string
20
+ /** Pixels an arrow key moves a separator. Shift moves five times as far. */
21
+ step?: number
22
+ /** The accessible name of a separator. */
23
+ label?: string
24
+ }
25
+
26
+ export interface SplitResizeDetail {
27
+ /** The layout, as fractions that add up to one. */
28
+ sizes: number[]
29
+ /** The same in pixels. */
30
+ pixels: number[]
31
+ /** The separator that moved, or -1 when the layout was set from outside. */
32
+ index: number
33
+ source: 'pointer' | 'keyboard' | 'reset' | 'api'
34
+ }
35
+
36
+ export interface SplitPaneCallbacks {
37
+ onResize?: (detail: SplitResizeDetail) => void
38
+ onResizeEnd?: (detail: SplitResizeDetail) => void
39
+ }
40
+
41
+ const SEPARATOR = 'data-split-separator'
42
+
43
+ export class SplitPaneView {
44
+ private direction: SplitDirection
45
+ private panes: HTMLElement[] = []
46
+ private separators: HTMLElement[] = []
47
+ private sizes: number[] = []
48
+ private initial: number[] = []
49
+ private limits: PaneLimit[] = []
50
+ private minSize: number
51
+ private step: number
52
+ private label: string
53
+ private storageKey: string
54
+ private observer: ResizeObserver | null = null
55
+ private drag: { index: number, start: number, sizes: number[], pointer: number } | null = null
56
+ private readonly disposers: Array<() => void> = []
57
+
58
+ constructor(private readonly root: HTMLElement, options: SplitPaneOptions = {}, private readonly callbacks: SplitPaneCallbacks = {}) {
59
+ this.direction = options.direction === 'vertical' ? 'vertical' : 'horizontal'
60
+ this.minSize = options.minSize ?? 48
61
+ this.step = options.step ?? 16
62
+ this.label = options.label || 'Resize panes'
63
+ this.storageKey = options.storageKey || ''
64
+
65
+ this.collect()
66
+ this.initial = this.startingSizes(options.sizes)
67
+ this.sizes = normalizeSizes(this.stored() ?? this.initial, this.panes.length)
68
+ this.build()
69
+ this.apply(false)
70
+
71
+ if (typeof ResizeObserver !== 'undefined') {
72
+ this.observer = new ResizeObserver(() => this.apply(false))
73
+ this.observer.observe(this.root)
74
+ }
75
+ this.root.setAttribute('data-split-ready', '')
76
+ }
77
+
78
+ /** The layout, as fractions that add up to one. */
79
+ getSizes(): number[] {
80
+ return [...this.sizes]
81
+ }
82
+
83
+ /** Set the layout. Sizes are shares: `[1, 2]` is a third and two thirds. */
84
+ setSizes(sizes: readonly number[] | null | undefined): void {
85
+ if (!sizes || sizes.length !== this.panes.length)
86
+ return
87
+ this.sizes = normalizeSizes(sizes, this.panes.length)
88
+ this.apply(true)
89
+ this.report(-1, 'api', true)
90
+ }
91
+
92
+ /** Back to the layout it started with, forgetting a stored one. */
93
+ reset(index = -1): void {
94
+ this.sizes = [...this.initial]
95
+ this.apply(true)
96
+ this.report(index, 'reset', true)
97
+ }
98
+
99
+ setDirection(direction: SplitDirection): void {
100
+ const next = direction === 'vertical' ? 'vertical' : 'horizontal'
101
+ if (next === this.direction)
102
+ return
103
+ this.direction = next
104
+ this.root.setAttribute('data-direction', next)
105
+ for (const separator of this.separators)
106
+ separator.setAttribute('aria-orientation', next === 'horizontal' ? 'vertical' : 'horizontal')
107
+ this.apply(false)
108
+ }
109
+
110
+ destroy(): void {
111
+ this.observer?.disconnect()
112
+ for (const dispose of this.disposers) dispose()
113
+ for (const separator of this.separators) separator.remove()
114
+ for (const pane of this.panes) {
115
+ pane.style.removeProperty('flex')
116
+ pane.removeAttribute('data-split-pane')
117
+ }
118
+ this.root.removeAttribute('data-split-ready')
119
+ }
120
+
121
+ /** The length of the container along the split, without the separators. */
122
+ private available(): number {
123
+ const box = this.root.getBoundingClientRect()
124
+ const length = this.direction === 'horizontal' ? box.width : box.height
125
+ let taken = 0
126
+ for (const separator of this.separators) {
127
+ const size = separator.getBoundingClientRect()
128
+ taken += this.direction === 'horizontal' ? size.width : size.height
129
+ }
130
+ return Math.max(0, length - taken)
131
+ }
132
+
133
+ private collect(): void {
134
+ this.panes = Array.from(this.root.children).filter((child): child is HTMLElement => child instanceof HTMLElement && !child.hasAttribute(SEPARATOR))
135
+ this.limits = this.panes.map(pane => ({
136
+ min: parseLength(pane.getAttribute('data-min-size'), this.minSize),
137
+ max: parseLength(pane.getAttribute('data-max-size'), Number.POSITIVE_INFINITY),
138
+ }))
139
+ }
140
+
141
+ /** A pane can name its own share with `data-size`; the rest come from the prop. */
142
+ private startingSizes(sizes: number[] | null | undefined): number[] {
143
+ const own = this.panes.map(pane => Number.parseFloat(pane.getAttribute('data-size') || ''))
144
+ const fromPanes = own.every(size => Number.isFinite(size) && size > 0) ? own : null
145
+ return normalizeSizes(sizes && sizes.length === this.panes.length ? sizes : fromPanes, this.panes.length)
146
+ }
147
+
148
+ private stored(): number[] | null {
149
+ if (!this.storageKey)
150
+ return null
151
+ try {
152
+ const sizes = parseSizes(localStorage.getItem(storageName(this.storageKey)))
153
+ return sizes && sizes.length === this.panes.length ? sizes : null
154
+ }
155
+ catch {
156
+ // Storage can be switched off, and a layout that is not kept still works.
157
+ return null
158
+ }
159
+ }
160
+
161
+ private store(): void {
162
+ if (!this.storageKey)
163
+ return
164
+ try {
165
+ localStorage.setItem(storageName(this.storageKey), serializeSizes(this.sizes))
166
+ }
167
+ catch {}
168
+ }
169
+
170
+ private build(): void {
171
+ const prefix = this.root.id || `split-${Math.random().toString(36).slice(2, 8)}`
172
+ this.panes.forEach((pane, index) => {
173
+ pane.setAttribute('data-split-pane', '')
174
+ if (!pane.id)
175
+ pane.id = `${prefix}-pane-${index}`
176
+ if (index === this.panes.length - 1)
177
+ return
178
+
179
+ const separator = document.createElement('div')
180
+ separator.setAttribute(SEPARATOR, '')
181
+ separator.setAttribute('role', 'separator')
182
+ separator.setAttribute('tabindex', '0')
183
+ separator.setAttribute('aria-orientation', this.direction === 'horizontal' ? 'vertical' : 'horizontal')
184
+ separator.setAttribute('aria-controls', pane.id)
185
+ separator.setAttribute('aria-label', this.panes.length > 2 ? `${this.label} ${index + 1} and ${index + 2}` : this.label)
186
+ separator.className = 'stx-split-separator'
187
+ pane.after(separator)
188
+ this.separators.push(separator)
189
+
190
+ const listen = <K extends keyof HTMLElementEventMap>(type: K, handler: (event: HTMLElementEventMap[K]) => void): void => {
191
+ separator.addEventListener(type, handler)
192
+ this.disposers.push(() => separator.removeEventListener(type, handler))
193
+ }
194
+ listen('pointerdown', event => this.onPointerDown(event, index))
195
+ listen('pointermove', event => this.onPointerMove(event))
196
+ listen('pointerup', event => this.onPointerEnd(event))
197
+ listen('pointercancel', event => this.onPointerEnd(event))
198
+ listen('dblclick', () => this.reset(index))
199
+ listen('keydown', event => this.onKeyDown(event, index))
200
+ })
201
+ }
202
+
203
+ /** Write the layout to the panes. `fit` brings it inside the limits first. */
204
+ private apply(persist: boolean): void {
205
+ const total = this.available()
206
+ if (total > 0)
207
+ this.sizes = fitSizes(this.sizes, total, this.limits)
208
+
209
+ this.panes.forEach((pane, index) => {
210
+ // The share as the grow factor, from a basis of nothing: the browser then
211
+ // divides whatever length there is in these proportions by itself.
212
+ pane.style.flex = `${(this.sizes[index] * 1000).toFixed(3)} 1 0px`
213
+ })
214
+ this.separators.forEach((separator, index) => {
215
+ const value = separatorValue(this.sizes, index, total, this.limits)
216
+ separator.setAttribute('aria-valuenow', String(value.now))
217
+ separator.setAttribute('aria-valuemin', String(value.min))
218
+ separator.setAttribute('aria-valuemax', String(value.max))
219
+ })
220
+ if (persist)
221
+ this.store()
222
+ }
223
+
224
+ private report(index: number, source: SplitResizeDetail['source'], end: boolean): void {
225
+ const total = this.available()
226
+ const detail: SplitResizeDetail = { sizes: [...this.sizes], pixels: this.sizes.map(size => Math.round(size * total)), index, source }
227
+ this.callbacks.onResize?.(detail)
228
+ if (end)
229
+ this.callbacks.onResizeEnd?.(detail)
230
+ }
231
+
232
+ private position(event: PointerEvent): number {
233
+ return this.direction === 'horizontal' ? event.clientX : event.clientY
234
+ }
235
+
236
+ private onPointerDown(event: PointerEvent, index: number): void {
237
+ if (event.button !== 0)
238
+ return
239
+ event.preventDefault()
240
+ const separator = this.separators[index]
241
+ // Captured, so the drag keeps going over an iframe or outside the window.
242
+ try {
243
+ separator.setPointerCapture(event.pointerId)
244
+ }
245
+ catch {}
246
+ this.drag = { index, start: this.position(event), sizes: [...this.sizes], pointer: event.pointerId }
247
+ this.root.setAttribute('data-dragging', '')
248
+ separator.setAttribute('data-active', '')
249
+ }
250
+
251
+ private onPointerMove(event: PointerEvent): void {
252
+ const drag = this.drag
253
+ if (!drag || event.pointerId !== drag.pointer)
254
+ return
255
+ // Always from where the drag began, so a pointer that overshoots a limit
256
+ // and comes back picks the divider up where it left it.
257
+ this.sizes = resizeAt(drag.sizes, drag.index, this.position(event) - drag.start, this.available(), this.limits)
258
+ this.apply(false)
259
+ this.report(drag.index, 'pointer', false)
260
+ }
261
+
262
+ private onPointerEnd(event: PointerEvent): void {
263
+ const drag = this.drag
264
+ if (!drag || event.pointerId !== drag.pointer)
265
+ return
266
+ this.drag = null
267
+ this.root.removeAttribute('data-dragging')
268
+ this.separators[drag.index].removeAttribute('data-active')
269
+ try {
270
+ this.separators[drag.index].releasePointerCapture(event.pointerId)
271
+ }
272
+ catch {}
273
+ this.store()
274
+ this.report(drag.index, 'pointer', true)
275
+ }
276
+
277
+ private onKeyDown(event: KeyboardEvent, index: number): void {
278
+ const horizontal = this.direction === 'horizontal'
279
+ const total = this.available()
280
+ const stride = this.step * (event.shiftKey ? 5 : 1)
281
+ let delta = 0
282
+
283
+ if (event.key === (horizontal ? 'ArrowLeft' : 'ArrowUp'))
284
+ delta = -stride
285
+ else if (event.key === (horizontal ? 'ArrowRight' : 'ArrowDown'))
286
+ delta = stride
287
+ else if (event.key === 'Home')
288
+ delta = -dividerRange(this.sizes, index, total, this.limits).back
289
+ else if (event.key === 'End')
290
+ delta = dividerRange(this.sizes, index, total, this.limits).forward
291
+ else if (event.key === 'Enter') {
292
+ event.preventDefault()
293
+ this.reset(index)
294
+ return
295
+ }
296
+ else {
297
+ return
298
+ }
299
+
300
+ event.preventDefault()
301
+ this.sizes = resizeAt(this.sizes, index, delta, total, this.limits)
302
+ this.apply(true)
303
+ this.report(index, 'keyboard', true)
304
+ }
305
+ }
@@ -1,3 +1,9 @@
1
+ /**
2
+ * @deprecated Use `<DataGrid>` (`../data-grid`). It virtualizes columns as
3
+ * well as rows and adds sorting, resizing, filtering, selection, keyboard
4
+ * navigation and export. `<VirtualTable>` stays so existing pages keep
5
+ * working, and gets no new features.
6
+ */
1
7
  export { default as VirtualTable } from './VirtualTable.stx'
2
8
 
3
9
  export interface VirtualTableColumn<T = any> {
@@ -0,0 +1,72 @@
1
+ /**
2
+ * The methods a component exposes, from outside it.
3
+ *
4
+ * A component hands its methods over with `defineExpose`: an editor's
5
+ * `setValue`, a grid's `setRows`, a menu's `open`. stx keeps them on the
6
+ * element that carries the component's scope, which is a wrapper around the
7
+ * component's own root, and a `ref` written on a component tag does not reach
8
+ * either. So there was no way to call one short of knowing where the runtime
9
+ * keeps it. This is that knowledge, in one place:
10
+ *
11
+ * ```ts
12
+ * import { componentApi } from '@stacksjs/components/component-api'
13
+ *
14
+ * componentApi('#results').setRows(rows)
15
+ * ```
16
+ *
17
+ * The target is the component's own element, or its id, or a selector, or any
18
+ * element around exactly one component. Null where there is nothing to find:
19
+ * on the server, before the component has mounted, or for an element that is
20
+ * not a component. A component mounts after the page's own script has run, so
21
+ * ask for its methods when they are needed, not at the top of the script.
22
+ */
23
+
24
+ interface ExposingElement extends Element {
25
+ __stx_exposed?: unknown
26
+ }
27
+
28
+ const SCOPE = '[data-stx-scope]'
29
+
30
+ function find(target: Element | string | null | undefined): Element | null {
31
+ if (!target)
32
+ return null
33
+ if (typeof target !== 'string')
34
+ return target
35
+ if (typeof document === 'undefined')
36
+ return null
37
+ // A bare word is an id, as it is the commonest thing to have.
38
+ const byId = /^[\w-]+$/.test(target) ? document.getElementById(target) : null
39
+ if (byId)
40
+ return byId
41
+ try {
42
+ return document.querySelector(target)
43
+ }
44
+ catch {
45
+ return null
46
+ }
47
+ }
48
+
49
+ /** What a component exposes when nothing more precise is known: methods by name. */
50
+ // eslint-disable-next-line ts/no-explicit-any
51
+ export type ExposedMethods = Record<string, (..._arguments: any[]) => any>
52
+
53
+ export function componentApi<T = ExposedMethods>(target: Element | string | null | undefined): T | null {
54
+ const element = find(target) as ExposingElement | null
55
+ if (!element || typeof element.closest !== 'function')
56
+ return null
57
+
58
+ if (element.__stx_exposed)
59
+ return element.__stx_exposed as T
60
+
61
+ // The component's own root sits inside the scope that holds its methods.
62
+ const around = element.closest(SCOPE) as ExposingElement | null
63
+ if (around?.__stx_exposed)
64
+ return around.__stx_exposed as T
65
+
66
+ // An element around a component: the first one inside it.
67
+ for (const inside of Array.from(element.querySelectorAll(SCOPE)) as ExposingElement[]) {
68
+ if (inside.__stx_exposed)
69
+ return inside.__stx_exposed as T
70
+ }
71
+ return null
72
+ }