Skip to content

Commit

Permalink
Merge pull request #1025 from TenTakano/remove_settimeout_from_demo_d…
Browse files Browse the repository at this point in the history
…agre

Remove setTimeout from demo-dagre to avoid layout breaks
  • Loading branch information
dylanvorster authored Feb 15, 2024
2 parents 8031008 + 1be4073 commit 633d03a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 55 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-bees-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@projectstorm/react-diagrams-gallery': minor
---

Remove setTimeout from demo-dagre to avoid layout breaks
120 changes: 65 additions & 55 deletions diagrams-demo-gallery/demos/demo-dagre/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import createEngine, {
DiagramEngine,
PathFindingLinkFactory
} from '@projectstorm/react-diagrams';
import * as React from 'react';
import { useLayoutEffect, useRef } from 'react';
import { DemoButton, DemoWorkspaceWidget } from '../helpers/DemoWorkspaceWidget';
import { CanvasWidget } from '@projectstorm/react-canvas-core';
import { DemoCanvasWidget } from '../helpers/DemoCanvasWidget';
Expand All @@ -33,72 +33,82 @@ function connectNodes(nodeFrom, nodeTo, engine: DiagramEngine) {
/**
* Tests auto distribution
*/
class DemoWidget extends React.Component<{ model: DiagramModel; engine: DiagramEngine }, any> {
engine: DagreEngine;

constructor(props) {
super(props);
this.engine = new DagreEngine({
graph: {
rankdir: 'RL',
ranker: 'longest-path',
marginx: 25,
marginy: 25
},
includeLinks: true,
nodeMargin: 25
});
}
function genDagreEngine() {
return new DagreEngine({
graph: {
rankdir: 'RL',
ranker: 'longest-path',
marginx: 25,
marginy: 25
},
includeLinks: true,
nodeMargin: 25
});
}

autoDistribute = () => {
this.engine.redistribute(this.props.model);

// only happens if pathfing is enabled (check line 25)
this.reroute();
this.props.engine.repaintCanvas();
};

autoRefreshLinks = () => {
this.engine.refreshLinks(this.props.model);

// only happens if pathfing is enabled (check line 25)
this.reroute();
this.props.engine.repaintCanvas();
};
componentDidMount(): void {
setTimeout(() => {
this.autoDistribute();
}, 500);
}
function autoDistribute(engine: DiagramEngine) {
const model = engine.getModel();

const dagreEngine = genDagreEngine();
dagreEngine.redistribute(model);

reroute(engine);
engine.repaintCanvas();
}

function autoRefreshLinks(engine: DiagramEngine) {
const model = engine.getModel();

reroute() {
this.props.engine
const dagreEngine = genDagreEngine();
dagreEngine.refreshLinks(model);

// only happens if pathfing is enabled (check line 29)
reroute(engine);
engine.repaintCanvas();
}

function reroute(engine: DiagramEngine) {
engine
.getLinkFactories()
.getFactory<PathFindingLinkFactory>(PathFindingLinkFactory.NAME)
.calculateRoutingMatrix();
}

function DemoWidget(props) {
const engine = props.engine;

useLayoutEffect(() => {
autoDistribute(engine);
}, []);

const redistribute = () => {
autoDistribute(engine);
}

render() {
return (
<DemoWorkspaceWidget
buttons={
<div>
<DemoButton onClick={this.autoDistribute}>Re-distribute</DemoButton>
<DemoButton onClick={this.autoRefreshLinks}>Refresh Links</DemoButton>
</div>
}
>
<DemoCanvasWidget>
<CanvasWidget engine={this.props.engine} />
</DemoCanvasWidget>
</DemoWorkspaceWidget>
);
const refreshLinks = () => {
autoRefreshLinks(engine);
}

return (
<DemoWorkspaceWidget
buttons={
<div>
<DemoButton onClick={redistribute}>Re-distribute</DemoButton>
<DemoButton onClick={refreshLinks}>Refresh Links</DemoButton>
</div>
}
>
<DemoCanvasWidget>
<CanvasWidget engine={engine} />
</DemoCanvasWidget>
</DemoWorkspaceWidget>
);
}

export default () => {
//1) setup the diagram engine
let engine = createEngine();
const engineRef = useRef(createEngine());
let engine = engineRef.current;

//2) setup the diagram model
let model = new DiagramModel();
Expand Down

0 comments on commit 633d03a

Please sign in to comment.