@malloy-publisher/sdk 0.9.1 → 0.9.2

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 (292) hide show
  1. package/dist/{Dashboard-H1kJlDFo.es.js → DashboardTile-BFcxdv7R.es.js} +15918 -17760
  2. package/dist/DashboardTile-sg8LjUk8.cjs.js +137 -0
  3. package/dist/RenderedResult-B48wAjM8.cjs.js +141 -0
  4. package/dist/{RenderedResult-BRgIgxWh.es.js → RenderedResult-CBLHr32C.es.js} +193 -183
  5. package/dist/{ServerProvider-Boxrv6Ym.es.js → ServerProvider-DCV8BUWD.es.js} +529 -521
  6. package/dist/ServerProvider-HRqT3w1C.cjs.js +1 -0
  7. package/dist/builder/index.cjs.js +146 -58
  8. package/dist/builder/index.es.js +9076 -6479
  9. package/dist/builder-entry.d.ts +4 -0
  10. package/dist/client/api.d.ts +77 -47
  11. package/dist/client/configuration.d.ts +1 -1
  12. package/dist/client/index.cjs.js +1 -1
  13. package/dist/client/index.d.ts +1 -1
  14. package/dist/client/index.es.js +84 -82
  15. package/dist/components/Dashboard/Dashboard.d.ts +8 -15
  16. package/dist/components/Dashboard/DashboardGrid.d.ts +23 -6
  17. package/dist/components/Dashboard/DashboardTile.d.ts +10 -1
  18. package/dist/components/Dashboard/DashboardView.d.ts +36 -0
  19. package/dist/components/Dashboard/TileCard.d.ts +15 -4
  20. package/dist/components/Dashboard/TileFilterTag.d.ts +12 -0
  21. package/dist/components/Dashboard/index.d.ts +3 -0
  22. package/dist/components/Dashboard/telemetry.d.ts +15 -3
  23. package/dist/components/DashboardBuilder/AddTileDialog.d.ts +8 -1
  24. package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +16 -5
  25. package/dist/components/DashboardBuilder/ChartPicker.d.ts +29 -0
  26. package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +48 -21
  27. package/dist/components/DashboardBuilder/DashboardEditor.d.ts +17 -4
  28. package/dist/components/DashboardBuilder/DiffDialog.d.ts +2 -10
  29. package/dist/components/DashboardBuilder/FilterDialog.d.ts +2 -2
  30. package/dist/components/DashboardBuilder/FilterStrip.d.ts +3 -1
  31. package/dist/components/DashboardBuilder/InlineMarkdown.d.ts +18 -0
  32. package/dist/components/DashboardBuilder/InlineText.d.ts +19 -0
  33. package/dist/components/DashboardBuilder/NotebookEditor.d.ts +15 -0
  34. package/dist/components/DashboardBuilder/SaveNotice.d.ts +27 -0
  35. package/dist/components/DashboardBuilder/SettingsPopover.d.ts +19 -7
  36. package/dist/components/DashboardBuilder/SourceViewPicker.d.ts +9 -0
  37. package/dist/components/DashboardBuilder/TextTileBody.d.ts +7 -0
  38. package/dist/components/DashboardBuilder/TileFrame.d.ts +14 -12
  39. package/dist/components/DashboardBuilder/TileMenu.d.ts +11 -3
  40. package/dist/components/DashboardBuilder/annotationText.d.ts +4 -0
  41. package/dist/components/DashboardBuilder/catalog.d.ts +4 -0
  42. package/dist/components/DashboardBuilder/changedTile.d.ts +7 -0
  43. package/dist/components/DashboardBuilder/chartLine.d.ts +33 -0
  44. package/dist/components/DashboardBuilder/controls.d.ts +2 -2
  45. package/dist/components/DashboardBuilder/document.d.ts +42 -26
  46. package/dist/components/DashboardBuilder/documentSession.d.ts +100 -0
  47. package/dist/components/DashboardBuilder/legacyNotebook.d.ts +73 -0
  48. package/dist/components/DashboardBuilder/loadMalloy.d.ts +15 -0
  49. package/dist/components/DashboardBuilder/malloyText.d.ts +45 -5
  50. package/dist/components/DashboardBuilder/malloyTree.d.ts +119 -0
  51. package/dist/components/DashboardBuilder/newDashboard.d.ts +3 -1
  52. package/dist/components/DashboardBuilder/openDraft.d.ts +10 -0
  53. package/dist/components/DashboardBuilder/preview.d.ts +24 -4
  54. package/dist/components/DashboardBuilder/readDocument.d.ts +4 -1
  55. package/dist/components/DashboardBuilder/readForEditor.d.ts +16 -0
  56. package/dist/components/DashboardBuilder/sortable.d.ts +14 -0
  57. package/dist/components/DashboardBuilder/spliceDocument.d.ts +13 -13
  58. package/dist/components/DashboardBuilder/spliceResult.d.ts +11 -0
  59. package/dist/components/DashboardBuilder/tagParse.d.ts +24 -0
  60. package/dist/components/DashboardBuilder/telemetry.d.ts +43 -0
  61. package/dist/components/DashboardBuilder/testing/fixtures.d.ts +3 -1
  62. package/dist/components/DashboardBuilder/testing/inline.d.ts +4 -0
  63. package/dist/components/DashboardBuilder/useBuilderSession.d.ts +89 -0
  64. package/dist/components/DashboardBuilder/useBuilderShortcuts.d.ts +9 -6
  65. package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +17 -55
  66. package/dist/components/DashboardBuilder/useDocumentEditor.d.ts +105 -0
  67. package/dist/components/DashboardBuilder/useExitGuard.d.ts +19 -0
  68. package/dist/components/DashboardBuilder/useFilterForm.d.ts +3 -3
  69. package/dist/components/DocumentCreate/canRetry.d.ts +6 -0
  70. package/dist/components/DocumentCreate/createDocument.d.ts +58 -0
  71. package/dist/components/DocumentCreate/documentPath.d.ts +10 -0
  72. package/dist/components/DocumentCreate/events.d.ts +10 -0
  73. package/dist/components/DocumentCreate/exportedSources.d.ts +7 -0
  74. package/dist/components/DocumentCreate/guards.d.ts +12 -0
  75. package/dist/components/DocumentCreate/index.d.ts +6 -0
  76. package/dist/components/DocumentCreate/newNotebook.d.ts +9 -0
  77. package/dist/components/DocumentCreate/useDocumentChoices.d.ts +32 -0
  78. package/dist/components/DocumentStorage/DocumentStorage.d.ts +2 -1
  79. package/dist/components/NarrowEditGate.d.ts +8 -0
  80. package/dist/components/Notebook/Notebook.d.ts +1 -1
  81. package/dist/components/Package/NewDocumentDialog.d.ts +33 -0
  82. package/dist/components/Package/Package.d.ts +1 -0
  83. package/dist/components/Package/documentLocation.d.ts +28 -0
  84. package/dist/components/Package/index.d.ts +4 -0
  85. package/dist/components/Package/useDocumentLocation.d.ts +12 -0
  86. package/dist/components/PackageSection.d.ts +2 -2
  87. package/dist/components/RenderedResult/RenderedResult.d.ts +2 -0
  88. package/dist/components/RenderedResult/ResultContainer.d.ts +8 -1
  89. package/dist/components/RenderedResult/ResultPanel.d.ts +3 -1
  90. package/dist/components/RenderedResult/resultSizing.d.ts +9 -1
  91. package/dist/components/ServerProvider.d.ts +2 -1
  92. package/dist/components/UnsavedChangesDialog.d.ts +9 -0
  93. package/dist/components/given/GivensPanel.d.ts +2 -2
  94. package/dist/components/index.d.ts +2 -0
  95. package/dist/hooks/index.d.ts +1 -0
  96. package/dist/hooks/useNarrowScreen.d.ts +4 -0
  97. package/dist/hooks/useQueryResult.d.ts +2 -0
  98. package/dist/hooks/useSettled.d.ts +14 -0
  99. package/dist/index.cjs.js +23 -24
  100. package/dist/index.es.js +9714 -7072
  101. package/dist/text/index.cjs.js +1 -0
  102. package/dist/text/index.d.ts +2 -0
  103. package/dist/text/index.es.js +6 -0
  104. package/dist/text-entry-BJwltZiy.cjs.js +7 -0
  105. package/dist/text-entry-CbbBcZl8.es.js +196 -0
  106. package/dist/text-entry.d.ts +2 -0
  107. package/dist/utils/clock.d.ts +2 -0
  108. package/package.json +10 -1
  109. package/src/builder-entry.ts +35 -0
  110. package/src/components/Dashboard/Dashboard.tsx +27 -278
  111. package/src/components/Dashboard/DashboardGrid.tsx +68 -7
  112. package/src/components/Dashboard/DashboardTile.spec.tsx +24 -0
  113. package/src/components/Dashboard/DashboardTile.tsx +31 -4
  114. package/src/components/Dashboard/DashboardView.spec.tsx +212 -0
  115. package/src/components/Dashboard/DashboardView.tsx +352 -0
  116. package/src/components/Dashboard/RowsDialog.tsx +1 -1
  117. package/src/components/Dashboard/TileCard.tsx +44 -10
  118. package/src/components/Dashboard/TileFilterTag.spec.tsx +73 -0
  119. package/src/components/Dashboard/TileFilterTag.tsx +78 -0
  120. package/src/components/Dashboard/defaultTileSpan.spec.ts +32 -0
  121. package/src/components/Dashboard/index.ts +3 -0
  122. package/src/components/Dashboard/telemetry.ts +16 -5
  123. package/src/components/DashboardBuilder/AddTileDialog.spec.tsx +88 -0
  124. package/src/components/DashboardBuilder/AddTileDialog.tsx +137 -111
  125. package/src/components/DashboardBuilder/BuilderToolbar.tsx +85 -27
  126. package/src/components/DashboardBuilder/ChartPicker.render.spec.tsx +25 -0
  127. package/src/components/DashboardBuilder/ChartPicker.spec.ts +77 -0
  128. package/src/components/DashboardBuilder/ChartPicker.tsx +153 -0
  129. package/src/components/DashboardBuilder/DashboardBuilder.charts.spec.tsx +205 -0
  130. package/src/components/DashboardBuilder/DashboardBuilder.exit.spec.tsx +192 -0
  131. package/src/components/DashboardBuilder/DashboardBuilder.inline.spec.tsx +329 -0
  132. package/src/components/DashboardBuilder/DashboardBuilder.notebook.spec.tsx +363 -0
  133. package/src/components/DashboardBuilder/DashboardBuilder.notice.spec.tsx +194 -0
  134. package/src/components/DashboardBuilder/DashboardBuilder.openDraft.spec.tsx +157 -0
  135. package/src/components/DashboardBuilder/DashboardBuilder.sources.spec.tsx +124 -0
  136. package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +103 -78
  137. package/src/components/DashboardBuilder/DashboardBuilder.textTiles.spec.tsx +177 -0
  138. package/src/components/DashboardBuilder/DashboardBuilder.tsx +610 -315
  139. package/src/components/DashboardBuilder/DashboardEditor.authoritative.spec.tsx +58 -8
  140. package/src/components/DashboardBuilder/DashboardEditor.emptyStart.spec.tsx +191 -0
  141. package/src/components/DashboardBuilder/DashboardEditor.filterTag.spec.tsx +162 -0
  142. package/src/components/DashboardBuilder/DashboardEditor.notebook.spec.tsx +316 -0
  143. package/src/components/DashboardBuilder/DashboardEditor.package.spec.tsx +56 -10
  144. package/src/components/DashboardBuilder/DashboardEditor.resourceUri.spec.tsx +5 -22
  145. package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +96 -14
  146. package/src/components/DashboardBuilder/DashboardEditor.tsx +482 -332
  147. package/src/components/DashboardBuilder/DiffDialog.spec.tsx +25 -0
  148. package/src/components/DashboardBuilder/DiffDialog.tsx +5 -24
  149. package/src/components/DashboardBuilder/DrillDialog.tsx +1 -1
  150. package/src/components/DashboardBuilder/FilterDialog.tsx +10 -3
  151. package/src/components/DashboardBuilder/FilterStrip.tsx +26 -4
  152. package/src/components/DashboardBuilder/InlineMarkdown.tsx +184 -0
  153. package/src/components/DashboardBuilder/InlineText.tsx +106 -0
  154. package/src/components/DashboardBuilder/NotebookEditor.tsx +44 -0
  155. package/src/components/DashboardBuilder/SaveNotice.tsx +113 -0
  156. package/src/components/DashboardBuilder/SettingsPopover.spec.tsx +87 -0
  157. package/src/components/DashboardBuilder/SettingsPopover.tsx +225 -67
  158. package/src/components/DashboardBuilder/SourceViewPicker.tsx +122 -0
  159. package/src/components/DashboardBuilder/TextTileBody.tsx +28 -0
  160. package/src/components/DashboardBuilder/TileFrame.tsx +89 -115
  161. package/src/components/DashboardBuilder/TileMenu.tsx +168 -90
  162. package/src/components/DashboardBuilder/annotationText.spec.ts +33 -0
  163. package/src/components/DashboardBuilder/annotationText.ts +17 -0
  164. package/src/components/DashboardBuilder/artifactBlock.spec.ts +248 -0
  165. package/src/components/DashboardBuilder/artifactTagParity.spec.ts +41 -0
  166. package/src/components/DashboardBuilder/catalog.aggregate.spec.ts +49 -0
  167. package/src/components/DashboardBuilder/catalog.spec.ts +2 -0
  168. package/src/components/DashboardBuilder/catalog.ts +61 -21
  169. package/src/components/DashboardBuilder/changedTile.spec.ts +46 -0
  170. package/src/components/DashboardBuilder/changedTile.ts +28 -0
  171. package/src/components/DashboardBuilder/chartLine.spec.ts +180 -0
  172. package/src/components/DashboardBuilder/chartLine.ts +121 -0
  173. package/src/components/DashboardBuilder/controls.spec.ts +21 -10
  174. package/src/components/DashboardBuilder/controls.ts +22 -8
  175. package/src/components/DashboardBuilder/document.ts +53 -5
  176. package/src/components/DashboardBuilder/documentSession.spec.ts +209 -0
  177. package/src/components/DashboardBuilder/documentSession.ts +257 -0
  178. package/src/components/DashboardBuilder/emptyStart.spec.ts +131 -0
  179. package/src/components/DashboardBuilder/invariants.spec.ts +6 -2
  180. package/src/components/DashboardBuilder/legacyNotebook.chart.spec.ts +77 -0
  181. package/src/components/DashboardBuilder/legacyNotebook.convert.spec.ts +630 -0
  182. package/src/components/DashboardBuilder/legacyNotebook.read.spec.ts +338 -0
  183. package/src/components/DashboardBuilder/legacyNotebook.ts +1117 -0
  184. package/src/components/DashboardBuilder/loadMalloy.spec.ts +80 -0
  185. package/src/components/DashboardBuilder/loadMalloy.ts +39 -0
  186. package/src/components/DashboardBuilder/malloyText.spec.ts +48 -0
  187. package/src/components/DashboardBuilder/malloyText.ts +181 -27
  188. package/src/components/DashboardBuilder/malloyTree.ts +155 -7
  189. package/src/components/DashboardBuilder/newDashboard.spec.ts +46 -2
  190. package/src/components/DashboardBuilder/newDashboard.ts +30 -3
  191. package/src/components/DashboardBuilder/openDraft.ts +52 -0
  192. package/src/components/DashboardBuilder/preview.shapes.spec.ts +75 -0
  193. package/src/components/DashboardBuilder/preview.spec.ts +159 -12
  194. package/src/components/DashboardBuilder/preview.ts +83 -9
  195. package/src/components/DashboardBuilder/readDocument.spec.ts +139 -27
  196. package/src/components/DashboardBuilder/readDocument.ts +130 -44
  197. package/src/components/DashboardBuilder/readForEditor.spec.ts +80 -0
  198. package/src/components/DashboardBuilder/readForEditor.ts +47 -0
  199. package/src/components/DashboardBuilder/scannerShapes.spec.ts +14 -13
  200. package/src/components/DashboardBuilder/sortable.spec.ts +65 -0
  201. package/src/components/DashboardBuilder/sortable.tsx +2 -12
  202. package/src/components/DashboardBuilder/spliceDocument.chart.spec.ts +340 -0
  203. package/src/components/DashboardBuilder/spliceDocument.imports.spec.ts +167 -0
  204. package/src/components/DashboardBuilder/spliceDocument.spec.ts +229 -94
  205. package/src/components/DashboardBuilder/spliceDocument.ts +762 -130
  206. package/src/components/DashboardBuilder/spliceResult.ts +15 -0
  207. package/src/components/DashboardBuilder/statementExtent.spec.ts +33 -30
  208. package/src/components/DashboardBuilder/tagParse.spec.ts +149 -0
  209. package/src/components/DashboardBuilder/tagParse.ts +103 -0
  210. package/src/components/DashboardBuilder/telemetry.ts +47 -0
  211. package/src/components/DashboardBuilder/testing/artifactTagParity.json +197 -0
  212. package/src/components/DashboardBuilder/testing/fixtures.ts +16 -1
  213. package/src/components/DashboardBuilder/testing/inline.ts +25 -0
  214. package/src/components/DashboardBuilder/textTiles.spec.ts +718 -0
  215. package/src/components/DashboardBuilder/useBuilderSession.spec.ts +335 -0
  216. package/src/components/DashboardBuilder/useBuilderSession.ts +292 -0
  217. package/src/components/DashboardBuilder/useBuilderShortcuts.overlay.spec.tsx +65 -0
  218. package/src/components/DashboardBuilder/useBuilderShortcuts.spec.tsx +60 -0
  219. package/src/components/DashboardBuilder/useBuilderShortcuts.ts +36 -10
  220. package/src/components/DashboardBuilder/useDashboardEditor.spec.ts +16 -13
  221. package/src/components/DashboardBuilder/useDashboardEditor.ts +54 -181
  222. package/src/components/DashboardBuilder/useDocumentEditor.spec.ts +565 -0
  223. package/src/components/DashboardBuilder/useDocumentEditor.ts +399 -0
  224. package/src/components/DashboardBuilder/useExitGuard.spec.ts +159 -0
  225. package/src/components/DashboardBuilder/useExitGuard.ts +89 -0
  226. package/src/components/DashboardBuilder/useFilterForm.existing.spec.tsx +41 -0
  227. package/src/components/DashboardBuilder/useFilterForm.spec.tsx +24 -2
  228. package/src/components/DashboardBuilder/useFilterForm.ts +41 -13
  229. package/src/components/DashboardBuilder/useTileReorder.spec.tsx +28 -5
  230. package/src/components/DashboardBuilder/useTileReorder.ts +5 -7
  231. package/src/components/DocumentCreate/canRetry.spec.ts +32 -0
  232. package/src/components/DocumentCreate/canRetry.ts +19 -0
  233. package/src/components/DocumentCreate/createDocument.spec.ts +310 -0
  234. package/src/components/DocumentCreate/createDocument.ts +159 -0
  235. package/src/components/DocumentCreate/documentPath.ts +40 -0
  236. package/src/components/DocumentCreate/events.ts +15 -0
  237. package/src/components/DocumentCreate/exportedSources.ts +25 -0
  238. package/src/components/DocumentCreate/guards.ts +45 -0
  239. package/src/components/DocumentCreate/index.ts +27 -0
  240. package/src/components/DocumentCreate/newNotebook.spec.ts +156 -0
  241. package/src/components/DocumentCreate/newNotebook.ts +44 -0
  242. package/src/components/DocumentCreate/useDocumentChoices.spec.tsx +138 -0
  243. package/src/components/DocumentCreate/useDocumentChoices.ts +113 -0
  244. package/src/components/DocumentStorage/DocumentStorage.ts +2 -1
  245. package/src/components/Model/ModelExplorer.spec.tsx +2 -2
  246. package/src/components/Model/SourcesExplorer.tsx +2 -4
  247. package/src/components/NarrowEditGate.spec.tsx +73 -0
  248. package/src/components/NarrowEditGate.tsx +29 -0
  249. package/src/components/Notebook/Notebook.layout.spec.tsx +94 -0
  250. package/src/components/Notebook/Notebook.tsx +66 -27
  251. package/src/components/Package/NewDocumentDialog.spec.tsx +749 -0
  252. package/src/components/Package/NewDocumentDialog.tsx +401 -0
  253. package/src/components/Package/Package.artifacts.spec.tsx +190 -0
  254. package/src/components/Package/Package.create.spec.tsx +380 -0
  255. package/src/components/Package/Package.notebooks.spec.tsx +18 -0
  256. package/src/components/Package/Package.tsx +366 -171
  257. package/src/components/Package/documentLocation.spec.ts +81 -0
  258. package/src/components/Package/documentLocation.ts +55 -0
  259. package/src/components/Package/index.ts +13 -0
  260. package/src/components/Package/useDocumentLocation.ts +59 -0
  261. package/src/components/PackageSection.tsx +2 -2
  262. package/src/components/RenderedResult/RenderedResult.tsx +18 -1
  263. package/src/components/RenderedResult/ResultContainer.tsx +44 -11
  264. package/src/components/RenderedResult/ResultPanel.tsx +4 -0
  265. package/src/components/RenderedResult/resultSizing.spec.ts +68 -0
  266. package/src/components/RenderedResult/resultSizing.ts +14 -3
  267. package/src/components/ServerProvider.tsx +2 -1
  268. package/src/components/UnsavedChangesDialog.spec.tsx +70 -0
  269. package/src/components/UnsavedChangesDialog.tsx +49 -0
  270. package/src/components/given/GivensPanel.spec.tsx +6 -6
  271. package/src/components/given/GivensPanel.tsx +3 -3
  272. package/src/components/index.ts +2 -0
  273. package/src/hooks/index.ts +3 -0
  274. package/src/hooks/useNarrowScreen.ts +21 -0
  275. package/src/hooks/useQueryResult.ts +16 -0
  276. package/src/hooks/useSettled.spec.ts +41 -0
  277. package/src/hooks/useSettled.ts +27 -0
  278. package/src/mainEntry.spec.ts +145 -0
  279. package/src/text-entry.ts +12 -0
  280. package/src/textEntry.spec.ts +118 -0
  281. package/src/utils/clock.spec.ts +12 -0
  282. package/src/utils/clock.ts +6 -0
  283. package/vite.config.ts +4 -0
  284. package/dist/Dashboard-ChyylkTD.cjs.js +0 -136
  285. package/dist/RenderedResult-AJPMUkAD.cjs.js +0 -135
  286. package/dist/ServerProvider-CXrOKD6g.cjs.js +0 -1
  287. package/dist/components/DashboardBuilder/useTileResize.d.ts +0 -29
  288. package/dist/components/Package/NewDashboardDialog.d.ts +0 -18
  289. package/src/components/DashboardBuilder/useTileResize.spec.tsx +0 -121
  290. package/src/components/DashboardBuilder/useTileResize.ts +0 -110
  291. package/src/components/Package/NewDashboardDialog.spec.tsx +0 -129
  292. package/src/components/Package/NewDashboardDialog.tsx +0 -265
@@ -0,0 +1,60 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { cleanup, fireEvent, render, screen } from "@testing-library/react";
5
+ import { afterEach, describe, expect, it, mock } from "bun:test";
6
+ import { useState } from "react";
7
+ import { isMac, useBuilderShortcuts } from "./useBuilderShortcuts";
8
+
9
+ const mod = isMac ? { metaKey: true } : { ctrlKey: true };
10
+
11
+ // Save reads the rendered state, as a builder's save reads its render closure.
12
+ function Harness({
13
+ onSave,
14
+ withSave = true,
15
+ }: {
16
+ onSave: (seen: string) => void;
17
+ withSave?: boolean;
18
+ }) {
19
+ const [value, setValue] = useState("");
20
+ const [committed, setCommitted] = useState("");
21
+ useBuilderShortcuts({
22
+ undo: () => {},
23
+ redo: () => {},
24
+ ...(withSave ? { save: () => onSave(committed) } : {}),
25
+ escape: () => {},
26
+ });
27
+ return (
28
+ <input
29
+ aria-label="Field"
30
+ value={value}
31
+ onChange={(event) => setValue(event.target.value)}
32
+ onBlur={() => setCommitted(value)}
33
+ />
34
+ );
35
+ }
36
+
37
+ afterEach(cleanup);
38
+
39
+ describe("useBuilderShortcuts: save inside a text field", () => {
40
+ it("leaves the field, then saves with what the blur committed", () => {
41
+ const onSave = mock((_seen: string) => {});
42
+ render(<Harness onSave={onSave} />);
43
+ const field = screen.getByLabelText("Field");
44
+ field.focus();
45
+ fireEvent.change(field, { target: { value: "typed" } });
46
+ fireEvent.keyDown(field, { key: "s", ...mod });
47
+ expect(onSave).toHaveBeenCalledTimes(1);
48
+ expect(onSave.mock.calls[0][0]).toBe("typed");
49
+ });
50
+
51
+ it("leaves the field alone when there is nothing to save to", () => {
52
+ const onSave = mock((_seen: string) => {});
53
+ render(<Harness onSave={onSave} withSave={false} />);
54
+ const field = screen.getByLabelText("Field");
55
+ field.focus();
56
+ fireEvent.keyDown(field, { key: "s", ...mod });
57
+ expect(onSave).not.toHaveBeenCalled();
58
+ expect(document.activeElement).toBe(field);
59
+ });
60
+ });
@@ -1,29 +1,31 @@
1
1
  // Copyright (c) Credible Data Inc.
2
2
  // SPDX-License-Identifier: MIT
3
3
 
4
- import { useEffect, useRef } from "react";
4
+ import { useEffect, useRef, useState } from "react";
5
5
 
6
6
  /**
7
7
  * The keyboard an editor is expected to have.
8
8
  *
9
9
  * Undo and redo on the platform's usual keys, save on ⌘S / Ctrl+S (which the
10
10
  * browser would otherwise take), Escape to drop the selection, and the arrow
11
- * keys to nudge the selected tile's width a column at a time — the one layout
12
- * edit that is otherwise drag-only.
11
+ * keys to nudge the selected tile's width a column at a time.
13
12
  *
14
- * Nothing but Escape fires while a text field has focus, so typing into a
15
- * title never undoes a layout.
13
+ * Nothing but Escape and save fires while a text field has focus, so typing
14
+ * into a title never undoes a layout. Save leaves the field first, so its
15
+ * blur commits the draft, and runs once that edit has rendered.
16
16
  */
17
17
  export interface BuilderShortcutHandlers {
18
18
  undo: () => void;
19
19
  redo: () => void;
20
20
  save?: () => void;
21
21
  escape: () => void;
22
- /** Nudge the selected tile's width by ±1 column. */
23
- nudge: (delta: 1 | -1) => void;
22
+ /** Nudge the selected tile's width by ±1 column. Absent, the arrow keys are left alone. */
23
+ nudge?: (delta: 1 | -1) => void;
24
+ /** A modal owns the keyboard: no shortcut fires, so none can open a second dialog over it. */
25
+ paused?: boolean;
24
26
  }
25
27
 
26
- const isMac =
28
+ export const isMac =
27
29
  typeof navigator !== "undefined" &&
28
30
  /Mac|iPhone|iPad/.test(navigator.platform);
29
31
 
@@ -37,6 +39,11 @@ const inTextEntry = (target: EventTarget | null) => {
37
39
  return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT";
38
40
  };
39
41
 
42
+ const inOverlay = (target: EventTarget | null) =>
43
+ target instanceof Element &&
44
+ target.closest('[role="dialog"], .MuiPopover-root, .MuiDialog-root') !==
45
+ null;
46
+
40
47
  export function useBuilderShortcuts(handlers: BuilderShortcutHandlers) {
41
48
  // The handlers are read at keypress time, not bound at subscription time:
42
49
  // the caller passes a fresh object every render, and a listener re-bound on
@@ -46,16 +53,34 @@ export function useBuilderShortcuts(handlers: BuilderShortcutHandlers) {
46
53
  latest.current = handlers;
47
54
  });
48
55
 
56
+ const [saveRequested, setSaveRequested] = useState(false);
57
+ // After the render, because save reads the document of the render it was made in.
58
+ useEffect(() => {
59
+ if (!saveRequested) return;
60
+ setSaveRequested(false);
61
+ latest.current.save?.();
62
+ }, [saveRequested]);
63
+
49
64
  useEffect(() => {
50
65
  const onKey = (event: KeyboardEvent) => {
51
66
  const current = latest.current;
67
+ if (current.paused) return;
52
68
  const mod = isMac ? event.metaKey : event.ctrlKey;
53
69
  const key = event.key.toLowerCase();
54
70
  if (event.key === "Escape") {
55
71
  current.escape();
56
72
  return;
57
73
  }
58
- if (inTextEntry(event.target)) return;
74
+ if (inTextEntry(event.target)) {
75
+ if (mod && key === "s") {
76
+ event.preventDefault();
77
+ // A dialog's draft commits on close, so a save here would report Saved without it.
78
+ if (!current.save || inOverlay(event.target)) return;
79
+ (event.target as HTMLElement).blur();
80
+ setSaveRequested(true);
81
+ }
82
+ return;
83
+ }
59
84
  if (mod && key === "z") {
60
85
  event.preventDefault();
61
86
  if (event.shiftKey) current.redo();
@@ -69,10 +94,11 @@ export function useBuilderShortcuts(handlers: BuilderShortcutHandlers) {
69
94
  }
70
95
  if (mod && key === "s") {
71
96
  event.preventDefault();
72
- current.save?.();
97
+ if (!inOverlay(event.target)) current.save?.();
73
98
  return;
74
99
  }
75
100
  if (
101
+ current.nudge &&
76
102
  !mod &&
77
103
  !event.altKey &&
78
104
  !event.shiftKey &&
@@ -5,6 +5,8 @@ import { act, renderHook, waitFor } from "@testing-library/react";
5
5
  import { describe, expect, it } from "bun:test";
6
6
  import { openDocument } from "./testing/fixtures";
7
7
  import { useDashboardEditor } from "./useDashboardEditor";
8
+ import { queryTile } from "./testing/fixtures";
9
+ import { tileKey } from "./document";
8
10
 
9
11
  const SOURCE = `## artifact { title="Probe" tiles=["a -> by_cat", "a -> by_brand"] } dashboard { columns=12 }
10
12
  import "../data_app.malloy"
@@ -36,14 +38,16 @@ describe("useDashboardEditor: editing", () => {
36
38
 
37
39
  act(() => {
38
40
  view.result.current.update((d) => {
39
- d.tiles[0].label = "Categories";
41
+ queryTile(d, 0).label = "Categories";
40
42
  });
41
43
  });
42
44
 
43
- expect(view.result.current.document.tiles[0].label).toBe("Categories");
45
+ expect(queryTile(view.result.current.document, 0).label).toBe(
46
+ "Categories",
47
+ );
44
48
  // The history entry it replaced is untouched, which is what makes undo a
45
49
  // move rather than a reconstruction.
46
- expect(before.tiles[0].label).toBe("By category");
50
+ expect(queryTile(before, 0).label).toBe("By category");
47
51
  });
48
52
 
49
53
  it("is dirty only once something actually changed", async () => {
@@ -64,7 +68,7 @@ describe("useDashboardEditor: editing", () => {
64
68
  const view = await editor();
65
69
  act(() => {
66
70
  view.result.current.update((d) => {
67
- d.tiles[0].label = "By category";
71
+ queryTile(d, 0).label = "By category";
68
72
  });
69
73
  });
70
74
  expect(view.result.current.canUndo).toBe(false);
@@ -145,7 +149,7 @@ describe("useDashboardEditor: saving", () => {
145
149
 
146
150
  act(() => {
147
151
  view.result.current.update((d) => {
148
- d.tiles[0].label = "Categories";
152
+ queryTile(d, 0).label = "Categories";
149
153
  });
150
154
  });
151
155
  await act(async () => {
@@ -169,7 +173,7 @@ describe("useDashboardEditor: saving", () => {
169
173
 
170
174
  act(() => {
171
175
  view.result.current.update((d) => {
172
- d.tiles[0].label = "One";
176
+ queryTile(d, 0).label = "One";
173
177
  });
174
178
  });
175
179
  await act(async () => {
@@ -177,7 +181,7 @@ describe("useDashboardEditor: saving", () => {
177
181
  });
178
182
  act(() => {
179
183
  view.result.current.update((d) => {
180
- d.tiles[1].label = "Two";
184
+ queryTile(d, 1).label = "Two";
181
185
  });
182
186
  });
183
187
  await act(async () => {
@@ -209,7 +213,7 @@ describe("useDashboardEditor: saving", () => {
209
213
  });
210
214
 
211
215
  expect(called).toBe(false);
212
- expect(view.result.current.error).toContain("imports");
216
+ expect(view.result.current.error).toContain("whole-file");
213
217
  // Still there, still dirty. The reader can undo or try something else.
214
218
  expect(view.result.current.document.imports).toHaveLength(2);
215
219
  expect(view.result.current.dirty).toBe(true);
@@ -255,8 +259,7 @@ describe("useDashboardEditor: saving", () => {
255
259
  });
256
260
 
257
261
  /**
258
- * `structural` decides whether the builder shows the author a diff before it
259
- * saves, so it has to mean what the WRITER means by a changed tile — not what
262
+ * `structural` is what a save reports as structural, so it has to mean what the WRITER means by a changed tile — not what
260
263
  * the grid means.
261
264
  */
262
265
  describe("useDashboardEditor: structural", () => {
@@ -264,7 +267,7 @@ describe("useDashboardEditor: structural", () => {
264
267
  const view = await editor();
265
268
  act(() => {
266
269
  view.result.current.update((d) => {
267
- d.tiles[0].label = "Categories";
270
+ queryTile(d, 0).label = "Categories";
268
271
  d.tiles[0].colspan = 4;
269
272
  });
270
273
  });
@@ -301,11 +304,11 @@ describe("useDashboardEditor: structural", () => {
301
304
  const before = view.result.current.document.tiles[0];
302
305
  act(() => {
303
306
  view.result.current.update((d) => {
304
- d.tiles[0].declaration = { kind: "inline" };
307
+ queryTile(d, 0).declaration = { kind: "inline" };
305
308
  });
306
309
  });
307
310
  const after = view.result.current.document.tiles[0];
308
- expect([after.source, after.name]).toEqual([before.source, before.name]);
311
+ expect(tileKey(after)).toBe(tileKey(before));
309
312
  expect(view.result.current.structural).toBe(true);
310
313
  });
311
314
  });
@@ -1,78 +1,35 @@
1
1
  // Copyright (c) Credible Data Inc.
2
2
  // SPDX-License-Identifier: MIT
3
3
 
4
- import { useCallback, useMemo, useState } from "react";
4
+ import { useCallback } from "react";
5
5
  import { type DashboardDocument } from "./document";
6
+ import { spliceDashboardDocument, tileFileKey } from "./spliceDocument";
6
7
  import {
7
- spliceDashboardDocument,
8
- spliceFailed,
9
- tileFileKey,
10
- } from "./spliceDocument";
8
+ type DocumentEditor,
9
+ type SaveHandler,
10
+ type SaveOutcome,
11
+ useDocumentEditor,
12
+ } from "./useDocumentEditor";
11
13
 
12
- /**
13
- * The editor's state: the document being edited, its history, and saving.
14
- *
15
- * Deliberately holds the DOCUMENT and nothing else. Given VALUES — what the
16
- * preview is running with — are editor state rather than document state and
17
- * live outside this hook, which is what keeps them out of the undo stack. Undo
18
- * should take back a layout change; it should not rewind the filter someone was
19
- * previewing under.
20
- */
21
-
22
- export interface DashboardEditor {
23
- document: DashboardDocument;
24
- /** The file as it currently stands, which is what a save patches. */
25
- source: string;
26
- canUndo: boolean;
27
- canRedo: boolean;
28
- /** Whether the document has changes the file does not have yet. */
29
- dirty: boolean;
30
- /**
31
- * Why the last save did not happen. The document is untouched when this is
32
- * set: a refused write keeps the reader's work and reports a defect in the
33
- * writer, rather than discarding an edit because a tool could not read back
34
- * what it produced.
35
- */
36
- error?: string;
37
- /** Apply a change. Receives a copy; mutate it freely. */
38
- update: (change: (draft: DashboardDocument) => void) => void;
39
- undo: () => void;
40
- redo: () => void;
41
- /** Splice the change into the file, or say why it was refused. */
42
- save: () => Promise<SaveOutcome>;
43
- /**
44
- * The file a save would write, without writing it: what a diff preview
45
- * shows. The failure arm is the writer's refusal, worded for the author.
46
- */
47
- preview: () => Promise<
48
- { ok: true; source: string } | { ok: false; reason: string }
49
- >;
50
- /**
51
- * Whether the unsaved change adds or removes a tile. Those moves touch
52
- * declarations and the comments beside them, which is when a save is worth
53
- * showing as a diff first; a property edit never is.
54
- */
55
- structural: boolean;
56
- }
57
-
58
- export type SaveOutcome = { ok: true } | { ok: false; reason: string };
59
-
60
- interface History {
61
- /** Every document state, oldest first. */
62
- stack: DashboardDocument[];
63
- /** Which one is current. Undo and redo move this rather than mutating. */
64
- index: number;
65
- }
14
+ export type DashboardEditor = DocumentEditor<DashboardDocument>;
15
+ export type { SaveOutcome };
66
16
 
67
17
  /**
68
- * How many documents of history to keep.
18
+ * Whether the unsaved change adds or removes a tile.
69
19
  *
70
- * A whole document per entry is affordable because the document is small and
71
- * immutable — it is a list of tiles and their presentation, not the file. Keeping
72
- * whole states is also what makes undo trivially correct: there is no inverse
73
- * operation to get wrong for each kind of edit.
20
+ * The FILE's identity for a tile, not the grid's: `document.tileKey` is
21
+ * `source.name`, so a tile redeclared from another view keeps its key and a
22
+ * save that rewrites its declaration would not be reported as structural — the
23
+ * one case where the author most wants the diff.
74
24
  */
75
- const HISTORY_LIMIT = 100;
25
+ const tilesChanged = (saved: DashboardDocument, next: DashboardDocument) => {
26
+ const before = new Set(saved.tiles.map(tileFileKey));
27
+ const after = new Set(next.tiles.map(tileFileKey));
28
+ return (
29
+ [...before].some((k) => !after.has(k)) ||
30
+ [...after].some((k) => !before.has(k))
31
+ );
32
+ };
76
33
 
77
34
  export function useDashboardEditor(options: {
78
35
  /** The file as read from storage. */
@@ -80,122 +37,38 @@ export function useDashboardEditor(options: {
80
37
  /** The document that file produced. */
81
38
  document: DashboardDocument;
82
39
  /** Persist the patched file. Rejecting leaves the editor dirty. */
83
- onSave?: (source: string) => Promise<void> | void;
40
+ onSave?: SaveHandler<DashboardDocument>;
41
+ /**
42
+ * A cell-format notebook the document is the conversion of: while `source`
43
+ * is `from` the edits are spliced into `to`, the layout text it converts to,
44
+ * and the open is unsaved ({@link DocumentEditorOptions.opensDirty}).
45
+ */
46
+ conversion?: { from: string; to: string };
47
+ /** The file a save overwrites when `source` is a draft of it ({@link DocumentEditorOptions.replaces}). */
48
+ replaces?: string;
49
+ /** The file's path within the package. */
50
+ modelPath?: string;
84
51
  }): DashboardEditor {
85
- const [history, setHistory] = useState<History>({
86
- stack: [options.document],
87
- index: 0,
88
- });
89
- const [source, setSource] = useState(options.source);
90
- const [error, setError] = useState<string | undefined>(undefined);
91
-
92
- // What the file currently holds, so `dirty` compares against the saved state
93
- // rather than against where the session started.
94
- //
95
- // State, not a ref, and the difference is visible: `dirty` is derived from
96
- // it, and a ref write does not re-render, so a successful save left the
97
- // editor reporting unsaved changes it had just written.
98
- const [saved, setSaved] = useState(options.document);
99
-
100
- const document = history.stack[history.index];
101
-
102
- const update = useCallback((change: (draft: DashboardDocument) => void) => {
103
- setError(undefined);
104
- setHistory((previous) => {
105
- const draft = structuredClone(previous.stack[previous.index]);
106
- change(draft);
107
- // A change that changes nothing does not deserve a history entry;
108
- // otherwise a click that sets a value to what it already was makes
109
- // undo appear broken.
110
- if (
111
- JSON.stringify(draft) ===
112
- JSON.stringify(previous.stack[previous.index])
113
- )
114
- return previous;
115
- // Editing after undo discards the redo tail, which is what every
116
- // editor does and what a reader expects.
117
- const kept = previous.stack.slice(0, previous.index + 1);
118
- const stack = [...kept, draft].slice(-HISTORY_LIMIT);
119
- return { stack, index: stack.length - 1 };
120
- });
121
- }, []);
122
-
123
- const undo = useCallback(() => {
124
- setError(undefined);
125
- setHistory((previous) =>
126
- previous.index > 0
127
- ? { ...previous, index: previous.index - 1 }
128
- : previous,
129
- );
130
- }, []);
131
-
132
- const redo = useCallback(() => {
133
- setError(undefined);
134
- setHistory((previous) =>
135
- previous.index < previous.stack.length - 1
136
- ? { ...previous, index: previous.index + 1 }
137
- : previous,
138
- );
139
- }, []);
140
-
141
- const save = useCallback(async (): Promise<SaveOutcome> => {
142
- const result = await spliceDashboardDocument(source, document);
143
- if (spliceFailed(result)) {
144
- setError(result.reason);
145
- return { ok: false, reason: result.reason };
146
- }
147
- try {
148
- await options.onSave?.(result.source);
149
- } catch (failure) {
150
- // The file may or may not have been written; what is certain is that
151
- // the editor must not pretend it was. Staying dirty is the safe read.
152
- const reason = `Could not save: ${failure}`;
153
- setError(reason);
154
- return { ok: false, reason };
155
- }
156
- // The patched file is the new baseline, so a second save patches what is
157
- // now on disk rather than re-deriving from the text this session opened.
158
- setSource(result.source);
159
- setSaved(document);
160
- setError(undefined);
161
- return { ok: true };
162
- }, [document, options, source]);
163
-
164
- const dirty = useMemo(
165
- () => JSON.stringify(document) !== JSON.stringify(saved),
166
- [document, saved],
52
+ const { conversion, modelPath, ...rest } = options;
53
+ const from = conversion?.from;
54
+ const to = conversion?.to;
55
+ const splice = useCallback(
56
+ (text: string, document: DashboardDocument) =>
57
+ // The kind is only ever changed by the settings toggle, so the writer is always allowed to follow it.
58
+ spliceDashboardDocument(
59
+ from !== undefined && text === from ? (to as string) : text,
60
+ document,
61
+ {
62
+ changeKind: true,
63
+ ...(modelPath !== undefined ? { modelPath } : {}),
64
+ },
65
+ ),
66
+ [from, to, modelPath],
167
67
  );
168
- const structural = useMemo(() => {
169
- // The FILE's identity for a tile, not the grid's: `document.tileKey` is
170
- // `source.name`, so a tile redeclared from another view keeps its key
171
- // and a save that rewrites its declaration would not be reported as
172
- // structural — the one case where the author most wants the diff.
173
- const before = new Set(saved.tiles.map(tileFileKey));
174
- const after = new Set(document.tiles.map(tileFileKey));
175
- return (
176
- [...before].some((k) => !after.has(k)) ||
177
- [...after].some((k) => !before.has(k))
178
- );
179
- }, [document, saved]);
180
- const preview = useCallback(async () => {
181
- const result = await spliceDashboardDocument(source, document);
182
- return spliceFailed(result)
183
- ? { ok: false as const, reason: result.reason }
184
- : { ok: true as const, source: result.source };
185
- }, [document, source]);
186
-
187
- return {
188
- document,
189
- source,
190
- canUndo: history.index > 0,
191
- canRedo: history.index < history.stack.length - 1,
192
- dirty,
193
- ...(error === undefined ? {} : { error }),
194
- update,
195
- undo,
196
- redo,
197
- save,
198
- preview,
199
- structural,
200
- };
68
+ return useDocumentEditor({
69
+ ...rest,
70
+ splice,
71
+ structural: tilesChanged,
72
+ opensDirty: conversion !== undefined,
73
+ });
201
74
  }