Skip to content

Commit

Permalink
full pdf preview
Browse files Browse the repository at this point in the history
  • Loading branch information
Phantom1003 committed Apr 21, 2023
1 parent e45c739 commit 6e034f0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
26 changes: 9 additions & 17 deletions src/pdfblock/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ export class PDFBlockRenderer extends MarkdownRenderChild {

async render() {
this.el.innerHTML = "";
// render PDF pages
if (this.params !== null) {
try {
// read PDF
const arrayBuffer = await app.vault.adapter.readBinary(this.params.file);
const buffer = Buffer.from(arrayBuffer);

Expand All @@ -51,33 +49,27 @@ export class PDFBlockRenderer extends MarkdownRenderChild {
if (!this.checkActiveFile(this.sourcePath))
return;

// page parameter as trigger for whole pdf, 0 = all pages
if ((<number[]>this.params.page).includes(0)) {
const pagesArray = [];
for (let i = 1; i <= document.numPages; i++) {
pagesArray.push(i);
}
this.params.page = pagesArray;
if (this.params.page.includes(0)) {
this.params.page = Array.from(
{length: document.numPages},
(_, i) => i + 1
);
}

// Read pages
for (const pageNumber of <number[]>this.params.page) {
for (const pageNumber of this.params.page) {
if (!this.checkActiveFile(this.sourcePath))
return;

const page = await document.getPage(pageNumber);
let host = this.el;
let host = this.el.createEl("div");

// Create hyperlink for Page
if (this.params.link) {
const href = this.el.createEl("a");
const href = host.createEl("a");
href.href = this.params.file + "#page=" + pageNumber;
href.className = "internal-link";

host = href;
}

// Render Canvas
const canvas = host.createEl("canvas");
canvas.style.width = `${Math.floor(this.params.scale * 100)}%`;

Expand Down Expand Up @@ -136,7 +128,7 @@ export class PDFBlockRenderer extends MarkdownRenderChild {
canvas.addEventListener("mouseleave", (event)=> {
app.workspace.trigger("slidenote:mouseleave");
});
MarkdownPreviewView.renderMarkdown(this.params.note, host, this.sourcePath, this)
MarkdownPreviewView.renderMarkdown(this.params.note, this.el, this.sourcePath, this)
await page.render(renderContext).promise.then(
() => {
if (this.params.annot != "" && this.settings.allow_annotations) {
Expand Down
11 changes: 3 additions & 8 deletions styles.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/*
This CSS file will be included with your plugin, and
available in the app when your plugin is enabled.
If your plugin does not need CSS, delete this file.
*/
.popover.hover-popover > .pdf-embed {
max-width: 100%;
}

0 comments on commit 6e034f0

Please sign in to comment.