@anaralabs/lector 3.5.5 → 3.5.7

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.
@@ -32,6 +32,7 @@ declare interface Annotation {
32
32
  pageNumber: number;
33
33
  }>;
34
34
  color: string;
35
+ borderColor: string;
35
36
  createdAt: Date;
36
37
  updatedAt: Date;
37
38
  comment?: string;
@@ -40,7 +41,7 @@ declare interface Annotation {
40
41
  export { Annotation }
41
42
  export { Annotation as Annotation_alias_1 }
42
43
 
43
- export declare const AnnotationHighlightLayer: ({ className, style, renderTooltipContent, renderHoverTooltipContent, tooltipClassName, highlightClassName, focusedAnnotationId, focusedHoverAnnotationId, onAnnotationClick, hoverTooltipClassName }: AnnotationHighlightLayerProps) => JSX.Element;
44
+ export declare const AnnotationHighlightLayer: ({ className, style, renderTooltipContent, renderHoverTooltipContent, tooltipClassName, highlightClassName, focusedAnnotationId, focusedHoverAnnotationId, onAnnotationClick, hoverTooltipClassName, tooltipBubbleSize, }: AnnotationHighlightLayerProps) => JSX.Element;
44
45
 
45
46
  declare interface AnnotationHighlightLayerProps {
46
47
  className?: string;
@@ -59,6 +60,7 @@ declare interface AnnotationHighlightLayerProps {
59
60
  tooltipClassName?: string;
60
61
  hoverTooltipClassName?: string;
61
62
  highlightClassName?: string;
63
+ tooltipBubbleSize?: number;
62
64
  }
63
65
 
64
66
  /**
@@ -98,7 +100,7 @@ declare interface AnnotationState {
98
100
  setAnnotations: (annotations: Annotation[]) => void;
99
101
  }
100
102
 
101
- export declare const AnnotationTooltip: ({ annotation, children, tooltipContent, hoverTooltipContent, onOpenChange, className, hoverClassName, isOpen: controlledIsOpen, hoverIsOpen: controlledHoverIsOpen, }: AnnotationTooltipProps) => JSX.Element;
103
+ export declare const AnnotationTooltip: ({ annotation, children, tooltipContent, hoverTooltipContent, onOpenChange, className, hoverClassName, isOpen: controlledIsOpen, tooltipBubbleSize, hoverIsOpen: controlledHoverIsOpen, }: AnnotationTooltipProps) => JSX.Element;
102
104
 
103
105
  declare interface AnnotationTooltipProps {
104
106
  annotation: Annotation;
@@ -110,6 +112,7 @@ declare interface AnnotationTooltipProps {
110
112
  className?: string;
111
113
  hoverClassName?: string;
112
114
  hoverIsOpen?: boolean;
115
+ tooltipBubbleSize: number;
113
116
  renderHoverTooltipContent?: (props: {
114
117
  annotation: Annotation;
115
118
  onClose: () => void;
@@ -381,12 +384,13 @@ declare const useAnnotations: UseBoundStore<StoreApi<AnnotationState>>;
381
384
  export { useAnnotations }
382
385
  export { useAnnotations as useAnnotations_alias_1 }
383
386
 
384
- export declare const useAnnotationTooltip: ({ annotation, onOpenChange, position, }: UseAnnotationTooltipProps) => UseAnnotationTooltipReturn;
387
+ export declare const useAnnotationTooltip: ({ annotation, onOpenChange, position, isOpen: controlledIsOpen, }: UseAnnotationTooltipProps) => UseAnnotationTooltipReturn;
385
388
 
386
389
  declare interface UseAnnotationTooltipProps {
387
390
  annotation: Annotation;
388
391
  onOpenChange?: (open: boolean) => void;
389
392
  position?: "top" | "bottom" | "left" | "right";
393
+ isOpen?: boolean;
390
394
  }
391
395
 
392
396
  declare interface UseAnnotationTooltipReturn {
@@ -428,6 +432,7 @@ export declare const usePdfJump: () => {
428
432
  }) => void;
429
433
  jumpToOffset: (offset: number) => void;
430
434
  jumpToHighlightRects: (rects: HighlightRect[], type: "pixels" | "percent", align?: "start" | "center", additionalOffset?: number) => void;
435
+ scrollToHighlightRects: (rects: HighlightRect[], type: "pixels" | "percent", align?: "start" | "center", additionalOffset?: number) => void;
431
436
  };
432
437
 
433
438
  declare const usePDFOutline: () => {
package/dist/index.js CHANGED
@@ -134,32 +134,25 @@ var PDFStore = createZustandContext(
134
134
  var usePdf = (selector) => useStore(PDFStore.useContext(), selector);
135
135
 
136
136
  // src/hooks/useAnnotationTooltip.ts
137
- var defaultRect = {
138
- width: 0,
139
- height: 0,
140
- x: 0,
141
- y: 0,
142
- top: 0,
143
- right: 0,
144
- bottom: 0,
145
- left: 0
146
- };
147
137
  var useAnnotationTooltip = ({
148
138
  annotation,
149
139
  onOpenChange,
150
- position = "top"
140
+ position = "top",
141
+ isOpen: controlledIsOpen
151
142
  }) => {
152
143
  const isNewAnnotation = Date.now() - new Date(annotation.createdAt).getTime() < 1e3;
153
- const [isOpen, setIsOpen] = useState(isNewAnnotation);
144
+ const [isPositionCalculated, setIsPositionCalculated] = useState(false);
145
+ const [isOpen, setIsOpen] = useState(false);
154
146
  const viewportRef = usePdf((state) => state.viewportRef);
155
147
  const scale = usePdf((state) => state.zoom);
148
+ const effectiveIsOpen = (isOpen && isPositionCalculated || controlledIsOpen) ?? false;
156
149
  const {
157
150
  refs,
158
151
  floatingStyles,
159
152
  context
160
153
  } = useFloating({
161
154
  placement: position,
162
- open: isOpen,
155
+ open: effectiveIsOpen,
163
156
  onOpenChange: (open) => {
164
157
  setIsOpen(open);
165
158
  onOpenChange?.(open);
@@ -178,18 +171,27 @@ var useAnnotationTooltip = ({
178
171
  const dismiss = useDismiss(context);
179
172
  const { getReferenceProps, getFloatingProps } = useInteractions([dismiss]);
180
173
  const updateTooltipPosition = useCallback(() => {
181
- if (!annotation.highlights.length) return;
174
+ if (!annotation.highlights.length) {
175
+ setIsPositionCalculated(false);
176
+ return;
177
+ }
182
178
  const highlightRects = annotation.highlights;
183
179
  let minLeft = Infinity;
184
180
  let maxRight = -Infinity;
185
181
  let minTop = Infinity;
186
182
  let maxBottom = -Infinity;
183
+ const viewportElement = viewportRef.current;
184
+ if (!viewportElement) {
185
+ setIsPositionCalculated(false);
186
+ return;
187
+ }
188
+ const pageElement = viewportElement.querySelector(`[data-page-number="${annotation.pageNumber}"]`);
189
+ if (!pageElement) {
190
+ setIsPositionCalculated(false);
191
+ return;
192
+ }
187
193
  refs.setReference({
188
194
  getBoundingClientRect() {
189
- const viewportElement = viewportRef.current;
190
- if (!viewportElement) return defaultRect;
191
- const pageElement = viewportElement.querySelector(`[data-page-number="${annotation.pageNumber}"]`);
192
- if (!pageElement) return defaultRect;
193
195
  const pageRect = pageElement.getBoundingClientRect();
194
196
  highlightRects.forEach((highlight) => {
195
197
  const scaledLeft = highlight.left * scale;
@@ -226,10 +228,17 @@ var useAnnotationTooltip = ({
226
228
  },
227
229
  contextElement: viewportRef.current || void 0
228
230
  });
229
- }, [annotation.highlights, annotation.pageNumber, refs, viewportRef, scale]);
231
+ setIsPositionCalculated(true);
232
+ if (isNewAnnotation) {
233
+ setIsOpen(true);
234
+ }
235
+ }, [annotation.highlights, annotation.pageNumber, refs, viewportRef, scale, isNewAnnotation]);
230
236
  useEffect(() => {
231
237
  const viewport = viewportRef.current;
232
- updateTooltipPosition();
238
+ setIsPositionCalculated(false);
239
+ requestAnimationFrame(() => {
240
+ updateTooltipPosition();
241
+ });
233
242
  const handleScroll = () => {
234
243
  requestAnimationFrame(updateTooltipPosition);
235
244
  };
@@ -248,9 +257,9 @@ var useAnnotationTooltip = ({
248
257
  }
249
258
  window.removeEventListener("resize", handleResize);
250
259
  };
251
- }, [updateTooltipPosition, viewportRef]);
260
+ }, [updateTooltipPosition, viewportRef, scale, controlledIsOpen]);
252
261
  return {
253
- isOpen,
262
+ isOpen: effectiveIsOpen,
254
263
  setIsOpen,
255
264
  refs,
256
265
  floatingStyles,
@@ -267,6 +276,7 @@ var AnnotationTooltip = ({
267
276
  className,
268
277
  hoverClassName,
269
278
  isOpen: controlledIsOpen,
279
+ tooltipBubbleSize,
270
280
  hoverIsOpen: controlledHoverIsOpen
271
281
  }) => {
272
282
  const viewportRef = usePdf((state) => state.viewportRef);
@@ -281,7 +291,8 @@ var AnnotationTooltip = ({
281
291
  getReferenceProps
282
292
  } = useAnnotationTooltip({
283
293
  annotation,
284
- onOpenChange
294
+ onOpenChange,
295
+ isOpen: controlledIsOpen
285
296
  });
286
297
  const {
287
298
  isOpen: uncontrolledHoverIsOpen,
@@ -292,7 +303,8 @@ var AnnotationTooltip = ({
292
303
  getReferenceProps: getHoverReferenceProps
293
304
  } = useAnnotationTooltip({
294
305
  position: "bottom",
295
- annotation
306
+ annotation,
307
+ isOpen: controlledHoverIsOpen
296
308
  });
297
309
  const isOpen = controlledIsOpen ?? uncontrolledIsOpen;
298
310
  const hoverIsOpen = controlledHoverIsOpen || uncontrolledHoverIsOpen;
@@ -331,7 +343,7 @@ var AnnotationTooltip = ({
331
343
  setHoverIsOpen(false);
332
344
  }, [setHoverIsOpen]);
333
345
  return /* @__PURE__ */ jsxs(Fragment, { children: [
334
- /* @__PURE__ */ jsx(
346
+ /* @__PURE__ */ jsxs(
335
347
  "div",
336
348
  {
337
349
  ref: (node) => {
@@ -343,7 +355,18 @@ var AnnotationTooltip = ({
343
355
  onMouseLeave: handleMouseLeave,
344
356
  ...getReferenceProps(),
345
357
  ...getHoverReferenceProps(),
346
- children
358
+ children: [
359
+ !isOpen && !hoverIsOpen && annotation.comment && /* @__PURE__ */ jsx("div", { className: "rounded-full", style: {
360
+ position: "absolute",
361
+ top: annotation.highlights[0]?.top ? annotation.highlights[0]?.top - tooltipBubbleSize / 2 : 0,
362
+ left: annotation.highlights[0]?.left ? annotation.highlights[0]?.left - tooltipBubbleSize / 2 : 0,
363
+ backgroundColor: annotation.borderColor,
364
+ height: `${tooltipBubbleSize}px`,
365
+ width: `${tooltipBubbleSize}px`,
366
+ zIndex: 50
367
+ } }),
368
+ children
369
+ ]
347
370
  }
348
371
  ),
349
372
  isOpen && viewportRef.current && createPortal(
@@ -423,7 +446,8 @@ var AnnotationHighlightLayer = ({
423
446
  focusedAnnotationId,
424
447
  focusedHoverAnnotationId,
425
448
  onAnnotationClick,
426
- hoverTooltipClassName
449
+ hoverTooltipClassName,
450
+ tooltipBubbleSize = 6
427
451
  }) => {
428
452
  const { annotations } = useAnnotations();
429
453
  const pageNumber = usePDFPageNumber();
@@ -437,6 +461,7 @@ var AnnotationHighlightLayer = ({
437
461
  className: tooltipClassName,
438
462
  hoverClassName: hoverTooltipClassName,
439
463
  isOpen: focusedAnnotationId === annotation.id,
464
+ tooltipBubbleSize,
440
465
  hoverIsOpen: focusedHoverAnnotationId === annotation.id,
441
466
  onOpenChange: (open) => {
442
467
  if (open && onAnnotationClick) {
@@ -542,9 +567,8 @@ var usePdfJump = () => {
542
567
  behavior: "smooth"
543
568
  });
544
569
  };
545
- const jumpToHighlightRects = (rects, type, align = "start", additionalOffset = 0) => {
570
+ const scrollToHighlightRects = (rects, type, align = "start", additionalOffset = 0) => {
546
571
  if (!virtualizer) return;
547
- setHighlight(rects);
548
572
  const firstPage = Math.min(...rects.map((rect) => rect.pageNumber));
549
573
  const pageOffset = virtualizer.getOffsetForIndex(firstPage - 1, "start");
550
574
  if (pageOffset === null) return;
@@ -581,7 +605,12 @@ var usePdfJump = () => {
581
605
  behavior: "smooth"
582
606
  });
583
607
  };
584
- return { jumpToPage, jumpToOffset, jumpToHighlightRects };
608
+ const jumpToHighlightRects = (rects, type, align = "start", additionalOffset = 0) => {
609
+ if (!virtualizer) return;
610
+ setHighlight(rects);
611
+ scrollToHighlightRects(rects, type, align, additionalOffset);
612
+ };
613
+ return { jumpToPage, jumpToOffset, jumpToHighlightRects, scrollToHighlightRects };
585
614
  };
586
615
  var LinkService = class {
587
616
  _pdfDocumentProxy;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anaralabs/lector",
3
- "version": "3.5.5",
3
+ "version": "3.5.7",
4
4
  "description": "Headless PDF viewer for React",
5
5
  "author": "andrewdr (https://github.com/andrewdoro)",
6
6
  "license": "MIT",