Skip to content

Commit

Permalink
Merge pull request #119 from kitschpatrol/edge-curl
Browse files Browse the repository at this point in the history
feat: add cover image edge curl option
  • Loading branch information
anpigon authored Oct 9, 2024
2 parents 72d6625 + ceb3240 commit 520dc53
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ You can set the template file location. There is an example template at the bott
You can set up the services that you use to search for books. Only Google and Naver(네이버) are available now.
To use Naver Book Search, clientId and clientSecret are required. I will explain how to get clientId and clientSecret from Naver on my blog.

### Cover Image Edge Curl

By default, the Google Books API adds a "page curl" effect to image thumbnails. Disabling this toggle will remove this effect from the cover images.

### Cover Image Saving

This feature allows for the automatic downloading and saving of book cover images directly into your Obsidian vault.
Expand Down
2 changes: 1 addition & 1 deletion src/apis/base_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ConfigurationError extends Error {
export function factoryServiceProvider(settings: BookSearchPluginSettings): BaseBooksApiImpl {
switch (settings.serviceProvider) {
case ServiceProvider.google:
return new GoogleBooksApi(settings.localePreference, settings.apiKey);
return new GoogleBooksApi(settings.localePreference, settings.enableCoverImageEdgeCurl, settings.apiKey);
case ServiceProvider.naver:
validateNaverSettings(settings);
return new NaverBooksApi(settings.naverClientId, settings.naverClientSecret);
Expand Down
15 changes: 14 additions & 1 deletion src/apis/google_books_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Book creation', () => {
canonicalVolumeLink: 'https://play.google.com/store/books/details?id=QVjPsd1UukEC',
};

const api: GoogleBooksApi = new GoogleBooksApi('default');
const api: GoogleBooksApi = new GoogleBooksApi('default', true);
const book: Book = api.createBookItem(volumeInfo);

it('Title', () => {
Expand Down Expand Up @@ -107,4 +107,17 @@ describe('Book creation', () => {
it('ISBN 13', () => {
expect(book.isbn13).toEqual(volumeInfo.industryIdentifiers[1].identifier);
});

it('Enables Edge curl', () => {
expect(book.coverUrl).toContain('&edge=curl');
expect(book.coverSmallUrl).toContain('&edge=curl');
});

it('Disables Edge curl', () => {
const api: GoogleBooksApi = new GoogleBooksApi('default', false);
const book: Book = api.createBookItem(volumeInfo);

expect(book.coverUrl).not.toContain('&edge=curl');
expect(book.coverSmallUrl).not.toContain('&edge=curl');
});
});
11 changes: 9 additions & 2 deletions src/apis/google_books_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class GoogleBooksApi implements BaseBooksApiImpl {

constructor(
private readonly localePreference: string,
private readonly enableCoverImageEdgeCurl: boolean,
private readonly apiKey?: string,
) {}

Expand Down Expand Up @@ -64,8 +65,8 @@ export class GoogleBooksApi implements BaseBooksApiImpl {
categories: item.categories,
publisher: item.publisher,
totalPage: item.pageCount,
coverUrl: item.imageLinks?.thumbnail ?? '',
coverSmallUrl: item.imageLinks?.smallThumbnail ?? '',
coverUrl: this.setCoverImageEdgeCurl(item.imageLinks?.thumbnail ?? '', this.enableCoverImageEdgeCurl),
coverSmallUrl: this.setCoverImageEdgeCurl(item.imageLinks?.smallThumbnail ?? '', this.enableCoverImageEdgeCurl),
publishDate: item.publishedDate || '',
description: item.description,
link: item.canonicalVolumeLink || item.infoLink,
Expand Down Expand Up @@ -99,6 +100,12 @@ export class GoogleBooksApi implements BaseBooksApiImpl {
return list && list.length > 1 ? list.map(item => item.trim()).join(', ') : list?.[0] ?? '';
}

private setCoverImageEdgeCurl(url: string, enabled: boolean): string {
// Edge curl is included in the cover image URL parameters by default,
// so we need to remove it if it's disabled
return enabled ? url : url.replace('&edge=curl', '');
}

static convertGoogleBookImageURLSize(url: string, zoom: number) {
return url.replace(/(&zoom)=\d/, `$1=${zoom}`);
}
Expand Down
27 changes: 27 additions & 0 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface BookSearchPluginSettings {
openPageOnCompletion: boolean;
showCoverImageInSearch: boolean;
enableCoverImageSave: boolean;
enableCoverImageEdgeCurl: boolean;
coverImagePath: string;
askForLocale: boolean;
}
Expand All @@ -52,6 +53,7 @@ export const DEFAULT_SETTINGS: BookSearchPluginSettings = {
openPageOnCompletion: true,
showCoverImageInSearch: false,
enableCoverImageSave: false,
enableCoverImageEdgeCurl: true,
coverImagePath: '',
askForLocale: true,
};
Expand Down Expand Up @@ -162,6 +164,8 @@ export class BookSearchSettingTab extends PluginSettingTab {
let serviceProviderExtraSettingButton: HTMLElement;
// eslint-disable-next-line prefer-const
let preferredLocaleDropdownSetting: Setting;
// eslint-disable-next-line prefer-const
let coverImageEdgeCurlToggleSetting: Setting;
const hideServiceProviderExtraSettingButton = () => {
serviceProviderExtraSettingButton.addClass('book-search-plugin__hide');
};
Expand All @@ -178,15 +182,28 @@ export class BookSearchSettingTab extends PluginSettingTab {
preferredLocaleDropdownSetting.settingEl.removeClass('book-search-plugin__hide');
}
};
const hideCoverImageEdgeCurlToggle = () => {
if (coverImageEdgeCurlToggleSetting !== undefined) {
coverImageEdgeCurlToggleSetting.settingEl.addClass('book-search-plugin__hide');
}
};
const showCoverImageEdgeCurlToggle = () => {
if (coverImageEdgeCurlToggleSetting !== undefined) {
coverImageEdgeCurlToggleSetting.settingEl.removeClass('book-search-plugin__hide');
}
};

const toggleServiceProviderExtraSettings = (
serviceProvider: ServiceProvider = this.plugin.settings?.serviceProvider,
) => {
if (serviceProvider === ServiceProvider.naver) {
showServiceProviderExtraSettingButton();
hideServiceProviderExtraSettingDropdown();
hideCoverImageEdgeCurlToggle();
} else {
hideServiceProviderExtraSettingButton();
showServiceProviderExtraSettingDropdown();
showCoverImageEdgeCurlToggle();
}
};
new Setting(containerEl)
Expand Down Expand Up @@ -263,6 +280,16 @@ export class BookSearchSettingTab extends PluginSettingTab {
}),
);

coverImageEdgeCurlToggleSetting = new Setting(containerEl)
.setName('Enable Cover Image Edge Curl Effect')
.setDesc('Toggle to show or hide page curl effect in cover images.')
.addToggle(toggle =>
toggle.setValue(this.plugin.settings.enableCoverImageEdgeCurl).onChange(async value => {
this.plugin.settings.enableCoverImageEdgeCurl = value;
await this.plugin.saveSettings();
}),
);

new Setting(containerEl)
.setName('Enable Cover Image Save')
.setDesc('Toggle to enable or disable saving cover images in notes.')
Expand Down

0 comments on commit 520dc53

Please sign in to comment.