refactor(client): typed localStorage

This commit is contained in:
syuilo
2023-01-07 10:13:02 +09:00
parent a42b03c154
commit 91503405b4
25 changed files with 157 additions and 88 deletions

View File

@ -1,6 +1,7 @@
import { utils, values } from '@syuilo/aiscript';
import * as os from '@/os';
import { $i } from '@/account';
import { miLocalStorage } from '@/local-storage';
export function createAiScriptEnv(opts) {
let apiRequests = 0;
@ -32,12 +33,12 @@ export function createAiScriptEnv(opts) {
}),
'Mk:save': values.FN_NATIVE(([key, value]) => {
utils.assertString(key);
localStorage.setItem('aiscript:' + opts.storageKey + ':' + key.value, JSON.stringify(utils.valToJs(value)));
miLocalStorage.setItem(`aiscript:${opts.storageKey}:${key.value}`, JSON.stringify(utils.valToJs(value)));
return values.NULL;
}),
'Mk:load': values.FN_NATIVE(([key]) => {
utils.assertString(key);
return utils.jsToVal(JSON.parse(localStorage.getItem('aiscript:' + opts.storageKey + ':' + key.value)));
return utils.jsToVal(JSON.parse(miLocalStorage.getItem(`aiscript:${opts.storageKey}:${key.value}`)));
}),
};
}

View File

@ -9,6 +9,7 @@ import copyToClipboard from '@/scripts/copy-to-clipboard';
import { url } from '@/config';
import { noteActions } from '@/store';
import { notePage } from '@/filters/note';
import { miLocalStorage } from '@/local-storage';
export function getNoteMenu(props: {
note: misskey.entities.Note;
@ -181,7 +182,7 @@ export function getNoteMenu(props: {
props.translating.value = true;
const res = await os.api('notes/translate', {
noteId: appearNote.id,
targetLang: localStorage.getItem('lang') || navigator.language,
targetLang: miLocalStorage.getItem('lang') || navigator.language,
});
props.translating.value = false;
props.translation.value = res;

View File

@ -22,15 +22,15 @@ if (idbAvailable) {
export async function get(key: string) {
if (idbAvailable) return iget(key);
return JSON.parse(localStorage.getItem(fallbackName(key)));
return JSON.parse(window.localStorage.getItem(fallbackName(key)));
}
export async function set(key: string, val: any) {
if (idbAvailable) return iset(key, val);
return localStorage.setItem(fallbackName(key), JSON.stringify(val));
return window.localStorage.setItem(fallbackName(key), JSON.stringify(val));
}
export async function del(key: string) {
if (idbAvailable) return idel(key);
return localStorage.removeItem(fallbackName(key));
return window.localStorage.removeItem(fallbackName(key));
}

View File

@ -14,6 +14,7 @@ export type Theme = {
import lightTheme from '@/themes/_light.json5';
import darkTheme from '@/themes/_dark.json5';
import { deepClone } from './clone';
import { miLocalStorage } from '@/local-storage';
export const themeProps = Object.keys(lightTheme.props).filter(key => !key.startsWith('X'));
@ -84,8 +85,8 @@ export function applyTheme(theme: Theme, persist = true) {
document.documentElement.style.setProperty('color-schema', colorSchema);
if (persist) {
localStorage.setItem('theme', JSON.stringify(props));
localStorage.setItem('colorSchema', colorSchema);
miLocalStorage.setItem('theme', JSON.stringify(props));
miLocalStorage.setItem('colorSchema', colorSchema);
}
// 色計算など再度行えるようにクライアント全体に通知