Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(pdf): anchor the selection menu to the selected text (#6893)

gbirman6985c226c29f
diff --git a/apps/web/src/features/block-pdf/PdfViewer/InternalPDFViewer.ts b/apps/web/src/features/block-pdf/PdfViewer/InternalPDFViewer.ts
index 3a3bdd71cf..657f63c5da 100644
--- a/apps/web/src/features/block-pdf/PdfViewer/InternalPDFViewer.ts
+++ b/apps/web/src/features/block-pdf/PdfViewer/InternalPDFViewer.ts
@@ -13,6 +13,7 @@ import {
TextLayerBuilder,
} from 'pdfjs-dist/web/pdf_viewer';
import { PageModel } from '../model/Page';
+import { selectedTextBounds } from '../util/pdfjsUtils';
import { PDF_TO_CSS_UNITS } from '../util/pixelsPerInch';
import { destHrefToDest } from './DestArray';
import {
@@ -1478,15 +1479,12 @@ export class InternalPDFViewer {
location: Rect;
} | null {
const range = selection.getRangeAt(0);
- // focus node will be the end span
- let lastTextNode = selection.focusNode?.parentElement;
- if (lastTextNode?.tagName !== 'SPAN') {
- // fixes triple click line selection
- lastTextNode = range.startContainer.parentElement;
- }
- const rect = lastTextNode
- ? lastTextNode.getBoundingClientRect()
- : range.getBoundingClientRect();
+ const textLayer = this._viewer
+ .getPageView(pageIndex)
+ ?.div.querySelector<HTMLElement>('.textLayer');
+ const rect =
+ (textLayer && selectedTextBounds(range, textLayer)) ??
+ range.getBoundingClientRect();
const pdfCoords = this.windowToPagePercentCoords(pageIndex, rect);
return this.setSelectionOverlay(pageIndex, pdfCoords, false);
}
diff --git a/apps/web/src/features/block-pdf/component/PDFPopup.tsx b/apps/web/src/features/block-pdf/component/PDFPopup.tsx
index 8e721f3482..c69b048f73 100644
--- a/apps/web/src/features/block-pdf/component/PDFPopup.tsx
+++ b/apps/web/src/features/block-pdf/component/PDFPopup.tsx
@@ -156,6 +156,7 @@ export function PDFPopup(props: PDFPopupProps) {
return (
<GeneralizedPopup
+ class="z-highlight-menu"
anchor={{
ref: props.anchorRef,
blockId: `${blockId}`,
diff --git a/apps/web/src/features/block-pdf/util/pdfjsUtils.ts b/apps/web/src/features/block-pdf/util/pdfjsUtils.ts
index caa31f19b7..874d419ea2 100644
--- a/apps/web/src/features/block-pdf/util/pdfjsUtils.ts
+++ b/apps/web/src/features/block-pdf/util/pdfjsUtils.ts
@@ -192,6 +192,56 @@ function textNodeRects(inputRange: Range) {
return result;
}
+function textNodesOf(root: Node) {
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
+ const nodes: Text[] = [];
+ while (walker.nextNode()) nodes.push(walker.currentNode as Text);
+ return nodes;
+}
+
+/** Narrows `range` to the part that falls inside `textLayer`, or null when they do not overlap. */
+function clipRangeToTextLayer(range: Range, textLayer: HTMLElement) {
+ const nodes = textNodesOf(textLayer).filter((node) =>
+ range.intersectsNode(node)
+ );
+ const first = nodes.at(0);
+ const last = nodes.at(-1);
+ if (!first || !last) return null;
+
+ const clipped = range.cloneRange();
+ if (!textLayer.contains(range.startContainer)) clipped.setStart(first, 0);
+ if (!textLayer.contains(range.endContainer))
+ clipped.setEnd(last, last.length);
+ return clipped;
+}
+
+/**
+ * Bounding rect of the selected glyphs on the page that owns `textLayer`. Text
+ * layer spans are padded out to the page edges to widen their selection hit
+ * area, so the span's own rect covers far more than the words it holds.
+ */
+export function selectedTextBounds(
+ range: Range,
+ textLayer: HTMLElement
+): DOMRect | null {
+ const pageRange = clipRangeToTextLayer(range, textLayer);
+ if (!pageRange) return null;
+
+ const textLayerRect = textLayer.getBoundingClientRect();
+ const rects = sortAndFilterDOMRects(
+ textNodeRects(pageRange).filter(
+ (rect) => !isPaddingBounds(textLayerRect, rect)
+ )
+ );
+ if (rects.length === 0) return null;
+
+ const left = Math.min(...rects.map((rect) => rect.left));
+ const top = Math.min(...rects.map((rect) => rect.top));
+ const right = Math.max(...rects.map((rect) => rect.right));
+ const bottom = Math.max(...rects.map((rect) => rect.bottom));
+ return new DOMRect(left, top, right - left, bottom - top);
+}
+
function rangeToHighlightByPageIndex(
range: Range,
text: string,
diff --git a/apps/web/src/lib/core/component/GeneralizedPopup/Popup.tsx b/apps/web/src/lib/core/component/GeneralizedPopup/Popup.tsx
index 90f006c2d4..986c770c86 100644
--- a/apps/web/src/lib/core/component/GeneralizedPopup/Popup.tsx
+++ b/apps/web/src/lib/core/component/GeneralizedPopup/Popup.tsx
@@ -29,7 +29,7 @@ export function GeneralizedPopup(props: GeneralizedPopupProps) {
<div
ref={props.ref}
id="generalized-popup"
- class="border border-edge bg-surface shadow-xl rounded-lg z-highlight-menu inline-flex items-start flex-col p-1"
+ class="border border-edge bg-surface shadow-xl rounded-lg inline-flex items-start flex-col p-1"
>
{props.children}
</div>