Skip to content

Commit 40c351d

Browse files
committed
♻️ Wrapper 분기 리팩토링: 책임을 EditorNodeWrapper에서 Canvas로 이동
1 parent d7fd4ed commit 40c351d

File tree

2 files changed

+45
-53
lines changed

2 files changed

+45
-53
lines changed

apps/editor/src/components/editor/Canvas.tsx

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { useDragStore } from "@/stores/useDragStore";
44
import {
55
useAddItemToStack,
66
useCanvas,
7+
useChildrenMap,
78
useClearNode,
8-
useCurNodes,
9+
useNodeMap,
910
useSelectedNodeId,
1011
useSelectNode,
1112
useSetCanvas,
@@ -19,29 +20,30 @@ import {
1920
import handleWheel from "@/utils/editor/handleWheel";
2021
import { DragProvider } from "@repo/ui/context/dragContext";
2122
import EditorNodeWrapper from "@repo/ui/core/EditorNodeWrapper";
23+
import FlowNodeWrapper from "@repo/ui/core/FlowNodeWrapper";
2224
import NodeRenderer from "@repo/ui/core/NodeRenderer";
2325
import SelectionOverlay from "@repo/ui/core/SelectionOverlay";
2426
import { WcxNode } from "@repo/ui/types/nodes";
2527
import React, { useRef } from "react";
2628

2729
export default function Canvas() {
28-
const nodes = useCurNodes();
2930
const selectedNodeId = useSelectedNodeId();
3031
const selectNode = useSelectNode();
3132
const updateNode = useUpdateNodeLayout();
3233
const canvasState = useCanvas();
3334
const setCanvas = useSetCanvas();
3435
const clearNode = useClearNode();
3536
const addItemToStack = useAddItemToStack();
37+
const nodeMap = useNodeMap();
38+
const childrenMap = useChildrenMap();
3639

3740
//TODO-이거 뭐임?
3841
const isPanning = useRef(false);
3942
const lastMousePos = useRef({ x: 0, y: 0 });
4043

4144
function getParentNode(parentId: string | null): WcxNode | undefined {
4245
if (!parentId) return undefined;
43-
44-
return nodes?.find((n) => n.id === parentId);
46+
return nodeMap[parentId];
4547
}
4648

4749
//FIXME-각 노드들에 key속성 추가해주기. -> 리액트 경고 발생
@@ -54,28 +56,14 @@ export default function Canvas() {
5456
* parentNode만 주면 NodeTree함수가 알아서 ParentNode의 자식 노드 객체 배열(WcxNode[])을 찾아준다.
5557
*/
5658
function renderTree(parentNode: WcxNode | { id: null }) {
57-
//parentNode의 자식 찾기
58-
const childrenObjArr = nodes
59-
?.filter(({ parent_id }) => parent_id === parentNode.id)
60-
.sort((a, b) => a.position - b.position);
59+
// parentNode의 자식 찾기 (O(1) lookup via childrenMap)
60+
const childrenObjArr = childrenMap[parentNode.id ?? "__root__"];
6161

6262
//BaseCondition
6363
//FIXME-솔직히 !childrenArr만 있어도 될듯? 길이가 0일 수가 없다.
6464
if (!childrenObjArr || childrenObjArr.length === 0) {
6565
if (parentNode.id === null) return;
66-
return (
67-
<EditorNodeWrapper
68-
node={parentNode}
69-
parentNode={getParentNode(parentNode.parent_id)}
70-
selectedId={selectedNodeId}
71-
updateNode={updateNode}
72-
selectNode={selectNode}
73-
canvas={canvasState}
74-
addItemToStack={addItemToStack}
75-
>
76-
<NodeRenderer node={parentNode} />
77-
</EditorNodeWrapper>
78-
);
66+
return renderWrappedNode(parentNode, <NodeRenderer node={parentNode} />);
7967
}
8068

8169
// 1. 자식들의 렌더링 결과물 (JSX 배열)
@@ -89,17 +77,48 @@ export default function Canvas() {
8977
}
9078

9179
// 3. 일반 노드인 경우 -> Wrapper + NodeRenderer + children
80+
return renderWrappedNode(
81+
parentNode,
82+
<NodeRenderer node={parentNode}>{children}</NodeRenderer>,
83+
);
84+
}
85+
86+
/**
87+
* 노드 유형에 따라 적절한 래퍼를 선택하여 렌더링합니다.
88+
* - Stack 내부 flow 아이템 → FlowNodeWrapper (Rnd 미사용, CSS 기반 크기)
89+
* - 그 외 → EditorNodeWrapper (Rnd 기반 드래그/리사이즈)
90+
*/
91+
function renderWrappedNode(node: WcxNode, content: React.ReactNode) {
92+
const parent = getParentNode(node.parent_id);
93+
const isFlowItem =
94+
parent?.type === "Stack" && node.style.position === "relative";
95+
96+
if (isFlowItem) {
97+
return (
98+
<FlowNodeWrapper
99+
node={node}
100+
parentNode={parent}
101+
selectedId={selectedNodeId}
102+
updateNode={updateNode}
103+
selectNode={selectNode}
104+
canvas={canvasState}
105+
>
106+
{content}
107+
</FlowNodeWrapper>
108+
);
109+
}
110+
92111
return (
93112
<EditorNodeWrapper
94-
node={parentNode}
95-
parentNode={getParentNode(parentNode.parent_id)}
113+
node={node}
114+
parentNode={parent}
96115
selectedId={selectedNodeId}
97116
updateNode={updateNode}
98117
selectNode={selectNode}
99118
canvas={canvasState}
100119
addItemToStack={addItemToStack}
101120
>
102-
<NodeRenderer node={parentNode}>{children}</NodeRenderer>
121+
{content}
103122
</EditorNodeWrapper>
104123
);
105124
}

packages/ui/src/core/EditorNodeWrapper.tsx

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { useState } from "react";
55
import { Rnd } from "react-rnd";
66
import { WcxNode } from "types";
77
import { CanvasState, Layer } from "types/rnd";
8-
import FlowNodeWrapper from "./FlowNodeWrapper";
98

109
interface WrapperProps {
1110
children: React.ReactNode;
@@ -53,24 +52,7 @@ export default function EditorNodeWrapper({
5352
const setDraggingId = useDragStore((s) => s.setDraggingId);
5453
const setHoveredStackId = useDragStore((s) => s.setHoveredStackId);
5554

56-
// Stack 내부 flow 아이템은 FlowNodeWrapper로 렌더링
57-
const isFlowItem = isStackItem && hasRelativePosition;
58-
if (isFlowItem) {
59-
return (
60-
<FlowNodeWrapper
61-
node={node}
62-
parentNode={parentNode}
63-
selectedId={selectedId}
64-
updateNode={updateNode}
65-
selectNode={selectNode}
66-
canvas={canvas}
67-
>
68-
{children}
69-
</FlowNodeWrapper>
70-
);
71-
}
7255

73-
const isSwitchItems = isStackItem && hasRelativePosition;
7456

7557
const wrapperStyle: React.CSSProperties = {
7658
cursor: "move",
@@ -162,17 +144,8 @@ export default function EditorNodeWrapper({
162144
`드래그 종료시 노드의 좌표 x:${d.node.offsetLeft}, y:${d.node.offsetTop}`,
163145
);
164146

165-
if (hasRelativePosition) {
166-
console.log("relative position");
167-
return;
168-
}
169-
170-
//드래그가 종료될 경우에 외부 아이템이 스택으로 들어오는경우,스택 내부의 아이템이 이동할 경우(포지션 앱솔루트), 내부 아이템끼리 위치 이동할 경우,기본 위치 이동을 생각해야한다.
171-
if (isSwitchItems) {
172-
//현재 놓인 Y위치에 따라서 노드의 순서 변경을 고려해야한다.
173-
//TODO-Stack내부에서 Item 노드의 순서 변경 로직 실행
174-
} else if (stackId && node.parent_id !== stackId) {
175-
//스택 외부의 노드가 스택 안으로 새롭게 들어오는 경우에만 해당이 된다.
147+
// 드래그 종료: 스택에 드롭 vs 위치 이동
148+
if (stackId && node.parent_id !== stackId) {
176149
addItemToStack(id, stackId);
177150
} else {
178151
updateNode(id, { x: d.x, y: d.y });

0 commit comments

Comments
 (0)