Skip to content

Commit 8f513b2

Browse files
authored
Merge pull request #310 from boostcampwm-2024/feature-fe-#294
컨테이너 노드 기능 추가
2 parents 1fdee59 + d14f0b9 commit 8f513b2

File tree

13 files changed

+435
-123
lines changed

13 files changed

+435
-123
lines changed

apps/backend/src/yjs/yjs.service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ export class YjsService
153153
for (const [key, change] of event.changes.keys) {
154154
if (change.action === 'update') {
155155
const node: any = nodesMap.get(key);
156+
if (node.type !== 'note') {
157+
continue;
158+
}
156159
const { title, id } = node.data; // TODO: 이모지 추가
157160
const { x, y } = node.position;
158161
const isHolding = node.isHolding;

apps/frontend/src/features/canvas/model/calculateHandles.ts

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,61 @@
1-
import { Position, Node } from "@xyflow/react";
1+
import { Position, Node, XYPosition } from "@xyflow/react";
22

3-
export const getHandlePosition = (node: Node, handleId: Position) => {
3+
const getAbsolutePosition = (node: Node, nodes: Node[]): XYPosition => {
4+
if (!node.parentId) {
5+
return node.position;
6+
}
7+
8+
const parentNode = nodes.find((n) => n.id === node.parentId);
9+
if (!parentNode) {
10+
return node.position;
11+
}
12+
13+
const parentPosition: XYPosition = getAbsolutePosition(parentNode, nodes);
14+
return {
15+
x: parentPosition.x + node.position.x,
16+
y: parentPosition.y + node.position.y,
17+
};
18+
};
19+
20+
export const getHandlePosition = (
21+
node: Node,
22+
handleId: Position,
23+
nodes: Node[],
24+
) => {
425
const nodeElement = document.querySelector(`[data-id="${node.id}"]`);
526
const nodeRect = nodeElement!.getBoundingClientRect();
627
const nodeWidth = nodeRect.width;
728
const nodeHeight = nodeRect.height;
829

30+
const absolutePosition = getAbsolutePosition(node, nodes);
31+
932
const positions = {
1033
[Position.Left]: {
11-
x: node.position.x,
12-
y: node.position.y + nodeHeight / 2,
34+
x: absolutePosition.x,
35+
y: absolutePosition.y + nodeHeight / 2,
1336
},
1437
[Position.Right]: {
15-
x: node.position.x + nodeWidth,
16-
y: node.position.y + nodeHeight / 2,
38+
x: absolutePosition.x + nodeWidth,
39+
y: absolutePosition.y + nodeHeight / 2,
1740
},
1841
[Position.Top]: {
19-
x: node.position.x + nodeWidth / 2,
20-
y: node.position.y,
42+
x: absolutePosition.x + nodeWidth / 2,
43+
y: absolutePosition.y,
2144
},
2245
[Position.Bottom]: {
23-
x: node.position.x + nodeWidth / 2,
24-
y: node.position.y + nodeHeight,
46+
x: absolutePosition.x + nodeWidth / 2,
47+
y: absolutePosition.y + nodeHeight,
2548
},
2649
};
2750

2851
return positions[handleId];
2952
};
3053

31-
export const calculateBestHandles = (sourceNode: Node, targetNode: Node) => {
54+
export const calculateBestHandles = (
55+
sourceNode: Node,
56+
targetNode: Node,
57+
nodes: Node[],
58+
) => {
3259
const handlePositions = [
3360
Position.Left,
3461
Position.Right,
@@ -42,9 +69,9 @@ export const calculateBestHandles = (sourceNode: Node, targetNode: Node) => {
4269
};
4370

4471
handlePositions.forEach((sourceHandle) => {
45-
const sourcePosition = getHandlePosition(sourceNode, sourceHandle);
72+
const sourcePosition = getHandlePosition(sourceNode, sourceHandle, nodes);
4673
handlePositions.forEach((targetHandle) => {
47-
const targetPosition = getHandlePosition(targetNode, targetHandle);
74+
const targetPosition = getHandlePosition(targetNode, targetHandle, nodes);
4875
const distance = Math.hypot(
4976
sourcePosition.x - targetPosition.x,
5077
sourcePosition.y - targetPosition.y,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { type Node } from "@xyflow/react";
2+
3+
export const getRelativePosition = (node: Node, parentNode: Node) => ({
4+
x: node.position.x - parentNode.position.x,
5+
y: node.position.y - parentNode.position.y,
6+
});
7+
8+
export const getAbsolutePosition = (node: Node, parentNode: Node) => ({
9+
x: parentNode.position.x + node.position.x,
10+
y: parentNode.position.y + node.position.y,
11+
});

0 commit comments

Comments
 (0)