@malloy-publisher/sdk 0.9.14 → 0.9.16

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 (164) hide show
  1. package/dist/RenderedResult-5hOs5XnV.cjs.js +171 -0
  2. package/dist/RenderedResult-DxmHGZNQ.es.js +671 -0
  3. package/dist/{ServerProvider-BV7IoDMR.es.js → ServerProvider-D7neZcTk.es.js} +283 -265
  4. package/dist/ServerProvider-e2369oom.cjs.js +1 -0
  5. package/dist/builder/index.cjs.js +80 -79
  6. package/dist/builder/index.es.js +7732 -6717
  7. package/dist/builder-entry.d.ts +1 -1
  8. package/dist/client/api.d.ts +31 -7
  9. package/dist/client/index.cjs.js +1 -1
  10. package/dist/client/index.es.js +1 -1
  11. package/dist/components/Dashboard/DashboardGrid.d.ts +51 -1
  12. package/dist/components/Dashboard/DashboardTile.d.ts +7 -2
  13. package/dist/components/Dashboard/DashboardView.d.ts +9 -2
  14. package/dist/components/Dashboard/index.d.ts +1 -1
  15. package/dist/components/Dashboard/promoteMeasureRow.d.ts +5 -7
  16. package/dist/components/Dashboard/useStuck.d.ts +18 -12
  17. package/dist/components/RenderedResult/RenderedResult.d.ts +3 -2
  18. package/dist/components/RenderedResult/ResultContainer.d.ts +18 -1
  19. package/dist/components/RenderedResult/ResultPanel.d.ts +7 -1
  20. package/dist/components/RenderedResult/chartPresentation.d.ts +15 -0
  21. package/dist/components/RenderedResult/chartShapeProblem.d.ts +55 -0
  22. package/dist/components/RenderedResult/resultSizing.d.ts +47 -6
  23. package/dist/components/Report/Report.d.ts +7 -1
  24. package/dist/components/Report/ReportPage.d.ts +7 -0
  25. package/dist/components/Report/layoutRules.d.ts +45 -0
  26. package/dist/components/ReportBuilder/AddTileDialog.d.ts +2 -1
  27. package/dist/components/ReportBuilder/ArrangeMenu.d.ts +9 -0
  28. package/dist/components/ReportBuilder/BuilderGrid.d.ts +15 -4
  29. package/dist/components/ReportBuilder/BuilderToolbar.d.ts +3 -1
  30. package/dist/components/ReportBuilder/ChartPicker.d.ts +4 -2
  31. package/dist/components/ReportBuilder/EditorSurface.d.ts +10 -1
  32. package/dist/components/ReportBuilder/ReportBuilder.d.ts +1 -1
  33. package/dist/components/ReportBuilder/TileFrame.d.ts +54 -10
  34. package/dist/components/ReportBuilder/TileMenu.d.ts +5 -2
  35. package/dist/components/ReportBuilder/builderControls.d.ts +19 -0
  36. package/dist/components/ReportBuilder/catalog.d.ts +23 -0
  37. package/dist/components/ReportBuilder/chartLine.d.ts +38 -5
  38. package/dist/components/ReportBuilder/controls.d.ts +2 -2
  39. package/dist/components/ReportBuilder/document.d.ts +53 -5
  40. package/dist/components/ReportBuilder/imports.d.ts +7 -0
  41. package/dist/components/ReportBuilder/placement.d.ts +121 -0
  42. package/dist/components/ReportBuilder/preview.d.ts +59 -10
  43. package/dist/components/ReportBuilder/sortable.d.ts +12 -1
  44. package/dist/components/ReportBuilder/useBuilderSession.d.ts +1 -1
  45. package/dist/components/ReportBuilder/useBuilderShortcuts.d.ts +9 -1
  46. package/dist/components/ReportBuilder/useEdgeResize.d.ts +84 -0
  47. package/dist/components/ReportBuilder/useTileReorder.d.ts +30 -3
  48. package/dist/index.cjs.js +7 -7
  49. package/dist/index.es.js +3349 -3307
  50. package/dist/text/index.es.js +1 -1
  51. package/dist/{text-entry-72YFLeNk.es.js → text-entry-YKJZbRPo.es.js} +2 -2
  52. package/dist/theme/classes.d.ts +1 -1
  53. package/dist/theme/resolveTheme.d.ts +15 -0
  54. package/dist/{useCompiledDocument-B4QF1mIl.es.js → useCompiledDocument-CaYP_OYG.es.js} +6353 -6047
  55. package/dist/useCompiledDocument-zYEfLogf.cjs.js +142 -0
  56. package/dist/utils/blankQuoted.d.ts +15 -0
  57. package/package.json +1 -1
  58. package/src/builder-entry.ts +1 -0
  59. package/src/components/Dashboard/DashboardGrid.tsx +129 -10
  60. package/src/components/Dashboard/DashboardTile.tsx +15 -1
  61. package/src/components/Dashboard/DashboardView.tsx +64 -10
  62. package/src/components/Dashboard/TileCard.tsx +6 -0
  63. package/src/components/Dashboard/index.ts +1 -0
  64. package/src/components/Dashboard/promoteMeasureRow.spec.ts +19 -0
  65. package/src/components/Dashboard/promoteMeasureRow.ts +15 -36
  66. package/src/components/Dashboard/tileHeight.spec.ts +72 -0
  67. package/src/components/Dashboard/useStuck.ts +76 -28
  68. package/src/components/Package/NewDocumentDialog.spec.tsx +20 -16
  69. package/src/components/Package/NewDocumentDialog.tsx +10 -11
  70. package/src/components/RenderedResult/RenderedResult.lifecycle.spec.tsx +167 -2
  71. package/src/components/RenderedResult/RenderedResult.tsx +39 -6
  72. package/src/components/RenderedResult/ResultContainer.tsx +99 -10
  73. package/src/components/RenderedResult/ResultPanel.tsx +25 -1
  74. package/src/components/RenderedResult/chartPresentation.spec.ts +153 -0
  75. package/src/components/RenderedResult/chartPresentation.ts +166 -0
  76. package/src/components/RenderedResult/chartShapeProblem.spec.tsx +245 -0
  77. package/src/components/RenderedResult/chartShapeProblem.ts +219 -0
  78. package/src/components/RenderedResult/resultSizing.spec.ts +75 -3
  79. package/src/components/RenderedResult/resultSizing.ts +71 -11
  80. package/src/components/Report/Report.spec.tsx +19 -0
  81. package/src/components/Report/Report.tsx +8 -1
  82. package/src/components/Report/ReportPage.tsx +13 -1
  83. package/src/components/Report/layoutRules.spec.ts +43 -3
  84. package/src/components/Report/layoutRules.ts +70 -0
  85. package/src/components/Report/tileOverflow.spec.tsx +162 -0
  86. package/src/components/ReportBuilder/AddTileDialog.spec.tsx +2 -2
  87. package/src/components/ReportBuilder/AddTileDialog.tsx +11 -6
  88. package/src/components/ReportBuilder/ArrangeMenu.tsx +80 -0
  89. package/src/components/ReportBuilder/BuilderBar.tsx +5 -1
  90. package/src/components/ReportBuilder/BuilderDialogs.tsx +28 -1
  91. package/src/components/ReportBuilder/BuilderGrid.tsx +142 -35
  92. package/src/components/ReportBuilder/BuilderToolbar.tsx +4 -0
  93. package/src/components/ReportBuilder/ChartPicker.spec.ts +91 -4
  94. package/src/components/ReportBuilder/ChartPicker.tsx +45 -10
  95. package/src/components/ReportBuilder/DrillDialog.tsx +8 -2
  96. package/src/components/ReportBuilder/EditorSurface.tsx +101 -20
  97. package/src/components/ReportBuilder/FilterDialog.tsx +17 -8
  98. package/src/components/ReportBuilder/FilterStrip.tsx +2 -1
  99. package/src/components/ReportBuilder/ReportBuilder.charts.spec.tsx +193 -2
  100. package/src/components/ReportBuilder/ReportBuilder.controls.spec.tsx +4 -2
  101. package/src/components/ReportBuilder/ReportBuilder.notebook.spec.tsx +2 -2
  102. package/src/components/ReportBuilder/ReportBuilder.spec.tsx +242 -2
  103. package/src/components/ReportBuilder/ReportBuilder.tsx +161 -27
  104. package/src/components/ReportBuilder/ReportEditor.fileModel.spec.tsx +178 -0
  105. package/src/components/ReportBuilder/ReportEditor.modelFilters.spec.tsx +3 -5
  106. package/src/components/ReportBuilder/ReportEditor.readOnlyTitle.spec.tsx +92 -0
  107. package/src/components/ReportBuilder/ReportEditor.tsx +13 -0
  108. package/src/components/ReportBuilder/TileFrame.tsx +366 -170
  109. package/src/components/ReportBuilder/TileMenu.tsx +259 -20
  110. package/src/components/ReportBuilder/ViewPicker.tsx +4 -3
  111. package/src/components/ReportBuilder/addTileToDocument.spec.ts +34 -0
  112. package/src/components/ReportBuilder/addTileToDocument.ts +26 -1
  113. package/src/components/ReportBuilder/builderControls.ts +34 -0
  114. package/src/components/ReportBuilder/catalog.aggregate.spec.ts +137 -1
  115. package/src/components/ReportBuilder/catalog.ts +90 -37
  116. package/src/components/ReportBuilder/chartLine.spec.ts +42 -5
  117. package/src/components/ReportBuilder/chartLine.ts +80 -2
  118. package/src/components/ReportBuilder/controls.ts +4 -3
  119. package/src/components/ReportBuilder/document.ts +65 -7
  120. package/src/components/ReportBuilder/emptyStart.spec.ts +1 -1
  121. package/src/components/ReportBuilder/entryLayout.spec.ts +151 -0
  122. package/src/components/ReportBuilder/imports.ts +1 -1
  123. package/src/components/ReportBuilder/invariants.spec.ts +18 -10
  124. package/src/components/ReportBuilder/layout.ts +3 -0
  125. package/src/components/ReportBuilder/placement.spec.ts +401 -0
  126. package/src/components/ReportBuilder/placement.ts +416 -0
  127. package/src/components/ReportBuilder/preview.shapes.spec.ts +46 -6
  128. package/src/components/ReportBuilder/preview.spec.ts +151 -11
  129. package/src/components/ReportBuilder/preview.ts +203 -40
  130. package/src/components/ReportBuilder/preview.unsaved.compile.spec.ts +190 -0
  131. package/src/components/ReportBuilder/readDocument.spec.ts +53 -19
  132. package/src/components/ReportBuilder/readDocument.ts +197 -29
  133. package/src/components/ReportBuilder/scannerShapes.spec.ts +10 -10
  134. package/src/components/ReportBuilder/sortable.tsx +30 -2
  135. package/src/components/ReportBuilder/spliceDocument.chart.spec.ts +1 -1
  136. package/src/components/ReportBuilder/spliceDocument.color.spec.ts +80 -0
  137. package/src/components/ReportBuilder/spliceDocument.colspan.spec.ts +78 -0
  138. package/src/components/ReportBuilder/spliceDocument.spec.ts +110 -58
  139. package/src/components/ReportBuilder/spliceDocument.ts +139 -91
  140. package/src/components/ReportBuilder/statementExtent.spec.ts +3 -3
  141. package/src/components/ReportBuilder/testing/reportGivenOrder.json +42 -0
  142. package/src/components/ReportBuilder/textTiles.spec.ts +2 -2
  143. package/src/components/ReportBuilder/tileDisplayTitle.ts +2 -2
  144. package/src/components/ReportBuilder/useBuilderSession.ts +7 -2
  145. package/src/components/ReportBuilder/useBuilderShortcuts.ts +25 -1
  146. package/src/components/ReportBuilder/useEdgeResize.spec.tsx +324 -0
  147. package/src/components/ReportBuilder/useEdgeResize.ts +397 -0
  148. package/src/components/ReportBuilder/useTileEditing.ts +16 -3
  149. package/src/components/ReportBuilder/useTileReorder.ts +143 -6
  150. package/src/components/given/FilterButton.spec.tsx +27 -11
  151. package/src/components/given/FilterButton.tsx +19 -6
  152. package/src/theme/classes.ts +1 -1
  153. package/src/theme/resolveTheme.spec.ts +72 -1
  154. package/src/theme/resolveTheme.ts +43 -0
  155. package/src/utils/blankQuoted.spec.ts +50 -0
  156. package/src/utils/blankQuoted.ts +32 -0
  157. package/src/utils/malloyText.ts +2 -1
  158. package/dist/RenderedResult-DnBtf-cI.es.js +0 -593
  159. package/dist/RenderedResult-DtQtNV0d.cjs.js +0 -153
  160. package/dist/ServerProvider-BIFPCzTD.cjs.js +0 -1
  161. package/dist/components/ReportBuilder/useTileResize.d.ts +0 -32
  162. package/dist/useCompiledDocument-D0OAMejE.cjs.js +0 -142
  163. package/src/components/ReportBuilder/useTileResize.spec.tsx +0 -146
  164. package/src/components/ReportBuilder/useTileResize.ts +0 -133
@@ -54,12 +54,12 @@ function mount(
54
54
  }
55
55
 
56
56
  describe("FilterButton's starting value, in the builder", () => {
57
- it("opens the report with the current value, a filter as its text", () => {
57
+ it("makes the current value the report's default, a filter as its text", () => {
58
58
  // Not an f'…' literal: the server unwraps one without un-escaping it.
59
59
  const { setStartingValue } = mount(BRAND, "O'Brien");
60
60
  fireEvent.click(
61
61
  screen.getByRole("button", {
62
- name: "Open the report with this value",
62
+ name: "Make this the report's default",
63
63
  }),
64
64
  );
65
65
  expect(setStartingValue).toHaveBeenCalledWith("BRAND", "O'Brien");
@@ -69,24 +69,40 @@ describe("FilterButton's starting value, in the builder", () => {
69
69
  const { setStartingValue } = mount(SINCE, "2024-03-01");
70
70
  fireEvent.click(
71
71
  screen.getByRole("button", {
72
- name: "Open the report with this value",
72
+ name: "Make this the report's default",
73
73
  }),
74
74
  );
75
75
  expect(setStartingValue).toHaveBeenCalledWith("SINCE", "2024-03-01");
76
76
  });
77
77
 
78
- it("says so when the report already opens there, and offers the default instead", () => {
78
+ it("says Report default when it is, and offers the filter's own default for one this report declares", () => {
79
79
  const { setStartingValue } = mount(BRAND, "Ford", { start: "f'Ford'" });
80
- expect(
81
- screen.getByText("The report opens with this value."),
82
- ).toBeDefined();
80
+ expect(screen.getByText("Report default")).toBeDefined();
83
81
  expect(
84
82
  screen.queryByRole("button", {
85
- name: "Open the report with this value",
83
+ name: "Make this the report's default",
86
84
  }),
87
85
  ).toBeNull();
86
+ expect(
87
+ screen.queryByRole("button", { name: "Use the model's default" }),
88
+ ).toBeNull();
89
+ fireEvent.click(
90
+ screen.getByRole("button", { name: "Use the filter's default" }),
91
+ );
92
+ expect(setStartingValue).toHaveBeenCalledWith("BRAND", undefined);
93
+ });
94
+
95
+ it("offers the model's default for a filter the model declares", () => {
96
+ const { setStartingValue } = mount(BRAND, "Ford", {
97
+ start: "f'Ford'",
98
+ state: { origin: "model" },
99
+ });
100
+ expect(screen.getByText("Report default")).toBeDefined();
101
+ expect(
102
+ screen.queryByRole("button", { name: "Use the filter's default" }),
103
+ ).toBeNull();
88
104
  fireEvent.click(
89
- screen.getByRole("button", { name: "Open at the default instead" }),
105
+ screen.getByRole("button", { name: "Use the model's default" }),
90
106
  );
91
107
  expect(setStartingValue).toHaveBeenCalledWith("BRAND", undefined);
92
108
  });
@@ -95,7 +111,7 @@ describe("FilterButton's starting value, in the builder", () => {
95
111
  mount(BRAND, undefined);
96
112
  expect(
97
113
  screen.queryByRole("button", {
98
- name: "Open the report with this value",
114
+ name: "Make this the report's default",
99
115
  }),
100
116
  ).toBeNull();
101
117
  });
@@ -122,7 +138,7 @@ describe("FilterButton for a filter the model sets", () => {
122
138
  // Where the report opens is still the report's to say.
123
139
  expect(
124
140
  screen.getByRole("button", {
125
- name: "Open the report with this value",
141
+ name: "Make this the report's default",
126
142
  }),
127
143
  ).toBeDefined();
128
144
  fireEvent.click(screen.getByRole("button", { name: "Open the model" }));
@@ -3,6 +3,7 @@
3
3
 
4
4
  import { reportClasses } from "../../theme/classes";
5
5
  import AddIcon from "@mui/icons-material/Add";
6
+ import CheckIcon from "@mui/icons-material/Check";
6
7
  import CloseIcon from "@mui/icons-material/Close";
7
8
  import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
8
9
  import {
@@ -256,9 +257,14 @@ function startingComparable(
256
257
  }
257
258
 
258
259
  /**
259
- * Where the report opens this filter, in the builder: "Open the report with
260
- * this value", or back to its default. Any filter has one, the model's
261
- * included, since where a report opens is the report's to say.
260
+ * The report's default for this filter, in the builder: the value the report
261
+ * opens with (`givens { … }`). "Make this the report's default" saves the
262
+ * current value as it; once saved, a quiet "Report default" with a check says
263
+ * so, and the clearing button goes back to the default the filter is declared
264
+ * with: "Use the model's default" for a model's filter, "Use the filter's
265
+ * default" for one this report declares, whose fallback is the default written
266
+ * in the report. Any filter has one, the model's included, since where a
267
+ * report opens is the report's to say.
262
268
  */
263
269
  function StartingValue({
264
270
  name,
@@ -275,6 +281,7 @@ function StartingValue({
275
281
  if (!editing?.setStartingValue) return null;
276
282
  const set = editing.setStartingValue;
277
283
  const start = startingComparable(editing.startingValueOf?.(name), type);
284
+ const declaredHere = editing.stateOf(name)?.origin === "dashboard";
278
285
  const buttonSx = { px: 0.75, minWidth: 0, ...ui.type.control };
279
286
  return (
280
287
  <>
@@ -284,14 +291,18 @@ function StartingValue({
284
291
  ...ui.type.caption,
285
292
  color: mui.palette.text.secondary,
286
293
  px: 0.75,
294
+ display: "flex",
295
+ alignItems: "center",
296
+ gap: 0.5,
287
297
  })}
288
298
  >
289
- The report opens with this value.
299
+ <CheckIcon aria-hidden sx={{ fontSize: 14 }} />
300
+ Report default
290
301
  </Typography>
291
302
  )}
292
303
  {text !== undefined && text !== start && (
293
304
  <Button size="small" onClick={() => set(name, text)} sx={buttonSx}>
294
- Open the report with this value
305
+ Make this the report&apos;s default
295
306
  </Button>
296
307
  )}
297
308
  {start !== undefined && (
@@ -300,7 +311,9 @@ function StartingValue({
300
311
  onClick={() => set(name, undefined)}
301
312
  sx={buttonSx}
302
313
  >
303
- Open at the default instead
314
+ {declaredHere
315
+ ? "Use the filter's default"
316
+ : "Use the model's default"}
304
317
  </Button>
305
318
  )}
306
319
  </>
@@ -38,7 +38,7 @@ export const reportClasses = {
38
38
  saveStatus: "publisher-save-status",
39
39
  /** A tile as the builder draws it: selection, handle and menu around the card. */
40
40
  builderTile: "publisher-builder-tile",
41
- /** The grip on a tile's right edge that sets its width. */
41
+ /** The invisible strip along a tile's right edge that sets its width (drawn only as a focus ring). */
42
42
  widthHandle: "publisher-width-handle",
43
43
  /** The builder's menus (Add, a tile's ⋯, the save status). */
44
44
  menu: "publisher-menu",
@@ -2,7 +2,7 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
 
4
4
  import { describe, expect, it } from "bun:test";
5
- import { resolveMode, resolveTheme } from "./resolveTheme";
5
+ import { chartThemeLayer, resolveMode, resolveTheme } from "./resolveTheme";
6
6
  import type { Theme } from "./types";
7
7
 
8
8
  describe("resolveTheme cascade", () => {
@@ -243,3 +243,74 @@ describe("resolveTheme series in dark mode", () => {
243
243
  expect(dark.series[1]).toBe("#60a5fa");
244
244
  });
245
245
  });
246
+
247
+ describe("a chart's own theme layer", () => {
248
+ const host: Theme = {
249
+ palette: { series: ["#628698", "#8a6a50", "#6f6a8f", "#7f9862"] },
250
+ };
251
+ const drawn = (chart: Theme) =>
252
+ resolveTheme([host, chartThemeLayer([host], chart)], "light").series;
253
+
254
+ // The builder's Color item writes one colour. On a bar and line chart the
255
+ // line is the second series: drawn from a one-colour list it came out the
256
+ // same colour as the bars and vanished against them.
257
+ // A pick changes the bars and not the line: every later series keeps the
258
+ // colour it has with no pick. It used to drop the pick from the host's
259
+ // list and shift the rest up, so the line took the host's FIRST colour.
260
+ it("changes only the first series for a colour on the palette", () => {
261
+ const series = drawn({ palette: { series: ["#7F9862"] } });
262
+ // The slot that held the pick takes the colour the first slot gave up.
263
+ expect(series).toEqual(["#7F9862", "#8a6a50", "#6f6a8f", "#628698"]);
264
+ });
265
+
266
+ it("keeps the line's colour whichever colour is picked", () => {
267
+ const theme = resolveTheme([host], "light").series;
268
+ for (const pick of ["#628698", "#6f6a8f", "#7f9862", "#123456"]) {
269
+ const series = drawn({ palette: { series: [pick] } });
270
+ expect(series[0]).toBe(pick);
271
+ expect(series[1]).toBe(theme[1]);
272
+ }
273
+ });
274
+
275
+ it("gives the line the first colour when the pick is the line's own", () => {
276
+ const series = drawn({ palette: { series: ["#8a6a50"] } });
277
+ expect(series).toEqual(["#8a6a50", "#628698", "#6f6a8f", "#7f9862"]);
278
+ });
279
+
280
+ it("keeps the host's later slots after a colour that is not on its palette", () => {
281
+ const series = drawn({ palette: { series: ["#123456"] } });
282
+ expect(series).toEqual(["#123456", "#8a6a50", "#6f6a8f", "#7f9862"]);
283
+ });
284
+
285
+ it("draws a one-colour host palette as the second series", () => {
286
+ const one: Theme = { palette: { series: ["#628698"] } };
287
+ const series = resolveTheme(
288
+ [one, chartThemeLayer([one], { palette: { series: ["#123456"] } })],
289
+ "light",
290
+ ).series;
291
+ expect(series).toEqual(["#123456", "#628698"]);
292
+ });
293
+
294
+ it("falls back to the default series with no host palette", () => {
295
+ const series = resolveTheme(
296
+ [chartThemeLayer([], { palette: { series: ["#123456"] } })],
297
+ "light",
298
+ ).series;
299
+ expect(series[0]).toBe("#123456");
300
+ expect(series.slice(1)).toEqual(
301
+ resolveTheme([], "light").series.slice(1),
302
+ );
303
+ });
304
+
305
+ it("uses a list of two or more colours as written", () => {
306
+ expect(drawn({ palette: { series: ["#111111", "#222222"] } })).toEqual([
307
+ "#111111",
308
+ "#222222",
309
+ ]);
310
+ });
311
+
312
+ it("leaves a layer that sets no series alone", () => {
313
+ const chart: Theme = { font: { size: 14 } };
314
+ expect(chartThemeLayer([host], chart)).toBe(chart);
315
+ });
316
+ });
@@ -158,6 +158,49 @@ export function resolveTheme(
158
158
  };
159
159
  }
160
160
 
161
+ /**
162
+ * A chart's own `# theme.*` layer, ready to go on top of `layers`.
163
+ *
164
+ * A one-colour `series` on a chart sets the chart's FIRST series colour (it is
165
+ * what the builder's Color item writes). Taken as the whole list, every series
166
+ * would cycle through that one colour, so a bar and line chart, or any chart
167
+ * with more than one series, would draw them all alike.
168
+ *
169
+ * So only the first slot changes: every other series keeps the colour it has
170
+ * with no pick, which is what makes a pick change the bars and not the line.
171
+ * The one exception is a later slot that holds the picked colour. It takes the
172
+ * colour the first slot gave up, so no two series are drawn alike. A list of
173
+ * two or more colours is the author's whole palette and is used as written.
174
+ */
175
+ export function chartThemeLayer(
176
+ layers: Array<Theme | undefined>,
177
+ chart: Theme,
178
+ ): Theme {
179
+ const own = chart.palette?.series;
180
+ if (!Array.isArray(own) || own.length !== 1) return chart;
181
+ const first = own[0];
182
+ const inherited = layers.reduce<string[]>(
183
+ (series, layer) =>
184
+ Array.isArray(layer?.palette?.series)
185
+ ? (layer.palette.series as string[])
186
+ : series,
187
+ (DEFAULT_THEME.palette?.series as string[]) ?? [],
188
+ );
189
+ const same = (color: string) => color.toLowerCase() === first.toLowerCase();
190
+ const [displaced, ...later] = inherited;
191
+ const rest =
192
+ later.length > 0
193
+ ? later.map((color) => (same(color) ? displaced : color))
194
+ : // A one-colour host palette has no second slot, so its one colour is the second series.
195
+ [displaced].filter(
196
+ (color): color is string => color !== undefined && !same(color),
197
+ );
198
+ return {
199
+ ...chart,
200
+ palette: { ...chart.palette, series: [first, ...rest] },
201
+ };
202
+ }
203
+
161
204
  /**
162
205
  * Pick the active mode for a viewer given the operator's `defaultMode`,
163
206
  * any persisted viewer choice, and a snapshot of the OS dark-mode
@@ -0,0 +1,50 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { describe, expect, it } from "bun:test";
5
+ import { blankQuoted } from "./blankQuoted";
6
+
7
+ describe("blankQuoted", () => {
8
+ it("blanks each quoted value, escapes included", () => {
9
+ expect(blankQuoted('# label="bar_chart" line_chart')).toBe(
10
+ '# label="" line_chart',
11
+ );
12
+ expect(blankQuoted('# a="x \\" y" b="z"')).toBe('# a="" b=""');
13
+ expect(blankQuoted('# a="x\\\\" bar_chart')).toBe('# a="" bar_chart');
14
+ expect(blankQuoted("# bar_chart")).toBe("# bar_chart");
15
+ expect(blankQuoted('""')).toBe('""');
16
+ });
17
+
18
+ it("leaves an unclosed quote and the rest of the line as they are", () => {
19
+ expect(blankQuoted('# a="x" b="bar_chart')).toBe('# a="" b="bar_chart');
20
+ expect(blankQuoted('# "\\"')).toBe('# "\\"');
21
+ });
22
+
23
+ // The regex it replaces, for lines it reads in linear time.
24
+ const QUOTED = /"(?:[^"\\]|\\.)*"/g;
25
+ it("agrees with the regex it replaces on every short line of quotes, escapes and words", () => {
26
+ const alphabet = ['"', "\\", "a", " "];
27
+ let checked = 0;
28
+ for (let length = 0; length <= 7; length++) {
29
+ const total = alphabet.length ** length;
30
+ for (let n = 0; n < total; n++) {
31
+ let text = "";
32
+ for (let k = 0, rest = n; k < length; k++) {
33
+ text += alphabet[rest % alphabet.length];
34
+ rest = Math.floor(rest / alphabet.length);
35
+ }
36
+ expect(blankQuoted(text)).toBe(text.replace(QUOTED, '""'));
37
+ checked++;
38
+ }
39
+ }
40
+ expect(checked).toBe(21845);
41
+ });
42
+
43
+ it("reads a long unclosed line of escaped quotes in linear time", () => {
44
+ // `"` then many `\"`: the shape that made the regex quadratic.
45
+ const hostile = `# "${'\\"'.repeat(200_000)}`;
46
+ const started = performance.now();
47
+ expect(blankQuoted(hostile)).toBe(hostile);
48
+ expect(performance.now() - started).toBeLessThan(250);
49
+ });
50
+ });
@@ -0,0 +1,32 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ /**
5
+ * `text` with every double-quoted string, escapes included, replaced by `""`,
6
+ * so a tag line can be split into words without a quoted value reading as a
7
+ * tag name: `# label="bar_chart" line_chart` -> `# label="" line_chart`.
8
+ *
9
+ * A quote that never closes is left as it is, along with the rest of the text.
10
+ *
11
+ * A scanner rather than `/"(?:[^"\\]|\\.)*"/g`: annotation text comes from the
12
+ * model, and that regex retries from every quote of an unclosed string, so a
13
+ * line like `"\"\"\"…` took time quadratic in its length. This one reads each
14
+ * character once. It can stop at the first unclosed quote because every quote
15
+ * after it was read as an escaped character, so a scan from any of them sees
16
+ * the same characters and cannot close either.
17
+ */
18
+ export function blankQuoted(text: string): string {
19
+ let out = "";
20
+ let from = 0;
21
+ for (;;) {
22
+ const open = text.indexOf('"', from);
23
+ if (open < 0) break;
24
+ let at = open + 1;
25
+ while (at < text.length && text[at] !== '"')
26
+ at += text[at] === "\\" ? 2 : 1;
27
+ if (at >= text.length) break;
28
+ out += `${text.slice(from, open)}""`;
29
+ from = at + 1;
30
+ }
31
+ return out + text.slice(from);
32
+ }
@@ -124,7 +124,8 @@ export function closesBlock(
124
124
  column: number | undefined,
125
125
  closer: "|#" | "|##",
126
126
  ): boolean {
127
- const indent = /^[ \t]*/.exec(line)[0].length;
127
+ // `[ \t]*` matches every string, so exec never returns null here.
128
+ const indent = /^[ \t]*/.exec(line)![0].length;
128
129
  if (column !== undefined && indent !== column) return false;
129
130
  const rest = line.slice(indent);
130
131
  return (