@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,100 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type BaseGraphEdge, type Graph } from '@dxos/graph';
|
|
6
|
+
import { faker } from '@dxos/random';
|
|
7
|
+
|
|
8
|
+
import { type TestNode } from './model';
|
|
9
|
+
|
|
10
|
+
// https://www.npmjs.com/package/faker#setting-a-randomness-seed
|
|
11
|
+
export const seed = (seed: number) => faker.seed(seed);
|
|
12
|
+
|
|
13
|
+
//
|
|
14
|
+
// Create data.
|
|
15
|
+
//
|
|
16
|
+
|
|
17
|
+
export const createNode = (type: string = undefined): TestNode => ({
|
|
18
|
+
id: faker.string.uuid(),
|
|
19
|
+
type,
|
|
20
|
+
label: faker.lorem.words(3).replace(/ /g, '-'),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
export const createNodes = (n = 0, types?: string[]): TestNode[] =>
|
|
24
|
+
Array.from({ length: n }, () => createNode(types ? faker.helpers.arrayElement(types) : undefined));
|
|
25
|
+
|
|
26
|
+
export const createEdge = (source: TestNode, target: TestNode): BaseGraphEdge => ({
|
|
27
|
+
id: `${source.id}-${target.id}`,
|
|
28
|
+
source: source.id,
|
|
29
|
+
target: target.id,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Creates a random tree.
|
|
34
|
+
* @param depth Depth of tree.
|
|
35
|
+
* @param children Branching factor.
|
|
36
|
+
*/
|
|
37
|
+
export const createTree = ({ depth = 2, children = 3 } = {}): TestNode => {
|
|
38
|
+
const createChildren = (root: TestNode, d = 0) => {
|
|
39
|
+
if (d < depth) {
|
|
40
|
+
const max = Math.round(Math.log(depth + 1 - d) * children);
|
|
41
|
+
const num = faker.number.int({ min: 1, max });
|
|
42
|
+
root.children = [...new Array(num)].map(() => {
|
|
43
|
+
return createChildren(createNode(), d + 1);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return root;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
return createChildren(createNode());
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Converts a tree into a graph.
|
|
55
|
+
* @param root
|
|
56
|
+
*/
|
|
57
|
+
export const convertTreeToGraph = (root: TestNode): Graph => {
|
|
58
|
+
const traverse = (node: TestNode, graph: Graph) => {
|
|
59
|
+
graph.nodes.push(node);
|
|
60
|
+
node.children?.forEach((child) => {
|
|
61
|
+
graph.edges.push(createEdge(node, child));
|
|
62
|
+
traverse(child, graph);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return graph;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
return traverse(root, {
|
|
69
|
+
nodes: [],
|
|
70
|
+
edges: [],
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Creates a random graph.
|
|
76
|
+
* @param numNodes
|
|
77
|
+
* @param numEdges
|
|
78
|
+
*/
|
|
79
|
+
export const createGraph = (numNodes = 0, numEdges = 0, types?: string[]): Graph => {
|
|
80
|
+
const nodes = createNodes(numNodes, types);
|
|
81
|
+
|
|
82
|
+
const edges = new Map();
|
|
83
|
+
if (numEdges && nodes.length >= 2) {
|
|
84
|
+
for (let i = 0; i < numEdges; i++) {
|
|
85
|
+
const source = faker.helpers.arrayElement(nodes);
|
|
86
|
+
const target = faker.helpers.arrayElement(nodes);
|
|
87
|
+
if (source.id !== target.id) {
|
|
88
|
+
const edge = createEdge(source, target);
|
|
89
|
+
if (!edges.get(edge.id)) {
|
|
90
|
+
edges.set(edge.id, edge);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
nodes,
|
|
98
|
+
edges: Array.from(edges.values()),
|
|
99
|
+
};
|
|
100
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import update from 'immutability-helper';
|
|
6
|
+
|
|
7
|
+
import { type Graph } from '@dxos/graph';
|
|
8
|
+
import { faker } from '@dxos/random';
|
|
9
|
+
|
|
10
|
+
import { createNode, createEdge } from './data';
|
|
11
|
+
import { type TestNode } from './model';
|
|
12
|
+
import { useStateRef } from '../hooks';
|
|
13
|
+
|
|
14
|
+
export type ObjectMutator<T> = [
|
|
15
|
+
T, // Current value.
|
|
16
|
+
(data: T) => void, // Set value.
|
|
17
|
+
(mutation: any) => T, // Apply update mutation.
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Returns a state object and setter, with addition live getter and updater (for the current up-to-date reference).
|
|
22
|
+
* https://reactjs.org/docs/hooks-faq.html#why-am-i-seeing-stale-props-or-state-inside-my-function
|
|
23
|
+
*/
|
|
24
|
+
export const useObjectMutator = <T>(initalValue: T): ObjectMutator<T> => {
|
|
25
|
+
const [data, setData, dataRef] = useStateRef<T>(initalValue);
|
|
26
|
+
|
|
27
|
+
return [
|
|
28
|
+
// Get data state (snapshot).
|
|
29
|
+
data,
|
|
30
|
+
|
|
31
|
+
// Update data state.
|
|
32
|
+
(data: T) => {
|
|
33
|
+
setData(data);
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
// Updater.
|
|
37
|
+
// https://github.com/kolodny/immutability-helper
|
|
38
|
+
// NOTE: Use $apply to update variable (e.g., push to potentially null object).
|
|
39
|
+
(mutation: any): T => {
|
|
40
|
+
setData((current) => update<T>(current, mutation));
|
|
41
|
+
return dataRef.current;
|
|
42
|
+
},
|
|
43
|
+
];
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Test data set generator and mutator.
|
|
48
|
+
*/
|
|
49
|
+
export const useGraphGenerator = (options: { data?: Graph } = {}) => {
|
|
50
|
+
const [data, setData, updateData] = useObjectMutator(options.data || { nodes: [], edges: [] });
|
|
51
|
+
|
|
52
|
+
let interval;
|
|
53
|
+
|
|
54
|
+
const mutator = () => {
|
|
55
|
+
const parent = data.nodes.length ? faker.helpers.arrayElement(data.nodes) : undefined;
|
|
56
|
+
const node = createNode();
|
|
57
|
+
|
|
58
|
+
updateData({
|
|
59
|
+
nodes: {
|
|
60
|
+
$push: [node],
|
|
61
|
+
},
|
|
62
|
+
edges: Object.assign(
|
|
63
|
+
{},
|
|
64
|
+
parent && {
|
|
65
|
+
$push: [createEdge(parent as TestNode, node)],
|
|
66
|
+
},
|
|
67
|
+
),
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const reset = (options = { count: 0 }) => {
|
|
72
|
+
const { count = 0 } = options;
|
|
73
|
+
setData({
|
|
74
|
+
nodes: [],
|
|
75
|
+
edges: [],
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
for (let i = 0; i < count; i++) {
|
|
79
|
+
mutator();
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const start = ({ delay = 250 } = {}) => {
|
|
84
|
+
stop();
|
|
85
|
+
interval = setInterval(mutator, delay);
|
|
86
|
+
return stop;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const stop = () => {
|
|
90
|
+
if (interval) {
|
|
91
|
+
clearInterval(interval);
|
|
92
|
+
interval = undefined;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
data,
|
|
98
|
+
mutator,
|
|
99
|
+
reset,
|
|
100
|
+
start,
|
|
101
|
+
stop,
|
|
102
|
+
};
|
|
103
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2022 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type BaseGraphNode, ReactiveGraphModel } from '@dxos/graph';
|
|
6
|
+
import { faker } from '@dxos/random';
|
|
7
|
+
|
|
8
|
+
import { createEdge, createNode } from './data';
|
|
9
|
+
|
|
10
|
+
export type TestNode = BaseGraphNode & {
|
|
11
|
+
label?: string;
|
|
12
|
+
children?: TestNode[];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export class TestGraphModel extends ReactiveGraphModel<TestNode> {
|
|
16
|
+
getRandomNode() {
|
|
17
|
+
return faker.helpers.arrayElement(this._graph.nodes);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
createNodes(node: TestNode = undefined, n = 1): void {
|
|
21
|
+
Array.from({ length: n }).forEach(() => {
|
|
22
|
+
const child = this.addNode(createNode());
|
|
23
|
+
const parent = node || faker.helpers.arrayElement(this._graph.nodes);
|
|
24
|
+
if (parent) {
|
|
25
|
+
this.addEdge(createEdge(parent, child));
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/typings.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type { BaseType, D3DragEvent as DragEvent, DraggedElementBaseType, Selection } from 'd3';
|
|
6
|
+
|
|
7
|
+
// Extends MouseEvent (e.g., subject).
|
|
8
|
+
export type D3DragEvent<GElement extends DraggedElementBaseType = any, Datum = any, Subject = any> = DragEvent<
|
|
9
|
+
GElement,
|
|
10
|
+
Datum,
|
|
11
|
+
Subject
|
|
12
|
+
>;
|
|
13
|
+
|
|
14
|
+
// const selection: D3Selection<E, T> = d3.select<E, T>()
|
|
15
|
+
export type D3Selection<
|
|
16
|
+
GElement extends BaseType = any,
|
|
17
|
+
Datum = any,
|
|
18
|
+
PElement extends BaseType = any,
|
|
19
|
+
PDatum = any,
|
|
20
|
+
> = Selection<GElement, Datum, PElement, PDatum>;
|
|
21
|
+
|
|
22
|
+
// d3.select<E, T>().call(callable: D3Callable<E, T>)
|
|
23
|
+
export type D3Callable<GElement extends BaseType = any, Datum = any, PElement extends BaseType = any, PDatum = any> = (
|
|
24
|
+
selection: D3Selection<GElement, Datum, PElement, PDatum>,
|
|
25
|
+
...args: any[]
|
|
26
|
+
) => void;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2021 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
type EventListener<T> = (value: T) => void;
|
|
6
|
+
|
|
7
|
+
export type EventHandle = () => void;
|
|
8
|
+
|
|
9
|
+
export class EventEmitter<T> {
|
|
10
|
+
private readonly _listeners = new Set<EventListener<T>>();
|
|
11
|
+
|
|
12
|
+
clear(): void {
|
|
13
|
+
this._listeners.clear();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
on(onEvent: EventListener<T>): EventHandle {
|
|
17
|
+
this._listeners.add(onEvent);
|
|
18
|
+
return () => this._listeners.delete(onEvent);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public emit(value: T): void {
|
|
22
|
+
this._listeners.forEach((listener) => listener(value));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { expect, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { FractionUtil } from './fraction';
|
|
8
|
+
|
|
9
|
+
test('invalid', () => {
|
|
10
|
+
expect(() => FractionUtil.validate([1, 0])).to.throw();
|
|
11
|
+
expect(() => FractionUtil.validate([NaN, 1])).to.throw();
|
|
12
|
+
expect(() => FractionUtil.validate([Infinity, 1])).to.throw();
|
|
13
|
+
expect(() => FractionUtil.validate([1, NaN])).to.throw();
|
|
14
|
+
expect(() => FractionUtil.validate([1, Infinity])).to.throw();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('toNumber', () => {
|
|
18
|
+
expect(FractionUtil.toNumber([9, 3])).to.equal(3);
|
|
19
|
+
expect(FractionUtil.toNumber([Math.PI * 3, 6])).to.equal(Math.PI / 2);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('round', () => {
|
|
23
|
+
expect(FractionUtil.round([0, 1])).to.deep.equal([0, 1]);
|
|
24
|
+
expect(FractionUtil.round([1, 3])).to.deep.equal([0, 1]);
|
|
25
|
+
expect(FractionUtil.round([2, 3])).to.deep.equal([1, 1]);
|
|
26
|
+
expect(FractionUtil.round([3, 4], 4)).to.deep.equal([3, 4]);
|
|
27
|
+
expect(FractionUtil.round([7, 5], 4)).to.deep.equal([3, 2]); // 7/5 = 1.4 => 1.5 => 3/2
|
|
28
|
+
expect(FractionUtil.round([1.75, 1], 4)).to.deep.equal([7, 4]);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('factors', () => {
|
|
32
|
+
expect(FractionUtil.factors(15)).to.deep.equal([1, 3, 5, 15]);
|
|
33
|
+
expect(FractionUtil.factors(16)).to.deep.equal([1, 2, 4, 8, 16]);
|
|
34
|
+
expect(FractionUtil.factors(20)).to.deep.equal([1, 2, 4, 5, 10, 20]);
|
|
35
|
+
expect(FractionUtil.factors(8.5)).to.deep.equal([1]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('equals', () => {
|
|
39
|
+
expect(FractionUtil.equals([0, 1], [0, 1])).to.be.true;
|
|
40
|
+
expect(FractionUtil.equals([1, 3], [1, 3])).to.be.true;
|
|
41
|
+
expect(FractionUtil.equals([2, 5], [4, 10])).to.be.true;
|
|
42
|
+
expect(FractionUtil.equals([1, 3], [2, 3])).to.be.false;
|
|
43
|
+
expect(FractionUtil.equals([0, 1], [1, 1])).to.be.false;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('simplify', () => {
|
|
47
|
+
expect(FractionUtil.simplify([0, 1])).to.deep.equal([0, 1]);
|
|
48
|
+
expect(FractionUtil.simplify([3, 2])).to.deep.equal([3, 2]);
|
|
49
|
+
expect(FractionUtil.simplify([8, 2])).to.deep.equal([4, 1]);
|
|
50
|
+
expect(FractionUtil.simplify([9, 3])).to.deep.equal([3, 1]);
|
|
51
|
+
expect(FractionUtil.simplify([4, 6])).to.deep.equal([2, 3]);
|
|
52
|
+
expect(FractionUtil.simplify([8, 0.5])).to.deep.equal([16, 1]);
|
|
53
|
+
expect(FractionUtil.simplify([8, 0.2])).to.deep.equal([40, 1]);
|
|
54
|
+
expect(FractionUtil.simplify([6.3, 0.3])).to.deep.equal([21, 1]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('add', () => {
|
|
58
|
+
expect(FractionUtil.add([1, 1], [0, 1])).to.deep.equal([1, 1]);
|
|
59
|
+
expect(FractionUtil.add([4, 2], [8, 3])).to.deep.equal([14, 3]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('subtract', () => {
|
|
63
|
+
expect(FractionUtil.subtract([1, 1], [0, 1])).to.deep.equal([1, 1]);
|
|
64
|
+
expect(FractionUtil.subtract([4, 2], [8, 3])).to.deep.equal([-2, 3]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('multiply', () => {
|
|
68
|
+
expect(FractionUtil.multiply([1, 1], [0, 1])).to.deep.equal([0, 1]);
|
|
69
|
+
expect(FractionUtil.multiply([4, 2], [8, 3])).to.deep.equal([16, 3]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('divide', () => {
|
|
73
|
+
expect(FractionUtil.divide([1, 1], [1, 1])).to.deep.equal([1, 1]);
|
|
74
|
+
expect(FractionUtil.divide([4, 2], [8, 3])).to.deep.equal([3, 4]);
|
|
75
|
+
expect(() => FractionUtil.divide([4, 2], [8, 0])).to.throw();
|
|
76
|
+
});
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { invariant } from '@dxos/invariant';
|
|
6
|
+
|
|
7
|
+
// TODO(burdon): Protobuf mapping?
|
|
8
|
+
export type Fraction = [num: number, denum: number];
|
|
9
|
+
|
|
10
|
+
export type Num = number | Fraction;
|
|
11
|
+
|
|
12
|
+
export class FractionUtil {
|
|
13
|
+
/**
|
|
14
|
+
* Convert number to fraction.
|
|
15
|
+
* @param n
|
|
16
|
+
*/
|
|
17
|
+
static toFraction = (n: Num): Fraction => {
|
|
18
|
+
return typeof n === 'number' ? [n, 1] : n;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
static validate = (...fraction: Fraction[]): Fraction[] => {
|
|
22
|
+
fraction.forEach(([n, d]) => {
|
|
23
|
+
if (!Number.isFinite(n) || !Number.isFinite(d) || d === 0) {
|
|
24
|
+
throw new Error(`Invalid fraction: ${n}/${d}`);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return fraction;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {Fraction}
|
|
33
|
+
* Convert to rounded number.
|
|
34
|
+
*/
|
|
35
|
+
static toNumber = ([n, d]: Fraction): number => {
|
|
36
|
+
FractionUtil.validate([n, d]);
|
|
37
|
+
return n / d;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Round the number to the nearest fraction.
|
|
42
|
+
* Example: 3/5 => 1/1; 3/5 (precision 2) => 1/2.
|
|
43
|
+
* @param {Fraction}
|
|
44
|
+
* @param p Precision (e.g., 1/2, 1/4, etc.)
|
|
45
|
+
*/
|
|
46
|
+
static round = ([n, d]: Fraction, p = 1): Fraction => {
|
|
47
|
+
FractionUtil.validate([n, d]);
|
|
48
|
+
const v = Math.round(FractionUtil.toNumber(FractionUtil.divide([n, d], [1, p])));
|
|
49
|
+
return FractionUtil.simplify([v, p]);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
//
|
|
53
|
+
// Boolean.
|
|
54
|
+
//
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Tests if equals.
|
|
58
|
+
* @param n1
|
|
59
|
+
* @param n2
|
|
60
|
+
*/
|
|
61
|
+
static equals = (n1: Fraction, n2: Fraction) => {
|
|
62
|
+
return n1[0] * n2[1] === n1[1] * n2[0];
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {Fraction}
|
|
67
|
+
* True if zero.
|
|
68
|
+
*/
|
|
69
|
+
static isZero = ([n]: Fraction): boolean => {
|
|
70
|
+
return n === 0;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
//
|
|
74
|
+
// Normalization.
|
|
75
|
+
//
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Simplify fraction finding LCDs.
|
|
79
|
+
* @param n
|
|
80
|
+
* @param d
|
|
81
|
+
*/
|
|
82
|
+
static simplify = ([n, d]: Fraction): Fraction => {
|
|
83
|
+
FractionUtil.validate([n, d]);
|
|
84
|
+
if (d < 1) {
|
|
85
|
+
const t = n / d;
|
|
86
|
+
if (Math.floor(t) === t) {
|
|
87
|
+
n = t;
|
|
88
|
+
d = 1;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Find highest common denominator.
|
|
93
|
+
const fn = FractionUtil.factors(n).reverse();
|
|
94
|
+
const fd = FractionUtil.factors(d).reverse();
|
|
95
|
+
|
|
96
|
+
let c;
|
|
97
|
+
for (let i = 0; i < fn.length; i++) {
|
|
98
|
+
c = fd.find((n) => n === fn[i]);
|
|
99
|
+
if (c) {
|
|
100
|
+
return [n / c, d / c];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// TODO(burdon): Convert to number if d == 1.
|
|
105
|
+
return [n, d];
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Return common factors.
|
|
110
|
+
* @param n
|
|
111
|
+
*/
|
|
112
|
+
static factors = (n: number): number[] => {
|
|
113
|
+
invariant(Number.isFinite(n), `Invalid number: ${n}`);
|
|
114
|
+
|
|
115
|
+
const factors = [1];
|
|
116
|
+
if (Number.isSafeInteger(n)) {
|
|
117
|
+
let f = 1;
|
|
118
|
+
do {
|
|
119
|
+
if (n % ++f === 0) {
|
|
120
|
+
factors.push(f);
|
|
121
|
+
}
|
|
122
|
+
} while (n / f > 1 && f < 100);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return factors;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
//
|
|
129
|
+
// Basic arithmetic.
|
|
130
|
+
//
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @param n1
|
|
134
|
+
* @param n2
|
|
135
|
+
*/
|
|
136
|
+
static add = (n1: Fraction, n2: Fraction): Fraction => {
|
|
137
|
+
FractionUtil.validate(n1, n2);
|
|
138
|
+
const d = n1[1] * n2[1]; // Same denom.
|
|
139
|
+
const n = n1[0] * n2[1] + n2[0] * n1[1];
|
|
140
|
+
return FractionUtil.simplify([n, d]);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @param n1
|
|
145
|
+
* @param n2
|
|
146
|
+
*/
|
|
147
|
+
static subtract = (n1: Fraction, n2: Fraction): Fraction => {
|
|
148
|
+
FractionUtil.validate(n1, n2);
|
|
149
|
+
const d = n1[1] * n2[1]; // Same denom.
|
|
150
|
+
const n = n1[0] * n2[1] - n2[0] * n1[1];
|
|
151
|
+
return FractionUtil.simplify([n, d]);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* @param n1
|
|
156
|
+
* @param n2
|
|
157
|
+
*/
|
|
158
|
+
static multiply = (n1: Fraction, n2: Fraction): Fraction => {
|
|
159
|
+
FractionUtil.validate(n1, n2);
|
|
160
|
+
const n = n1[0] * n2[0];
|
|
161
|
+
const d = n1[1] * n2[1];
|
|
162
|
+
return FractionUtil.simplify([n, d]);
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* @param n1
|
|
167
|
+
* @param n2
|
|
168
|
+
*/
|
|
169
|
+
static divide = (n1: Fraction, n2: Fraction): Fraction => {
|
|
170
|
+
FractionUtil.validate(n1, n2);
|
|
171
|
+
const n = n1[0] * n2[1];
|
|
172
|
+
const d = n1[1] * n2[0];
|
|
173
|
+
return FractionUtil.simplify([n, d]);
|
|
174
|
+
};
|
|
175
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2021 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
export * from './d3-types';
|
|
6
|
+
export * from './events';
|
|
7
|
+
export * from './fraction';
|
|
8
|
+
export * from './points';
|
|
9
|
+
export * from './scale';
|
|
10
|
+
export * from './screen';
|
|
11
|
+
export * from './types';
|
|
12
|
+
export * from './vector';
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2022 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Point } from './types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Creates an array of points on the the circumference of two circles.
|
|
9
|
+
* http://www.math.com/tables/geometry/circles.htm
|
|
10
|
+
*
|
|
11
|
+
* @param p1
|
|
12
|
+
* @param p2
|
|
13
|
+
* @param r1
|
|
14
|
+
* @param r2
|
|
15
|
+
* @return {[Point, Point]}
|
|
16
|
+
*/
|
|
17
|
+
export const getCircumferencePoints = (p1: Point, p2: Point, r1 = 0, r2 = 0): [Point, Point] => {
|
|
18
|
+
const [x1, y1] = p1;
|
|
19
|
+
const [x2, y2] = p2;
|
|
20
|
+
|
|
21
|
+
const theta = Math.atan2(x2 - x1, y2 - y1);
|
|
22
|
+
|
|
23
|
+
return [
|
|
24
|
+
[
|
|
25
|
+
// p1
|
|
26
|
+
x1 + (r1 === 0 ? 0 : r1 * Math.sin(theta)),
|
|
27
|
+
y1 + (r1 === 0 ? 0 : r1 * Math.cos(theta)),
|
|
28
|
+
],
|
|
29
|
+
[
|
|
30
|
+
// p2
|
|
31
|
+
x2 - (r2 === 0 ? 0 : r2 * Math.cos(Math.PI / 2 - theta)),
|
|
32
|
+
y2 - (r2 === 0 ? 0 : r2 * Math.sin(Math.PI / 2 - theta)),
|
|
33
|
+
],
|
|
34
|
+
];
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Compute unit normal for a line segment.
|
|
38
|
+
export const normal = (p1: Point, p2: Point): Point => {
|
|
39
|
+
const dx = p2[0] - p1[0];
|
|
40
|
+
const dy = p2[1] - p1[1];
|
|
41
|
+
const len = Math.hypot(dx, dy);
|
|
42
|
+
return [dy / len, -dx / len]; // rotated +90°
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// Average adjacent normals.
|
|
46
|
+
export const offsetPoints = (points: Point[], distance: number, closed: boolean): Point[] => {
|
|
47
|
+
const len = points.length;
|
|
48
|
+
const out: Point[] = [];
|
|
49
|
+
|
|
50
|
+
for (let i = 0; i < len; i++) {
|
|
51
|
+
const p0 = points[(i - 1 + len) % len];
|
|
52
|
+
const p1 = points[i];
|
|
53
|
+
const p2 = points[(i + 1) % len];
|
|
54
|
+
|
|
55
|
+
const n1 = normal(p0, p1);
|
|
56
|
+
const n2 = normal(p1, p2);
|
|
57
|
+
const nx = (n1[0] + n2[0]) / 2;
|
|
58
|
+
const ny = (n1[1] + n2[1]) / 2;
|
|
59
|
+
const norm = Math.hypot(nx, ny) || 1;
|
|
60
|
+
out.push([p1[0] + (distance * nx) / norm, p1[1] + (distance * ny) / norm]);
|
|
61
|
+
|
|
62
|
+
if (!closed && (i === 0 || i === len - 1)) {
|
|
63
|
+
out[i] = [p1[0] + distance * normal(p0, p1)[0], p1[1] + distance * normal(p0, p1)[1]];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return out;
|
|
68
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { expect, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { Scale } from './scale';
|
|
8
|
+
import { Vector } from './vector';
|
|
9
|
+
|
|
10
|
+
test('toVertex', () => {
|
|
11
|
+
const scale = new Scale(16);
|
|
12
|
+
const v1 = Vector.toVertex({ x: 4, y: 8 });
|
|
13
|
+
const p1 = scale.model.toPoint(v1);
|
|
14
|
+
expect(p1).to.deep.equal([4 * 16, -8 * 16]);
|
|
15
|
+
const v2 = scale.screen.toVertex(p1);
|
|
16
|
+
expect(v1).to.deep.equal(v2);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test('toBounds', () => {
|
|
20
|
+
const scale = new Scale(16);
|
|
21
|
+
const b1 = Vector.toBounds({ x: 0, y: 0, width: 4, height: 4 });
|
|
22
|
+
const b2 = scale.model.toBounds(b1);
|
|
23
|
+
expect(b2).to.deep.equal({ x: 0, y: -4 * 16, width: 4 * 16, height: 4 * 16 });
|
|
24
|
+
const b3 = scale.screen.toBounds(b2);
|
|
25
|
+
expect(b1).to.deep.equal(b3);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('snapValues', () => {
|
|
29
|
+
const scale = new Scale(16);
|
|
30
|
+
expect(scale.screen.snapValues([3, 9])).to.deep.equal([0, 16]);
|
|
31
|
+
});
|