refactor: 统一 storage 操作

This commit is contained in:
purocean
2021-07-11 13:44:30 +08:00
parent 5200f716b5
commit da26d03401
4 changed files with 25 additions and 7 deletions
+3 -2
View File
@@ -4,6 +4,7 @@ import Renderer from 'markdown-it/lib/renderer'
import { Plugin } from '@fe/context/plugin'
import crypto from '@fe/utils/crypto'
import { dataURItoBlobLink, openInNewWindow } from '@fe/utils'
import storage from '@fe/utils/storage'
const layoutStorageKey = 'mind-map-layout'
@@ -86,7 +87,7 @@ const render = async (ele: HTMLElement, content: string) => {
km.disable()
try {
await km.importData('text', code)
km.useTemplate(localStorage.getItem(layoutStorageKey) || 'default')
km.useTemplate(storage.get(layoutStorageKey, 'default'))
} catch (error) {
await km.importData('text', '转换错误\n 1. 请保证大纲只有一个根项目\n 2. 请保证大纲层级正确')
km.useTemplate('structure')
@@ -102,7 +103,7 @@ const render = async (ele: HTMLElement, content: string) => {
const index = tplList.indexOf(tpl)
const nextIndex = index > tplList.length - 2 ? 0 : index + 1
const layout = tplList[nextIndex]
localStorage.setItem(layoutStorageKey, layout)
storage.set(layoutStorageKey, layout)
km.useTemplate(layout)
km.execCommand('camera')
}
+5 -4
View File
@@ -5,6 +5,7 @@ import { Plugin } from '@fe/context/plugin'
import { getAction } from '@fe/context/action'
import * as api from '@fe/support/api'
import { FLAG_DISABLE_XTERM } from '@fe/support/global-args'
import storage from '@fe/utils/storage'
const cachePrefix = 'run_code_result_'
@@ -31,7 +32,7 @@ const RunCode = defineComponent({
hasResult = true
}
localStorage[`${cachePrefix}${hash.value}`] = result.value
storage.set(`${cachePrefix}${hash.value}`, result.value)
}
const run = async () => {
@@ -59,7 +60,7 @@ const RunCode = defineComponent({
})
return () => {
const runResult = result.value || localStorage[`${cachePrefix}${hash.value}`] || ''
const runResult = result.value || storage.get(`${cachePrefix}${hash.value}`, '')
return [
h('div', { class: 'run-code-action', style: 'position: sticky; left: 0; border-top: dashed 1px #888; margin: 1em 0' }, [
@@ -105,9 +106,9 @@ const RunPlugin = (md: Markdown) => {
}
const clearCache = () => {
Object.keys(localStorage).forEach(key => {
Object.keys(storage.getAll()).forEach(key => {
if (key.startsWith(cachePrefix)) {
localStorage.removeItem(key)
storage.remove(key)
}
})
}
+2 -1
View File
@@ -1,8 +1,9 @@
import storage from '@fe/utils/storage'
import { FLAG_DEMO } from './global-args'
import { useToast } from './toast'
if (FLAG_DEMO) {
localStorage.clear()
storage.clear()
const xFetch = window.fetch
const cache: {[key: string]: string} = {}
+15
View File
@@ -10,7 +10,22 @@ const set = function (key: string, value: any) {
window.localStorage[key] = JSON.stringify(value)
}
const remove = function (key: string) {
window.localStorage.removeItem(key)
}
const getAll = function () {
return window.localStorage
}
const clear = function () {
window.localStorage.clear()
}
export default {
clear,
remove,
getAll,
set,
get
}