Skip to content

Commit

Permalink
release: 0.40.6
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Jun 11, 2024
1 parent 797f24e commit f527c6a
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 63 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.40.5",
"version": "0.40.6",
"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.5",
"version": "0.40.6",
"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.5",
"version": "0.40.6",
"description": "The most Obsidian-native PDF annotation tool ever.",
"scripts": {
"dev": "node esbuild.config.mjs",
Expand Down
13 changes: 7 additions & 6 deletions src/bib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { PDFDocumentProxy } from 'pdfjs-dist';
import PDFPlus from 'main';
import { PDFPlusComponent } from 'lib/component';
import { genId, isCanvas, isEmbed, isHoverPopover, isNonEmbedLike, onModKeyPress, toSingleLine } from 'utils';
import { PDFViewerChild, PDFjsDestArray, TextContentItem } from 'typings';
import { PDFViewerChild, PDFJsDestArray, TextContentItem } from 'typings';


export type AnystyleJson = Partial<{
Expand Down Expand Up @@ -61,7 +61,7 @@ export class BibliographyManager extends PDFPlusComponent {
const promises: Promise<void>[] = [];
for (const destId in dests) {
if (destId.startsWith('cite.')) {
const destArray = dests[destId] as PDFjsDestArray;
const destArray = dests[destId] as PDFJsDestArray;
promises.push(
BibliographyManager.getBibliographyTextFromDest(destArray, doc)
.then((bibInfo) => {
Expand Down Expand Up @@ -151,7 +151,8 @@ export class BibliographyManager extends PDFPlusComponent {
// Clean up the file when this PDF viewer is unloaded
this.register(() => app.vault.adapter.remove(anystyleInputPath));

const { spawn } = await import('child_process');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { spawn } = require('child_process') as typeof import('child_process');

return new Promise<any>((resolve) => {
const anystyleProcess = spawn(anystylePath, ['parse', anystyleInputFullPath]);
Expand Down Expand Up @@ -208,10 +209,10 @@ export class BibliographyManager extends PDFPlusComponent {
return null;
}

static async getBibliographyTextFromDest(dest: string | PDFjsDestArray, doc: PDFDocumentProxy) {
let explicitDest: PDFjsDestArray | null = null;
static async getBibliographyTextFromDest(dest: string | PDFJsDestArray, doc: PDFDocumentProxy) {
let explicitDest: PDFJsDestArray | null = null;
if (typeof dest === 'string') {
explicitDest = (await doc.getDestination(dest)) as PDFjsDestArray | null;
explicitDest = (await doc.getDestination(dest)) as PDFJsDestArray | null;
} else {
explicitDest = dest;
}
Expand Down
7 changes: 4 additions & 3 deletions src/color-palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ export class ColorPalette extends PDFPlusComponent {
itemEl.createDiv(ColorPalette.CLS + '-item-inner');
this.setTooltipToActionItem(itemEl, name);

itemEl.addEventListener('click', (evt) => this.onItemClick(itemEl, name, evt));
// Listen to pointerup, not click, to prevent the selection from being cleared before the handler is called on mobile devices.
itemEl.addEventListener('pointerup', (evt) => this.onItemPointerUp(itemEl, name, evt));

let shown = false;
itemEl.addEventListener('contextmenu', () => {
Expand All @@ -140,7 +141,7 @@ export class ColorPalette extends PDFPlusComponent {
});
}

onItemClick(itemEl: HTMLElement, name: string | null, evt: MouseEvent) {
onItemPointerUp(itemEl: HTMLElement, name: string | null, evt: MouseEvent) {
const colorChanged = !itemEl.hasClass('is-active');
this.setActiveItem(name);
if (this.plugin.settings.syncColorPaletteItem && this.plugin.settings.syncDefaultColorPaletteItem) {
Expand All @@ -159,7 +160,7 @@ export class ColorPalette extends PDFPlusComponent {
this.lib.copyLink.copyLinkToSelection(false, { copyFormat: template }, name ?? undefined);
}

evt.preventDefault();
evt.preventDefault();
}

setActiveItem(name: string | null) {
Expand Down
4 changes: 2 additions & 2 deletions src/context-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export const onOutlineItemContextMenu = (plugin: PDFPlus, child: PDFViewerChild,
plugin.lastCopiedDestInfo = { file, destName: dest };
} else {
const pageNumber = await item.getPageNumber();
const destArray = lib.normalizePDFjsDestArray(dest, pageNumber);
const destArray = lib.normalizePDFJsDestArray(dest, pageNumber);
plugin.lastCopiedDestInfo = { file, destArray };
}
})
Expand Down Expand Up @@ -706,7 +706,7 @@ export class PDFPlusContextMenu extends PDFPlusMenu {
.setIcon('lucide-copy')
.onClick(() => {
// How does the electron version differ?
navigator.clipboard.writeText(selectedText);
navigator.clipboard.writeText(this.plugin.settings.copyAsSingleLine ? selectedText : (selectionObj?.toString() ?? ''));
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/copy-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class copyLinkLib extends PDFPlusLibSubmodule {
async getTextToCopyForOutlineItemDynamic(child: PDFViewerChild, file: TFile, item: PDFOutlineTreeNode) {
const dest = await item.getExplicitDestination();
const pageNumber = await item.getPageNumber();
const destArray = this.lib.normalizePDFjsDestArray(dest, pageNumber);
const destArray = this.lib.normalizePDFJsDestArray(dest, pageNumber);
const subpath = this.lib.destArrayToSubpath(destArray);

return (sourcePath?: string) => this.getTextToCopy(
Expand Down
28 changes: 8 additions & 20 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import { PDFOutlines } from './outlines';
import { NameTree, NumberTree } from './name-or-number-trees';
import { PDFNamedDestinations } from './destinations';
import { PDFPageLabels } from './page-labels';
import { AnnotationElement, CanvasFileNode, CanvasNode, CanvasView, DestArray, EventBus, ObsidianViewer, PDFOutlineViewer, PDFPageView, PDFSidebar, PDFThumbnailView, PDFView, PDFViewExtraState, PDFViewerChild, PDFjsDestArray, PDFViewer, PDFEmbed, PDFViewState, Rect, TextContentItem, PDFFindBar, PDFSearchSettings } from 'typings';
import { AnnotationElement, CanvasFileNode, CanvasNode, CanvasView, DestArray, EventBus, ObsidianViewer, PDFPageView, PDFView, PDFViewExtraState, PDFViewerChild, PDFJsDestArray, PDFViewer, PDFEmbed, PDFViewState, Rect, TextContentItem, PDFFindBar, PDFSearchSettings, PDFJsEventMap } from 'typings';
import { PDFCroppedEmbed } from 'pdf-cropped-embed';
import { PDFBacklinkIndex } from './pdf-backlink-index';
import { Speech } from './speech';
import { SidebarView } from 'pdfjs-enums';


export class PDFPlusLib {
Expand Down Expand Up @@ -54,18 +53,7 @@ export class PDFPlusLib {
/**
* @param component A component such that the callback is unregistered when the component is unloaded, or `null` if the callback should be called only once.
*/
registerPDFEvent(name: 'outlineloaded', eventBus: EventBus, component: Component | null, callback: (data: { source: PDFOutlineViewer, outlineCount: number, currentOutlineItemPromise: Promise<void> }) => any): void;
registerPDFEvent(name: 'thumbnailrendered', eventBus: EventBus, component: Component | null, callback: (data: { source: PDFThumbnailView, pageNumber: number, pdfPage: PDFPageProxy }) => any): void;
registerPDFEvent(name: 'sidebarviewchanged', eventBus: EventBus, component: Component | null, callback: (data: { source: PDFSidebar, view: SidebarView }) => any): void;
registerPDFEvent(name: 'textlayerrendered', eventBus: EventBus, component: Component | null, callback: (data: { source: PDFPageView, pageNumber: number }) => any): void;
registerPDFEvent(name: 'annotationlayerrendered', eventBus: EventBus, component: Component | null, callback: (data: { source: PDFPageView, pageNumber: number }) => any): void;
registerPDFEvent(name: 'pagesloaded', eventBus: EventBus, component: Component | null, callback: (data: { source: PDFViewer, pagesCount: number }) => any): void;
registerPDFEvent(name: 'pagerendered', eventBus: EventBus, component: Component | null, callback: (data: { source: PDFPageView, pageNumber: number, cssTransform: boolean, timestamp: number, error: any }) => any): void;
registerPDFEvent(name: 'pagechanging', eventBus: EventBus, component: Component | null, callback: (data: { source: PDFViewer, pageNumber: number, pageLabel: string | null, previous: number }) => any): void;
registerPDFEvent(name: 'findbaropen', eventBus: EventBus, component: Component | null, callback: (data: { source: PDFFindBar }) => any): void;
registerPDFEvent(name: 'findbarclose', eventBus: EventBus, component: Component | null, callback: (data: { source: PDFFindBar }) => any): void;

registerPDFEvent(name: string, eventBus: EventBus, component: Component | null, callback: (data: any) => any) {
registerPDFEvent<K extends keyof PDFJsEventMap>(name: K, eventBus: EventBus, component: Component | null, callback: (data: PDFJsEventMap[K]) => any) {
const listener = async (data: any) => {
await callback(data);
if (!component) eventBus.off(name, listener);
Expand Down Expand Up @@ -293,22 +281,22 @@ export class PDFPlusLib {
* shall be retained unchanged. A zoom value of 0 has the same meaning as a null value."
*/
async destIdToSubpath(destId: string, doc: PDFDocumentProxy) {
const dest = await doc.getDestination(destId) as PDFjsDestArray;
const dest = await doc.getDestination(destId) as PDFJsDestArray;
if (!dest) return null;
return this.pdfJsDestArrayToSubpath(dest, doc);
}

async pdfJsDestArrayToSubpath(dest: PDFjsDestArray, doc: PDFDocumentProxy) {
async pdfJsDestArrayToSubpath(dest: PDFJsDestArray, doc: PDFDocumentProxy) {
const page = await doc.getPageIndex(dest[0]);
return this.destArrayToSubpath(this.normalizePDFjsDestArray(dest, page + 1));
return this.destArrayToSubpath(this.normalizePDFJsDestArray(dest, page + 1));
}

/**
*
* @param dest
* @param pageNumber 1-based page number
*/
normalizePDFjsDestArray(dest: PDFjsDestArray, pageNumber: number): DestArray {
normalizePDFJsDestArray(dest: PDFJsDestArray, pageNumber: number): DestArray {
return [
pageNumber - 1,
dest[1].name,
Expand Down Expand Up @@ -340,9 +328,9 @@ export class PDFPlusLib {

async ensureDestArray(dest: string | DestArray, doc: PDFDocumentProxy) {
if (typeof dest === 'string') {
const destArray = await doc.getDestination(dest) as PDFjsDestArray;
const destArray = await doc.getDestination(dest) as PDFJsDestArray;
if (!destArray) return null;
dest = this.normalizePDFjsDestArray(destArray, await doc.getPageIndex(destArray[0]) + 1);
dest = this.normalizePDFJsDestArray(destArray, await doc.getPageIndex(destArray[0]) + 1);
}

return dest;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/outlines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class PDFOutlines {
}
} else {
const pageNumber = await node.getPageNumber();
if (JSON.stringify(this.lib.normalizePDFjsDestArray(dest, pageNumber)) === JSON.stringify(outlineDest)) {
if (JSON.stringify(this.lib.normalizePDFJsDestArray(dest, pageNumber)) === JSON.stringify(outlineDest)) {
found = outlineItem;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export default class PDFPlus extends Plugin {

await this.loadSettings();
await this.saveSettings();
this.addSettingTab(this.settingTab = new PDFPlusSettingTab(this));

this.domManager = this.addChild(new DomManager(this));
this.domManager.registerCalloutRenderer();
Expand All @@ -111,6 +110,8 @@ export default class PDFPlus extends Plugin {
this.startTrackingActiveMarkdownFile();

this.registerObsidianProtocolHandler('pdf-plus', this.obsidianProtocolHandler.bind(this));

this.addSettingTab(this.settingTab = new PDFPlusSettingTab(this));
}

async onunload() {
Expand All @@ -129,7 +130,7 @@ export default class PDFPlus extends Plugin {
}

private checkVersion() {
const untestedVersion = '1.6.3';
const untestedVersion = '1.7.0';
if (requireApiVersion(untestedVersion)) {
console.warn(`${this.manifest.name}: This plugin has not been tested on Obsidian ${untestedVersion} or above. Please report any issue you encounter on GitHub (https://github.com/RyotaUshio/obsidian-pdf-plus/issues/new/choose).`);
}
Expand Down
27 changes: 27 additions & 0 deletions src/patchers/pdf-internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ const patchPDFViewerChild = (plugin: PDFPlus, child: PDFViewerChild) => {
addColorPaletteToToolbar();
plugin.on('update-dom', addColorPaletteToToolbar);

// Use !isMobile, not isDesktopApp, because in app.js, PDFViewerChild.onMobileCopy is called when isMobile is true.
if (!Platform.isMobile) {
const eventBus = this.pdfViewer.eventBus;
if (eventBus) {
eventBus.on('textlayerrendered', ({ source: pageView }) => {
const textLayerDiv = pageView?.textLayer?.div;
if (textLayerDiv) {
textLayerDiv.addEventListener('copy', onCopy);
}
});
}
}

return ret;
}

Expand Down Expand Up @@ -820,6 +833,7 @@ const patchPDFViewerChild = (plugin: PDFPlus, child: PDFViewerChild) => {
return function (this: PDFViewerChild, evt: ClipboardEvent, pageView: PDFPageView) {
switch (plugin.settings.mobileCopyAction) {
case 'text':
onCopy(evt);
return;
case 'pdf-plus':
setTimeout(() => lib.commands.copyLink(false));
Expand All @@ -839,6 +853,19 @@ const patchPDFViewerChild = (plugin: PDFPlus, child: PDFViewerChild) => {
}
}
}));

const onCopy = (evt: ClipboardEvent) => {
if (!plugin.settings.copyAsSingleLine) return;

const dataTransfer = evt.clipboardData;
if (!dataTransfer) return;

let text = (evt.target as HTMLElement).win.getSelection()?.toString(); // dataTransfer.getData('text/plain');
if (text) {
text = lib.toSingleLine(text);
dataTransfer.setData('text/plain', text);
}
};
}

/** Monkey-patch ObsidianViewer so that it can open external PDF files. */
Expand Down
6 changes: 3 additions & 3 deletions src/post-process/pdf-link-like.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { App, HoverParent, HoverPopover, Keymap } from 'obsidian';

import PDFPlus from 'main';
import { PDFPlusLib } from 'lib';
import { AnnotationElement, PDFOutlineTreeNode, PDFViewerChild, PDFjsDestArray } from 'typings';
import { AnnotationElement, PDFOutlineTreeNode, PDFViewerChild, PDFJsDestArray } from 'typings';
import { isCitationId, isMouseEventExternal, isTargetHTMLElement } from 'utils';
import { BibliographyManager } from 'bib';

Expand Down Expand Up @@ -159,7 +159,7 @@ abstract class PDFLinkLikePostProcessor implements HoverParent {
* The destination can be either a string (a named destination) or an array (an explicit destination).
*/
abstract class PDFDestinationHolderPostProcessor extends PDFLinkLikePostProcessor {
abstract getDest(): string | PDFjsDestArray;
abstract getDest(): string | PDFJsDestArray;

async getLinkText(evt: MouseEvent) {
const { lib, child, targetEl } = this;
Expand Down Expand Up @@ -226,7 +226,7 @@ export class PDFInternalLinkPostProcessor extends PDFDestinationHolderPostProces
return super.getLinkText(evt);
}

getDest(): string | PDFjsDestArray {
getDest(): string | PDFJsDestArray {
return this.linkAnnotationElement.data.dest;
}

Expand Down
Loading

0 comments on commit f527c6a

Please sign in to comment.