Skip to content

Commit b4d1c63

Browse files
djk01281yewonJin
andcommitted
fix: 여러 에디터에서 동시 편집 가능하게 수정
Co-authored-by: 진예원 <[email protected]>
1 parent 5816b42 commit b4d1c63

File tree

6 files changed

+43
-29
lines changed

6 files changed

+43
-29
lines changed

backend/src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ async function bootstrap() {
88
const app = await NestFactory.create(AppModule);
99
app.useWebSocketAdapter(new WsAdapter(app));
1010
app.useGlobalFilters(new HttpExceptionFilter());
11-
11+
app.enableCors();
12+
1213
const config = new DocumentBuilder()
1314
.setTitle('OctoDocs')
1415
.setDescription('OctoDocs API 명세서')

frontend/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
22

33
import Sidebar from "./components/sidebar";
44
import HoverTrigger from "./components/HoverTrigger";
5-
import EditorView from "./components/editor/EditorView";
5+
import EditorView from "./components/EditorView";
66
import SideWrapper from "./components/layout/SideWrapper";
77
import Canvas from "./components/canvas";
88

frontend/src/components/editor/EditorView.tsx renamed to frontend/src/components/EditorView.tsx

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
import usePageStore from "@/store/usePageStore";
2-
import Editor from ".";
2+
import Editor from "./editor";
33
import {
44
usePage,
55
useUpdatePage,
66
useOptimisticUpdatePage,
77
} from "@/hooks/usePages";
8-
import EditorLayout from "../layout/EditorLayout";
9-
import EditorTitle from "./EditorTitle";
8+
import EditorLayout from "./layout/EditorLayout";
9+
import EditorTitle from "./editor/EditorTitle";
1010
import { EditorInstance } from "novel";
1111
import { useEffect, useRef, useState } from "react";
12-
import SaveStatus from "./ui/SaveStatus";
1312
import { useDebouncedCallback } from "use-debounce";
1413
import { WebsocketProvider } from "y-websocket";
1514
import * as Y from "yjs";
15+
import SaveStatus from "./editor/ui/SaveStatus";
1616

1717
export default function EditorView() {
1818
const { currentPage } = usePageStore();
1919
const { page, isLoading } = usePage(currentPage);
20+
const [editorKey, setEditorKey] = useState(0);
21+
const [saveStatus, setSaveStatus] = useState<"saved" | "unsaved">("saved");
2022

2123
const ydoc = useRef<Y.Doc>();
2224
const provider = useRef<WebsocketProvider>();
2325

2426
useEffect(() => {
25-
if (!currentPage) return;
27+
if (currentPage === null) return;
28+
29+
if (provider.current) {
30+
provider.current.disconnect();
31+
provider.current.destroy();
32+
}
33+
if (ydoc.current) {
34+
ydoc.current.destroy();
35+
}
2636

2737
const doc = new Y.Doc();
2838
const wsProvider = new WebsocketProvider(
@@ -33,6 +43,14 @@ export default function EditorView() {
3343

3444
ydoc.current = doc;
3545
provider.current = wsProvider;
46+
47+
setEditorKey((prev) => prev + 1);
48+
49+
return () => {
50+
wsProvider.disconnect();
51+
wsProvider.destroy();
52+
doc.destroy();
53+
};
3654
}, [currentPage]);
3755

3856
const pageTitle = page?.title ?? "제목없음";
@@ -43,9 +61,9 @@ export default function EditorView() {
4361
id: currentPage ?? 0,
4462
});
4563

46-
const [saveStatus, setSaveStatus] = useState<"saved" | "unsaved">("saved");
47-
4864
const handleTitleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
65+
if (currentPage === null) return;
66+
4967
setSaveStatus("unsaved");
5068

5169
optimisticUpdatePageMutation.mutate(
@@ -96,7 +114,7 @@ export default function EditorView() {
96114
<SaveStatus saveStatus={saveStatus} />
97115
<EditorTitle title={pageTitle} onTitleChange={handleTitleChange} />
98116
<Editor
99-
key={currentPage}
117+
key={editorKey}
100118
initialContent={pageContent}
101119
pageId={currentPage}
102120
ydoc={ydoc.current}

frontend/src/components/editor/EditorTitle.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default function EditorTitle({
77
title,
88
onTitleChange,
99
}: EditorTitleProps) {
10+
console.log(title);
1011
return (
1112
<div className="p-12 pb-0">
1213
<input

frontend/src/components/editor/index.tsx

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ interface EditorProp {
3939
provider: WebsocketProvider;
4040
}
4141

42-
const Editor = ({
43-
initialContent,
44-
onEditorUpdate,
45-
ydoc,
46-
provider,
47-
}: EditorProp) => {
42+
const Editor = ({ onEditorUpdate, ydoc, provider }: EditorProp) => {
4843
const [openNode, setOpenNode] = useState(false);
4944
const [openColor, setOpenColor] = useState(false);
5045
const [openLink, setOpenLink] = useState(false);
@@ -57,19 +52,17 @@ const Editor = ({
5752
onContentError={({ disableCollaboration }) => {
5853
disableCollaboration();
5954
}}
60-
onCreate={({ editor: currentEditor }) => {
61-
provider.on("synced", () => {
62-
console.log(ydoc);
63-
64-
if (
65-
!ydoc.getMap("config").get("initialContentLoaded") &&
66-
currentEditor
67-
) {
68-
ydoc.getMap("config").set("initialContentLoaded", true);
69-
currentEditor.commands.setContent(initialContent as JSONContent);
70-
}
71-
});
72-
}}
55+
// onCreate={({ editor: currentEditor }) => {
56+
// provider.on("synced", () => {
57+
// if (
58+
// !ydoc.getMap("config").get("initialContentLoaded") &&
59+
// currentEditor
60+
// ) {
61+
// ydoc.getMap("config").set("initialContentLoaded", true);
62+
// ydoc.getMap("content").set("content", initialContent);
63+
// }
64+
// });
65+
// }}
7366
extensions={[
7467
...extensions,
7568
Collaboration.extend().configure({

frontend/src/hooks/useNoteList.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const useNoteList = () => {
1313
const deleteMutation = useDeletePage();
1414

1515
const handleNoteClick = (id: number) => {
16+
console.log("handleNoteClick", id);
1617
setCurrentPage(id);
1718
};
1819

0 commit comments

Comments
 (0)