Skip to content
This repository was archived by the owner on Mar 17, 2024. It is now read-only.

Commit ab3d01a

Browse files
authored
Merge pull request #65 from ts-graphviz/cluster-common-attributes
Add cluster common attributes
2 parents 36a159c + 0cedb2a commit ab3d01a

File tree

9 files changed

+107
-60
lines changed

9 files changed

+107
-60
lines changed

example/example.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@ import React, { FC } from 'react';
22
import { Digraph, Node, Subgraph, renderToDot, Edge, DOT } from '../src';
33

44
const Example: FC = () => (
5-
<Digraph dpi={150}>
5+
<Digraph
6+
dpi={150}
7+
rankdir="TB"
8+
edge={{
9+
color: 'blue',
10+
fontcolor: 'blue',
11+
}}
12+
node={{
13+
shape: 'none',
14+
}}
15+
>
616
<Node
717
id="nodeA"
818
shape="none"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { EdgeAttributes, NodeAttributes, ClusterSubgraphAttributes, ICluster, AttributesObject } from 'ts-graphviz';
2+
import { useEffect } from 'react';
3+
4+
export type ClusterAttributesProps = {
5+
edge?: EdgeAttributes;
6+
node?: NodeAttributes;
7+
graph?: ClusterSubgraphAttributes;
8+
};
9+
10+
export const useClusterAttributes = <T extends string>(
11+
cluster: ICluster<T>,
12+
attributes: AttributesObject<T>,
13+
{ edge, node, graph }: ClusterAttributesProps,
14+
): void => {
15+
useEffect(() => {
16+
cluster.clear();
17+
cluster.apply(attributes);
18+
}, [cluster, attributes]);
19+
useEffect(() => {
20+
cluster.attributes.node.clear();
21+
cluster.attributes.node.apply(node ?? {});
22+
}, [cluster, node]);
23+
24+
useEffect(() => {
25+
cluster.attributes.edge.clear();
26+
cluster.attributes.edge.apply(edge ?? {});
27+
}, [cluster, edge]);
28+
29+
useEffect(() => {
30+
cluster.attributes.graph.clear();
31+
cluster.attributes.graph.apply(graph ?? {});
32+
}, [cluster, graph]);
33+
};

src/hooks/use-comment.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { IHasComment } from 'ts-graphviz';
2+
import { useEffect } from 'react';
3+
4+
export const useHasComment = (target: IHasComment, comment?: string): void =>
5+
useEffect(() => {
6+
// eslint-disable-next-line no-param-reassign
7+
target.comment = comment;
8+
}, [target, comment]);

src/hooks/use-digraph.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
import { useMemo, useEffect } from 'react';
22
import { Digraph, RootClusterAttributes } from 'ts-graphviz';
33
import { useGraphvizContext } from './use-graphviz-context';
4+
import { useClusterAttributes, ClusterAttributesProps } from './use-cluster-attributes';
5+
import { useHasComment } from './use-comment';
46

57
export type DigraphProps = {
68
id?: string;
79
comment?: string;
8-
} & RootClusterAttributes;
10+
} & RootClusterAttributes &
11+
ClusterAttributesProps;
912

10-
export const useDigraph = ({ id, comment, ...attributes }: DigraphProps = {}): Digraph => {
13+
export const useDigraph = ({ id, comment, edge, node, graph, ...attributes }: DigraphProps = {}): Digraph => {
1114
const context = useGraphvizContext();
1215
const digraph = useMemo(() => {
1316
const g = new Digraph(context, id);
1417
g.comment = comment;
1518
g.apply(attributes);
19+
g.attributes.node.apply(node ?? {});
20+
g.attributes.edge.apply(edge ?? {});
21+
g.attributes.graph.apply(graph ?? {});
1622
return g;
17-
}, [context, id, comment, attributes]);
23+
}, [context, id, comment, edge, node, graph, attributes]);
24+
useHasComment(digraph, comment);
25+
useClusterAttributes(digraph, attributes, { edge, node, graph });
1826
useEffect(() => {
1927
return (): void => {
2028
context.root = undefined;
2129
};
2230
}, [context]);
23-
24-
useEffect(() => {
25-
digraph.clear();
26-
digraph.apply(attributes);
27-
}, [digraph, attributes]);
28-
29-
useEffect(() => {
30-
digraph.comment = comment;
31-
}, [digraph, comment]);
3231
return digraph;
3332
};

src/hooks/use-edge.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { useEffect, useMemo } from 'react';
22
import { EdgeTargetLike, IEdge, EdgeAttributes } from 'ts-graphviz';
33
import { useCluster } from './use-cluster';
44
import { EdgeTargetLengthErrorMessage } from '../utils/errors';
5+
import { useHasComment } from './use-comment';
6+
import { useHasAttributes } from './use-has-attributes';
57

68
export type EdgeProps = {
79
targets: EdgeTargetLike[];
@@ -19,15 +21,8 @@ export const useEdge = ({ targets, comment, ...attributes }: EdgeProps): IEdge =
1921
e.attributes.apply(attributes);
2022
return e;
2123
}, [cluster, targets, comment, attributes]);
22-
useEffect(() => {
23-
edge.attributes.clear();
24-
edge.attributes.apply(attributes);
25-
}, [edge, attributes]);
26-
27-
useEffect(() => {
28-
edge.comment = comment;
29-
}, [edge, comment]);
30-
24+
useHasComment(edge, comment);
25+
useHasAttributes(edge, attributes);
3126
useEffect(() => {
3227
return (): void => {
3328
cluster.removeEdge(edge);

src/hooks/use-graph.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
import { useMemo, useEffect } from 'react';
2-
import { Graph, RootClusterAttributes } from 'ts-graphviz';
2+
import { Graph, RootClusterAttributes, NodeAttributes, EdgeAttributes, ClusterSubgraphAttributes } from 'ts-graphviz';
33
import { useGraphvizContext } from './use-graphviz-context';
4+
import { ClusterAttributesProps, useClusterAttributes } from './use-cluster-attributes';
5+
import { useHasComment } from './use-comment';
46

57
export type GraphProps = {
68
id?: string;
79
comment?: string;
8-
} & RootClusterAttributes;
10+
edge?: EdgeAttributes;
11+
node?: NodeAttributes;
12+
graph?: ClusterSubgraphAttributes;
13+
} & RootClusterAttributes &
14+
ClusterAttributesProps;
915

10-
export const useGraph = ({ id, comment, ...attributes }: GraphProps = {}): Graph => {
16+
export const useGraph = ({ id, comment, edge, node, graph, ...attributes }: GraphProps = {}): Graph => {
1117
const context = useGraphvizContext();
12-
const graph = useMemo(() => {
18+
const memoGraph = useMemo(() => {
1319
const g = new Graph(context, id);
1420
g.comment = comment;
1521
g.apply(attributes);
22+
g.attributes.node.apply(node ?? {});
23+
g.attributes.edge.apply(edge ?? {});
24+
g.attributes.graph.apply(graph ?? {});
1625
return g;
17-
}, [context, id, comment, attributes]);
26+
}, [context, id, comment, edge, node, graph, attributes]);
27+
useHasComment(memoGraph, comment);
28+
useClusterAttributes(memoGraph, attributes, { edge, node, graph });
1829
useEffect(() => {
1930
return (): void => {
2031
context.root = undefined;
2132
};
2233
}, [context]);
23-
24-
useEffect(() => {
25-
graph.clear();
26-
graph.apply(attributes);
27-
}, [graph, attributes]);
28-
29-
useEffect(() => {
30-
graph.comment = comment;
31-
}, [graph, comment]);
32-
return graph;
34+
return memoGraph;
3335
};

src/hooks/use-has-attributes.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { useEffect } from 'react';
2+
import { IHasAttributes, AttributesObject } from 'ts-graphviz';
3+
4+
export const useHasAttributes = <T extends string>(target: IHasAttributes<T>, attributes: AttributesObject<T>): void =>
5+
useEffect(() => {
6+
target.attributes.clear();
7+
target.attributes.apply(attributes);
8+
}, [target, attributes]);

src/hooks/use-node.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { useEffect, useMemo } from 'react';
22
import { INode, NodeAttributes } from 'ts-graphviz';
33
import { useCluster } from './use-cluster';
4+
import { useHasComment } from './use-comment';
5+
import { useHasAttributes } from './use-has-attributes';
46

57
export type NodeProps = {
68
id: string;
@@ -15,14 +17,8 @@ export const useNode = ({ id, comment, ...attributes }: NodeProps): INode => {
1517
n.comment = comment;
1618
return n;
1719
}, [cluster, id, attributes, comment]);
18-
useEffect(() => {
19-
node.attributes.clear();
20-
node.attributes.apply(attributes);
21-
}, [node, attributes]);
22-
23-
useEffect(() => {
24-
node.comment = comment;
25-
}, [node, comment]);
20+
useHasComment(node, comment);
21+
useHasAttributes(node, attributes);
2622
useEffect(() => {
2723
return (): void => {
2824
cluster.removeNode(node);

src/hooks/use-subgraph.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
import { useMemo, useEffect } from 'react';
22
import { ISubgraph, ClusterSubgraphAttributes } from 'ts-graphviz';
33
import { useCluster } from './use-cluster';
4+
import { useClusterAttributes, ClusterAttributesProps } from './use-cluster-attributes';
5+
import { useHasComment } from './use-comment';
46

57
export type SubgraphProps = {
68
id?: string;
79
comment?: string;
8-
} & ClusterSubgraphAttributes;
10+
} & ClusterSubgraphAttributes &
11+
ClusterAttributesProps;
912

10-
export const useSubgraph = ({ id, comment, ...attributes }: SubgraphProps = {}): ISubgraph => {
13+
export const useSubgraph = ({ id, comment, edge, node, graph, ...attributes }: SubgraphProps = {}): ISubgraph => {
1114
const cluster = useCluster();
1215
const subgraph = useMemo(() => {
1316
const g = cluster.createSubgraph(id);
1417
g.comment = comment;
1518
g.apply(attributes);
19+
g.attributes.node.apply(node ?? {});
20+
g.attributes.edge.apply(edge ?? {});
21+
g.attributes.graph.apply(graph ?? {});
1622
return g;
17-
}, [cluster, id, comment, attributes]);
23+
}, [cluster, id, comment, edge, node, graph, attributes]);
24+
useHasComment(subgraph, comment);
25+
useClusterAttributes(subgraph, attributes, { edge, node, graph });
1826
useEffect(() => {
1927
return (): void => {
2028
cluster.removeSubgraph(subgraph);
2129
};
2230
}, [cluster, subgraph]);
23-
useEffect(() => {
24-
subgraph.id = id;
25-
}, [subgraph, id]);
26-
27-
useEffect(() => {
28-
subgraph.clear();
29-
subgraph.apply(attributes);
30-
}, [subgraph, attributes]);
31-
32-
useEffect(() => {
33-
subgraph.comment = comment;
34-
}, [subgraph, comment]);
3531
return subgraph;
3632
};

0 commit comments

Comments
 (0)