Skip to content

Commit

Permalink
release: 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Dec 20, 2023
1 parent 17cc3f1 commit b9e518e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 12 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "pdf-plus",
"name": "PDF++",
"version": "0.2.0",
"version": "0.3.0",
"minAppVersion": "1.3.5",
"description": "Enhance PDF viewer & embeds.",
"author": "Ryota Ushio",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-pdf-plus",
"version": "0.2.0",
"version": "0.3.0",
"description": "An Obsidian.md plugin to enhance PDF viewer & embeds.",
"scripts": {
"dev": "node esbuild.config.mjs",
Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Notice, Plugin } from 'obsidian';
import { DEFAULT_SETTINGS, PDFPlusSettings, PDFPlusSettingTab } from './settings';
import { patchPDF } from 'patch';
import { patchPDF, patchWorkspace } from 'patch';


export default class PDFPlus extends Plugin {
Expand All @@ -11,6 +11,8 @@ export default class PDFPlus extends Plugin {
await this.saveSettings();
this.addSettingTab(new PDFPlusSettingTab(this));

patchWorkspace(this);

this.app.workspace.onLayoutReady(() => {
const success = patchPDF(this);
if (!success) {
Expand Down
53 changes: 47 additions & 6 deletions src/patch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import PDFPlus from "main";
import { around } from "monkey-around";
import { EditableFileView, Workspace, parseLinktext } from "obsidian";
import { ObsidianViewer, PDFView, PDFViewerChild } from "typings";

export const patchPDF = (plugin: PDFPlus): boolean => {
Expand Down Expand Up @@ -36,16 +37,18 @@ export const patchPDF = (plugin: PDFPlus): boolean => {
}
}

if (self.isEmbed && !self._zoomedIn) {
self._zoomedIn = true;
setTimeout(async () => {
for (let i = 0; i < plugin.settings.zoomInEmbed; i++) {
if (self.isEmbed && plugin.settings.zoomInEmbed) {
const listener = async () => {
for (self._zoomedIn ??= 0; self._zoomedIn < plugin.settings.zoomInEmbed; self._zoomedIn++) {
console.log(self._zoomedIn);
self.zoomIn();
await new Promise<void>((resolve) => {
setTimeout(resolve, 50);
})
}
}, 100);
self.eventBus._off("textlayerrendered", listener);
};
self.eventBus._on("textlayerrendered", listener);
}

old.call(this, height);
Expand All @@ -54,4 +57,42 @@ export const patchPDF = (plugin: PDFPlus): boolean => {
}));

return true;
}
}

export const patchWorkspace = (plugin: PDFPlus) => {
const app = plugin.app;

plugin.register(around(Workspace.prototype, {
openLinkText(old) {
return function (linktext: string, sourcePath: string, ...args: any[]) {
const { path, subpath } = parseLinktext(linktext);
const file = app.metadataCache.getFirstLinkpathDest(path, sourcePath);

if (file && file.extension === 'pdf') {
const leaf = app.workspace.getLeavesOfType('pdf').find(leaf => {
return leaf.view instanceof EditableFileView && leaf.view.file === file;
});
if (leaf) {
const view = leaf.view as PDFView;
const self = this as Workspace;
console.log(view);
self.setActiveLeaf(leaf);
const child = view.viewer.child;
if (child) {
child.applySubpath(subpath);
if (child.subpathHighlight?.type === 'text') {
const { page, range } = child.subpathHighlight;
child.highlightText(page, range);
} else if (child.subpathHighlight?.type === 'annotation') {
const { page, id } = child.subpathHighlight;
child.highlightAnnotation(page, id);
}
}
return;
}
}
return old.call(this, linktext, sourcePath, ...args);
}
}
}));
};
20 changes: 19 additions & 1 deletion src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface PDFViewerChild {
containerEl: HTMLElement;
opts: any;
pdfViewer: ObsidianViewer;
subpathHighlight: PDFTextHighlight | PDFAnnotationHighlight | null;
load(): void;
getPage(page: number): any;
getTextByRect(pageView: any, rect: number[]): any;
Expand All @@ -34,6 +35,22 @@ interface PDFViewerChild {
onContextMenu(evt: MouseEvent): void;
onResize(): void;
applySubpath(subpath?: string): any;
highlightText(page: number, range: [[number, number], [number, number]]): void;
highlightAnnotation(page: number, id: string): void;
}

interface PDFHighlight {
page: number;
}

interface PDFTextHighlight extends PDFHighlight {
type: "text";
range: [[number, number], [number, number]];
}

interface PDFAnnotationHighlight extends PDFHighlight {
type: "annotation";
id: string;
}

interface ObsidianViewer {
Expand All @@ -44,10 +61,11 @@ interface ObsidianViewer {
page?: number;
subpath: string | null;
isEmbed: boolean;
eventBus: any;
setHeight(height?: number | "page" | "auto"): void;
applySubpath(subpath: string): void;
zoomIn(): void;
_zoomedIn?: boolean;
_zoomedIn?: number;
}

interface AppSetting extends Modal {
Expand Down

0 comments on commit b9e518e

Please sign in to comment.