Skip to content

Commit 2ec262b

Browse files
committed
feat: title 변경 시 노드 및 페이지에서 변경 반영
1 parent b5017a8 commit 2ec262b

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

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

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { usePages } from "@/features/pageSidebar/api/usePages";
1717
import useYDocStore from "@/shared/model/ydocStore";
1818
import { calculateBestHandles } from "@/features/canvas/model/calculateHandles";
1919
import { createSocketIOProvider } from "@/shared/api/socketProvider";
20-
import { initializeYText } from "@/shared/model/yjs";
2120
import { useCollaborativeCursors } from "./useCollaborativeCursors";
2221
import { getSortedNodes } from "./sortNodes";
2322

@@ -43,16 +42,51 @@ export const useCanvas = () => {
4342
const holdingNodeRef = useRef<string | null>(null);
4443

4544
useEffect(() => {
46-
if (!pages) return;
47-
4845
const yTitleMap = ydoc.getMap("title");
4946
const yEmojiMap = ydoc.getMap("emoji");
5047

51-
pages.forEach((page) => {
52-
initializeYText(yTitleMap, `title_${page.id}`, page.title);
53-
initializeYText(yEmojiMap, `emoji_${page.id}`, page.emoji || "");
48+
const nodesMap = ydoc.getMap("nodes");
49+
50+
yTitleMap.observeDeep((event) => {
51+
if (!event[0].path.length) return;
52+
53+
const pageId = event[0].path[0].toString().split("_")[1];
54+
const value = event[0].target.toString();
55+
56+
const existingNode = nodesMap.get(pageId) as YNode;
57+
58+
const newNode: YNode = {
59+
id: pageId,
60+
type: "note",
61+
data: { title: value, id: pageId, emoji: existingNode.data.emoji },
62+
position: existingNode.position,
63+
selected: false,
64+
isHolding: false,
65+
};
66+
67+
nodesMap.set(pageId, newNode);
68+
});
69+
70+
yEmojiMap.observeDeep((event) => {
71+
if (!event[0].path.length) return;
72+
73+
const pageId = event[0].path[0].toString().split("_")[1];
74+
const value = event[0].target.toString();
75+
76+
const existingNode = nodesMap.get(pageId) as YNode;
77+
78+
const newNode: YNode = {
79+
id: pageId,
80+
type: "note",
81+
data: { title: existingNode.data.title, id: pageId, emoji: value },
82+
position: existingNode.position,
83+
selected: false,
84+
isHolding: false,
85+
};
86+
87+
nodesMap.set(pageId, newNode);
5488
});
55-
}, [pages]);
89+
}, []);
5690

5791
useEffect(() => {
5892
if (!ydoc) return;

apps/frontend/src/features/pageSidebar/ui/Tools/index.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import Button from "@/shared/ui/Button";
44

55
import { useCreatePage, usePages } from "@/features/pageSidebar/api/usePages";
66
import { usePageStore } from "../../model/pageStore";
7+
import useYDocStore from "@/shared/model/ydocStore";
8+
import { initializeYText } from "@/shared/model";
79

810
export function Tools() {
911
const { setCurrentPage } = usePageStore();
1012
const { pages } = usePages();
1113
const createMutation = useCreatePage();
14+
const { ydoc } = useYDocStore();
1215

1316
return (
1417
<Button
@@ -30,7 +33,15 @@ export function Tools() {
3033
y: 0,
3134
emoji: null,
3235
})
33-
.then((res) => setCurrentPage(res.pageId));
36+
.then((res) => {
37+
setCurrentPage(res.pageId);
38+
39+
const yTitleMap = ydoc.getMap("title");
40+
const yEmojiMap = ydoc.getMap("emoji");
41+
42+
initializeYText(yTitleMap, `title_${res.pageId}`, "제목 없음");
43+
initializeYText(yEmojiMap, `emoji_${res.pageId}`, "");
44+
});
3445
}}
3546
>
3647
<div>

0 commit comments

Comments
 (0)