Skip to content

Commit 75d1b6b

Browse files
authored
Merge pull request #387 from DanieLazarLDAPPS/features/labview-style-of-routes
Features/labview style of routes
2 parents 96655ba + 4cae28c commit 75d1b6b

File tree

7 files changed

+390
-3
lines changed

7 files changed

+390
-3
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import createEngine, {
2+
DiagramModel,
3+
DefaultNodeModel,
4+
DefaultPortModel,
5+
RightAngleLinkFactory, LinkModel, RightAngleLinkModel,
6+
} from '@projectstorm/react-diagrams';
7+
import * as React from 'react';
8+
import { DemoButton, DemoWorkspaceWidget } from '../helpers/DemoWorkspaceWidget';
9+
import { action } from '@storybook/addon-actions';
10+
import {AbstractModelFactory, CanvasWidget} from '@projectstorm/react-canvas-core';
11+
import { DemoCanvasWidget } from '../helpers/DemoCanvasWidget';
12+
13+
// When new link is created by clicking on port the RightAngleLinkModel needs to be returned.
14+
export class RightAnglePortModel extends DefaultPortModel {
15+
createLinkModel(factory?: AbstractModelFactory<LinkModel>) {
16+
return new RightAngleLinkModel();
17+
}
18+
}
19+
20+
export default () => {
21+
// setup the diagram engine
22+
const engine = createEngine();
23+
engine.getLinkFactories().registerFactory(new RightAngleLinkFactory());
24+
25+
// setup the diagram model
26+
const model = new DiagramModel();
27+
28+
// create four nodes in a way that straight links wouldn't work
29+
const node1 = new DefaultNodeModel("Node A", "rgb(0,192,255)");
30+
const port1 = node1.addPort(new RightAnglePortModel(false, "out-1", "Out"));
31+
node1.setPosition(340, 350);
32+
33+
const node2 = new DefaultNodeModel("Node B", "rgb(255,255,0)");
34+
const port2 = node2.addPort(new RightAnglePortModel(false, "out-1", "Out"));
35+
node2.setPosition(240, 80);
36+
const node3 = new DefaultNodeModel("Node C", "rgb(192,255,255)");
37+
const port3 = node3.addPort(new RightAnglePortModel(true, "in-1", "In"));
38+
node3.setPosition(540, 180);
39+
const node4 = new DefaultNodeModel("Node D", "rgb(192,0,255)");
40+
const port4 = node4.addPort(new RightAnglePortModel(true, "in-1", "In"));
41+
node4.setPosition(95, 185);
42+
43+
// linking things together
44+
const link1 = port1.link(port4);
45+
const link2 = port2.link(port3);
46+
47+
// add all to the main model
48+
model.addAll(node1, node2, node3, node4, link1, link2);
49+
50+
// load model into engine and render
51+
engine.setModel(model);
52+
53+
return (
54+
<DemoWorkspaceWidget
55+
buttons={
56+
<DemoButton
57+
onClick={() => {
58+
action('Serialized Graph')(JSON.stringify(model.serialize(), null, 2));
59+
}}>
60+
Serialize Graph
61+
</DemoButton>
62+
}>
63+
<DemoCanvasWidget>
64+
<CanvasWidget engine={engine} />
65+
</DemoCanvasWidget>
66+
</DemoWorkspaceWidget>
67+
);
68+
};

packages/diagrams-demo-gallery/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ import demo_adv_ser_des from './demos/demo-serializing';
5050
import demo_adv_prog from './demos/demo-mutate-graph';
5151
import demo_adv_dnd from './demos/demo-drag-and-drop';
5252
import demo_smart_routing from './demos/demo-smart-routing';
53+
import demo_right_angles_routing from './demos/demo-right-angles-routing';
5354

5455
storiesOf('Advanced Techniques', module)
5556
.add('Clone Selected', demo_adv_clone_selected)
5657
.add('Serializing and de-serializing', demo_adv_ser_des)
5758
.add('Programatically modifying graph', demo_adv_prog)
5859
.add('Drag and drop', demo_adv_dnd)
59-
.add('Smart routing', demo_smart_routing);
60+
.add('Smart routing', demo_smart_routing)
61+
.add('Right angles routing', demo_right_angles_routing);
6062

6163
import demo_cust_nodes from './demos/demo-custom-node1';
6264
import demo_cust_links from './demos/demo-custom-link1';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
export * from './src/link/PathFindingLinkFactory';
22
export * from './src/link/PathFindingLinkModel';
33
export * from './src/link/PathFindingLinkWidget';
4+
export * from './src/link/RightAngleLinkWidget';
5+
export * from './src/link/RightAngleLinkFactory';
6+
export * from './src/link/RightAngleLinkModel';
47

58
export * from './src/engine/PathFinding';
69
export * from './src/dagre/DagreEngine';

packages/react-diagrams-routing/src/engine/PathFinding.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,22 @@ export default class PathFinding {
5959
pathToStart: number[][];
6060
pathToEnd: number[][];
6161
} {
62-
const startIndex = path.findIndex(point => matrix[point[1]][point[0]] === 0);
62+
const startIndex = path.findIndex(point => {
63+
if (matrix[point[1]])
64+
return matrix[point[1]][point[0]] === 0;
65+
else return false;
66+
});
6367
const endIndex =
6468
path.length -
6569
1 -
6670
path
6771
.slice()
6872
.reverse()
69-
.findIndex(point => matrix[point[1]][point[0]] === 0);
73+
.findIndex(point => {
74+
if (matrix[point[1]])
75+
return matrix[point[1]][point[0]] === 0;
76+
else return false;
77+
});
7078

7179
// are we trying to create a path exclusively through blocked areas?
7280
// if so, let's fallback to the linear routing
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as React from "react";
2+
import { RightAngleLinkWidget } from "./RightAngleLinkWidget";
3+
import { DiagramEngine } from '@projectstorm/react-diagrams-core';
4+
import { DefaultLinkFactory, DefaultLinkModel } from '@projectstorm/react-diagrams-defaults';
5+
import {RightAngleLinkModel} from "./RightAngleLinkModel";
6+
7+
/**
8+
* @author Daniel Lazar
9+
*/
10+
export class RightAngleLinkFactory extends DefaultLinkFactory<RightAngleLinkModel> {
11+
12+
static NAME = 'rightAngle';
13+
14+
constructor() {
15+
super(RightAngleLinkFactory.NAME);
16+
}
17+
18+
generateModel(event): RightAngleLinkModel {
19+
return new RightAngleLinkModel();
20+
}
21+
22+
generateReactWidget(event): JSX.Element {
23+
return <RightAngleLinkWidget diagramEngine={this.engine} link={event.model} factory={this}/>
24+
}
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { DefaultLinkModel, DefaultLinkModelOptions } from '@projectstorm/react-diagrams-defaults';
2+
import {RightAngleLinkFactory} from "./RightAngleLinkFactory";
3+
4+
export class RightAngleLinkModel extends DefaultLinkModel {
5+
constructor(options: DefaultLinkModelOptions = {}) {
6+
super({
7+
type: RightAngleLinkFactory.NAME,
8+
...options
9+
});
10+
}
11+
12+
performanceTune() {
13+
return false;
14+
}
15+
}

0 commit comments

Comments
 (0)