Skip to content

Commit

Permalink
merge existing settings when upgrading
Browse files Browse the repository at this point in the history
  • Loading branch information
jgchristopher committed Jan 30, 2024
1 parent acda21a commit 3f5aacf
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 6 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
"tailwindcss": "npx tailwindcss -i ./input.css -o ./styles.css",
"version": "node ./src/build/version-bump.mjs && git add manifest.json versions.json",
"webpack": "webpack",
"build": "npm run build:bookmarklet && npm run build:bookmarkletgen && npm run compile && npm run tailwindcss && node esbuild.config.mjs production",
"dev": "npm run build && cp main.js styles.css manifest.json ~/gitprojects/obsidian-plugins/obsidian-clipper-projects/dev/",
"build": "npm run build:bookmarklet && npm run build:bookmarkletgen && npm run compile && npm run tailwindcss && node esbuild.config.mjs production",
"dev": "npm run build && npm run copy",
"copy": "cp main.js styles.css manifest.json /Users/johnchristopher/gitprojects/obsidian-plugins/dev_plugin_links/obsidian-clipper/",
"clean": "rimraf ./src/build/bookmarkletcode/index.js && rimraf ./src/build/bookmarkletcode/obsidian-clipper.min.js && rimraf ./src/build/bookmarkletcode/dist/*"
},
"keywords": [],
Expand Down
72 changes: 68 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
DEFAULT_SETTINGS,
type ObsidianClipperSettings,
ClipperType,
DEFAULT_CLIPPER_SETTING,
type OldClipperSettings,
DEFAULT_SETTINGS_EMPTY,
} from './settings/types';
import { ClippedData } from './clippeddata';
import { DailyPeriodicNoteEntry } from './periodicnotes/dailyperiodicnoteentry';
Expand All @@ -34,6 +37,8 @@ import {
BookmarkletLinksView,
VIEW_TYPE_EXAMPLE,
} from './views/BookmarkletLinksView';
import { deepmerge } from 'deepmerge-ts';
import { randomUUID } from 'crypto';

export default class ObsidianClipperPlugin extends Plugin {
settings: ObsidianClipperPluginSettings;
Expand Down Expand Up @@ -165,25 +170,84 @@ export default class ObsidianClipperPlugin extends Plugin {
}

async loadSettings() {
const mergedSettings = DEFAULT_SETTINGS;
const settingsData = await this.loadData();

// Existing data
if (settingsData !== null) {
if (!settingsData.hasOwnProperty('version')) {
console.log(
"Settings exist and haven't been migrated to version 2 or higher"
);
// mergedSettings = deepmerge(DEFAULT_SETTINGS, settingsData);
this.settings = this.mergeExistingSetting(settingsData);
this.saveSettings();
} else {
this.settings = settingsData;
}
this.settings = settingsData;
} else {
this.settings = mergedSettings;
this.settings = Object.assign({}, DEFAULT_SETTINGS, null);
}
}

async saveSettings() {
await this.saveData(this.settings);
}

mergeExistingSetting(
settingsData: OldClipperSettings
): ObsidianClipperPluginSettings {
const mergedSettings = deepmerge(
DEFAULT_SETTINGS_EMPTY,
{}
) as ObsidianClipperPluginSettings;
if (settingsData.useDailyNote === true) {
const transfered = deepmerge(
DEFAULT_CLIPPER_SETTING,
{}
) as ObsidianClipperSettings;
transfered.clipperId = randomUUID();
transfered.type = 'daily';
transfered.name = settingsData.dailyNoteHeading;
transfered.vaultName = this.app.vault.getName();
transfered.heading = settingsData.dailyNoteHeading;
transfered.tags = settingsData.tags;
transfered.timestampFormat = settingsData.timestampFormat;
transfered.dateFormat = settingsData.dateFormat;
transfered.openOnWrite = settingsData.dailyOpenOnWrite;
transfered.position = settingsData.dailyPosition;
transfered.entryTemplateLocation =
settingsData.dailyEntryTemplateLocation;
transfered.markdownSettings = settingsData.markdownSettings;
transfered.advancedStorage = settingsData.advanced;
transfered.advancedStorageFolder = settingsData.advancedStorageFolder;
mergedSettings.clippers.push(transfered);
}

if (settingsData.useWeeklyNote === true) {
const transfered = deepmerge(
DEFAULT_CLIPPER_SETTING,
{}
) as ObsidianClipperSettings;
transfered.clipperId = randomUUID();
transfered.type = 'weekly';
transfered.name = settingsData.weeklyNoteHeading;
transfered.vaultName = this.app.vault.getName();
transfered.heading = settingsData.weeklyNoteHeading;
transfered.tags = settingsData.tags;
transfered.timestampFormat = settingsData.timestampFormat;
transfered.dateFormat = settingsData.dateFormat;
transfered.openOnWrite = settingsData.weeklyOpenOnWrite;
transfered.position = settingsData.weeklyPosition;
transfered.entryTemplateLocation =
settingsData.weeklyEntryTemplateLocation;
transfered.markdownSettings = settingsData.markdownSettings;
transfered.advancedStorage = settingsData.advanced;
transfered.advancedStorageFolder = settingsData.advancedStorageFolder;
mergedSettings.clippers.push(transfered);
}

return mergedSettings;
}

handleCopyBookmarkletToClipboard(clipperId: string, notePath = '') {
const clipperSettings = this.settings.clippers.find(
(settings: ObsidianClipperSettings) => {
Expand Down
31 changes: 31 additions & 0 deletions src/settings/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ export interface ObsidianClipperPluginSettings {
experimentalBookmarkletComment: boolean;
}

export interface OldClipperSettings {
tags: string;
timestampFormat: string;
dateFormat: string;
dailyNoteHeading: string;
weeklyNoteHeading: string;
dailyOpenOnWrite: boolean;
useDailyNote: boolean;
dailyPosition: SectionPosition;
useWeeklyNote: boolean;
weeklyPosition: SectionPosition;
weeklyEntryTemplateLocation: string;
weeklyOpenOnWrite: boolean;
dailyEntryTemplateLocation: string;
markdownSettings: ObsidianClipperMarkdownSettings;
topicPosition: SectionPosition;
topicEntryTemplateLocation: string;
topicOpenOnWrite: boolean;
advanced: boolean;
advancedStorageFolder: string;
captureComments: boolean;
experimentalCanvas: boolean;
experimentalBookmarkletComment: boolean;
}

export interface BaseClipperSettings {
type: ClipperType;
name: string;
Expand Down Expand Up @@ -89,3 +114,9 @@ export const DEFAULT_SETTINGS: ObsidianClipperPluginSettings = {
version: 2.0,
experimentalBookmarkletComment: false,
};

export const DEFAULT_SETTINGS_EMPTY: ObsidianClipperPluginSettings = {
clippers: [],
version: 2.0,
experimentalBookmarkletComment: false,
};
31 changes: 31 additions & 0 deletions src/utils/daily-advanced-storage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"dailyNoteHeading": "Clipped 1",
"weeklyNoteHeading": "",
"tags": "",
"timestampFormat": "HH:mm",
"dateFormat": "MM/DD/YY",
"dailyOpenOnWrite": false,
"useDailyNote": true,
"dailyPosition": "append",
"useWeeklyNote": false,
"weeklyPosition": "append",
"weeklyOpenOnWrite": false,
"dailyEntryTemplateLocation": "",
"weeklyEntryTemplateLocation": "",
"topicEntryTemplateLocation": "",
"topicPosition": "append",
"topicOpenOnWrite": false,
"markdownSettings": {
"h1": "##",
"h2": "##",
"h3": "###",
"h4": "####",
"h5": "#####",
"h6": "######"
},
"advanced": true,
"advancedStorageFolder": "clippings",
"captureComments": false,
"experimentalCanvas": false,
"experimentalBookmarkletComment": false
}
Empty file added src/utils/daily-comments.json
Empty file.
Empty file added src/utils/daily-data.json
Empty file.
31 changes: 31 additions & 0 deletions src/utils/daily-weekly-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"dailyNoteHeading": "Clipped Daily",
"weeklyNoteHeading": "Clipped Weekly",
"tags": "some-tag,tags",
"timestampFormat": "HH:mm",
"dateFormat": "MM/DD/YY",
"dailyOpenOnWrite": false,
"useDailyNote": true,
"dailyPosition": "append",
"useWeeklyNote": true,
"weeklyPosition": "prepend",
"weeklyOpenOnWrite": false,
"dailyEntryTemplateLocation": "templates",
"weeklyEntryTemplateLocation": "other-templates",
"topicEntryTemplateLocation": "",
"topicPosition": "append",
"topicOpenOnWrite": false,
"markdownSettings": {
"h1": "##",
"h2": "##",
"h3": "###",
"h4": "####",
"h5": "#####",
"h6": "######"
},
"advanced": true,
"advancedStorageFolder": "clippings",
"captureComments": true,
"experimentalCanvas": false,
"experimentalBookmarkletComment": false
}
31 changes: 31 additions & 0 deletions src/utils/weekly-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"dailyNoteHeading": "",
"weeklyNoteHeading": "Clipped Weekly",
"tags": "",
"timestampFormat": "HH:mm",
"dateFormat": "MM/DD/YY",
"dailyOpenOnWrite": false,
"useDailyNote": false,
"dailyPosition": "append",
"useWeeklyNote": true,
"weeklyPosition": "prepend",
"weeklyOpenOnWrite": true,
"dailyEntryTemplateLocation": "",
"weeklyEntryTemplateLocation": "templates",
"topicEntryTemplateLocation": "",
"topicPosition": "append",
"topicOpenOnWrite": false,
"markdownSettings": {
"h1": "##",
"h2": "##",
"h3": "###",
"h4": "####",
"h5": "#####",
"h6": "######"
},
"advanced": false,
"advancedStorageFolder": "clippings",
"captureComments": false,
"experimentalCanvas": false,
"experimentalBookmarkletComment": false
}

0 comments on commit 3f5aacf

Please sign in to comment.