@malloy-publisher/sdk 0.9.13 → 0.9.15

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 (148) hide show
  1. package/dist/RenderedResult-BClN_PG6.cjs.js +154 -0
  2. package/dist/{RenderedResult-DnBtf-cI.es.js → RenderedResult-YBzVMVH2.es.js} +295 -234
  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 +7430 -6548
  7. package/dist/client/api.d.ts +30 -6
  8. package/dist/client/index.cjs.js +1 -1
  9. package/dist/client/index.es.js +1 -1
  10. package/dist/components/Dashboard/DashboardGrid.d.ts +49 -1
  11. package/dist/components/Dashboard/DashboardTile.d.ts +5 -2
  12. package/dist/components/Dashboard/DashboardView.d.ts +9 -2
  13. package/dist/components/Dashboard/index.d.ts +1 -1
  14. package/dist/components/Dashboard/promoteMeasureRow.d.ts +5 -7
  15. package/dist/components/Dashboard/useStuck.d.ts +18 -12
  16. package/dist/components/RenderedResult/RenderedResult.d.ts +3 -2
  17. package/dist/components/RenderedResult/ResultContainer.d.ts +11 -1
  18. package/dist/components/RenderedResult/ResultPanel.d.ts +5 -1
  19. package/dist/components/RenderedResult/chartPresentation.d.ts +15 -0
  20. package/dist/components/RenderedResult/chartShapeProblem.d.ts +55 -0
  21. package/dist/components/RenderedResult/resultSizing.d.ts +42 -6
  22. package/dist/components/Report/Report.d.ts +7 -1
  23. package/dist/components/Report/ReportPage.d.ts +7 -0
  24. package/dist/components/Report/layoutRules.d.ts +45 -0
  25. package/dist/components/ReportBuilder/AddTileDialog.d.ts +2 -1
  26. package/dist/components/ReportBuilder/BuilderGrid.d.ts +14 -1
  27. package/dist/components/ReportBuilder/ChartPicker.d.ts +4 -2
  28. package/dist/components/ReportBuilder/EditorSurface.d.ts +17 -1
  29. package/dist/components/ReportBuilder/ReportEditor.d.ts +18 -0
  30. package/dist/components/ReportBuilder/TileFrame.d.ts +33 -1
  31. package/dist/components/ReportBuilder/TileMenu.d.ts +5 -2
  32. package/dist/components/ReportBuilder/catalog.d.ts +23 -0
  33. package/dist/components/ReportBuilder/chartLine.d.ts +38 -5
  34. package/dist/components/ReportBuilder/controls.d.ts +2 -2
  35. package/dist/components/ReportBuilder/document.d.ts +53 -5
  36. package/dist/components/ReportBuilder/imports.d.ts +7 -0
  37. package/dist/components/ReportBuilder/placement.d.ts +75 -0
  38. package/dist/components/ReportBuilder/preview.d.ts +59 -10
  39. package/dist/components/ReportBuilder/sortable.d.ts +3 -1
  40. package/dist/components/ReportBuilder/useTileHeight.d.ts +39 -0
  41. package/dist/components/ReportBuilder/useTileReorder.d.ts +27 -3
  42. package/dist/components/ReportBuilder/useTileResize.d.ts +8 -0
  43. package/dist/index.cjs.js +7 -7
  44. package/dist/index.es.js +3328 -3287
  45. package/dist/text/index.es.js +1 -1
  46. package/dist/{text-entry-72YFLeNk.es.js → text-entry-YKJZbRPo.es.js} +2 -2
  47. package/dist/theme/resolveTheme.d.ts +15 -0
  48. package/dist/{useCompiledDocument-B4QF1mIl.es.js → useCompiledDocument-BGk38oaW.es.js} +5097 -4805
  49. package/dist/useCompiledDocument-D9t5ry_t.cjs.js +142 -0
  50. package/dist/utils/blankQuoted.d.ts +15 -0
  51. package/package.json +1 -1
  52. package/src/components/Dashboard/DashboardGrid.tsx +127 -10
  53. package/src/components/Dashboard/DashboardTile.tsx +11 -1
  54. package/src/components/Dashboard/DashboardView.tsx +63 -10
  55. package/src/components/Dashboard/TileCard.tsx +6 -0
  56. package/src/components/Dashboard/index.ts +1 -0
  57. package/src/components/Dashboard/promoteMeasureRow.spec.ts +19 -0
  58. package/src/components/Dashboard/promoteMeasureRow.ts +15 -36
  59. package/src/components/Dashboard/tileHeight.spec.ts +72 -0
  60. package/src/components/Dashboard/useStuck.ts +76 -28
  61. package/src/components/Package/NewDocumentDialog.spec.tsx +20 -16
  62. package/src/components/Package/NewDocumentDialog.tsx +10 -11
  63. package/src/components/RenderedResult/RenderedResult.lifecycle.spec.tsx +167 -2
  64. package/src/components/RenderedResult/RenderedResult.tsx +21 -6
  65. package/src/components/RenderedResult/ResultContainer.tsx +60 -7
  66. package/src/components/RenderedResult/ResultPanel.tsx +21 -1
  67. package/src/components/RenderedResult/chartPresentation.spec.ts +153 -0
  68. package/src/components/RenderedResult/chartPresentation.ts +166 -0
  69. package/src/components/RenderedResult/chartShapeProblem.spec.tsx +245 -0
  70. package/src/components/RenderedResult/chartShapeProblem.ts +219 -0
  71. package/src/components/RenderedResult/resultSizing.spec.ts +75 -3
  72. package/src/components/RenderedResult/resultSizing.ts +65 -11
  73. package/src/components/Report/Report.spec.tsx +19 -0
  74. package/src/components/Report/Report.tsx +8 -1
  75. package/src/components/Report/ReportPage.tsx +13 -1
  76. package/src/components/Report/layoutRules.spec.ts +43 -3
  77. package/src/components/Report/layoutRules.ts +70 -0
  78. package/src/components/Report/tileOverflow.spec.tsx +162 -0
  79. package/src/components/ReportBuilder/AddTileDialog.spec.tsx +2 -2
  80. package/src/components/ReportBuilder/AddTileDialog.tsx +11 -6
  81. package/src/components/ReportBuilder/BuilderBar.tsx +1 -1
  82. package/src/components/ReportBuilder/BuilderDialogs.tsx +20 -0
  83. package/src/components/ReportBuilder/BuilderGrid.tsx +126 -20
  84. package/src/components/ReportBuilder/ChartPicker.spec.ts +91 -4
  85. package/src/components/ReportBuilder/ChartPicker.tsx +45 -10
  86. package/src/components/ReportBuilder/DrillDialog.tsx +8 -2
  87. package/src/components/ReportBuilder/EditorSurface.tsx +110 -28
  88. package/src/components/ReportBuilder/FilterDialog.tsx +17 -8
  89. package/src/components/ReportBuilder/FilterStrip.tsx +2 -1
  90. package/src/components/ReportBuilder/ReportBuilder.charts.spec.tsx +193 -2
  91. package/src/components/ReportBuilder/ReportBuilder.controls.spec.tsx +4 -2
  92. package/src/components/ReportBuilder/ReportBuilder.notebook.spec.tsx +2 -2
  93. package/src/components/ReportBuilder/ReportBuilder.spec.tsx +143 -2
  94. package/src/components/ReportBuilder/ReportBuilder.tsx +73 -19
  95. package/src/components/ReportBuilder/ReportEditor.fileModel.spec.tsx +178 -0
  96. package/src/components/ReportBuilder/ReportEditor.modelFilters.spec.tsx +3 -5
  97. package/src/components/ReportBuilder/ReportEditor.queryVersion.spec.tsx +292 -0
  98. package/src/components/ReportBuilder/ReportEditor.readOnlyTitle.spec.tsx +92 -0
  99. package/src/components/ReportBuilder/ReportEditor.tsx +35 -0
  100. package/src/components/ReportBuilder/TileFrame.tsx +251 -6
  101. package/src/components/ReportBuilder/TileMenu.tsx +259 -20
  102. package/src/components/ReportBuilder/ViewPicker.tsx +4 -3
  103. package/src/components/ReportBuilder/addTileToDocument.spec.ts +34 -0
  104. package/src/components/ReportBuilder/addTileToDocument.ts +26 -1
  105. package/src/components/ReportBuilder/catalog.aggregate.spec.ts +137 -1
  106. package/src/components/ReportBuilder/catalog.ts +90 -37
  107. package/src/components/ReportBuilder/chartLine.spec.ts +42 -5
  108. package/src/components/ReportBuilder/chartLine.ts +80 -2
  109. package/src/components/ReportBuilder/controls.ts +4 -3
  110. package/src/components/ReportBuilder/document.ts +65 -7
  111. package/src/components/ReportBuilder/emptyStart.spec.ts +1 -1
  112. package/src/components/ReportBuilder/entryLayout.spec.ts +151 -0
  113. package/src/components/ReportBuilder/imports.ts +1 -1
  114. package/src/components/ReportBuilder/invariants.spec.ts +18 -10
  115. package/src/components/ReportBuilder/layout.ts +3 -0
  116. package/src/components/ReportBuilder/placement.spec.ts +193 -0
  117. package/src/components/ReportBuilder/placement.ts +250 -0
  118. package/src/components/ReportBuilder/preview.shapes.spec.ts +46 -6
  119. package/src/components/ReportBuilder/preview.spec.ts +151 -11
  120. package/src/components/ReportBuilder/preview.ts +203 -40
  121. package/src/components/ReportBuilder/preview.unsaved.compile.spec.ts +190 -0
  122. package/src/components/ReportBuilder/readDocument.spec.ts +46 -2
  123. package/src/components/ReportBuilder/readDocument.ts +194 -23
  124. package/src/components/ReportBuilder/scannerShapes.spec.ts +10 -10
  125. package/src/components/ReportBuilder/sortable.tsx +4 -0
  126. package/src/components/ReportBuilder/spliceDocument.chart.spec.ts +1 -1
  127. package/src/components/ReportBuilder/spliceDocument.color.spec.ts +80 -0
  128. package/src/components/ReportBuilder/spliceDocument.colspan.spec.ts +78 -0
  129. package/src/components/ReportBuilder/spliceDocument.spec.ts +110 -18
  130. package/src/components/ReportBuilder/spliceDocument.ts +139 -52
  131. package/src/components/ReportBuilder/statementExtent.spec.ts +3 -3
  132. package/src/components/ReportBuilder/testing/reportGivenOrder.json +42 -0
  133. package/src/components/ReportBuilder/textTiles.spec.ts +2 -2
  134. package/src/components/ReportBuilder/tileDisplayTitle.ts +2 -2
  135. package/src/components/ReportBuilder/useTileEditing.ts +16 -3
  136. package/src/components/ReportBuilder/useTileHeight.ts +149 -0
  137. package/src/components/ReportBuilder/useTileReorder.ts +143 -6
  138. package/src/components/ReportBuilder/useTileResize.ts +16 -1
  139. package/src/components/given/FilterButton.spec.tsx +27 -11
  140. package/src/components/given/FilterButton.tsx +19 -6
  141. package/src/theme/resolveTheme.spec.ts +72 -1
  142. package/src/theme/resolveTheme.ts +43 -0
  143. package/src/utils/blankQuoted.spec.ts +50 -0
  144. package/src/utils/blankQuoted.ts +32 -0
  145. package/src/utils/malloyText.ts +2 -1
  146. package/dist/RenderedResult-DtQtNV0d.cjs.js +0 -153
  147. package/dist/ServerProvider-BIFPCzTD.cjs.js +0 -1
  148. package/dist/useCompiledDocument-D0OAMejE.cjs.js +0 -142
@@ -0,0 +1,151 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { describe, expect, it } from "bun:test";
5
+ import { isTextTile } from "./document";
6
+ import { openDocument, queryTile, spliced } from "./testing/fixtures";
7
+
8
+ /**
9
+ * A tile's layout lives on its entry in `tiles=[…]`: the place this report
10
+ * puts it. The reader takes the entry over the view's own tags, as the server
11
+ * does, and the writer puts a changed layout there and takes the view's
12
+ * `# colspan` and `# break` off, so the layout has one home.
13
+ */
14
+ const TAGGED = `## artifact { layout=grid title="T" tiles=["a -> kpis", "a -> trend"] } dashboard { columns=12 }
15
+ import "../m.malloy"
16
+
17
+ source: a is one extend {
18
+ # colspan=6
19
+ # break
20
+ # label="KPIs"
21
+ view: kpis is vk
22
+
23
+ view: trend is vt
24
+ }`;
25
+
26
+ describe("entry layout: reading", () => {
27
+ it("takes the entry's colspan, break and rowspan over the view's tags", async () => {
28
+ const d = await openDocument(
29
+ TAGGED.replace(
30
+ '"a -> kpis", "a -> trend"',
31
+ '"a -> kpis" { colspan=4 break=false rowspan=3 }, "a -> trend" { break }',
32
+ ),
33
+ );
34
+ expect(queryTile(d, 0)).toMatchObject({ colspan: 4, rowspan: 3 });
35
+ expect(queryTile(d, 0).break).toBeUndefined();
36
+ expect(queryTile(d, 1)).toMatchObject({ break: true });
37
+ });
38
+
39
+ it("still reads a view's own tags when the entry carries none", async () => {
40
+ const d = await openDocument(TAGGED);
41
+ expect(queryTile(d, 0)).toMatchObject({ colspan: 6, break: true });
42
+ expect(queryTile(d, 0).rowspan).toBeUndefined();
43
+ });
44
+
45
+ it("reads a text tile's rowspan", async () => {
46
+ const d = await openDocument(
47
+ `## artifact { layout=grid title="T" tiles=[intro { kind=text rowspan=2 }] }\n##|(markdown) intro\nHi\n|##\n`,
48
+ );
49
+ const tile = d.tiles[0];
50
+ expect(isTextTile(tile) && tile.rowspan).toBe(2);
51
+ });
52
+ });
53
+
54
+ describe("entry layout: writing", () => {
55
+ it("moves a changed width to the entry, taking the view's layout tags off", async () => {
56
+ const out = await spliced(TAGGED, (d) => {
57
+ d.tiles[0].colspan = 8;
58
+ });
59
+ expect(out).toContain(
60
+ 'tiles=["a -> kpis" { colspan=8 break }, "a -> trend"]',
61
+ );
62
+ expect(out).toContain(' # label="KPIs"\n view: kpis is vk');
63
+ expect(out).not.toContain("# colspan");
64
+ expect(out).not.toContain("# break");
65
+ const back = await openDocument(out);
66
+ expect(queryTile(back, 0)).toMatchObject({ colspan: 8, break: true });
67
+ });
68
+
69
+ it("leaves a view's layout tags alone when only its title changes", async () => {
70
+ const out = await spliced(TAGGED, (d) => {
71
+ queryTile(d, 0).label = "Totals";
72
+ });
73
+ expect(out).toContain('tiles=["a -> kpis", "a -> trend"]');
74
+ expect(out).toContain(
75
+ ' # colspan=6\n # break\n # label="Totals"\n view: kpis is vk',
76
+ );
77
+ });
78
+
79
+ it("writes a rowspan on a query tile and a text tile", async () => {
80
+ const source = TAGGED.replace(
81
+ '"a -> kpis", "a -> trend"',
82
+ 'intro { kind=text }, "a -> kpis", "a -> trend"',
83
+ ).replace("import", "##|(markdown) intro\nHi\n|##\nimport");
84
+ const out = await spliced(source, (d) => {
85
+ d.tiles[0].rowspan = 2;
86
+ d.tiles[2].rowspan = 5;
87
+ });
88
+ expect(out).toContain(
89
+ 'tiles=[intro { kind=text rowspan=2 }, "a -> kpis", "a -> trend" { rowspan=5 }]',
90
+ );
91
+ });
92
+
93
+ it("drops the braces when an entry's last layout property goes", async () => {
94
+ const source = TAGGED.replace(
95
+ '"a -> trend"',
96
+ '"a -> trend" { rowspan=4 }',
97
+ );
98
+ const out = await spliced(source, (d) => {
99
+ delete d.tiles[1].rowspan;
100
+ });
101
+ expect(out).toContain('tiles=["a -> kpis", "a -> trend"]');
102
+ });
103
+
104
+ it("keeps an entry's spelling and other properties when its layout changes", async () => {
105
+ const source = TAGGED.replace(
106
+ '"a -> trend"',
107
+ '"a->trend" { rowspan=4 kind=query }',
108
+ );
109
+ const out = await spliced(source, (d) => {
110
+ d.tiles[1].rowspan = 6;
111
+ });
112
+ expect(out).toContain('"a->trend" { kind=query rowspan=6 }');
113
+ });
114
+
115
+ it("rewrites an entry that carries layout as written when only the order changes", async () => {
116
+ const source = TAGGED.replace(
117
+ '"a -> trend"',
118
+ '"a -> trend" { rowspan=4 }',
119
+ );
120
+ const out = await spliced(source, (d) => {
121
+ d.tiles = [d.tiles[1], d.tiles[0]];
122
+ // The pattern of breaks is the grid's, not the tiles': see layout.ts.
123
+ delete d.tiles[1].break;
124
+ d.tiles[0].break = undefined;
125
+ });
126
+ expect(out).toContain('tiles=["a -> trend" { rowspan=4 }, "a -> kpis"');
127
+ });
128
+
129
+ it("writes where a placed tile sits, ahead of its size", async () => {
130
+ const out = await spliced(TAGGED, (d) => {
131
+ d.tiles[1].row = 3;
132
+ d.tiles[1].col = 7;
133
+ d.tiles[1].colspan = 6;
134
+ });
135
+ expect(out).toContain('"a -> trend" { row=3 col=7 colspan=6 }');
136
+ const back = await openDocument(out);
137
+ expect(queryTile(back, 1)).toMatchObject({ row: 3, col: 7, colspan: 6 });
138
+ });
139
+
140
+ it("takes a tile's place off without touching its size", async () => {
141
+ const source = TAGGED.replace(
142
+ '"a -> trend"',
143
+ '"a -> trend" { row=3 col=7 colspan=6 }',
144
+ );
145
+ const out = await spliced(source, (d) => {
146
+ delete d.tiles[1].row;
147
+ delete d.tiles[1].col;
148
+ });
149
+ expect(out).toContain('"a -> trend" { colspan=6 }');
150
+ });
151
+ });
@@ -15,7 +15,7 @@ const folderOf = (path: string) => path.slice(0, path.lastIndexOf("/") + 1);
15
15
  * alone would miss the second, and importing a source the file already
16
16
  * reaches declares it twice.
17
17
  */
18
- const resolvedOf = (from: string, documentPath: string) =>
18
+ export const resolvedOf = (from: string, documentPath: string) =>
19
19
  new URL(
20
20
  from,
21
21
  `https://malloy.invalid/${folderOf(documentPath)}`,
@@ -151,16 +151,20 @@ const ROWS: Row[] = [
151
151
  outcome: "succeeds",
152
152
  },
153
153
  {
154
- // The `#` tags this rewrites sit above a `--` line, so a writer that
155
- // cannot see them adds a second `# colspan` rather than patching the one
156
- // that is there -- and the reader, which takes tags from the parser,
157
- // reads the lower one back and the round-trip gate is satisfied.
158
- what: "retags a tile whose tag block holds a `--` comment",
154
+ // A width moves to the tile's entry and takes the view's `# colspan`
155
+ // with it. That tag sits above a `--` line, so a writer that cannot see
156
+ // it leaves it behind -- and the reader, which lets the entry win, reads
157
+ // the new width back and the round-trip gate is satisfied.
158
+ what: "moves a width off a tag block that holds a `--` comment",
159
159
  edit: (d) => {
160
160
  tile(d, "kpis").colspan = 3;
161
161
  },
162
162
  outcome: "succeeds",
163
- moved: { tagText: ["~view:a.kpis"] },
163
+ moved: {
164
+ tagText: ["~view:a.kpis"],
165
+ tags: ["~view:a.kpis"],
166
+ declarations: ["~source:a"],
167
+ },
164
168
  },
165
169
  {
166
170
  // The same shape as the row above, in the spelling that also hid a tag
@@ -169,12 +173,16 @@ const ROWS: Row[] = [
169
173
  // which here is prose: walking further up to find the real tag must not
170
174
  // turn a line inside a comment into an annotation -- to rewrite in place
171
175
  // on the writer's side, or to report as a tag on the reader's.
172
- what: "retags a tile whose tag block holds a block comment",
176
+ what: "moves a width off a tag block that holds a block comment",
173
177
  edit: (d) => {
174
178
  tile(d, "counts").colspan = 6;
175
179
  },
176
180
  outcome: "succeeds",
177
- moved: { tagText: ["~view:a.counts"] },
181
+ moved: {
182
+ tagText: ["~view:a.counts"],
183
+ tags: ["~view:a.counts"],
184
+ declarations: ["~source:a"],
185
+ },
178
186
  },
179
187
  {
180
188
  // A line that is a comment AND a declaration at once. Read as wholly
@@ -183,12 +191,12 @@ const ROWS: Row[] = [
183
191
  // to match a tile that never had a label. Nothing downstream can tell:
184
192
  // the reader takes tags from the parser and never attributed it to the
185
193
  // tile in the first place.
186
- what: "retags a tile below a declaration sharing its line with a comment",
194
+ what: "moves a width off a tile below a declaration sharing its line with a comment",
187
195
  edit: (d) => {
188
196
  tile(d, "trend").colspan = 4;
189
197
  },
190
198
  outcome: "succeeds",
191
- moved: { tagText: ["~view:a.trend"] },
199
+ moved: { tagText: ["~view:a.trend"], tags: ["~view:a.trend"] },
192
200
  },
193
201
  {
194
202
  what: "relabels a control",
@@ -96,6 +96,9 @@ export const tileEntry = (tile: DashboardTile, index: number): GridEntry => ({
96
96
  index,
97
97
  colspan: tile.colspan,
98
98
  break: tile.break,
99
+ rowspan: tile.rowspan,
100
+ row: tile.row,
101
+ col: tile.col,
99
102
  });
100
103
 
101
104
  /** A gap's id: its drop target's, and the grid's key for it. */
@@ -0,0 +1,193 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { describe, expect, it } from "bun:test";
5
+ import type { DashboardTile, QueryTile } from "./document";
6
+ import {
7
+ cellAt,
8
+ dropAt,
9
+ placeAtEnd,
10
+ inReadingOrder,
11
+ placeWhereDrawn,
12
+ settle,
13
+ trackWidth,
14
+ } from "./placement";
15
+
16
+ const tile = (name: string, layout: Partial<QueryTile> = {}): QueryTile => ({
17
+ name,
18
+ source: "a",
19
+ declaration: { kind: "inline" },
20
+ ...layout,
21
+ });
22
+ const where = (tiles: DashboardTile[]) =>
23
+ tiles.map(({ name, row, col, colspan, rowspan }) => ({
24
+ name,
25
+ row,
26
+ col,
27
+ colspan,
28
+ rowspan,
29
+ }));
30
+
31
+ describe("settle", () => {
32
+ it("pushes a tile the moved one now covers straight down", () => {
33
+ const tiles = [
34
+ tile("a", { row: 1, col: 1, colspan: 6, rowspan: 4 }),
35
+ tile("b", { row: 1, col: 4, colspan: 6, rowspan: 3 }),
36
+ ];
37
+ expect(where(settle(tiles, 12, 1))).toEqual([
38
+ { name: "a", row: 4, col: 1, colspan: 6, rowspan: 4 },
39
+ { name: "b", row: 1, col: 4, colspan: 6, rowspan: 3 },
40
+ ]);
41
+ });
42
+
43
+ it("pushes in a chain, top to bottom", () => {
44
+ const tiles = [
45
+ tile("a", { row: 1, col: 1, colspan: 12, rowspan: 2 }),
46
+ tile("b", { row: 3, col: 1, colspan: 12, rowspan: 2 }),
47
+ tile("big", { row: 1, col: 1, colspan: 12, rowspan: 3 }),
48
+ ];
49
+ expect(where(settle(tiles, 12, 2)).map((t) => [t.name, t.row])).toEqual([
50
+ ["a", 4],
51
+ ["b", 6],
52
+ ["big", 1],
53
+ ]);
54
+ });
55
+
56
+ it("leaves empty cells empty: nothing is pulled up", () => {
57
+ const tiles = [
58
+ tile("a", { row: 1, col: 1, colspan: 6, rowspan: 2 }),
59
+ tile("b", { row: 9, col: 1, colspan: 6, rowspan: 2 }),
60
+ ];
61
+ expect(settle(tiles, 12, 0)[1].row).toBe(9);
62
+ });
63
+
64
+ it("pulls a tile hanging off the right edge back onto the grid", () => {
65
+ expect(
66
+ settle([tile("a", { row: 1, col: 10, colspan: 6 })], 12, 0)[0].col,
67
+ ).toBe(7);
68
+ });
69
+
70
+ it("counts a tile with no rowspan at its kind's default rows", () => {
71
+ const tiles = [
72
+ tile("a", { row: 1, col: 1, colspan: 6 }),
73
+ tile("b", { row: 5, col: 1, colspan: 6, rowspan: 2 }),
74
+ ];
75
+ // a is 5 rows by default, so b at row 5 is covered and goes to row 6.
76
+ expect(settle(tiles, 12, 0)[1].row).toBe(6);
77
+ });
78
+
79
+ it("leaves a tile that is not placed for the grid to place", () => {
80
+ const tiles = [tile("a", { row: 1, col: 1 }), tile("free")];
81
+ expect(settle(tiles, 12, 0)[1]).toEqual(tile("free"));
82
+ });
83
+ });
84
+
85
+ describe("cellAt", () => {
86
+ const track = trackWidth(1200 - 0, 12);
87
+ it("snaps a corner to the nearest row and column", () => {
88
+ expect(cellAt(0, 0, 6, 12, track)).toEqual({ row: 1, col: 1 });
89
+ expect(cellAt(3 * (track + 16) + 10, 2 * 96 - 20, 6, 12, track)).toEqual({
90
+ row: 3,
91
+ col: 4,
92
+ });
93
+ });
94
+
95
+ it("keeps a tile on the grid at its edges", () => {
96
+ expect(cellAt(-500, -500, 6, 12, track)).toEqual({ row: 1, col: 1 });
97
+ expect(cellAt(5000, 0, 6, 12, track)).toEqual({ row: 1, col: 7 });
98
+ });
99
+ });
100
+
101
+ describe("placeWhereDrawn", () => {
102
+ it("reads each tile's row, column, width and height off where it is drawn", () => {
103
+ const track = trackWidth(1200, 12);
104
+ const step = track + 16;
105
+ const tiles = [
106
+ tile("kpis", { colspan: 12, break: true }),
107
+ tile("trend"),
108
+ tile("map", { colspan: 6 }),
109
+ ];
110
+ const boxes = [
111
+ { top: 0, left: 0, width: 1200, height: 272 },
112
+ { top: 288, left: 0, width: 6 * step - 16, height: 492 },
113
+ { top: 288, left: 6 * step, width: 6 * step - 16, height: 492 },
114
+ ];
115
+ expect(where(placeWhereDrawn(tiles, boxes, 12, track))).toEqual([
116
+ { name: "kpis", row: 1, col: 1, colspan: 12, rowspan: 3 },
117
+ { name: "trend", row: 4, col: 1, colspan: 6, rowspan: 5 },
118
+ { name: "map", row: 4, col: 7, colspan: 6, rowspan: 5 },
119
+ ]);
120
+ // A row break means nothing once tiles are placed.
121
+ expect(placeWhereDrawn(tiles, boxes, 12, track)[0].break).toBeUndefined();
122
+ });
123
+ });
124
+
125
+ describe("inReadingOrder", () => {
126
+ it("orders placed tiles top to bottom, then left to right, unplaced last", () => {
127
+ const tiles = [
128
+ tile("free"),
129
+ tile("c", { row: 3, col: 1 }),
130
+ tile("b", { row: 1, col: 7 }),
131
+ tile("a", { row: 1, col: 1 }),
132
+ ];
133
+ expect(inReadingOrder(tiles).map((t) => t.name)).toEqual([
134
+ "a",
135
+ "b",
136
+ "c",
137
+ "free",
138
+ ]);
139
+ });
140
+ });
141
+
142
+ describe("dropAt", () => {
143
+ const tiles = () => [
144
+ tile("map", { row: 1, col: 7, colspan: 6, rowspan: 6 }),
145
+ tile("bar", { row: 1, col: 1, colspan: 6, rowspan: 3 }),
146
+ ];
147
+
148
+ it("lands under a tile whose lower part it was dropped on", () => {
149
+ // Dropped at row 4, col 7: on the map's lower half, which starts at row 1.
150
+ expect(where(dropAt(tiles(), 1, { row: 4, col: 7 }, 12))).toEqual([
151
+ { name: "map", row: 1, col: 7, colspan: 6, rowspan: 6 },
152
+ { name: "bar", row: 7, col: 7, colspan: 6, rowspan: 3 },
153
+ ]);
154
+ });
155
+
156
+ it("pushes a tile down when dropped at or above its top", () => {
157
+ expect(
158
+ where(dropAt(tiles(), 1, { row: 1, col: 7 }, 12)).map((t) => [
159
+ t.name,
160
+ t.row,
161
+ ]),
162
+ ).toEqual([
163
+ ["map", 4],
164
+ ["bar", 1],
165
+ ]);
166
+ });
167
+ });
168
+
169
+ describe("placeAtEnd", () => {
170
+ it("puts a new tile under everything on a placed report, at the left", () => {
171
+ const tiles = [
172
+ tile("a", { row: 1, col: 1, colspan: 6, rowspan: 3 }),
173
+ tile("b", { row: 2, col: 7, colspan: 6, rowspan: 6 }),
174
+ tile("new", { colspan: 6 }),
175
+ ];
176
+ expect(placeAtEnd(tiles, 2, 12)[2]).toMatchObject({ row: 8, col: 1 });
177
+ });
178
+
179
+ it("leaves a report that flows alone", () => {
180
+ const tiles = [tile("a", { colspan: 6 }), tile("new")];
181
+ expect(placeAtEnd(tiles, 1, 12)[1]).toEqual(tile("new"));
182
+ });
183
+ });
184
+
185
+ describe("dropAt, far below", () => {
186
+ it("lands no lower than right under the lowest other tile", () => {
187
+ const tiles = [
188
+ tile("a", { row: 1, col: 1, colspan: 6, rowspan: 3 }),
189
+ tile("b", { row: 1, col: 7, colspan: 6, rowspan: 3 }),
190
+ ];
191
+ expect(dropAt(tiles, 1, { row: 40, col: 7 }, 12)[1].row).toBe(4);
192
+ });
193
+ });
@@ -0,0 +1,250 @@
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
+ PLACED_DEFAULT_ROWSPAN,
9
+ TILE_ROW_PX,
10
+ } from "../Dashboard/DashboardGrid";
11
+ import { isTextTile, type DashboardTile } from "./document";
12
+
13
+ /**
14
+ * Free placement on a grid report: each tile at its own `row` and `col`, so
15
+ * empty space anywhere on the grid is somewhere a tile can go.
16
+ *
17
+ * Pure: what a move or a resize does to the tiles, never how the gesture
18
+ * feels. A move or resize keeps the tile it changed where it was put and
19
+ * pushes whatever it now covers straight down, the way the grid dashboards
20
+ * people already know behave. Nothing is pulled up to close a gap, since an
21
+ * empty cell is a choice the author made.
22
+ */
23
+
24
+ /** A tile's rectangle on the grid, in rows and columns counted from 1. */
25
+ export interface Cell {
26
+ row: number;
27
+ col: number;
28
+ colspan: number;
29
+ rowspan: number;
30
+ }
31
+
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;
35
+
36
+ /** Rows a tile takes on a placed report: its own, or its kind's default. */
37
+ export const rowsOf = (tile: DashboardTile) =>
38
+ tile.rowspan ?? PLACED_DEFAULT_ROWSPAN[isTextTile(tile) ? "text" : "query"];
39
+
40
+ /** A placed tile's rectangle, held to the grid; undefined for one not placed yet. */
41
+ export function cellOf(tile: DashboardTile, columns: number): Cell | undefined {
42
+ if (tile.row === undefined || tile.col === undefined) return undefined;
43
+ const colspan = Math.min(tile.colspan ?? 1, columns);
44
+ return {
45
+ row: tile.row,
46
+ col: Math.min(tile.col, columns - colspan + 1),
47
+ colspan,
48
+ rowspan: rowsOf(tile),
49
+ };
50
+ }
51
+
52
+ const overlaps = (a: Cell, b: Cell) =>
53
+ a.col < b.col + b.colspan &&
54
+ b.col < a.col + a.colspan &&
55
+ a.row < b.row + b.rowspan &&
56
+ b.row < a.row + a.rowspan;
57
+
58
+ /** Top to bottom, then left to right: the order a placed report is read in, and written. */
59
+ export function inReadingOrder(
60
+ tiles: readonly DashboardTile[],
61
+ ): DashboardTile[] {
62
+ return tiles
63
+ .map((tile, index) => ({ tile, index }))
64
+ .sort(
65
+ (a, b) =>
66
+ (a.tile.row ?? Infinity) - (b.tile.row ?? Infinity) ||
67
+ (a.tile.col ?? Infinity) - (b.tile.col ?? Infinity) ||
68
+ a.index - b.index,
69
+ )
70
+ .map(({ tile }) => tile);
71
+ }
72
+
73
+ /**
74
+ * The tiles with nothing covering anything: `fixed` (the tile just moved or
75
+ * resized) stays exactly where it is, and every placed tile it, or a tile it
76
+ * pushed, now covers moves straight down to just below what it hit. Tiles
77
+ * settle top to bottom, so a pushed tile pushes the ones under it in turn.
78
+ * Without `fixed`, the first tile in reading order holds its place.
79
+ */
80
+ export function settle(
81
+ tiles: readonly DashboardTile[],
82
+ columns: number,
83
+ fixed?: number,
84
+ ): DashboardTile[] {
85
+ const out = tiles.map((tile) => ({ ...tile }));
86
+ const held: Cell[] = [];
87
+ const hold = (index: number) => {
88
+ const cell = cellOf(out[index], columns);
89
+ if (!cell) return;
90
+ // Pulled onto the grid if it was hanging off its right edge.
91
+ out[index].col = cell.col;
92
+ held.push(cell);
93
+ };
94
+ if (fixed !== undefined) hold(fixed);
95
+ const rest = out
96
+ .map((tile, index) => ({ tile, index }))
97
+ .filter(({ index, tile }) => index !== fixed && tile.row !== undefined)
98
+ .sort(
99
+ (a, b) =>
100
+ (a.tile.row ?? 0) - (b.tile.row ?? 0) ||
101
+ (a.tile.col ?? 0) - (b.tile.col ?? 0) ||
102
+ a.index - b.index,
103
+ );
104
+ for (const { index } of rest) {
105
+ const cell = cellOf(out[index], columns);
106
+ if (!cell) continue;
107
+ for (;;) {
108
+ const hit = held.filter((other) => overlaps(cell, other));
109
+ if (hit.length === 0) break;
110
+ cell.row = Math.max(...hit.map((other) => other.row + other.rowspan));
111
+ }
112
+ out[index].row = cell.row;
113
+ hold(index);
114
+ }
115
+ return out;
116
+ }
117
+
118
+ /**
119
+ * The tiles with the one at `index` dropped at `at`. A drop onto the lower
120
+ * part of a tile that starts higher up lands just under that tile, rather
121
+ * than shoving it beneath the dropped one; a drop at or above a tile's top
122
+ * pushes that tile down. Then everything settles around where it landed.
123
+ */
124
+ export function dropAt(
125
+ tiles: readonly DashboardTile[],
126
+ index: number,
127
+ at: { row: number; col: number },
128
+ columns: number,
129
+ ): DashboardTile[] {
130
+ const others = tiles.flatMap((tile, i) => {
131
+ const cell = i === index ? undefined : cellOf(tile, columns);
132
+ return cell ? [cell] : [];
133
+ });
134
+ // Never lower than right under the lowest other tile: space below
135
+ // everything holds nothing, and a drag held at the window's bottom edge
136
+ // would otherwise grow the page, and scroll it, without end.
137
+ const lowest = Math.max(1, ...others.map((cell) => cell.row + cell.rowspan));
138
+ const moved = { ...tiles[index], ...at, row: Math.min(at.row, lowest) };
139
+ const cell = cellOf(moved, columns);
140
+ if (cell) {
141
+ for (;;) {
142
+ const above = others.filter(
143
+ (other) => other.row < cell.row && overlaps(cell, other),
144
+ );
145
+ if (above.length === 0) break;
146
+ cell.row = Math.max(
147
+ ...above.map((other) => other.row + other.rowspan),
148
+ );
149
+ }
150
+ moved.row = cell.row;
151
+ }
152
+ return settle(
153
+ tiles.map((tile, i) => (i === index ? moved : tile)),
154
+ columns,
155
+ index,
156
+ );
157
+ }
158
+
159
+ /** A tile's box against the grid's top left corner, in px. */
160
+ export interface Box {
161
+ top: number;
162
+ left: number;
163
+ width: number;
164
+ height: number;
165
+ }
166
+
167
+ /** One column track's width in a grid `gridWidth` px wide. */
168
+ export const trackWidth = (gridWidth: number, columns: number) =>
169
+ (gridWidth - GRID_GAP_PX * (columns - 1)) / columns;
170
+
171
+ const ROW_STEP = TILE_ROW_PX + GRID_GAP_PX;
172
+
173
+ /**
174
+ * The cell a tile of `colspan` columns lands in when its top left corner is
175
+ * at (`left`, `top`) px from the grid's: the nearest row and column, held on
176
+ * the grid.
177
+ */
178
+ export function cellAt(
179
+ left: number,
180
+ top: number,
181
+ colspan: number,
182
+ columns: number,
183
+ track: number,
184
+ ): { row: number; col: number } {
185
+ const span = Math.min(colspan, columns);
186
+ const col = Math.round(left / (track + GRID_GAP_PX)) + 1;
187
+ return {
188
+ row: Math.max(1, Math.round(top / ROW_STEP) + 1),
189
+ col: Math.min(Math.max(col, 1), columns - span + 1),
190
+ };
191
+ }
192
+
193
+ /**
194
+ * The tiles placed where they are drawn now, the first time a report is
195
+ * placed: each tile's row, column, width and height read off its box, so
196
+ * turning a flowing report into a placed one moves nothing. A tile with no
197
+ * box (not drawn) is left for the grid to place.
198
+ */
199
+ export function placeWhereDrawn(
200
+ tiles: readonly DashboardTile[],
201
+ boxes: ReadonlyArray<Box | undefined>,
202
+ columns: number,
203
+ track: number,
204
+ ): DashboardTile[] {
205
+ const placed = tiles.map((tile, index) => {
206
+ if (tile.row !== undefined && tile.col !== undefined) return tile;
207
+ const box = boxes[index];
208
+ if (!box) return tile;
209
+ const { break: _break, ...rest } = tile;
210
+ const colspan =
211
+ tile.colspan ??
212
+ Math.max(
213
+ 1,
214
+ Math.round((box.width + GRID_GAP_PX) / (track + GRID_GAP_PX)),
215
+ );
216
+ return {
217
+ ...rest,
218
+ ...cellAt(box.left, box.top, colspan, columns, track),
219
+ colspan,
220
+ rowspan:
221
+ tile.rowspan ??
222
+ Math.max(
223
+ minRowsOf(tile),
224
+ Math.round((box.height + GRID_GAP_PX) / ROW_STEP),
225
+ ),
226
+ };
227
+ });
228
+ return settle(placed, columns);
229
+ }
230
+
231
+ /**
232
+ * The tiles with the one at `index` placed under everything else, at the
233
+ * left, when the report is placed: where a new tile goes, so it never drops
234
+ * into a gap an author left on purpose. A report that flows is left as it is.
235
+ */
236
+ export function placeAtEnd(
237
+ tiles: readonly DashboardTile[],
238
+ index: number,
239
+ columns: number,
240
+ ): DashboardTile[] {
241
+ const others = tiles.flatMap((tile, i) => {
242
+ const cell = i === index ? undefined : cellOf(tile, columns);
243
+ return cell ? [cell] : [];
244
+ });
245
+ if (others.length === 0) return [...tiles];
246
+ const bottom = Math.max(...others.map((cell) => cell.row + cell.rowspan));
247
+ return tiles.map((tile, i) =>
248
+ i === index ? { ...tile, row: bottom, col: 1 } : tile,
249
+ );
250
+ }