@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
@@ -1034,3 +1034,102 @@ describe("ReportBuilder: saving", () => {
1034
1034
  expect(button("Save")).toBeDefined();
1035
1035
  });
1036
1036
  });
1037
+
1038
+ describe("ReportBuilder: arranging a placed report", () => {
1039
+ const PLACED = `## artifact { title="T" tiles=["a -> x" { row=1 col=1 colspan=6 rowspan=3 }, "a -> y" { row=9 col=1 colspan=6 rowspan=3 }] } dashboard { columns=12 }
1040
+ import "../m.malloy"
1041
+
1042
+ source: a is one extend {
1043
+ view: x is vx
1044
+
1045
+ view: y is vy
1046
+ }`;
1047
+ const mountPlaced = async (source = PLACED) => {
1048
+ const document = await openDocument(source);
1049
+ render(<ReportBuilder source={source} document={document} />);
1050
+ };
1051
+
1052
+ it("packs the tiles upward from the Arrange menu, as one undo step", async () => {
1053
+ await mountPlaced();
1054
+ expect(itemStyleOf("y")).toContain("grid-row: 9/span 3");
1055
+ fireEvent.click(button("Arrange tiles"));
1056
+ fireEvent.click(
1057
+ screen.getByRole("menuitem", { name: /Pack tiles upward/ }),
1058
+ );
1059
+ expect(itemStyleOf("y")).toContain("grid-row: 4/span 3");
1060
+ fireEvent.click(button("Undo"));
1061
+ expect(itemStyleOf("y")).toContain("grid-row: 9/span 3");
1062
+ });
1063
+
1064
+ it("lets the tiles flow again, keeping their sizes", async () => {
1065
+ await mountPlaced();
1066
+ fireEvent.click(button("Arrange tiles"));
1067
+ fireEvent.click(screen.getByRole("menuitem", { name: /Let tiles flow/ }));
1068
+ expect(itemStyleOf("y")).not.toMatch(/grid-row: \d+\//);
1069
+ expect(itemStyleOf("y")).toContain("height: 272px");
1070
+ // A report that flows has nothing to arrange.
1071
+ expect(
1072
+ screen.queryByRole("button", { name: "Arrange tiles", hidden: true }),
1073
+ ).toBeNull();
1074
+ });
1075
+
1076
+ it("offers no packing when there is no gap to close", async () => {
1077
+ await mountPlaced(PLACED.replace("row=9", "row=4"));
1078
+ fireEvent.click(button("Arrange tiles"));
1079
+ expect(
1080
+ screen
1081
+ .getByRole("menuitem", { name: /Pack tiles upward/ })
1082
+ .getAttribute("aria-disabled"),
1083
+ ).toBe("true");
1084
+ });
1085
+
1086
+ it("moves the selected tile a cell at a time with Option and the arrows", async () => {
1087
+ await mountPlaced();
1088
+ fireEvent.click(tile("x"));
1089
+ fireEvent.keyDown(window, { key: "ArrowRight", altKey: true });
1090
+ expect(itemStyleOf("x")).toContain("grid-column: 2/span 6");
1091
+ fireEvent.keyDown(window, { key: "ArrowDown", altKey: true });
1092
+ expect(itemStyleOf("x")).toContain("grid-row: 2/span 3");
1093
+ // Held on the grid: six columns wide, it goes no further right than 7.
1094
+ for (let i = 0; i < 10; i++)
1095
+ fireEvent.keyDown(window, { key: "ArrowRight", altKey: true });
1096
+ expect(itemStyleOf("x")).toContain("grid-column: 7/span 6");
1097
+ });
1098
+
1099
+ it("keeps the selection on the same tile when packing re-sorts the tiles", async () => {
1100
+ // y sits at row 9 beside nothing; packing moves it to row 4, and on a
1101
+ // report where it reads first after the move the order changes.
1102
+ await mountPlaced(
1103
+ PLACED.replace(
1104
+ '"a -> x" { row=1 col=1 colspan=6 rowspan=3 }',
1105
+ '"a -> x" { row=5 col=7 colspan=6 rowspan=3 }',
1106
+ ),
1107
+ );
1108
+ fireEvent.click(tile("y"));
1109
+ expect(tile("y").getAttribute("aria-current")).toBe("true");
1110
+ fireEvent.click(button("Arrange tiles"));
1111
+ fireEvent.click(
1112
+ screen.getByRole("menuitem", { name: /Pack tiles upward/ }),
1113
+ );
1114
+ expect(tile("y").getAttribute("aria-current")).toBe("true");
1115
+ expect(tile("x").getAttribute("aria-current")).toBe("false");
1116
+ });
1117
+
1118
+ it("leaves Option and an arrow to the browser when no placed tile is selected", async () => {
1119
+ await mountPlaced();
1120
+ const event = new KeyboardEvent("keydown", {
1121
+ key: "ArrowLeft",
1122
+ altKey: true,
1123
+ cancelable: true,
1124
+ });
1125
+ window.dispatchEvent(event);
1126
+ expect(event.defaultPrevented).toBe(false);
1127
+ });
1128
+
1129
+ it("offers no Arrange menu on a report that flows", async () => {
1130
+ await mount();
1131
+ expect(
1132
+ screen.queryByRole("button", { name: "Arrange tiles", hidden: true }),
1133
+ ).toBeNull();
1134
+ });
1135
+ });
@@ -26,7 +26,7 @@ import {
26
26
  } from "./builderControls";
27
27
  import { OpenDraftContext } from "./openDraft";
28
28
  import type { PackageCatalog } from "./catalog";
29
- import type { DashboardDocument, QueryTile } from "./document";
29
+ import { tileKey, type DashboardDocument, type QueryTile } from "./document";
30
30
  import { FilterStrip, type FixedFilter } from "./FilterStrip";
31
31
  import { clockTime, type SaveStatusProps } from "./SaveStatus";
32
32
  import { SELECTION_RING_PX } from "./TileFrame";
@@ -39,9 +39,18 @@ import type { SaveHandler } from "./useDocumentEditor";
39
39
  import { useOpenDraftCommit } from "./useOpenDraftCommit";
40
40
  import { useTileEditing } from "./useTileEditing";
41
41
  import { useTileReorder } from "./useTileReorder";
42
- import { settle } from "./placement";
43
- import { useTileHeight } from "./useTileHeight";
44
- import { useTileResize, widestFrom } from "./useTileResize";
42
+ import { ArrangeMenu } from "./ArrangeMenu";
43
+ import type { ArrangeActions } from "./builderControls";
44
+ import {
45
+ flowTiles,
46
+ hasGaps,
47
+ inReadingOrder,
48
+ pack,
49
+ settle,
50
+ stepPlaced,
51
+ widestFrom,
52
+ } from "./placement";
53
+ import { useEdgeResize, withShape } from "./useEdgeResize";
45
54
  import { layoutRules, withSpan } from "../Report/layoutRules";
46
55
  import { ReportPage } from "../Report/ReportPage";
47
56
 
@@ -63,7 +72,7 @@ export type { BuilderGiven } from "./controls";
63
72
  * draws its own card AND its own heading. Nesting one inside a card of ours
64
73
  * would show a card in a card under two titles — which is precisely not what a
65
74
  * reader sees. So every affordance is drawn AROUND whatever the caller renders:
66
- * an outline for selection, a grip and a menu in the corners.
75
+ * an outline for selection, a menu in the corner, and edges that resize.
67
76
  *
68
77
  * **Everything here is on the thing it changes.** Set a tile's width from its menu, and drag
69
78
  * the tile itself to reorder — into the empty end of a row
@@ -320,6 +329,17 @@ export function ReportBuilder({
320
329
  });
321
330
  // After a tile on a placed report changes size, whatever it now covers
322
331
  // is pushed down; a report that flows has nothing to settle.
332
+ // Write a new tile list as one edit, keeping the selection on the tile it
333
+ // was on: a placed report is written in reading order, so a resize, a
334
+ // pack or a flow can move the selected tile to another index.
335
+ const writeTiles = (next: DashboardDocument["tiles"], key?: string) => {
336
+ editor.update((draft) => {
337
+ draft.tiles = next;
338
+ });
339
+ if (key === undefined) return;
340
+ const landed = next.findIndex((each) => tileKey(each) === key);
341
+ if (landed >= 0) selectTile(landed);
342
+ };
323
343
  const placesTiles = rules.widths;
324
344
  const place = useCallback(
325
345
  (tiles: DashboardDocument["tiles"], index: number) =>
@@ -329,7 +349,7 @@ export function ReportBuilder({
329
349
  const { dragging } = reorder;
330
350
  // Dragging a tile's right edge sets its width, written once on release.
331
351
  // A tile with no `# colspan` is drawn as the reader draws it, at least
332
- // MIN_TILE_PX wide, rather than one column: the width handle and the row
352
+ // MIN_TILE_PX wide, rather than one column: the right edge and the row
333
353
  // arithmetic start from that span. Only drawn; nothing is written until
334
354
  // the author sets a width.
335
355
  const defaultSpan = useDefaultTileSpan(gridBox, columns);
@@ -342,32 +362,28 @@ export function ReportBuilder({
342
362
  ),
343
363
  [editor.document.tiles, defaultSpan],
344
364
  );
345
- const resizing = useTileResize({
365
+ // Dragging any edge or corner of a tile resizes it: the right and bottom
366
+ // edges set width and height, the left and top move those edges (placing a
367
+ // report that flows, as a move does), a corner both at once. Written once,
368
+ // on release, and on a placed report whatever it now covers moves down.
369
+ const edging = useEdgeResize({
346
370
  tiles: laidOut,
347
371
  columns,
348
372
  gridBox,
349
373
  onStart: selectTile,
350
- commit: (index, span) =>
351
- editor.update((draft) => {
352
- const tile = draft.tiles[index];
353
- if (tile)
354
- draft.tiles[index] = withSpan(tile, span, columns, defaultSpan);
355
- draft.tiles = place(draft.tiles, index);
356
- }),
357
- });
358
- // Dragging a tile's bottom edge sets its height in rows, the same way.
359
- const sizing = useTileHeight({
360
- tiles: editor.document.tiles,
361
- gridBox,
362
- onStart: selectTile,
363
- commit: (index, rows) =>
364
- editor.update((draft) => {
365
- const tile = draft.tiles[index];
366
- if (!tile) return;
367
- if (rows === undefined) delete tile.rowspan;
368
- else tile.rowspan = rows;
369
- draft.tiles = place(draft.tiles, index);
370
- }),
374
+ commit: ({ index, edges, shape, placed, placedAll }) => {
375
+ const all = placedAll ?? editor.document.tiles;
376
+ const tile = all[index];
377
+ if (!tile) return;
378
+ let next = withShape(tile, shape, edges, placed);
379
+ if (edges.e || edges.w)
380
+ next = withSpan(next, shape.colspan, columns, defaultSpan);
381
+ const resized = all.map((each, i) => (i === index ? next : each));
382
+ writeTiles(
383
+ placed ? inReadingOrder(settle(resized, columns, index)) : resized,
384
+ tileKey(tile),
385
+ );
386
+ },
371
387
  });
372
388
  const tiles = useTileEditing({
373
389
  editor,
@@ -393,8 +409,12 @@ export function ReportBuilder({
393
409
  editorDirty: editor.dirty,
394
410
  });
395
411
 
412
+ const edgeDragging = edging.drag !== undefined;
396
413
  const shortcuts = useMemo(
397
414
  () => ({
415
+ // While an edge is dragged: its release writes the layout the drag
416
+ // began from, so an undo or a move made mid-drag would be lost.
417
+ paused: edgeDragging,
398
418
  // Escape drops the selection — unless the menu or the filter window is open, in which case the key is theirs and they close on it themselves.
399
419
  escape: () => {
400
420
  // Not while a menu or a window is open: the key is theirs, and
@@ -407,6 +427,39 @@ export function ReportBuilder({
407
427
  )
408
428
  clearSelection();
409
429
  },
430
+ // Option/Alt and an arrow: a placed tile moves exactly one cell. A
431
+ // step into the tile beside it swaps the two; otherwise whatever it
432
+ // lands on is pushed down. Unlike a drop it never slides under a
433
+ // taller tile, so a key press moves it where the arrow points.
434
+ move: (rows: -1 | 0 | 1, cols: -1 | 0 | 1) => {
435
+ if (dragging || selected === undefined || !rules.widths || !arrange)
436
+ return false;
437
+ const tile = editor.document.tiles[selected];
438
+ if (!tile || tile.row === undefined || tile.col === undefined)
439
+ return false;
440
+ const next = stepPlaced(
441
+ editor.document.tiles,
442
+ selected,
443
+ rows,
444
+ cols,
445
+ columns,
446
+ );
447
+ // At the grid's edge: the key is still the builder's, so it does
448
+ // not fall through to the browser's Back or Forward.
449
+ if (next.every((each, i) => each === editor.document.tiles[i]))
450
+ return true;
451
+ const key = tileKey(tile);
452
+ editor.update((draft) => {
453
+ draft.tiles = inReadingOrder(
454
+ stepPlaced(draft.tiles, selected, rows, cols, columns),
455
+ );
456
+ });
457
+ const landed = inReadingOrder(next).findIndex(
458
+ (each) => tileKey(each) === key,
459
+ );
460
+ if (landed >= 0) selectTile(landed);
461
+ return true;
462
+ },
410
463
  nudge: (delta: 1 | -1) => {
411
464
  // The drag's keyboard sensor also reads the arrows, and a drop would write the drag-start width back.
412
465
  if (dragging || selected === undefined || !rules.widths || !arrange)
@@ -432,9 +485,11 @@ export function ReportBuilder({
432
485
  },
433
486
  }),
434
487
  [
488
+ edgeDragging,
435
489
  defaultSpan,
436
490
  arrange,
437
491
  place,
492
+ selectTile,
438
493
  editor,
439
494
  menu,
440
495
  filterDialog,
@@ -492,6 +547,28 @@ export function ReportBuilder({
492
547
  ? { dismissRestored: draft.dismissRestored }
493
548
  : {}),
494
549
  };
550
+ // The layout as a whole, once a grid report's tiles are placed: close its
551
+ // gaps, or let it flow again. Each is one undoable edit.
552
+ const tilesNow = editor.document.tiles;
553
+ const selectedKey =
554
+ selected !== undefined && tilesNow[selected]
555
+ ? tileKey(tilesNow[selected])
556
+ : undefined;
557
+ const arrangeActions: ArrangeActions | undefined =
558
+ rules.widths && arrange && isPlaced(tilesNow)
559
+ ? {
560
+ ...(hasGaps(tilesNow, columns)
561
+ ? {
562
+ pack: () =>
563
+ writeTiles(
564
+ inReadingOrder(pack(tilesNow, columns)),
565
+ selectedKey,
566
+ ),
567
+ }
568
+ : {}),
569
+ flow: () => writeTiles(flowTiles(tilesNow), selectedKey),
570
+ }
571
+ : undefined;
495
572
  useReportControls(onControlsChange, {
496
573
  ...saveStatus,
497
574
  ...(toolbarProps.onSave ? { save: toolbarProps.onSave } : {}),
@@ -512,6 +589,7 @@ export function ReportBuilder({
512
589
  ...(toolbarProps.onExit ? { exit: toolbarProps.onExit } : {}),
513
590
  ...(session.done ? { done: session.done } : {}),
514
591
  ...(restored?.pending ? { restore: draft.restoreOffered } : {}),
592
+ ...(arrangeActions ? { arrange: arrangeActions } : {}),
515
593
  });
516
594
  // The builder's actions: on the title's line, at the right, above the
517
595
  // description.
@@ -520,6 +598,9 @@ export function ReportBuilder({
520
598
  {...session.toolbarProps}
521
599
  {...(toolbar ? { actions: toolbar } : {})}
522
600
  {...(catalog ? { onAddTile: () => openAdd() } : {})}
601
+ {...(arrangeActions
602
+ ? { arrange: <ArrangeMenu arrange={arrangeActions} /> }
603
+ : {})}
523
604
  savesTo={savesTo}
524
605
  {...(saveLabel ? { saveLabel } : {})}
525
606
  {...(draftKey !== undefined && toolbarProps.onSave
@@ -673,8 +754,7 @@ export function ReportBuilder({
673
754
  editor={editor}
674
755
  arrange={arrange}
675
756
  reorder={reorder}
676
- resizing={resizing}
677
- sizing={sizing}
757
+ edging={edging}
678
758
  place={place}
679
759
  gridBox={gridBox}
680
760
  defaultSpan={defaultSpan}