Skip to content

Commit

Permalink
add inline note support
Browse files Browse the repository at this point in the history
  • Loading branch information
Phantom1003 committed Apr 21, 2023
1 parent 69d2bb3 commit e45c739
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
12 changes: 9 additions & 3 deletions src/pdfblock/processor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as pdfjs from "pdfjs-dist";
import {FrontMatterCache, MarkdownPostProcessorContext, Notice} from "obsidian";
import { FrontMatterCache, MarkdownPostProcessorContext } from "obsidian";
import SlideNotePlugin from '../main';
import { PDFBlockRenderer } from "./renderer";

Expand All @@ -11,6 +11,7 @@ export interface PDFBlockParameters {
rotat: number;
rect: Array<number>;
annot: string;
note: string;
}

export class PDFBlockProcessor {
Expand Down Expand Up @@ -39,6 +40,7 @@ export class PDFBlockProcessor {
const keywords = ["file", "page", "link", "scale", "rotat", "rect"];
const paramsRaw: { [k: string]: string } = {};
const annot: Array<string> = [];
const note: Array<string> = [];

for (let i = 0; i < lines.length; i++) {
const words = lines[i].trim().split(":")
Expand All @@ -47,7 +49,10 @@ export class PDFBlockProcessor {
paramsRaw[words[0]] = words[1].trim();
}
else {
annot.push(lines[i].trim());
if (lines[i].trim().startsWith("@"))
annot.push(lines[i].trim().slice(1));
else
note.push(lines[i]);
}
}

Expand All @@ -58,7 +63,8 @@ export class PDFBlockProcessor {
scale: 0,
rotat: 0,
rect: [-1, -1, -1, -1],
annot: annot.join("\n")
annot: annot.join("\n"),
note: note.join("\n")
};

// handle file
Expand Down
20 changes: 10 additions & 10 deletions src/pdfblock/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import * as pdfjs from "pdfjs-dist";

import { MarkdownRenderChild } from "obsidian";
import {MarkdownPreviewView, MarkdownRenderChild} from "obsidian";
import { PDFBlockParameters } from "./processor";
import { SlideNoteSettings } from "../settings";

export class PDFBlockRenderer extends MarkdownRenderChild {
el: HTMLElement
params: PDFBlockParameters
note: string
sourcePath: string
settings: SlideNoteSettings
public constructor(
el: HTMLElement,
params: PDFBlockParameters,
note: string,
sourcePath: string,
settings: SlideNoteSettings

) {
super(el);
this.el = el;
this.params = params;
this.note = note;
this.sourcePath = sourcePath;
this.settings = settings;
}

Expand All @@ -43,12 +43,12 @@ export class PDFBlockRenderer extends MarkdownRenderChild {
const arrayBuffer = await app.vault.adapter.readBinary(this.params.file);
const buffer = Buffer.from(arrayBuffer);

if (!this.checkActiveFile(this.note))
if (!this.checkActiveFile(this.sourcePath))
return;

const document = await pdfjs.getDocument(buffer).promise;

if (!this.checkActiveFile(this.note))
if (!this.checkActiveFile(this.sourcePath))
return;

// page parameter as trigger for whole pdf, 0 = all pages
Expand All @@ -62,7 +62,7 @@ export class PDFBlockRenderer extends MarkdownRenderChild {

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

const page = await document.getPage(pageNumber);
Expand All @@ -81,7 +81,7 @@ export class PDFBlockRenderer extends MarkdownRenderChild {
const canvas = host.createEl("canvas");
canvas.style.width = `${Math.floor(this.params.scale * 100)}%`;

if (!this.checkActiveFile(this.note))
if (!this.checkActiveFile(this.sourcePath))
return;

const context = canvas.getContext("2d");
Expand All @@ -108,7 +108,7 @@ export class PDFBlockRenderer extends MarkdownRenderChild {
viewport: pageview,
};

if (!this.checkActiveFile(this.note))
if (!this.checkActiveFile(this.sourcePath))
return;

canvas.addEventListener("mouseup", (event)=> {
Expand Down Expand Up @@ -136,7 +136,7 @@ export class PDFBlockRenderer extends MarkdownRenderChild {
canvas.addEventListener("mouseleave", (event)=> {
app.workspace.trigger("slidenote:mouseleave");
});

MarkdownPreviewView.renderMarkdown(this.params.note, host, this.sourcePath, this)
await page.render(renderContext).promise.then(
() => {
if (this.params.annot != "" && this.settings.allow_annotations) {
Expand Down

0 comments on commit e45c739

Please sign in to comment.