diff --git a/src/client/backup.js b/src/client/backup.js new file mode 100644 index 00000000..bf98b978 --- /dev/null +++ b/src/client/backup.js @@ -0,0 +1,58 @@ +var localBackup = localStorage.getItem('osc.backup') + + +module.exports = { + + exists: localBackup !== null, + + save: ()=>{ + + var session = require('./managers/session/'), + state = require('./managers/state'), + editor = require('./editor/') + + if (session.session) { + localStorage.setItem('osc.backup', JSON.stringify({ + session: session.session.data, + sessionPath: session.sessionPath, + state: state.get(), + history: editor.history, + historyState: editor.historyState, + editorEnabled: editor.enabled + })) + } + + }, + + load: ()=>{ + + var session = require('./managers/session/'), + state = require('./managers/state'), + editor = require('./editor/'), + ipc = require('./ipc/') + + if (localBackup) { + + var data = JSON.parse(localBackup) + + localStorage.removeItem('osc.backup') + session.load(data.session, ()=>{ + + state.set(data.state, false) + + editor.clearHistory() + editor.history = data.history + editor.historyState = data.historyState + + if (data.editorEnabled) editor.enable() + + + ipc.send('sessionSetPath', {path: data.sessionPath}) + session.setSessionPath(data.sessionPath) + + }) + + } + + } +} diff --git a/src/client/editor/index.js b/src/client/editor/index.js index 4d9e2fe0..a44db6df 100644 --- a/src/client/editor/index.js +++ b/src/client/editor/index.js @@ -704,7 +704,7 @@ class Editor { this.historyState = -1 } - var d = diff.diff(this.historySession, sessionManager.session) + var d = diff.diff(this.historySession, sessionManager.session.getRoot()) if (d) { diff.patch(this.historySession, deepCopy(d)) @@ -718,7 +718,7 @@ class Editor { this.history = [] this.historyState = -1 - this.historySession = deepCopy(sessionManager.session) + this.historySession = deepCopy(sessionManager.session.getRoot()) } @@ -733,7 +733,7 @@ class Editor { d2 = deepCopy(patch) diff.unpatch(this.historySession, d1) - diff.unpatch(sessionManager.session, d2) + diff.unpatch(sessionManager.session.getRoot(), d2) this.updateWidgetFromPatch(patch, indexes ? { addedIndexes: deepCopy(indexes.removedIndexes), @@ -751,7 +751,7 @@ class Editor { d2 = deepCopy(patch) diff.patch(this.historySession, d1) - diff.patch(sessionManager.session, d2) + diff.patch(sessionManager.session.getRoot(), d2) this.updateWidgetFromPatch(patch, indexes ? { addedIndexes: deepCopy(indexes.addedIndexes), @@ -796,4 +796,4 @@ var editor = new Editor() module.exports = editor require('./context-menu') -sessionManager = require('../managers/session') +sessionManager = require('../managers/session/') diff --git a/src/client/index.js b/src/client/index.js index 5e5c52c8..e995bb93 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -16,7 +16,9 @@ document.addEventListener('DOMContentLoaded', function(event) { setTimeout(()=>{ - var ipc = require('./ipc/') + var ipc = require('./ipc/'), + backup = require('./backup') + ipc.init() @@ -24,7 +26,9 @@ document.addEventListener('DOMContentLoaded', function(event) { document.title = TITLE - ipc.send('ready', {backupId: ENV.backupId}) + ipc.send('ready', {hotReload: backup.exists}) + + backup.load() }, 100) diff --git a/src/client/ipc/callbacks.js b/src/client/ipc/callbacks.js index bdea1748..3dcc0953 100644 --- a/src/client/ipc/callbacks.js +++ b/src/client/ipc/callbacks.js @@ -1,6 +1,6 @@ var utils = require('../ui/utils'), osc = require('../osc'), - session = require('../managers/session'), + session = require('../managers/session/'), widgetManager = require('../managers/widgets'), state = require('../managers/state'), editor = require('../editor/'), @@ -11,7 +11,8 @@ var utils = require('../ui/utils'), notifications = require('../ui/notifications'), {TRAVERSING_SAMEWIDGET} = require('../events/utils'), raw = require('nanohtml/raw'), - ipc = require('./') + ipc = require('./'), + backup = require('../backup') module.exports = { @@ -96,75 +97,9 @@ module.exports = { reload: function(){ - var id = Math.random(), - search = location.search, - query = 'backupId=' + id - - // disable editor's warning + backup.save() window.onbeforeunload = null - - if (!session.session) { - window.location.href = window.location.href - return - } - - try { - - // store session & state backup - ipc.send('storeBackup', { - backupId: id, - session: session.session || {}, - sessionPath: session.sessionPath, - state: state.get(), - history: editor.history, - historyState: editor.historyState, - editorEnabled: editor.enabled, - }) - - // reload page and hold backup id - - if (search) { - if (search.includes('backupId=')) { - search = search.replace(/backupId=[^&]*/, query) - } else { - search += '&' + query - } - } else { - search += '?' + query - } - - window.location.search = search - - } catch(e) { - - window.location.href = window.location.href - - } - - }, - - loadBackup: function(data) { - - session.load(data.session, ()=>{ - - state.set(data.state, false) - - editor.historySession = deepCopy(data.session) - editor.history = data.history - editor.historyState = data.historyState - if (data.editorEnabled) editor.enable() - - if (data.traversing === TRAVERSING_SAMEWIDGET) { - DOM.get('.traversingSmart')[0].click() - } else if (data.traversing) { - DOM.get('.traversingEnable')[0].click() - } - - ipc.send('sessionSetPath', {path: data.sessionPath}) - session.setSessionPath(data.sessionPath) - ipc.send('deleteBackup', data.backupId) - - }) + window.location.href = window.location.href }, diff --git a/src/client/locales/en.js b/src/client/locales/en.js index 178f9014..59173b52 100644 --- a/src/client/locales/en.js +++ b/src/client/locales/en.js @@ -63,6 +63,8 @@ module.exports = { session_parsingerror: 'Parsing error.', session_uploaderror: 'Failed to upload session file.', session_unsaved: 'Your changes will be lost if you close this session without saving, continue ?', + session_oldversion_title: 'Warning', + session_oldversion: 'This session was created with an older version, an automatic conversion has been done.', // state state: 'State', diff --git a/src/client/locales/fr.js b/src/client/locales/fr.js index 7aada79a..a3385c08 100644 --- a/src/client/locales/fr.js +++ b/src/client/locales/fr.js @@ -63,6 +63,8 @@ module.exports = { session_parsingerror: 'Erreur à l\'ouverture de la session.', session_uploaderror: 'Échec à l\'envoi du fichier de session.', session_unsaved: 'Vos modifications seront perdues si vous fermez la session actuelle sans sauvegarder, continuer ?', + session_oldversion_title: 'Attention', + session_oldversion: 'Cette session a été créée avec une ancienne version, une conversion automatique a été appliquée.', // state state: 'État', diff --git a/src/client/managers/session.js b/src/client/managers/session/index.js similarity index 83% rename from src/client/managers/session.js rename to src/client/managers/session/index.js index c6ec0e58..e7cffafc 100644 --- a/src/client/managers/session.js +++ b/src/client/managers/session/index.js @@ -1,13 +1,14 @@ -var ipc = require('../ipc/'), - parser = require('../parser'), - editor = require('../editor/'), - uiFilebrowser = require('../ui/ui-filebrowser'), - uiFileupload = require('../ui/ui-fileupload'), - UiModal = require('../ui/ui-modal'), - uiLoading = require('../ui/ui-loading'), +var ipc = require('../../ipc/'), + parser = require('../../parser'), + editor = require('../../editor/'), + uiFilebrowser = require('../../ui/ui-filebrowser'), + uiFileupload = require('../../ui/ui-fileupload'), + UiModal = require('../../ui/ui-modal'), + uiLoading = require('../../ui/ui-loading'), {saveAs} = require('file-saver'), - widgetManager = require('./widgets'), - locales = require('../locales') + widgetManager = require('../widgets'), + locales = require('../../locales'), + Session = require('./session') var SessionManager = class SessionManager { @@ -38,18 +39,12 @@ var SessionManager = class SessionManager { setTimeout(()=>{ try { - // backward compat - if (Array.isArray(session)) session = session[0] - - // session object must be a root widget - if (session.type !== 'root') throw new Error(locales('session_malformed')) - - // ok - this.session = session + this.session = new Session(session) + console.log(this.session) container.innerHTML = '' parser.reset() parser.parse({ - data: this.session, + data: this.session.getRoot(), parentNode: DOM.get('#osc-container')[0] }) editor.clearHistory() @@ -99,7 +94,7 @@ var SessionManager = class SessionManager { if (!this.sessionPath) return this.saveAs() ipc.send('sessionSave', { - session: JSON.stringify(this.session, null, ' '), + session: this.session.toJSON(), path: this.sessionPath }) @@ -118,7 +113,7 @@ var SessionManager = class SessionManager { export() { - var sessionfile = JSON.stringify(this.session,null,' '), + var sessionfile = this.session.toJSON(), blob = new Blob([sessionfile],{type : 'application/json'}) saveAs(blob, new Date().toJSON().slice(0,10)+'_'+new Date().toJSON().slice(11,16) + '.json') @@ -181,7 +176,7 @@ var SessionManager = class SessionManager { if (editor.unsavedSession && !confirm(locales('session_unsaved'))) return - this.load({type: 'root'},function(){ + this.load(null, function(){ editor.enable() editor.select(widgetManager.getWidgetById('root')) }) diff --git a/src/client/managers/session/session.js b/src/client/managers/session/session.js new file mode 100644 index 00000000..b8eee3a5 --- /dev/null +++ b/src/client/managers/session/session.js @@ -0,0 +1,144 @@ +var semver = require('semver'), + UiModal = require('../../ui/ui-modal'), + locales = require('../../locales') + +module.exports = class Session { + + constructor(data) { + + if (data === null) { + + data = { + session: {type: 'root'} + } + + + } else { + + var version = data.version || '0.0.0', + warning = false + + for (var converter of converters) { + + if (semver.lte(version, converter.version)) { + + if (converter.global) data = converter.global(data) + if (converter.widget) this.applyConvert(data.session, converter.widget) + + if (converter.warning) warning = true + + } + + } + + + if (warning) { + + new UiModal({title: locales('session_oldversion_title'), content: locales('session_oldversion'), icon: 'exclamation-triangle', closable:true}) + + + } + + } + + data.version = PACKAGE.version + data.type = PACKAGE.productName + ' ' + 'session' + + this.data = data + + } + + applyConvert(data, convert) { + + convert(data) + + if (data.widgets) { + for (let c of data.widgets) { + this.applyConvert(c, convert) + } + } + + if (data.tabs) { + for (let c of data.tabs) { + this.applyConvert(c, convert) + } + } + + } + + getRoot() { + + return this.data.session + + } + + toJSON() { + + return JSON.stringify(this.data, null, ' ') + + } + +} + +var converters = [ + { + version: '0.49.12', + warning: true, + global: (data)=>{ + return { + session: data + } + }, + widget: (data)=>{ + if (data.precision === 0) data.typeTags = 'i' + + console.log(data.type) + switch (data.type) { + + case 'toggle': + data.type = 'button' + data.mode = 'toggle' + break + + case 'push': + data.type = 'button' + data.mode = data.norelease ? 'tap' : 'push' + break + + case 'meter': + data.type = 'fader' + data.design = 'compact' + data.interaction = false + break + + case 'strip': + data.type = 'panel' + data.layout = data.horizontal ? 'horizontal' : 'vertical' + break + + case 'eq': + data.filter = data.value + data.value = '' + break + + case 'keys': + data.type = 'script' + data.event = 'keyboard' + data.keyRepeat = data.repeat + data.keyBinding = data.binding + data.keyType = data.keyDown && data.keyUp ? 'both' : data.keyUp ? 'keyup' : 'keydown' + data.script = ` + // converted from keys widget data + if (type === 'keyup') { + ${String(data.keyUp).replace(/^JS{{(.*)}}$/, '$1')} + } else if (type === 'keydown') { + ${String(data.keyDown).replace(/^JS{{(.*)}}$/, '$1')} + } + ` + break + + } + + } + } +] diff --git a/src/client/ui/ui-toolbar.js b/src/client/ui/ui-toolbar.js index e24bc07f..d62e976f 100644 --- a/src/client/ui/ui-toolbar.js +++ b/src/client/ui/ui-toolbar.js @@ -5,7 +5,7 @@ var UiWidget = require('./ui-widget'), notifications = require('./notifications'), fullscreen = require('./fullscreen'), editor = require('../editor'), - sessionManager = require('../managers/session'), + sessionManager = require('../managers/session/'), stateManager = require('../managers/state'), {leftUiSidePanel, rightUiSidePanel} = require('../ui/') diff --git a/src/client/ui/ui-tree.js b/src/client/ui/ui-tree.js index b5436d86..52b7b543 100644 --- a/src/client/ui/ui-tree.js +++ b/src/client/ui/ui-tree.js @@ -174,6 +174,7 @@ class UiTree extends UiWidget { if (this.expanded[widget.hash]) node.classList.add('expanded') if (!init && widget instanceof Root) { node.classList.add('expanded') + this.expanded[widget.hash] = true init = true } var sublist = node.appendChild(html`
    `) diff --git a/src/client/widgets/containers/root.js b/src/client/widgets/containers/root.js index 2d1f9a0b..f17152bb 100644 --- a/src/client/widgets/containers/root.js +++ b/src/client/widgets/containers/root.js @@ -47,6 +47,7 @@ class Root extends StaticProperties(Panel, {visible: true, label: false, id: 'ro constructor(options) { options.root = true + options.props.id = 'root' super(options) diff --git a/src/server/callbacks.js b/src/server/callbacks.js index ac24305a..780e5be8 100644 --- a/src/server/callbacks.js +++ b/src/server/callbacks.js @@ -7,7 +7,6 @@ var path = require('path'), theme = require('./theme') var widgetHashTable = {}, - sessionBackups = {}, clipboard = {clipboard: null, idClipboard: null} module.exports = { @@ -20,23 +19,10 @@ module.exports = { ipc.send('sessionList', recentSessions, clientId) ipc.send('clipboard', clipboard, clientId) - - if (data.backupId && sessionBackups[data.backupId]) { - ipc.send('loadBackup', sessionBackups[data.backupId]) - return - } - - if (settings.read('load')) return this.sessionOpen({path:settings.read('load')},clientId) - - - if (settings.read('examples')) { - var dir = path.resolve(__dirname + '/../examples') - recentSessions = fs.readdirSync(dir) - recentSessions = recentSessions.map(function(file){return dir + '/' + file}) - } - ipc.send('serverTargets', settings.read('send'), clientId) + if (settings.read('load') && !data.hotReload) return this.sessionOpen({path: settings.read('load')}, clientId) + }, sessionAddToHistory: function(data) { @@ -386,18 +372,6 @@ module.exports = { }, - storeBackup: function(data, clientId) { - - sessionBackups[data.backupId] = data - - }, - - deleteBackup: function(data) { - - delete sessionBackups[data] - - }, - reloadCss:function(){ theme.load() ipc.send('reloadCss')