diff --git a/src/obsidian/Plugin/PluginSettingsBase.ts b/src/obsidian/Plugin/PluginSettingsBase.ts index 9a9cc43..226d9da 100644 --- a/src/obsidian/Plugin/PluginSettingsBase.ts +++ b/src/obsidian/Plugin/PluginSettingsBase.ts @@ -20,7 +20,7 @@ export class PluginSettingsBase { * @param data - The data to initialize the settings from. */ public constructor(data: unknown) { - this.initFromJson(data); + this.init(data); } /** @@ -33,22 +33,24 @@ export class PluginSettingsBase { } /** - * Initializes the settings from JSON data. + * Determines if the settings should be saved after loading. * - * @param data - The data to initialize the settings from. + * @returns A boolean indicating whether the settings should be saved after loading. */ - public initFromJson(data: unknown): void { - if (data === undefined || data === null) { - return; - } + public shouldSaveAfterLoad(): boolean { + return false; + } - if (typeof data !== 'object' || Array.isArray(data)) { - const type = Array.isArray(data) ? 'Array' : typeof data; - console.error(`Invalid data type. Expected Object, got: ${type}`); - return; - } + /** + * Converts the settings to a JSON object. + * + * @returns The settings as a JSON object. + */ + public toJSON(): Record { + return Object.fromEntries(Object.entries(this)); + } - const record = data as Record; + protected initFromRecord(record: Record): void { for (const [key, value] of Object.entries(record)) { if (key in this) { this[key as keyof this] = value as this[keyof this]; @@ -59,20 +61,21 @@ export class PluginSettingsBase { } /** - * Determines if the settings should be saved after loading. + * Initializes the settings from JSON data. * - * @returns A boolean indicating whether the settings should be saved after loading. + * @param data - The data to initialize the settings from. */ - public shouldSaveAfterLoad(): boolean { - return false; - } + private init(data: unknown): void { + if (data === undefined || data === null) { + return; + } - /** - * Converts the settings to a JSON object. - * - * @returns The settings as a JSON object. - */ - public toJSON(): Record { - return Object.fromEntries(Object.entries(this)); + if (typeof data !== 'object' || Array.isArray(data)) { + const type = Array.isArray(data) ? 'Array' : typeof data; + console.error(`Invalid data type. Expected Object, got: ${type}`); + return; + } + + this.initFromRecord(data as Record); } }