diff --git a/manifest.json b/manifest.json index bd601159..81dbb7c1 100644 --- a/manifest.json +++ b/manifest.json @@ -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", diff --git a/package-lock.json b/package-lock.json index e62246b2..dcf37112 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-pdf-plus", - "version": "0.2.0", + "version": "0.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "obsidian-pdf-plus", - "version": "0.2.0", + "version": "0.3.0", "license": "MIT", "dependencies": { "@codemirror/language": "^6.9.1", diff --git a/package.json b/package.json index 9ab907c1..5b40ffff 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/main.ts b/src/main.ts index af7a7513..2f240b64 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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 { @@ -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) { diff --git a/src/patch.ts b/src/patch.ts index 6e4c06bf..953dcea2 100644 --- a/src/patch.ts +++ b/src/patch.ts @@ -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 => { @@ -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((resolve) => { setTimeout(resolve, 50); }) } - }, 100); + self.eventBus._off("textlayerrendered", listener); + }; + self.eventBus._on("textlayerrendered", listener); } old.call(this, height); @@ -54,4 +57,42 @@ export const patchPDF = (plugin: PDFPlus): boolean => { })); return true; -} \ No newline at end of file +} + +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); + } + } + })); +}; diff --git a/src/typings.d.ts b/src/typings.d.ts index e33c2907..783c2ec3 100644 --- a/src/typings.d.ts +++ b/src/typings.d.ts @@ -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; @@ -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 { @@ -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 {