start file saving logic

This commit is contained in:
Ishaan Dey 2024-04-27 16:41:25 -04:00
parent d941e2c056
commit d6769f3407
3 changed files with 40 additions and 1 deletions

View File

@ -73,6 +73,12 @@ io.on("connection", (socket) => __awaiter(void 0, void 0, void 0, function* () {
console.log("file " + file.id + ": ", file.data); console.log("file " + file.id + ": ", file.data);
callback(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* () { socket.on("renameFile", (fileId, newName) => __awaiter(void 0, void 0, void 0, function* () {
const file = sandboxFiles.fileData.find((f) => f.id === fileId); const file = sandboxFiles.fileData.find((f) => f.id === fileId);
if (!file) if (!file)

View File

@ -78,6 +78,12 @@ io.on("connection", async (socket) => {
console.log("file " + file.id + ": ", file.data) console.log("file " + file.id + ": ", file.data)
callback(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) => { socket.on("renameFile", async (fileId: string, newName: string) => {
const file = sandboxFiles.fileData.find((f) => f.id === fileId) const file = sandboxFiles.fileData.find((f) => f.id === fileId)
if (!file) return if (!file) return

View File

@ -49,9 +49,34 @@ export default function CodeEditor({
`http://localhost:4000?userId=${userId}&sandboxId=${sandboxId}` `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 // connection/disconnection effect
useEffect(() => { useEffect(() => {
console.log("connecting")
socket.connect() socket.connect()
return () => { return () => {
@ -80,6 +105,8 @@ export default function CodeEditor({
} }
}, []) }, [])
// ------------
const clerk = useClerk() const clerk = useClerk()
const selectFile = (tab: TTab) => { const selectFile = (tab: TTab) => {