diff --git a/main.ts b/main.ts index 4035324..7ff1eea 100644 --- a/main.ts +++ b/main.ts @@ -1,42 +1,15 @@ import { BrowserCanvasFactory } from "utils/canvas/canvasFactory"; import { - App, Editor, MarkdownView, Notice, Plugin, - PluginSettingTab, - Setting, TFile, loadPdfJs, - normalizePath, } from "obsidian"; import { v4 as uuidv4 } from "uuid"; -import { FolderSuggest } from "utils/suggest/folderSuggest"; - -interface PdfPrinterSettings { - imageFolder: string; - imageQuality: number; - imageEmbedFormat: string; - preservePdfLink: boolean; -} - -const DEFAULT_SETTINGS: PdfPrinterSettings = { - imageFolder: "", - imageQuality: 0.5, - imageEmbedFormat: "![[${filename}]]", - preservePdfLink: false, -}; - -type PdfDocumentBuffer = { - fileName: string; - pages: PdfPage[]; -}; - -type PdfPage = { - pageNumber: number; - buffer: ArrayBuffer; -}; +import { DEFAULT_SETTINGS, PdfPrinterSettingsTab } from "modules/settings"; +import { PdfDocumentBuffer, PdfPage, PdfPrinterSettings } from "modules/types"; export default class PdfPrinterPlugin extends Plugin { settings: PdfPrinterSettings; @@ -90,8 +63,6 @@ export default class PdfPrinterPlugin extends Plugin { this.addSettingTab(new PdfPrinterSettingsTab(this.app, this)); } - onunload() {} - async loadSettings() { this.settings = Object.assign( {}, @@ -235,74 +206,3 @@ export default class PdfPrinterPlugin extends Plugin { return Promise.all(writeTasks); } } - -class PdfPrinterSettingsTab extends PluginSettingTab { - plugin: PdfPrinterPlugin; - - constructor(app: App, plugin: PdfPrinterPlugin) { - super(app, plugin); - this.plugin = plugin; - } - - display(): void { - const { containerEl } = this; - containerEl.empty(); - - new Setting(containerEl) - .setName("Image quality") - .setDesc( - "Quality of the printed images. Higher values result in better quality but larger file sizes." - ) - .addSlider((cb) => { - cb.setLimits(0, 1, 0.01) - .setValue(this.plugin.settings.imageQuality) - .setDynamicTooltip() - .onChange(async (value) => { - this.plugin.settings.imageQuality = value; - await this.plugin.saveSettings(); - }); - }); - - new Setting(this.containerEl) - .setName("Printed image folder path") - .setDesc("Place printed images from PDFs in this folder.") - .addSearch((cb) => { - new FolderSuggest(this.app, cb.inputEl); - cb.setPlaceholder("Example: folder1/folder2") - .setValue(this.plugin.settings.imageFolder) - .onChange(async (new_folder) => { - this.plugin.settings.imageFolder = - normalizePath(new_folder); - await this.plugin.saveSettings(); - }); - }); - - new Setting(this.containerEl) - .setName("Show link to original PDF above printed images") - .setDesc( - "Keep a link to the original PDF above the printed images as a reference." - ) - .addToggle((cb) => { - cb.setValue(this.plugin.settings.preservePdfLink).onChange( - async (value) => { - this.plugin.settings.preservePdfLink = value; - await this.plugin.saveSettings(); - } - ); - }); - - new Setting(this.containerEl) - .setName("Image embed format") - .setDesc( - "Format for embedding images in the markdown file. Use ${filename} as a placeholder for the image file name." - ) - .addText((cb) => { - cb.setPlaceholder("![[${filename}]]") - .setValue(this.plugin.settings.imageEmbedFormat) - .onChange(async (value) => { - this.plugin.settings.imageEmbedFormat = value; - await this.plugin.saveSettings(); - }); - }); - } -} diff --git a/modules/settings.ts b/modules/settings.ts new file mode 100644 index 0000000..7c41d5e --- /dev/null +++ b/modules/settings.ts @@ -0,0 +1,82 @@ +import PdfPrinterPlugin from "main"; +import { PluginSettingTab, App, Setting, normalizePath } from "obsidian"; +import { FolderSuggest } from "utils/suggest/folderSuggest"; +import { PdfPrinterSettings } from "./types"; + +export const DEFAULT_SETTINGS: PdfPrinterSettings = { + imageFolder: "", + imageQuality: 0.5, + imageEmbedFormat: "![[${filename}]]", + preservePdfLink: false, +}; + +export class PdfPrinterSettingsTab extends PluginSettingTab { + plugin: PdfPrinterPlugin; + + constructor(app: App, plugin: PdfPrinterPlugin) { + super(app, plugin); + this.plugin = plugin; + } + + display(): void { + const { containerEl } = this; + containerEl.empty(); + + new Setting(containerEl) + .setName("Image quality") + .setDesc( + "Quality of the printed images. Higher values result in better quality but larger file sizes." + ) + .addSlider((cb) => { + cb.setLimits(0, 1, 0.01) + .setValue(this.plugin.settings.imageQuality) + .setDynamicTooltip() + .onChange(async (value) => { + this.plugin.settings.imageQuality = value; + await this.plugin.saveSettings(); + }); + }); + + new Setting(this.containerEl) + .setName("Printed image folder path") + .setDesc("Place printed images from PDFs in this folder.") + .addSearch((cb) => { + new FolderSuggest(this.app, cb.inputEl); + cb.setPlaceholder("Example: folder1/folder2") + .setValue(this.plugin.settings.imageFolder) + .onChange(async (new_folder) => { + this.plugin.settings.imageFolder = + normalizePath(new_folder); + await this.plugin.saveSettings(); + }); + }); + + new Setting(this.containerEl) + .setName("Show link to original PDF above printed images") + .setDesc( + "Keep a link to the original PDF above the printed images as a reference." + ) + .addToggle((cb) => { + cb.setValue(this.plugin.settings.preservePdfLink).onChange( + async (value) => { + this.plugin.settings.preservePdfLink = value; + await this.plugin.saveSettings(); + } + ); + }); + + new Setting(this.containerEl) + .setName("Image embed format") + .setDesc( + "Format for embedding images in the markdown file. Use ${filename} as a placeholder for the image file name." + ) + .addText((cb) => { + cb.setPlaceholder("![[${filename}]]") + .setValue(this.plugin.settings.imageEmbedFormat) + .onChange(async (value) => { + this.plugin.settings.imageEmbedFormat = value; + await this.plugin.saveSettings(); + }); + }); + } +} diff --git a/modules/types.ts b/modules/types.ts new file mode 100644 index 0000000..807e4d2 --- /dev/null +++ b/modules/types.ts @@ -0,0 +1,16 @@ +export interface PdfPrinterSettings { + imageFolder: string; + imageQuality: number; + imageEmbedFormat: string; + preservePdfLink: boolean; +} + +export type PdfDocumentBuffer = { + fileName: string; + pages: PdfPage[]; +}; + +export type PdfPage = { + pageNumber: number; + buffer: ArrayBuffer; +};