remote-file: extend feature to state files

This commit is contained in:
jean-emmanuel 2019-04-08 20:06:35 +02:00
parent 98696b56d9
commit 1a1b54f873
5 changed files with 153 additions and 60 deletions

View file

@ -55,7 +55,7 @@ module.exports = {
stateLoad: function(data){
state.set(data.state, data.send)
state.load(data.state, data.send)
},

View file

@ -1,5 +1,6 @@
var widgetManager = require('./widgets'),
{upload} = require('../ui/utils'),
var ipc = require('../ipc'),
widgetManager = require('./widgets'),
{upload, remoteBrowse} = require('../ui/utils'),
notifications = require('../ui/notifications'),
{saveAs} = require('file-saver'),
locales = require('../locales')
@ -9,6 +10,9 @@ var StateManager = class StateManager {
constructor() {
this.statePath = ''
this.lastDir = null
this.quickState = []
this.valueStateQueue = {}
@ -82,32 +86,77 @@ var StateManager = class StateManager {
}
load() {
save(path) {
function stateLoadError(){
notifications.add({
class: 'error',
message: locales('state_uploaderror')
})
}
if (path) this.statePath = path
upload('.state', (path, result)=>{
try {
this.set(JSON.parse(result),true)
notifications.add({
icon: 'sliders-h',
message: locales('state_recallsuccess')
})
} catch(e) {
stateLoadError()
}
}, (e)=>{
stateLoadError()
if (!this.statePath) return this.saveAs()
ipc.send('stateSave', {
session: JSON.stringify(this.get(), null, ' '),
path: this.statePath
})
}
save() {
saveAs() {
remoteBrowse({extension: 'state', save:true, directory: this.lastDir}, (path)=>{
this.lastDir = path[0]
this.save(path)
})
}
browse() {
remoteBrowse({extension: 'state', directory: this.lastDir}, (path)=>{
this.lastDir = path[0]
ipc.send('stateOpen',{path: path})
})
}
load(state, send) {
if (!state) return
try {
if (typeof state === 'string') {
state = JSON.parse(state)
}
this.set(state, send)
notifications.add({
icon: 'sliders-h',
message: locales('state_recallsuccess')
})
} catch(e) {
this.loadError()
}
}
loadError(){
notifications.add({
class: 'error',
message: locales('state_uploaderror')
})
}
import() {
upload('.state', (path, result)=>{
this.load(result, true)
}, (e)=>{
this.loadError()
})
}
export() {
var state = JSON.stringify(this.get(),null,' ')
var blob = new Blob([state],{type : 'application/json'})
@ -115,23 +164,15 @@ var StateManager = class StateManager {
}
quickSave(state) {
quickSave() {
if (state) {
this.quickState = state
} else {
this.quickState = this.get()
}
this.quickState = this.get()
}
quickLoad() {
this.set(this.quickState,true)
this.load(this.quickState, true)
}

View file

@ -41,10 +41,6 @@ var sidepanelData = [
label: locales('state_recall'),
action: ()=>{
state.quickLoad()
notifications.add({
icon: 'sliders-h',
message: locales('state_recallsuccess')
})
},
class:'disabled quickload'
},
@ -59,12 +55,38 @@ var sidepanelData = [
}
},
{
label: locales('state_import'),
action:state.load.bind(state)
label: locales('editor_load'),
action:()=>{
state.browse()
}
},
{
label: locales('state_export'),
action:state.save.bind(state)
label: locales('editor_save'),
action:()=>{
state.save()
}
},
{
label: raw(icon('ellipsis-h')),
class: 'narrow',
menu: [
{
label: locales('editor_save_as'),
action:()=>{
state.saveAs()
}
},
{
label: locales('state_import'),
click: true,
action:state.load.bind(state)
},
{
label: locales('state_export'),
click: true,
action:state.save.bind(state)
},
]
}
]
},

View file

@ -612,7 +612,7 @@ body {
.file-browser {
width: 540px;
height: 400px;
height: 360px;
max-width: 100%;
display: flex;
flex-direction: column;

View file

@ -82,29 +82,16 @@ module.exports = {
},
sessionOpen: function(data,clientId) {
sessionOpen: function(data, clientId) {
if (Array.isArray(data.path)) data.path = path.resolve(...data.path)
var file = data.file || (function(){try {return fs.readFileSync(data.path,'utf8')} catch(err) {return false}})(),
error = file === false && data.path ? 'Session file "' + data.path + '" not found.' : false,
session
module.exports.fileRead(data, clientId, true, (result)=>{
try {
session = JSON.parse(file)
} catch(err) {
error = err
}
ipc.send('sessionOpen', {path: data.path, session: result}, clientId)
if (!error) {
})
ipc.send('sessionOpen', {path: data.path, session: session}, clientId)
} else {
ipc.send('error', `Invalid session file (${error})`)
}
},
sessionOpened: function(data, clientId) {
@ -132,6 +119,33 @@ module.exports = {
},
fileRead: function(data, clientId, ok, callback) {
if (!ok) return
if (Array.isArray(data.path)) data.path = path.resolve(...data.path)
fs.readFile(data.path, 'utf8', (err, result)=>{
var error
if (err) {
error = err
} else {
try {
result = JSON.parse(result)
callback(result)
} catch(err) {
error = err
}
}
if (error) ipc.send('error', `Could not open file (${error})`)
})
},
fileSave: function(data, clientId, ok, callback) {
if (!ok) return
@ -189,6 +203,18 @@ module.exports = {
},
stateOpen: function(data, clientId) {
if (Array.isArray(data.path)) data.path = path.resolve(...data.path)
module.exports.fileRead(data, clientId, true, (result)=>{
ipc.send('stateLoad', {state: result, send: true}, clientId)
})
},
sessionSave: function(data, clientId) {
if (Array.isArray(data.path)) data.path = path.resolve(...data.path)
@ -216,6 +242,10 @@ module.exports = {
stateSave: function(data, clientId) {
if (Array.isArray(data.path)) data.path = path.resolve(...data.path)
if (!path.basename(data.path).match(/.*\.state/)) return console.error('Statesaves must be saved as .state files')
module.exports.fileSave(data, clientId, true, ()=>{
console.log('State file saved in '+ data.path)