@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
@@ -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
+ }
@@ -21,7 +21,7 @@ import {
21
21
  import { GAP_TYPE, type GapData } from "./sortable";
22
22
 
23
23
  /** The boxes of the tiles as drawn, against the grid's top left corner, by tile key. */
24
- function drawnBoxes(grid: HTMLElement): Map<string, Box> {
24
+ export function drawnBoxes(grid: HTMLElement): Map<string, Box> {
25
25
  const origin = grid.getBoundingClientRect();
26
26
  const boxes = new Map<string, Box>();
27
27
  for (const frame of grid.querySelectorAll<HTMLElement>("[data-tile-key]")) {
@@ -38,7 +38,7 @@ export const reportClasses = {
38
38
  saveStatus: "publisher-save-status",
39
39
  /** A tile as the builder draws it: selection, handle and menu around the card. */
40
40
  builderTile: "publisher-builder-tile",
41
- /** The grip on a tile's right edge that sets its width. */
41
+ /** The invisible strip along a tile's right edge that sets its width (drawn only as a focus ring). */
42
42
  widthHandle: "publisher-width-handle",
43
43
  /** The builder's menus (Add, a tile's ⋯, the save status). */
44
44
  menu: "publisher-menu",
@@ -1,39 +0,0 @@
1
- import { RefObject } from 'react';
2
- import { DashboardTile } from './document';
3
- /** A height drag in flight: the tile, what it WOULD be, and where its top sits. */
4
- export interface HeightState {
5
- index: number;
6
- /** The rows it would be, or undefined for auto: no `rowspan`, as tall as its row. */
7
- rows: number | undefined;
8
- /** Its `rowspan` when the drag began: a release that changed nothing writes nothing. */
9
- from: number | undefined;
10
- top: number;
11
- /** The tile's top against the grid's, where the row guides are drawn from. */
12
- guideTop: number;
13
- }
14
- /**
15
- * Dragging a tile's bottom edge to set its `rowspan`, in whole grid rows, or
16
- * back to auto. Previewed by the caller from `height.rows` and written ONCE,
17
- * on release, so one gesture is one undo step.
18
- *
19
- * Auto is a snap point of its own. A tile with no `rowspan` is as tall as its
20
- * row, and a row of such tiles is as tall as its tallest content, which is
21
- * almost never a whole number of rows: 492px sits between 5 rows (464) and 6
22
- * (560). With rows alone a tile could never be dragged level with an auto
23
- * neighbour; snapping to the row's own height, and writing that as no
24
- * `rowspan`, is what makes level reachable.
25
- */
26
- export declare function useTileHeight({ tiles, gridBox, onStart, commit, }: {
27
- tiles: readonly DashboardTile[];
28
- /** Wraps the grid: row-mates are found in it and the row guides are placed against it. */
29
- gridBox: RefObject<HTMLDivElement | null>;
30
- /** The tile a drag begins on, which the caller selects. */
31
- onStart: (index: number) => void;
32
- /** What to write when the drag ends: rows, or undefined to take the `rowspan` off. */
33
- commit: (index: number, rows: number | undefined) => void;
34
- }): {
35
- height: HeightState;
36
- startHeight: (event: React.PointerEvent<HTMLDivElement>, index: number) => void;
37
- onHeight: (event: React.PointerEvent<HTMLDivElement>) => void;
38
- endHeight: (event: React.PointerEvent<HTMLDivElement>) => void;
39
- };
@@ -1,40 +0,0 @@
1
- import { RefObject } from 'react';
2
- import { DashboardTile } from './document';
3
- /**
4
- * The widest a tile may be dragged: the whole grid, or, for a placed tile, up
5
- * to the grid's right edge from its column. A right edge dragged past the grid
6
- * stops there; it never moves the tile's left edge.
7
- */
8
- export declare const widestFrom: (tile: DashboardTile | undefined, columns: number) => number;
9
- /** A resize in flight: the tile, the width it WOULD be, and the geometry. */
10
- export interface ResizeState {
11
- index: number;
12
- span: number;
13
- /** The width the drag started from, so a release that changed nothing writes nothing. */
14
- from: number;
15
- left: number;
16
- track: number;
17
- /** The widest it may go: to the grid's right edge from the column a placed tile starts in. */
18
- max: number;
19
- }
20
- /**
21
- * Dragging a tile's right edge to set its `colspan`. The width is previewed
22
- * by the caller from `resize.span` and written ONCE, on release: writing on
23
- * every pointer move would put a dozen documents in the history for one
24
- * gesture, and undo would walk back through the drag a column at a time.
25
- */
26
- export declare function useTileResize({ tiles, columns, gridBox, onStart, commit, }: {
27
- tiles: readonly DashboardTile[];
28
- columns: number;
29
- /** Wraps the grid, so its content box IS the grid's: what a dragged edge is measured against to work out a column count. */
30
- gridBox: RefObject<HTMLDivElement | null>;
31
- /** The tile a resize begins on, which the caller selects. */
32
- onStart: (index: number) => void;
33
- /** The width to write when the drag ends. */
34
- commit: (index: number, span: number) => void;
35
- }): {
36
- resize: ResizeState;
37
- startResize: (event: React.PointerEvent<HTMLDivElement>, index: number) => void;
38
- onResize: (event: React.PointerEvent<HTMLDivElement>) => void;
39
- endResize: (event: React.PointerEvent<HTMLDivElement>) => void;
40
- };
@@ -1,149 +0,0 @@
1
- // Copyright (c) Credible Data Inc.
2
- // SPDX-License-Identifier: MIT
3
-
4
- import { useEffect, useRef, useState, type RefObject } from "react";
5
- import { rowsForHeight, tileHeightPx } from "../Dashboard/DashboardGrid";
6
- import { tileKey, type DashboardTile } from "./document";
7
- import { minRowsOf } from "./placement";
8
-
9
- /** A height drag in flight: the tile, what it WOULD be, and where its top sits. */
10
- export interface HeightState {
11
- index: number;
12
- /** The rows it would be, or undefined for auto: no `rowspan`, as tall as its row. */
13
- rows: number | undefined;
14
- /** Its `rowspan` when the drag began: a release that changed nothing writes nothing. */
15
- from: number | undefined;
16
- top: number;
17
- /** The tile's top against the grid's, where the row guides are drawn from. */
18
- guideTop: number;
19
- }
20
-
21
- /** While the edge is dragged the page stops selecting text and keeps one cursor, as a width drag does. */
22
- const dragChrome = (on: boolean) => {
23
- const body = window.document.body;
24
- body.style.userSelect = on ? "none" : "";
25
- body.style.cursor = on ? "row-resize" : "";
26
- };
27
-
28
- /**
29
- * Dragging a tile's bottom edge to set its `rowspan`, in whole grid rows, or
30
- * back to auto. Previewed by the caller from `height.rows` and written ONCE,
31
- * on release, so one gesture is one undo step.
32
- *
33
- * Auto is a snap point of its own. A tile with no `rowspan` is as tall as its
34
- * row, and a row of such tiles is as tall as its tallest content, which is
35
- * almost never a whole number of rows: 492px sits between 5 rows (464) and 6
36
- * (560). With rows alone a tile could never be dragged level with an auto
37
- * neighbour; snapping to the row's own height, and writing that as no
38
- * `rowspan`, is what makes level reachable.
39
- */
40
- export function useTileHeight({
41
- tiles,
42
- gridBox,
43
- onStart,
44
- commit,
45
- }: {
46
- tiles: readonly DashboardTile[];
47
- /** Wraps the grid: row-mates are found in it and the row guides are placed against it. */
48
- gridBox: RefObject<HTMLDivElement | null>;
49
- /** The tile a drag begins on, which the caller selects. */
50
- onStart: (index: number) => void;
51
- /** What to write when the drag ends: rows, or undefined to take the `rowspan` off. */
52
- commit: (index: number, rows: number | undefined) => void;
53
- }) {
54
- const [height, setHeightState] = useState<HeightState | undefined>(
55
- undefined,
56
- );
57
- // The gesture as the handlers last left it: a release can arrive before
58
- // the render for the last move does.
59
- const live = useRef<HeightState | undefined>(undefined);
60
- // A tile that began on auto: its height then, which was its row's.
61
- const startedAuto = useRef<number | undefined>(undefined);
62
- const setHeight = (next: HeightState | undefined) => {
63
- live.current = next;
64
- setHeightState(next);
65
- };
66
- useEffect(() => () => dragChrome(false), []);
67
-
68
- /**
69
- * How tall the row is when this tile is on auto: the tallest of the auto
70
- * tiles beside it, measured now. A neighbour stretched by this tile's own
71
- * preview reads as that preview's height, which ties with it and loses;
72
- * once the preview is shorter than the row, the neighbours read their own
73
- * height and auto wins. Undefined when nothing says how tall auto would be.
74
- */
75
- const autoHeight = (drag: HeightState): number | undefined => {
76
- const auto = new Set(
77
- tiles.filter((tile) => tile.rowspan === undefined).map(tileKey),
78
- );
79
- const self = tiles[drag.index];
80
- let tallest = startedAuto.current;
81
- for (const frame of gridBox.current?.querySelectorAll<HTMLElement>(
82
- "[data-tile-key]",
83
- ) ?? []) {
84
- const key = frame.dataset.tileKey;
85
- if (!key || !auto.has(key) || (self && key === tileKey(self)))
86
- continue;
87
- const box = frame.getBoundingClientRect();
88
- if (Math.abs(box.top - drag.top) > 1) continue;
89
- tallest = Math.max(tallest ?? 0, box.height);
90
- }
91
- return tallest;
92
- };
93
-
94
- const startHeight = (
95
- event: React.PointerEvent<HTMLDivElement>,
96
- index: number,
97
- ) => {
98
- const tile = event.currentTarget.parentElement?.getBoundingClientRect();
99
- const grid = gridBox.current?.getBoundingClientRect();
100
- if (!tile || !grid) return;
101
- event.stopPropagation();
102
- event.preventDefault();
103
- event.currentTarget.setPointerCapture(event.pointerId);
104
- dragChrome(true);
105
- onStart(index);
106
- const from = tiles[index]?.rowspan;
107
- startedAuto.current = from === undefined ? tile.height : undefined;
108
- setHeight({
109
- index,
110
- rows: from,
111
- from,
112
- top: tile.top,
113
- guideTop: tile.top - grid.top,
114
- });
115
- };
116
-
117
- const onHeight = (event: React.PointerEvent<HTMLDivElement>) => {
118
- const drag = live.current;
119
- if (!drag) return;
120
- const px = event.clientY - drag.top;
121
- const self = tiles[drag.index];
122
- const rows = rowsForHeight(px, self ? minRowsOf(self) : undefined);
123
- const auto = autoHeight(drag);
124
- // Ties go to rows: a neighbour stretched to the preview is no reason to leave it.
125
- const next =
126
- auto !== undefined &&
127
- Math.abs(px - auto) < Math.abs(px - tileHeightPx(rows))
128
- ? undefined
129
- : rows;
130
- if (next !== drag.rows) setHeight({ ...drag, rows: next });
131
- };
132
-
133
- const endHeight = (event: React.PointerEvent<HTMLDivElement>) => {
134
- if (event.currentTarget.hasPointerCapture(event.pointerId))
135
- event.currentTarget.releasePointerCapture(event.pointerId);
136
- dragChrome(false);
137
- const drag = live.current;
138
- if (!drag) return;
139
- const { index, rows, from } = drag;
140
- setHeight(undefined);
141
- startedAuto.current = undefined;
142
- // A cancelled gesture puts the height back, and a press that moved
143
- // nothing writes nothing.
144
- if (event.type === "pointercancel" || rows === from) return;
145
- commit(index, rows);
146
- };
147
-
148
- return { height, startHeight, onHeight, endHeight };
149
- }