From 6030a78b919153671075dd3dd05ac82e0d18d717 Mon Sep 17 00:00:00 2001 From: Jiiks Date: Thu, 28 Feb 2019 06:40:33 +0200 Subject: [PATCH] Delete and rename handlers --- core/src/main.js | 22 ++++++++++++++++++++++ core/src/modules/utils.js | 22 ++++++++++++++++++++++ editor/src/Editor.vue | 27 +++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/core/src/main.js b/core/src/main.js index 204255ec..6a6eab7d 100644 --- a/core/src/main.js +++ b/core/src/main.js @@ -112,6 +112,28 @@ class Comms { BDIpc.on('bd-getPath', (event, paths) => { event.reply(path.resolve(this.bd.config.getPath(paths[0]), ...paths.splice(1))); }); + + BDIpc.on('bd-rmFile', async (event, paths) => { + const fullPath = path.resolve(this.bd.config.getPath(paths[0]), ...paths.splice(1)); + try { + await FileUtils.rm(fullPath); + event.reply('ok'); + } catch (err) { + event.reject(err); + } + }); + + BDIpc.on('bd-rnFile', async (event, paths) => { + const oldPath = path.resolve(this.bd.config.getPath(paths.oldName[0]), ...paths.oldName.splice(1)); + const newPath = path.resolve(this.bd.config.getPath(paths.newName[0]), ...paths.newName.splice(1)); + + try { + await FileUtils.rn(oldPath, newPath); + event.reply('ok'); + } catch (err) { + event.reject(err); + } + }); } async send(channel, message) { diff --git a/core/src/modules/utils.js b/core/src/modules/utils.js index 4dd062e2..66586e7f 100644 --- a/core/src/modules/utils.js +++ b/core/src/modules/utils.js @@ -11,6 +11,7 @@ // TODO Use common import fs from 'fs'; +import rimraf from 'rimraf'; import Module from './modulebase'; import BDIpc from './bdipc'; @@ -164,6 +165,27 @@ export class FileUtils { } } } + + static async rm(path) { + return new Promise((resolve, reject) => { + rimraf(path, err => { + if (err) { + console.log(err); + reject(err); + } + resolve(); + }); + }); + } + + static async rn(oldPath, newPath) { + return new Promise((resolve, reject) => { + fs.rename(oldPath, newPath, err => { + if (err) return reject(err); + resolve(); + }); + }); + } } export class WindowUtils extends Module { diff --git a/editor/src/Editor.vue b/editor/src/Editor.vue index f8d325ad..54b7d05f 100644 --- a/editor/src/Editor.vue +++ b/editor/src/Editor.vue @@ -214,6 +214,33 @@ remote.clipboard.writeText(fullPath); return; } + + if (action === 'rename') { // TODO select correct file after + this.loading = true; + const { oldName, newName } = item; + + try { + await ClientIPC.send('rnFile', { oldName: ['userfiles', oldName], newName: ['userfiles', newName] }); + } catch (err) { + console.log(err); + } finally { + this.loading = false; + } + + return; + } + + if (action === 'delete') { + this.loading = true; + try { + await ClientIPC.send('rmFile', ['userfiles', item.name]); + } catch (err) { + console.log(err); + } finally { + this.loading = false; + } + return; + } }, toggleLiveUpdate(item) {