@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,397 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { useEffect, useRef, useState, type RefObject } from "react";
5
+ import {
6
+ GRID_GAP_PX,
7
+ isPlaced,
8
+ MAX_TILE_ROWS,
9
+ rowsForHeight,
10
+ TILE_ROW_PX,
11
+ tileHeightPx,
12
+ } from "../Dashboard/DashboardGrid";
13
+ import { tileKey, type DashboardTile } from "./document";
14
+ import { minRowsOf, placeWhereDrawn, rowsOf, trackWidth } from "./placement";
15
+ import { drawnBoxes } from "./useTileReorder";
16
+ import { widthLabel } from "./widthLabel";
17
+
18
+ /** The edges a resize moves: one for an edge, two for a corner. */
19
+ export interface Edges {
20
+ n?: boolean;
21
+ s?: boolean;
22
+ e?: boolean;
23
+ w?: boolean;
24
+ }
25
+
26
+ /** A tile's place and size, as a resize changes them. `rowspan` absent is auto height. */
27
+ export interface TileShape {
28
+ row?: number;
29
+ col?: number;
30
+ colspan: number;
31
+ rowspan: number | undefined;
32
+ }
33
+
34
+ /** A resize in flight. */
35
+ export interface EdgeDrag {
36
+ index: number;
37
+ edges: Edges;
38
+ /** The tiles it started from: placed where they were drawn, when a left or top edge needs places. */
39
+ base: DashboardTile[];
40
+ /** Whether this gesture placed them, which the write then has to keep. */
41
+ placedHere: boolean;
42
+ /** Whether the report is placed for this gesture: the edges then move on the grid's own rows. */
43
+ placed: boolean;
44
+ from: TileShape;
45
+ /** The shape the drag began at, as it is drawn: a release here writes nothing. */
46
+ begun: TileShape;
47
+ /** What the tile would be if the pointer let go now. */
48
+ shape: TileShape;
49
+ /** The tile's top against the grid's, where the row guides of a flowing report are drawn from. */
50
+ guideTop: number;
51
+ /** The fewest rows offered, read off what the tile draws when the drag began. */
52
+ min: number;
53
+ /** The tile's top and left on the page when it began, for a flowing report's edges. */
54
+ top: number;
55
+ left: number;
56
+ }
57
+
58
+ const ROW_STEP = TILE_ROW_PX + GRID_GAP_PX;
59
+
60
+ /** The cursor an edge, or a corner, takes. */
61
+ export const edgeCursor = (edges: Edges): string =>
62
+ (edges.n && edges.w) || (edges.s && edges.e)
63
+ ? "nwse-resize"
64
+ : (edges.n && edges.e) || (edges.s && edges.w)
65
+ ? "nesw-resize"
66
+ : edges.n || edges.s
67
+ ? "ns-resize"
68
+ : "ew-resize";
69
+
70
+ /** While an edge is dragged the page selects no text and keeps the edge's cursor. */
71
+ const dragChrome = (cursor: string | undefined) => {
72
+ const body = window.document.body;
73
+ body.style.userSelect = cursor ? "none" : "";
74
+ body.style.cursor = cursor ?? "";
75
+ };
76
+
77
+ const shapeOf = (tile: DashboardTile): TileShape => ({
78
+ ...(tile.row !== undefined ? { row: tile.row } : {}),
79
+ ...(tile.col !== undefined ? { col: tile.col } : {}),
80
+ colspan: tile.colspan ?? 1,
81
+ rowspan: tile.rowspan,
82
+ });
83
+
84
+ const sameShape = (a: TileShape, b: TileShape) =>
85
+ a.row === b.row &&
86
+ a.col === b.col &&
87
+ a.colspan === b.colspan &&
88
+ a.rowspan === b.rowspan;
89
+
90
+ /**
91
+ * `tile` with what a resize of `edges` changed on it, and nothing else: a
92
+ * right or left edge its width (and, placed, its column), a bottom or top
93
+ * edge its height (and, placed, its row). So a height drag never writes the
94
+ * width a tile happens to be drawn at.
95
+ */
96
+ export function withShape(
97
+ tile: DashboardTile,
98
+ shape: TileShape,
99
+ edges: Edges,
100
+ placed: boolean,
101
+ ): DashboardTile {
102
+ const next = { ...tile };
103
+ if (edges.e || edges.w) {
104
+ next.colspan = shape.colspan;
105
+ if (placed && shape.col !== undefined) next.col = shape.col;
106
+ }
107
+ if (edges.n || edges.s) {
108
+ if (shape.rowspan === undefined) delete next.rowspan;
109
+ else next.rowspan = shape.rowspan;
110
+ if (placed && shape.row !== undefined) next.row = shape.row;
111
+ }
112
+ return next;
113
+ }
114
+
115
+ /** What a resize asks the caller to write. */
116
+ export interface EdgeResult {
117
+ index: number;
118
+ edges: Edges;
119
+ shape: TileShape;
120
+ placed: boolean;
121
+ /** Every tile placed where it was drawn, when this gesture is what placed the report. */
122
+ placedAll?: DashboardTile[];
123
+ }
124
+
125
+ /**
126
+ * Dragging any edge or corner of a tile. The right edge sets its width and
127
+ * the bottom edge its height, as they always have; the left and top edges
128
+ * move that edge and keep the opposite one where it is, which needs the
129
+ * tile's row and column, so on a report that flows they place every tile
130
+ * where it is drawn first, as a move does. A corner moves its two edges at
131
+ * once. Previewed from `drag.shape` and written ONCE, on release, so one
132
+ * gesture is one undo step.
133
+ */
134
+ export function useEdgeResize({
135
+ tiles,
136
+ columns,
137
+ gridBox,
138
+ onStart,
139
+ commit,
140
+ }: {
141
+ /** The tiles as drawn: one with no `colspan` carries the span it is drawn at. */
142
+ tiles: readonly DashboardTile[];
143
+ columns: number;
144
+ /** Wraps the grid, so its box is the grid's. */
145
+ gridBox: RefObject<HTMLDivElement | null>;
146
+ /** The tile a drag begins on, which the caller selects. */
147
+ onStart: (index: number) => void;
148
+ /** What to write when the drag ends. */
149
+ commit: (result: EdgeResult) => void;
150
+ }) {
151
+ const [drag, setDragState] = useState<EdgeDrag | undefined>(undefined);
152
+ // The gesture as the handlers last left it: a release can arrive before
153
+ // the render for the last move does.
154
+ const live = useRef<EdgeDrag | undefined>(undefined);
155
+ // A flowing tile that began on auto height: its height then, its row's.
156
+ const startedAuto = useRef<number | undefined>(undefined);
157
+ const setDrag = (next: EdgeDrag | undefined) => {
158
+ live.current = next;
159
+ setDragState(next);
160
+ };
161
+ useEffect(() => () => dragChrome(undefined), []);
162
+ // Escape cancels a resize in flight wherever focus is: the tile goes back
163
+ // to where it began and the release writes nothing.
164
+ const active = drag !== undefined;
165
+ useEffect(() => {
166
+ if (!active) return;
167
+ const onKey = (event: KeyboardEvent) => {
168
+ if (event.key !== "Escape" || !live.current) return;
169
+ event.preventDefault();
170
+ event.stopPropagation();
171
+ live.current = undefined;
172
+ setDragState(undefined);
173
+ startedAuto.current = undefined;
174
+ dragChrome(undefined);
175
+ };
176
+ window.addEventListener("keydown", onKey, true);
177
+ return () => window.removeEventListener("keydown", onKey, true);
178
+ }, [active]);
179
+
180
+ /**
181
+ * How tall a flowing tile's row is on auto height: the tallest of the auto
182
+ * tiles beside it, measured now (#1453: a row of auto tiles is rarely a whole number of rows, so auto is a snap
183
+ * point of its own, and what makes a tile reachable level with them).
184
+ */
185
+ const autoHeight = (state: EdgeDrag): number | undefined => {
186
+ const auto = new Set(
187
+ state.base.filter((tile) => tile.rowspan === undefined).map(tileKey),
188
+ );
189
+ const self = state.base[state.index];
190
+ let tallest = startedAuto.current;
191
+ for (const frame of gridBox.current?.querySelectorAll<HTMLElement>(
192
+ "[data-tile-key]",
193
+ ) ?? []) {
194
+ const key = frame.dataset.tileKey;
195
+ if (!key || !auto.has(key) || (self && key === tileKey(self)))
196
+ continue;
197
+ const box = frame.getBoundingClientRect();
198
+ if (Math.abs(box.top - state.top) > 1) continue;
199
+ tallest = Math.max(tallest ?? 0, box.height);
200
+ }
201
+ return tallest;
202
+ };
203
+
204
+ const start = (
205
+ event: React.PointerEvent<HTMLElement>,
206
+ index: number,
207
+ edges: Edges,
208
+ ) => {
209
+ const grid = gridBox.current;
210
+ const gridBoxNow = grid?.getBoundingClientRect();
211
+ const tileBox =
212
+ event.currentTarget.parentElement?.getBoundingClientRect();
213
+ if (!grid || !gridBoxNow || !tileBox || !tiles[index]) return;
214
+ event.stopPropagation();
215
+ event.preventDefault();
216
+ event.currentTarget.setPointerCapture(event.pointerId);
217
+ dragChrome(edgeCursor(edges));
218
+ onStart(index);
219
+ // A left or top edge moves a place, which a flowing report has not got
220
+ // yet, nor has a tile a placed report leaves to the grid: each tile
221
+ // without one is placed where it is drawn, so nothing jumps.
222
+ const needsPlace =
223
+ (edges.n || edges.w) &&
224
+ (!isPlaced(tiles) ||
225
+ tiles[index].row === undefined ||
226
+ tiles[index].col === undefined);
227
+ const base = needsPlace
228
+ ? (() => {
229
+ const boxes = drawnBoxes(grid);
230
+ return placeWhereDrawn(
231
+ tiles,
232
+ tiles.map((tile) => boxes.get(tileKey(tile))),
233
+ columns,
234
+ trackWidth(gridBoxNow.width, columns),
235
+ );
236
+ })()
237
+ : [...tiles];
238
+ const placed = isPlaced(base);
239
+ const tile = base[index];
240
+ const from = shapeOf(tile);
241
+ // A flowing tile on auto height starts from the rows it is drawn at,
242
+ // so the edge does not jump when it is first pulled.
243
+ if (!placed && from.rowspan === undefined) {
244
+ startedAuto.current = tileBox.height;
245
+ } else startedAuto.current = undefined;
246
+ const begun = placed ? { ...from, rowspan: rowsOf(tile) } : from;
247
+ setDrag({
248
+ index,
249
+ edges,
250
+ base,
251
+ placedHere: Boolean(needsPlace),
252
+ placed,
253
+ from,
254
+ begun,
255
+ shape: begun,
256
+ guideTop: tileBox.top - gridBoxNow.top,
257
+ min: minRowsOf(tile, event.currentTarget.parentElement),
258
+ top: tileBox.top,
259
+ left: tileBox.left,
260
+ });
261
+ };
262
+
263
+ const move = (event: React.PointerEvent<HTMLElement>) => {
264
+ const state = live.current;
265
+ const grid = gridBox.current?.getBoundingClientRect();
266
+ if (!state || !grid) return;
267
+ const tile = state.base[state.index];
268
+ if (!tile) return;
269
+ const track = trackWidth(grid.width, columns);
270
+ const colStep = track + GRID_GAP_PX;
271
+ // The pointer against the grid as it is now, so a page that scrolled
272
+ // during the drag still lands the edge under the pointer.
273
+ const x = event.clientX - grid.left;
274
+ const y = event.clientY - grid.top;
275
+ // The tile's own box as it is drawn now. A bottom or right edge is
276
+ // measured from it, not from the grid's rows: on a narrow screen a
277
+ // placed report stacks in one column, where a tile's row says nothing
278
+ // about where it is, and a page scrolled mid-drag moves it too.
279
+ const own = event.currentTarget.parentElement?.getBoundingClientRect();
280
+ const ownTop = own?.top ?? state.top;
281
+ const ownLeft = own?.left ?? state.left;
282
+ const { min } = state;
283
+ const now = state.shape;
284
+ let { row, col, colspan, rowspan } = now;
285
+
286
+ if (state.placed && col !== undefined && row !== undefined) {
287
+ const from = state.from;
288
+ const firstCol = from.col ?? col;
289
+ const lastCol = firstCol + from.colspan - 1;
290
+ const firstRow = from.row ?? row;
291
+ const fromRows = from.rowspan ?? rowsOf(tile);
292
+ const lastRow = firstRow + fromRows - 1;
293
+ if (state.edges.e) {
294
+ const right = Math.round((x + GRID_GAP_PX) / colStep);
295
+ colspan =
296
+ Math.min(Math.max(right, firstCol), columns) - firstCol + 1;
297
+ col = firstCol;
298
+ }
299
+ if (state.edges.w) {
300
+ const left = Math.round(x / colStep) + 1;
301
+ col = Math.min(Math.max(left, 1), lastCol);
302
+ colspan = lastCol - col + 1;
303
+ }
304
+ if (state.edges.s) {
305
+ // From the tile's top, so it is right in the stacked layout too.
306
+ // A top edge moved in the same gesture (a corner) has moved that
307
+ // top already, so it is measured from the row it started on.
308
+ const top = state.edges.n
309
+ ? grid.top + (firstRow - 1) * ROW_STEP
310
+ : ownTop;
311
+ rowspan = rowsForHeight(event.clientY - top, min);
312
+ row = firstRow;
313
+ }
314
+ if (state.edges.n) {
315
+ const top = Math.round(y / ROW_STEP) + 1;
316
+ // A tile already shorter than its floor (a 2-row chart written
317
+ // by hand, or one still loading) holds the rows it has rather
318
+ // than growing upward, which could put its top above row 1.
319
+ const floor = Math.min(min, fromRows);
320
+ row = Math.max(
321
+ 1,
322
+ Math.min(
323
+ Math.max(top, lastRow - MAX_TILE_ROWS + 1),
324
+ lastRow - floor + 1,
325
+ ),
326
+ );
327
+ rowspan = lastRow - row + 1;
328
+ }
329
+ } else {
330
+ // A report that flows: the right edge is a width and the bottom
331
+ // edge a height, each against the tile's own top left.
332
+ if (state.edges.e) {
333
+ const span = Math.round(
334
+ (event.clientX - ownLeft + GRID_GAP_PX) / colStep,
335
+ );
336
+ colspan = Math.min(Math.max(span, 1), columns);
337
+ }
338
+ if (state.edges.s) {
339
+ const px = event.clientY - ownTop;
340
+ const rows = rowsForHeight(px, min);
341
+ const auto = autoHeight(state);
342
+ // Ties go to rows: a neighbour stretched to the preview is no
343
+ // reason to leave it.
344
+ rowspan =
345
+ auto !== undefined &&
346
+ Math.abs(px - auto) < Math.abs(px - tileHeightPx(rows))
347
+ ? undefined
348
+ : rows;
349
+ }
350
+ }
351
+ const next: TileShape = {
352
+ ...(row !== undefined ? { row } : {}),
353
+ ...(col !== undefined ? { col } : {}),
354
+ colspan,
355
+ rowspan,
356
+ };
357
+ if (!sameShape(next, now)) setDrag({ ...state, shape: next });
358
+ };
359
+
360
+ const end = (event: React.PointerEvent<HTMLElement>) => {
361
+ if (event.currentTarget.hasPointerCapture(event.pointerId))
362
+ event.currentTarget.releasePointerCapture(event.pointerId);
363
+ dragChrome(undefined);
364
+ const state = live.current;
365
+ startedAuto.current = undefined;
366
+ if (!state) return;
367
+ setDrag(undefined);
368
+ // A cancelled gesture puts the tile back, and a press that changed
369
+ // nothing writes nothing: a click on an edge must not place a report
370
+ // that flows, or pin a tile on auto height to the rows it is drawn at.
371
+ if (event.type === "pointercancel" || sameShape(state.shape, state.begun))
372
+ return;
373
+ commit({
374
+ index: state.index,
375
+ edges: state.edges,
376
+ shape: state.shape,
377
+ placed: state.placed,
378
+ ...(state.placedHere ? { placedAll: state.base } : {}),
379
+ });
380
+ };
381
+
382
+ return { drag, start, move, end };
383
+ }
384
+
385
+ /** What a resize would make a tile, in words, for the label beside the pointer. */
386
+ export function shapeLabel(drag: EdgeDrag, columns: number): string {
387
+ const parts: string[] = [];
388
+ if (drag.edges.e || drag.edges.w)
389
+ parts.push(widthLabel(drag.shape.colspan, columns));
390
+ if (drag.edges.n || drag.edges.s)
391
+ parts.push(
392
+ drag.shape.rowspan === undefined
393
+ ? "Auto height"
394
+ : `${drag.shape.rowspan} rows tall`,
395
+ );
396
+ return parts.join(" · ");
397
+ }
@@ -1,6 +1,7 @@
1
1
  // Copyright (c) Credible Data Inc.
2
2
  // SPDX-License-Identifier: MIT
3
3
 
4
+ import { placeAtEnd } from "./placement";
4
5
  import { useState } from "react";
5
6
  import type { NewTile } from "./AddTileDialog";
6
7
  import {
@@ -56,14 +57,20 @@ export function useTileEditing({
56
57
  /** A tile from the picker: on the extension of its source, or a new one. */
57
58
  const addTile = (tile: NewTile) => {
58
59
  setAddingTile(false);
59
- editor.update((draft) =>
60
+ editor.update((draft) => {
60
61
  addTileToDocument(draft, tile, {
61
62
  modelPath,
62
63
  textHeld,
63
64
  widths: rules.widths,
64
65
  insertAt,
65
- }),
66
- );
66
+ });
67
+ if (rules.widths)
68
+ draft.tiles = placeAtEnd(
69
+ draft.tiles,
70
+ insertAt ?? draft.tiles.length - 1,
71
+ rules.columns,
72
+ );
73
+ });
67
74
  selectTile(insertAt ?? editor.document.tiles.length);
68
75
  };
69
76
 
@@ -85,6 +92,12 @@ export function useTileEditing({
85
92
  markdown: "",
86
93
  ...(rules.widths ? { colspan: rules.columns } : {}),
87
94
  });
95
+ if (rules.widths)
96
+ draft.tiles = placeAtEnd(
97
+ draft.tiles,
98
+ at ?? draft.tiles.length - 1,
99
+ rules.columns,
100
+ );
88
101
  });
89
102
  selectTile(at ?? editor.document.tiles.length);
90
103
  };
@@ -2,12 +2,42 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
 
4
4
  import { move } from "@dnd-kit/helpers";
5
- import type { DragEndEvent, DragOverEvent } from "@dnd-kit/react";
6
- import { useRef, useState } from "react";
5
+ import type {
6
+ DragEndEvent,
7
+ DragMoveEvent,
8
+ DragOverEvent,
9
+ } from "@dnd-kit/react";
10
+ import { useRef, useState, type RefObject } from "react";
7
11
  import { tileKey, type DashboardTile } from "./document";
8
12
  import { keepRowStructure, moveIntoGap } from "./layout";
13
+ import {
14
+ cellAt,
15
+ dropAt,
16
+ inReadingOrder,
17
+ placeWhereDrawn,
18
+ trackWidth,
19
+ type Box,
20
+ } from "./placement";
9
21
  import { GAP_TYPE, type GapData } from "./sortable";
10
22
 
23
+ /** The boxes of the tiles as drawn, against the grid's top left corner, by tile key. */
24
+ export function drawnBoxes(grid: HTMLElement): Map<string, Box> {
25
+ const origin = grid.getBoundingClientRect();
26
+ const boxes = new Map<string, Box>();
27
+ for (const frame of grid.querySelectorAll<HTMLElement>("[data-tile-key]")) {
28
+ const key = frame.dataset.tileKey;
29
+ if (!key) continue;
30
+ const box = frame.getBoundingClientRect();
31
+ boxes.set(key, {
32
+ top: box.top - origin.top,
33
+ left: box.left - origin.left,
34
+ width: box.width,
35
+ height: box.height,
36
+ });
37
+ }
38
+ return boxes;
39
+ }
40
+
11
41
  /**
12
42
  * Dragging a tile to reorder it. The gesture is `@dnd-kit/react`'s (see
13
43
  * `sortable.tsx`); what is decided here is what a drop means for the file,
@@ -19,8 +49,19 @@ export function useTileReorder({
19
49
  tiles,
20
50
  commit,
21
51
  onLanded,
52
+ placement,
22
53
  }: {
23
54
  tiles: DashboardTile[];
55
+ /**
56
+ * Set on a grid report, where a move is free placement: the tile lands in
57
+ * the cell under it and anything it covers is pushed down. Absent, a move
58
+ * reorders the tiles, as a linear report's does.
59
+ */
60
+ placement?: {
61
+ columns: number;
62
+ /** Wraps the grid, so its box is the grid's. */
63
+ gridBox: RefObject<HTMLDivElement | null>;
64
+ };
24
65
  /** The order to write when the drag ends somewhere new. */
25
66
  commit: (next: DashboardTile[]) => void;
26
67
  /** Where the dragged tile came to rest, which the caller selects. */
@@ -57,12 +98,93 @@ export function useTileReorder({
57
98
  * rebuilt from the document on each report and written once, on release:
58
99
  * ONE history entry for the whole drag, however far it wandered.
59
100
  */
60
- const onDragStart = () => {
101
+ // A placement in flight: the tiles placed where they stood when it began,
102
+ // and the moved tile's box then.
103
+ const placing = useRef<
104
+ | {
105
+ base: DashboardTile[];
106
+ index: number;
107
+ from: Box;
108
+ grab: { x: number; y: number };
109
+ }
110
+ | undefined
111
+ >(undefined);
112
+
113
+ const onDragStart = (event?: {
114
+ operation: {
115
+ source: { id: unknown } | null;
116
+ position?: { current: { x: number; y: number } };
117
+ };
118
+ }) => {
61
119
  setDragging(true);
62
120
  previewRef.current = tiles;
121
+ const grid = placement?.gridBox.current;
122
+ const id = event?.operation.source?.id;
123
+ if (!placement || !grid) return;
124
+ const index = tiles.findIndex((tile) => tileKey(tile) === id);
125
+ const boxes = drawnBoxes(grid);
126
+ const from = boxes.get(String(id));
127
+ if (index < 0 || !from) return;
128
+ // The first move on a report that flows places every tile where it is
129
+ // drawn, so nothing jumps; a placed report keeps what it says.
130
+ const base = placeWhereDrawn(
131
+ tiles,
132
+ tiles.map((tile) => boxes.get(tileKey(tile))),
133
+ placement.columns,
134
+ trackWidth(grid.getBoundingClientRect().width, placement.columns),
135
+ );
136
+ // Where in the tile the pointer took hold, so the tile's corner can be
137
+ // found from the pointer wherever the page has scrolled to since.
138
+ const origin = grid.getBoundingClientRect();
139
+ const pointer = event?.operation.position?.current;
140
+ const grab = pointer
141
+ ? {
142
+ x: pointer.x - (origin.left + from.left),
143
+ y: pointer.y - (origin.top + from.top),
144
+ }
145
+ : { x: 0, y: 0 };
146
+ placing.current = { base, index, from, grab };
147
+ previewRef.current = base;
148
+ setPreview(base);
149
+ };
150
+
151
+ const onDragMove = (event: DragMoveEvent) => {
152
+ const live = placing.current;
153
+ const grid = placement?.gridBox.current;
154
+ if (!live || !placement || !grid) return;
155
+ // The tile's corner against the grid as it is now: from the pointer and
156
+ // the grid's live box, so a page that auto-scrolled during the drag
157
+ // lands the tile under the pointer, not where the pointer was on screen.
158
+ const origin = grid.getBoundingClientRect();
159
+ // A keyboard step reports where it is going (`to`, or `by` from here)
160
+ // before the operation's position catches up; read the position alone
161
+ // and the preview trailed the tile by one key press.
162
+ const now = event.operation.position.current;
163
+ const pointer =
164
+ event.to ??
165
+ (event.by ? { x: now.x + event.by.x, y: now.y + event.by.y } : now);
166
+ const moved = live.base[live.index];
167
+ const cell = cellAt(
168
+ pointer.x - live.grab.x - origin.left,
169
+ pointer.y - live.grab.y - origin.top,
170
+ moved.colspan ?? 1,
171
+ placement.columns,
172
+ trackWidth(grid.getBoundingClientRect().width, placement.columns),
173
+ );
174
+ const current = previewRef.current?.[live.index];
175
+ if (current?.row === cell.row && current.col === cell.col) return;
176
+ const next = dropAt(live.base, live.index, cell, placement.columns);
177
+ previewRef.current = next;
178
+ setPreview(next);
63
179
  };
64
180
 
65
181
  const onDragOver = (event: DragOverEvent) => {
182
+ // Placing, where the tile is over says nothing: the cell under it does.
183
+ // Prevented, so the library does not reorder the tiles under it either.
184
+ if (placing.current) {
185
+ event.preventDefault();
186
+ return;
187
+ }
66
188
  const { source, target } = event.operation;
67
189
  if (!source || !target) return;
68
190
  const current = previewRef.current ?? tiles;
@@ -96,10 +218,25 @@ export function useTileReorder({
96
218
 
97
219
  const onDragEnd = (event: DragEndEvent) => {
98
220
  setDragging(false);
99
- const next = previewRef.current;
221
+ const live = placing.current;
222
+ const wasPlacing = live !== undefined;
223
+ placing.current = undefined;
224
+ const previewed = previewRef.current;
225
+ // A placing drag that let go in the cell it started from moved nothing:
226
+ // it writes nothing, and a report that flowed keeps flowing rather than
227
+ // being placed (and its heights rounded to rows) by a stray wiggle.
228
+ const stayed =
229
+ live !== undefined &&
230
+ previewed !== undefined &&
231
+ previewed[live.index]?.row === live.base[live.index]?.row &&
232
+ previewed[live.index]?.col === live.base[live.index]?.col;
233
+ // Placed tiles are written in reading order, so the file reads the way
234
+ // the page does and a narrow screen stacks them the same way.
235
+ const next =
236
+ wasPlacing && previewed ? inReadingOrder(previewed) : previewed;
100
237
  previewRef.current = undefined;
101
238
  setPreview(undefined);
102
- if (event.canceled || !next) return;
239
+ if (event.canceled || !next || stayed) return;
103
240
  // ONE history entry for the whole drag; a drag that ends where it began
104
241
  // changes nothing and makes none.
105
242
  commit(next);
@@ -109,5 +246,5 @@ export function useTileReorder({
109
246
  if (landing >= 0) onLanded(landing);
110
247
  };
111
248
 
112
- return { dragging, preview, onDragStart, onDragOver, onDragEnd };
249
+ return { dragging, preview, onDragStart, onDragMove, onDragOver, onDragEnd };
113
250
  }