@malloy-publisher/sdk 0.0.247 → 0.0.249

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 (98) hide show
  1. package/README.md +32 -17
  2. package/dist/ServerProvider-CcfeBpt3.cjs.js +1 -0
  3. package/dist/{ServerProvider-BxEh3nFI.es.js → ServerProvider-EqBlMMqx.es.js} +1056 -905
  4. package/dist/client/api.d.ts +298 -7
  5. package/dist/client/index.cjs.js +1 -1
  6. package/dist/client/index.es.js +74 -70
  7. package/dist/components/Dashboard/Dashboard.d.ts +49 -0
  8. package/dist/components/Dashboard/DashboardTile.d.ts +48 -0
  9. package/dist/components/Dashboard/index.d.ts +2 -0
  10. package/dist/components/Notebook/Notebook.d.ts +46 -8
  11. package/dist/components/Notebook/NotebookCell.d.ts +37 -3
  12. package/dist/components/Notebook/runPlan.d.ts +29 -0
  13. package/dist/components/Notebook/types.d.ts +10 -0
  14. package/dist/components/Package/ContentTypeIcon.d.ts +17 -1
  15. package/dist/components/Package/Package.d.ts +1 -4
  16. package/dist/components/RenderedResult/RenderedResult.d.ts +7 -1
  17. package/dist/components/RenderedResult/ResultContainer.d.ts +3 -1
  18. package/dist/components/ResultsDialog.d.ts +4 -1
  19. package/dist/components/ServerProvider.d.ts +2 -1
  20. package/dist/components/click_helper.d.ts +10 -1
  21. package/dist/components/drill/index.d.ts +3 -0
  22. package/dist/components/drill/markDrillableCells.d.ts +55 -0
  23. package/dist/components/drill/resolveDrill.d.ts +159 -0
  24. package/dist/components/drill/useDrill.d.ts +63 -0
  25. package/dist/components/given/GivenInput.d.ts +27 -3
  26. package/dist/components/given/GivensPanel.d.ts +36 -7
  27. package/dist/components/given/filterValue.d.ts +117 -0
  28. package/dist/components/given/index.d.ts +1 -1
  29. package/dist/components/given/paramCodec.d.ts +60 -0
  30. package/dist/components/index.d.ts +4 -1
  31. package/dist/components/styles.d.ts +23 -0
  32. package/dist/constants/docLinks.d.ts +6 -0
  33. package/dist/{core-BuvlzSIX.es.js → core-DFBwNhvb.es.js} +1 -1
  34. package/dist/{core-C86Rb7tJ.cjs.js → core-DZk7z7ko.cjs.js} +1 -1
  35. package/dist/hooks/givenValue.d.ts +10 -0
  36. package/dist/hooks/index.d.ts +3 -1
  37. package/dist/hooks/useGivensState.d.ts +64 -0
  38. package/dist/hooks/useSuggestOptions.d.ts +78 -0
  39. package/dist/{index-BdtAZ5k6.es.js → index-CLcRrkxd.es.js} +41221 -39894
  40. package/dist/index-CWQXhe0D.cjs.js +338 -0
  41. package/dist/index.cjs.js +1 -1
  42. package/dist/index.es.js +77 -62
  43. package/dist/theme/types.d.ts +9 -0
  44. package/package.json +3 -1
  45. package/src/components/Connections/AddConnectionDialog.tsx +10 -1
  46. package/src/components/Dashboard/Dashboard.tsx +395 -0
  47. package/src/components/Dashboard/DashboardTile.tsx +150 -0
  48. package/src/components/Dashboard/index.ts +2 -0
  49. package/src/components/Home/Home.tsx +114 -23
  50. package/src/components/Notebook/Notebook.tsx +444 -390
  51. package/src/components/Notebook/NotebookCell.tsx +117 -3
  52. package/src/components/Notebook/runPlan.spec.ts +152 -0
  53. package/src/components/Notebook/runPlan.ts +64 -0
  54. package/src/components/Notebook/types.ts +11 -0
  55. package/src/components/Package/ContentTypeIcon.spec.ts +88 -0
  56. package/src/components/Package/ContentTypeIcon.tsx +77 -6
  57. package/src/components/Package/Package.tsx +148 -38
  58. package/src/components/RenderedResult/RenderedResult.tsx +309 -19
  59. package/src/components/RenderedResult/ResultContainer.tsx +28 -1
  60. package/src/components/ResultsDialog.tsx +5 -0
  61. package/src/components/ServerProvider.tsx +2 -0
  62. package/src/components/click_helper.ts +14 -1
  63. package/src/components/drill/index.ts +25 -0
  64. package/src/components/drill/markDrillableCells.dom.spec.ts +310 -0
  65. package/src/components/drill/markDrillableCells.spec.ts +106 -0
  66. package/src/components/drill/markDrillableCells.ts +311 -0
  67. package/src/components/drill/resolveDrill.spec.ts +446 -0
  68. package/src/components/drill/resolveDrill.ts +372 -0
  69. package/src/components/drill/useDrill.tsx +200 -0
  70. package/src/components/given/GivenInput.spec.tsx +495 -15
  71. package/src/components/given/GivenInput.tsx +562 -30
  72. package/src/components/given/GivensPanel.spec.tsx +172 -0
  73. package/src/components/given/GivensPanel.tsx +139 -32
  74. package/src/components/given/filterValue.spec.ts +361 -0
  75. package/src/components/given/filterValue.ts +204 -0
  76. package/src/components/given/index.ts +5 -1
  77. package/src/components/given/paramCodec.spec.ts +854 -0
  78. package/src/components/given/paramCodec.ts +480 -0
  79. package/src/components/index.ts +4 -1
  80. package/src/components/styles.ts +27 -3
  81. package/src/constants/docLinks.spec.ts +51 -0
  82. package/src/constants/docLinks.ts +26 -0
  83. package/src/hooks/givenValue.ts +10 -0
  84. package/src/hooks/index.ts +6 -4
  85. package/src/hooks/useGivensState.spec.ts +446 -0
  86. package/src/hooks/useGivensState.ts +337 -0
  87. package/src/hooks/useSuggestOptions.spec.ts +109 -0
  88. package/src/hooks/useSuggestOptions.ts +260 -0
  89. package/src/theme/buildTableCssVars.ts +4 -0
  90. package/src/theme/resolveTheme.spec.ts +13 -0
  91. package/src/theme/resolveTheme.ts +3 -0
  92. package/src/theme/types.ts +9 -0
  93. package/test/README.md +16 -10
  94. package/dist/ServerProvider-Dg5S1qBU.cjs.js +0 -1
  95. package/dist/hooks/useGivensForm.d.ts +0 -35
  96. package/dist/index-MES4MGpQ.cjs.js +0 -309
  97. package/src/hooks/useGivensForm.spec.ts +0 -99
  98. package/src/hooks/useGivensForm.ts +0 -107
@@ -0,0 +1,311 @@
1
+ /**
2
+ * Making a drillable cell *look* drillable.
3
+ *
4
+ * Resolution (`resolveDrill.ts`) answers what a click means; this answers the
5
+ * question that comes first, from the reader's side, which cells can be clicked
6
+ * at all. Without it a `# drill` is a hidden feature: the cells that navigate
7
+ * and the cells that do nothing render identically, and the only way to find one
8
+ * is to click at random.
9
+ *
10
+ * Ported from Malloyyo's `frame-runtime/drill.ts`, class name aside, so the same
11
+ * tagged model reads the same way in both: a drillable cell is ordinary text
12
+ * with a pointer cursor until hovered, at which point it reads as a link. The
13
+ * styling lives with Publisher's other renderer overrides in
14
+ * `RenderedResult.tsx`; this module only marks the DOM.
15
+ *
16
+ * Browser-only, and deliberately outside React: the cells belong to
17
+ * `@malloydata/render`, which offers no per-cell hook.
18
+ */
19
+
20
+ import type { DrillField } from "./resolveDrill";
21
+
22
+ /**
23
+ * Marks a leaf value cell whose column declares an honorable `# drill`.
24
+ * Malloyyo's counterpart is `dash-drill`; the name is local to each app's
25
+ * stylesheet, and only the tag grammar is a compatibility surface.
26
+ */
27
+ export const DRILL_CELL_CLASS = "publisher-drill";
28
+
29
+ /** As much of the renderer's viz handle as the affordance reads. */
30
+ export interface DrillMetadataSource {
31
+ getMetadata: () => { getAllFields: () => DrillField[] } | null | undefined;
32
+ }
33
+
34
+ /**
35
+ * The column names to mark: every field whose `# drill` this surface can honor.
36
+ *
37
+ * Both the field's own name and its `# label` go in, because the header cell
38
+ * this is matched against renders `getLabel()`: the label when the author set
39
+ * one, the name otherwise, and the caller cannot tell which from the DOM.
40
+ *
41
+ * `canDrill` is the host's capability filter (see `useDrill`), so a destination
42
+ * the surface cannot reach never becomes a link: a dead link is worse than none.
43
+ */
44
+ export function drillableFieldNames(
45
+ viz: DrillMetadataSource,
46
+ canDrill: (field: DrillField) => boolean,
47
+ ): Set<string> {
48
+ const names = new Set<string>();
49
+ let fields: DrillField[];
50
+ try {
51
+ fields = viz.getMetadata()?.getAllFields() ?? [];
52
+ } catch {
53
+ // Metadata unavailable: no affordance, but clicks still resolve.
54
+ return names;
55
+ }
56
+ // Header text that a NON-drillable field renders. A header shows the `# label`
57
+ // when the author set one and the field name otherwise, so that is the one
58
+ // spelling each of these can put on screen.
59
+ const taken = new Set<string>();
60
+ for (const field of fields) {
61
+ if (!field || canDrill(field)) continue;
62
+ // A `# hidden` field renders no header, so it cannot be confused with
63
+ // anything and must not suppress a drillable field's spelling. Counting
64
+ // it gave up the affordance on a column nothing else could collide with.
65
+ if (field.tag?.tag("hidden") !== undefined) continue;
66
+ taken.add(field.tag?.text("label") ?? field.name);
67
+ }
68
+ // STILL over-broad for nests: `getAllFields` is flat, so a field inside a
69
+ // nested view that renders no header of its own in THIS table is counted
70
+ // here too. Narrowing that needs the nesting structure, which this flat list
71
+ // does not carry, and erring toward suppression costs an affordance rather
72
+ // than painting a dead link.
73
+
74
+ for (const field of fields) {
75
+ if (!field || !canDrill(field)) continue;
76
+ // The ONE spelling this field can render, by the same rule `taken` uses
77
+ // above. Adding the raw `name` as well whenever a field had a label put a
78
+ // key in here that this field can never put on screen, so its only
79
+ // possible effect was to match some OTHER column whose header happened to
80
+ // equal this field's name, and paint that column as a link that does
81
+ // nothing. The two loops now agree about what a header shows.
82
+ //
83
+ // A spelling some non-drillable field also renders is dropped rather than
84
+ // marked. Matching on text cannot tell those two columns apart, and this
85
+ // file would rather lose an affordance than paint a dead link, which is
86
+ // the same reason `canDrill` filters at all.
87
+ const rendered = field.tag?.text("label") ?? field.name;
88
+ if (!taken.has(rendered)) names.add(rendered);
89
+ // KNOWN LIMITATION that remains: `markDrillableCells` walks every
90
+ // `.malloy-table` in the result, nested ones included, and matches this
91
+ // one flat set against all of them. Two fields that render the same text
92
+ // are handled above, but only because `getAllFields` reports both; a
93
+ // column this set never saw can still collide. The honest fix is to match
94
+ // on field identity rather than rendered text, which is a rewrite of the
95
+ // DOM walk rather than a patch to it.
96
+ }
97
+ return names;
98
+ }
99
+
100
+ /**
101
+ * The renderer gives a cell no field identity, but its normal and pivot tables
102
+ * lay out on a CSS grid with an inline `grid-column: N / …` per cell, so a
103
+ * header cell naming a drillable field identifies that field's column by
104
+ * number, and the body cells sharing the number are its values.
105
+ *
106
+ * NOT every table. A `# transpose` result sets `grid-template-columns` on the
107
+ * container and no `grid-column` on any cell (checked against the renderer's
108
+ * own bundle: zero such calls on that path against nine elsewhere), so this
109
+ * returns undefined for every cell and a transposed table is left unmarked.
110
+ * Its clicks still resolve, because the renderer hands them to `onClick`
111
+ * regardless, so the drill WORKS there and is simply undiscoverable. Marking it
112
+ * needs a second strategy keyed on the transpose layout rather than a patch to
113
+ * this one.
114
+ */
115
+ function gridColumnStart(element: HTMLElement): string | undefined {
116
+ const match = /^\s*(\d+)/.exec(element.style?.gridColumn ?? "");
117
+ return match ? match[1] : undefined;
118
+ }
119
+
120
+ /**
121
+ * Add {@link DRILL_CELL_CLASS} to the cells `names` makes clickable.
122
+ *
123
+ * Per table rather than per container: a nested table has its own grid and its
124
+ * own column numbering, so matching across them would mark whichever column of
125
+ * the child happened to share a number with the parent's.
126
+ *
127
+ * Idempotent, and safe to call repeatedly: a `# dashboard` result builds its
128
+ * cards over several frames, so the caller re-runs this as the DOM settles.
129
+ */
130
+ export function markDrillableCells(
131
+ container: HTMLElement,
132
+ names: Set<string>,
133
+ ): void {
134
+ if (names.size === 0) return;
135
+ for (const table of container.querySelectorAll<HTMLElement>(
136
+ ".malloy-table",
137
+ )) {
138
+ const ownedByTable = (element: Element) =>
139
+ element.closest(".malloy-table") === table;
140
+
141
+ const columns = new Set<string>();
142
+ for (const header of table.querySelectorAll<HTMLElement>(
143
+ ".column-cell.th",
144
+ )) {
145
+ if (!ownedByTable(header)) continue;
146
+ // The renderer inserts a zero-width space after each underscore in a
147
+ // header so long field names wrap; strip it before matching.
148
+ const text = (header.textContent ?? "").replace(/\u200b/g, "").trim();
149
+ const column = gridColumnStart(header);
150
+ if (column && names.has(text)) columns.add(column);
151
+ }
152
+ if (columns.size === 0) continue;
153
+
154
+ // Which columns already own their single tab stop. Read off the DOM first
155
+ // so the roving position survives the re-runs a settling `# dashboard`
156
+ // triggers; rebuilding it from scratch each pass would snap the stop back
157
+ // to the first row under a reader who had arrowed down.
158
+ const hasStop = new Set<string>();
159
+ for (const marked of table.querySelectorAll<HTMLElement>(
160
+ `.${DRILL_CELL_CLASS}[tabindex="0"]`,
161
+ )) {
162
+ // LOAD-BEARING, and a previous comment here claimed the opposite on
163
+ // the strength of a measurement that missed the case. This query is
164
+ // scoped to `table`, whose subtree contains any nested table, so
165
+ // without this line an outer pass sees a nested table's stop and
166
+ // demotes it as a duplicate of its own column. The nested pass then
167
+ // re-promotes, but it promotes the FIRST row, not the one the reader
168
+ // had roved to, so the restoration is not position-preserving.
169
+ //
170
+ // Measured both ways. Move the stop to the nested table's second row
171
+ // and re-mark: with this line, the stop stays there; without it, the
172
+ // stop is back on the nested table's first row. The earlier
173
+ // measurement moved the stop in the OUTER table, where the fallback
174
+ // lands on the same cell and the two are indistinguishable.
175
+ if (!ownedByTable(marked)) continue;
176
+ const column = gridColumnStart(marked);
177
+ if (!column) continue;
178
+ // The first one keeps the stop and a duplicate is DEMOTED, not left
179
+ // alone. Preserving every `tabindex="0"` it found meant a column that
180
+ // had somehow gained a second stop kept both through every re-mark,
181
+ // which is the accumulation this whole scheme exists to prevent.
182
+ if (hasStop.has(column)) marked.tabIndex = -1;
183
+ else hasStop.add(column);
184
+ }
185
+
186
+ for (const cell of table.querySelectorAll<HTMLElement>(
187
+ ".column-cell.td",
188
+ )) {
189
+ if (!ownedByTable(cell)) continue;
190
+ const column = gridColumnStart(cell);
191
+ // A cell with no value is not marked. `resolveDrill` refuses a null or
192
+ // empty value (clicking a blank cell is far likelier a misclick than a
193
+ // request for the rows with no value), so painting one as a link
194
+ // promised a click that was then dropped in silence.
195
+ //
196
+ // Structure first, TEXT second. Testing the text alone did not work:
197
+ // the renderer draws a null cell as the glyph `∅` inside a
198
+ // `.value-null` span, so its `textContent` is not empty and every null
199
+ // cell stayed marked, which is the exact case this guard was added for.
200
+ // The U+200B strip is for the renderer's own soft-wrap hints inside
201
+ // long HEADER names, not for values, and stripping it here made this
202
+ // disagree with the resolver in the other direction: a cell whose
203
+ // value IS a zero-width space is refused an affordance while
204
+ // `drillValueToFilter` accepts it and drills fine. `trim()` already
205
+ // leaves U+200B alone, which is the boundary `filterValue` documents.
206
+ const hasValue =
207
+ !cell.querySelector(".value-null") &&
208
+ (cell.textContent ?? "").trim() !== "";
209
+ // Leaf value cells only: a cell wrapping a nested table is structure,
210
+ // and its click lands on the inner cell anyway.
211
+ if (
212
+ column &&
213
+ hasValue &&
214
+ columns.has(column) &&
215
+ !cell.querySelector(".malloy-table")
216
+ ) {
217
+ cell.classList.add(DRILL_CELL_CLASS);
218
+ // Reachable without a mouse. The class alone gave a pointer cursor
219
+ // and a hover colour, both of which a keyboard user never sees and
220
+ // a touch user cannot produce, so the drill was discoverable only
221
+ // by clicking at random. `tabindex` puts the cell in the tab order
222
+ // and `role` tells a screen reader it does something; the cell's
223
+ // own text is its accessible name, which is the value that gets
224
+ // drilled on, so no `aria-label` is invented here.
225
+ //
226
+ // `button` rather than `link`: a `to=self` drill filters in place
227
+ // and does not navigate, and one tag can offer both, so the honest
228
+ // role is the one that promises less. Set alongside the class and
229
+ // never separately, so the three can never disagree about which
230
+ // cells are actionable.
231
+ // ONE tab stop per drillable column, not one per cell. Making every
232
+ // cell tabbable put the whole column in the tab order, so a result at
233
+ // the server's row cap cost a reader hundreds of presses to reach the
234
+ // next tile, and a composite grid multiplied that by its tiles. That
235
+ // traded one accessibility problem for another. Arrow keys move
236
+ // within the column instead (see `drillColumnSiblings`, driven from
237
+ // RenderedResult), which is the ordinary pattern for a grid.
238
+ //
239
+ // A cell already holding the stop keeps it, so a re-run does not move
240
+ // focus out from under the reader.
241
+ if (cell.tabIndex === 0) {
242
+ hasStop.add(column);
243
+ } else {
244
+ const owned = hasStop.has(column);
245
+ if (!owned) hasStop.add(column);
246
+ cell.tabIndex = owned ? -1 : 0;
247
+ }
248
+ cell.setAttribute("role", "button");
249
+ }
250
+ }
251
+ }
252
+ }
253
+
254
+ /**
255
+ * The drillable cells sharing `cell`'s column, in row order, or an empty array
256
+ * when `cell` is not a marked cell.
257
+ *
258
+ * Lives here rather than in the keyboard handler that calls it because the
259
+ * column identity is this module's rule: a cell's column is its inline
260
+ * `grid-column` start, scoped to its own `.malloy-table` so a nested table's
261
+ * numbering never matches the parent's. Two sites deciding that separately is
262
+ * how they come to disagree, so the handler asks this one.
263
+ */
264
+ export function drillColumnSiblings(cell: HTMLElement): HTMLElement[] {
265
+ const table = cell.closest<HTMLElement>(".malloy-table");
266
+ const column = gridColumnStart(cell);
267
+ if (!table || !column) return [];
268
+ return Array.from(
269
+ table.querySelectorAll<HTMLElement>(`.${DRILL_CELL_CLASS}`),
270
+ ).filter(
271
+ (sibling) =>
272
+ sibling.closest(".malloy-table") === table &&
273
+ gridColumnStart(sibling) === column,
274
+ );
275
+ }
276
+
277
+ /**
278
+ * Move a drillable column's single tab stop `to` rows from `from`, or to the
279
+ * first or last cell, and return the cell that should now take focus.
280
+ *
281
+ * Lives beside {@link drillColumnSiblings} because it enforces the same
282
+ * invariant the marking does: exactly one cell per column carries
283
+ * `tabindex="0"`.
284
+ */
285
+ export function moveDrillStop(
286
+ from: HTMLElement,
287
+ to: number | "first" | "last",
288
+ ): HTMLElement | undefined {
289
+ const siblings = drillColumnSiblings(from);
290
+ const at = siblings.indexOf(from);
291
+ if (at === -1) return undefined;
292
+ const next =
293
+ to === "first"
294
+ ? siblings[0]
295
+ : to === "last"
296
+ ? siblings[siblings.length - 1]
297
+ : siblings[Math.min(Math.max(at + to, 0), siblings.length - 1)];
298
+ if (!next) return undefined;
299
+ // EVERY sibling is rewritten, not just `from`. A cell with `tabindex="-1"`
300
+ // is still click-focusable, so `from` is frequently NOT the cell holding the
301
+ // stop: clicking a cell mid-column and then pressing an arrow used to clear
302
+ // `from` (already -1, a no-op) and promote its neighbour, leaving the column
303
+ // with two stops. The marking pass then preserved both, so it stuck, and
304
+ // repeating the gesture accumulated more.
305
+ //
306
+ // Rewriting all of them also means a clamped move at either end, where
307
+ // `next === from`, repairs a column that had drifted rather than doing
308
+ // nothing.
309
+ for (const sibling of siblings) sibling.tabIndex = sibling === next ? 0 : -1;
310
+ return next;
311
+ }