From 5c43cc22b46c5284be6db3b66d9646f1b3b02ff2 Mon Sep 17 00:00:00 2001 From: Andrea Alberti Date: Thu, 15 Aug 2024 01:21:30 +0200 Subject: [PATCH] Backups --- src/main.ts | 37 +++++++++++++++++++++++-------------- src/settings_tab.ts | 6 +++--- src/types.ts | 2 +- styles/styles.css | 1 + 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/main.ts b/src/main.ts index 05c2076..a0397a3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,7 +24,7 @@ import { readAnnotationsFromMdFile, writeAnnotationsToMdFile } from 'manageAnnot import { sortPluginAnnotationsByName } from 'utils'; export default class PluginsAnnotations extends Plugin { - settings: PluginsAnnotationsSettings = {...DEFAULT_SETTINGS}; + settings: PluginsAnnotationsSettings = structuredClone(DEFAULT_SETTINGS); pluginNameToIdMap: Record = {}; pluginIdToNameMap: Record = {}; @@ -68,6 +68,7 @@ export default class PluginsAnnotations extends Plugin { // Nested function to handle different versions of settings const getSettingsFromData = async (data: unknown): Promise => { + if(data === null) { // if the file is empty return data; } else if (isPluginsAnnotationsSettings(data)) { @@ -75,7 +76,9 @@ export default class PluginsAnnotations extends Plugin { return settings; } else if (isSettingsFormat_1_4_0(data)) { // previous versions where the name of the plugins was not stored // Make a backup - await this.backupSettings('Backup before upgrading from 1.4 to 1.5',data); + await this.backupSettings('Settings before upgrade from 1.4 to 1.5',data); + + const default_settings = DEFAULT_SETTINGS; // Upgrade annotations format const upgradedAnnotations: PluginAnnotationDict = {}; @@ -95,16 +98,19 @@ export default class PluginsAnnotations extends Plugin { const newSettings: PluginsAnnotationsSettings = { ...oldSettings, annotations: upgradedAnnotations, - plugins_annotations_uuid: DEFAULT_SETTINGS.plugins_annotations_uuid, - backups: DEFAULT_SETTINGS.backups, - compatibility: DEFAULT_SETTINGS.compatibility, - markdown_file_path: DEFAULT_SETTINGS.markdown_file_path + plugins_annotations_uuid: default_settings.plugins_annotations_uuid, + backups: this.settings.backups, + compatibility: default_settings.compatibility, + markdown_file_path: default_settings.markdown_file_path }; wasUpdated = true; + return await getSettingsFromData(newSettings); } else if (isSettingsFormat_1_3_0(data)) { // previous versions where the name of the plugins was not stored // Make a backup - await this.backupSettings('Backup before upgrading from 1.3 to 1.4',data); + await this.backupSettings('Settings before upgrade from 1.3 to 1.4',data); + + const default_settings_1_4_0 = DEFAULT_SETTINGS_1_4_0 // Upgrade annotations format const upgradedAnnotations: PluginAnnotationDict_1_4_0 = {}; @@ -122,17 +128,20 @@ export default class PluginsAnnotations extends Plugin { const newSettings: PluginsAnnotationsSettings_1_4_0 = { ...oldSettings, annotations: upgradedAnnotations, - plugins_annotations_uuid: DEFAULT_SETTINGS_1_4_0.plugins_annotations_uuid, + plugins_annotations_uuid: default_settings_1_4_0.plugins_annotations_uuid, }; wasUpdated = true; return await getSettingsFromData(newSettings); } else { // Make a backup - await this.backupSettings('Backup before upgrading from 1.0 to 1.3',data); + await this.backupSettings('Settings before upgrade from 1.0 to 1.3',data); + + const default_settings_1_3_0 = structuredClone(DEFAULT_SETTINGS_1_3_0); // Very first version of the plugin 1.0 -- no options were stored, only the dictionary of annotations - const newSettings: PluginsAnnotationsSettings_1_3_0 = { ...DEFAULT_SETTINGS_1_3_0 }; - newSettings.annotations = isPluginAnnotationDictFormat_1_3_0(data) ? data : DEFAULT_SETTINGS_1_3_0.annotations; + const newSettings: PluginsAnnotationsSettings_1_3_0 = default_settings_1_3_0; + console.log("IS 1_3_0:",isPluginAnnotationDictFormat_1_3_0(data)); + newSettings.annotations = isPluginAnnotationDictFormat_1_3_0(data) ? data : default_settings_1_3_0.annotations; wasUpdated = true; return await getSettingsFromData(newSettings); } @@ -158,8 +167,8 @@ export default class PluginsAnnotations extends Plugin { settingsWithoutBackup = settings; } - // Use JSON.parse(JSON.stringify()) for a deep copy - const deepCopiedSettings = JSON.parse(JSON.stringify(settingsWithoutBackup)); + // Deep copy + const deepCopiedSettings = structuredClone(settingsWithoutBackup); // Add the backup with the deep-copied settings this.settings.backups.push({ @@ -184,7 +193,7 @@ export default class PluginsAnnotations extends Plugin { const {importedSettings, wasUpdated} = await this.importSettings(data); // Merge loaded settings with default settings - this.settings = Object.assign({}, DEFAULT_SETTINGS, importedSettings); + this.settings = Object.assign({}, structuredClone(DEFAULT_SETTINGS), importedSettings); if (this.settings.backups) { this.settings.backups.forEach((backup: PluginBackup) => { diff --git a/src/settings_tab.ts b/src/settings_tab.ts index 2a20aac..d26ab5c 100644 --- a/src/settings_tab.ts +++ b/src/settings_tab.ts @@ -349,7 +349,7 @@ export class PluginsAnnotationsSettingTab extends PluginSettingTab { const headerRow = tableDiv.createDiv({ cls: 'plugin-comment-backup-table-row header' }); headerRow.createDiv({ cls: 'plugin-comment-backup-table-cell', text: 'Backup name (click to edit)' }); headerRow.createDiv({ cls: 'plugin-comment-backup-table-cell', text: 'Created on' }); - headerRow.createDiv({ cls: 'plugin-comment-backup-table-cell', text: 'Actions' }); + headerRow.createDiv({ cls: 'plugin-comment-backup-table-cell', text: '' }); this.plugin.settings.backups.forEach((backup, index) => { const rowDiv = tableDiv.createDiv({ cls: 'plugin-comment-backup-table-row' }); @@ -365,7 +365,7 @@ export class PluginsAnnotationsSettingTab extends PluginSettingTab { this.plugin.debouncedSaveAnnotations(); }); - // Optionally, you can handle the Enter key to finish editing + // Handle the Enter key to finish editing nameDiv.addEventListener('keydown', (event) => { if (event.key === 'Enter') { event.preventDefault(); @@ -393,7 +393,7 @@ export class PluginsAnnotationsSettingTab extends PluginSettingTab { })); if(answer) { const backups = [...this.plugin.settings.backups]; // store a copy of the backups before restoring the old settings - this.plugin.loadSettings(this.plugin.settings.backups[index].settings); + await this.plugin.loadSettings(structuredClone(this.plugin.settings.backups[index].settings)); this.plugin.settings.backups = backups; // restore the copy of the backups await this.plugin.saveSettings(); // save the restored settings with the backups new Notice(`Annotations restored from backup "${backup.name}"`); diff --git a/src/types.ts b/src/types.ts index b5b098f..06efe2b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -15,7 +15,7 @@ export interface PluginAnnotationDict { export interface PluginBackup { name: string; date: Date; - settings: PluginsAnnotationsSettingsWithoutBackups; + settings: unknown; } export interface PluginsAnnotationsSettings { diff --git a/styles/styles.css b/styles/styles.css index 43e7dfe..05e81c6 100644 --- a/styles/styles.css +++ b/styles/styles.css @@ -97,4 +97,5 @@ div.plugin-comment-instructions strong { .plugin-comment-backup-buttons { display: flex; gap: 10px; + justify-content: flex-end; } \ No newline at end of file