Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "My Clippings Path" preference to avoid having to find it manually each time #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/settingsTab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class SettingsTab extends PluginSettingTab {
}

this.highlightsFolder();
this.myClippingsFileLocation();
this.amazonRegion();
this.downloadBookMetadata();
this.syncOnBoot();
Expand All @@ -42,6 +43,24 @@ export class SettingsTab extends PluginSettingTab {
this.sponsorMe();
}


private async myClippingsFileLocation(): void {
new Setting(this.containerEl)
.setName('My Clippings file location')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this preference make more sense if it was called "Default 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<void> {
const syncMessage = get(settingsStore).lastSyncDate
? `Last sync ${moment(get(settingsStore).lastSyncDate).fromNow()}`
Expand Down
10 changes: 10 additions & 0 deletions src/store/settingsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Settings = {
fileNameTemplate?: string;
syncOnBoot: boolean;
downloadBookMetadata: boolean;
myClippingsFileLocation: string;
};

const DEFAULT_SETTINGS: Settings = {
Expand All @@ -23,6 +24,7 @@ const DEFAULT_SETTINGS: Settings = {
isLoggedIn: false,
syncOnBoot: false,
downloadBookMetadata: true,
myClippingsFileLocation: '',
};

const createSettingsStore = () => {
Expand Down Expand Up @@ -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 }));
};
Expand Down Expand Up @@ -159,6 +168,7 @@ const createSettingsStore = () => {
setSyncOnBoot,
setDownloadBookMetadata,
setAmazonRegion,
setMyClippingsFileLocation,
upgradeStoreState,
},
};
Expand Down
26 changes: 24 additions & 2 deletions src/sync/syncClippings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const [clippingsFile, canceled] = await openDialog();

const [clippingsFile, canceled] = await this.findMyClippingsFile();
if (canceled) {
return; // Do nothing...
}
Expand All @@ -29,4 +35,20 @@ export default class SyncKindleClippings {
console.error(message);
}
}

public async findMyClippingsFile(): Promise<FindMyClippingsFileResponse> {
/* 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();
}
}
}