Skip to content

Commit

Permalink
release: 0.8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Dec 22, 2023
1 parent 4186427 commit fea515f
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 17 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.
- By default, all backlinks are highlighted. But there is an option that allows you to highlight only backlinks with colors specified in the link text (see below).
- **Double click a piece of highlighted text to open the corresponding backlink**
- **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.
- **Custom highlight colors**: Append `&color={{COLOR NAME}}` to a link text to highlight the selection with a specified color, where `{{COLOR NAME}}` is one of the colors that you register in the plugin settings. e.g `[[file.pdf#page=1&selection=4,0,5,20&color=red]]`
- **Show color palette in the toolbar**: A color palette will be added to the toolbar of the PDF viewer. Clicking a color while selecting a range of text will copy a link to the selection with `&color=...` appended.
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.8.0",
"version": "0.8.1",
"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.8.0",
"version": "0.8.1",
"description": "An Obsidian.md plugin to enhance PDF viewer & embeds.",
"scripts": {
"dev": "node esbuild.config.mjs",
Expand Down
31 changes: 23 additions & 8 deletions src/backlinks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PDFPlus from "main";
import { App, Component, HoverParent, HoverPopover, Keymap, Notice, TFile, parseLinktext } from "obsidian";
import { App, Component, HoverParent, HoverPopover, Keymap, MarkdownView, Notice, PaneType, SectionCache, TFile, parseLinktext } from "obsidian";
import { BacklinkView, ObsidianViewer } from "typings";


Expand Down Expand Up @@ -100,22 +100,37 @@ export class BacklinkManager extends Component implements HoverParent {
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);
// const itemDoms = fileDom?.vChildren.children; // better search view destroys this!! So we have to take a detour

const cache = this.app.metadataCache.getFileCache(sourceFile);
if (!cache?.sections) return;

const sectionsContainingBacklinks = new Set<SectionCache>();
for (const [start, end] of fileDom.result.content) {
const sec = cache.sections.find(sec => sec.position.start.offset <= start && end <= sec.position.end.offset);
if (sec) {
sectionsContainingBacklinks.add(sec);
if (start === link.position.start.offset && end === link.position.end.offset) {
break;
}
}
}

const index = sectionsContainingBacklinks.size - 1;
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');

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

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

this.eventManager.registerDomEvent(highlightedEl, 'click', (event) => {
const paneType = Keymap.isModEvent(event);
if (paneType) {
this.eventManager.registerDomEvent(highlightedEl, 'dblclick', (event) => {
if (this.plugin.settings.doubleClickHighlightToOpenBacklink) {
const paneType = Keymap.isModEvent(event) || 'tab'; // keep the PDF view open
this.app.workspace.openLinkText(sourcePath, "", paneType, {
eState: {
line: link.position.start.line
Expand Down
8 changes: 5 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, Notice, Plugin } from 'obsidian';
import { DEFAULT_SETTINGS, PDFPlusSettings, PDFPlusSettingTab } from 'settings';
import { patchPDF, patchWorkspace } from 'patch';
import { patchPDF, patchPagePreview, patchWorkspace } from 'patch';
import { PDFView, PDFViewerChild } from 'typings';
import { addColorPalette, copyLinkToSelection, isHexString, iteratePDFViews } from 'utils';
import { BacklinkManager } from 'backlinks';
Expand All @@ -20,9 +20,11 @@ export default class PDFPlus extends Plugin {

this.app.workspace.onLayoutReady(() => this.loadStyle());

patchWorkspace(this);


this.app.workspace.onLayoutReady(() => {
patchWorkspace(this);
patchPagePreview(this);

const success = patchPDF(this);
if (!success) {
const notice = new Notice(`${this.manifest.name}: Open a PDF file to enable the plugin.`, 0);
Expand Down
32 changes: 31 additions & 1 deletion 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 { ColorComponent, EditableFileView, OpenViewState, PaneType, TFile, Workspace, parseLinktext } from "obsidian";
import { ColorComponent, EditableFileView, FileView, HoverParent, MarkdownView, OpenViewState, PaneType, TFile, Workspace, parseLinktext } from "obsidian";
import { ObsidianViewer, PDFToolbar, PDFView, PDFViewer, PDFViewerChild } from "typings";
import { addColorPalette, highlightSubpath, isHexString, onTextLayerReady } from "utils";

Expand Down Expand Up @@ -168,3 +168,33 @@ export const patchWorkspace = (plugin: PDFPlus) => {
}
}));
};

export const patchPagePreview = (plugin: PDFPlus) => {
const app = plugin.app;
const pagePreview = app.internalPlugins.plugins['page-preview'].instance;

plugin.register(around(pagePreview.constructor.prototype, {
onLinkHover(old) {
return function (hoverParent: HoverParent, targetEl: HTMLElement | null, linktext: string, sourcePath: string, state: any): void {
if (plugin.settings.openOnHoverHighlight && hoverParent instanceof BacklinkManager) {
const file = app.metadataCache.getFirstLinkpathDest(linktext, sourcePath);
let leafFound = false;
app.workspace.iterateAllLeaves((leaf) => {
if (leafFound) return;

if (leaf.view instanceof MarkdownView && leaf.view.file === file) {
leaf.openLinkText(linktext, sourcePath, { eState: { line: state.scroll } });
leafFound = true;
}
});
if (!leafFound) {
// seems like the third parameter is just ignored by Obsidian??
app.workspace.openLinkText(linktext, sourcePath, true, { eState: { line: state.scroll } });
}
return;
}
old.call(this, hoverParent, targetEl, linktext, sourcePath, state);
}
}
}));
}
9 changes: 9 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export interface PDFPlusSettings {
defaultColor: string;
colorPaletteInToolbar: boolean;
highlightColorSpecifiedOnly: boolean;
doubleClickHighlightToOpenBacklink: boolean;
openOnHoverHighlight: boolean;
}

export const DEFAULT_SETTINGS: PDFPlusSettings = {
Expand All @@ -42,6 +44,8 @@ export const DEFAULT_SETTINGS: PDFPlusSettings = {
defaultColor: '',
colorPaletteInToolbar: true,
highlightColorSpecifiedOnly: false,
doubleClickHighlightToOpenBacklink: true,
openOnHoverHighlight: true,
};

// Inspired by https://stackoverflow.com/a/50851710/13613783
Expand Down Expand Up @@ -227,6 +231,11 @@ 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('openOnHoverHighlight')
.setName('Actually open backlink rather than popover preview when hovering over a highlight')
.setDesc('When hovering over a highlight, actually open the corresponding backlink instead of displaying a popover preview')
this.addToggleSetting('doubleClickHighlightToOpenBacklink')
.setName('Double click a piece of highlighted text to open the corresponding backlink');
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.');
Expand Down
13 changes: 12 additions & 1 deletion src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BacklinkManager } from 'backlinks';
import { App, CachedMetadata, Component, Debouncer, EditableFileView, FileView, Modal, PluginSettingTab, Scope, SearchComponent, SearchMatches, SettingTab, TFile } from 'obsidian';
import { App, CachedMetadata, Component, Debouncer, EditableFileView, FileView, Modal, PluginSettingTab, Scope, SearchComponent, SearchMatches, SettingTab, TFile, HoverParent } from 'obsidian';
import { PDFDocumentProxy, PDFPageProxy, PageViewport } from 'pdfjs-dist';


Expand Down Expand Up @@ -68,6 +68,7 @@ interface PDFAnnotationHighlight extends PDFHighlight {

interface ObsidianViewer {
dom: {
containerEl: HTMLElement;
viewerEl: HTMLElement;
viewerContainerEl: HTMLElement;
} | null;
Expand Down Expand Up @@ -371,6 +372,15 @@ declare module "obsidian" {
setting: AppSetting;
plugins: {
manifests: Record<string, PluginManifest>;
},
internalPlugins: {
plugins: {
'page-preview': {
instance: {
onLinkHover(hoverParent: HoverParent, targetEl: HTMLElement | null, linktext: string, sourcePath: string, state: any): void;
}
}
}
}
embedRegistry: EmbedRegistry;
}
Expand All @@ -384,6 +394,7 @@ declare module "obsidian" {
}

interface WorkspaceLeaf {
group: string | null;
openLinkText(linktext: string, sourcePath: string, openViewState?: OpenViewState): Promise<void>;
}
}

0 comments on commit fea515f

Please sign in to comment.