cache: managed localStorage & sessionStorage

ui: remember panel state
This commit is contained in:
bordun 2020-07-14 11:10:24 +02:00
parent ad4a8ff110
commit bd6c70f8e8
6 changed files with 88 additions and 16 deletions

View file

@ -1,4 +1,5 @@
var localBackup = sessionStorage.getItem('osc.backup')
var cache = require('./managers/cache'),
localBackup = cache.get('backup', false)
module.exports = {
@ -12,14 +13,14 @@ module.exports = {
editor = require('./editor/')
if (session.session) {
sessionStorage.setItem('osc.backup', JSON.stringify({
cache.set('backup', {
session: session.session.data,
sessionPath: session.sessionPath,
state: state.get(),
history: editor.history,
historyState: editor.historyState,
editorEnabled: editor.enabled
}))
}, false)
}
},
@ -33,9 +34,9 @@ module.exports = {
if (localBackup) {
var data = JSON.parse(localBackup)
var data = localBackup
sessionStorage.removeItem('osc.backup')
cache.remove('backup', false)
session.load(data.session, ()=>{
state.set(data.state, false)

View file

@ -1,14 +1,15 @@
var EventEmitter = require('../events/event-emitter'),
cache = require('../managers/cache'),
nanoid = require('nanoid/generate'), uuid,
notifications,
locales
var localUuid = ENV.id || sessionStorage.getItem('osc.uuid')
var localUuid = ENV.id || cache.get('osc.uuid', false)
if (localUuid) {
uuid = localUuid
} else {
uuid = nanoid('0123456789abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ', 10)
sessionStorage.setItem('osc.uuid', uuid)
cache.set('uuid', uuid, false)
}
setTimeout(()=>{

View file

@ -0,0 +1,55 @@
var keyPrefix = 'osc.'
class Cache {
set(key, value, persistent = true) {
var storage = persistent ? localStorage : sessionStorage
storage.setItem(keyPrefix + key, JSON.stringify(value))
}
get(key, persistent = true) {
var storage = persistent ? localStorage : sessionStorage
return JSON.parse(storage.getItem(keyPrefix + key))
}
remove(key, persistent = true) {
var storage = persistent ? localStorage : sessionStorage
storage.removeItem(keyPrefix + key)
}
clear(domain) {
if (domain) {
for (let storage of [localStorage, sessionStorage]) {
for (let k in storage) {
if (k.indexOf(keyPrefix + domain) === 0) {
storage.removeItem(k)
}
}
}
} else {
localStorage.clear()
sessionStorage.clear()
}
}
}
module.exports = new Cache()

View file

@ -1,5 +1,6 @@
var ipc = require('../ipc'),
widgetManager = require('./widgets'),
cache = require('./cache'),
uiFilebrowser = require('../ui/ui-filebrowser'),
uiFileupload = require('../ui/ui-fileupload'),
notifications = require('../ui/notifications'),
@ -15,7 +16,7 @@ var StateManager = class StateManager {
this.statePath = ''
this.lastDir = null
this.quickState = JSON.parse(localStorage.getItem('osc.state.quickState')) || null
this.quickState = cache.get('state.quickState') || null
this.valueStateQueue = {}
this.valueOldPropQueue = {}

View file

@ -1,4 +1,5 @@
var UiWidget = require('./ui-widget')
var UiWidget = require('./ui-widget'),
cache = require('../managers/cache')
class UiSidePanel extends UiWidget {
@ -14,11 +15,13 @@ class UiSidePanel extends UiWidget {
this.toggleButton = DOM.get(this.container, '.toggle-button')[0]
this.content = DOM.get(this.container, 'osc-panel-content')[0]
this.cacheKey = 'ui.' + options.selector
this.minWidth = options.size || 250
this.width = options.size || 250
this.width = cache.get(this.cacheKey + '.width') || options.size || 250
this.disabled = false
this.minimized = false
this.minimized = cache.get(this.cacheKey + '.minimized') || false
this.init = false
@ -31,13 +34,14 @@ class UiSidePanel extends UiWidget {
this.on('drag', (event)=>{
this.width += this.resizeDirection * event[this.vertical ? 'movementY' : 'movementX'] / PXSCALE
this.container.style[this.vertical ? 'height' : 'width'] = this.width + 'rem'
this.updateWidth()
}, {element: this.resizeHandle})
this.on('dragend', (event)=>{
this.width = parseInt(this.container[this.vertical ? 'offsetHeight' : 'offsetWidth']) / PXSCALE
cache.set(this.cacheKey + '.width', this.width)
DOM.dispatchEvent(window, 'resize')
}, {element: this.resizeHandle})
@ -51,6 +55,7 @@ class UiSidePanel extends UiWidget {
})
this.updateWidth()
this.disable()
}
@ -78,6 +83,7 @@ class UiSidePanel extends UiWidget {
if (this.minimized) return
this.minimized = true
cache.set(this.cacheKey + '.minimized', true)
if (!this.disabled) this.close()
@ -89,12 +95,19 @@ class UiSidePanel extends UiWidget {
if (!this.minimized) return
this.minimized = false
cache.set(this.cacheKey + '.minimized', false)
if (!this.disabled) this.open()
}
updateWidth() {
this.container.style[this.vertical ? 'height' : 'width'] = this.width + 'rem'
}
open() {
this.container.style[this.vertical ? 'height' : 'width'] = this.width + 'rem'

View file

@ -1,5 +1,6 @@
var widgetManager = require('../../managers/widgets'),
stateManager = require('../../managers/state'),
cache = require('../../managers/cache'),
{deepCopy} = require('../../utils'),
{urlParser} = require('../utils'),
Vm = require('../vm')
@ -220,10 +221,10 @@ class ScriptVm extends Vm {
this.sandbox.contentWindow.storage = {
setItem: (k, v)=>localStorage.setItem(k, JSON.stringify(v)),
getItem: (k)=>JSON.parse(localStorage.getItem(k)),
removeItem: (k)=>localStorage.removeItem(k),
clear: ()=>localStorage.clear()
setItem: (k, v)=>cache.set('script.' + k, v),
getItem: (k)=>cache.get('script.' + k, v),
removeItem: (k)=>cache.remove('script.' + k, v),
clear: ()=>cache.clear('script.')
}