mirror of
https://github.com/Kornstalx/5etools-mirror-2.github.io.git
synced 2025-10-28 20:45:35 -05:00
v1.208.0
This commit is contained in:
66
js/utils-config/utils-config-config.js
Normal file
66
js/utils-config/utils-config-config.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import {SETTINGS_GROUPS} from "./utils-config-registry.js";
|
||||
import {UtilConfigHelpers} from "./util-config-helpers.js";
|
||||
|
||||
export class VetoolsConfig {
|
||||
static _STORAGE_KEY = "config";
|
||||
|
||||
static _CONFIG = null;
|
||||
|
||||
static _init () {
|
||||
if (this._CONFIG) return;
|
||||
|
||||
this._CONFIG = StorageUtil.syncGet(this._STORAGE_KEY) || {};
|
||||
|
||||
SETTINGS_GROUPS
|
||||
.forEach(settingsGroup => settingsGroup.mutDefaults(this._CONFIG));
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
static get (groupId, configId) {
|
||||
this._init();
|
||||
return MiscUtil.get(this._CONFIG, groupId, configId);
|
||||
}
|
||||
|
||||
static set (groupId, configId, val) {
|
||||
this._init();
|
||||
MiscUtil.set(this._CONFIG, groupId, configId, val);
|
||||
this._save();
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
static _save () {
|
||||
StorageUtil.syncSet(this._STORAGE_KEY, this._CONFIG);
|
||||
}
|
||||
|
||||
static _saveThrottled = MiscUtil.throttle(this._save.bind(this), 50);
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
static getConfigComp () {
|
||||
this._init();
|
||||
|
||||
const state = {};
|
||||
Object.entries(this._CONFIG)
|
||||
.forEach(([groupId, groupTo]) => {
|
||||
Object.entries(groupTo)
|
||||
.forEach(([configId, val]) => {
|
||||
state[UtilConfigHelpers.packSettingId(groupId, configId)] = MiscUtil.copyFast(val);
|
||||
});
|
||||
});
|
||||
|
||||
const comp = BaseComponent.fromObject(state, "*");
|
||||
comp._addHookAllBase(() => {
|
||||
Object.entries(comp._state)
|
||||
.forEach(([settingId, v]) => {
|
||||
const {groupId, configId} = UtilConfigHelpers.unpackSettingId(settingId);
|
||||
MiscUtil.set(this._CONFIG, groupId, configId, v);
|
||||
});
|
||||
|
||||
this._saveThrottled();
|
||||
});
|
||||
|
||||
return comp;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user