1
0
Fork 0

add ability to dump and load settings from settings class

This commit is contained in:
ChaotiCryptidz 2022-03-07 18:40:14 +00:00
parent 509cf6205e
commit 814c73faca
No known key found for this signature in database

View file

@ -3,6 +3,32 @@ import { default_theme } from "../ThemeLoader";
type OnChangeListener = (key: string) => void;
// Documenting this here as Settings class is too big and clunky
export type SettingsData = {
// Base URL for Vault API
apiURL: string;
// User's Token
token: string;
// Page Direction (ltr, rtl)
pageDirection: string;
// Language code for all UI text
language: string;
// Theme (light, dark)
theme: string;
// Always view K/V secrets as a code block
kvAlwaysCodeView: boolean;
// Default language on K/V editor page
kvEditorDefaultLanguage: string;
// Default indentation on K/V editor page
kvEditorIndent: number;
// Default language on K/V view page
kvViewDefaultLanguage: string;
// Default indent on K/V indent page
kvViewIndent: number;
// Keys to hide values on (setting input type=password) like *****
kvHideKeyValues: string[];
};
export class Settings {
constructor() {
this.storage = localStorage;
@ -22,6 +48,33 @@ export class Settings {
}
}
// Partial as token and apiURL are not saved.
dumpSettings(): Partial<SettingsData> {
return {
pageDirection: this.pageDirection,
language: this.language,
theme: this.theme,
kvAlwaysCodeView: this.kvAlwaysCodeView,
kvEditorDefaultLanguage: this.kvEditorDefaultLanguage,
kvEditorIndent: this.kvEditorIndent,
kvViewDefaultLanguage: this.kvViewDefaultLanguage,
kvViewIndent: this.kvViewIndent,
kvHideKeyValues: this.kvHideKeyValues,
}
}
loadSettings(settings: Partial<SettingsData>) {
this.pageDirection = settings.pageDirection;
this.language = settings.language;
this.theme = settings.theme;
this.kvAlwaysCodeView = settings.kvAlwaysCodeView;
this.kvEditorDefaultLanguage = settings.kvEditorDefaultLanguage;
this.kvEditorIndent = settings.kvEditorIndent;
this.kvViewDefaultLanguage = settings.kvViewDefaultLanguage;
this.kvViewIndent = settings.kvViewIndent;
this.kvHideKeyValues = settings.kvHideKeyValues;
}
get apiURL(): string | null {
const apiurl = this.storage.getItem("apiURL") || "";
return apiurl;
@ -59,7 +112,6 @@ export class Settings {
get theme(): string {
return this.storage.getItem("theme") || default_theme;
}
set theme(value: string) {
this.storage.setItem("theme", value);
this.alertChange("theme");
@ -69,7 +121,6 @@ export class Settings {
const value = this.storage.getItem("kvAlwaysCodeView") || false;
return value == "true";
}
set kvAlwaysCodeView(value: boolean) {
this.storage.setItem("kvAlwaysCodeView", String(value));
this.alertChange("kvAlwaysCodeView");
@ -78,7 +129,6 @@ export class Settings {
get kvEditorDefaultLanguage(): string {
return this.storage.getItem("kvEditorDefaultLanguage") || "yaml";
}
set kvEditorDefaultLanguage(value: string) {
this.storage.setItem("kvEditorDefaultLanguage", value);
this.alertChange("kvEditorDefaultLanguage");
@ -89,7 +139,6 @@ export class Settings {
if (value) return parseInt(value);
return 2;
}
set kvEditorIndent(value: number) {
this.storage.setItem("kvEditorIndent", String(value));
this.alertChange("kvEditorIndent");
@ -98,7 +147,6 @@ export class Settings {
get kvViewDefaultLanguage(): string {
return this.storage.getItem("kvViewDefaultLanguage") || "yaml";
}
set kvViewDefaultLanguage(value: string) {
this.storage.setItem("kvViewDefaultLanguage", value);
this.alertChange("kvViewDefaultLanguage");
@ -109,7 +157,6 @@ export class Settings {
if (value) return parseInt(value);
return 2;
}
set kvViewIndent(value: number) {
this.storage.setItem("kvViewIndent", String(value));
this.alertChange("kvViewIndent");
@ -119,7 +166,6 @@ export class Settings {
const value = this.storage.getItem("kvHideKeyValues") || "";
return value.split(",");
}
set kvHideKeyValues(value: string[] | string) {
if (typeof value === "string") {
this.storage.setItem("kvHideKeyValues", value);