mirror of
https://github.com/alberti42/obsidian-plugins-annotations.git
synced 2026-07-22 10:10:24 +00:00
Backups
This commit is contained in:
parent
c402d7c65a
commit
5c43cc22b4
4 changed files with 28 additions and 18 deletions
37
src/main.ts
37
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<string,string> = {};
|
||||
pluginIdToNameMap: Record<string,string> = {};
|
||||
|
||||
|
|
@ -68,6 +68,7 @@ export default class PluginsAnnotations extends Plugin {
|
|||
|
||||
// Nested function to handle different versions of settings
|
||||
const getSettingsFromData = async (data: unknown): Promise<unknown> => {
|
||||
|
||||
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) => {
|
||||
|
|
|
|||
|
|
@ -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}"`);
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ export interface PluginAnnotationDict {
|
|||
export interface PluginBackup {
|
||||
name: string;
|
||||
date: Date;
|
||||
settings: PluginsAnnotationsSettingsWithoutBackups;
|
||||
settings: unknown;
|
||||
}
|
||||
|
||||
export interface PluginsAnnotationsSettings {
|
||||
|
|
|
|||
|
|
@ -97,4 +97,5 @@ div.plugin-comment-instructions strong {
|
|||
.plugin-comment-backup-buttons {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
Loading…
Reference in a new issue