@dxos/react-ui-canvas 0.8.4-main.fd6878d → 0.8.4-staging.60fe92afc8
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.
- package/LICENSE +102 -5
- package/dist/lib/browser/index.mjs +1157 -363
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +1157 -363
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/components/Canvas/Canvas.d.ts +2 -2
- package/dist/types/src/components/Canvas/Canvas.stories.d.ts +12 -4
- package/dist/types/src/components/Canvas/Canvas.stories.d.ts.map +1 -1
- package/dist/types/src/components/CellGrid/CellGrid.d.ts +21 -0
- package/dist/types/src/components/CellGrid/CellGrid.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/CellGrid.stories.d.ts +21 -0
- package/dist/types/src/components/CellGrid/CellGrid.stories.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/headers/Ruler.d.ts +15 -0
- package/dist/types/src/components/CellGrid/headers/Ruler.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/headers/TrackHeader.d.ts +19 -0
- package/dist/types/src/components/CellGrid/headers/TrackHeader.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/headers/index.d.ts +3 -0
- package/dist/types/src/components/CellGrid/headers/index.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/index.d.ts +6 -0
- package/dist/types/src/components/CellGrid/index.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/input/index.d.ts +3 -0
- package/dist/types/src/components/CellGrid/input/index.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/input/pointer.d.ts +33 -0
- package/dist/types/src/components/CellGrid/input/pointer.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/input/wheel.d.ts +14 -0
- package/dist/types/src/components/CellGrid/input/wheel.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/render/index.d.ts +3 -0
- package/dist/types/src/components/CellGrid/render/index.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/render/overlay-layer.d.ts +21 -0
- package/dist/types/src/components/CellGrid/render/overlay-layer.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/render/static-layer.d.ts +36 -0
- package/dist/types/src/components/CellGrid/render/static-layer.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/state/atoms.d.ts +23 -0
- package/dist/types/src/components/CellGrid/state/atoms.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/state/index.d.ts +4 -0
- package/dist/types/src/components/CellGrid/state/index.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/state/types.d.ts +39 -0
- package/dist/types/src/components/CellGrid/state/types.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/state/viewport.d.ts +52 -0
- package/dist/types/src/components/CellGrid/state/viewport.d.ts.map +1 -0
- package/dist/types/src/components/CellGrid/state/viewport.test.d.ts +2 -0
- package/dist/types/src/components/CellGrid/state/viewport.test.d.ts.map +1 -0
- package/dist/types/src/components/FPS.d.ts.map +1 -1
- package/dist/types/src/components/Grid/Grid.d.ts +2 -2
- package/dist/types/src/components/Grid/Grid.d.ts.map +1 -1
- package/dist/types/src/components/Grid/Grid.stories.d.ts +19 -4
- package/dist/types/src/components/Grid/Grid.stories.d.ts.map +1 -1
- package/dist/types/src/components/index.d.ts +1 -0
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/hooks/index.d.ts +1 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/projection.d.ts.map +1 -1
- package/dist/types/src/hooks/useDrag.d.ts +6 -0
- package/dist/types/src/hooks/useDrag.d.ts.map +1 -0
- package/dist/types/src/hooks/useWheel.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +1 -1
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/util/svg.d.ts +1 -1
- package/dist/types/src/util/svg.d.ts.map +1 -1
- package/dist/types/src/util/svg.stories.d.ts +12 -4
- package/dist/types/src/util/svg.stories.d.ts.map +1 -1
- package/dist/types/src/util/util.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +26 -26
- package/src/components/Canvas/Canvas.stories.tsx +12 -11
- package/src/components/Canvas/Canvas.tsx +4 -4
- package/src/components/CellGrid/CellGrid.stories.tsx +238 -0
- package/src/components/CellGrid/CellGrid.tsx +270 -0
- package/src/components/CellGrid/headers/Ruler.tsx +71 -0
- package/src/components/CellGrid/headers/TrackHeader.tsx +58 -0
- package/src/components/CellGrid/headers/index.ts +6 -0
- package/src/components/CellGrid/index.ts +9 -0
- package/src/components/CellGrid/input/index.ts +6 -0
- package/src/components/CellGrid/input/pointer.ts +236 -0
- package/src/components/CellGrid/input/wheel.ts +68 -0
- package/src/components/CellGrid/render/index.ts +6 -0
- package/src/components/CellGrid/render/overlay-layer.ts +66 -0
- package/src/components/CellGrid/render/static-layer.ts +112 -0
- package/src/components/CellGrid/state/atoms.ts +43 -0
- package/src/components/CellGrid/state/index.ts +7 -0
- package/src/components/CellGrid/state/types.ts +40 -0
- package/src/components/CellGrid/state/viewport.test.ts +50 -0
- package/src/components/CellGrid/state/viewport.ts +94 -0
- package/src/components/FPS.tsx +3 -3
- package/src/components/Grid/Grid.stories.tsx +9 -9
- package/src/components/Grid/Grid.tsx +13 -15
- package/src/components/index.ts +1 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useDrag.tsx +96 -0
- package/src/hooks/useWheel.tsx +0 -28
- package/src/types.ts +1 -1
- package/src/util/svg.stories.tsx +9 -9
- package/src/util/svg.tsx +1 -1
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
export type CellCoord = { col: number; row: number };
|
|
6
|
+
|
|
7
|
+
export type Cell<T = unknown> = {
|
|
8
|
+
col: number;
|
|
9
|
+
row: number;
|
|
10
|
+
length: number;
|
|
11
|
+
data?: T;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type Viewport = {
|
|
15
|
+
/** World-space scroll offset (pixels). */
|
|
16
|
+
scrollX: number;
|
|
17
|
+
scrollY: number;
|
|
18
|
+
/** Cell dimensions in pixels at zoomX = 1. */
|
|
19
|
+
baseCellWidth: number;
|
|
20
|
+
cellHeight: number;
|
|
21
|
+
/** Horizontal zoom factor. */
|
|
22
|
+
zoomX: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type SelectionRange = {
|
|
26
|
+
col0: number;
|
|
27
|
+
row0: number;
|
|
28
|
+
col1: number;
|
|
29
|
+
row1: number;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type Selection = {
|
|
33
|
+
range: SelectionRange | null;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type Tool = 'toggle' | 'select' | 'resize' | 'edit' | 'delete';
|
|
37
|
+
|
|
38
|
+
export type Row = { id: string; label?: string };
|
|
39
|
+
|
|
40
|
+
export type Headers = { left: number; top: number };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { defaultViewport } from './atoms';
|
|
8
|
+
import { hitTestCell, screenToWorld, visibleCellRange, visibleCells, worldToScreen } from './viewport';
|
|
9
|
+
|
|
10
|
+
const headers = { left: 80, top: 24 };
|
|
11
|
+
|
|
12
|
+
describe('viewport', () => {
|
|
13
|
+
test('worldToScreen / screenToWorld round-trip', ({ expect }) => {
|
|
14
|
+
const viewport = { ...defaultViewport(), scrollX: 100, scrollY: 50 };
|
|
15
|
+
const { x, y } = worldToScreen(viewport, headers, { col: 5, row: 3 });
|
|
16
|
+
const back = screenToWorld(viewport, headers, { x, y });
|
|
17
|
+
expect(back.col).toBeCloseTo(5);
|
|
18
|
+
expect(back.row).toBeCloseTo(3);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('hitTestCell returns null in header region', ({ expect }) => {
|
|
22
|
+
const viewport = defaultViewport();
|
|
23
|
+
expect(hitTestCell(viewport, headers, { x: 10, y: 10 })).toBeNull();
|
|
24
|
+
expect(hitTestCell(viewport, headers, { x: 200, y: 10 })).toBeNull();
|
|
25
|
+
expect(hitTestCell(viewport, headers, { x: 10, y: 200 })).toBeNull();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('hitTestCell floors fractional coords', ({ expect }) => {
|
|
29
|
+
const viewport = { ...defaultViewport(), baseCellWidth: 20, cellHeight: 20 };
|
|
30
|
+
const coord = hitTestCell(viewport, headers, { x: 80 + 25, y: 24 + 45 });
|
|
31
|
+
expect(coord).toEqual({ col: 1, row: 2 });
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('visibleCellRange respects scroll', ({ expect }) => {
|
|
35
|
+
const viewport = { ...defaultViewport(), scrollX: 240, baseCellWidth: 24, cellHeight: 24 };
|
|
36
|
+
const range = visibleCellRange(viewport, headers, { width: 400, height: 200 });
|
|
37
|
+
expect(range.minCol).toBe(10);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('visibleCells filters by row and column extent', ({ expect }) => {
|
|
41
|
+
const cells = new Map([
|
|
42
|
+
['0,0', { col: 0, row: 0, length: 1 }],
|
|
43
|
+
['100,0', { col: 100, row: 0, length: 1 }],
|
|
44
|
+
['5,5', { col: 5, row: 5, length: 1 }],
|
|
45
|
+
['8,1', { col: 8, row: 1, length: 10 }],
|
|
46
|
+
]);
|
|
47
|
+
const result = Array.from(visibleCells(cells, { minCol: 0, maxCol: 12, minRow: 0, maxRow: 2 }));
|
|
48
|
+
expect(result.map((cell) => `${cell.col},${cell.row}`).sort()).toEqual(['0,0', '8,1']);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type { Cell, CellCoord, Headers, Viewport } from './types';
|
|
6
|
+
|
|
7
|
+
export const cellKey = (col: number, row: number): string => `${col},${row}`;
|
|
8
|
+
|
|
9
|
+
export const cellWidth = (viewport: Viewport): number => viewport.baseCellWidth * viewport.zoomX;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Convert a cell's world coordinates to screen-space pixel rectangle (relative to canvas origin).
|
|
13
|
+
*/
|
|
14
|
+
export const worldToScreen = (
|
|
15
|
+
viewport: Viewport,
|
|
16
|
+
headers: Headers,
|
|
17
|
+
coord: { col: number; row: number; length?: number },
|
|
18
|
+
): { x: number; y: number; w: number; h: number } => {
|
|
19
|
+
const w = cellWidth(viewport);
|
|
20
|
+
return {
|
|
21
|
+
x: headers.left + coord.col * w - viewport.scrollX,
|
|
22
|
+
y: headers.top + coord.row * viewport.cellHeight - viewport.scrollY,
|
|
23
|
+
w: (coord.length ?? 1) * w,
|
|
24
|
+
h: viewport.cellHeight,
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Convert screen-space pixels (relative to canvas origin) to fractional cell coordinates.
|
|
30
|
+
*/
|
|
31
|
+
export const screenToWorld = (
|
|
32
|
+
viewport: Viewport,
|
|
33
|
+
headers: Headers,
|
|
34
|
+
point: { x: number; y: number },
|
|
35
|
+
): { col: number; row: number } => {
|
|
36
|
+
const w = cellWidth(viewport);
|
|
37
|
+
return {
|
|
38
|
+
col: (point.x - headers.left + viewport.scrollX) / w,
|
|
39
|
+
row: (point.y - headers.top + viewport.scrollY) / viewport.cellHeight,
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const hitTestCell = (
|
|
44
|
+
viewport: Viewport,
|
|
45
|
+
headers: Headers,
|
|
46
|
+
point: { x: number; y: number },
|
|
47
|
+
): CellCoord | null => {
|
|
48
|
+
if (point.x < headers.left || point.y < headers.top) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
const { col, row } = screenToWorld(viewport, headers, point);
|
|
52
|
+
if (col < 0 || row < 0) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return { col: Math.floor(col), row: Math.floor(row) };
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Compute the inclusive range of cell coordinates intersecting the visible content rect.
|
|
60
|
+
*/
|
|
61
|
+
export const visibleCellRange = (
|
|
62
|
+
viewport: Viewport,
|
|
63
|
+
headers: Headers,
|
|
64
|
+
size: { width: number; height: number },
|
|
65
|
+
): { minCol: number; maxCol: number; minRow: number; maxRow: number } => {
|
|
66
|
+
const w = cellWidth(viewport);
|
|
67
|
+
const innerW = Math.max(0, size.width - headers.left);
|
|
68
|
+
const innerH = Math.max(0, size.height - headers.top);
|
|
69
|
+
const minCol = Math.max(0, Math.floor(viewport.scrollX / w));
|
|
70
|
+
const maxCol = Math.floor((viewport.scrollX + innerW) / w);
|
|
71
|
+
const minRow = Math.max(0, Math.floor(viewport.scrollY / viewport.cellHeight));
|
|
72
|
+
const maxRow = Math.floor((viewport.scrollY + innerH) / viewport.cellHeight);
|
|
73
|
+
return { minCol, maxCol, minRow, maxRow };
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Iterate sparse cell map, yielding only cells whose horizontal extent intersects visible cols and whose row is visible.
|
|
78
|
+
*/
|
|
79
|
+
export const visibleCells = function* <T>(
|
|
80
|
+
cells: ReadonlyMap<string, Cell<T>>,
|
|
81
|
+
range: { minCol: number; maxCol: number; minRow: number; maxRow: number },
|
|
82
|
+
): Generator<Cell<T>> {
|
|
83
|
+
for (const cell of cells.values()) {
|
|
84
|
+
if (cell.row < range.minRow || cell.row > range.maxRow) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const start = cell.col;
|
|
88
|
+
const end = cell.col + cell.length - 1;
|
|
89
|
+
if (end < range.minCol || start > range.maxCol) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
yield cell;
|
|
93
|
+
}
|
|
94
|
+
};
|
package/src/components/FPS.tsx
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import React, { useEffect, useReducer, useRef } from 'react';
|
|
7
7
|
|
|
8
8
|
import { type ThemedClassName } from '@dxos/react-ui';
|
|
9
|
-
import { mx } from '@dxos/
|
|
9
|
+
import { mx } from '@dxos/ui-theme';
|
|
10
10
|
|
|
11
11
|
export type FPSProps = ThemedClassName<{
|
|
12
12
|
width?: number;
|
|
@@ -53,7 +53,7 @@ export const FPS = ({ classNames, width = 60, height = 30, bar = 'bg-cyan-500' }
|
|
|
53
53
|
},
|
|
54
54
|
);
|
|
55
55
|
|
|
56
|
-
const requestRef = useRef<number>();
|
|
56
|
+
const requestRef = useRef<number | null>(null);
|
|
57
57
|
const tick = () => {
|
|
58
58
|
dispatch();
|
|
59
59
|
requestRef.current = requestAnimationFrame(tick);
|
|
@@ -73,7 +73,7 @@ export const FPS = ({ classNames, width = 60, height = 30, bar = 'bg-cyan-500' }
|
|
|
73
73
|
style={{ width: width + 6 }}
|
|
74
74
|
className={mx(
|
|
75
75
|
'relative flex flex-col p-0.5',
|
|
76
|
-
'bg-
|
|
76
|
+
'bg-base-surface text-xs text-subdued font-thin pointer-events-none border border-separator',
|
|
77
77
|
classNames,
|
|
78
78
|
)}
|
|
79
79
|
>
|
|
@@ -2,15 +2,12 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import '@
|
|
6
|
-
|
|
7
|
-
import type { Meta, StoryObj } from '@storybook/react-vite';
|
|
5
|
+
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
8
6
|
import React, { useRef, useState } from 'react';
|
|
9
7
|
|
|
10
|
-
import { withLayout, withTheme } from '@dxos/
|
|
8
|
+
import { withLayout, withTheme } from '@dxos/react-ui/testing';
|
|
11
9
|
|
|
12
10
|
import { type ProjectionState } from '../../hooks';
|
|
13
|
-
|
|
14
11
|
import { GridComponent, type GridProps } from './Grid';
|
|
15
12
|
|
|
16
13
|
const DefaultStory = (props: GridProps) => {
|
|
@@ -24,16 +21,19 @@ const DefaultStory = (props: GridProps) => {
|
|
|
24
21
|
);
|
|
25
22
|
};
|
|
26
23
|
|
|
27
|
-
const meta
|
|
24
|
+
const meta = {
|
|
28
25
|
title: 'ui/react-ui-canvas/Grid',
|
|
29
26
|
component: GridComponent,
|
|
30
27
|
render: DefaultStory,
|
|
31
|
-
decorators: [withTheme, withLayout({
|
|
32
|
-
|
|
28
|
+
decorators: [withTheme(), withLayout({ layout: 'fullscreen' })],
|
|
29
|
+
parameters: {
|
|
30
|
+
layout: 'fullscreen',
|
|
31
|
+
},
|
|
32
|
+
} satisfies Meta<typeof GridComponent>;
|
|
33
33
|
|
|
34
34
|
export default meta;
|
|
35
35
|
|
|
36
|
-
type Story = StoryObj<
|
|
36
|
+
type Story = StoryObj<typeof meta>;
|
|
37
37
|
|
|
38
38
|
export const Default: Story = {
|
|
39
39
|
args: {
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import React, { forwardRef, useId, useMemo } from 'react';
|
|
6
6
|
|
|
7
7
|
import { type ThemedClassName, useForwardedRef } from '@dxos/react-ui';
|
|
8
|
-
import { mx } from '@dxos/
|
|
8
|
+
import { mx } from '@dxos/ui-theme';
|
|
9
9
|
|
|
10
10
|
import { useCanvasContext } from '../../hooks';
|
|
11
11
|
import { type Point } from '../../types';
|
|
@@ -18,6 +18,8 @@ const defaultOffset: Point = { x: 0, y: 0 };
|
|
|
18
18
|
|
|
19
19
|
const createId = (parent: string, grid: number) => `dx-canvas-grid-${parent}-${grid}`;
|
|
20
20
|
|
|
21
|
+
// TODO(burdon): Click to drag.
|
|
22
|
+
|
|
21
23
|
export type GridProps = ThemedClassName<{
|
|
22
24
|
size?: number;
|
|
23
25
|
scale?: number;
|
|
@@ -25,32 +27,34 @@ export type GridProps = ThemedClassName<{
|
|
|
25
27
|
showAxes?: boolean;
|
|
26
28
|
}>;
|
|
27
29
|
|
|
30
|
+
// TODO(burdon): Use id of parent canvas.
|
|
31
|
+
export const Grid = (props: GridProps) => {
|
|
32
|
+
const { scale, offset } = useCanvasContext();
|
|
33
|
+
return <GridComponent {...props} scale={scale} offset={offset} />;
|
|
34
|
+
};
|
|
35
|
+
|
|
28
36
|
export const GridComponent = forwardRef<SVGSVGElement, GridProps>(
|
|
29
37
|
(
|
|
30
38
|
{ size: gridSize = defaultGridSize, scale = 1, offset = defaultOffset, showAxes = true, classNames },
|
|
31
39
|
forwardedRef,
|
|
32
40
|
) => {
|
|
33
41
|
const svgRef = useForwardedRef(forwardedRef);
|
|
42
|
+
const { width = 0, height = 0 } = svgRef.current?.getBoundingClientRect() ?? {};
|
|
43
|
+
|
|
34
44
|
const instanceId = useId();
|
|
35
45
|
const grids = useMemo(
|
|
36
46
|
() =>
|
|
37
47
|
gridRatios
|
|
38
48
|
.map((ratio) => ({ id: ratio, size: ratio * gridSize * scale }))
|
|
39
|
-
.filter(({ size }) => size >= gridSize && size <=
|
|
49
|
+
.filter(({ size }) => size >= gridSize && size <= 128),
|
|
40
50
|
[gridSize, scale],
|
|
41
51
|
);
|
|
42
52
|
|
|
43
|
-
const { width = 0, height = 0 } = svgRef.current?.getBoundingClientRect() ?? {};
|
|
44
|
-
|
|
45
53
|
return (
|
|
46
54
|
<svg
|
|
47
55
|
{...testId('dx-canvas-grid')}
|
|
48
56
|
ref={svgRef}
|
|
49
|
-
className={mx(
|
|
50
|
-
'absolute inset-0 w-full h-full pointer-events-none touch-none select-none',
|
|
51
|
-
'stroke-neutral-500',
|
|
52
|
-
classNames,
|
|
53
|
-
)}
|
|
57
|
+
className={mx('dx-fullscreen pointer-events-none touch-none select-none', 'stroke-neutral-500', classNames)}
|
|
54
58
|
>
|
|
55
59
|
{/* NOTE: The pattern is offset so that the middle of the pattern aligns with the grid. */}
|
|
56
60
|
<defs>
|
|
@@ -79,9 +83,3 @@ export const GridComponent = forwardRef<SVGSVGElement, GridProps>(
|
|
|
79
83
|
);
|
|
80
84
|
},
|
|
81
85
|
);
|
|
82
|
-
|
|
83
|
-
// TODO(burdon): Use id of parent canvas.
|
|
84
|
-
export const Grid = (props: GridProps) => {
|
|
85
|
-
const { scale, offset } = useCanvasContext();
|
|
86
|
-
return <GridComponent {...props} scale={scale} offset={offset} />;
|
|
87
|
-
};
|
package/src/components/index.ts
CHANGED
package/src/hooks/index.ts
CHANGED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { bind } from 'bind-event-listener';
|
|
6
|
+
import { useEffect, useRef } from 'react';
|
|
7
|
+
|
|
8
|
+
import { useCanvasContext } from './useCanvasContext';
|
|
9
|
+
|
|
10
|
+
export type DragOptions = {
|
|
11
|
+
// TODO(burdon): Add constraints?
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Handle drag events to update the transform state (offset).
|
|
16
|
+
*/
|
|
17
|
+
export const useDrag = (_options: DragOptions = {}) => {
|
|
18
|
+
const { root, setProjection } = useCanvasContext();
|
|
19
|
+
|
|
20
|
+
// Track drag state.
|
|
21
|
+
const state = useRef<{
|
|
22
|
+
panning: boolean;
|
|
23
|
+
x: number;
|
|
24
|
+
y: number;
|
|
25
|
+
}>({ panning: false, x: 0, y: 0 });
|
|
26
|
+
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (!root) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// TODO(burdon): Use d3-drag?
|
|
33
|
+
return bind(root, {
|
|
34
|
+
type: 'pointerdown',
|
|
35
|
+
listener: (ev: PointerEvent) => {
|
|
36
|
+
// Only left click.
|
|
37
|
+
if (ev.button !== 0) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (ev.defaultPrevented) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (ev.target !== root || ev.shiftKey) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Check if clicking on an interactive element?
|
|
50
|
+
// For now, assume if it bubbled to root, it's fair game unless prevented.
|
|
51
|
+
|
|
52
|
+
ev.preventDefault(); // Prevent text selection.
|
|
53
|
+
root.setPointerCapture(ev.pointerId);
|
|
54
|
+
state.current = { panning: true, x: ev.clientX, y: ev.clientY };
|
|
55
|
+
|
|
56
|
+
const moveUnbind = bind(root, {
|
|
57
|
+
type: 'pointermove',
|
|
58
|
+
listener: (ev: PointerEvent) => {
|
|
59
|
+
if (!state.current.panning) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Calculate delta.
|
|
64
|
+
const dx = ev.clientX - state.current.x;
|
|
65
|
+
const dy = ev.clientY - state.current.y;
|
|
66
|
+
|
|
67
|
+
state.current.x = ev.clientX;
|
|
68
|
+
state.current.y = ev.clientY;
|
|
69
|
+
|
|
70
|
+
setProjection((prev) => ({
|
|
71
|
+
...prev,
|
|
72
|
+
offset: {
|
|
73
|
+
x: prev.offset.x + dx,
|
|
74
|
+
y: prev.offset.y + dy,
|
|
75
|
+
},
|
|
76
|
+
}));
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const upUnbind = bind(root, {
|
|
81
|
+
type: 'pointerup',
|
|
82
|
+
listener: (ev: PointerEvent) => {
|
|
83
|
+
state.current.panning = false;
|
|
84
|
+
root.releasePointerCapture(ev.pointerId);
|
|
85
|
+
moveUnbind();
|
|
86
|
+
upUnbind();
|
|
87
|
+
// Clean up lostpointercapture as well?
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Handle cancellation/lost capture just in case?
|
|
92
|
+
// Using setPointerCapture usually handles this well on the element.
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
}, [root]);
|
|
96
|
+
};
|
package/src/hooks/useWheel.tsx
CHANGED
|
@@ -6,7 +6,6 @@ import { bindAll } from 'bind-event-listener';
|
|
|
6
6
|
import { useEffect } from 'react';
|
|
7
7
|
|
|
8
8
|
import { getRelativePoint } from '../util';
|
|
9
|
-
|
|
10
9
|
import { getZoomTransform } from './projection';
|
|
11
10
|
import { useCanvasContext } from './useCanvasContext';
|
|
12
11
|
|
|
@@ -34,9 +33,6 @@ export const useWheel = (options: WheelOptions = defaultOptions) => {
|
|
|
34
33
|
options: { capture: true, passive: false },
|
|
35
34
|
listener: (ev: WheelEvent) => {
|
|
36
35
|
const zooming = isWheelZooming(ev);
|
|
37
|
-
if (!hasFocus(root) && !zooming) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
36
|
|
|
41
37
|
ev.preventDefault();
|
|
42
38
|
if (zooming && !options.zoom) {
|
|
@@ -82,27 +78,3 @@ const isWheelZooming = (ev: WheelEvent): boolean => {
|
|
|
82
78
|
|
|
83
79
|
return false;
|
|
84
80
|
};
|
|
85
|
-
|
|
86
|
-
const hasFocus = (element: HTMLElement): boolean => {
|
|
87
|
-
const activeElement = document.activeElement;
|
|
88
|
-
if (!activeElement) {
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Handle shadow DOM.
|
|
93
|
-
let shadowActive = activeElement;
|
|
94
|
-
while (shadowActive?.shadowRoot?.activeElement) {
|
|
95
|
-
shadowActive = shadowActive.shadowRoot.activeElement;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Check if element or any parent has focus.
|
|
99
|
-
let current: HTMLElement | null = element;
|
|
100
|
-
while (current) {
|
|
101
|
-
if (current === activeElement || current === shadowActive) {
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
current = current.parentElement;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return false;
|
|
108
|
-
};
|
package/src/types.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
6
|
|
|
7
7
|
export const Point = Schema.Struct({ x: Schema.Number, y: Schema.Number });
|
|
8
8
|
export const Dimension = Schema.Struct({ width: Schema.Number, height: Schema.Number });
|
package/src/util/svg.stories.tsx
CHANGED
|
@@ -2,18 +2,16 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import '@
|
|
6
|
-
|
|
7
|
-
import type { Meta } from '@storybook/react-vite';
|
|
5
|
+
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
8
6
|
import React from 'react';
|
|
9
7
|
|
|
10
|
-
import { withTheme } from '@dxos/
|
|
8
|
+
import { withTheme } from '@dxos/react-ui/testing';
|
|
11
9
|
|
|
12
10
|
import { Arrow, createPath } from './svg';
|
|
13
11
|
import { testId } from './util';
|
|
14
12
|
|
|
15
13
|
const DefaultStory = () => (
|
|
16
|
-
<svg className='border border-
|
|
14
|
+
<svg className='border border-separator w-[30rem] h-[400px]'>
|
|
17
15
|
<defs>
|
|
18
16
|
<Arrow id='arrow-start' classNames='fill-none stroke-red-500' dir='start' />
|
|
19
17
|
<Arrow id='arrow-end' classNames='fill-none stroke-red-500' dir='end' />
|
|
@@ -31,15 +29,17 @@ const DefaultStory = () => (
|
|
|
31
29
|
</svg>
|
|
32
30
|
);
|
|
33
31
|
|
|
34
|
-
const meta
|
|
32
|
+
const meta = {
|
|
35
33
|
title: 'ui/react-ui-canvas/svg',
|
|
36
34
|
render: DefaultStory,
|
|
37
|
-
decorators: [withTheme],
|
|
35
|
+
decorators: [withTheme()],
|
|
38
36
|
parameters: {
|
|
39
37
|
layout: 'centered',
|
|
40
38
|
},
|
|
41
|
-
};
|
|
39
|
+
} satisfies Meta;
|
|
42
40
|
|
|
43
41
|
export default meta;
|
|
44
42
|
|
|
45
|
-
|
|
43
|
+
type Story = StoryObj<typeof meta>;
|
|
44
|
+
|
|
45
|
+
export const Default: Story = {};
|
package/src/util/svg.tsx
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import React, { type PropsWithChildren, type SVGProps } from 'react';
|
|
6
6
|
|
|
7
7
|
import { type ThemedClassName } from '@dxos/react-ui';
|
|
8
|
-
import { mx } from '@dxos/
|
|
8
|
+
import { mx } from '@dxos/ui-theme';
|
|
9
9
|
|
|
10
10
|
import { type Dimension, type Point } from '../types';
|
|
11
11
|
|