@dxos/react-ui-graph 0.8.2-main.10c050d
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 +8 -0
- package/README.md +25 -0
- package/dist/lib/browser/chunk-2YMRPQU7.mjs +979 -0
- package/dist/lib/browser/chunk-2YMRPQU7.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +1546 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/browser/testing/index.mjs +180 -0
- package/dist/lib/browser/testing/index.mjs.map +7 -0
- package/dist/lib/node/chunk-WFHWEMPB.cjs +919 -0
- package/dist/lib/node/chunk-WFHWEMPB.cjs.map +7 -0
- package/dist/lib/node/index.cjs +1547 -0
- package/dist/lib/node/index.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -0
- package/dist/lib/node/testing/index.cjs +214 -0
- package/dist/lib/node/testing/index.cjs.map +7 -0
- package/dist/lib/node-esm/chunk-ITHTKQOY.mjs +981 -0
- package/dist/lib/node-esm/chunk-ITHTKQOY.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +1547 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/lib/node-esm/testing/index.mjs +181 -0
- package/dist/lib/node-esm/testing/index.mjs.map +7 -0
- package/dist/types/src/components/Graph/Graph.d.ts +26 -0
- package/dist/types/src/components/Graph/Graph.d.ts.map +1 -0
- package/dist/types/src/components/Graph/Graph.stories.d.ts +29 -0
- package/dist/types/src/components/Graph/Graph.stories.d.ts.map +1 -0
- package/dist/types/src/components/Graph/index.d.ts +2 -0
- package/dist/types/src/components/Graph/index.d.ts.map +1 -0
- package/dist/types/src/components/Mesh/Mesh.d.ts +6 -0
- package/dist/types/src/components/Mesh/Mesh.d.ts.map +1 -0
- package/dist/types/src/components/Mesh/Mesh.stories.d.ts +12 -0
- package/dist/types/src/components/Mesh/Mesh.stories.d.ts.map +1 -0
- package/dist/types/src/components/Mesh/index.d.ts +2 -0
- package/dist/types/src/components/Mesh/index.d.ts.map +1 -0
- package/dist/types/src/components/SVG/Grid.d.ts +9 -0
- package/dist/types/src/components/SVG/Grid.d.ts.map +1 -0
- package/dist/types/src/components/SVG/Markers.d.ts +10 -0
- package/dist/types/src/components/SVG/Markers.d.ts.map +1 -0
- package/dist/types/src/components/SVG/Root.d.ts +12 -0
- package/dist/types/src/components/SVG/Root.d.ts.map +1 -0
- package/dist/types/src/components/SVG/SVG.d.ts +23 -0
- package/dist/types/src/components/SVG/SVG.d.ts.map +1 -0
- package/dist/types/src/components/SVG/Svg.stories.d.ts +17 -0
- package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -0
- package/dist/types/src/components/SVG/Zoom.d.ts +12 -0
- package/dist/types/src/components/SVG/Zoom.d.ts.map +1 -0
- package/dist/types/src/components/SVG/index.d.ts +2 -0
- package/dist/types/src/components/SVG/index.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +4 -0
- package/dist/types/src/components/index.d.ts.map +1 -0
- package/dist/types/src/fx/fx.stories.d.ts +19 -0
- package/dist/types/src/fx/fx.stories.d.ts.map +1 -0
- package/dist/types/src/fx/index.d.ts +2 -0
- package/dist/types/src/fx/index.d.ts.map +1 -0
- package/dist/types/src/fx/pulsar.d.ts +17 -0
- package/dist/types/src/fx/pulsar.d.ts.map +1 -0
- package/dist/types/src/graph/index.d.ts +5 -0
- package/dist/types/src/graph/index.d.ts.map +1 -0
- package/dist/types/src/graph/markers.d.ts +11 -0
- package/dist/types/src/graph/markers.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-drag.d.ts +16 -0
- package/dist/types/src/graph/projector/graph-drag.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-force-projector.d.ts +96 -0
- package/dist/types/src/graph/projector/graph-force-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-forces.d.ts +8 -0
- package/dist/types/src/graph/projector/graph-forces.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts +14 -0
- package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-projector.d.ts +25 -0
- package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts +34 -0
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-relational-projector.d.ts +14 -0
- package/dist/types/src/graph/projector/graph-relational-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/index.d.ts +8 -0
- package/dist/types/src/graph/projector/index.d.ts.map +1 -0
- package/dist/types/src/graph/projector/projector.d.ts +32 -0
- package/dist/types/src/graph/projector/projector.d.ts.map +1 -0
- package/dist/types/src/graph/renderer/bullets.d.ts +16 -0
- package/dist/types/src/graph/renderer/bullets.d.ts.map +1 -0
- package/dist/types/src/graph/renderer/graph-renderer.d.ts +43 -0
- package/dist/types/src/graph/renderer/graph-renderer.d.ts.map +1 -0
- package/dist/types/src/graph/renderer/index.d.ts +4 -0
- package/dist/types/src/graph/renderer/index.d.ts.map +1 -0
- package/dist/types/src/graph/renderer/linker.d.ts +12 -0
- package/dist/types/src/graph/renderer/linker.d.ts.map +1 -0
- package/dist/types/src/graph/renderer/renderer.d.ts +20 -0
- package/dist/types/src/graph/renderer/renderer.d.ts.map +1 -0
- package/dist/types/src/graph/types.d.ts +52 -0
- package/dist/types/src/graph/types.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +5 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -0
- package/dist/types/src/hooks/useGrid.d.ts +24 -0
- package/dist/types/src/hooks/useGrid.d.ts.map +1 -0
- package/dist/types/src/hooks/useStateRef.d.ts +22 -0
- package/dist/types/src/hooks/useStateRef.d.ts.map +1 -0
- package/dist/types/src/hooks/useSvgContext.d.ts +41 -0
- package/dist/types/src/hooks/useSvgContext.d.ts.map +1 -0
- package/dist/types/src/hooks/useZoom.d.ts +34 -0
- package/dist/types/src/hooks/useZoom.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +6 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/stories/hooks.stories.d.ts +22 -0
- package/dist/types/src/stories/hooks.stories.d.ts.map +1 -0
- package/dist/types/src/testing/data.d.ts +27 -0
- package/dist/types/src/testing/data.d.ts.map +1 -0
- package/dist/types/src/testing/data.test.d.ts +2 -0
- package/dist/types/src/testing/data.test.d.ts.map +1 -0
- package/dist/types/src/testing/hooks.d.ts +42 -0
- package/dist/types/src/testing/hooks.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +4 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/types/src/testing/model.d.ts +14 -0
- package/dist/types/src/testing/model.d.ts.map +1 -0
- package/dist/types/src/util/d3-types.d.ts +5 -0
- package/dist/types/src/util/d3-types.d.ts.map +1 -0
- package/dist/types/src/util/events.d.ts +10 -0
- package/dist/types/src/util/events.d.ts.map +1 -0
- package/dist/types/src/util/fraction.d.ts +65 -0
- package/dist/types/src/util/fraction.d.ts.map +1 -0
- package/dist/types/src/util/fraction.test.d.ts +2 -0
- package/dist/types/src/util/fraction.test.d.ts.map +1 -0
- package/dist/types/src/util/index.d.ts +9 -0
- package/dist/types/src/util/index.d.ts.map +1 -0
- package/dist/types/src/util/points.d.ts +15 -0
- package/dist/types/src/util/points.d.ts.map +1 -0
- package/dist/types/src/util/scale.d.ts +57 -0
- package/dist/types/src/util/scale.d.ts.map +1 -0
- package/dist/types/src/util/scale.test.d.ts +2 -0
- package/dist/types/src/util/scale.test.d.ts.map +1 -0
- package/dist/types/src/util/screen.d.ts +25 -0
- package/dist/types/src/util/screen.d.ts.map +1 -0
- package/dist/types/src/util/screen.test.d.ts +2 -0
- package/dist/types/src/util/screen.test.d.ts.map +1 -0
- package/dist/types/src/util/types.d.ts +4 -0
- package/dist/types/src/util/types.d.ts.map +1 -0
- package/dist/types/src/util/vector.d.ts +48 -0
- package/dist/types/src/util/vector.d.ts.map +1 -0
- package/dist/types/src/util/vector.test.d.ts +2 -0
- package/dist/types/src/util/vector.test.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +86 -0
- package/src/components/Graph/Graph.stories.tsx +457 -0
- package/src/components/Graph/Graph.tsx +130 -0
- package/src/components/Graph/index.ts +5 -0
- package/src/components/Mesh/Mesh.stories.tsx +62 -0
- package/src/components/Mesh/Mesh.tsx +143 -0
- package/src/components/Mesh/index.ts +5 -0
- package/src/components/SVG/Grid.tsx +20 -0
- package/src/components/SVG/Markers.tsx +25 -0
- package/src/components/SVG/Root.tsx +55 -0
- package/src/components/SVG/SVG.tsx +27 -0
- package/src/components/SVG/Svg.stories.tsx +57 -0
- package/src/components/SVG/Zoom.tsx +27 -0
- package/src/components/SVG/index.ts +5 -0
- package/src/components/index.ts +7 -0
- package/src/fx/fx.stories.tsx +116 -0
- package/src/fx/index.ts +5 -0
- package/src/fx/pulsar.ts +58 -0
- package/src/graph/index.ts +8 -0
- package/src/graph/markers.ts +85 -0
- package/src/graph/projector/graph-drag.ts +124 -0
- package/src/graph/projector/graph-force-projector.ts +414 -0
- package/src/graph/projector/graph-forces.ts +35 -0
- package/src/graph/projector/graph-hierarchical-projector.ts +80 -0
- package/src/graph/projector/graph-projector.ts +99 -0
- package/src/graph/projector/graph-radial-projector.ts +142 -0
- package/src/graph/projector/graph-relational-projector.ts +110 -0
- package/src/graph/projector/index.ts +11 -0
- package/src/graph/projector/projector.ts +72 -0
- package/src/graph/renderer/bullets.ts +72 -0
- package/src/graph/renderer/graph-renderer.ts +484 -0
- package/src/graph/renderer/index.ts +7 -0
- package/src/graph/renderer/linker.ts +42 -0
- package/src/graph/renderer/renderer.ts +49 -0
- package/src/graph/types.ts +80 -0
- package/src/hooks/index.ts +8 -0
- package/src/hooks/useGrid.ts +194 -0
- package/src/hooks/useStateRef.ts +60 -0
- package/src/hooks/useSvgContext.ts +96 -0
- package/src/hooks/useZoom.ts +118 -0
- package/src/index.ts +9 -0
- package/src/stories/hooks.stories.tsx +212 -0
- package/src/testing/data.test.ts +12 -0
- package/src/testing/data.ts +100 -0
- package/src/testing/hooks.ts +103 -0
- package/src/testing/index.ts +7 -0
- package/src/testing/model.ts +29 -0
- package/src/typings.d.ts +5 -0
- package/src/util/d3-types.ts +26 -0
- package/src/util/events.ts +24 -0
- package/src/util/fraction.test.ts +76 -0
- package/src/util/fraction.ts +175 -0
- package/src/util/index.ts +12 -0
- package/src/util/points.ts +68 -0
- package/src/util/scale.test.ts +31 -0
- package/src/util/scale.ts +137 -0
- package/src/util/screen.test.ts +41 -0
- package/src/util/screen.ts +91 -0
- package/src/util/types.ts +9 -0
- package/src/util/vector.test.ts +15 -0
- package/src/util/vector.ts +67 -0
- package/styles/graph.css +142 -0
- package/testing.d.ts +11 -0
- package/testing.js +5 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type ZoomTransform, zoomIdentity } from 'd3';
|
|
6
|
+
|
|
7
|
+
import { FractionUtil } from './fraction';
|
|
8
|
+
import { type Fraction } from './fraction';
|
|
9
|
+
import { type Point, type Rect } from './types';
|
|
10
|
+
import { type Bounds, type Vertex } from './vector';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Scale to map vector space to view (screen) space.
|
|
14
|
+
*/
|
|
15
|
+
export class Scale {
|
|
16
|
+
private _transform?: ZoomTransform;
|
|
17
|
+
|
|
18
|
+
constructor(private readonly _gridSize: number = 32) {}
|
|
19
|
+
|
|
20
|
+
get gridSize() {
|
|
21
|
+
return this._gridSize;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get transform() {
|
|
25
|
+
return this._transform || zoomIdentity;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
setTransform(transform: ZoomTransform): boolean {
|
|
29
|
+
if (
|
|
30
|
+
this._transform?.x === transform.x &&
|
|
31
|
+
this._transform?.y === transform.y &&
|
|
32
|
+
this._transform?.k === transform.k
|
|
33
|
+
) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
this._transform = transform;
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Translate a screen point to a logical point.
|
|
43
|
+
* @param point
|
|
44
|
+
*/
|
|
45
|
+
translate(point: Point): Point {
|
|
46
|
+
const { x, y, k } = this._transform ?? { x: 0, y: 0, k: 1 };
|
|
47
|
+
return [(point[0] - x) / k, (point[1] - y) / k];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Map model to screen.
|
|
52
|
+
* Model points are represented by fractions (to preserve precision) and use normal cartesian coordinates.
|
|
53
|
+
*/
|
|
54
|
+
readonly model = {
|
|
55
|
+
// TODO(burdon): Precision depends on scale.
|
|
56
|
+
snapValues: (array: Fraction[]): Fraction[] => array.map((n) => FractionUtil.round(n)),
|
|
57
|
+
|
|
58
|
+
snapVertex: ({ x, y }: Vertex): Vertex => {
|
|
59
|
+
const [sx, sy] = this.model.snapValues([x, y]);
|
|
60
|
+
return { x: sx, y: sy };
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
snapBounds: ({ x, y, width, height }: { x: Fraction; y: Fraction; width: Fraction; height: Fraction }): Bounds => {
|
|
64
|
+
[x, y, width, height] = this.model.snapValues([x, y, width, height]);
|
|
65
|
+
return { x, y, width, height };
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
toValue: (value: Fraction): number => {
|
|
69
|
+
return Math.round(FractionUtil.toNumber(value) * this._gridSize);
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
toValues: (values: Fraction[]): number[] => {
|
|
73
|
+
return values.map((value) => Math.round(FractionUtil.toNumber(value) * this._gridSize));
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
toPoint: (point: Vertex): Point => {
|
|
77
|
+
const [x, y] = this.model.toValues([point.x, point.y]);
|
|
78
|
+
return [x, y === 0 ? 0 : -y]; // Prevent -0.
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
toBounds: ({ x, y, width, height }: Bounds): Rect => {
|
|
82
|
+
const point = this.model.toPoint({ x, y });
|
|
83
|
+
const [width2, height2] = this.model.toValues([width, height]);
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
x: point[0],
|
|
87
|
+
y: point[1] - height2,
|
|
88
|
+
width: width2,
|
|
89
|
+
height: height2,
|
|
90
|
+
};
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Map screen to model.
|
|
96
|
+
* Screen points are represented by floating numbers and use translated SVG coordinates (with the y-axis inverted).
|
|
97
|
+
*/
|
|
98
|
+
readonly screen = {
|
|
99
|
+
snapValues: (array: number[]): number[] => array.map((n) => Math.round(n / this._gridSize) * this._gridSize),
|
|
100
|
+
|
|
101
|
+
snapPoint: ([x, y]: [number, number]): Point =>
|
|
102
|
+
[x, y].map((n) => Math.round(n / this._gridSize) * this._gridSize) as Point,
|
|
103
|
+
|
|
104
|
+
snapBounds: ({ x, y, width, height }: { x: number; y: number; width: number; height: number }): Rect => {
|
|
105
|
+
[x, y, width, height] = this.screen.snapValues([x, y, width, height]);
|
|
106
|
+
return { x, y, width, height };
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
toValue: (value: number): Fraction => {
|
|
110
|
+
return FractionUtil.divide(FractionUtil.toFraction(value), [this._gridSize, 1]);
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
toValues: (values: number[]): Fraction[] => {
|
|
114
|
+
return values.map((value) => FractionUtil.divide(FractionUtil.toFraction(value), [this._gridSize, 1]));
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
toVertex: (point: Point): Vertex => {
|
|
118
|
+
const [x, y] = point;
|
|
119
|
+
return {
|
|
120
|
+
x: FractionUtil.toFraction(x / this._gridSize),
|
|
121
|
+
y: FractionUtil.toFraction(-y / this._gridSize),
|
|
122
|
+
};
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
toBounds: (bounds: Rect): Bounds => {
|
|
126
|
+
const { x, y } = this.screen.toVertex([bounds.x, bounds.y]);
|
|
127
|
+
const [width, height] = this.screen.toValues([bounds.width, bounds.height]);
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
x,
|
|
131
|
+
y: FractionUtil.subtract(y, height),
|
|
132
|
+
width,
|
|
133
|
+
height,
|
|
134
|
+
};
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { expect, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { Screen } from './screen';
|
|
8
|
+
import { type Rect, type Point } from './types';
|
|
9
|
+
|
|
10
|
+
test('createBounds', () => {
|
|
11
|
+
const tests: { p1: Point; p2: Point; bounds: Rect }[] = [
|
|
12
|
+
{
|
|
13
|
+
p1: [0, 0],
|
|
14
|
+
p2: [0, 0],
|
|
15
|
+
bounds: { x: 0, y: 0, width: 0, height: 0 },
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
p1: [0, 0],
|
|
19
|
+
p2: [5, 5],
|
|
20
|
+
bounds: { x: 0, y: 0, width: 5, height: 5 },
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
p1: [0, 0],
|
|
24
|
+
p2: [-5, -5],
|
|
25
|
+
bounds: { x: -5, y: -5, width: 5, height: 5 },
|
|
26
|
+
},
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
tests.forEach(({ p1, p2, bounds }) => {
|
|
30
|
+
expect(Screen.createBounds(p1, p2)).to.deep.equal(bounds);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('contains', () => {
|
|
35
|
+
const bounds = Screen.createBounds([0, 0], [5, 5]);
|
|
36
|
+
|
|
37
|
+
expect(Screen.contains(bounds, [0, 0])).to.be.true;
|
|
38
|
+
expect(Screen.contains(bounds, [2, 1])).to.be.true;
|
|
39
|
+
expect(Screen.contains(bounds, [5, 5])).to.be.true;
|
|
40
|
+
expect(Screen.contains(bounds, [0, 6])).to.be.false;
|
|
41
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Rect, type Point } from './types';
|
|
6
|
+
|
|
7
|
+
export type Modifiers = {
|
|
8
|
+
center?: boolean;
|
|
9
|
+
constrain?: boolean;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export class Screen {
|
|
13
|
+
static createBounds = ([x1, y1]: Point, [x2, y2]: Point, mod: Modifiers = {}): Rect => {
|
|
14
|
+
const { constrain, center } = mod;
|
|
15
|
+
|
|
16
|
+
const x = Math.min(x1, x2);
|
|
17
|
+
const y = Math.min(y1, y2);
|
|
18
|
+
|
|
19
|
+
let width = Math.abs(x2 - x1);
|
|
20
|
+
let height = Math.abs(y2 - y1);
|
|
21
|
+
if (constrain) {
|
|
22
|
+
width = height = Math.max(width, height);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (center) {
|
|
26
|
+
return {
|
|
27
|
+
x: x - width,
|
|
28
|
+
y: y - height,
|
|
29
|
+
width: width * 2,
|
|
30
|
+
height: height * 2,
|
|
31
|
+
};
|
|
32
|
+
} else {
|
|
33
|
+
return {
|
|
34
|
+
x,
|
|
35
|
+
y,
|
|
36
|
+
width,
|
|
37
|
+
height,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
static center = ({ x, y, width, height }: Rect): Point => {
|
|
43
|
+
return [x + width / 2, y + height / 2];
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
static contains = (bounds: Rect, point: Point): boolean => {
|
|
47
|
+
const [x, y] = point;
|
|
48
|
+
|
|
49
|
+
if (x < bounds.x || y < bounds.y) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (x > bounds.x + bounds.width || y > bounds.y + bounds.height) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return true;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Distance between two points.
|
|
62
|
+
* @param p1
|
|
63
|
+
* @param p2
|
|
64
|
+
*/
|
|
65
|
+
static len = (p1: Point, p2: Point) => {
|
|
66
|
+
return Math.sqrt(Math.pow(Math.abs(p1[0] - p2[0]), 2) + Math.pow(Math.abs(p1[1] - p2[1]), 2));
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Distance to line segment.
|
|
71
|
+
* https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment#1501725
|
|
72
|
+
* @param v1
|
|
73
|
+
* @param v2
|
|
74
|
+
* @param p
|
|
75
|
+
*/
|
|
76
|
+
static normal = (v1: Point, v2: Point, p: Point) => {
|
|
77
|
+
const d2 = (p1: Point, p2: Point) => Math.pow(Math.abs(p1[0] - p2[0]), 2) + Math.pow(Math.abs(p1[1] - p2[1]), 2);
|
|
78
|
+
|
|
79
|
+
let d: number;
|
|
80
|
+
const len2 = d2(v1, v2);
|
|
81
|
+
if (len2 === 0) {
|
|
82
|
+
d = d2(p, v1);
|
|
83
|
+
} else {
|
|
84
|
+
let t = ((p[0] - v1[0]) * (v2[0] - v1[0]) + (p[1] - v1[1]) * (v2[1] - v1[1])) / len2;
|
|
85
|
+
t = Math.max(0, Math.min(1, t));
|
|
86
|
+
d = d2(p, [v1[0] + t * (v2[0] - v1[0]), v1[1] + t * (v2[1] - v1[1])]);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return Math.sqrt(d);
|
|
90
|
+
};
|
|
91
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { expect, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { FractionUtil } from './fraction';
|
|
8
|
+
import { Vector } from './vector';
|
|
9
|
+
|
|
10
|
+
test('center', () => {
|
|
11
|
+
expect(Vector.center(Vector.toBounds({ x: 0, y: 0, width: 2, height: 2 }))).to.deep.equal({
|
|
12
|
+
x: FractionUtil.toFraction(1),
|
|
13
|
+
y: FractionUtil.toFraction(1),
|
|
14
|
+
});
|
|
15
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Num, type Fraction, FractionUtil } from './fraction';
|
|
6
|
+
|
|
7
|
+
export type Vertex = { x: Fraction; y: Fraction };
|
|
8
|
+
|
|
9
|
+
export type Bounds = {
|
|
10
|
+
x: Fraction;
|
|
11
|
+
y: Fraction;
|
|
12
|
+
width: Fraction;
|
|
13
|
+
height: Fraction;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export class Vector {
|
|
17
|
+
/**
|
|
18
|
+
* Convert to Vertex.
|
|
19
|
+
* @param x
|
|
20
|
+
* @param y
|
|
21
|
+
*/
|
|
22
|
+
static toVertex = ({ x, y }: { x: Num; y: Num }): Vertex => ({
|
|
23
|
+
x: FractionUtil.toFraction(x),
|
|
24
|
+
y: FractionUtil.toFraction(y),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Convert to Bounds.
|
|
29
|
+
* @param bounds
|
|
30
|
+
*/
|
|
31
|
+
static toBounds = ({ x, y, width, height }: { x: Num; y: Num; width: Num; height: Num }): Bounds => ({
|
|
32
|
+
x: FractionUtil.toFraction(x),
|
|
33
|
+
y: FractionUtil.toFraction(y),
|
|
34
|
+
width: FractionUtil.toFraction(width),
|
|
35
|
+
height: FractionUtil.toFraction(height),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Calculate center point.
|
|
40
|
+
* @param bounds
|
|
41
|
+
*/
|
|
42
|
+
static center = ({ x, y, width, height }: Bounds): Vertex => {
|
|
43
|
+
const cx = FractionUtil.add(x, FractionUtil.multiply(width, [1, 2]));
|
|
44
|
+
const cy = FractionUtil.add(y, FractionUtil.multiply(height, [1, 2]));
|
|
45
|
+
return { x: cx, y: cy };
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Calculate centered left edge.
|
|
50
|
+
* @param bounds
|
|
51
|
+
*/
|
|
52
|
+
static left = ({ x, y, height }: Bounds): Vertex => {
|
|
53
|
+
const cx = x;
|
|
54
|
+
const cy = FractionUtil.add(y, FractionUtil.multiply(height, [1, 2]));
|
|
55
|
+
return { x: cx, y: cy };
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Calculate centered right edge.
|
|
60
|
+
* @param bounds
|
|
61
|
+
*/
|
|
62
|
+
static right = ({ x, y, width, height }: Bounds): Vertex => {
|
|
63
|
+
const cx = FractionUtil.add(x, width);
|
|
64
|
+
const cy = FractionUtil.add(y, FractionUtil.multiply(height, [1, 2]));
|
|
65
|
+
return { x: cx, y: cy };
|
|
66
|
+
};
|
|
67
|
+
}
|
package/styles/graph.css
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Graph styles.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/* TODO(burdon): Implement light/dark theme or use semantic tokens. */
|
|
6
|
+
|
|
7
|
+
svg {
|
|
8
|
+
marker {
|
|
9
|
+
&.dx-arrow path {
|
|
10
|
+
@apply fill-none;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
&.dx-dot circle {
|
|
14
|
+
@apply fill-none;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
g.dx-grid {
|
|
19
|
+
path.dx-axis {
|
|
20
|
+
@apply stroke-neutral-200 dark:stroke-neutral-700;
|
|
21
|
+
}
|
|
22
|
+
path.dx-major {
|
|
23
|
+
@apply stroke-neutral-100 dark:stroke-neutral-800;
|
|
24
|
+
}
|
|
25
|
+
path.dx-minor {
|
|
26
|
+
@apply stroke-neutral-100 dark:stroke-neutral-850;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
g.dx-graph {
|
|
31
|
+
g.dx-guides {
|
|
32
|
+
circle {
|
|
33
|
+
@apply fill-neutral-300 stroke-neutral-500 opacity-10 dark:fill-neutral-700 dark:stroke-neutral-900;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
g.dx-subgraphs {
|
|
38
|
+
path.dx-subgraph {
|
|
39
|
+
@apply stroke-red-500 fill-neutral-100 dark:fill-neutral-900 [stroke-dasharray:4_4];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
g.dx-nodes {
|
|
44
|
+
g.dx-node {
|
|
45
|
+
circle {
|
|
46
|
+
@apply fill-neutral-200 stroke-neutral-700 dark:fill-neutral-500 dark:stroke-none;
|
|
47
|
+
}
|
|
48
|
+
&.dx-selected {
|
|
49
|
+
circle {
|
|
50
|
+
@apply !fill-red-500 stroke-2 stroke-neutral-900 dark:stroke-neutral-200;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
g.dx-label {
|
|
55
|
+
@apply hidden;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
g[data-color="orange" ].dx-node { circle { @apply fill-orange-300 dark:fill-orange-500 } }
|
|
60
|
+
g[data-color="amber" ].dx-node { circle { @apply fill-amber-300 dark:fill-amber-500 } }
|
|
61
|
+
g[data-color="yellow" ].dx-node { circle { @apply fill-yellow-300 dark:fill-yellow-500 } }
|
|
62
|
+
g[data-color="lime" ].dx-node { circle { @apply fill-lime-300 dark:fill-lime-500 } }
|
|
63
|
+
g[data-color="green" ].dx-node { circle { @apply fill-green-300 dark:fill-green-500 } }
|
|
64
|
+
g[data-color="emerald"].dx-node { circle { @apply fill-emerald-300 dark:fill-emerald-500 } }
|
|
65
|
+
g[data-color="teal" ].dx-node { circle { @apply fill-teal-300 dark:fill-teal-500 } }
|
|
66
|
+
g[data-color="cyan" ].dx-node { circle { @apply fill-cyan-300 dark:fill-cyan-500 } }
|
|
67
|
+
g[data-color="sky" ].dx-node { circle { @apply fill-sky-300 dark:fill-sky-500 } }
|
|
68
|
+
g[data-color="indigo" ].dx-node { circle { @apply fill-indigo-300 dark:fill-indigo-500 } }
|
|
69
|
+
g[data-color="violet" ].dx-node { circle { @apply fill-violet-300 dark:fill-violet-500 } }
|
|
70
|
+
g[data-color="purple" ].dx-node { circle { @apply fill-purple-300 dark:fill-purple-500 } }
|
|
71
|
+
g[data-color="fuchsia"].dx-node { circle { @apply fill-fuchsia-300 dark:fill-fuchsia-500 } }
|
|
72
|
+
g[data-color="rose" ].dx-node { circle { @apply fill-rose-300 dark:fill-rose-500 } }
|
|
73
|
+
g[data-color="pink" ].dx-node { circle { @apply fill-pink-300 dark:fill-pink-500 } }
|
|
74
|
+
|
|
75
|
+
g.dx-dragging,
|
|
76
|
+
g.dx-highlight {
|
|
77
|
+
circle {
|
|
78
|
+
@apply stroke-2 stroke-neutral-900 dark:stroke-neutral-200 cursor-pointer;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
g.dx-label {
|
|
82
|
+
@apply block;
|
|
83
|
+
|
|
84
|
+
line {
|
|
85
|
+
@apply stroke-2 stroke-neutral-500;
|
|
86
|
+
}
|
|
87
|
+
rect {
|
|
88
|
+
@apply stroke-1 stroke-neutral-500 fill-inverseSurface;
|
|
89
|
+
}
|
|
90
|
+
text {
|
|
91
|
+
@apply fill-inverseSurfaceText;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
g.dx-edges {
|
|
98
|
+
g.dx-edge {
|
|
99
|
+
path {
|
|
100
|
+
/** NOTE: Set text- so that current color is picked up by marker. */
|
|
101
|
+
@apply stroke-1 stroke-neutral-700 dark:stroke-neutral-700;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
&.dx-dashed {
|
|
105
|
+
path {
|
|
106
|
+
@apply [stroke-dasharray:4_4];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
g[data-color="orange" ].dx-edge { path { @apply stroke-orange-300 dark:stroke-orange-500 } }
|
|
112
|
+
g[data-color="amber" ].dx-edge { path { @apply stroke-amber-300 dark:stroke-amber-500 } }
|
|
113
|
+
g[data-color="yellow" ].dx-edge { path { @apply stroke-yellow-300 dark:stroke-yellow-500 } }
|
|
114
|
+
g[data-color="lime" ].dx-edge { path { @apply stroke-lime-300 dark:stroke-lime-500 } }
|
|
115
|
+
g[data-color="green" ].dx-edge { path { @apply stroke-green-300 dark:stroke-green-500 } }
|
|
116
|
+
g[data-color="emerald"].dx-edge { path { @apply stroke-emerald-300 dark:stroke-emerald-500 } }
|
|
117
|
+
g[data-color="teal" ].dx-edge { path { @apply stroke-teal-300 dark:stroke-teal-500 } }
|
|
118
|
+
g[data-color="cyan" ].dx-edge { path { @apply stroke-cyan-300 dark:stroke-cyan-500 } }
|
|
119
|
+
g[data-color="sky" ].dx-edge { path { @apply stroke-sky-300 dark:stroke-sky-500 } }
|
|
120
|
+
g[data-color="indigo" ].dx-edge { path { @apply stroke-indigo-300 dark:stroke-indigo-500 } }
|
|
121
|
+
g[data-color="violet" ].dx-edge { path { @apply stroke-violet-300 dark:stroke-violet-500 } }
|
|
122
|
+
g[data-color="purple" ].dx-edge { path { @apply stroke-purple-300 dark:stroke-purple-500 } }
|
|
123
|
+
g[data-color="fuchsia"].dx-edge { path { @apply stroke-fuchsia-300 dark:stroke-fuchsia-500 } }
|
|
124
|
+
g[data-color="rose" ].dx-edge { path { @apply stroke-rose-300 dark:stroke-rose-500 } }
|
|
125
|
+
g[data-color="pink" ].dx-edge { path { @apply stroke-pink-300 dark:stroke-pink-500 } }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
.dx-linker {
|
|
129
|
+
path {
|
|
130
|
+
@apply stroke-orange-500 [stroke-dasharray:4_4];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
path::marker {
|
|
134
|
+
@apply stroke-red-500;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
circle.dx-bullet {
|
|
139
|
+
@apply fill-red-500 stroke-none;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
package/testing.d.ts
ADDED