diff --git a/src/main.ts b/src/main.ts index c2e2b14..5240b76 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -import { normalizePath, Notice, Plugin } from 'obsidian'; +import { normalizePath, Notice, Plugin, TFile } from 'obsidian'; import { type SubstackClipperSettings, DEFAULT_SETTINGS } from './types'; import type { ParsedArticle, DownloadResult } from './types'; import { SettingsTab } from './settings'; @@ -18,10 +18,14 @@ export default class SubstackClipperPlugin extends Plugin { this.addCommand({ id: 'clip-post', - name: 'Clip substack post', + name: 'Save substack post', callback: () => { - new ClipModal(this.app, (url) => { - void this.clipPost(url); + new ClipModal(this.app, this.settings.openAfterClip, (url, openAfterClip) => { + if (openAfterClip !== this.settings.openAfterClip) { + this.settings.openAfterClip = openAfterClip; + void this.saveSettings(); + } + void this.clipPost(url, openAfterClip); }).open(); }, }); @@ -35,8 +39,8 @@ export default class SubstackClipperPlugin extends Plugin { await this.saveData(this.settings); } - private async clipPost(url: string): Promise { - const notice = new Notice('Clipping...', 0); + private async clipPost(url: string, openAfterClip: boolean): Promise { + const notice = new Notice('Saving...', 0); try { const { username, slug, domain } = parseUrl(url); @@ -189,12 +193,19 @@ export default class SubstackClipperPlugin extends Plugin { await this.writeFile(htmlPath, article.html); } + if (openAfterClip) { + const file = this.app.vault.getAbstractFileByPath(notePath); + if (file instanceof TFile) { + await this.app.workspace.getLeaf(false).openFile(file); + } + } + notice.hide(); - new Notice(`Clipped: ${article.title}`); + new Notice(`Saved: ${article.title}`); } catch (e) { notice.hide(); - new Notice(`Clip failed: ${e instanceof Error ? e.message : String(e)}`); + new Notice(`Save failed: ${e instanceof Error ? e.message : String(e)}`); } } diff --git a/src/modal.ts b/src/modal.ts index e751d64..d3133df 100644 --- a/src/modal.ts +++ b/src/modal.ts @@ -2,17 +2,19 @@ import { App, Modal, Notice, Setting } from 'obsidian'; export class ClipModal extends Modal { private url = ''; - private onSubmit: (url: string) => void; + private openAfterClip: boolean; + private onSubmit: (url: string, openAfterClip: boolean) => void; - constructor(app: App, onSubmit: (url: string) => void) { + constructor(app: App, openAfterClip: boolean, onSubmit: (url: string, openAfterClip: boolean) => void) { super(app); + this.openAfterClip = openAfterClip; this.onSubmit = onSubmit; } onOpen(): void { const { contentEl } = this; contentEl.addClass('substack-clipper-modal'); - contentEl.createEl('h2', { text: 'Clip substack post' }); + contentEl.createEl('h2', { text: 'Save substack post' }); new Setting(contentEl) .setName('Post URL') @@ -29,9 +31,15 @@ export class ClipModal extends Modal { text.inputEl.setCssStyles({ width: '100%' }); }); + new Setting(contentEl) + .setName('Open after saving') + .addToggle(toggle => toggle + .setValue(this.openAfterClip) + .onChange(value => { this.openAfterClip = value; })); + new Setting(contentEl) .addButton(btn => btn - .setButtonText('Clip') + .setButtonText('Save') .setCta() .onClick(() => { this.submit(); })); } @@ -42,7 +50,7 @@ export class ClipModal extends Modal { return; } this.close(); - this.onSubmit(this.url); + this.onSubmit(this.url, this.openAfterClip); } onClose(): void { diff --git a/src/types.ts b/src/types.ts index b028181..62dfe78 100644 --- a/src/types.ts +++ b/src/types.ts @@ -32,6 +32,7 @@ export interface SubstackClipperSettings { saveRawJson: boolean; saveRawHtml: boolean; commentSort: 'most_recent_first' | 'oldest_first' | 'best_first'; + openAfterClip: boolean; } export const DEFAULT_SETTINGS: SubstackClipperSettings = { @@ -42,6 +43,7 @@ export const DEFAULT_SETTINGS: SubstackClipperSettings = { saveRawJson: false, saveRawHtml: false, commentSort: 'most_recent_first', + openAfterClip: false, }; export interface DownloadResult {