@malloy-publisher/sdk 0.9.15 → 0.10.0

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 (94) hide show
  1. package/dist/{RenderedResult-BClN_PG6.cjs.js → RenderedResult-B5f4qOjp.cjs.js} +21 -4
  2. package/dist/{RenderedResult-YBzVMVH2.es.js → RenderedResult-DiDsN6JE.es.js} +204 -187
  3. package/dist/builder/index.cjs.js +75 -75
  4. package/dist/builder/index.es.js +5304 -5156
  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/TextTileBody.d.ts +9 -2
  17. package/dist/components/ReportBuilder/TileFrame.d.ts +34 -22
  18. package/dist/components/ReportBuilder/builderControls.d.ts +19 -0
  19. package/dist/components/ReportBuilder/placement.d.ts +48 -2
  20. package/dist/components/ReportBuilder/sortable.d.ts +9 -0
  21. package/dist/components/ReportBuilder/useBuilderSession.d.ts +1 -1
  22. package/dist/components/ReportBuilder/useBuilderShortcuts.d.ts +9 -1
  23. package/dist/components/ReportBuilder/useEdgeResize.d.ts +84 -0
  24. package/dist/components/ReportBuilder/useTileReorder.d.ts +3 -0
  25. package/dist/components/legacy/malloynb/CellRunnerView.d.ts +6 -6
  26. package/dist/hooks/useSettled.d.ts +5 -8
  27. package/dist/hooks/useSettledDraft.d.ts +33 -0
  28. package/dist/index.cjs.js +10 -10
  29. package/dist/index.es.js +3861 -3870
  30. package/dist/theme/classes.d.ts +1 -1
  31. package/dist/useCompiledDocument-C995sQ3j.cjs.js +142 -0
  32. package/dist/{useCompiledDocument-BGk38oaW.es.js → useCompiledDocument-CRmmbUza.es.js} +6426 -6257
  33. package/dist/utils/malloyLiteral.d.ts +2 -0
  34. package/package.json +11 -16
  35. package/src/builder-entry.ts +1 -0
  36. package/src/components/Dashboard/DashboardGrid.tsx +2 -0
  37. package/src/components/Dashboard/DashboardTile.tsx +4 -0
  38. package/src/components/Dashboard/DashboardView.tsx +1 -0
  39. package/src/components/RenderedResult/RenderedResult.tsx +18 -0
  40. package/src/components/RenderedResult/ResultContainer.tsx +39 -3
  41. package/src/components/RenderedResult/ResultPanel.tsx +4 -0
  42. package/src/components/RenderedResult/resultSizing.ts +6 -0
  43. package/src/components/ReportBuilder/ArrangeMenu.tsx +80 -0
  44. package/src/components/ReportBuilder/BuilderBar.tsx +4 -0
  45. package/src/components/ReportBuilder/BuilderDialogs.tsx +8 -1
  46. package/src/components/ReportBuilder/BuilderGrid.tsx +62 -60
  47. package/src/components/ReportBuilder/BuilderToolbar.tsx +4 -0
  48. package/src/components/ReportBuilder/EditorSurface.tsx +9 -2
  49. package/src/components/ReportBuilder/ReportBuilder.spec.tsx +99 -0
  50. package/src/components/ReportBuilder/ReportBuilder.tsx +110 -30
  51. package/src/components/ReportBuilder/TextTileBody.tsx +24 -4
  52. package/src/components/ReportBuilder/TileFrame.tsx +261 -310
  53. package/src/components/ReportBuilder/builderControls.ts +34 -0
  54. package/src/components/ReportBuilder/placement.spec.ts +235 -0
  55. package/src/components/ReportBuilder/placement.ts +179 -9
  56. package/src/components/ReportBuilder/readDocument.spec.ts +7 -17
  57. package/src/components/ReportBuilder/readDocument.ts +3 -6
  58. package/src/components/ReportBuilder/sortable.tsx +26 -2
  59. package/src/components/ReportBuilder/spliceDocument.spec.ts +0 -40
  60. package/src/components/ReportBuilder/spliceDocument.ts +0 -39
  61. package/src/components/ReportBuilder/useBuilderSession.ts +7 -2
  62. package/src/components/ReportBuilder/useBuilderShortcuts.ts +25 -1
  63. package/src/components/ReportBuilder/useEdgeResize.spec.tsx +324 -0
  64. package/src/components/ReportBuilder/useEdgeResize.ts +397 -0
  65. package/src/components/ReportBuilder/useTileReorder.ts +1 -1
  66. package/src/components/given/GivenInput.spec.tsx +447 -2
  67. package/src/components/given/GivenInput.tsx +192 -80
  68. package/src/components/given/GivensPanel.spec.tsx +59 -0
  69. package/src/components/given/GivensPanel.tsx +14 -2
  70. package/src/components/legacy/malloynb/CellRunnerView.tsx +6 -6
  71. package/src/hooks/useDimensionFilters.ts +1 -35
  72. package/src/hooks/useDimensionFiltersQuery.ts +2 -36
  73. package/src/hooks/useDimensionalFilterRangeData.ts +1 -35
  74. package/src/hooks/useGivensState.spec.ts +106 -1
  75. package/src/hooks/useGivensState.ts +48 -13
  76. package/src/hooks/useSettled.ts +5 -8
  77. package/src/hooks/useSettledDraft.ts +127 -0
  78. package/src/theme/classes.ts +1 -1
  79. package/src/utils/malloyLiteral.spec.ts +12 -1
  80. package/src/utils/malloyLiteral.ts +32 -0
  81. package/vite.config.ts +0 -2
  82. package/.eslintrc +0 -36
  83. package/dist/components/DOMElement/DOMElement.d.ts +0 -5
  84. package/dist/components/DOMElement/index.d.ts +0 -1
  85. package/dist/components/Package/FileTreeView.d.ts +0 -9
  86. package/dist/components/ReportBuilder/useTileHeight.d.ts +0 -39
  87. package/dist/components/ReportBuilder/useTileResize.d.ts +0 -40
  88. package/dist/useCompiledDocument-D9t5ry_t.cjs.js +0 -142
  89. package/src/components/DOMElement/DOMElement.tsx +0 -22
  90. package/src/components/DOMElement/index.ts +0 -4
  91. package/src/components/Package/FileTreeView.tsx +0 -244
  92. package/src/components/ReportBuilder/useTileHeight.ts +0 -149
  93. package/src/components/ReportBuilder/useTileResize.spec.tsx +0 -146
  94. package/src/components/ReportBuilder/useTileResize.ts +0 -148
@@ -1,22 +0,0 @@
1
- // Copyright (c) Credible Data Inc.
2
- // SPDX-License-Identifier: MIT
3
-
4
- import { useEffect, useRef } from "react";
5
-
6
- interface DOMElement {
7
- element: HTMLElement;
8
- }
9
-
10
- export default function DOMElement({ element }: DOMElement) {
11
- const ref = useRef<HTMLDivElement>(null);
12
-
13
- useEffect(() => {
14
- const parent = ref.current;
15
- if (parent) {
16
- parent.innerHTML = "";
17
- parent.appendChild(element);
18
- }
19
- }, [element]);
20
-
21
- return <div ref={ref}></div>;
22
- }
@@ -1,4 +0,0 @@
1
- // Copyright (c) Credible Data Inc.
2
- // SPDX-License-Identifier: MIT
3
-
4
- export { default as DOMElement } from "./DOMElement";
@@ -1,244 +0,0 @@
1
- // Copyright (c) Credible Data Inc.
2
- // SPDX-License-Identifier: MIT
3
-
4
- import * as React from "react";
5
- import {
6
- TreeItem2Content,
7
- TreeItem2Label,
8
- TreeItem2Root,
9
- TreeItem2IconContainer,
10
- } from "@mui/x-tree-view/TreeItem2";
11
- import { RichTreeView } from "@mui/x-tree-view/RichTreeView";
12
- import { TreeItem2Provider } from "@mui/x-tree-view/TreeItem2Provider";
13
- import { TreeViewBaseItem } from "@mui/x-tree-view/models";
14
- import {
15
- unstable_useTreeItem2 as useTreeItem2,
16
- UseTreeItem2Parameters,
17
- } from "@mui/x-tree-view/useTreeItem2";
18
- import { TreeItem2Icon } from "@mui/x-tree-view/TreeItem2Icon";
19
- import FolderIcon from "@mui/icons-material/FolderOutlined";
20
- import ArticleIcon from "@mui/icons-material/ArticleOutlined";
21
- import ErrorIcon from "@mui/icons-material/ErrorOutlined";
22
- import { TransitionProps } from "@mui/material/transitions";
23
- import { animated, useSpring } from "@react-spring/web";
24
- import Collapse from "@mui/material/Collapse";
25
- import DnsIcon from "@mui/icons-material/DnsOutlined";
26
- import DataArrayIcon from "@mui/icons-material/DataArrayOutlined";
27
- import { Database, Model } from "../../client";
28
- import { Typography, Tooltip } from "@mui/material";
29
-
30
- interface FiieTreeViewProps {
31
- items: Model[] | Database[];
32
- defaultExpandedItems: string[];
33
- onClickTreeNode?: (to: string, event?: React.MouseEvent) => void;
34
- }
35
-
36
- export function FileTreeView({
37
- items,
38
- defaultExpandedItems,
39
- onClickTreeNode,
40
- }: FiieTreeViewProps) {
41
- return (
42
- <RichTreeView
43
- items={getTreeView(items, onClickTreeNode)}
44
- defaultExpandedItems={defaultExpandedItems}
45
- slots={{ item: CustomTreeItem }}
46
- />
47
- );
48
- }
49
-
50
- const AnimatedCollapse = animated(Collapse);
51
-
52
- function TransitionComponent(props: TransitionProps) {
53
- const style = useSpring({
54
- to: {
55
- opacity: props.in ? 1 : 0,
56
- transform: `translate3d(0,${props.in ? 0 : 20}px,0)`,
57
- },
58
- });
59
-
60
- return <AnimatedCollapse style={style} {...props} />;
61
- }
62
-
63
- type FileType = "directory" | "model" | "notebook" | "database" | "unknown";
64
-
65
- type ExtendedTreeItemProps = {
66
- id: string;
67
- label: string;
68
- fileType: FileType;
69
- selectable: boolean;
70
- link: ((event?: React.MouseEvent) => void) | undefined;
71
- error?: string;
72
- };
73
-
74
- interface CustomLabelProps {
75
- item: ExtendedTreeItemProps;
76
- }
77
-
78
- function CustomTreeItem2Label({ item, ...other }: CustomLabelProps) {
79
- const label = (
80
- <TreeItem2Label
81
- {...other}
82
- sx={{
83
- display: "flex",
84
- alignItems: "center",
85
- color: item.error ? "error.main" : "grey.600",
86
- }}
87
- >
88
- {(item.fileType === "directory" && <FolderIcon />) ||
89
- (item.fileType === "notebook" && <ArticleIcon />) ||
90
- (item.fileType === "model" && <DataArrayIcon />) ||
91
- (item.fileType == "database" && <DnsIcon />) || <ErrorIcon />}
92
- <Typography
93
- variant="body2"
94
- sx={{ marginLeft: "5px" }}
95
- color={
96
- item.error
97
- ? "error.main"
98
- : item.link
99
- ? "primary.main"
100
- : "grey.600"
101
- }
102
- >
103
- {item.label}
104
- </Typography>
105
- </TreeItem2Label>
106
- );
107
-
108
- return item.error ? (
109
- <Tooltip title={item.error} placement="right">
110
- {label}
111
- </Tooltip>
112
- ) : (
113
- label
114
- );
115
- }
116
-
117
- interface CustomTreeItemProps
118
- extends Omit<UseTreeItem2Parameters, "rootRef">,
119
- Omit<React.HTMLAttributes<HTMLLIElement>, "onFocus"> {}
120
-
121
- const CustomTreeItem = React.forwardRef(function CustomTreeItem(
122
- props: CustomTreeItemProps,
123
- ref: React.Ref<HTMLLIElement>,
124
- ) {
125
- const { id, itemId, label, children, disabled, ...other } = props;
126
- const {
127
- getRootProps,
128
- getContentProps,
129
- getLabelProps,
130
- getIconContainerProps,
131
- getGroupTransitionProps,
132
- status,
133
- publicAPI,
134
- } = useTreeItem2({
135
- id,
136
- itemId,
137
- children,
138
- label,
139
- disabled,
140
- rootRef: ref,
141
- });
142
-
143
- const item = publicAPI.getItem(itemId);
144
-
145
- // Disable select and focus. We want to either click through to a model or notebook or just exapnd
146
- // collapse
147
- status.selected = false;
148
- status.focused = false;
149
-
150
- return (
151
- <TreeItem2Provider itemId={itemId}>
152
- <TreeItem2Root {...getRootProps(other)}>
153
- <TreeItem2Content
154
- {...getContentProps()}
155
- {...(item.link && {
156
- onClick: (event) => item.link(event),
157
- })}
158
- sx={{
159
- // If the item is not selectable, disable pointer events.
160
- pointerEvents: !item.selectable && "none",
161
- }}
162
- >
163
- <TreeItem2IconContainer {...getIconContainerProps()}>
164
- <TreeItem2Icon status={status} />
165
- </TreeItem2IconContainer>
166
- <CustomTreeItem2Label
167
- {...getLabelProps({
168
- item,
169
- })}
170
- />
171
- </TreeItem2Content>
172
- {children && <TransitionComponent {...getGroupTransitionProps()} />}
173
- </TreeItem2Root>
174
- </TreeItem2Provider>
175
- );
176
- });
177
-
178
- function getTreeView(
179
- metadataEntries: Model[] | Database[],
180
- onClickTreeNode: (to: string, event?: React.MouseEvent) => void,
181
- ): TreeViewBaseItem<ExtendedTreeItemProps>[] {
182
- const tree = new Map<string, unknown>();
183
- metadataEntries.map((entry: Model | Database) => {
184
- let node = tree;
185
- const pathParts = entry.path.split("/");
186
- pathParts.forEach((part, index) => {
187
- if (index === pathParts.length - 1) {
188
- node.set(part, entry);
189
- } else if (!node.has(part)) {
190
- node.set(part, new Map<string, unknown>());
191
- node = node.get(part) as Map<string, unknown>;
192
- } else {
193
- node = node.get(part) as Map<string, unknown>;
194
- }
195
- });
196
- });
197
- return getTreeViewRecursive(tree, "", onClickTreeNode);
198
- }
199
-
200
- function getTreeViewRecursive(
201
- node: Map<string, unknown>,
202
- path: string,
203
- onClickNode: (to: string, event?: React.MouseEvent) => void,
204
- ): TreeViewBaseItem<ExtendedTreeItemProps>[] {
205
- const treeViewItems: TreeViewBaseItem<ExtendedTreeItemProps>[] = [];
206
- node.forEach((value, key) => {
207
- const fileType =
208
- (key.endsWith(".malloy") && "model") ||
209
- (key.endsWith(".malloynb") && "notebook") ||
210
- (key.endsWith(".parquet") && "database") ||
211
- "unknown";
212
- if (fileType !== "unknown") {
213
- // This is a model or database.
214
- const entry = value as Model | Database;
215
- treeViewItems.push({
216
- id: path + key,
217
- label: key,
218
- fileType: fileType,
219
- link:
220
- fileType === "model" || fileType === "notebook"
221
- ? (event) => onClickNode(path + key, event)
222
- : undefined,
223
- selectable: fileType === "model" || fileType === "notebook",
224
- error: "error" in entry ? entry.error : undefined,
225
- });
226
- } else {
227
- // This is a directory.
228
- const childPath = path + key + "/";
229
- treeViewItems.push({
230
- id: childPath,
231
- label: key,
232
- fileType: "directory",
233
- selectable: true,
234
- link: undefined,
235
- children: getTreeViewRecursive(
236
- value as Map<string, unknown>,
237
- childPath,
238
- onClickNode,
239
- ),
240
- });
241
- }
242
- });
243
- return treeViewItems;
244
- }
@@ -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
- }
@@ -1,146 +0,0 @@
1
- // Copyright (c) Credible Data Inc.
2
- // SPDX-License-Identifier: MIT
3
-
4
- import { act, renderHook } from "@testing-library/react";
5
- import { describe, expect, it, mock } from "bun:test";
6
- import type { PointerEvent } from "react";
7
- import { GRID_GAP_PX } from "../Dashboard/DashboardGrid";
8
- import type { DashboardTile } from "./document";
9
- import { useTileResize } from "./useTileResize";
10
-
11
- const COLUMNS = 12;
12
- const GRID_WIDTH = 1200 + GRID_GAP_PX * (COLUMNS - 1);
13
- const TRACK = 100;
14
-
15
- const tiles: DashboardTile[] = [
16
- {
17
- name: "a",
18
- source: "s",
19
- declaration: { kind: "reference", from: "x" },
20
- colspan: 6,
21
- },
22
- { name: "b", source: "s", declaration: { kind: "inherited" } },
23
- ];
24
-
25
- /** A pointer event on a handle whose tile starts at `left`, with the grid measured. */
26
- const pointer = (clientX: number, left = 0) => {
27
- const handle = document.createElement("div");
28
- const tile = document.createElement("div");
29
- tile.appendChild(handle);
30
- tile.getBoundingClientRect = () =>
31
- ({ left, width: 600, top: 0, height: 100 }) as DOMRect;
32
- let captured = false;
33
- Object.assign(handle, {
34
- setPointerCapture: () => {
35
- captured = true;
36
- },
37
- hasPointerCapture: () => captured,
38
- releasePointerCapture: () => {
39
- captured = false;
40
- },
41
- });
42
- return {
43
- clientX,
44
- pointerId: 1,
45
- currentTarget: handle,
46
- stopPropagation: mock(() => {}),
47
- preventDefault: mock(() => {}),
48
- } as unknown as PointerEvent<HTMLDivElement>;
49
- };
50
-
51
- const mount = () => {
52
- const commit = mock((_index: number, _span: number) => {});
53
- const onStart = mock((_index: number) => {});
54
- const grid = document.createElement("div");
55
- grid.getBoundingClientRect = () =>
56
- ({ left: 0, width: GRID_WIDTH, top: 0, height: 400 }) as DOMRect;
57
- const gridBox = { current: grid as HTMLDivElement | null };
58
- const view = renderHook(() =>
59
- useTileResize({ tiles, columns: COLUMNS, gridBox, onStart, commit }),
60
- );
61
- return { view, commit, onStart };
62
- };
63
-
64
- describe("useTileResize", () => {
65
- it("previews the width in whole columns as the edge moves, and writes it once on release", () => {
66
- const { view, commit, onStart } = mount();
67
- act(() => view.result.current.startResize(pointer(600), 0));
68
- expect(onStart).toHaveBeenCalledWith(0);
69
- expect(view.result.current.resize).toMatchObject({
70
- index: 0,
71
- span: 6,
72
- track: TRACK,
73
- });
74
-
75
- // Eight tracks and seven gutters: the edge sits at exactly eight columns.
76
- act(() =>
77
- view.result.current.onResize(pointer(8 * TRACK + 7 * GRID_GAP_PX)),
78
- );
79
- expect(view.result.current.resize?.span).toBe(8);
80
- // Part way into the ninth column still rounds to eight; nothing is
81
- // written yet.
82
- act(() =>
83
- view.result.current.onResize(
84
- pointer(8 * TRACK + 7 * GRID_GAP_PX + TRACK / 3),
85
- ),
86
- );
87
- expect(view.result.current.resize?.span).toBe(8);
88
- expect(commit).not.toHaveBeenCalled();
89
-
90
- act(() => view.result.current.endResize(pointer(0)));
91
- expect(commit).toHaveBeenCalledTimes(1);
92
- expect(commit).toHaveBeenCalledWith(0, 8);
93
- expect(view.result.current.resize).toBeUndefined();
94
- });
95
-
96
- it("clamps the preview to the grid, one column to full width", () => {
97
- const { view } = mount();
98
- act(() => view.result.current.startResize(pointer(600), 0));
99
- act(() => view.result.current.onResize(pointer(-500)));
100
- expect(view.result.current.resize?.span).toBe(1);
101
- act(() => view.result.current.onResize(pointer(GRID_WIDTH * 3)));
102
- expect(view.result.current.resize?.span).toBe(COLUMNS);
103
- });
104
-
105
- it("writes nothing for a press that does not change the width", () => {
106
- const { view, commit } = mount();
107
- act(() => view.result.current.startResize(pointer(600), 1));
108
- act(() => view.result.current.endResize(pointer(600)));
109
- expect(commit).not.toHaveBeenCalled();
110
- expect(view.result.current.resize).toBeUndefined();
111
- });
112
-
113
- it("puts the width back when the gesture is cancelled", () => {
114
- const { view, commit } = mount();
115
- act(() => view.result.current.startResize(pointer(600), 0));
116
- act(() =>
117
- view.result.current.onResize(pointer(8 * TRACK + 7 * GRID_GAP_PX)),
118
- );
119
- expect(view.result.current.resize?.span).toBe(8);
120
- act(() =>
121
- view.result.current.endResize({
122
- ...pointer(0),
123
- type: "pointercancel",
124
- } as PointerEvent<HTMLDivElement>),
125
- );
126
- expect(commit).not.toHaveBeenCalled();
127
- expect(view.result.current.resize).toBeUndefined();
128
- });
129
-
130
- it("does nothing when the grid has not been measured", () => {
131
- const commit = mock(() => {});
132
- const view = renderHook(() =>
133
- useTileResize({
134
- tiles,
135
- columns: COLUMNS,
136
- gridBox: { current: null },
137
- onStart: () => {},
138
- commit,
139
- }),
140
- );
141
- act(() => view.result.current.startResize(pointer(600), 0));
142
- expect(view.result.current.resize).toBeUndefined();
143
- act(() => view.result.current.endResize(pointer(0)));
144
- expect(commit).not.toHaveBeenCalled();
145
- });
146
- });