Skip to content

Commit f80691e

Browse files
authored
fix/COMPASS-9664 fix default height calc (#105)
1 parent 4e0885f commit f80691e

File tree

6 files changed

+35
-22
lines changed

6 files changed

+35
-22
lines changed

src/components/edge/floating-edge.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('floating-edge', () => {
5757
expect(path).toHaveAttribute('id', 'orders-to-employees');
5858
expect(path).toHaveAttribute(
5959
'd',
60-
'M269.5 180L289.5 180L 386.5,180Q 391.5,180 391.5,185L 391.5,295Q 391.5,300 386.5,300L371.5 300',
60+
'M267 197.5L267 217.5L 267,240Q 267,245 272,245L 354,245Q 359,245 359,250L359 272.5L359 292.5',
6161
);
6262
});
6363
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('self-referencing-edge', () => {
5353
renderComponent();
5454
const path = screen.getByTestId('self-referencing-edge-employees-to-employees');
5555
expect(path).toHaveAttribute('id', 'employees-to-employees');
56-
expect(path).toHaveAttribute('d', 'M422,292.5L422,262.5L584,262.5L584,350.5L551.5,350.5');
56+
expect(path).toHaveAttribute('d', 'M422,292.5L422,262.5L584,262.5L584,355.5L551.5,355.5');
5757
});
5858
});
5959

src/utilities/apply-layout.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ describe('apply-layout', () => {
6464
...nodes[1],
6565
position: {
6666
x: 12,
67-
y: 76, // 12 + 44 (0 fields height) + 2*10 (padding)
67+
y: 86, // 12 + 44 (0 fields height) + 2*10 (padding) + 10 (extra padding)
6868
},
6969
}),
7070
expect.objectContaining({
7171
...nodes[2],
7272
position: {
7373
x: 12,
74-
y: 140, // 76 + 44 (0 fields height) + 2*10 (padding)
74+
y: 160, // 86 + 44 (0 fields height) + 2*10 (padding) + 10 (extra padding)
7575
},
7676
}),
7777
]);
@@ -94,14 +94,14 @@ describe('apply-layout', () => {
9494
...nodesWithOneField[1],
9595
position: {
9696
x: 12,
97-
y: 94, // 12 + 62 (1 field height) + 2*10 (padding)
97+
y: 104, // 12 + 62 (1 field height) + 2*10 (padding) + 10 (extra padding)
9898
},
9999
}),
100100
expect.objectContaining({
101101
...nodesWithOneField[2],
102102
position: {
103103
x: 12,
104-
y: 176, // 94 + 62 (1 field height) + 2*10 (padding)
104+
y: 196, // 104 + 62 (1 field height) + 2*10 (padding) + 10 (extra padding)
105105
},
106106
}),
107107
]);
@@ -124,14 +124,14 @@ describe('apply-layout', () => {
124124
...baseNodes[1],
125125
position: {
126126
x: 12,
127-
y: 94, // 12 + 62 (default height) + 2*10 (padding)
127+
y: 104, // 12 + 62 (default height) + 2*10 (padding) + 10 (extra padding)
128128
},
129129
}),
130130
expect.objectContaining({
131131
...baseNodes[2],
132132
position: {
133133
x: 12,
134-
y: 176, // 94 + 62 (default height) + 2*10 (padding)
134+
y: 196, // 104 + 62 (default height) + 2*10 (padding) + 10 (extra padding)
135135
},
136136
}),
137137
]);
@@ -188,7 +188,7 @@ describe('apply-layout', () => {
188188
id: '1',
189189
position: {
190190
x: 12,
191-
y: 76,
191+
y: 86,
192192
},
193193
}),
194194
expect.objectContaining({
@@ -208,7 +208,7 @@ describe('apply-layout', () => {
208208
id: '3',
209209
position: {
210210
x: 12,
211-
y: 220,
211+
y: 240,
212212
},
213213
}),
214214
]);

src/utilities/get-edge-params.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ describe('get-edge-params', () => {
1010
{ ...EMPLOYEES_NODE, data: { title: EMPLOYEES_NODE.title, fields: EMPLOYEES_NODE.fields } },
1111
);
1212
expect(result).toEqual({
13-
sourcePos: 'right',
14-
sx: 269.5,
15-
sy: 180,
16-
targetPos: 'right',
17-
tx: 371.5,
18-
ty: 300,
13+
sourcePos: 'bottom',
14+
sx: 267,
15+
sy: 197.5,
16+
targetPos: 'top',
17+
tx: 359,
18+
ty: 292.5,
1919
});
2020
});
2121
});

src/utilities/get-edge-params.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ const getEdgePosition = (node: InternalNode, intersectionPoint: XYPosition) => {
5454
if (px <= nx + 1) {
5555
return Position.Left;
5656
}
57-
if (px >= nx + (n.measured?.width ?? 0) - 1) {
57+
if (px >= nx + (getNodeWidth(n) - 1)) {
5858
return Position.Right;
5959
}
6060
if (py <= ny + 1) {
6161
return Position.Top;
6262
}
63-
if (py >= n.y + (n.measured?.height ?? 0) - 1) {
63+
if (py >= n.y + (getNodeHeight(n) - 1)) {
6464
return Position.Bottom;
6565
}
6666

src/utilities/node-dimensions.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { BaseNode } from '@/types';
1+
import type { BaseNode, NodeProps } from '@/types';
2+
import { InternalNode } from '@/types/internal';
23

34
import {
45
DEFAULT_FIELD_HEIGHT,
@@ -7,11 +8,23 @@ import {
78
DEFAULT_NODE_WIDTH,
89
} from './constants';
910

10-
export const getNodeHeight = <N extends BaseNode>(node: N) => {
11+
export const getNodeHeight = <N extends BaseNode | NodeProps | InternalNode>(node: N) => {
1112
if ('height' in node && typeof node.height === 'number') return node.height;
1213
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;
14+
15+
let fieldCount = 1;
16+
if ('fields' in node && Array.isArray(node.fields)) {
17+
fieldCount = node.fields.length;
18+
}
19+
if ('data' in node) {
20+
let internalNode = node as InternalNode;
21+
if (internalNode.data?.fields && Array.isArray(internalNode.data.fields)) {
22+
fieldCount = internalNode.data.fields.length;
23+
}
24+
}
25+
const calculatedHeight =
26+
DEFAULT_NODE_HEADER_HEIGHT + DEFAULT_FIELD_PADDING * 2 + fieldCount * DEFAULT_FIELD_HEIGHT + 10;
27+
return calculatedHeight;
1528
};
1629

1730
export const getNodeWidth = <N extends BaseNode>(node: N) => {

0 commit comments

Comments
 (0)