use storage instance

main
bat 3 years ago
parent 3cc25e6d37
commit 8696ad8228

@ -283,7 +283,7 @@ export class FileGroupPage extends HTMLElement {
} }
getPage(path) { getPage(path) {
const body = localStorage.getItem(path) const body = this.storage.getItem(path)
try { try {
return JSON.parse(body) return JSON.parse(body)
} catch (err) { } catch (err) {
@ -296,12 +296,12 @@ export class FileGroupPage extends HTMLElement {
if (typeof value !== 'string') { if (typeof value !== 'string') {
value = JSON.stringify(value) value = JSON.stringify(value)
} }
localStorage.setItem(path, value) this.storage.setItem(path, value)
} }
set body(value) { set body(value) {
try { try {
localStorage.setItem(this.path, value) this.storage.setItem(this.path, value)
} catch (err) { } catch (err) {
console.error(err) console.error(err)
} }
@ -309,7 +309,7 @@ export class FileGroupPage extends HTMLElement {
get body() { get body() {
try { try {
return localStorage.getItem(this.path) return this.storage.getItem(this.path)
} catch (err) { } catch (err) {
console.error(err) console.error(err)
return '' return ''
@ -318,7 +318,7 @@ export class FileGroupPage extends HTMLElement {
set settings(value) { set settings(value) {
try { try {
localStorage.setItem( this.storage.setItem(
'settings/page:' + this.path, 'settings/page:' + this.path,
JSON.stringify(value) JSON.stringify(value)
) )
@ -330,7 +330,7 @@ export class FileGroupPage extends HTMLElement {
get settings() { get settings() {
let data let data
try { try {
data = localStorage.getItem( data = this.storage.getItem(
'settings/page:' + this.path 'settings/page:' + this.path
) )
if (data === null || data === undefined) { if (data === null || data === undefined) {

@ -223,7 +223,7 @@ export class Header extends HTMLElement {
getPages() { getPages() {
return ( return (
Object.keys(localStorage).slice() this.storage.keys().slice()
.filter(s => s.startsWith('/')) .filter(s => s.startsWith('/'))
.sort() .sort()
) )
@ -231,7 +231,7 @@ export class Header extends HTMLElement {
getStorageUse() { getStorageUse() {
let bytes = 0 let bytes = 0
const entries = Object.entries(localStorage) const entries = this.storage.entries()
for (const [k, v] of entries) { for (const [k, v] of entries) {
bytes += k.length bytes += k.length
bytes += v.length bytes += v.length
@ -283,7 +283,7 @@ export class Header extends HTMLElement {
) )
bGroup.addPrimary(this.text.createPage, () => { bGroup.addPrimary(this.text.createPage, () => {
const newPath = this.encodePath(input.value) const newPath = this.encodePath(input.value)
const v = localStorage.getItem(newPath) const v = this.storage.getItem(newPath)
if (v !== null || newPath === this.path) { if (v !== null || newPath === this.path) {
if (!errorEl) { if (!errorEl) {
errorEl = document.createElement('p') errorEl = document.createElement('p')
@ -312,8 +312,8 @@ export class Header extends HTMLElement {
], ],
}) })
) )
localStorage.setItem(newPath, value) this.storage.setItem(newPath, value)
localStorage.setItem( this.storage.setItem(
'settings/page:' + newPath, '{}' 'settings/page:' + newPath, '{}'
) )
location.hash = newPath location.hash = newPath

@ -71,7 +71,7 @@ export class Layout extends HTMLElement {
this.editing = false this.editing = false
const prevPage = this.page const prevPage = this.page
let isGroup = false let isGroup = false
const body = localStorage.getItem(path) || '' const body = this.storage.getItem(path) || ''
if (body.match(/^\s*{/)) { if (body.match(/^\s*{/)) {
try { try {
const bodyData = JSON.parse(body) const bodyData = JSON.parse(body)
@ -86,6 +86,7 @@ export class Layout extends HTMLElement {
this.page = document.createElement( this.page = document.createElement(
isGroup ? 'm-file-group-page' : 'm-page' isGroup ? 'm-file-group-page' : 'm-page'
) )
this.page.storage = this.storage
if (isGroup) { if (isGroup) {
this.page.cspOff = this.csp === undefined this.page.cspOff = this.csp === undefined
this.page.cspProfiles = this.cspProfiles this.page.cspProfiles = this.cspProfiles
@ -117,7 +118,7 @@ export class Layout extends HTMLElement {
} }
set editing(value) { set editing(value) {
sessionStorage.setItem( this.storage.session.setItem(
'editing', value ? 'true' : 'false' 'editing', value ? 'true' : 'false'
) )
this.header.editing = this.editing this.header.editing = this.editing
@ -129,7 +130,7 @@ export class Layout extends HTMLElement {
get editing() { get editing() {
try { try {
return ( return (
sessionStorage.getItem('editing') === 'true' this.storage.session.getItem('editing') === 'true'
) )
} catch (e) { } catch (e) {
return false return false
@ -142,4 +143,14 @@ export class Layout extends HTMLElement {
} }
return this._editorBuild return this._editorBuild
} }
set storage(value) {
this._storage = value
this.header.storage = value
this.pageActions.storage = value
}
get storage() {
return this._storage
}
} }

@ -80,7 +80,7 @@ export class PageActions extends HTMLElement {
} }
download() { download() {
const text = localStorage.getItem(this.path) const text = this.storage.getItem(this.path)
const sp = this.path.split('/') const sp = this.path.split('/')
const filename = sp[sp.length - 1] const filename = sp[sp.length - 1]
const el = document.createElement('a') const el = document.createElement('a')
@ -111,7 +111,7 @@ export class PageActions extends HTMLElement {
) )
bGroup.addPrimary(this.text.rename, () => { bGroup.addPrimary(this.text.rename, () => {
const newPath = input.value const newPath = input.value
const v = localStorage.getItem(newPath) const v = this.storage.getItem(newPath)
if (v !== null || newPath === this.path) { if (v !== null || newPath === this.path) {
if (!errorEl) { if (!errorEl) {
errorEl = document.createElement('p') errorEl = document.createElement('p')
@ -124,10 +124,10 @@ export class PageActions extends HTMLElement {
} }
const sKeyOld = 'settings/page:' + this.path const sKeyOld = 'settings/page:' + this.path
const sKeyNew = 'settings/page:' + newPath const sKeyNew = 'settings/page:' + newPath
const settingsJson = localStorage.getItem(sKeyOld) const settingsJson = this.storage.getItem(sKeyOld)
if (settingsJson ?? true === true) { if (settingsJson ?? true === true) {
localStorage.setItem(sKeyNew, settingsJson) this.storage.setItem(sKeyNew, settingsJson)
localStorage.removeItem(sKeyOld) this.storage.removeItem(sKeyOld)
let settingsData let settingsData
try { try {
settingsData = JSON.parse(settingsJson) settingsData = JSON.parse(settingsJson)
@ -145,13 +145,13 @@ export class PageActions extends HTMLElement {
} }
} }
} else { } else {
localStorage.removeItem(sKeyNew) this.storage.removeItem(sKeyNew)
} }
localStorage.setItem( this.storage.setItem(
newPath, newPath,
localStorage.getItem(this.path) this.storage.getItem(this.path)
) )
localStorage.removeItem(this.path) this.storage.removeItem(this.path)
dialog.close() dialog.close()
location.hash = newPath location.hash = newPath
}) })
@ -178,7 +178,7 @@ export class PageActions extends HTMLElement {
const btnText = this.text.duplicate const btnText = this.text.duplicate
bGroup.addPrimary(btnText, () => { bGroup.addPrimary(btnText, () => {
const newPath = input.value const newPath = input.value
const v = localStorage.getItem(newPath) const v = this.storage.getItem(newPath)
if (v !== null || newPath === this.path) { if (v !== null || newPath === this.path) {
if (!errorEl) { if (!errorEl) {
errorEl = document.createElement('p') errorEl = document.createElement('p')
@ -189,13 +189,13 @@ export class PageActions extends HTMLElement {
} }
return return
} }
localStorage.setItem( this.storage.setItem(
newPath, newPath,
localStorage.getItem(this.path) this.storage.getItem(this.path)
) )
localStorage.setItem( this.storage.setItem(
'settings/page:' + newPath, 'settings/page:' + newPath,
localStorage.getItem(this.path) ?? '{}' this.storage.getItem(this.path) ?? '{}'
) )
dialog.close() dialog.close()
location.hash = newPath location.hash = newPath
@ -222,8 +222,8 @@ export class PageActions extends HTMLElement {
'm-forms-button-group' 'm-forms-button-group'
) )
bGroup.addPrimary(this.text.delete_, () => { bGroup.addPrimary(this.text.delete_, () => {
localStorage.removeItem(this.path) this.storage.removeItem(this.path)
localStorage.removeItem( this.storage.removeItem(
'settings/page:' + this.path 'settings/page:' + this.path
) )
location.hash = '/' location.hash = '/'
@ -246,6 +246,9 @@ export class PageActions extends HTMLElement {
) )
settingsEl.cspProfiles = this.cspProfiles settingsEl.cspProfiles = this.cspProfiles
settingsEl.path = this.path settingsEl.path = this.path
settingsEl.checkExists = path => (
this.storage.getItem(path) !== null
)
settingsEl.data = this.page.settings settingsEl.data = this.page.settings
const h = document.createElement('h2') const h = document.createElement('h2')
h.innerText = this.text.settings h.innerText = this.text.settings
@ -283,7 +286,7 @@ export class PageActions extends HTMLElement {
) )
for (const [path, access] of selfEntries) { for (const [path, access] of selfEntries) {
const key = 'settings/page:' + path const key = 'settings/page:' + path
let val = localStorage.getItem(key) let val = this.storage.getItem(key)
try { try {
if (val !== null) { if (val !== null) {
val = JSON.parse(val) val = JSON.parse(val)
@ -297,7 +300,7 @@ export class PageActions extends HTMLElement {
data.connections[otherDir] ?? {} data.connections[otherDir] ?? {}
) )
data.connections[otherDir][this.path] = access data.connections[otherDir][this.path] = access
localStorage.setItem( this.storage.setItem(
key, JSON.stringify(data) key, JSON.stringify(data)
) )
} }
@ -311,7 +314,7 @@ export class PageActions extends HTMLElement {
) )
for (const [path, access] of selfEntries) { for (const [path, access] of selfEntries) {
const key = 'settings/page:' + path const key = 'settings/page:' + path
let val = localStorage.getItem(key) let val = this.storage.getItem(key)
try { try {
if (val !== null) { if (val !== null) {
val = JSON.parse(val) val = JSON.parse(val)
@ -327,7 +330,7 @@ export class PageActions extends HTMLElement {
const accessValue = data.connections[otherDir][oldPath] const accessValue = data.connections[otherDir][oldPath]
data.connections[otherDir][newPath] = accessValue data.connections[otherDir][newPath] = accessValue
data.connections[otherDir][oldPath] = undefined data.connections[otherDir][oldPath] = undefined
localStorage.setItem( this.storage.setItem(
key, JSON.stringify(data) key, JSON.stringify(data)
) )
} }

@ -141,7 +141,7 @@ export class Page extends HTMLElement {
set body(value) { set body(value) {
try { try {
localStorage.setItem(this.path, value) this.storage.setItem(this.path, value)
} catch (err) { } catch (err) {
console.error(err) console.error(err)
} }
@ -149,7 +149,7 @@ export class Page extends HTMLElement {
get body() { get body() {
try { try {
return localStorage.getItem(this.path) return this.storage.getItem(this.path)
} catch (err) { } catch (err) {
console.error(err) console.error(err)
return '' return ''

Loading…
Cancel
Save