@malloy-publisher/sdk 0.9.15 → 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 (65) hide show
  1. package/dist/{RenderedResult-BClN_PG6.cjs.js → RenderedResult-5hOs5XnV.cjs.js} +21 -4
  2. package/dist/{RenderedResult-YBzVMVH2.es.js → RenderedResult-DxmHGZNQ.es.js} +204 -187
  3. package/dist/builder/index.cjs.js +75 -75
  4. package/dist/builder/index.es.js +5329 -5193
  5. package/dist/builder-entry.d.ts +1 -1
  6. package/dist/client/api.d.ts +1 -1
  7. package/dist/components/Dashboard/DashboardGrid.d.ts +2 -0
  8. package/dist/components/Dashboard/DashboardTile.d.ts +3 -1
  9. package/dist/components/RenderedResult/ResultContainer.d.ts +8 -1
  10. package/dist/components/RenderedResult/ResultPanel.d.ts +3 -1
  11. package/dist/components/RenderedResult/resultSizing.d.ts +5 -0
  12. package/dist/components/ReportBuilder/ArrangeMenu.d.ts +9 -0
  13. package/dist/components/ReportBuilder/BuilderGrid.d.ts +5 -7
  14. package/dist/components/ReportBuilder/BuilderToolbar.d.ts +3 -1
  15. package/dist/components/ReportBuilder/ReportBuilder.d.ts +1 -1
  16. package/dist/components/ReportBuilder/TileFrame.d.ts +34 -22
  17. package/dist/components/ReportBuilder/builderControls.d.ts +19 -0
  18. package/dist/components/ReportBuilder/placement.d.ts +48 -2
  19. package/dist/components/ReportBuilder/sortable.d.ts +9 -0
  20. package/dist/components/ReportBuilder/useBuilderSession.d.ts +1 -1
  21. package/dist/components/ReportBuilder/useBuilderShortcuts.d.ts +9 -1
  22. package/dist/components/ReportBuilder/useEdgeResize.d.ts +84 -0
  23. package/dist/components/ReportBuilder/useTileReorder.d.ts +3 -0
  24. package/dist/index.cjs.js +5 -5
  25. package/dist/index.es.js +489 -488
  26. package/dist/theme/classes.d.ts +1 -1
  27. package/dist/{useCompiledDocument-BGk38oaW.es.js → useCompiledDocument-CaYP_OYG.es.js} +4021 -4007
  28. package/dist/{useCompiledDocument-D9t5ry_t.cjs.js → useCompiledDocument-zYEfLogf.cjs.js} +32 -32
  29. package/package.json +1 -1
  30. package/src/builder-entry.ts +1 -0
  31. package/src/components/Dashboard/DashboardGrid.tsx +2 -0
  32. package/src/components/Dashboard/DashboardTile.tsx +4 -0
  33. package/src/components/Dashboard/DashboardView.tsx +1 -0
  34. package/src/components/RenderedResult/RenderedResult.tsx +18 -0
  35. package/src/components/RenderedResult/ResultContainer.tsx +39 -3
  36. package/src/components/RenderedResult/ResultPanel.tsx +4 -0
  37. package/src/components/RenderedResult/resultSizing.ts +6 -0
  38. package/src/components/ReportBuilder/ArrangeMenu.tsx +80 -0
  39. package/src/components/ReportBuilder/BuilderBar.tsx +4 -0
  40. package/src/components/ReportBuilder/BuilderDialogs.tsx +8 -1
  41. package/src/components/ReportBuilder/BuilderGrid.tsx +61 -60
  42. package/src/components/ReportBuilder/BuilderToolbar.tsx +4 -0
  43. package/src/components/ReportBuilder/EditorSurface.tsx +9 -2
  44. package/src/components/ReportBuilder/ReportBuilder.spec.tsx +99 -0
  45. package/src/components/ReportBuilder/ReportBuilder.tsx +110 -30
  46. package/src/components/ReportBuilder/TileFrame.tsx +261 -310
  47. package/src/components/ReportBuilder/builderControls.ts +34 -0
  48. package/src/components/ReportBuilder/placement.spec.ts +208 -0
  49. package/src/components/ReportBuilder/placement.ts +175 -9
  50. package/src/components/ReportBuilder/readDocument.spec.ts +7 -17
  51. package/src/components/ReportBuilder/readDocument.ts +3 -6
  52. package/src/components/ReportBuilder/sortable.tsx +26 -2
  53. package/src/components/ReportBuilder/spliceDocument.spec.ts +0 -40
  54. package/src/components/ReportBuilder/spliceDocument.ts +0 -39
  55. package/src/components/ReportBuilder/useBuilderSession.ts +7 -2
  56. package/src/components/ReportBuilder/useBuilderShortcuts.ts +25 -1
  57. package/src/components/ReportBuilder/useEdgeResize.spec.tsx +324 -0
  58. package/src/components/ReportBuilder/useEdgeResize.ts +397 -0
  59. package/src/components/ReportBuilder/useTileReorder.ts +1 -1
  60. package/src/theme/classes.ts +1 -1
  61. package/dist/components/ReportBuilder/useTileHeight.d.ts +0 -39
  62. package/dist/components/ReportBuilder/useTileResize.d.ts +0 -40
  63. package/src/components/ReportBuilder/useTileHeight.ts +0 -149
  64. package/src/components/ReportBuilder/useTileResize.spec.tsx +0 -146
  65. package/src/components/ReportBuilder/useTileResize.ts +0 -148
@@ -36,6 +36,11 @@ export interface BuilderControls {
36
36
  /** Why a filter cannot be added here, when it cannot; `filter` is then absent. */
37
37
  filterUnavailable?: string;
38
38
  };
39
+ /**
40
+ * The layout as a whole, on a grid report whose tiles are placed: close
41
+ * its gaps, or let its tiles flow again. Absent on a report that flows.
42
+ */
43
+ arrange?: ArrangeActions;
39
44
  /** Leave the builder: asks first when edits are unsaved. Absent unless the host passed `onExit`. */
40
45
  exit?: () => void;
41
46
  /**
@@ -63,6 +68,21 @@ export interface BuilderControls {
63
68
  dismissRestored?: () => void;
64
69
  }
65
70
 
71
+ /**
72
+ * What can be done to a placed report's layout as a whole, as
73
+ * {@link BuilderControls.arrange} offers it.
74
+ */
75
+ export interface ArrangeActions {
76
+ /**
77
+ * Slide every tile up into the empty space above it. Present only when
78
+ * there is such a gap: absent means there is nothing to close, and a host
79
+ * draws the action disabled (or not at all).
80
+ */
81
+ pack?: () => void;
82
+ /** Take every tile's place off, so the report flows again; sizes are kept. */
83
+ flow: () => void;
84
+ }
85
+
66
86
  /** What a host passes to take over the builder's page-level controls. */
67
87
  export type BuilderControlsHandler = (
68
88
  controls: BuilderControls | undefined,
@@ -97,6 +117,8 @@ export function useReportControls(
97
117
  discard: () => latest.current.discard?.(),
98
118
  restore: () => latest.current.restore?.(),
99
119
  dismissRestored: () => latest.current.dismissRestored?.(),
120
+ pack: () => latest.current.arrange?.pack?.(),
121
+ flow: () => latest.current.arrange?.flow(),
100
122
  }),
101
123
  [],
102
124
  );
@@ -121,6 +143,8 @@ export function useReportControls(
121
143
  const restoredPending = controls.restored?.pending ?? false;
122
144
  const hasRestore = controls.restore !== undefined;
123
145
  const hasDismissRestored = controls.dismissRestored !== undefined;
146
+ const hasArrange = controls.arrange !== undefined;
147
+ const canPack = controls.arrange?.pack !== undefined;
124
148
 
125
149
  const shown = useMemo<BuilderControls>(
126
150
  () => ({
@@ -154,6 +178,14 @@ export function useReportControls(
154
178
  ...(hasDismissRestored
155
179
  ? { dismissRestored: actions.dismissRestored }
156
180
  : {}),
181
+ ...(hasArrange
182
+ ? {
183
+ arrange: {
184
+ ...(canPack ? { pack: actions.pack } : {}),
185
+ flow: actions.flow,
186
+ },
187
+ }
188
+ : {}),
157
189
  }),
158
190
  [
159
191
  actions,
@@ -175,6 +207,8 @@ export function useReportControls(
175
207
  restoredPending,
176
208
  hasRestore,
177
209
  hasDismissRestored,
210
+ hasArrange,
211
+ canPack,
178
212
  ],
179
213
  );
180
214
 
@@ -6,10 +6,16 @@ import type { DashboardTile, QueryTile } from "./document";
6
6
  import {
7
7
  cellAt,
8
8
  dropAt,
9
+ flowTiles,
10
+ hasGaps,
11
+ holdChartRows,
12
+ minRowsOf,
13
+ pack,
9
14
  placeAtEnd,
10
15
  inReadingOrder,
11
16
  placeWhereDrawn,
12
17
  settle,
18
+ stepPlaced,
13
19
  trackWidth,
14
20
  } from "./placement";
15
21
 
@@ -191,3 +197,205 @@ describe("dropAt, far below", () => {
191
197
  expect(dropAt(tiles, 1, { row: 40, col: 7 }, 12)[1].row).toBe(4);
192
198
  });
193
199
  });
200
+
201
+ describe("pack", () => {
202
+ it("slides every tile up into the gaps above it, never sideways", () => {
203
+ const tiles = [
204
+ tile("a", { row: 1, col: 1, colspan: 6, rowspan: 3 }),
205
+ tile("b", { row: 7, col: 1, colspan: 6, rowspan: 2 }),
206
+ tile("c", { row: 5, col: 7, colspan: 6, rowspan: 4 }),
207
+ ];
208
+ expect(hasGaps(tiles, 12)).toBe(true);
209
+ expect(where(pack(tiles, 12))).toEqual([
210
+ { name: "a", row: 1, col: 1, colspan: 6, rowspan: 3 },
211
+ { name: "b", row: 4, col: 1, colspan: 6, rowspan: 2 },
212
+ { name: "c", row: 1, col: 7, colspan: 6, rowspan: 4 },
213
+ ]);
214
+ expect(hasGaps(pack(tiles, 12), 12)).toBe(false);
215
+ });
216
+
217
+ it("stops a tile under the lowest tile it would pass through", () => {
218
+ const tiles = [
219
+ tile("wide", { row: 1, col: 1, colspan: 12, rowspan: 2 }),
220
+ tile("low", { row: 9, col: 4, colspan: 4, rowspan: 2 }),
221
+ ];
222
+ expect(pack(tiles, 12)[1].row).toBe(3);
223
+ });
224
+ });
225
+
226
+ describe("flowTiles", () => {
227
+ it("takes every place off and keeps sizes, in reading order", () => {
228
+ const tiles = [
229
+ tile("b", { row: 4, col: 1, colspan: 6, rowspan: 3 }),
230
+ tile("a", { row: 1, col: 7, colspan: 6, rowspan: 2 }),
231
+ ];
232
+ expect(where(flowTiles(tiles))).toEqual([
233
+ { name: "a", row: undefined, col: undefined, colspan: 6, rowspan: 2 },
234
+ { name: "b", row: undefined, col: undefined, colspan: 6, rowspan: 3 },
235
+ ]);
236
+ });
237
+ });
238
+
239
+ describe("placeWhereDrawn keeps rows where they are drawn", () => {
240
+ it("takes each edge to its nearest row line, so stacked tiles still touch and nothing is pushed", () => {
241
+ const track = trackWidth(1200, 12);
242
+ const half = 6 * (track + 16) - 16;
243
+ // Four rows of two auto tiles, each 300px tall with a 16px gap: drawn
244
+ // tops at 0, 316, 632, 948.
245
+ const tiles = Array.from({ length: 8 }, (_, i) =>
246
+ tile(`t${i}`, { colspan: 6 }),
247
+ );
248
+ const boxes = tiles.map((_, i) => ({
249
+ top: Math.floor(i / 2) * 316,
250
+ left: i % 2 === 0 ? 0 : half + 16,
251
+ width: half,
252
+ height: 300,
253
+ }));
254
+ const placed = placeWhereDrawn(tiles, boxes, 12, track);
255
+ // Every row's top stays within half a row (48px) of where it was drawn.
256
+ for (const [i, each] of placed.entries())
257
+ expect(
258
+ Math.abs((each.row! - 1) * 96 - boxes[i].top),
259
+ ).toBeLessThanOrEqual(48);
260
+ // And each row starts right where the one above ends: no row was pushed.
261
+ for (let r = 2; r < 8; r += 2)
262
+ expect(placed[r].row).toBe(
263
+ placed[r - 2].row! + placed[r - 2].rowspan!,
264
+ );
265
+ });
266
+ });
267
+
268
+ describe("minRowsOf", () => {
269
+ const drawn = (html: string) => {
270
+ const frame = document.createElement("div");
271
+ frame.innerHTML = html;
272
+ return frame;
273
+ };
274
+ it("gives a chart three rows, value cards and a table two, and prose one", () => {
275
+ expect(minRowsOf(tile("chart"), drawn('<svg class="plot"></svg>'))).toBe(
276
+ 3,
277
+ );
278
+ expect(
279
+ minRowsOf(tile("kpis"), drawn('<div class="malloy-big-value"></div>')),
280
+ ).toBe(2);
281
+ expect(
282
+ minRowsOf(tile("rows"), drawn('<div class="malloy-table"></div>')),
283
+ ).toBe(2);
284
+ expect(
285
+ minRowsOf({ kind: "text", name: "note", markdown: "" }, null),
286
+ ).toBe(1);
287
+ });
288
+
289
+ it("takes a query tile for a chart until it has drawn", () => {
290
+ expect(minRowsOf(tile("t"))).toBe(3);
291
+ });
292
+
293
+ it("gives a dashboard with a chart beside its cards a chart's rows", () => {
294
+ expect(
295
+ minRowsOf(
296
+ tile("mixed"),
297
+ drawn(
298
+ '<div class="malloy-dashboard"><div class="malloy-big-value"></div><div class="malloy-chart"></div></div>',
299
+ ),
300
+ ),
301
+ ).toBe(3);
302
+ });
303
+
304
+ it("lets a chart inside a card or a table's cell size with its host", () => {
305
+ expect(
306
+ minRowsOf(
307
+ tile("spark"),
308
+ drawn(
309
+ '<div class="malloy-big-value"><div class="malloy-chart"></div></div>',
310
+ ),
311
+ ),
312
+ ).toBe(2);
313
+ expect(
314
+ minRowsOf(
315
+ tile("cells"),
316
+ drawn(
317
+ '<div class="malloy-table"><div class="malloy-chart"></div></div>',
318
+ ),
319
+ ),
320
+ ).toBe(2);
321
+ });
322
+ });
323
+
324
+ describe("holdChartRows", () => {
325
+ it("raises a tile switched to a plotted chart to a chart's rows", () => {
326
+ expect(
327
+ holdChartRows(tile("t", { rowspan: 2, chart: "line_chart" })).rowspan,
328
+ ).toBe(3);
329
+ });
330
+ it("leaves value cards, a table, and a tile on auto height alone", () => {
331
+ expect(
332
+ holdChartRows(tile("t", { rowspan: 2, chart: "big_value" })).rowspan,
333
+ ).toBe(2);
334
+ expect(
335
+ holdChartRows(tile("t", { rowspan: 2, chart: "none" })).rowspan,
336
+ ).toBe(2);
337
+ expect(
338
+ holdChartRows(tile("t", { chart: "bar_chart" })).rowspan,
339
+ ).toBeUndefined();
340
+ });
341
+ });
342
+
343
+ describe("stepPlaced", () => {
344
+ const at = (tiles: DashboardTile[], name: string) => {
345
+ const t = tiles.find((each) => each.name === name)!;
346
+ return [t.row, t.col];
347
+ };
348
+ const stacked = () => [
349
+ tile("a", { row: 1, col: 1, colspan: 6, rowspan: 4 }),
350
+ tile("b", { row: 5, col: 1, colspan: 6, rowspan: 3 }),
351
+ ];
352
+
353
+ it("swaps a tile down past the one under it", () => {
354
+ const next = stepPlaced(stacked(), 0, 1, 0, 12);
355
+ expect(at(next, "b")).toEqual([1, 1]);
356
+ expect(at(next, "a")).toEqual([4, 1]);
357
+ });
358
+
359
+ it("swaps a tile up past the one over it, the mirror of down", () => {
360
+ const next = stepPlaced(stacked(), 1, -1, 0, 12);
361
+ expect(at(next, "b")).toEqual([1, 1]);
362
+ expect(at(next, "a")).toEqual([4, 1]);
363
+ });
364
+
365
+ it("swaps tiles side by side", () => {
366
+ const tiles = [
367
+ tile("a", { row: 1, col: 1, colspan: 4, rowspan: 3 }),
368
+ tile("b", { row: 1, col: 5, colspan: 8, rowspan: 3 }),
369
+ ];
370
+ const right = stepPlaced(tiles, 0, 0, 1, 12);
371
+ expect(at(right, "b")).toEqual([1, 1]);
372
+ expect(at(right, "a")).toEqual([1, 9]);
373
+ const left = stepPlaced(tiles, 1, 0, -1, 12);
374
+ expect(at(left, "b")).toEqual([1, 1]);
375
+ expect(at(left, "a")).toEqual([1, 9]);
376
+ });
377
+
378
+ it("steps one cell into empty space", () => {
379
+ const next = stepPlaced(stacked(), 0, 0, 1, 12);
380
+ expect(at(next, "a")).toEqual([1, 2]);
381
+ expect(at(next, "b")).toEqual([5, 1]);
382
+ });
383
+
384
+ it("pushes down what it lands on when two tiles are in the way", () => {
385
+ const tiles = [
386
+ tile("a", { row: 1, col: 1, colspan: 12, rowspan: 3 }),
387
+ tile("b", { row: 4, col: 1, colspan: 6, rowspan: 3 }),
388
+ tile("c", { row: 4, col: 7, colspan: 6, rowspan: 3 }),
389
+ ];
390
+ const next = stepPlaced(tiles, 0, 1, 0, 12);
391
+ expect(at(next, "a")).toEqual([2, 1]);
392
+ expect(at(next, "b")).toEqual([5, 1]);
393
+ expect(at(next, "c")).toEqual([5, 7]);
394
+ });
395
+
396
+ it("leaves the tiles as they were at the grid's edge", () => {
397
+ const tiles = stacked();
398
+ const next = stepPlaced(tiles, 0, -1, 0, 12);
399
+ expect(next.every((each, i) => each === tiles[i])).toBe(true);
400
+ });
401
+ });
@@ -5,9 +5,11 @@ import {
5
5
  GRID_GAP_PX,
6
6
  MIN_TEXT_TILE_ROWS,
7
7
  MIN_TILE_ROWS,
8
+ MIN_VALUE_TILE_ROWS,
8
9
  PLACED_DEFAULT_ROWSPAN,
9
10
  TILE_ROW_PX,
10
11
  } from "../Dashboard/DashboardGrid";
12
+ import { isChartPick } from "./chartLine";
11
13
  import { isTextTile, type DashboardTile } from "./document";
12
14
 
13
15
  /**
@@ -29,9 +31,34 @@ export interface Cell {
29
31
  rowspan: number;
30
32
  }
31
33
 
32
- /** The fewest rows the builder offers a tile: a chart needs room to draw, prose does not. */
33
- export const minRowsOf = (tile: DashboardTile) =>
34
- isTextTile(tile) ? MIN_TEXT_TILE_ROWS : MIN_TILE_ROWS;
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;
35
62
 
36
63
  /** Rows a tile takes on a placed report: its own, or its kind's default. */
37
64
  export const rowsOf = (tile: DashboardTile) =>
@@ -213,16 +240,20 @@ export function placeWhereDrawn(
213
240
  1,
214
241
  Math.round((box.width + GRID_GAP_PX) / (track + GRID_GAP_PX)),
215
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
+ );
216
251
  return {
217
252
  ...rest,
218
- ...cellAt(box.left, box.top, colspan, columns, track),
253
+ ...cell,
219
254
  colspan,
220
255
  rowspan:
221
- tile.rowspan ??
222
- Math.max(
223
- minRowsOf(tile),
224
- Math.round((box.height + GRID_GAP_PX) / ROW_STEP),
225
- ),
256
+ tile.rowspan ?? Math.max(minRowsOf(tile), lastRow - cell.row + 1),
226
257
  };
227
258
  });
228
259
  return settle(placed, columns);
@@ -248,3 +279,138 @@ export function placeAtEnd(
248
279
  i === index ? { ...tile, row: bottom, col: 1 } : tile,
249
280
  );
250
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
+ }
@@ -192,42 +192,32 @@ describe("blockAbove", () => {
192
192
  });
193
193
 
194
194
  describe("readDashboardDocument", () => {
195
- it("reads a width spelled only as the dashboard_columns alias", async () => {
196
- const doc = await read(
197
- SIMPLE.replace(
198
- '"a -> by_cat"] givens { CATEGORY="Jeans" } } dashboard { columns=12 }',
199
- '"a -> by_cat"] givens { CATEGORY="Jeans" } dashboard_columns=8 }',
200
- ),
201
- );
202
- expect(doc.columns).toBe(8);
203
- });
204
-
205
195
  describe("a width written badly", () => {
206
196
  const withTag = (tagLine: string) =>
207
197
  SIMPLE.replace(/^## artifact.*$/m, tagLine);
208
198
 
209
- it("lets a written columns win even when it is not a width", async () => {
199
+ it("leaves the default width for a columns that is not a width", async () => {
210
200
  const doc = await read(
211
201
  withTag(
212
- '## artifact { tiles=["a -> by_cat"] dashboard_columns=8 } dashboard { columns=1.5 }',
202
+ '## artifact { tiles=["a -> by_cat"] } dashboard { columns=1.5 }',
213
203
  ),
214
204
  );
215
205
  expect(doc.columns).toBeUndefined();
216
206
  });
217
207
 
218
- it("reads the alias, spaced round its =, when no columns is written", async () => {
208
+ it("takes text that only begins with digits as no width", async () => {
219
209
  const doc = await read(
220
210
  withTag(
221
- '## artifact { tiles=["a -> by_cat"] dashboard_columns = 8 }',
211
+ '## artifact { tiles=["a -> by_cat"] } dashboard { columns="8px" }',
222
212
  ),
223
213
  );
224
- expect(doc.columns).toBe(8);
214
+ expect(doc.columns).toBeUndefined();
225
215
  });
226
216
 
227
- it("takes text that only begins with digits as no width", async () => {
217
+ it("reads no width from the removed dashboard_columns spelling", async () => {
228
218
  const doc = await read(
229
219
  withTag(
230
- '## artifact { tiles=["a -> by_cat"] dashboard_columns=8px }',
220
+ '## artifact { tiles=["a -> by_cat"] dashboard_columns=8 }',
231
221
  ),
232
222
  );
233
223
  expect(doc.columns).toBeUndefined();
@@ -721,14 +721,11 @@ async function readDocumentText(sourceText: string): Promise<ReadResult> {
721
721
  if (value !== undefined) startingGivens[key] = value;
722
722
  }
723
723
 
724
- // The server's rule: a written `dashboard { columns }` wins even when it is
725
- // not a width (the default width then applies), and the alias only counts
726
- // beside `tiles`.
724
+ // The server's rule: a `dashboard { columns }` that is not a width leaves
725
+ // the default width.
727
726
  const columnsTag = tag?.tag("dashboard")?.has("columns")
728
727
  ? gridWidth(tag.tag("dashboard"), "columns")
729
- : artifactTag?.array("tiles")
730
- ? gridWidth(artifactTag, "dashboard_columns")
731
- : undefined;
728
+ : undefined;
732
729
 
733
730
  return {
734
731
  ok: true,
@@ -3,9 +3,9 @@
3
3
 
4
4
  import { pointerIntersection } from "@dnd-kit/collision";
5
5
  import { Feedback, KeyboardSensor, PointerSensor } from "@dnd-kit/dom";
6
- import { useDroppable } from "@dnd-kit/react";
6
+ import { useDragDropManager, useDroppable } from "@dnd-kit/react";
7
7
  import { useSortable, type UseSortableInput } from "@dnd-kit/react/sortable";
8
- import type { ReactNode } from "react";
8
+ import { useEffect, type ReactNode } from "react";
9
9
 
10
10
  /**
11
11
  * The builder's drag and drop, on `@dnd-kit/react`.
@@ -162,3 +162,27 @@ export function GapDroppable({
162
162
  });
163
163
  return <>{children({ ref, isDropTarget })}</>;
164
164
  }
165
+
166
+ /**
167
+ * Escape cancels a drag wherever focus is. The library's pointer drag listens
168
+ * for Escape only on the element under the pointer, and a press on a tile's
169
+ * padding focuses nothing, so the key reached the page instead and the drop
170
+ * landed on release. Mounted inside the drag provider while a drag is live.
171
+ */
172
+ export function EscapeCancelsDrag({ active }: { active: boolean }) {
173
+ const manager = useDragDropManager();
174
+ useEffect(() => {
175
+ if (!active || !manager) return;
176
+ const onKey = (event: KeyboardEvent) => {
177
+ if (event.key !== "Escape" || manager.dragOperation.status.idle)
178
+ return;
179
+ // The drag's, not the builder's: it must not also drop the selection.
180
+ event.preventDefault();
181
+ event.stopPropagation();
182
+ manager.actions.stop({ event, canceled: true });
183
+ };
184
+ window.addEventListener("keydown", onKey, true);
185
+ return () => window.removeEventListener("keydown", onKey, true);
186
+ }, [active, manager]);
187
+ return null;
188
+ }
@@ -1093,24 +1093,6 @@ describe("spliceDashboardDocument: the page's own settings", () => {
1093
1093
  expect(flowed).not.toContain("dashboard {");
1094
1094
  });
1095
1095
 
1096
- it("replaces the dashboard_columns alias when a width is set, and the result lints clean", async () => {
1097
- const aliased = SOURCE.replace(
1098
- '"a -> by_brand"] } dashboard { columns=12 }',
1099
- '"a -> by_brand"] dashboard_columns=8 }',
1100
- );
1101
- expect(aliased).toContain("dashboard_columns=8");
1102
- const out = await spliced(aliased, (d) => {
1103
- d.columns = 12;
1104
- });
1105
- expect(out).not.toContain("dashboard_columns");
1106
- expect(out).toContain("dashboard { columns=12 }");
1107
- const removed = await spliced(aliased, (d) => {
1108
- delete d.columns;
1109
- });
1110
- expect(removed).not.toContain("dashboard_columns");
1111
- expect(removed).not.toContain("dashboard {");
1112
- });
1113
-
1114
1096
  it("adds and removes autorun and starting values inside the tag", async () => {
1115
1097
  const out = await spliced(SOURCE, (d) => {
1116
1098
  d.autorun = false;
@@ -1199,28 +1181,6 @@ describe("spliceDashboardDocument: the page's own settings", () => {
1199
1181
  lintNotebookText("dashboards/d.malloy", source).map((f) => f.code);
1200
1182
  const TAIL = `import "../m.malloy"\n\nsource: a is one extend {\n view: x is vx\n}`;
1201
1183
 
1202
- describe("a width change against the deprecated alias", () => {
1203
- it("removes the alias whatever spacing surrounds its =", async () => {
1204
- const source = `## artifact { title="T" tiles=["a -> x"] dashboard_columns = 3 }\n${TAIL}`;
1205
- const out = await spliced(source, (d) => {
1206
- d.columns = 6;
1207
- });
1208
- expect(out).not.toContain("dashboard_columns");
1209
- expect(out).toContain("dashboard { columns=6 }");
1210
- expect(lintCodes(out)).toEqual([]);
1211
- });
1212
-
1213
- it("removes the alias, not the same words inside a quoted title", async () => {
1214
- const source = `## artifact { title="a dashboard_columns=2 b" tiles=["a -> x"] dashboard_columns=3 }\n${TAIL}`;
1215
- const out = await spliced(source, (d) => {
1216
- d.columns = 6;
1217
- });
1218
- expect(out).toContain('title="a dashboard_columns=2 b"');
1219
- expect(out.match(/dashboard_columns/g)).toHaveLength(1);
1220
- expect(lintCodes(out)).toEqual([]);
1221
- });
1222
- });
1223
-
1224
1184
  it("reorders a list holding a text tile, keeping its entry as written", async () => {
1225
1185
  const source = fs
1226
1186
  .readFileSync(