mirror of
https://github.com/cubexy/obsidian-pdf-printer.git
synced 2026-07-22 05:48:03 +00:00
add code modules
This commit is contained in:
parent
435f0a5c9b
commit
52c7840d7d
3 changed files with 100 additions and 102 deletions
104
main.ts
104
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();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
82
modules/settings.ts
Normal file
82
modules/settings.ts
Normal file
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
16
modules/types.ts
Normal file
16
modules/types.ts
Normal file
|
|
@ -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;
|
||||
};
|
||||
Loading…
Reference in a new issue