@malloy-publisher/sdk 0.0.382-dev → 0.2.1

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