Skip to content

Commit

Permalink
release: 0.40.14
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Nov 7, 2024
1 parent 0334d46 commit 3d20afb
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 33 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.13",
"version": "0.40.14",
"minAppVersion": "1.5.8",
"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.13",
"version": "0.40.14",
"minAppVersion": "1.5.8",
"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.13",
"version": "0.40.14",
"description": "The most Obsidian-native PDF annotation tool ever.",
"scripts": {
"dev": "node esbuild.config.mjs",
Expand Down
5 changes: 4 additions & 1 deletion src/lib/copy-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ export class copyLinkLib extends PDFPlusLibSubmodule {
page = child.pdfViewer.pdfViewer?.currentPageNumber ?? page;
}

const selectionStr = child.getTextSelectionRangeStr(pageEl);
if (!selectionStr) return null;

const subpath = paramsToSubpath({
page,
selection: child.getTextSelectionRangeStr(pageEl),
selection: selectionStr,
...subpathParams
});

Expand Down
54 changes: 27 additions & 27 deletions src/lib/highlights/geometry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PDFPlusLibSubmodule } from 'lib/submodule';
import { PropRequired } from 'utils';
import { getNodeAndOffsetOfTextPos, PropRequired } from 'utils';
import { TextLayerBuilder, Rect, TextContentItem } from 'typings';


Expand All @@ -13,7 +13,7 @@ export class HighlightGeometryLib extends PDFPlusLibSubmodule {
* Each rectangle is associated with an array of indices of the text content items contained in the rectangle.
*/
computeMergedHighlightRects(textLayer: TextLayerBuilder, beginIndex: number, beginOffset: number, endIndex: number, endOffset: number): MergedRect[] {
const { textContentItems, textDivs, div } = textLayer;
const { textContentItems, textDivs } = textLayer;

const results: MergedRect[] = [];

Expand All @@ -34,7 +34,7 @@ export class HighlightGeometryLib extends PDFPlusLibSubmodule {
if (!item.str) continue;

// the minimum rectangle that contains all the chars of this text content item
const rect = this.computeHighlightRectForItem(div, item, textDiv, index, beginIndex, beginOffset, endIndex, endOffset);
const rect = this.computeHighlightRectForItem(item, textDiv, index, beginIndex, beginOffset, endIndex, endOffset);
if (!rect) continue;

if (!mergedRect) {
Expand All @@ -59,13 +59,13 @@ export class HighlightGeometryLib extends PDFPlusLibSubmodule {
return results;
}

computeHighlightRectForItem(textLayerDiv: HTMLElement, item: TextContentItem, textDiv: HTMLElement, index: number, beginIndex: number, beginOffset: number, endIndex: number, endOffset: number): Rect | null {
computeHighlightRectForItem(item: TextContentItem, textDiv: HTMLElement, index: number, beginIndex: number, beginOffset: number, endIndex: number, endOffset: number): Rect | null {
// If the item has the `chars` property filled, use it to get the bounding rectangle of each character in the item.
if (item.chars && item.chars.length >= item.str.length) {
return this.computeHighlightRectForItemFromChars(item as PropRequired<TextContentItem, 'chars'>, index, beginIndex, beginOffset, endIndex, endOffset);
}
// Otherwise, use the text layer divs to get the bounding rectangle of the text selection.
return this.computeHighlightRectForItemFromTextLayer(textLayerDiv, item, textDiv, index, beginIndex, beginOffset, endIndex, endOffset);
return this.computeHighlightRectForItemFromTextLayer(item, textDiv, index, beginIndex, beginOffset, endIndex, endOffset);
}

computeHighlightRectForItemFromChars(item: PropRequired<TextContentItem, 'chars'>, index: number, beginIndex: number, beginOffset: number, endIndex: number, endOffset: number): Rect | null {
Expand All @@ -92,40 +92,40 @@ export class HighlightGeometryLib extends PDFPlusLibSubmodule {
];
}

// Inspired by PDFViewerChild.prototype.hightlightText from Obsidian's app.js
computeHighlightRectForItemFromTextLayer(textLayerDiv: HTMLElement, item: TextContentItem, textDiv: HTMLElement, index: number, beginIndex: number, beginOffset: number, endIndex: number, endOffset: number): Rect | null {
const offsetFrom = index === beginIndex ? beginOffset : 0;
// `endOffset` is computed from the `endOffset` property (https://developer.mozilla.org/en-US/docs/Web/API/Range/endOffset)
// of the `Range` contained in the selection, which is the number of characters from the start of the `Range` to its end.
// Therefore, `endOffset` is 1 greater than the index of the last character in the selection.
const offsetTo = index === endIndex ? endOffset : undefined;

computeHighlightRectForItemFromTextLayer(item: TextContentItem, textDiv: HTMLElement, index: number, beginIndex: number, beginOffset: number, endIndex: number, endOffset: number): Rect | null {
// the bounding box of the whole text content item
const x1 = item.transform[4];
const y1 = item.transform[5];
const x2 = item.transform[4] + item.width;
const y2 = item.transform[5] + item.height;

const textDivCopied = textDiv.cloneNode() as HTMLElement;
textLayerDiv.appendChild(textDivCopied);

const textBefore = item.str.substring(0, offsetFrom);
textDivCopied.appendText(textBefore);
const range = textDiv.doc.createRange();

const text = item.str.substring(offsetFrom, offsetTo);
const boundingEl = textDivCopied.createSpan();
boundingEl.appendText(text);
if (index === beginIndex) {
const posFrom = getNodeAndOffsetOfTextPos(textDiv, beginOffset);
if (posFrom) {
range.setStart(posFrom.node, posFrom.offset);
} else {
range.setStartBefore(textDiv);
}
} else {
range.setStartBefore(textDiv);
}

if (offsetTo !== undefined) {
const textAfter = item.str.substring(offsetTo);
textDivCopied.appendText(textAfter);
if (index === endIndex) {
const posTo = getNodeAndOffsetOfTextPos(textDiv, endOffset);
if (posTo) {
range.setEnd(posTo.node, posTo.offset);
} else {
range.setEndAfter(textDiv);
}
} else {
range.setEndAfter(textDiv);
}

const rect = boundingEl.getBoundingClientRect();
const rect = range.getBoundingClientRect();
const parentRect = textDiv.getBoundingClientRect();

textDivCopied.remove();

return [
x1 + (rect.left - parentRect.left) / parentRect.width * item.width,
y1 + (rect.bottom - parentRect.bottom) / parentRect.height * item.height,
Expand Down
12 changes: 12 additions & 0 deletions src/toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ export class PDFPlusToolbar extends PDFPlusComponent {
});
});
})
.addItem((item) => {
item.setSection('scroll')
.setIcon('lucide-sticky-note')
.setTitle('In-page scroll')
.setChecked(scrollMode === ScrollMode.PAGE)
.onClick(() => {
eventBus.dispatch('switchscrollmode', {
source: toolbar,
mode: ScrollMode.PAGE
});
});
})
.addItem((item) => {
item.setSection('scroll')
.setIcon('lucide-wrap-text')
Expand Down

0 comments on commit 3d20afb

Please sign in to comment.