Skip to content

Commit

Permalink
release: 0.40.1
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Jun 1, 2024
1 parent 22fd79a commit daddd9c
Show file tree
Hide file tree
Showing 26 changed files with 1,492 additions and 272 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ He's also the author of several popular Obsidian plugins including Tag Wrangler.
PDF++ offers two ways to highlight text in PDF: one that does not involve modifying the PDF file, and the other that writes highlight annotations directly into the PDF file.
The latter is powered by the pdf-lib, a JavaScript library for creating and modifying PDF documents. The [original project](https://github.com/Hopding/pdf-lib) was created by Andrew Dillon. PDF++ uses a [forked version](https://github.com/cantoo-scribe/pdf-lib) maintained by Cantoo Scribe.

PDF++ also supports Vim-like keybindings. Its design was inspired by [codemirror-vim](https://github.com/replit/codemirror-vim) and [Tridactyl](https://github.com/tridactyl/tridactyl). Especially, [some code for the link mode](https://github.com/RyotaUshio/obsidian-pdf-plus/blob/main/src/vim/hintnames.ts) was borrowed from Tridactyl, which is [distributed under the Apache 2.0 License](https://github.com/tridactyl/tridactyl?tab=License-1-ov-file) by Colin Caine, Oliver Blanthorn and Koushien with some modification.

## Compatibility

I'm trying to keep PDF++ compatible with the following plugin(s) as much as possible:
Expand Down
1 change: 1 addition & 0 deletions esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const context = await esbuild.context({
"@lezer/common",
"@lezer/highlight",
"@lezer/lr",
"pdfjs-dist",
...builtins],
format: "cjs",
target: "es2018",
Expand Down
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.40.0",
"version": "0.40.1",
"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.40.0",
"version": "0.40.1",
"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.40.0",
"version": "0.40.1",
"description": "The most Obsidian-native PDF annotation tool ever.",
"scripts": {
"dev": "node esbuild.config.mjs",
Expand Down
27 changes: 21 additions & 6 deletions src/backlink-visualizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PDFPlus from 'main';
import { PDFPlusComponent } from 'lib/component';
import { PDFBacklinkCache, PDFBacklinkIndex, PDFPageBacklinkIndex } from 'lib/pdf-backlink-index';
import { PDFPageView, PDFViewerChild, Rect } from 'typings';
import { isCanvas, isEmbed, isHoverPopover, isMouseEventExternal, isNonEmbedLike } from 'utils';
import { MultiValuedMap, isCanvas, isEmbed, isHoverPopover, isMouseEventExternal, isNonEmbedLike } from 'utils';
import { onBacklinkVisualizerContextMenu } from 'context-menu';
import { BidirectionalMultiValuedMap } from 'utils';
import { MergedRect } from 'lib/highlights/geometry';
Expand Down Expand Up @@ -37,6 +37,7 @@ export class BacklinkDomManager extends PDFPlusComponent {

private pagewiseCacheToDomsMap = new Map<number, BidirectionalMultiValuedMap<PDFBacklinkCache, HTMLElement>>;
private pagewiseStatus = new Map<number, { onPageReady: boolean, onTextLayerReady: boolean, onAnnotationLayerReady: boolean }>;
private pagewiseOnClearDomCallbacksMap = new MultiValuedMap<number, () => any>();

constructor(visualizer: PDFViewerBacklinkVisualizer) {
super(visualizer.plugin);
Expand All @@ -63,6 +64,7 @@ export class BacklinkDomManager extends PDFPlusComponent {
// Avoid removing elements in the annotation layer
if (el.closest('.pdf-plus-backlink-highlight-layer')) el.remove();
}
this.pagewiseOnClearDomCallbacksMap.get(pageNumber).forEach(cb => cb());
this.pagewiseCacheToDomsMap.delete(pageNumber);
this.updateStatus(pageNumber, { onPageReady: false, onTextLayerReady: false, onAnnotationLayerReady: false });
}
Expand Down Expand Up @@ -121,7 +123,7 @@ export class BacklinkDomManager extends PDFPlusComponent {
if (typeof lineNumber === 'number') {
state.scroll = lineNumber;
}
this.registerDomEvent(el, 'mouseover', (event) => {
this.registerDomEventForCache(cache, el, 'mouseover', (event) => {
this.app.workspace.trigger('hover-link', {
event,
source: 'pdf-plus',
Expand All @@ -133,7 +135,7 @@ export class BacklinkDomManager extends PDFPlusComponent {
});
});

this.registerDomEvent(el, 'dblclick', (event) => {
this.registerDomEventForCache(cache, el, 'dblclick', (event) => {
if (this.plugin.settings.doubleClickHighlightToOpenBacklink) {
const paneType = Keymap.isModEvent(event);
this.lib.workspace.openMarkdownLinkFromPDF(cache.sourcePath, this.file.path, paneType, pos ? { pos } : undefined);
Expand All @@ -142,7 +144,7 @@ export class BacklinkDomManager extends PDFPlusComponent {
}

hookBacklinkViewEventHandlers(el: HTMLElement, cache: PDFBacklinkCache) {
this.registerDomEvent(el, 'mouseover', (event) => {
this.registerDomEventForCache(cache, el, 'mouseover', (event) => {
// highlight the corresponding item in backlink pane
if (this.plugin.settings.highlightBacklinksPane) {
this.lib.workspace.iterateBacklinkViews((view) => {
Expand All @@ -169,7 +171,7 @@ export class BacklinkDomManager extends PDFPlusComponent {
}

hookContextMenuHandler(el: HTMLElement, cache: PDFBacklinkCache) {
this.registerDomEvent(el, 'contextmenu', (evt) => {
this.registerDomEventForCache(cache, el, 'contextmenu', (evt) => {
onBacklinkVisualizerContextMenu(evt, this.visualizer, cache);
});
}
Expand All @@ -180,7 +182,7 @@ export class BacklinkDomManager extends PDFPlusComponent {
if (typeof pageNumber === 'number') {
const className = 'is-hovered';

el.addEventListener('mouseover', () => {
this.registerDomEventForCache(cache, el, 'mouseover', () => {
for (const otherEl of this.getCacheToDomsMap(pageNumber).get(cache)) {
otherEl.addClass(className);
}
Expand Down Expand Up @@ -208,6 +210,19 @@ export class BacklinkDomManager extends PDFPlusComponent {
});
}
}

onClearDomInPage(pageNumber: number, callback: () => any) {
this.pagewiseOnClearDomCallbacksMap.addValue(pageNumber, callback);
}

registerDomEventForCache<K extends keyof HTMLElementEventMap>(cache: PDFBacklinkCache, el: HTMLElement, type: K, callback: (this: HTMLElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions) {
this.registerDomEvent(el, type, callback, options);
if (cache.page && cache.annotation) {
this.onClearDomInPage(cache.page, () => {
el.removeEventListener(type, callback);
});
}
}
}


Expand Down
34 changes: 32 additions & 2 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Component, EditableFileView, MarkdownView, Notice, Platform, TFile, TextFileView, View, base64ToArrayBuffer, parseLinktext, requestUrl } from 'obsidian';
import { App, Component, EditableFileView, MarkdownView, Notice, Platform, TFile, TextFileView, View, base64ToArrayBuffer, normalizePath, parseLinktext, requestUrl } from 'obsidian';
import { PDFDocumentProxy, PDFPageProxy } from 'pdfjs-dist';
import { EncryptedPDFError, PDFArray, PDFDict, PDFDocument, PDFName, PDFNumber, PDFRef } from '@cantoo/pdf-lib';

Expand Down Expand Up @@ -673,7 +673,7 @@ export class PDFPlusLib {
getBibliographyManager(activeOnly: boolean = false) {
return this.getPDFViewerChild(activeOnly)?.bib;
}

getVim(activeOnly: boolean = false) {
return this.getPDFViewerComponent(activeOnly)?.vim;
}
Expand Down Expand Up @@ -968,4 +968,34 @@ export class PDFPlusLib {
toSingleLine(str: string): string {
return toSingleLine(str, this.plugin.settings.removeWhitespaceBetweenCJChars);
}

/** Write data to the file at path. */
async write(path: string, data: string | ArrayBuffer, existOk: boolean): Promise<TFile | null> {
const file = this.app.vault.getAbstractFileByPath(path);

if (file instanceof TFile) {
if (!existOk) {
new Notice(`${this.plugin.manifest.name}: File already exists: ${path}`);
}
if (typeof data === 'string') {
await this.app.vault.modify(file, data);
} else {
await this.app.vault.modifyBinary(file, data);
}
return file;
} else if (file === null) {
const folderPath = normalizePath(path.split('/').slice(0, -1).join('/'));
if (folderPath) {
const folderExists = !!(this.app.vault.getAbstractFileByPath(folderPath));
if (!folderExists) await this.app.vault.createFolder(folderPath);
}
if (typeof data === 'string') {
return await this.app.vault.create(path, data);
} else {
return await this.app.vault.createBinary(path, data);
}
}

return null;
}
}
8 changes: 8 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export default class PDFPlus extends Plugin {
lastAnnotationPopupChild: PDFViewerChild | null = null;
/** Stores the file and the explicit destination array corresponding to the last link copied with the "Copy link to current page view" command */
lastCopiedDestInfo: { file: TFile, destArray: DestArray } | { file: TFile, destName: string } | null = null;
vimrc: string | null = null;
/** Maps a `div.pdf-viewer` element to the corresponding `PDFViewerChild` object. */
// In most use cases of this map, the goal is also achieved by using lib.workspace.iteratePDFViewerChild.
// However, a PDF embed inside a Canvas text node cannot be handled by the function, so we need this map.
Expand Down Expand Up @@ -546,6 +547,13 @@ export default class PDFPlus extends Plugin {
this.saveSettings();
}
}));

// Keep the vimrc content up-to-date
this.registerEvent(this.app.vault.on('modify', async (file) => {
if (file instanceof TFile && file.path === this.settings.vimrcPath) {
this.vimrc = await this.app.vault.read(file);
}
}));
}

registerOneTimeEvent<T extends Events>(events: T, ...[evt, callback, ctx]: OverloadParameters<T['on']>) {
Expand Down
25 changes: 25 additions & 0 deletions src/modals/markdown-modal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { MarkdownRenderer } from 'obsidian';
import { PDFPlusModal } from './base-modal';
import PDFPlus from 'main';


export class MarkdownModal extends PDFPlusModal {
markdown: string = '';

static renderAsModal(plugin: PDFPlus, markdown: string) {
const modal = new MarkdownModal(plugin);
modal.markdown = markdown;
modal.open();
return modal;
}

onOpen() {
MarkdownRenderer.render(
this.app,
this.markdown,
this.contentEl.createDiv('markdown-rendered'),
'',
this.component
);
}
}
Loading

0 comments on commit daddd9c

Please sign in to comment.