Skip to content

Commit 23a154f

Browse files
authored
Feat/#221-B: 회의록 붙여넣기 기능 구현 (#233)
1 parent 6e161ac commit 23a154f

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

client/src/components/Mom/Block/index.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,13 @@ function Block({ id, onKeyDown, index }: BlockProps) {
127127
};
128128

129129
ee.on(`${SOCKET_MESSAGE.BLOCK.INIT}-${id}`, onInitialize);
130+
ee.on(`${SOCKET_MESSAGE.BLOCK.UPDATE_TEXT}-${id}`, onInitialize);
130131
ee.on(`${SOCKET_MESSAGE.BLOCK.INSERT_TEXT}-${id}`, onInsert);
131132
ee.on(`${SOCKET_MESSAGE.BLOCK.DELETE_TEXT}-${id}`, onDelete);
132133

133134
return () => {
134135
ee.off(`${SOCKET_MESSAGE.BLOCK.INIT}-${id}`, onInitialize);
136+
ee.off(`${SOCKET_MESSAGE.BLOCK.UPDATE_TEXT}-${id}`, onInitialize);
135137
ee.off(`${SOCKET_MESSAGE.BLOCK.INSERT_TEXT}-${id}`, onInsert);
136138
ee.off(`${SOCKET_MESSAGE.BLOCK.DELETE_TEXT}-${id}`, onDelete);
137139
};
@@ -158,6 +160,26 @@ function Block({ id, onKeyDown, index }: BlockProps) {
158160

159161
const onPaste: React.ClipboardEventHandler<HTMLParagraphElement> = (e) => {
160162
e.preventDefault();
163+
164+
setOffset();
165+
if (offsetRef.current === null || !blockRef.current) return;
166+
167+
let previousLetterIndex = offsetRef.current - 1;
168+
const previousText = blockRef.current.innerText.slice(
169+
0,
170+
previousLetterIndex + 1,
171+
);
172+
const nextText = blockRef.current.innerText.slice(previousLetterIndex + 1);
173+
174+
const pastedText = e.clipboardData.getData('text/plain').replace('\n', '');
175+
const remoteInsertions = pastedText
176+
.split('')
177+
.map((letter) => localInsertCRDT(previousLetterIndex++, letter));
178+
179+
socket.emit(SOCKET_MESSAGE.BLOCK.UPDATE_TEXT, id, remoteInsertions);
180+
181+
blockRef.current.innerText = previousText + pastedText + nextText;
182+
updateCaretPosition(pastedText.length);
161183
};
162184

163185
const onKeyDownComposite: React.KeyboardEventHandler<HTMLParagraphElement> = (

client/src/components/Mom/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ function Mom() {
108108
ee.emit(`${SOCKET_MESSAGE.BLOCK.DELETE_TEXT}-${id}`, op);
109109
});
110110

111+
socket.on(SOCKET_MESSAGE.BLOCK.UPDATE_TEXT, (id, crdt) => {
112+
ee.emit(`${SOCKET_MESSAGE.BLOCK.UPDATE_TEXT}-${id}`, crdt);
113+
});
114+
111115
return () => {
112116
[
113117
SOCKET_MESSAGE.MOM.INIT,

client/src/constants/socket-message.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const SOCKET_MESSAGE = {
1515
INIT: 'init-block',
1616
INSERT_TEXT: 'insert-text',
1717
DELETE_TEXT: 'delete-text',
18+
UPDATE_TEXT: 'update-text',
1819
},
1920
WORKSPACE: {
2021
START_MEETING: 'start-meeting',

server/constants/socket-message.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const SOCKET_MESSAGE = {
1515
INIT: 'init-block',
1616
INSERT_TEXT: 'insert-text',
1717
DELETE_TEXT: 'delete-text',
18+
UPDATE_TEXT: 'update-text',
1819
},
1920
WORKSPACE: {
2021
START_MEETING: 'start-meeting',

server/socket/mom.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ async function momSocketServer(io: Server) {
103103
socket.to(momId).emit(SOCKET_MESSAGE.BLOCK.DELETE_TEXT, blockId, op);
104104
});
105105

106+
socket.on(SOCKET_MESSAGE.BLOCK.UPDATE_TEXT, async (blockId, ops) => {
107+
const momId = socket.data.momId;
108+
109+
for await (const op of ops) {
110+
await crdtManager.onInsertText(blockId, op);
111+
}
112+
113+
const blockCrdt = await crdtManager.getBlockCRDT(blockId);
114+
115+
socket
116+
.to(momId)
117+
.emit(SOCKET_MESSAGE.BLOCK.UPDATE_TEXT, blockId, blockCrdt.data);
118+
});
119+
106120
addEventHandlersForQuestionBlock(workspace, socket);
107121

108122
/* 투표 관련 이벤트 */

0 commit comments

Comments
 (0)