Skip to content

Commit

Permalink
release: 0.38.8
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Mar 19, 2024
1 parent f56bdab commit 168925f
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 48 deletions.
2 changes: 1 addition & 1 deletion manifest-beta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "pdf-plus",
"name": "PDF++",
"version": "0.38.7",
"version": "0.38.8",
"minAppVersion": "1.4.16",
"description": "The most Obsidian-native PDF annotation tool ever.",
"author": "Ryota Ushio",
Expand Down
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.38.7",
"version": "0.38.8",
"minAppVersion": "1.4.16",
"description": "The most Obsidian-native PDF annotation tool ever.",
"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.38.7",
"version": "0.38.8",
"description": "The most Obsidian-native PDF annotation tool ever.",
"scripts": {
"dev": "node esbuild.config.mjs",
Expand Down
28 changes: 10 additions & 18 deletions src/modals/external-pdf-modals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export class ExternalPDFModal extends PDFPlusModal {
super(...args);

this.scope.register([], 'Enter', () => {
this.submit();
if (activeDocument.activeElement?.tagName === 'INPUT') {
this.submit();
}
});
}

Expand Down Expand Up @@ -295,30 +297,20 @@ export class ExternalPDFModal extends PDFPlusModal {
failed = this.urls;
}

// const files = await Promise.all(promises);
const files = await Promise.all(promises);

if (failed.length) {
new Notice(`${this.plugin.manifest.name}: Failed to create dummy files for the following URLs: ${failed.join(', ')}`);
} else {
new Notice(`${this.plugin.manifest.name}: Dummy files created successfully.`);
}

// Sometimes file opening fails even with `setTimeout`. Commented out for now.

// // Ideally, I want to open all the created files, but it does not work as expected for some reasons that I don't understand.
// // TODO: Figure it out!
// // So, for now, I open only the first file.
// const firstFile = files.find((file): file is TFile => !!file);
// if (firstFile) {
// // Again, for some reasons that I don't understand, opening the file fails without `setTimeout`.
// await new Promise<void>((resolve) => {
// activeWindow.setTimeout(async () => {
// const leaf = this.app.workspace.getLeaf(true);
// await leaf.openFile(firstFile);
// resolve();
// }, 300);
// });
// }
for (const file of files) {
if (file) {
const leaf = this.app.workspace.getLeaf(true);
await leaf.openFile(file);
}
}
}

static async createDummyFilesFromObsidianUrl(plugin: PDFPlus, params: ObsidianProtocolData) {
Expand Down
61 changes: 36 additions & 25 deletions src/patchers/pdf-internals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, MarkdownRenderer, Notice, TFile, Vault, debounce, setIcon, setTooltip } from 'obsidian';
import { Component, MarkdownRenderer, Notice, TFile, debounce, setIcon, setTooltip } from 'obsidian';
import { around } from 'monkey-around';

import PDFPlus from 'main';
Expand All @@ -8,7 +8,7 @@ import { onContextMenu, onOutlineContextMenu, onThumbnailContextMenu } from 'con
import { registerAnnotationPopupDrag, registerOutlineDrag, registerThumbnailDrag } from 'drag';
import { patchPDFOutlineViewer } from './pdf-outline-viewer';
import { camelCaseToKebabCase, hookInternalLinkMouseEventHandlers, isNonEmbedLike, toSingleLine } from 'utils';
import { AnnotationElement, PDFOutlineViewer, PDFViewerComponent, PDFViewerChild, PDFSearchSettings, Rect, PDFAnnotationHighlight, PDFTextHighlight, PDFRectHighlight } from 'typings';
import { AnnotationElement, PDFOutlineViewer, PDFViewerComponent, PDFViewerChild, PDFSearchSettings, Rect, PDFAnnotationHighlight, PDFTextHighlight, PDFRectHighlight, ObsidianViewer } from 'typings';
import { PDFInternalLinkPostProcessor, PDFOutlineItemPostProcessor, PDFThumbnailItemPostProcessor } from 'pdf-link-like';
import { PDFViewerBacklinkVisualizer } from 'backlink-visualizer';

Expand All @@ -29,6 +29,10 @@ export const patchPDFInternals = async (plugin: PDFPlus, pdfViewerComponent: PDF

patchPDFViewerChild(plugin, child);

// This check should be unnecessary, but just in case
if (!child.pdfViewer) return resolve(false);
patchObsidianViewer(plugin, child.pdfViewer);

plugin.patchStatus.pdfInternals = true;

// @ts-ignore
Expand Down Expand Up @@ -172,41 +176,25 @@ const patchPDFViewerChild = (plugin: PDFPlus, child: PDFViewerChild) => {
this.component.load();

// If the file is small enough, first check the text content.
// If it's a URL to a PDF located outside the vault, monkey-patch
// `Vault.prototype.getResourcePath` (which will be called inside `old` to load the PDF content to the viewer)
// If it's a URL to a PDF located outside the vault, tell ObsidianViewer to use the URL instead of `app.vault.getResourcePath(file)` (which is called inside the original `loadFile` method)
// so that it can directly load the PDF content from the URL.
// This way, we can open local PDF files outside the vault or PDF files on the web
// as if it were in the vault.
let externalFileLoaded = false;

if (file.stat.size < 300) {
const url = await lib.getExternalPDFUrl(file);
if (url) {
let uninstalled = false;
const uninstaller = around(Vault.prototype, {
getResourcePath(old) {
return function (this: Vault, f: TFile) {
if (f === file) {
uninstaller();
uninstalled = true;
return url;
}
return old.call(this, file);
}
}
});
const redirectTo = await lib.getExternalPDFUrl(file);
if (redirectTo) {
const redirectFrom = app.vault.getResourcePath(file).replace(/\?\d+$/, '');
this.pdfViewer.pdfPlusRedirect = { from: redirectFrom, to: redirectTo };

await old.call(this, file, subpath);
if (!uninstalled) {
uninstaller();
uninstalled = true;
}

this.component.register(() => URL.revokeObjectURL(url));
this.component.register(() => URL.revokeObjectURL(redirectTo));

externalFileLoaded = true;
this.isFileExternal = true;
this.externalFileUrl = url;
this.externalFileUrl = redirectTo;

if (this.palette && this.palette.paletteEl) {
this.palette.removeWriteFileToggle();
Expand Down Expand Up @@ -741,3 +729,26 @@ const patchPDFViewerChild = (plugin: PDFPlus, child: PDFViewerChild) => {
}
}));
}

/** Monkey-patch ObsidianViewer so that it can open external PDF files. */
const patchObsidianViewer = (plugin: PDFPlus, pdfViewer: ObsidianViewer) => {
plugin.register(around(pdfViewer.constructor.prototype, { // equivalent to window.pdfjsViewer.ObsidianViewer
open(old) {
return async function (this: ObsidianViewer, args: any) {
if (this.pdfPlusRedirect) {
const { from, to } = this.pdfPlusRedirect;
const url = args.url;
if (typeof url === 'string'
&& url.startsWith(from) // on desktop, Vault.getResourcePath() returns a path with a query string like "?1629350400000"
) {
args.url = to;
}
}

delete this.pdfPlusRedirect;

return await old.call(this, args);
}
}
}));
}
5 changes: 5 additions & 0 deletions src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ interface ObsidianViewer {
zoomIn(): void;
zoomOut(): void;
open(options: any): Promise<void>;
//////////////////////////
// Added by this plugin //
//////////////////////////
/** Used to open external PDFs. */
pdfPlusRedirect?: { from: string, to: string };
}

interface PDFSidebar {
Expand Down

0 comments on commit 168925f

Please sign in to comment.