use storage instance

main
bat 3 years ago
parent 3cc25e6d37
commit 8696ad8228

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

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

@ -71,7 +71,7 @@ export class Layout extends HTMLElement {
this.editing = false
const prevPage = this.page
let isGroup = false
const body = localStorage.getItem(path) || ''
const body = this.storage.getItem(path) || ''
if (body.match(/^\s*{/)) {
try {
const bodyData = JSON.parse(body)
@ -86,6 +86,7 @@ export class Layout extends HTMLElement {
this.page = document.createElement(
isGroup ? 'm-file-group-page' : 'm-page'
)
this.page.storage = this.storage
if (isGroup) {
this.page.cspOff = this.csp === undefined
this.page.cspProfiles = this.cspProfiles
@ -117,7 +118,7 @@ export class Layout extends HTMLElement {
}
set editing(value) {
sessionStorage.setItem(
this.storage.session.setItem(
'editing', value ? 'true' : 'false'
)
this.header.editing = this.editing
@ -129,7 +130,7 @@ export class Layout extends HTMLElement {
get editing() {
try {
return (
sessionStorage.getItem('editing') === 'true'
this.storage.session.getItem('editing') === 'true'
)
} catch (e) {
return false
@ -142,4 +143,14 @@ export class Layout extends HTMLElement {
}
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() {
const text = localStorage.getItem(this.path)
const text = this.storage.getItem(this.path)
const sp = this.path.split('/')
const filename = sp[sp.length - 1]
const el = document.createElement('a')
@ -111,7 +111,7 @@ export class PageActions extends HTMLElement {
)
bGroup.addPrimary(this.text.rename, () => {
const newPath = input.value
const v = localStorage.getItem(newPath)
const v = this.storage.getItem(newPath)
if (v !== null || newPath === this.path) {
if (!errorEl) {
errorEl = document.createElement('p')
@ -124,10 +124,10 @@ export class PageActions extends HTMLElement {
}
const sKeyOld = 'settings/page:' + this.path
const sKeyNew = 'settings/page:' + newPath
const settingsJson = localStorage.getItem(sKeyOld)
const settingsJson = this.storage.getItem(sKeyOld)
if (settingsJson ?? true === true) {
localStorage.setItem(sKeyNew, settingsJson)
localStorage.removeItem(sKeyOld)
this.storage.setItem(sKeyNew, settingsJson)
this.storage.removeItem(sKeyOld)
let settingsData
try {
settingsData = JSON.parse(settingsJson)
@ -145,13 +145,13 @@ export class PageActions extends HTMLElement {
}
}
} else {
localStorage.removeItem(sKeyNew)
this.storage.removeItem(sKeyNew)
}
localStorage.setItem(
this.storage.setItem(
newPath,
localStorage.getItem(this.path)
this.storage.getItem(this.path)
)
localStorage.removeItem(this.path)
this.storage.removeItem(this.path)
dialog.close()
location.hash = newPath
})
@ -178,7 +178,7 @@ export class PageActions extends HTMLElement {
const btnText = this.text.duplicate
bGroup.addPrimary(btnText, () => {
const newPath = input.value
const v = localStorage.getItem(newPath)
const v = this.storage.getItem(newPath)
if (v !== null || newPath === this.path) {
if (!errorEl) {
errorEl = document.createElement('p')
@ -189,13 +189,13 @@ export class PageActions extends HTMLElement {
}
return
}
localStorage.setItem(
this.storage.setItem(
newPath,
localStorage.getItem(this.path)
this.storage.getItem(this.path)
)
localStorage.setItem(
this.storage.setItem(
'settings/page:' + newPath,
localStorage.getItem(this.path) ?? '{}'
this.storage.getItem(this.path) ?? '{}'
)
dialog.close()
location.hash = newPath
@ -222,8 +222,8 @@ export class PageActions extends HTMLElement {
'm-forms-button-group'
)
bGroup.addPrimary(this.text.delete_, () => {
localStorage.removeItem(this.path)
localStorage.removeItem(
this.storage.removeItem(this.path)
this.storage.removeItem(
'settings/page:' + this.path
)
location.hash = '/'
@ -246,6 +246,9 @@ export class PageActions extends HTMLElement {
)
settingsEl.cspProfiles = this.cspProfiles
settingsEl.path = this.path
settingsEl.checkExists = path => (
this.storage.getItem(path) !== null
)
settingsEl.data = this.page.settings
const h = document.createElement('h2')
h.innerText = this.text.settings
@ -283,7 +286,7 @@ export class PageActions extends HTMLElement {
)
for (const [path, access] of selfEntries) {
const key = 'settings/page:' + path
let val = localStorage.getItem(key)
let val = this.storage.getItem(key)
try {
if (val !== null) {
val = JSON.parse(val)
@ -297,7 +300,7 @@ export class PageActions extends HTMLElement {
data.connections[otherDir] ?? {}
)
data.connections[otherDir][this.path] = access
localStorage.setItem(
this.storage.setItem(
key, JSON.stringify(data)
)
}
@ -311,7 +314,7 @@ export class PageActions extends HTMLElement {
)
for (const [path, access] of selfEntries) {
const key = 'settings/page:' + path
let val = localStorage.getItem(key)
let val = this.storage.getItem(key)
try {
if (val !== null) {
val = JSON.parse(val)
@ -327,7 +330,7 @@ export class PageActions extends HTMLElement {
const accessValue = data.connections[otherDir][oldPath]
data.connections[otherDir][newPath] = accessValue
data.connections[otherDir][oldPath] = undefined
localStorage.setItem(
this.storage.setItem(
key, JSON.stringify(data)
)
}

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

Loading…
Cancel
Save