diff --git a/lib/class/Storage/browser.ts b/lib/class/Storage/browser.ts index d4ccf2c..b673ab5 100644 --- a/lib/class/Storage/browser.ts +++ b/lib/class/Storage/browser.ts @@ -17,11 +17,15 @@ export default class BrowserStorage implements StorageTool { await this.#item.set({ ...data, [key]: newdata }); } + async getAll() { + return await this.#item.get(); + } + async get(value: keyof T) { - return (await this.#item.get())[value]; + return (await this.getAll())[value]; } - async set(value: T) { + async set(value: Partial) { const data = await this.#item.get(); await this.#item.set({ ...data, value }); diff --git a/lib/class/Storage/common.ts b/lib/class/Storage/common.ts new file mode 100644 index 0000000..a557aa5 --- /dev/null +++ b/lib/class/Storage/common.ts @@ -0,0 +1,28 @@ +import { WebsiteIds } from "@/data/websites"; + +import BrowserStorage from "./browser"; + +export interface UnivCommonConfig { + rainbow?: boolean; + dark?: boolean; +} + +export type CommonConfig = Record; + +export const CommonStorage = new (class { + #storage: BrowserStorage; + constructor() { + this.#storage = new BrowserStorage("common"); + } + + async get(id: WebsiteIds, key: keyof UnivCommonConfig) { + const isRainbowEnabled = (await this.#storage.get(id))[key]; + return isRainbowEnabled; + } + + async set(id: WebsiteIds, value: Partial) { + const data = await this.#storage.getAll(); + + await this.#storage.set({ ...data, [id]: { ...data[id], ...value } }); + } +})(); diff --git a/lib/class/Storage/type.ts b/lib/class/Storage/type.ts index 37ee608..8530a16 100644 --- a/lib/class/Storage/type.ts +++ b/lib/class/Storage/type.ts @@ -8,4 +8,5 @@ export type StorageKeys = | "installed" | "auto-2fa" | "quick-switch"; -export type StorageIds = WebsiteIds | "other"; + +export type StorageIds = "common" | "other" | WebsiteIds; diff --git a/lib/class/UnivWebsite/index.ts b/lib/class/UnivWebsite/index.ts index 17870cd..e2cb828 100644 --- a/lib/class/UnivWebsite/index.ts +++ b/lib/class/UnivWebsite/index.ts @@ -7,20 +7,15 @@ import { WebsiteIds } from "@/data/websites"; import isTrue from "../../utils/isTrue"; import { DarkApplicator, HiddenApplicator, RainbowApplicator } from "../ClassApplicator"; import BrowserStorage from "../Storage/browser"; +import { CommonStorage } from "../Storage/common"; import { OtherStorage } from "../Storage/other"; import StorageTool from "../Storage/storage"; -export interface UnivConfig { - rainbow?: boolean; - dark?: boolean; -} - // UnivWebSiteはゲーミング化するウェブサイトを定義したクラス // 型変数とoptionsプロパティによって任意の情報を追加できる -export class UnivWebsite { +export class UnivWebsite { // 基本情報 id: WebsiteIds; - storage: StorageTool; // Applicator @@ -50,19 +45,19 @@ export class UnivWebsite { } async initilizeWebsiteDb() { - await this.storage.set({ + await CommonStorage.set(this.id, { rainbow: false, dark: false, }); } async isRainbowEnabled() { - const isRainbowEnabled = await this.storage.get("rainbow"); + const isRainbowEnabled = await CommonStorage.get(this.id, "rainbow"); return isTrue(isRainbowEnabled); } async isDarkEnabled() { - const isDarkEnabled = await this.storage.get("dark"); + const isDarkEnabled = await CommonStorage.get(this.id, "dark"); return isTrue(isDarkEnabled); } @@ -77,23 +72,23 @@ export class UnivWebsite { if (isRainbowEnabled) { // CSSのためにHTML要素にデータ属性を追加 document.documentElement.dataset.gaming_gundai = "true"; - this.storage.set({ rainbow: true }); + CommonStorage.set(this.id, { rainbow: true }); this.rainbow.enable(); } else { // CSSのためにHTML要素にデータ属性を追加 document.documentElement.dataset.gaming_gundai = "false"; - this.storage.set({ rainbow: false }); + CommonStorage.set(this.id, { rainbow: false }); this.rainbow.disable(); } const isDarkEnabled = await this.isDarkEnabled(); if (isDarkEnabled) { document.documentElement.dataset.gaming_gundai_dark = "true"; - this.storage.set({ dark: true }); + CommonStorage.set(this.id, { dark: true }); this.dark.enable(); } else { document.documentElement.dataset.gaming_gundai_dark = "false"; - this.storage.set({ dark: false }); + CommonStorage.set(this.id, { dark: false }); this.dark.disable(); } @@ -117,4 +112,4 @@ export class UnivWebsite { } } -export class GundaiWebSite extends UnivWebsite {} +export class GundaiWebSite extends UnivWebsite {} diff --git a/lib/class/UnivWebsite/test.ts b/lib/class/UnivWebsite/test.ts deleted file mode 100644 index 4171bd5..0000000 --- a/lib/class/UnivWebsite/test.ts +++ /dev/null @@ -1,20 +0,0 @@ -interface Config { - hoge: boolean; - fugo: string; -} - -export class Hoge { - config1: T; - config2: T; - constructor() { - const config: Config = { - hoge: true, - fugo: "fugo", - }; - this.config1 = config; - - this.config2 = {} as T; - this.config2.fugo = config.fugo; - this.config2.hoge = config.hoge; - } -} diff --git a/lib/components/ToggleWithStorage.tsx b/lib/components/ToggleWithStorage.tsx index f2b91ac..ba00ef3 100644 --- a/lib/components/ToggleWithStorage.tsx +++ b/lib/components/ToggleWithStorage.tsx @@ -5,8 +5,8 @@ import { Toggle } from "react-daisyui"; import { ComponentColor } from "react-daisyui/dist/types"; import { StorageTool } from "@/class"; +import { UnivCommonConfig as UnivConfig } from "@/class/Storage/common"; import { OtherConfig, OtherStorage } from "@/class/Storage/other"; -import { UnivConfig } from "@/class/UnivWebsite"; import IsTrue from "@/utils/isTrue"; import { sendMsgToAllTab } from "@/utils/sendMsgToAllTab";