Skip to content

Instantly share code, notes, and snippets.

@hckhanh
Created April 14, 2020 12:35
Show Gist options
  • Save hckhanh/cc8cc69fa0125b88e3f8fa1bf055b093 to your computer and use it in GitHub Desktop.
Save hckhanh/cc8cc69fa0125b88e3f8fa1bf055b093 to your computer and use it in GitHub Desktop.
DagredWidget (class component) for react-diagrams
class DagreWidget extends React.Component<DagreWidgetProps, any> {
engine: DagreEngine;
constructor(props: any) {
super(props);
this.engine = new DagreEngine({
graph: {
rankdir: "RL",
ranker: "longest-path",
marginx: 25,
marginy: 25
},
includeLinks: true
});
}
autoDistribute = () => {
this.engine.redistribute(this.props.model);
// only happens if pathfing is enabled (check line 25)
this.reroute();
this.props.engine.repaintCanvas();
};
componentDidMount(): void {
setTimeout(() => {
this.autoDistribute();
}, 500);
}
reroute() {
this.props.engine
.getLinkFactories()
.getFactory<PathFindingLinkFactory>(PathFindingLinkFactory.NAME)
.calculateRoutingMatrix();
}
render() {
return (
<CanvasWidget
className={this.props.className}
engine={this.props.engine}
/>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment