-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathconfig.ts
More file actions
122 lines (117 loc) · 3.81 KB
/
Copy pathconfig.ts
File metadata and controls
122 lines (117 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { Config, Settings } from "../types/fullscreen";
import defaultsDeep from "lodash.defaultsdeep";
import { DEFAULTS } from "../constants";
let CONFIG: Config | null = null;
let ACTIVE: "tv" | "def" | null = null;
function getConfig(DEFAULTS: Config): Config {
try {
const parsed = JSON.parse(localStorage.getItem("full-screen-config") ?? "{}");
if (Boolean(parsed) && typeof parsed === "object") {
defaultsDeep(parsed, DEFAULTS);
const def = parsed.def as Record<string, unknown> | undefined;
const legacyAlbum = def?.albumArtSizing;
if (
def &&
typeof legacyAlbum === "string" &&
[
"classic",
"auto",
"scale125",
"scale15",
"scale175",
"scale2",
"scale25",
].includes(legacyAlbum)
) {
(parsed as unknown as Config).defaultModeAlbumArtSizing =
legacyAlbum as Config["defaultModeAlbumArtSizing"];
delete def.albumArtSizing;
}
localStorage.setItem("full-screen-config", JSON.stringify(parsed));
return parsed;
}
throw "";
} catch {
localStorage.setItem("full-screen-config", JSON.stringify(DEFAULTS));
return DEFAULTS;
}
}
function saveConfig(CONFIG: Config) {
localStorage.setItem("full-screen-config", JSON.stringify(CONFIG));
}
const ConfigManager = {
get(key: keyof Settings) {
if (CONFIG === null) {
CONFIG = getConfig(DEFAULTS);
}
if (ACTIVE === null) {
ACTIVE = CONFIG.tvMode ? "tv" : "def";
}
return CONFIG[ACTIVE][key];
},
set(key: keyof Settings, value: Settings[keyof Settings]) {
if (CONFIG === null) {
CONFIG = getConfig(DEFAULTS);
}
if (ACTIVE === null) {
ACTIVE = CONFIG.tvMode ? "tv" : "def";
}
//@ts-ignore: using bracket notation to access key
CONFIG[ACTIVE][key] = value;
saveConfig(CONFIG);
document.dispatchEvent(new CustomEvent(key, { detail: value }));
},
getGlobal(key: keyof Config) {
if (CONFIG === null) {
CONFIG = getConfig(DEFAULTS);
}
if (ACTIVE === null) {
ACTIVE = CONFIG.tvMode ? "tv" : "def";
}
return CONFIG[key];
},
setGlobal(key: keyof Config, value: Config[keyof Config]) {
if (CONFIG === null) {
CONFIG = getConfig(DEFAULTS);
}
if (ACTIVE === null) {
ACTIVE = CONFIG.tvMode ? "tv" : "def";
}
//@ts-ignore: using bracket notation to access key
CONFIG[key] = value;
saveConfig(CONFIG);
document.dispatchEvent(new CustomEvent(key, { detail: value }));
},
getMode() {
if (CONFIG === null) {
CONFIG = getConfig(DEFAULTS);
}
if (ACTIVE === null) {
ACTIVE = CONFIG.tvMode ? "tv" : "def";
}
return ACTIVE;
},
setMode(modeValue: "tv" | "def") {
ACTIVE = modeValue;
},
resetSettings(key: keyof Settings | null = null, isGlobal = false) {
if (CONFIG === null) {
CONFIG = getConfig(DEFAULTS);
}
if (isGlobal) {
CONFIG = DEFAULTS;
} else {
if (ACTIVE === null) {
ACTIVE = CONFIG.tvMode ? "tv" : "def";
}
if (key === null) {
CONFIG[ACTIVE] = DEFAULTS[ACTIVE];
} else {
//@ts-ignore: using bracket notation to access key
CONFIG[ACTIVE][key] = DEFAULTS[ACTIVE][key];
}
}
saveConfig(CONFIG);
},
};
export default ConfigManager;