Skip to content

Commit

Permalink
release: 0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Dec 22, 2023
1 parent 46a9ed0 commit 25f964c
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 41 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Each feature can be toggled on and off in the plugin settings.

- **Highlight backlinks**: In the PDF viewer, any referenced text will be highlighted for easy identification. Additionally, when you hover over the highlighted text, a popover will appear, displaying the corresponding backlink.
- **Backlink highlights background color**: Requires the [Style Settings](https://github.com/mgmeyers/obsidian-style-settings) plugin.
- **Highlight hovered backlinks in the backlinks pane**: Hovering over highlighted backlinked text will also highlight the corresponding item in the [backlink pane]((https://help.obsidian.md/Plugins/Backlinks)). This feature is compatible with the [Better Search Views]((https://github.com/ivan-lednev/better-search-views)) plugin.

These features enrich Obsidian as a stand-alone PDF annotation tool. I recommend combining the core [Backlinks](https://help.obsidian.md/Plugins/Backlinks) plugin & the [Better Search View](https://github.com/ivan-lednev/better-search-views) plugin together with this plugin.

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.6.1",
"version": "0.7.0",
"minAppVersion": "1.3.5",
"description": "Enhance PDF viewer & embeds.",
"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.6.1",
"version": "0.7.0",
"description": "An Obsidian.md plugin to enhance PDF viewer & embeds.",
"scripts": {
"dev": "node esbuild.config.mjs",
Expand Down
73 changes: 57 additions & 16 deletions src/backlinks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PDFPlus from "main";
import { App, Component, HoverParent, HoverPopover, Notice, TFile, parseLinktext } from "obsidian";
import { ObsidianViewer } from "typings";
import { App, Component, HoverParent, HoverPopover, Keymap, Notice, TFile, parseLinktext } from "obsidian";
import { BacklinkView, ObsidianViewer } from "typings";


export class BacklinkManager extends Component implements HoverParent {
Expand All @@ -23,7 +23,7 @@ export class BacklinkManager extends Component implements HoverParent {
this.highlightBacklinks();
this.registerEvent(this.app.metadataCache.on('resolved', () => {
this.highlightBacklinks();
}));
}));
}
}

Expand Down Expand Up @@ -60,19 +60,60 @@ export class BacklinkManager extends Component implements HoverParent {
const page = parseInt(params.get('page')!);
const selection = params.get('selection')!.split(',').map((s) => parseInt(s));
if (selection.length === 4) {
let backlinkItemEl: HTMLElement | null = null;
// @ts-ignore
this.viewer.pdfViewer._pagesCapability.promise.then(() => {
this.highlightText(
page,
...selection as [number, number, number, number],
(event, el) => {
this.app.workspace.trigger('hover-link', {
event,
source: 'pdf-plus',
hoverParent: this,
targetEl: el,
linktext: sourcePath,
state: { scroll: link.position.start.line }
(textDiv) => {
this.eventManager.registerDomEvent(textDiv, 'mouseover', (event) => {
this.app.workspace.trigger('hover-link', {
event,
source: 'pdf-plus',
hoverParent: this,
targetEl: textDiv,
linktext: sourcePath,
state: { scroll: link.position.start.line }
});

// highlight the corresponding item in backlink pane

const backlinkLeaf = this.app.workspace.getLeavesOfType('backlink')[0];
if (!backlinkLeaf) return;

const backlinkView = backlinkLeaf.view as BacklinkView;
if (!backlinkView.containerEl.isShown()) return;

const backlinkDom = backlinkView.backlink.backlinkDom;
const sourceFile = this.app.vault.getAbstractFileByPath(sourcePath);
if (!(sourceFile instanceof TFile)) return;

const fileDom = backlinkDom.getResult(sourceFile);
if (!fileDom) return;

const index = fileDom.result.content.findIndex(([start, end]) => start === link.position.start.offset && end === link.position.end.offset);
if (index === -1) return;

// const itemDoms = fileDom?.vChildren.children; // better search view clashes this
backlinkItemEl = fileDom?.childrenEl.querySelectorAll<HTMLElement>('.search-result-file-match')[index];

backlinkItemEl.addClass('hovered-backlink');
});

this.eventManager.registerDomEvent(textDiv, 'mouseout', (event) => {
backlinkItemEl?.removeClass('hovered-backlink');
});

this.eventManager.registerDomEvent(textDiv, 'click', (event) => {
const paneType = Keymap.isModEvent(event);
if (paneType) {
this.app.workspace.openLinkText(sourcePath, "", paneType, {
eState: {
line: link.position.start.line
}
});
}
});
}
);
Expand All @@ -85,7 +126,7 @@ export class BacklinkManager extends Component implements HoverParent {
}

// This is a modified version of PDFViewerChild.prototype.hightlightText from Obsidian's app.js
highlightText(pageNumber: number, beginIndex: number, beginOffset: number, endIndex: number, endOffset: number, onHover?: (evt: MouseEvent, el: HTMLElement) => void) {
highlightText(pageNumber: number, beginIndex: number, beginOffset: number, endIndex: number, endOffset: number, onHighlight?: (textDiv: HTMLElement) => void) {
if (!(pageNumber < 1 || pageNumber > this.viewer.pagesCount)) {
const pageView = this.viewer.pdfViewer.getPageView(pageNumber - 1);
if (pageView != null && pageView.div.dataset.loaded) {
Expand All @@ -110,9 +151,7 @@ export class BacklinkManager extends Component implements HoverParent {
const textNode = document.createTextNode(text);
if (className) {
textDiv.createSpan(className + " appended").append(textNode);
if (onHover) {
this.eventManager.registerDomEvent(textDiv, 'mouseover', (evt) => onHover(evt, textDiv));
}
onHighlight?.(textDiv);
}
else textDiv.append(textNode);
}
Expand All @@ -136,7 +175,9 @@ export class BacklinkManager extends Component implements HoverParent {

clearTextHighlight() {
for (const { page, index } of this.highlightedTexts) {
const { textDivs, textContentItems } = this.viewer.pdfViewer.getPageView(page - 1).textLayer;
const pageView = this.viewer.pdfViewer.getPageView(page - 1);
if (!pageView) return;
const { textDivs, textContentItems } = pageView.textLayer;
const textDiv = textDivs[index];
textDiv.textContent = textContentItems[index].str;
textDiv.className = textDiv.hasClass("textLayerNode") ? "textLayerNode" : "";
Expand Down
27 changes: 13 additions & 14 deletions src/patch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BacklinkManager } from "backlinks";
import PDFPlus from "main";
import { around } from "monkey-around";
import { EditableFileView, TFile, Workspace, parseLinktext } from "obsidian";
import { EditableFileView, OpenViewState, PaneType, TFile, Workspace, parseLinktext } from "obsidian";
import { ObsidianViewer, PDFView, PDFViewer, PDFViewerChild } from "typings";
import { highlightSubpath, onTextLayerReady } from "utils";

Expand Down Expand Up @@ -127,7 +127,7 @@ export const patchWorkspace = (plugin: PDFPlus) => {

plugin.register(around(Workspace.prototype, {
openLinkText(old) {
return function (linktext: string, sourcePath: string, ...args: any[]) {
return function (linktext: string, sourcePath: string, newLeaf?: PaneType | boolean, openViewState?: OpenViewState) {
if (plugin.settings.openLinkCleverly) {
const { path, subpath } = parseLinktext(linktext);
const file = app.metadataCache.getFirstLinkpathDest(path, sourcePath);
Expand All @@ -137,22 +137,21 @@ export const patchWorkspace = (plugin: PDFPlus) => {
return leaf.view instanceof EditableFileView && leaf.view.file === file;
});
if (leaf) {
const view = leaf.view as PDFView;
const self = this as Workspace;
self.revealLeaf(leaf);
if (!plugin.settings.dontActivateAfterOpen) self.setActiveLeaf(leaf);

const child = view.viewer.child;
if (child) {
const duration = plugin.settings.highlightDuration;
highlightSubpath(child, subpath, duration);
}
return;
openViewState = openViewState ?? {};
openViewState.active = !plugin.settings.dontActivateAfterOpen;
return leaf.openLinkText(linktext, sourcePath, openViewState).then(() => {
const view = leaf.view as PDFView;
const child = view.viewer.child;
if (child) {
const duration = plugin.settings.highlightDuration;
highlightSubpath(child, subpath, duration);
}
})
}
}
}

return old.call(this, linktext, sourcePath, ...args);
return old.call(this, linktext, sourcePath, newLeaf, openViewState);
}
}
}));
Expand Down
5 changes: 5 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface PDFPlusSettings {
persistentHighlightsInEmbed: boolean;
highlightBacklinks: boolean;
clickEmbedToOpenLink: boolean;
highlightBacklinksPane: boolean;
}

export const DEFAULT_SETTINGS: PDFPlusSettings = {
Expand All @@ -28,6 +29,7 @@ export const DEFAULT_SETTINGS: PDFPlusSettings = {
persistentHighlightsInEmbed: true,
highlightBacklinks: true,
clickEmbedToOpenLink: true,
highlightBacklinksPane: true,
};

// Inspired by https://stackoverflow.com/a/50851710/13613783
Expand Down Expand Up @@ -132,6 +134,9 @@ export class PDFPlusSettingTab extends PluginSettingTab {
this.addToggleSetting('highlightBacklinks')
.setName('Highlight backlinks')
.setDesc('In the PDF viewer, any referenced text will be highlighted for easy identification. Additionally, when you hover over the highlighted text, a popover will appear, displaying the corresponding backlink. (Being a new feature, this may not work well in some cases. Please reopen the tab if you encounter any problem.)');
this.addToggleSetting('highlightBacklinksPane')
.setName('Highlight hovered backlinks in the backlinks pane')
.setDesc('Hovering over highlighted backlinked text will also highlight the corresponding item in the backlink pane. This feature is compatible with the Better Search Views plugin.');

this.addHeading('Opening links to PDF files');
this.addToggleSetting('openLinkCleverly', () => this.display())
Expand Down

0 comments on commit 25f964c

Please sign in to comment.