Skip to content

Commit a978a9a

Browse files
issue-277 few null checks to avoid crash when node went under bottom of screen
1 parent 01e9f5f commit a978a9a

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/DiagramEngine.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ export class DiagramEngine extends BaseEntity<DiagramEngineListener> {
275275
}
276276

277277
getNodeElement(node: NodeModel): Element {
278+
if (!this.canvas) {
279+
return null;
280+
}
278281
const selector = this.canvas.querySelector(`.node[data-nodeid="${node.getID()}"]`);
279282
if (selector === null) {
280283
throw new Error("Cannot find Node element with nodeID: [" + node.getID() + "]");
@@ -283,6 +286,9 @@ export class DiagramEngine extends BaseEntity<DiagramEngineListener> {
283286
}
284287

285288
getNodePortElement(port: PortModel): any {
289+
if (!this.canvas) {
290+
return null;
291+
}
286292
var selector = this.canvas.querySelector(
287293
`.port[data-name="${port.getName()}"][data-nodeid="${port.getParent().getID()}"]`
288294
);
@@ -326,6 +332,14 @@ export class DiagramEngine extends BaseEntity<DiagramEngineListener> {
326332
height: number;
327333
} {
328334
const sourceElement = this.getNodePortElement(port);
335+
if (!sourceElement) {
336+
return {
337+
x: 0,
338+
y: 0,
339+
width: 0,
340+
height: 0
341+
};
342+
}
329343
const sourceRect = sourceElement.getBoundingClientRect();
330344
const canvasRect = this.canvas.getBoundingClientRect() as ClientRect;
331345

@@ -354,6 +368,12 @@ export class DiagramEngine extends BaseEntity<DiagramEngineListener> {
354368
}
355369

356370
const nodeElement = this.getNodeElement(node);
371+
if (!nodeElement) {
372+
return {
373+
width: 0,
374+
height: 0
375+
};
376+
}
357377
const nodeRect = nodeElement.getBoundingClientRect();
358378

359379
return {
@@ -496,6 +516,14 @@ export class DiagramEngine extends BaseEntity<DiagramEngineListener> {
496516
}));
497517

498518
const canvas = this.canvas as HTMLDivElement;
519+
if (!canvas) {
520+
return {
521+
width: 0,
522+
hAdjustmentFactor: 0,
523+
height: 0,
524+
vAdjustmentFactor: 0
525+
};
526+
}
499527
const minX =
500528
Math.floor(
501529
Math.min(_.minBy(_.concat(allNodesCoords, allPortsCoords, allPointsCoords), item => item.x).x, 0) /

src/routing/PathFinding.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,22 @@ export default class PathFinding {
6868
pathToStart: number[][];
6969
pathToEnd: number[][];
7070
} {
71-
const startIndex = path.findIndex(point => matrix[point[1]][point[0]] === 0);
71+
const startIndex = path.findIndex(point => {
72+
if (matrix[point[1]])
73+
return matrix[point[1]][point[0]] === 0;
74+
else return false;
75+
});
7276
const endIndex =
7377
path.length -
7478
1 -
7579
path
7680
.slice()
7781
.reverse()
78-
.findIndex(point => matrix[point[1]][point[0]] === 0);
82+
.findIndex(point => {
83+
if (matrix[point[1]])
84+
return matrix[point[1]][point[0]] === 0;
85+
else return false;
86+
});
7987

8088
// are we trying to create a path exclusively through blocked areas?
8189
// if so, let's fallback to the linear routing

0 commit comments

Comments
 (0)