@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
@@ -0,0 +1,416 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import {
5
+ GRID_GAP_PX,
6
+ MIN_TEXT_TILE_ROWS,
7
+ MIN_TILE_ROWS,
8
+ MIN_VALUE_TILE_ROWS,
9
+ PLACED_DEFAULT_ROWSPAN,
10
+ TILE_ROW_PX,
11
+ } from "../Dashboard/DashboardGrid";
12
+ import { isChartPick } from "./chartLine";
13
+ import { isTextTile, type DashboardTile } from "./document";
14
+
15
+ /**
16
+ * Free placement on a grid report: each tile at its own `row` and `col`, so
17
+ * empty space anywhere on the grid is somewhere a tile can go.
18
+ *
19
+ * Pure: what a move or a resize does to the tiles, never how the gesture
20
+ * feels. A move or resize keeps the tile it changed where it was put and
21
+ * pushes whatever it now covers straight down, the way the grid dashboards
22
+ * people already know behave. Nothing is pulled up to close a gap, since an
23
+ * empty cell is a choice the author made.
24
+ */
25
+
26
+ /** A tile's rectangle on the grid, in rows and columns counted from 1. */
27
+ export interface Cell {
28
+ row: number;
29
+ col: number;
30
+ colspan: number;
31
+ rowspan: number;
32
+ }
33
+
34
+ /** Value cards or a table, which hold their shape when short. */
35
+ const NOT_A_CHART = ".malloy-big-value, .malloy-table";
36
+
37
+ /**
38
+ * Whether a drawn result is only value cards or a table. A dashboard result
39
+ * can hold a chart beside its cards, and that chart needs a chart's rows; a
40
+ * sparkline in a card, or a chart in a table's cell, sizes with its host.
41
+ */
42
+ const drawsNoChart = (frame: Element) =>
43
+ frame.querySelector(NOT_A_CHART) !== null &&
44
+ ![...frame.querySelectorAll(".malloy-chart")].some(
45
+ (chart) => chart.closest(NOT_A_CHART) === null,
46
+ );
47
+
48
+ /**
49
+ * The fewest rows the builder offers a tile: a chart needs three to draw its
50
+ * plot at all (at two a line chart had no plot area and the renderer failed
51
+ * on it); value cards and a table fit in two, the cards drawing compact;
52
+ * prose fits in one. Which a query tile is comes from what it draws, so
53
+ * `frame` is the tile's element when the caller has it; without it, a query
54
+ * tile is taken for a chart.
55
+ */
56
+ export const minRowsOf = (tile: DashboardTile, frame?: Element | null) =>
57
+ isTextTile(tile)
58
+ ? MIN_TEXT_TILE_ROWS
59
+ : frame && drawsNoChart(frame)
60
+ ? MIN_VALUE_TILE_ROWS
61
+ : MIN_TILE_ROWS;
62
+
63
+ /** Rows a tile takes on a placed report: its own, or its kind's default. */
64
+ export const rowsOf = (tile: DashboardTile) =>
65
+ tile.rowspan ?? PLACED_DEFAULT_ROWSPAN[isTextTile(tile) ? "text" : "query"];
66
+
67
+ /** A placed tile's rectangle, held to the grid; undefined for one not placed yet. */
68
+ export function cellOf(tile: DashboardTile, columns: number): Cell | undefined {
69
+ if (tile.row === undefined || tile.col === undefined) return undefined;
70
+ const colspan = Math.min(tile.colspan ?? 1, columns);
71
+ return {
72
+ row: tile.row,
73
+ col: Math.min(tile.col, columns - colspan + 1),
74
+ colspan,
75
+ rowspan: rowsOf(tile),
76
+ };
77
+ }
78
+
79
+ const overlaps = (a: Cell, b: Cell) =>
80
+ a.col < b.col + b.colspan &&
81
+ b.col < a.col + a.colspan &&
82
+ a.row < b.row + b.rowspan &&
83
+ b.row < a.row + a.rowspan;
84
+
85
+ /** Top to bottom, then left to right: the order a placed report is read in, and written. */
86
+ export function inReadingOrder(
87
+ tiles: readonly DashboardTile[],
88
+ ): DashboardTile[] {
89
+ return tiles
90
+ .map((tile, index) => ({ tile, index }))
91
+ .sort(
92
+ (a, b) =>
93
+ (a.tile.row ?? Infinity) - (b.tile.row ?? Infinity) ||
94
+ (a.tile.col ?? Infinity) - (b.tile.col ?? Infinity) ||
95
+ a.index - b.index,
96
+ )
97
+ .map(({ tile }) => tile);
98
+ }
99
+
100
+ /**
101
+ * The tiles with nothing covering anything: `fixed` (the tile just moved or
102
+ * resized) stays exactly where it is, and every placed tile it, or a tile it
103
+ * pushed, now covers moves straight down to just below what it hit. Tiles
104
+ * settle top to bottom, so a pushed tile pushes the ones under it in turn.
105
+ * Without `fixed`, the first tile in reading order holds its place.
106
+ */
107
+ export function settle(
108
+ tiles: readonly DashboardTile[],
109
+ columns: number,
110
+ fixed?: number,
111
+ ): DashboardTile[] {
112
+ const out = tiles.map((tile) => ({ ...tile }));
113
+ const held: Cell[] = [];
114
+ const hold = (index: number) => {
115
+ const cell = cellOf(out[index], columns);
116
+ if (!cell) return;
117
+ // Pulled onto the grid if it was hanging off its right edge.
118
+ out[index].col = cell.col;
119
+ held.push(cell);
120
+ };
121
+ if (fixed !== undefined) hold(fixed);
122
+ const rest = out
123
+ .map((tile, index) => ({ tile, index }))
124
+ .filter(({ index, tile }) => index !== fixed && tile.row !== undefined)
125
+ .sort(
126
+ (a, b) =>
127
+ (a.tile.row ?? 0) - (b.tile.row ?? 0) ||
128
+ (a.tile.col ?? 0) - (b.tile.col ?? 0) ||
129
+ a.index - b.index,
130
+ );
131
+ for (const { index } of rest) {
132
+ const cell = cellOf(out[index], columns);
133
+ if (!cell) continue;
134
+ for (;;) {
135
+ const hit = held.filter((other) => overlaps(cell, other));
136
+ if (hit.length === 0) break;
137
+ cell.row = Math.max(...hit.map((other) => other.row + other.rowspan));
138
+ }
139
+ out[index].row = cell.row;
140
+ hold(index);
141
+ }
142
+ return out;
143
+ }
144
+
145
+ /**
146
+ * The tiles with the one at `index` dropped at `at`. A drop onto the lower
147
+ * part of a tile that starts higher up lands just under that tile, rather
148
+ * than shoving it beneath the dropped one; a drop at or above a tile's top
149
+ * pushes that tile down. Then everything settles around where it landed.
150
+ */
151
+ export function dropAt(
152
+ tiles: readonly DashboardTile[],
153
+ index: number,
154
+ at: { row: number; col: number },
155
+ columns: number,
156
+ ): DashboardTile[] {
157
+ const others = tiles.flatMap((tile, i) => {
158
+ const cell = i === index ? undefined : cellOf(tile, columns);
159
+ return cell ? [cell] : [];
160
+ });
161
+ // Never lower than right under the lowest other tile: space below
162
+ // everything holds nothing, and a drag held at the window's bottom edge
163
+ // would otherwise grow the page, and scroll it, without end.
164
+ const lowest = Math.max(1, ...others.map((cell) => cell.row + cell.rowspan));
165
+ const moved = { ...tiles[index], ...at, row: Math.min(at.row, lowest) };
166
+ const cell = cellOf(moved, columns);
167
+ if (cell) {
168
+ for (;;) {
169
+ const above = others.filter(
170
+ (other) => other.row < cell.row && overlaps(cell, other),
171
+ );
172
+ if (above.length === 0) break;
173
+ cell.row = Math.max(
174
+ ...above.map((other) => other.row + other.rowspan),
175
+ );
176
+ }
177
+ moved.row = cell.row;
178
+ }
179
+ return settle(
180
+ tiles.map((tile, i) => (i === index ? moved : tile)),
181
+ columns,
182
+ index,
183
+ );
184
+ }
185
+
186
+ /** A tile's box against the grid's top left corner, in px. */
187
+ export interface Box {
188
+ top: number;
189
+ left: number;
190
+ width: number;
191
+ height: number;
192
+ }
193
+
194
+ /** One column track's width in a grid `gridWidth` px wide. */
195
+ export const trackWidth = (gridWidth: number, columns: number) =>
196
+ (gridWidth - GRID_GAP_PX * (columns - 1)) / columns;
197
+
198
+ const ROW_STEP = TILE_ROW_PX + GRID_GAP_PX;
199
+
200
+ /**
201
+ * The cell a tile of `colspan` columns lands in when its top left corner is
202
+ * at (`left`, `top`) px from the grid's: the nearest row and column, held on
203
+ * the grid.
204
+ */
205
+ export function cellAt(
206
+ left: number,
207
+ top: number,
208
+ colspan: number,
209
+ columns: number,
210
+ track: number,
211
+ ): { row: number; col: number } {
212
+ const span = Math.min(colspan, columns);
213
+ const col = Math.round(left / (track + GRID_GAP_PX)) + 1;
214
+ return {
215
+ row: Math.max(1, Math.round(top / ROW_STEP) + 1),
216
+ col: Math.min(Math.max(col, 1), columns - span + 1),
217
+ };
218
+ }
219
+
220
+ /**
221
+ * The tiles placed where they are drawn now, the first time a report is
222
+ * placed: each tile's row, column, width and height read off its box, so
223
+ * turning a flowing report into a placed one moves nothing. A tile with no
224
+ * box (not drawn) is left for the grid to place.
225
+ */
226
+ export function placeWhereDrawn(
227
+ tiles: readonly DashboardTile[],
228
+ boxes: ReadonlyArray<Box | undefined>,
229
+ columns: number,
230
+ track: number,
231
+ ): DashboardTile[] {
232
+ const placed = tiles.map((tile, index) => {
233
+ if (tile.row !== undefined && tile.col !== undefined) return tile;
234
+ const box = boxes[index];
235
+ if (!box) return tile;
236
+ const { break: _break, ...rest } = tile;
237
+ const colspan =
238
+ tile.colspan ??
239
+ Math.max(
240
+ 1,
241
+ Math.round((box.width + GRID_GAP_PX) / (track + GRID_GAP_PX)),
242
+ );
243
+ const cell = cellAt(box.left, box.top, colspan, columns, track);
244
+ // Its top and bottom are each taken to the nearest row line, rather
245
+ // than its height rounded on its own: tiles that touched still touch,
246
+ // so a whole page's rounding never adds up and pushes rows down. No
247
+ // edge moves more than half a row.
248
+ const lastRow = Math.round(
249
+ (box.top + box.height + GRID_GAP_PX) / ROW_STEP,
250
+ );
251
+ return {
252
+ ...rest,
253
+ ...cell,
254
+ colspan,
255
+ rowspan:
256
+ tile.rowspan ?? Math.max(minRowsOf(tile), lastRow - cell.row + 1),
257
+ };
258
+ });
259
+ return settle(placed, columns);
260
+ }
261
+
262
+ /**
263
+ * The tiles with the one at `index` placed under everything else, at the
264
+ * left, when the report is placed: where a new tile goes, so it never drops
265
+ * into a gap an author left on purpose. A report that flows is left as it is.
266
+ */
267
+ export function placeAtEnd(
268
+ tiles: readonly DashboardTile[],
269
+ index: number,
270
+ columns: number,
271
+ ): DashboardTile[] {
272
+ const others = tiles.flatMap((tile, i) => {
273
+ const cell = i === index ? undefined : cellOf(tile, columns);
274
+ return cell ? [cell] : [];
275
+ });
276
+ if (others.length === 0) return [...tiles];
277
+ const bottom = Math.max(...others.map((cell) => cell.row + cell.rowspan));
278
+ return tiles.map((tile, i) =>
279
+ i === index ? { ...tile, row: bottom, col: 1 } : tile,
280
+ );
281
+ }
282
+
283
+ /**
284
+ * The tiles slid up as far as each will go, top to bottom and then left to
285
+ * right, so every gap above a tile closes; nothing moves sideways. A tile
286
+ * with no place is left for the grid to place.
287
+ */
288
+ export function pack(
289
+ tiles: readonly DashboardTile[],
290
+ columns: number,
291
+ ): DashboardTile[] {
292
+ const out = tiles.map((tile) => ({ ...tile }));
293
+ const held: Cell[] = [];
294
+ const order = out
295
+ .map((tile, index) => ({ tile, index }))
296
+ .filter(({ tile }) => tile.row !== undefined && tile.col !== undefined)
297
+ .sort(
298
+ (a, b) =>
299
+ (a.tile.row ?? 0) - (b.tile.row ?? 0) ||
300
+ (a.tile.col ?? 0) - (b.tile.col ?? 0) ||
301
+ a.index - b.index,
302
+ );
303
+ for (const { index } of order) {
304
+ const cell = cellOf(out[index], columns);
305
+ if (!cell) continue;
306
+ while (
307
+ cell.row > 1 &&
308
+ !held.some((other) => overlaps({ ...cell, row: cell.row - 1 }, other))
309
+ )
310
+ cell.row -= 1;
311
+ out[index].row = cell.row;
312
+ out[index].col = cell.col;
313
+ held.push(cell);
314
+ }
315
+ return out;
316
+ }
317
+
318
+ /** Whether packing would move anything: the action is offered only then. */
319
+ export const hasGaps = (tiles: readonly DashboardTile[], columns: number) =>
320
+ pack(tiles, columns).some(
321
+ (tile, index) =>
322
+ tile.row !== tiles[index].row || tile.col !== tiles[index].col,
323
+ );
324
+
325
+ /**
326
+ * The tiles with their places taken off, so the report flows again in the
327
+ * order it is read. Widths and heights stay; a row break means nothing to a
328
+ * placed report, so none is set.
329
+ */
330
+ export function flowTiles(tiles: readonly DashboardTile[]): DashboardTile[] {
331
+ return inReadingOrder(tiles).map((tile) => {
332
+ const { row: _row, col: _col, ...rest } = tile;
333
+ return rest as DashboardTile;
334
+ });
335
+ }
336
+
337
+ /** The tiles with the one at `index` set to `at`, nothing else changed: a keyboard step, before it settles. */
338
+ export const stepTo = (
339
+ tiles: readonly DashboardTile[],
340
+ index: number,
341
+ at: { row: number; col: number },
342
+ ): DashboardTile[] =>
343
+ tiles.map((tile, i) => (i === index ? { ...tile, ...at } : tile));
344
+
345
+ /**
346
+ * A placed tile moved one cell by the keyboard, settled. A step into the one
347
+ * tile beside it in that direction swaps the two, so Down past a tile is the
348
+ * mirror of Up past it; otherwise the tile steps and whatever it lands on is
349
+ * pushed down. Returns the tiles unchanged when the tile is at the grid's edge.
350
+ */
351
+ export function stepPlaced(
352
+ tiles: readonly DashboardTile[],
353
+ index: number,
354
+ rows: -1 | 0 | 1,
355
+ cols: -1 | 0 | 1,
356
+ columns: number,
357
+ ): DashboardTile[] {
358
+ const cell = cellOf(tiles[index], columns);
359
+ if (!cell) return [...tiles];
360
+ const to = {
361
+ row: Math.max(1, cell.row + rows),
362
+ // Held so the tile's right edge stays on the grid.
363
+ col: Math.min(Math.max(1, cell.col + cols), columns - cell.colspan + 1),
364
+ };
365
+ if (to.row === cell.row && to.col === cell.col) return [...tiles];
366
+ const moved = { ...cell, ...to };
367
+ const blockers = tiles.flatMap((each, i) => {
368
+ const other = i === index ? undefined : cellOf(each, columns);
369
+ return other && overlaps(moved, other) && !overlaps(cell, other)
370
+ ? [{ i, other }]
371
+ : [];
372
+ });
373
+ const swap = blockers.length === 1 ? blockers[0] : undefined;
374
+ if (!swap) return settle(stepTo(tiles, index, to), columns, index);
375
+ const { i, other } = swap;
376
+ const out = tiles.map((tile) => ({ ...tile }));
377
+ if (rows > 0) {
378
+ out[i].row = cell.row;
379
+ out[index].row = cell.row + other.rowspan;
380
+ } else if (rows < 0) {
381
+ out[index].row = other.row;
382
+ out[i].row = other.row + cell.rowspan;
383
+ } else if (cols > 0) {
384
+ out[i].col = cell.col;
385
+ out[index].col = cell.col + other.colspan;
386
+ } else {
387
+ out[index].col = other.col;
388
+ out[i].col = other.col + cell.colspan;
389
+ }
390
+ return settle(out, columns, index);
391
+ }
392
+
393
+ /**
394
+ * The widest a tile may be dragged: the whole grid, or, for a placed tile, up
395
+ * to the grid's right edge from its column. A right edge dragged past the grid
396
+ * stops there; it never moves the tile's left edge.
397
+ */
398
+ export const widestFrom = (
399
+ tile: DashboardTile | undefined,
400
+ columns: number,
401
+ ): number =>
402
+ tile?.col === undefined ? columns : Math.max(1, columns - tile.col + 1);
403
+
404
+ /**
405
+ * A tile given a chart that plots (anything but value cards or a table) at
406
+ * fewer rows than a chart draws in, raised to that floor. A tile set to two
407
+ * rows as a table and then switched to a line chart in its menu would
408
+ * otherwise keep two rows, where the plot has no room and the renderer fails.
409
+ */
410
+ export function holdChartRows(tile: DashboardTile): DashboardTile {
411
+ if (isTextTile(tile) || tile.rowspan === undefined) return tile;
412
+ const plots = isChartPick(tile.chart) && tile.chart !== "big_value";
413
+ return plots && tile.rowspan < MIN_TILE_ROWS
414
+ ? { ...tile, rowspan: MIN_TILE_ROWS }
415
+ : tile;
416
+ }
@@ -7,7 +7,7 @@ import { previewTileQuery } from "./preview";
7
7
  import { openDocument, queryTile, spliced } from "./testing/fixtures";
8
8
 
9
9
  const NONE =
10
- "# -line_chart -bar_chart -big_value -scatter_chart -shape_map -segment_map -viz";
10
+ "# -line_chart -bar_chart -scatter_chart -shape_map -segment_map -combo_chart";
11
11
 
12
12
  const SHAPES = `## artifact { title="T" tiles=["a -> inl", "a -> comp", "a -> pipe", "a -> chain", "a -> paren", "a -> ref"] }
13
13
  import "../m.malloy"
@@ -34,17 +34,57 @@ describe("the Default chart preview, for the shapes a real dashboard reads", ()
34
34
  ).annotation,
35
35
  ]),
36
36
  );
37
+ // No tile here has a colour, so each one that runs its own saved view
38
+ // also takes off any colour that view was saved with.
39
+ const RESET = "# -theme.palette.series";
37
40
  expect(annotations).toEqual({
38
- inl: NONE,
39
- comp: NONE,
40
- pipe: NONE,
41
- chain: undefined,
42
- paren: undefined,
41
+ inl: `${NONE}\n${RESET}`,
42
+ comp: `${NONE}\n${RESET}`,
43
+ pipe: `${NONE}\n${RESET}`,
44
+ chain: RESET,
45
+ paren: RESET,
43
46
  ref: undefined,
44
47
  });
45
48
  });
46
49
  });
47
50
 
51
+ describe("the Theme colour preview, for the colour lines a real dashboard reads", () => {
52
+ const COLORS = `## artifact { title="T" tiles=["a -> picked", "a -> listed", "a -> block", "a -> spelled"] }
53
+ import "../m.malloy"
54
+
55
+ source: a is one extend {
56
+ # theme.palette.series=["#7f9862"]
57
+ view: picked is { group_by: x }
58
+ # theme.palette.series=["#111111", "#222222"]
59
+ view: listed is { group_by: x }
60
+ # theme { palette.series=["#111111"] }
61
+ view: block is { group_by: x }
62
+ # theme.palette.series = [ '#111111' ]
63
+ view: spelled is { group_by: x }
64
+ }`;
65
+
66
+ it("takes a builder colour off on Theme, and keeps a palette the author wrote", async () => {
67
+ const document = await openDocument(COLORS);
68
+ const annotations = Object.fromEntries(
69
+ document.tiles.map((tile) => {
70
+ // Theme: what the Color item's Theme leaves on the tile.
71
+ const { color: _picked, ...theme } = tile as QueryTile;
72
+ return [
73
+ tile.name,
74
+ previewTileQuery(document, theme as QueryTile, new Set())
75
+ .annotation,
76
+ ];
77
+ }),
78
+ );
79
+ expect(annotations).toEqual({
80
+ picked: "# -theme.palette.series",
81
+ listed: undefined,
82
+ block: undefined,
83
+ spelled: undefined,
84
+ });
85
+ });
86
+ });
87
+
48
88
  describe("a filter field is read and written as it stands", () => {
49
89
  const FILTERS = `##! experimental.givens
50
90
  ## artifact { title="T" tiles=["a -> x", "a -> y", "a -> z"] }