From 4b5bfb86dd11d7e9a2254be0e2b7a07a25b840c9 Mon Sep 17 00:00:00 2001 From: Slava Ganzin Date: Sat, 29 Jul 2023 15:42:16 +0400 Subject: [PATCH] delete/rename file --- front/State.js | 3 +++ front/index.js | 15 +++++++++++++++ rconf.js | 8 ++++++++ 3 files changed, 26 insertions(+) diff --git a/front/State.js b/front/State.js index 169cc61..90a896b 100644 --- a/front/State.js +++ b/front/State.js @@ -3,6 +3,9 @@ import {Observable} from './observable' window.State = Observable({}) State.files = {} +State.selectFile = file => + setTimeout(() => State.selectedFile = find(x => x.name == file, State.files), 300) + ws.emit('file:list') ws.on('file:list', f => { State.files = f diff --git a/front/index.js b/front/index.js index 4630d7c..d4c6256 100644 --- a/front/index.js +++ b/front/index.js @@ -33,6 +33,7 @@ const onMount = (editor, monaco) => { if (file) { ws.emit('file:save', { value: '', file }) ws.emit('file:list', {}) + State.selectFile(file) } }, }); @@ -46,6 +47,20 @@ const onMount = (editor, monaco) => { }, }); + editor.addAction({ + id: "rename file", + label: "Rename File", + run: function (ed) { + const to = prompt('Enter new filename') + if (to) { + ws.emit('file:rename', {file: State.selectedFile.name, to}) + ws.emit('file:list', {}) + State.selectFile(to) + } + }, + }); + + editor.addAction({ id: "save", diff --git a/rconf.js b/rconf.js index 2088d4c..a7bb2be 100755 --- a/rconf.js +++ b/rconf.js @@ -260,6 +260,14 @@ for (const name of ['Sync', 'Server']) { } +Server.on('file:delete', (ws, {file}) => { + fs.unlink(joinPath(DATADIR, file), console.log) +}) + +Server.on('file:rename', (ws, {file, to}) => { + fs.rename(joinPath(DATADIR, file), joinPath(DATADIR, to), console.log) +}) + Server.on('file:delete', (ws, {file}) => { fs.unlink(joinPath(DATADIR, file), console.log) })