From 24ee96b435f6526a62e16b824b2998c55013596e Mon Sep 17 00:00:00 2001 From: RyotaUshio Date: Wed, 1 Jan 2025 21:00:15 +0900 Subject: [PATCH] release: 0.40.21 --- manifest-beta.json | 2 +- manifest.json | 2 +- package-lock.json | 4 ++-- package.json | 2 +- src/lib/copy-link.ts | 4 ++-- src/main.ts | 17 ++++++++++++----- src/patchers/pdf-internals.ts | 12 ++++++++++++ src/post-process/pdf-link-like.ts | 17 ++++++++++++++++- src/typings.d.ts | 3 ++- src/vim/text-structure-parser.ts | 7 ++++++- src/vim/visual.ts | 2 +- styles.css | 31 ++++++++++++++++++++++++++++++- 12 files changed, 86 insertions(+), 17 deletions(-) diff --git a/manifest-beta.json b/manifest-beta.json index 42faf44..28d074a 100644 --- a/manifest-beta.json +++ b/manifest-beta.json @@ -1,7 +1,7 @@ { "id": "pdf-plus", "name": "PDF++", - "version": "0.40.20", + "version": "0.40.21", "minAppVersion": "1.5.8", "description": "The most Obsidian-native PDF annotation tool ever.", "author": "Ryota Ushio", diff --git a/manifest.json b/manifest.json index 42faf44..28d074a 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "pdf-plus", "name": "PDF++", - "version": "0.40.20", + "version": "0.40.21", "minAppVersion": "1.5.8", "description": "The most Obsidian-native PDF annotation tool ever.", "author": "Ryota Ushio", diff --git a/package-lock.json b/package-lock.json index ff6600d..5fdb2a8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-pdf-plus", - "version": "0.40.20", + "version": "0.40.21", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "obsidian-pdf-plus", - "version": "0.40.20", + "version": "0.40.21", "license": "MIT", "devDependencies": { "@cantoo/pdf-lib": "^1.21.1", diff --git a/package.json b/package.json index ed209dc..7727fd8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-pdf-plus", - "version": "0.40.20", + "version": "0.40.21", "description": "The most Obsidian-native PDF annotation tool ever.", "scripts": { "dev": "node esbuild.config.mjs", diff --git a/src/lib/copy-link.ts b/src/lib/copy-link.ts index d6de904..cd714b0 100644 --- a/src/lib/copy-link.ts +++ b/src/lib/copy-link.ts @@ -50,9 +50,9 @@ export class copyLinkLib extends PDFPlusLibSubmodule { const endOffset = getOffsetInTextLayerNode(endTextLayerNode, range.endContainer, range.endOffset); if (beginIndex !== undefined && endIndex !== undefined && beginOffset !== null && endOffset !== null) return { - beginIndex: +beginIndex, + beginIndex: +beginIndex - this.plugin.textDivFirstIdx, beginOffset, - endIndex: +endIndex, + endIndex: +endIndex - this.plugin.textDivFirstIdx, endOffset }; } diff --git a/src/main.ts b/src/main.ts index 4f8ea57..0ae63d9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -79,6 +79,7 @@ export default class PDFPlus extends Plugin { pdfViewerChildren: Map = new Map(); /** Stores all the shown context menu objects. Used to close all visible menus programatically. */ shownMenus: Set = new Set(); + textDivFirstIdx: number; isDebugMode: boolean = false; async onload() { @@ -142,10 +143,10 @@ export default class PDFPlus extends Plugin { } checkVersion() { - const untestedVersion = '1.8.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).`); - } + // See: + // https://forum.obsidian.md/t/in-1-8-0-pdf-copy-link-to-selection-fails-to-copy-proper-links-in-some-cases/93545 + // https://github.com/RyotaUshio/obsidian-pdf-plus/issues/327 + this.textDivFirstIdx = requireApiVersion('1.8.0') ? 1 : 0; InstallerVersionModal.openIfNecessary(this); } @@ -733,7 +734,13 @@ export default class PDFPlus extends Plugin { openSettingTab(): PDFPlusSettingTab { this.app.setting.open(); - return this.app.setting.openTabById(this.manifest.id); + // This `if` check is necessary. If we omit it, the following bug occurs: + // https://github.com/RyotaUshio/obsidian-pdf-plus/issues/309 + // I learned this from the core Sync plugin's `openSettings` method. + if (this.app.setting.activeTab !== this.settingTab) { + this.app.setting.openTabById(this.manifest.id); + } + return this.settingTab; } openHotkeySettingTab(query?: string): SettingTab { diff --git a/src/patchers/pdf-internals.ts b/src/patchers/pdf-internals.ts index 2603090..876a5fe 100644 --- a/src/patchers/pdf-internals.ts +++ b/src/patchers/pdf-internals.ts @@ -584,6 +584,18 @@ const patchPDFViewerChild = (plugin: PDFPlus, child: PDFViewerChild) => { return embedLink.slice(1); }; }, + getTextSelectionRangeStr() { + return function (this: PDFViewerChild, pageEl: HTMLElement) { + const selection = pageEl.win.getSelection(); + const range = (selection && selection.rangeCount > 0) ? selection.getRangeAt(0) : null; + const textSelectionRange = range && lib.copyLink.getTextSelectionRange(pageEl, range); + if (textSelectionRange) { + const { beginIndex, beginOffset, endIndex, endOffset } = textSelectionRange; + return `${beginIndex},${beginOffset},${endIndex},${endOffset}`; + } + return null; + }; + }, getPageLinkAlias(old) { return function (this: PDFViewerChild, page: number): string { if (this.file) { diff --git a/src/post-process/pdf-link-like.ts b/src/post-process/pdf-link-like.ts index be3be0c..ee88801 100644 --- a/src/post-process/pdf-link-like.ts +++ b/src/post-process/pdf-link-like.ts @@ -42,6 +42,7 @@ abstract class PDFLinkLikePostProcessor implements HoverParent { set hoverPopover(hoverPopover) { this.child.hoverPopover = hoverPopover; if (hoverPopover) { + hoverPopover.hoverEl.addClass('pdf-plus-pdf-link-like-popover'); this.onHoverPopoverSet(hoverPopover); } } @@ -183,7 +184,6 @@ abstract class PDFDestinationHolderPostProcessor extends PDFLinkLikePostProcesso onHoverPopoverSet(hoverPopover: HoverPopover): void { const el = hoverPopover.hoverEl; - el.addClass('pdf-plus-pdf-internal-link-popover'); const dest = this.getDest(); if (typeof dest === 'string') el.dataset.dest = dest; } @@ -266,6 +266,11 @@ export class PDFInternalLinkPostProcessor extends PDFDestinationHolderPostProces return false; } + + onHoverPopoverSet(hoverPopover: HoverPopover): void { + super.onHoverPopoverSet(hoverPopover); + hoverPopover.hoverEl.addClass('pdf-plus-pdf-internal-link-popover'); + } } @@ -299,6 +304,11 @@ export class PDFOutlineItemPostProcessor extends PDFDestinationHolderPostProcess return this.plugin.settings.recordHistoryOnOutlineClick && !this.child.opts.isEmbed; } + + onHoverPopoverSet(hoverPopover: HoverPopover): void { + super.onHoverPopoverSet(hoverPopover); + hoverPopover.hoverEl.addClass('pdf-plus-outline-item-popover'); + } } @@ -328,4 +338,9 @@ export class PDFThumbnailItemPostProcessor extends PDFLinkLikePostProcessor { return this.plugin.settings.recordHistoryOnThumbnailClick && !this.child.opts.isEmbed; } + + onHoverPopoverSet(hoverPopover: HoverPopover): void { + super.onHoverPopoverSet(hoverPopover); + hoverPopover.hoverEl.addClass('pdf-plus-thumbnail-item-popover'); + } } diff --git a/src/typings.d.ts b/src/typings.d.ts index 4ba5fa6..1e6d505 100644 --- a/src/typings.d.ts +++ b/src/typings.d.ts @@ -118,7 +118,7 @@ interface PDFViewerChild { getTextByRect(pageView: PDFPageView, rect: Rect): string; getAnnotationFromEvt(pageView: PDFPageView, evt: MouseEvent): AnnotationElement | null; getPageLinkAlias(page: number): string; - getTextSelectionRangeStr(el: HTMLElement): string; + getTextSelectionRangeStr(pageEl: HTMLElement): string | null; getMarkdownLink(subpath?: string, alias?: string, embed?: boolean): string; onContextMenu(evt: MouseEvent): void; /** On mobile, tapping on "Copy" in the the OS-provided menu calls this method, in which, in the original implementation, performs "Copy as quote". */ @@ -812,6 +812,7 @@ interface Queue { interface AppSetting extends Modal { openTab(tab: SettingTab): void; openTabById(id: string): any; + activeTab: SettingTab | null; pluginTabs: PluginSettingTab[]; } diff --git a/src/vim/text-structure-parser.ts b/src/vim/text-structure-parser.ts index 001fd2e..07d75b3 100644 --- a/src/vim/text-structure-parser.ts +++ b/src/vim/text-structure-parser.ts @@ -44,7 +44,12 @@ export class PDFDocumentTextStructureParser extends PDFPlusComponent { } -export type PDFTextPos = { index: number, offset: number }; +export type PDFTextPos = { + /** The 0-origin index of the text layer node containing this position. */ + index: number; + /** The offset of this position within the text layer node. */ + offset: number; +}; export class PDFPageTextStructureParser { diff --git a/src/vim/visual.ts b/src/vim/visual.ts index f4b13bb..2d9a1cc 100644 --- a/src/vim/visual.ts +++ b/src/vim/visual.ts @@ -210,7 +210,7 @@ export class VimVisualMode extends VimBindingsMode { } } - return { index, offset }; + return { index: index - this.plugin.textDivFirstIdx, offset }; } extendSelection(getNewHeadPos: (state: { currentHeadPos: PDFTextPos, pageNumber: number, pageParser: PDFPageTextStructureParser }) => PDFTextPos | null) { diff --git a/styles.css b/styles.css index 0dcbe7d..c1b2f7a 100644 --- a/styles.css +++ b/styles.css @@ -66,7 +66,7 @@ settings: - id: pdf-plus-backlink-popover title: Backlink popovers - description: Style for popovers displayed when hovering over backlinked highlights or rectangular selections in PDF viewer + description: Styles for popovers displayed when hovering over backlinked highlights or rectangular selections in PDF viewer type: heading level: 3 - @@ -87,6 +87,30 @@ settings: step: 10 default: 400 format: px + - + id: pdf-plus-pdf-link-like-popover + title: PDF internal link popovers + description: Styles for popovers displayed when hovering over internal links, outline items or thumbnails in PDF viewer + type: heading + level: 3 + - + id: pdf-plus-pdf-link-like-popover-width + title: PDF internal link popover width (px) + type: variable-number-slider + min: 100 + max: 1000 + step: 10 + default: 450 + format: px + - + id: pdf-plus-pdf-link-like-popover-height + title: PDF internal link popover height (px) + type: variable-number-slider + min: 100 + max: 1000 + step: 10 + default: 400 + format: px - id: pdf-toolbar title: PDF toolbars @@ -560,6 +584,11 @@ body { --popover-height: var(--pdf-plus-backlink-popover-height, 400px); } +.popover.hover-popover.pdf-plus-pdf-link-like-popover { + --popover-pdf-width: var(--pdf-plus-pdf-link-like-popover-width, 450px); + --popover-pdf-height: var(--pdf-plus-pdf-link-like-popover-height, 400px); +} + .popover.hover-popover.pdf-plus-bib-popover { --popover-width: 400px; --pdf-plus-bib-metadata-font-size: var(--font-ui-small);