Skip to content

Commit 0fcc1fd

Browse files
committed
feat: YDoc에 title, emoji 초기 세팅
1 parent 2066453 commit 0fcc1fd

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class YjsService
100100
const nodes = await this.nodeService.findNodes();
101101
const edges = await this.edgeService.findEdges();
102102
const nodesMap = doc.getMap('nodes');
103-
const title = doc.getText('title');
103+
const title = doc.getMap('title');
104104
const emoji = doc.getText('emoji');
105105
const edgesMap = doc.getMap('edges');
106106

@@ -130,6 +130,7 @@ export class YjsService
130130
});
131131
// node의 변경 사항을 감지한다.
132132
nodesMap.observe(async (event) => {
133+
console.log('노드 개수', event.changes.keys);
133134
for (const [key, change] of event.changes.keys) {
134135
if (change.action === 'update') {
135136
const node: any = nodesMap.get(key);
@@ -166,7 +167,7 @@ export class YjsService
166167
}
167168

168169
// YMap에 노드 정보를 넣어준다.
169-
initializeYNodeMap(nodes: Node[], yMap: Y.Map<unknown>): void {
170+
initializeYNodeMap(nodes: Node[], yMap: Y.Map<Object>): void {
170171
nodes.forEach((node) => {
171172
const nodeId = node.id.toString(); // id를 string으로 변환
172173

@@ -187,11 +188,26 @@ export class YjsService
187188
dragging: true,
188189
isHolding: false,
189190
});
191+
192+
// Y.Text title에 데이터 삽입
193+
const pageId = node.page.id.toString(); // id를 string으로 변환
194+
const yTitleText = new Y.Text();
195+
yTitleText.insert(0, node.page.title);
196+
// Y.Map에 데이터를 삽입
197+
yMap.set(`title_${pageId}`, yTitleText);
198+
199+
// Y.Text emoji에 데이터 삽입
200+
const yEmojiText = new Y.Text();
201+
const emoji = node.page.emoji ?? '📄';
202+
console.log(node.page);
203+
yEmojiText.insert(0, emoji);
204+
// Y.Map에 데이터를 삽입
205+
yMap.set(`emoji_${pageId}`, yEmojiText);
190206
});
191207
}
192208

193209
// yMap에 edge 정보를 넣어준다.
194-
initializeYEdgeMap(edges: Edge[], yMap: Y.Map<unknown>): void {
210+
initializeYEdgeMap(edges: Edge[], yMap: Y.Map<Object>): void {
195211
edges.forEach((edge) => {
196212
const edgeId = edge.id.toString(); // id를 string으로 변환
197213

@@ -207,7 +223,7 @@ export class YjsService
207223
}
208224

209225
// yXmlFragment에 content를 넣어준다.
210-
initializePageContent(content: object, yXmlFragment: Y.XmlFragment) {
226+
initializePageContent(content: Object, yXmlFragment: Y.XmlFragment) {
211227
prosemirrorJSONToYXmlFragment(novelEditorSchema, content, yXmlFragment);
212228
}
213229

0 commit comments

Comments
 (0)