remote-file: extend feature to state files

This commit is contained in:
jean-emmanuel 2019-04-08 20:06:35 +02:00
parent 5d48874565
commit 4801bd88e9
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)
},
]
}
]
},