From d6769f3407b32849128128e4d7ff982e07ba34f6 Mon Sep 17 00:00:00 2001 From: Ishaan Dey Date: Sat, 27 Apr 2024 16:41:25 -0400 Subject: [PATCH] start file saving logic --- backend/server/dist/index.js | 6 ++++++ backend/server/src/index.ts | 6 ++++++ frontend/components/editor/index.tsx | 29 +++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/backend/server/dist/index.js b/backend/server/dist/index.js index 6950ecc..2754d1e 100644 --- a/backend/server/dist/index.js +++ b/backend/server/dist/index.js @@ -73,6 +73,12 @@ io.on("connection", (socket) => __awaiter(void 0, void 0, void 0, function* () { console.log("file " + file.id + ": ", file.data); callback(file.data); }); + socket.on("saveFile", (activeId, body, callback) => { + // const file = sandboxFiles.fileData.find((f) => f.id === fileId) + // if (!file) return + // console.log("file " + file.id + ": ", file.data) + // callback(file.data) + }); socket.on("renameFile", (fileId, newName) => __awaiter(void 0, void 0, void 0, function* () { const file = sandboxFiles.fileData.find((f) => f.id === fileId); if (!file) diff --git a/backend/server/src/index.ts b/backend/server/src/index.ts index 6d1b703..4864971 100644 --- a/backend/server/src/index.ts +++ b/backend/server/src/index.ts @@ -78,6 +78,12 @@ io.on("connection", async (socket) => { console.log("file " + file.id + ": ", file.data) callback(file.data) }) + socket.on("saveFile", (activeId: string, body: string, callback) => { + // const file = sandboxFiles.fileData.find((f) => f.id === fileId) + // if (!file) return + // console.log("file " + file.id + ": ", file.data) + // callback(file.data) + }) socket.on("renameFile", async (fileId: string, newName: string) => { const file = sandboxFiles.fileData.find((f) => f.id === fileId) if (!file) return diff --git a/frontend/components/editor/index.tsx b/frontend/components/editor/index.tsx index 29c695c..bf1d42b 100644 --- a/frontend/components/editor/index.tsx +++ b/frontend/components/editor/index.tsx @@ -49,9 +49,34 @@ export default function CodeEditor({ `http://localhost:4000?userId=${userId}&sandboxId=${sandboxId}` ) + useEffect(() => { + const down = (e: KeyboardEvent) => { + if (e.key === "s" && (e.metaKey || e.ctrlKey)) { + e.preventDefault() + + const activeTab = tabs.find((t) => t.id === activeId) + console.log("saving " + activeTab?.name) + + setTabs((prev) => + prev.map((tab) => + tab.id === activeId ? { ...tab, saved: true } : tab + ) + ) + + socket.emit("saveFile", activeId, editorRef.current?.getValue()) + } + } + document.addEventListener("keydown", down) + + return () => { + document.removeEventListener("keydown", down) + } + }, [tabs, activeId]) + + // WS event handlers ------------ + // connection/disconnection effect useEffect(() => { - console.log("connecting") socket.connect() return () => { @@ -80,6 +105,8 @@ export default function CodeEditor({ } }, []) + // ------------ + const clerk = useClerk() const selectFile = (tab: TTab) => {