From b313d165c3f9978ef9d2b9e763b4ae07cf3b630e Mon Sep 17 00:00:00 2001 From: Hady Osman Date: Sun, 14 Nov 2021 22:27:22 +1300 Subject: [PATCH 1/6] Add file name template setting --- src/fileNameRenderer.ts | 26 +++++++++++++++++++ src/settingsTab/components/Chip.svelte | 14 ++++++++++ .../components/FileNameDescription.svelte | 14 ++++++++++ src/settingsTab/index.ts | 24 +++++++++++++++++ src/store/settingsStore.ts | 6 +++++ 5 files changed, 84 insertions(+) create mode 100644 src/fileNameRenderer.ts create mode 100644 src/settingsTab/components/Chip.svelte create mode 100644 src/settingsTab/components/FileNameDescription.svelte diff --git a/src/fileNameRenderer.ts b/src/fileNameRenderer.ts new file mode 100644 index 0000000..7c7c390 --- /dev/null +++ b/src/fileNameRenderer.ts @@ -0,0 +1,26 @@ +import nunjucks, { Environment } from 'nunjucks'; + +const DefaultTemplate = '{{shortTitle}}'; + +class FileNameRenderer { + private nunjucks: Environment; + + constructor() { + this.nunjucks = new nunjucks.Environment(null, { autoescape: false }); + } + + public validate(template: string): boolean { + try { + this.nunjucks.renderString(template, {}); + return true; + } catch (error) { + return false; + } + } + + public defaultTemplate(): string { + return DefaultTemplate; + } +} + +export const fileNameRenderer = new FileNameRenderer(); diff --git a/src/settingsTab/components/Chip.svelte b/src/settingsTab/components/Chip.svelte new file mode 100644 index 0000000..b080ec5 --- /dev/null +++ b/src/settingsTab/components/Chip.svelte @@ -0,0 +1,14 @@ + + +
+ {`{{${title}}}`} +
+ + diff --git a/src/settingsTab/components/FileNameDescription.svelte b/src/settingsTab/components/FileNameDescription.svelte new file mode 100644 index 0000000..d7b1321 --- /dev/null +++ b/src/settingsTab/components/FileNameDescription.svelte @@ -0,0 +1,14 @@ + + +Template to use for creation of file names from a book. + +
+ + + Available variables: + + + + diff --git a/src/settingsTab/index.ts b/src/settingsTab/index.ts index a0da95e..406b23c 100644 --- a/src/settingsTab/index.ts +++ b/src/settingsTab/index.ts @@ -7,10 +7,12 @@ import AmazonLogoutModal from '~/components/amazonLogoutModal'; import type KindlePlugin from '~/.'; import type FileManager from '~/fileManager'; import type { AmazonAccountRegion } from '~/models'; +import { fileNameRenderer } from '~/fileNameRenderer'; import { Renderer } from '~/renderer'; import { settingsStore } from '~/store'; import { scrapeLogoutUrl } from '~/scraper'; import { AmazonRegions } from '~/amazonRegion'; +import FileNameDescription from './components/FileNameDescription.svelte'; const { moment } = window; @@ -36,6 +38,7 @@ export class SettingsTab extends PluginSettingTab { this.amazonRegion(); this.downloadBookMetadata(); this.syncOnBoot(); + this.fileNameTemplate(); this.highlightTemplate(); this.sponsorMe(); } @@ -128,6 +131,27 @@ export class SettingsTab extends PluginSettingTab { }); } + private fileNameTemplate(): void { + const settingEl = new Setting(this.containerEl) + .setName('File name template') + .addText((text) => { + text.inputEl.style.fontSize = '0.8em'; + text.inputEl.placeholder = fileNameRenderer.defaultTemplate(); + text.setValue(get(settingsStore).fileNameTemplate).onChange(async (value) => { + const isValid = fileNameRenderer.validate(value); + + if (isValid) { + console.log('template is valid. saving', value); + } + + text.inputEl.style.border = isValid ? '' : '1px solid red'; + }); + return text; + }); + + new FileNameDescription({ target: settingEl.descEl }); + } + private highlightTemplate(): void { new Setting(this.containerEl) .setName('Highlight template') diff --git a/src/store/settingsStore.ts b/src/store/settingsStore.ts index 940ad30..af3dadf 100644 --- a/src/store/settingsStore.ts +++ b/src/store/settingsStore.ts @@ -11,6 +11,7 @@ type Settings = { lastSyncMode: SyncMode; isLoggedIn: boolean; highlightTemplate?: string; + fileNameTemplate?: string; syncOnBoot: boolean; downloadBookMetadata: boolean; }; @@ -120,6 +121,10 @@ const createSettingsStore = () => { }); }; + const setFileNameTemplate = (value: string) => { + store.update((state) => ({ ...state, fileNameTemplate: value })); + }; + const setSyncOnBoot = (value: boolean) => { store.update((state) => { state.syncOnBoot = value; @@ -149,6 +154,7 @@ const createSettingsStore = () => { setHighlightsFolder, login, logout, + setFileNameTemplate, setHighlightTemplate, setSyncOnBoot, setDownloadBookMetadata, From e5fd179f883ad328f107bd09e2c646cbefefce1e Mon Sep 17 00:00:00 2001 From: Hady Osman Date: Sun, 14 Nov 2021 23:19:33 +1300 Subject: [PATCH 2/6] File name preview component --- src/settingsTab/components/Preview.svelte | 39 +++++++++++++++++++++++ src/settingsTab/index.ts | 17 +++++++--- 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 src/settingsTab/components/Preview.svelte diff --git a/src/settingsTab/components/Preview.svelte b/src/settingsTab/components/Preview.svelte new file mode 100644 index 0000000..f00815c --- /dev/null +++ b/src/settingsTab/components/Preview.svelte @@ -0,0 +1,39 @@ +
+
Preview
+ +
File name: Animal Farm.md
+
+ + diff --git a/src/settingsTab/index.ts b/src/settingsTab/index.ts index 406b23c..0cf8883 100644 --- a/src/settingsTab/index.ts +++ b/src/settingsTab/index.ts @@ -13,6 +13,7 @@ import { settingsStore } from '~/store'; import { scrapeLogoutUrl } from '~/scraper'; import { AmazonRegions } from '~/amazonRegion'; import FileNameDescription from './components/FileNameDescription.svelte'; +import Preview from './components/Preview.svelte'; const { moment } = window; @@ -132,7 +133,7 @@ export class SettingsTab extends PluginSettingTab { } private fileNameTemplate(): void { - const settingEl = new Setting(this.containerEl) + const setting = new Setting(this.containerEl) .setName('File name template') .addText((text) => { text.inputEl.style.fontSize = '0.8em'; @@ -149,17 +150,23 @@ export class SettingsTab extends PluginSettingTab { return text; }); - new FileNameDescription({ target: settingEl.descEl }); + setting.settingEl.style.alignItems = 'normal'; + setting.controlEl.style.flexDirection = 'column'; + setting.controlEl.style.alignItems = 'flex-end'; + + new FileNameDescription({ target: setting.descEl }); + new Preview({ target: setting.controlEl }); } private highlightTemplate(): void { - new Setting(this.containerEl) + const setting = new Setting(this.containerEl) .setName('Highlight template') .setDesc('Template for an individual highlight') .addTextArea((text) => { text.inputEl.style.width = '100%'; - text.inputEl.style.height = '450px'; + text.inputEl.style.height = '200px'; text.inputEl.style.fontSize = '0.8em'; + text.inputEl.style.fontFamily = 'var(--font-monospace)'; text.inputEl.placeholder = this.renderer.defaultHighlightTemplate(); text.setValue(get(settingsStore).highlightTemplate).onChange(async (value) => { const isValid = this.renderer.validate(value); @@ -172,6 +179,8 @@ export class SettingsTab extends PluginSettingTab { }); return text; }); + + setting.settingEl.style.alignItems = 'normal'; } private downloadBookMetadata(): void { From e9bd2002b37d0b04c6000c617bb2f0e610c88b32 Mon Sep 17 00:00:00 2001 From: Hady Osman Date: Sun, 14 Nov 2021 23:53:08 +1300 Subject: [PATCH 3/6] Two-way binding of data for Preview component --- src/settingsTab/components/Preview.svelte | 21 ++++++++++++++----- src/settingsTab/index.ts | 25 ++++++++++++++++++++++- src/settingsTab/store.ts | 3 +++ 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 src/settingsTab/store.ts diff --git a/src/settingsTab/components/Preview.svelte b/src/settingsTab/components/Preview.svelte index f00815c..ce2011d 100644 --- a/src/settingsTab/components/Preview.svelte +++ b/src/settingsTab/components/Preview.svelte @@ -1,11 +1,22 @@ + +
Preview
- + {#each books as book} + + {/each} -
File name: Animal Farm.md
+
File name: {$fileName}
diff --git a/src/settingsTab/fileNameTemplateSetting/components/Legend.svelte b/src/settingsTab/fileNameTemplateSetting/components/Legend.svelte new file mode 100644 index 0000000..98c0f8e --- /dev/null +++ b/src/settingsTab/fileNameTemplateSetting/components/Legend.svelte @@ -0,0 +1,20 @@ + + +
+ Available variables: + + + +
+ + diff --git a/src/settingsTab/components/Preview.svelte b/src/settingsTab/fileNameTemplateSetting/components/Preview.svelte similarity index 64% rename from src/settingsTab/components/Preview.svelte rename to src/settingsTab/fileNameTemplateSetting/components/Preview.svelte index ce2011d..38847c3 100644 --- a/src/settingsTab/components/Preview.svelte +++ b/src/settingsTab/fileNameTemplateSetting/components/Preview.svelte @@ -1,22 +1,20 @@
-
Preview
+
Live preview
-
File name: {$fileName}
+
Example file name: {$fileName}