From 04af3e79b5102c7cf5a6c98c973754c90b46c1f2 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Wed, 29 Sep 2021 13:13:12 +0200 Subject: [PATCH] add `Save backup` menu action (saves a backup copy of current session with an incremented suffix appended to the file name) --- CHANGELOG.md | 3 +++ resources/docs/docs/user-interface/client.md | 1 + src/client/locales/en.js | 1 + src/client/locales/fr.js | 1 + src/client/managers/session/index.js | 12 +++++++++--- src/client/ui/main-menu.js | 9 +++++++++ src/server/callbacks.js | 17 +++++++++++++++-- 7 files changed, 39 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2604bac3..b17459a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ - new textarea widget (multi line input) - expose the computed dimensions of canvas-based widgets in `css` (as `--widget-width` and `--widget-height`) and `script` (as `locals.width` and `locals.height`) +- client + - add `Save backup` menu action (saves a backup copy of current session with an incremented suffix appended to the file name) + ## 1.9.14 - bug fixes diff --git a/resources/docs/docs/user-interface/client.md b/resources/docs/docs/user-interface/client.md index 68b31872..b43b31c9 100644 --- a/resources/docs/docs/user-interface/client.md +++ b/resources/docs/docs/user-interface/client.md @@ -8,6 +8,7 @@ - Open recent: open a recent session on the server - Save / Save as: save session file on the server - Fragment mode: when enabled, the session will be saved as a fragment file (only the root's first child will be saved) + - Save backup: save a copy of current session with a incremented suffix appended to the file name - Import: import a session file from the client's filesystem - Export: export a session file to the client's filesystem - State diff --git a/src/client/locales/en.js b/src/client/locales/en.js index 4e7b68a3..3bc8134a 100644 --- a/src/client/locales/en.js +++ b/src/client/locales/en.js @@ -46,6 +46,7 @@ module.exports = { file_open_recent_wait: 'List not loaded yet, please close and reopen the menu', file_save: 'Save', file_save_as: 'Save as...', + file_backup: 'Save backup', file_import: 'Import', file_export: 'Export', diff --git a/src/client/locales/fr.js b/src/client/locales/fr.js index 63815921..7cad1241 100644 --- a/src/client/locales/fr.js +++ b/src/client/locales/fr.js @@ -46,6 +46,7 @@ module.exports = { file_open_recent_wait: 'Liste non chargée, veuillez fermer puis ouvrir le menu à nouveau', file_save: 'Enregistrer', file_save_as: 'Enregistrer sous...', + file_backup: 'Enregistrer une sauvegarde', file_import: 'Importer', file_export: 'Exporter', diff --git a/src/client/managers/session/index.js b/src/client/managers/session/index.js index acce5b67..c9c2cca7 100644 --- a/src/client/managers/session/index.js +++ b/src/client/managers/session/index.js @@ -97,7 +97,7 @@ var SessionManager = class SessionManager extends EventEmitter { } - save(path) { + save(path, backup=false) { if (!this.session || READ_ONLY) return @@ -113,7 +113,8 @@ var SessionManager = class SessionManager extends EventEmitter { ipc.send('sessionSave', { session: this.session.toJSON(), - path: this.sessionPath + path: this.sessionPath, + backup: backup }) } else { @@ -127,13 +128,18 @@ var SessionManager = class SessionManager extends EventEmitter { content: data, version: PACKAGE.version }, 'fragment'), - path: this.sessionPath + path: this.sessionPath, + backup: backup }) } } + saveBackup() { + this.save('', true) + } + saveAs() { if (!this.session || READ_ONLY) return diff --git a/src/client/ui/main-menu.js b/src/client/ui/main-menu.js index 77310df5..8245a4d1 100644 --- a/src/client/ui/main-menu.js +++ b/src/client/ui/main-menu.js @@ -58,6 +58,15 @@ var menuEntries = [ { separator: true }, + { + label: locales('file_backup'), + action: sessionManager.saveBackup.bind(sessionManager), + shortcut: 'mod + b', + class: ()=>{return READ_ONLY || sessionManager.session === null || !sessionManager.sessionPath ? 'disabled' :''} + }, + { + separator: true + }, { label: locales('file_import'), action: sessionManager.import.bind(sessionManager), diff --git a/src/server/callbacks.js b/src/server/callbacks.js index f4116e53..9b5861d7 100644 --- a/src/server/callbacks.js +++ b/src/server/callbacks.js @@ -281,11 +281,24 @@ module.exports = { if (!path.basename(data.path).match(/.*\.json$/)) return console.error('(ERROR) Sessions must be saved as .json files') + if (data.backup) { + var newpath, i = 0 + while (true) { + newpath = data.path.replace(/\.json$/, '-backup' + String(i).padStart(3, 0) + '.json') + if (fs.existsSync(newpath)) i++ + else { + data.path = newpath + break + } + } + } + + module.exports.fileSave(data, clientId, true, ()=>{ console.log('(INFO) Session file saved in '+ data.path) - ipc.send('sessionSaved', clientId) + if (!data.backup) ipc.send('sessionSaved', clientId) // reload session in other clients for (var id in ipc.clients) { @@ -294,7 +307,7 @@ module.exports = { } } - module.exports.sessionSetPath({path: data.path}, clientId) + if (!data.backup) module.exports.sessionSetPath({path: data.path}, clientId) })