Skip to content

Commit 359d7b0

Browse files
committed
fix/COMPASS-9664 Do not require measured heights for relationships
1 parent c40fb28 commit 359d7b0

File tree

5 files changed

+33
-38
lines changed

5 files changed

+33
-38
lines changed

src/components/edge/self-referencing-edge.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { path } from 'd3-path';
55
import { InternalNode } from '@/types/internal';
66
import { DEFAULT_MARKER_SIZE } from '@/utilities/constants';
77
import { Edge } from '@/components/edge/edge';
8+
import { getNodeWidth, getNodeHeight } from '@/utilities/node-dimensions';
89

910
export const SelfReferencingEdge = ({ id, source, markerEnd, markerStart, selected }: EdgeProps) => {
1011
const nodes = useNodes<InternalNode>();
@@ -18,8 +19,8 @@ export const SelfReferencingEdge = ({ id, source, markerEnd, markerStart, select
1819
return null;
1920
}
2021

21-
const centerX = (sourceNode.measured?.width || 0) / 2;
22-
const centerY = (sourceNode.measured?.height || 0) / 2;
22+
const centerX = (getNodeWidth(sourceNode) || 0) / 2;
23+
const centerY = (getNodeHeight(sourceNode) || 0) / 2;
2324

2425
const width = centerX + 40;
2526
const leftHeight = 30;

src/mocks/datasets/nodes.tsx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { NodeProps } from '@/types';
2-
import { DEFAULT_FIELD_HEIGHT, DEFAULT_NODE_WIDTH } from '@/utilities/constants';
32

43
export const ORDERS_NODE: NodeProps = {
54
id: 'orders',
@@ -8,10 +7,6 @@ export const ORDERS_NODE: NodeProps = {
87
x: 100,
98
y: 100,
109
},
11-
measured: {
12-
width: DEFAULT_NODE_WIDTH,
13-
height: DEFAULT_FIELD_HEIGHT * 2,
14-
},
1510
title: 'orders',
1611
fields: [
1712
{ name: 'ORDER_ID', type: 'varchar', glyphs: ['key'] },
@@ -26,10 +21,6 @@ export const EMPLOYEES_NODE: NodeProps = {
2621
x: 300,
2722
y: 300,
2823
},
29-
measured: {
30-
width: DEFAULT_NODE_WIDTH,
31-
height: DEFAULT_FIELD_HEIGHT * 4,
32-
},
3324
title: 'employees',
3425
fields: [
3526
{ name: 'employeeId', type: 'objectId', glyphs: ['key'] },
@@ -46,10 +37,6 @@ export const EMPLOYEE_TERRITORIES_NODE: NodeProps = {
4637
x: 400,
4738
y: 100,
4839
},
49-
measured: {
50-
width: DEFAULT_NODE_WIDTH,
51-
height: DEFAULT_FIELD_HEIGHT * 4,
52-
},
5340
title: 'employee_territories',
5441
fields: [
5542
{ name: 'employeeId', type: 'string', glyphs: ['key'] },

src/utilities/apply-layout.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import { ElkExtendedEdge } from 'elkjs';
22
import ELK from 'elkjs/lib/elk.bundled';
33

4-
import {
5-
DEFAULT_FIELD_HEIGHT,
6-
DEFAULT_FIELD_PADDING,
7-
DEFAULT_NODE_HEADER_HEIGHT,
8-
DEFAULT_NODE_SPACING,
9-
DEFAULT_NODE_STAR_SPACING,
10-
DEFAULT_NODE_WIDTH,
11-
} from '@/utilities/constants';
4+
import { DEFAULT_NODE_SPACING, DEFAULT_NODE_STAR_SPACING } from '@/utilities/constants';
125
import { ApplyLayout, BaseEdge, BaseNode, LayoutDirection } from '@/types/layout';
136

7+
import { getNodeHeight, getNodeWidth } from './node-dimensions';
8+
149
const TOP_BOTTOM = {
1510
'elk.algorithm': 'layered',
1611
'elk.direction': 'UP',
@@ -42,11 +37,6 @@ const getLayoutOptions = (direction: LayoutDirection) => {
4237
}
4338
};
4439

45-
const getNodeHeight = <N extends BaseNode>(node: N) => {
46-
const fieldCount = !('fields' in node) || !Array.isArray(node.fields) ? 1 : node.fields.length;
47-
return DEFAULT_NODE_HEADER_HEIGHT + DEFAULT_FIELD_PADDING * 2 + fieldCount * DEFAULT_FIELD_HEIGHT;
48-
};
49-
5040
/**
5141
* Applies a layout to a graph of nodes and edges using the ELK layout engine.
5242
*
@@ -65,12 +55,8 @@ export const applyLayout = <N extends BaseNode, E extends BaseEdge>(
6555
): Promise<ApplyLayout<N, E>> => {
6656
const transformedNodes = nodes.map<N>(node => ({
6757
...node,
68-
height:
69-
'height' in node && typeof node.height === 'number'
70-
? node.height
71-
: (node.measured?.height ?? getNodeHeight(node)),
72-
width:
73-
'width' in node && typeof node.width === 'number' ? node.width : (node.measured?.width ?? DEFAULT_NODE_WIDTH),
58+
height: getNodeHeight(node),
59+
width: getNodeWidth(node),
7460
}));
7561

7662
const transformedEdges = edges.map<ElkExtendedEdge>(edge => ({

src/utilities/get-edge-params.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Position, XYPosition } from '@xyflow/react';
33
import { InternalNode } from '@/types/internal';
44
import { DEFAULT_MARKER_SIZE } from '@/utilities/constants';
55

6+
import { getNodeHeight, getNodeWidth } from './node-dimensions';
7+
68
/**
79
* Returns the coordinates where a line connecting the centers of the source and target nodes intersects
810
* the edges of those nodes. This implementation is copied from:
@@ -12,10 +14,8 @@ import { DEFAULT_MARKER_SIZE } from '@/utilities/constants';
1214
* @param targetNode The target node
1315
*/
1416
const getNodeIntersection = (intersectionNode: InternalNode, targetNode: InternalNode): XYPosition => {
15-
const { width: intersectionNodeWidth, height: intersectionNodeHeight } = intersectionNode.measured ?? {
16-
width: 0,
17-
height: 0,
18-
};
17+
const intersectionNodeWidth = getNodeWidth(intersectionNode);
18+
const intersectionNodeHeight = getNodeHeight(intersectionNode);
1919
const targetPosition = targetNode.position;
2020

2121
const w = (intersectionNodeWidth ?? 0) / 2;

src/utilities/node-dimensions.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { BaseNode } from '@/types';
2+
3+
import {
4+
DEFAULT_FIELD_HEIGHT,
5+
DEFAULT_FIELD_PADDING,
6+
DEFAULT_NODE_HEADER_HEIGHT,
7+
DEFAULT_NODE_WIDTH,
8+
} from './constants';
9+
10+
export const getNodeHeight = <N extends BaseNode>(node: N) => {
11+
if ('height' in node && typeof node.height === 'number') return node.height;
12+
if ('measured' in node && node.measured?.height) return node.measured.height;
13+
const fieldCount = !('fields' in node) || !Array.isArray(node.fields) ? 1 : node.fields.length;
14+
return DEFAULT_NODE_HEADER_HEIGHT + DEFAULT_FIELD_PADDING * 2 + fieldCount * DEFAULT_FIELD_HEIGHT;
15+
};
16+
17+
export const getNodeWidth = <N extends BaseNode>(node: N) => {
18+
if ('width' in node && typeof node.width === 'number') return node.width;
19+
if ('measured' in node && node.measured?.width) return node.measured.width;
20+
return DEFAULT_NODE_WIDTH;
21+
};

0 commit comments

Comments
 (0)