@anaralabs/lector 3.5.1 → 3.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -39,7 +39,7 @@ declare interface Annotation {
39
39
  export { Annotation }
40
40
  export { Annotation as Annotation_alias_1 }
41
41
 
42
- export declare const AnnotationHighlightLayer: ({ className, style, renderTooltipContent, focusedAnnotationId, onAnnotationClick, }: AnnotationHighlightLayerProps) => JSX.Element;
42
+ export declare const AnnotationHighlightLayer: ({ className, style, renderTooltipContent, tooltipClassName, focusedAnnotationId, onAnnotationClick, }: AnnotationHighlightLayerProps) => JSX.Element;
43
43
 
44
44
  declare interface AnnotationHighlightLayerProps {
45
45
  className?: string;
@@ -50,6 +50,7 @@ declare interface AnnotationHighlightLayerProps {
50
50
  }) => React.ReactNode;
51
51
  focusedAnnotationId?: string;
52
52
  onAnnotationClick?: (annotation: Annotation) => void;
53
+ tooltipClassName?: string;
53
54
  }
54
55
 
55
56
  /**
@@ -89,7 +90,7 @@ declare interface AnnotationState {
89
90
  setAnnotations: (annotations: Annotation[]) => void;
90
91
  }
91
92
 
92
- export declare const AnnotationTooltip: ({ annotation, children, tooltipContent, onOpenChange, isOpen: controlledIsOpen, }: AnnotationTooltipProps) => JSX.Element;
93
+ export declare const AnnotationTooltip: ({ annotation, children, tooltipContent, onOpenChange, className, isOpen: controlledIsOpen, }: AnnotationTooltipProps) => JSX.Element;
93
94
 
94
95
  declare interface AnnotationTooltipProps {
95
96
  annotation: Annotation;
@@ -97,6 +98,7 @@ declare interface AnnotationTooltipProps {
97
98
  tooltipContent: React.ReactNode;
98
99
  onOpenChange?: (open: boolean) => void;
99
100
  isOpen?: boolean;
101
+ className?: string;
100
102
  }
101
103
 
102
104
  /**
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  'use client';
2
2
 
3
3
  import React, { createContext, forwardRef, useCallback, useContext, useState, useRef, useEffect, cloneElement, createRef, useLayoutEffect } from 'react';
4
- import { useFloating, autoUpdate, offset, shift, useDismiss, useInteractions, flip } from '@floating-ui/react';
4
+ import { createPortal } from 'react-dom';
5
+ import { useFloating, autoUpdate, offset, shift, useDismiss, useInteractions, inline, flip } from '@floating-ui/react';
5
6
  import { useVirtualizer, elementScroll, debounce } from '@tanstack/react-virtual';
6
7
  import { create, useStore, createStore } from 'zustand';
7
8
  import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
@@ -150,6 +151,7 @@ var useAnnotationTooltip = ({
150
151
  const isNewAnnotation = Date.now() - new Date(annotation.createdAt).getTime() < 1e3;
151
152
  const [isOpen, setIsOpen] = useState(isNewAnnotation);
152
153
  const viewportRef = usePdf((state) => state.viewportRef);
154
+ const scale = usePdf((state) => state.zoom);
153
155
  const {
154
156
  refs,
155
157
  floatingStyles,
@@ -163,9 +165,11 @@ var useAnnotationTooltip = ({
163
165
  },
164
166
  whileElementsMounted: autoUpdate,
165
167
  middleware: [
168
+ inline(),
166
169
  offset(10),
167
170
  flip({
168
- crossAxis: false
171
+ crossAxis: false,
172
+ fallbackAxisSideDirection: "end"
169
173
  }),
170
174
  shift({ padding: 8 })
171
175
  ]
@@ -174,31 +178,54 @@ var useAnnotationTooltip = ({
174
178
  const { getReferenceProps, getFloatingProps } = useInteractions([dismiss]);
175
179
  const updateTooltipPosition = useCallback(() => {
176
180
  if (!annotation.highlights.length) return;
177
- const lastHighlight = annotation.highlights[annotation.highlights.length - 1];
181
+ const highlightRects = annotation.highlights;
182
+ let minLeft = Infinity;
183
+ let maxRight = -Infinity;
184
+ let minTop = Infinity;
185
+ let maxBottom = -Infinity;
178
186
  refs.setReference({
179
- getBoundingClientRect: () => {
187
+ getBoundingClientRect() {
180
188
  const viewportElement = viewportRef.current;
181
- if (!viewportElement || !lastHighlight) return defaultRect;
189
+ if (!viewportElement) return defaultRect;
182
190
  const pageElement = viewportElement.querySelector(`[data-page-number="${annotation.pageNumber}"]`);
183
191
  if (!pageElement) return defaultRect;
184
192
  const pageRect = pageElement.getBoundingClientRect();
185
- const left = pageRect.left + lastHighlight.left;
186
- const top = pageRect.top + lastHighlight.top;
187
- const width = lastHighlight.width;
188
- const height = lastHighlight.height;
189
- return {
193
+ highlightRects.forEach((highlight) => {
194
+ const scaledLeft = highlight.left * scale;
195
+ const scaledWidth = highlight.width * scale;
196
+ const scaledTop = highlight.top * scale;
197
+ const scaledHeight = highlight.height * scale;
198
+ const left = pageRect.left + scaledLeft;
199
+ const right = left + scaledWidth;
200
+ const top = pageRect.top + scaledTop;
201
+ const bottom = top + scaledHeight;
202
+ minLeft = Math.min(minLeft, left);
203
+ maxRight = Math.max(maxRight, right);
204
+ minTop = Math.min(minTop, top);
205
+ maxBottom = Math.max(maxBottom, bottom);
206
+ });
207
+ const width = maxRight - minLeft;
208
+ const height = maxBottom - minTop;
209
+ const centerX = minLeft + width / 2;
210
+ const centerY = minTop + height / 2;
211
+ const rect = {
190
212
  width,
191
213
  height,
192
- x: left,
193
- y: top,
194
- top,
195
- right: left + width,
196
- bottom: top + height,
197
- left
214
+ x: centerX - width / 2,
215
+ y: centerY - height / 2,
216
+ top: centerY - height / 2,
217
+ right: centerX + width / 2,
218
+ bottom: centerY + height / 2,
219
+ left: centerX - width / 2
198
220
  };
199
- }
221
+ return rect;
222
+ },
223
+ getClientRects() {
224
+ return [this.getBoundingClientRect()];
225
+ },
226
+ contextElement: viewportRef.current || void 0
200
227
  });
201
- }, [annotation.highlights, annotation.pageNumber, refs, viewportRef]);
228
+ }, [annotation.highlights, annotation.pageNumber, refs, viewportRef, scale]);
202
229
  useEffect(() => {
203
230
  const viewport = viewportRef.current;
204
231
  updateTooltipPosition();
@@ -235,8 +262,10 @@ var AnnotationTooltip = ({
235
262
  children,
236
263
  tooltipContent,
237
264
  onOpenChange,
265
+ className,
238
266
  isOpen: controlledIsOpen
239
267
  }) => {
268
+ const viewportRef = usePdf((state) => state.viewportRef);
240
269
  const {
241
270
  isOpen: uncontrolledIsOpen,
242
271
  setIsOpen,
@@ -264,19 +293,22 @@ var AnnotationTooltip = ({
264
293
  children
265
294
  }
266
295
  ),
267
- isOpen && /* @__PURE__ */ jsx(
268
- "div",
269
- {
270
- ref: refs.setFloating,
271
- className: "bg-white shadow-lg rounded-lg p-3 z-50 min-w-[200px]",
272
- style: {
273
- ...floatingStyles,
274
- position: "fixed",
275
- zIndex: 9999
276
- },
277
- ...getFloatingProps(),
278
- children: tooltipContent
279
- }
296
+ isOpen && viewportRef.current && createPortal(
297
+ /* @__PURE__ */ jsx(
298
+ "div",
299
+ {
300
+ ref: refs.setFloating,
301
+ className,
302
+ style: {
303
+ ...floatingStyles,
304
+ position: "absolute",
305
+ pointerEvents: "auto"
306
+ },
307
+ ...getFloatingProps(),
308
+ children: tooltipContent
309
+ }
310
+ ),
311
+ viewportRef.current
280
312
  )
281
313
  ] });
282
314
  };
@@ -315,6 +347,7 @@ var AnnotationHighlightLayer = ({
315
347
  className,
316
348
  style,
317
349
  renderTooltipContent,
350
+ tooltipClassName,
318
351
  focusedAnnotationId,
319
352
  onAnnotationClick
320
353
  }) => {
@@ -327,6 +360,7 @@ var AnnotationHighlightLayer = ({
327
360
  AnnotationTooltip,
328
361
  {
329
362
  annotation,
363
+ className: tooltipClassName,
330
364
  isOpen: focusedAnnotationId === annotation.id,
331
365
  onOpenChange: (open) => {
332
366
  if (open && onAnnotationClick) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anaralabs/lector",
3
- "version": "3.5.1",
3
+ "version": "3.5.2",
4
4
  "description": "Headless PDF viewer for React",
5
5
  "author": "andrewdr (https://github.com/andrewdoro)",
6
6
  "license": "MIT",