diff --git a/src/settingsTab/index.ts b/src/settingsTab/index.ts index 4b0df1b..fd686c6 100644 --- a/src/settingsTab/index.ts +++ b/src/settingsTab/index.ts @@ -34,6 +34,7 @@ export class SettingsTab extends PluginSettingTab { } this.highlightsFolder(); + this.myClippingsFileLocation(); this.amazonRegion(); this.downloadBookMetadata(); this.syncOnBoot(); @@ -42,6 +43,24 @@ export class SettingsTab extends PluginSettingTab { this.sponsorMe(); } + + private async myClippingsFileLocation(): void { + new Setting(this.containerEl) + .setName('My Clippings file location') + .setDesc( + "Set a path for the plugin to check for your \"My Clippings\" file, instead of asking to find it each time." + ) + .addText(text => { + return text + .setPlaceholder('') + .setValue(get(settingsStore).myClippingsFileLocation) + .onChange((async (value: string) => { + await settingsStore.actions.setMyClippingsFileLocation(value); + })) + + }) + } + private async logout(): Promise { const syncMessage = get(settingsStore).lastSyncDate ? `Last sync ${moment(get(settingsStore).lastSyncDate).fromNow()}` diff --git a/src/store/settingsStore.ts b/src/store/settingsStore.ts index af3dadf..06f2469 100644 --- a/src/store/settingsStore.ts +++ b/src/store/settingsStore.ts @@ -14,6 +14,7 @@ type Settings = { fileNameTemplate?: string; syncOnBoot: boolean; downloadBookMetadata: boolean; + myClippingsFileLocation: string; }; const DEFAULT_SETTINGS: Settings = { @@ -23,6 +24,7 @@ const DEFAULT_SETTINGS: Settings = { isLoggedIn: false, syncOnBoot: false, downloadBookMetadata: true, + myClippingsFileLocation: '', }; const createSettingsStore = () => { @@ -121,6 +123,13 @@ const createSettingsStore = () => { }); }; + const setMyClippingsFileLocation = (value: string) => { + store.update((state => { + state.myClippingsFileLocation = value; + return state; + })); + } + const setFileNameTemplate = (value: string) => { store.update((state) => ({ ...state, fileNameTemplate: value })); }; @@ -159,6 +168,7 @@ const createSettingsStore = () => { setSyncOnBoot, setDownloadBookMetadata, setAmazonRegion, + setMyClippingsFileLocation, upgradeStoreState, }, }; diff --git a/src/sync/syncClippings/index.ts b/src/sync/syncClippings/index.ts index 20d6ce5..9f51a89 100644 --- a/src/sync/syncClippings/index.ts +++ b/src/sync/syncClippings/index.ts @@ -3,12 +3,18 @@ import { openDialog } from './openDialog'; import { parseBooks } from './parseBooks'; import type { SyncManager } from '~/sync'; +import { get } from 'svelte/store'; +import { settingsStore } from '~/store'; +import { existsSync } from 'fs'; + + +type FindMyClippingsFileResponse = [ file: string, canceled: boolean ]; + export default class SyncKindleClippings { constructor(private syncManager: SyncManager) {} public async startSync(): Promise { - const [clippingsFile, canceled] = await openDialog(); - + const [clippingsFile, canceled] = await this.findMyClippingsFile(); if (canceled) { return; // Do nothing... } @@ -29,4 +35,20 @@ export default class SyncKindleClippings { console.error(message); } } + + public async findMyClippingsFile(): Promise { + /* First, check if setting for My Clippings path is specified */ + const myClippingsPath = await get(settingsStore).myClippingsFileLocation; + + /* Check if this path actually exists */ + const exists = myClippingsPath ? existsSync(myClippingsPath) : false; + + if(exists){ + /* We found it! */ + return [ myClippingsPath, false ]; + } else { + /* We didn't... summon the file picker */ + return await openDialog(); + } + } }