2022-03-02 07:26:40 +08:00
|
|
|
import { load, store } from "./storage";
|
|
|
|
|
|
|
|
const SETTINGS_KEY = "settings";
|
2021-12-04 04:57:21 +08:00
|
|
|
|
2022-01-14 02:06:50 +08:00
|
|
|
export const EDITOR_FONT_SIZE = {
|
|
|
|
normal: 14,
|
|
|
|
large: 16,
|
|
|
|
};
|
|
|
|
|
2022-01-16 05:46:25 +08:00
|
|
|
export const EDITOR_THEME = {
|
|
|
|
default: "default",
|
|
|
|
highContrast: "highContrast",
|
|
|
|
};
|
|
|
|
|
2021-12-04 04:57:21 +08:00
|
|
|
const DEFAULT_SETTINGS = {
|
|
|
|
editor_auto_completion: true,
|
|
|
|
editor_auto_signature: true,
|
2022-01-14 02:06:50 +08:00
|
|
|
editor_font_size: EDITOR_FONT_SIZE.normal,
|
2022-01-16 05:46:25 +08:00
|
|
|
editor_theme: EDITOR_THEME.default,
|
2021-12-04 04:57:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2022-01-16 20:50:44 +08:00
|
|
|
* Stores local configuration and persists it across browser sessions.
|
2021-12-04 04:57:21 +08:00
|
|
|
*/
|
2022-01-16 20:50:44 +08:00
|
|
|
class SettingsStore {
|
|
|
|
constructor() {
|
|
|
|
this._subscribers = [];
|
|
|
|
this._settings = DEFAULT_SETTINGS;
|
|
|
|
|
|
|
|
this._loadSettings();
|
2021-12-04 04:57:21 +08:00
|
|
|
}
|
|
|
|
|
2022-01-16 20:50:44 +08:00
|
|
|
/**
|
|
|
|
* Returns the current settings.
|
|
|
|
*/
|
|
|
|
get() {
|
|
|
|
return this._settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores new settings.
|
|
|
|
*
|
|
|
|
* The given attributes are merged into the current settings.
|
|
|
|
*/
|
|
|
|
update(newSettings) {
|
|
|
|
const prevSettings = this._settings;
|
|
|
|
this._settings = { ...this._settings, ...newSettings };
|
|
|
|
this._subscribers.forEach((callback) =>
|
|
|
|
callback(this._settings, prevSettings)
|
|
|
|
);
|
|
|
|
this._storeSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers to settings changes.
|
|
|
|
*
|
|
|
|
* The given function is called immediately with the current
|
|
|
|
* settings and then on every change.
|
|
|
|
*/
|
|
|
|
getAndSubscribe(callback) {
|
|
|
|
this._subscribers.push(callback);
|
|
|
|
callback(this._settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
_loadSettings() {
|
2022-03-02 07:26:40 +08:00
|
|
|
const settings = load(SETTINGS_KEY);
|
2022-01-16 20:50:44 +08:00
|
|
|
|
2022-03-02 07:26:40 +08:00
|
|
|
if (settings) {
|
|
|
|
this._settings = { ...this._settings, ...settings };
|
2022-01-16 20:50:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_storeSettings() {
|
2022-03-02 07:26:40 +08:00
|
|
|
store(SETTINGS_KEY, this._settings);
|
2021-12-04 04:57:21 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-16 20:50:44 +08:00
|
|
|
|
|
|
|
export const settingsStore = new SettingsStore();
|