Skip to content

Commit a3a181a

Browse files
authored
feat(Storybook): change elk examples (#46)
* chore(Storybook): change generatePretty attributes * feat(Storybook): change elk examples
1 parent 378e178 commit a3a181a

File tree

13 files changed

+327
-85
lines changed

13 files changed

+327
-85
lines changed

src/stories/api/startStopGraph/startStop.stories.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { BlockStory } from "../../main/Block";
1212

1313
import "@gravity-ui/uikit/styles/styles.css";
1414

15-
const config = generatePrettyBlocks(10, 10, true, {}, 0);
15+
const config = generatePrettyBlocks({ layersCount: 10, connectionsPerLayer: 10, dashedLine: true });
1616

1717
const exampleGraph = new Graph({
1818
configurationName: "start-stop",
@@ -45,10 +45,15 @@ const GraphApp = () => {
4545
});
4646

4747
const update = () => {
48-
const config = generatePrettyBlocks(10, 10, true, {}, Math.random() * 1000);
48+
const { blocks, connections } = generatePrettyBlocks({
49+
layersCount: 10,
50+
connectionsPerLayer: 10,
51+
dashedLine: true,
52+
startIndex: Math.random() * 1000,
53+
});
4954
setEntities({
50-
blocks: config.blocks,
51-
connections: config.connections,
55+
blocks,
56+
connections,
5257
});
5358
};
5459

src/stories/api/zoomSpeed/zoomSpeed.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { GraphComponentStory } from "../../main/GraphEditor";
99

1010
import "@gravity-ui/uikit/styles/styles.css";
1111

12-
const config = generatePrettyBlocks(10, 10, true);
12+
const config = generatePrettyBlocks({ layersCount: 10, connectionsPerLayer: 10, dashedLine: true });
1313

1414
const GraphApp = () => {
1515
const [speed, setSpeed] = useState("1");

src/stories/api/zoomToBlocks/zoomToBlocks.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { GraphComponentStory } from "../../main/GraphEditor";
99

1010
import "@gravity-ui/uikit/styles/styles.css";
1111

12-
const config = generatePrettyBlocks(10, 10, true);
12+
const config = generatePrettyBlocks({ layersCount: 10, connectionsPerLayer: 10, dashedLine: true });
1313

1414
const GraphApp = () => {
1515
const [transition, setTransition] = useState("1000");

src/stories/api/zoomToRect/zoomToRect.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { GraphComponentStory } from "../../main/GraphEditor";
99

1010
import "@gravity-ui/uikit/styles/styles.css";
1111

12-
const config = generatePrettyBlocks(10, 10, true);
12+
const config = generatePrettyBlocks({ layersCount: 10, connectionsPerLayer: 10, dashedLine: true });
1313

1414
const GraphApp = () => {
1515
const [x, setX] = useState("0");

src/stories/api/zoomToViewPort/zoomToViewPort.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { GraphComponentStory } from "../../main/GraphEditor";
99

1010
import "@gravity-ui/uikit/styles/styles.css";
1111

12-
const config = generatePrettyBlocks(10, 10, true);
12+
const config = generatePrettyBlocks({ layersCount: 10, connectionsPerLayer: 10, dashedLine: true });
1313

1414
const GraphApp = () => {
1515
const [transition, setTransition] = useState("1000");

src/stories/configurations/generatePretty.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { IS_BLOCK_TYPE } from "../../store/block/Block";
66

77
import { storiesSettings } from "./definitions";
88

9-
function createBlock(x: number, y: number, index): TBlock {
9+
export function createBlock(x: number, y: number, index): TBlock {
1010
const blockId = `block_${index}`;
1111
return {
1212
id: blockId,
@@ -25,13 +25,21 @@ function getRandomArbitrary(min, max) {
2525
return (Math.random() * (max - min) + min) | 0;
2626
}
2727

28-
export function generatePrettyBlocks(
29-
layersCount: number,
30-
connectionsPerLayer: number,
31-
dashedLine = false,
32-
overrideSettings?: Partial<TGraphConfig["settings"]>,
33-
startIndex = 0
34-
) {
28+
type Props = {
29+
layersCount: number;
30+
connectionsPerLayer: number;
31+
dashedLine?: boolean;
32+
overrideSettings?: Partial<TGraphConfig["settings"]>;
33+
startIndex?: number;
34+
};
35+
36+
export function generatePrettyBlocks({
37+
layersCount,
38+
connectionsPerLayer,
39+
dashedLine,
40+
overrideSettings = {},
41+
startIndex = 0,
42+
}: Props) {
3543
const config: TGraphConfig = {
3644
configurationName: "power of 2",
3745
blocks: [],

src/stories/examples/imageInsteadBlock/imageInsteadBlock.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ const GraphApp = () => {
105105
const [config, setConfig] = useState<TGraphConfig | undefined>();
106106

107107
useEffect(() => {
108-
const newConfig = generatePrettyBlocks(10, 100, true);
108+
const newConfig = generatePrettyBlocks({ layersCount: 10, connectionsPerLayer: 100, dashedLine: true });
109109
newConfig.settings.blockComponents = {};
110110
newConfig.settings.blockComponents[SpecificBlockIs] = SpecificBlockView;
111111

src/stories/main/GraphEditor.stories.tsx

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,27 @@ export const WithAnchors: Story = {
122122
};
123123

124124
export const HundredBlocks: Story = {
125-
render: (args) => <GraphApp config={generatePrettyBlocks(10, 100, true)} {...args}></GraphApp>,
125+
render: (args) => (
126+
<GraphApp
127+
config={generatePrettyBlocks({ layersCount: 10, connectionsPerLayer: 100, dashedLine: true })}
128+
{...args}
129+
></GraphApp>
130+
),
126131
};
127132

128133
export const ThousandBlocks: Story = {
129-
render: (args) => <GraphApp config={generatePrettyBlocks(25, 200)} {...args}></GraphApp>,
134+
render: (args) => (
135+
<GraphApp config={generatePrettyBlocks({ layersCount: 25, connectionsPerLayer: 200 })} {...args}></GraphApp>
136+
),
130137
};
131138

132139
export const FiveThousandsBlocks: Story = {
133140
args: {
134141
useBezierConnections: false,
135142
},
136-
render: (args) => <GraphApp config={generatePrettyBlocks(40, 300, false)} {...args}></GraphApp>,
143+
render: (args) => (
144+
<GraphApp config={generatePrettyBlocks({ layersCount: 30, connectionsPerLayer: 300 })} {...args}></GraphApp>
145+
),
137146
};
138147

139148
export const GraphStressTest: Story = {
@@ -142,14 +151,28 @@ export const GraphStressTest: Story = {
142151
},
143152
render: (args) => {
144153
return (
145-
<GraphApp config={generatePrettyBlocks(110, 1000, false, { useBezierConnections: false })} {...args}></GraphApp>
154+
<GraphApp
155+
config={generatePrettyBlocks({
156+
layersCount: 110,
157+
connectionsPerLayer: 1000,
158+
overrideSettings: { useBezierConnections: false },
159+
})}
160+
{...args}
161+
></GraphApp>
146162
);
147163
},
148164
};
149165
export const NirvanaMaxGraphTest: Story = {
150166
render: (args) => {
151167
return (
152-
<GraphApp config={generatePrettyBlocks(55, 400, false, { useBezierConnections: true })} {...args}></GraphApp>
168+
<GraphApp
169+
config={generatePrettyBlocks({
170+
layersCount: 55,
171+
connectionsPerLayer: 400,
172+
overrideSettings: { useBezierConnections: true },
173+
})}
174+
{...args}
175+
></GraphApp>
153176
);
154177
},
155178
};
@@ -168,7 +191,7 @@ export const VerticalGraph: Story = {
168191
export const SnappingGraph: Story = {
169192
render: (args) => (
170193
<GraphApp
171-
config={generatePrettyBlocks(4, 100)}
194+
config={generatePrettyBlocks({ layersCount: 4, connectionsPerLayer: 100 })}
172195
{...args}
173196
constants={{ block: { SNAPPING_GRID_SIZE: 60 } }}
174197
></GraphApp>

src/stories/plugins/elk/elk.stories.tsx

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,59 @@ import { Select, SelectOption, ThemeProvider } from "@gravity-ui/uikit";
44
import type { Meta, StoryFn } from "@storybook/react";
55

66
import { Graph, GraphCanvas, GraphState, TBlock, TConnection, useGraph, useGraphEvent } from "../../../index";
7-
import { MultipointConnection } from "../../../plugins/elk/components/MultipointConnection";
8-
import { useElk } from "../../../plugins/elk/hooks/useElk";
7+
import { MultipointConnection, useElk } from "../../../plugins";
98
import { TMultipointConnection } from "../../../plugins/elk/types";
109
import { useFn } from "../../../utils/hooks/useFn";
11-
import { generatePrettyBlocks } from "../../configurations/generatePretty";
1210
import { BlockStory } from "../../main/Block";
1311

14-
import { getElkConfig } from "./getElkConfig";
12+
import { getExampleConfig } from "./getExampleConfig";
13+
14+
export enum Algorithm {
15+
Box = "box",
16+
Layered = "layered",
17+
Disco = "disco",
18+
Radial = "radial",
19+
MrTree = "mrtree",
20+
Force = "force",
21+
Stress = "stress",
22+
Random = "random",
23+
SporeOverlap = "sporeOverlap",
24+
SporeCompaction = "sporeCompaction",
25+
}
1526

1627
import "@gravity-ui/uikit/styles/styles.css";
1728

18-
const config = generatePrettyBlocks(10, 30, true);
19-
2029
const GraphApp = () => {
21-
const [algorithm, setAlgorithm] = useState("layered");
30+
const [algorithm, setAlgorithm] = useState(Algorithm.Layered);
31+
const [algorithms, setAlgorithms] = useState<SelectOption[]>([]);
2232
const { graph, setEntities, start } = useGraph({
2333
settings: {
2434
connection: MultipointConnection,
2535
},
2636
});
2737

28-
const elkConfig = useMemo(() => {
29-
return getElkConfig(config, algorithm);
38+
const { elkConfig, graphConfig } = useMemo(() => {
39+
return getExampleConfig(algorithm);
3040
}, [algorithm]);
3141

3242
const { isLoading, elk, result } = useElk(elkConfig);
3343

3444
useEffect(() => {
3545
if (isLoading || !result) return;
3646

37-
const connections = config.connections.reduce<(TConnection & Pick<TMultipointConnection, "points" | "labels">)[]>(
38-
(acc, connection) => {
39-
if (connection.id in result.edges) {
40-
acc.push({
41-
...connection,
42-
...result.edges[connection.id],
43-
});
44-
}
45-
return acc;
46-
},
47-
[]
48-
);
49-
50-
const blocks = config.blocks.map((block) => {
47+
const connections = graphConfig.connections.reduce<
48+
(TConnection & Pick<TMultipointConnection, "points" | "labels">)[]
49+
>((acc, connection) => {
50+
if (connection.id in result.edges) {
51+
acc.push({
52+
...connection,
53+
...result.edges[connection.id],
54+
});
55+
}
56+
return acc;
57+
}, []);
58+
59+
const blocks = graphConfig.blocks.map((block) => {
5160
return {
5261
...block,
5362
...result.blocks[block.id],
@@ -62,8 +71,6 @@ const GraphApp = () => {
6271
graph.zoomTo("center", { padding: 300 });
6372
}, [isLoading, result]);
6473

65-
const [algorithms, setAlgorithms] = useState<SelectOption[]>([]);
66-
6774
useEffect(() => {
6875
elk.knownLayoutAlgorithms().then((knownLayoutAlgorithms) => {
6976
setAlgorithms(
@@ -88,7 +95,7 @@ const GraphApp = () => {
8895

8996
return (
9097
<ThemeProvider theme={"light"}>
91-
<Select value={[algorithm]} options={algorithms} onUpdate={(v) => setAlgorithm(v[0])}></Select>
98+
<Select value={[algorithm]} options={algorithms} onUpdate={(v) => setAlgorithm(v[0] as Algorithm)}></Select>
9299
<GraphCanvas className="graph" graph={graph} renderBlock={renderBlockFn} />;
93100
</ThemeProvider>
94101
);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { TGraphConfig } from "../../../graph";
2+
import { createBlock } from "../../configurations/generatePretty";
3+
4+
export function generateExampleTree(levels: number) {
5+
const tree: Pick<TGraphConfig, "blocks" | "connections"> = {
6+
blocks: [],
7+
connections: [],
8+
};
9+
10+
function generateLevel(level: number, parentIndex = -1) {
11+
const numNodes = 2 ** level;
12+
13+
for (let i = 0; i < numNodes; i++) {
14+
const id = `block_${level}-${i}`;
15+
tree.blocks.push(createBlock(1, 1, `${level}-${i}`));
16+
17+
if (level) {
18+
const sourceId = `block_${level - 1}-${parentIndex - 1}`;
19+
tree.connections.push({
20+
id: `${sourceId}/${id}`,
21+
targetBlockId: id,
22+
sourceBlockId: sourceId,
23+
});
24+
}
25+
}
26+
}
27+
28+
for (let i = 0; i < levels; i++) {
29+
generateLevel(i, Math.floor(2 ** i / 2));
30+
}
31+
32+
return tree;
33+
}

0 commit comments

Comments
 (0)