From 62acbf479f1b2f9c5027063fc5dd5b1d59c6e471 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Fri, 4 Jan 2019 10:25:06 +0100 Subject: [PATCH] session: implement remote saving for session files located on the server --- src/client/app/editor/index.js | 5 ++ src/client/app/ipc/callbacks.js | 7 +++ src/client/app/locales/en.js | 3 + src/client/app/locales/fr.js | 3 + src/client/app/locales/ru.js | 3 + src/client/app/managers/session.js | 23 ++++++-- src/client/app/ui/sidepanel.js | 4 ++ src/server/callbacks.js | 88 ++++++++++++------------------ src/server/ipc/client.js | 2 + 9 files changed, 82 insertions(+), 56 deletions(-) diff --git a/src/client/app/editor/index.js b/src/client/app/editor/index.js index 31c45e8a..b128498d 100644 --- a/src/client/app/editor/index.js +++ b/src/client/app/editor/index.js @@ -209,6 +209,11 @@ var Editor = class Editor { sessionManager.save() }) + keyboardJS.bind('mod + shift + s', (e)=>{ + e.preventDefault() + sessionManager.saveAs() + }) + keyboardJS.bind('mod + o', (e)=>{ e.preventDefault() sessionManager.browse() diff --git a/src/client/app/ipc/callbacks.js b/src/client/app/ipc/callbacks.js index e5093c6a..4ee1c2a4 100644 --- a/src/client/app/ipc/callbacks.js +++ b/src/client/app/ipc/callbacks.js @@ -45,6 +45,12 @@ module.exports = { }, + sessionAllowRemoteSave: function(data){ + + session.allowRemoteSave = data + + }, + stateLoad: function(data){ state.set(data.state, data.send) @@ -166,6 +172,7 @@ module.exports = { DOM.get('.traversingEnable')[0].click() } + ipc.send('sessionSetPath', {path: data.sessionPath}) ipc.send('deleteBackup', data.backupId) }) diff --git a/src/client/app/locales/en.js b/src/client/app/locales/en.js index 14805661..389df046 100644 --- a/src/client/app/locales/en.js +++ b/src/client/app/locales/en.js @@ -6,6 +6,7 @@ module.exports = { editor_root: 'Root', editor_load: 'Load', editor_save: 'Save', + editor_save_as: 'Export', // editor context-menu editor_editparent: 'Edit parent', @@ -36,6 +37,8 @@ module.exports = { session_malformed: 'Malformed session file.', session_parsingerror: 'Parsing error.', session_uploaderror: 'Failed to upload session file.', + session_savesuccess: 'Session saved successfully', + session_saveerror: 'The session file can\'t be saved remotely, use "export" instead', // sidepanel sidepanel_fs: 'Fullscreen', diff --git a/src/client/app/locales/fr.js b/src/client/app/locales/fr.js index d0bdae06..5ae9a2b7 100644 --- a/src/client/app/locales/fr.js +++ b/src/client/app/locales/fr.js @@ -6,6 +6,7 @@ module.exports = { editor_root: 'Root', editor_load: 'Charger', editor_save: 'Sauver', + editor_save_as: 'Exporter', // editor context-menu editor_editparent: 'Éditer parent', @@ -36,6 +37,8 @@ module.exports = { session_malformed: 'Fichier de session mal formé.', session_parsingerror: 'Erreur à l\'ouverture de la session.', session_uploaderror: 'Échec à l\'envoi du fichier de session.', + session_savesuccess: 'Session enregistrée avec succès', + session_saveerror: 'La session ne peut pas être enregistrée sur le serveur, utilisez la fonction "exporter"', // sidepanel sidepanel_fs: 'Plein Écran', diff --git a/src/client/app/locales/ru.js b/src/client/app/locales/ru.js index d3cf3483..64f0a95a 100644 --- a/src/client/app/locales/ru.js +++ b/src/client/app/locales/ru.js @@ -6,6 +6,7 @@ module.exports = { // @https://github.com/suhr editor_root: 'Корень', editor_load: 'Загрузить', editor_save: 'Сохранить', + editor_save_as: 'Экспорт', // editor context-menu editor_editparent: 'К родителю', @@ -36,6 +37,8 @@ module.exports = { // @https://github.com/suhr session_malformed: 'Файл сессии повреждён.', session_parsingerror: 'Ошибка разбора.', session_uploaderror: 'Не удалось загрузить файл сессии.', + session_savesuccess: 'Session saved successfully', + session_saveerror: 'The session file can\'t be saved remotely, use "export" instead', // sidepanel sidepanel_fs: 'Полный экран', diff --git a/src/client/app/managers/session.js b/src/client/app/managers/session.js index f9eff072..dbf8a9f8 100644 --- a/src/client/app/managers/session.js +++ b/src/client/app/managers/session.js @@ -17,6 +17,8 @@ var SessionManager = class SessionManager { this.session = [] this.lock = false + this.allowRemoteSave = false + } load(session, callback) { @@ -83,6 +85,7 @@ var SessionManager = class SessionManager { loader.close() container.classList.add('show') this.lock = false + this.allowRemoteSave = false if (callback) callback() }, 25) @@ -92,13 +95,25 @@ var SessionManager = class SessionManager { save() { + if (this.allowRemoteSave) { + + ipc.send('sessionSave', JSON.stringify(this.session, null, ' ')) + + } else { + + ipc.send('log', 'Could not save session file') + + } + + } + + saveAs() { + var sessionfile = JSON.stringify(this.session,null,' '), blob = new Blob([sessionfile],{type : 'application/json'}) saveAs(blob, new Date().toJSON().slice(0,10)+'_'+new Date().toJSON().slice(11,16) + '.json') - ipc.send('savingSession') - } list(data) { @@ -161,8 +176,8 @@ var SessionManager = class SessionManager { open(data) { - this.load(data,function(){ - ipc.send('sessionOpened') + this.load(data.session,function(){ + ipc.send('sessionOpened', {path: data.path}) }) } diff --git a/src/client/app/ui/sidepanel.js b/src/client/app/ui/sidepanel.js index 60779c25..032bc00a 100644 --- a/src/client/app/ui/sidepanel.js +++ b/src/client/app/ui/sidepanel.js @@ -117,6 +117,10 @@ var sidepanelData = [ { title: locales('editor_save'), action:session.save.bind(session) + }, + { + title: locales('editor_save_as'), + action:session.saveAs.bind(session) } ] }, diff --git a/src/server/callbacks.js b/src/server/callbacks.js index e41e3111..bf61811b 100644 --- a/src/server/callbacks.js +++ b/src/server/callbacks.js @@ -4,12 +4,10 @@ var path = require('path'), osc = require('./osc'), {ipc} = require('./server'), {deepCopy} = require('./utils'), - theme = require('./theme'), - chokidar = require('chokidar') + theme = require('./theme') var openedSessions = {}, widgetHashTable = {}, - lastSavingClient, sessionBackups = {} module.exports = { @@ -85,52 +83,7 @@ module.exports = { if (!error) { - ipc.send('sessionOpen', session,clientId) - - - for (var i in openedSessions) { - if (openedSessions[i].indexOf(clientId) != -1) { - openedSessions[i].splice(openedSessions[i].indexOf(clientId), 1) - } - } - - if (data.path) { - - if (!settings.read('readOnly')) this.sessionAddToHistory(data.path) - - fs.lstat(data.path, (err, stats)=>{ - - if (err || !stats.isFile()) return - - - if (!openedSessions[data.path]) { - - openedSessions[data.path] = [] - - var watchFile = ()=>{ - var watcher = chokidar.watch(data.path) - watcher.on('change',()=>{ - var openedSessionsClone = deepCopy(openedSessions[data.path]) - for (var k in openedSessionsClone) { - if (openedSessionsClone[k] != lastSavingClient) { - module.exports.sessionOpen({path:data.path}, openedSessionsClone[k]) - } - } - watcher.close() - watchFile() - }) - } - - watchFile() - - - } - - openedSessions[data.path].push(clientId) - - }) - - } + ipc.send('sessionOpen', {path: data.path, session: session}, clientId) } else { @@ -158,11 +111,41 @@ module.exports = { ipc.send('stateSend', null, null, clientId) + module.exports.sessionSetPath(data, clientId) + }, - savingSession: function(data, clientId) { + sessionSetPath: function(data, clientId) { - lastSavingClient = clientId + ipc.clients[clientId].sessionPath = '' + fs.lstat(data.path, (err, stats)=>{ + if (err || !stats.isFile()) return + ipc.clients[clientId].sessionPath = data.path + if (!settings.read('readOnly')) { + this.sessionAddToHistory(data.path) + ipc.send('sessionAllowRemoteSave', true, clientId) + } + }) + + }, + + sessionSave: function(data, clientId) { + + var path = ipc.clients[clientId].sessionPath + + if (path) { + + fs.writeFile(path, data, function(err, data) { + if (err) throw err + console.log('Session file saved in '+ path) + for (var id in ipc.clients) { + if (id !== clientId) { + module.exports.sessionOpen({path: path}, id) + } + } + }) + + } }, @@ -307,9 +290,10 @@ module.exports = { }, - storeBackup: function(data) { + storeBackup: function(data, clientId) { sessionBackups[data.backupId] = data + sessionBackups[data.backupId].sessionPath = ipc.clients[clientId].sessionPath }, diff --git a/src/server/ipc/client.js b/src/server/ipc/client.js index 5bb8adb3..84215a1e 100644 --- a/src/server/ipc/client.js +++ b/src/server/ipc/client.js @@ -13,6 +13,8 @@ class Socket extends EventEmitter { this.id = id this.socket = null + this.sessionPath = '' + this.queue = [] this.hearbeat = undefined