@anaralabs/lector 3.11.1 → 3.12.1

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.
@@ -11,13 +11,12 @@ import type { PageViewport } from 'pdfjs-dist';
11
11
  import type { PDFDocumentProxy } from 'pdfjs-dist';
12
12
  import { pdfjsDist } from 'pdfjs-dist';
13
13
  import type { PDFPageProxy } from 'pdfjs-dist';
14
- import type { default as React_2 } from 'react';
14
+ import { default as React_2 } from 'react';
15
15
  import { ReactElement } from 'react';
16
16
  import { ReactNode } from 'react';
17
17
  import { RefAttributes } from 'react';
18
18
  import { StoreApi } from 'zustand';
19
19
  import type { TypedArray } from 'pdfjs-dist/types/src/display/api';
20
- import { UseBoundStore } from 'zustand';
21
20
  import { useFloating } from '@floating-ui/react';
22
21
  import { useInteractions } from '@floating-ui/react';
23
22
  import type { Virtualizer } from '@tanstack/react-virtual';
@@ -90,6 +89,15 @@ declare interface AnnotationLayerParams {
90
89
  jumpOptions?: Parameters<ReturnType<typeof usePdfJump>["jumpToPage"]>[1];
91
90
  }
92
91
 
92
+ declare const AnnotationsStoreProvider: ({ children, }: {
93
+ children?: React_2.ReactNode;
94
+ }) => React_2.FunctionComponentElement<{
95
+ children?: React_2.ReactNode;
96
+ initialValue: void;
97
+ }>;
98
+ export { AnnotationsStoreProvider }
99
+ export { AnnotationsStoreProvider as AnnotationsStoreProvider_alias_1 }
100
+
93
101
  declare interface AnnotationState {
94
102
  annotations: Annotation[];
95
103
  addAnnotation: (annotation: Annotation) => void;
@@ -420,7 +428,7 @@ export declare const Thumbnails: ({ children, ...props }: HTMLProps<HTMLDivEleme
420
428
 
421
429
  export declare const TotalPages: ({ ...props }: HTMLProps<HTMLDivElement>) => JSX.Element;
422
430
 
423
- declare const useAnnotations: UseBoundStore<StoreApi<AnnotationState>>;
431
+ declare const useAnnotations: () => AnnotationState;
424
432
  export { useAnnotations }
425
433
  export { useAnnotations as useAnnotations_alias_1 }
426
434
 
package/dist/index.d.ts CHANGED
@@ -30,6 +30,7 @@ export { SearchResults } from './_tsup-dts-rollup.js';
30
30
  export { useSearch } from './_tsup-dts-rollup.js';
31
31
  export { calculateHighlightRects } from './_tsup-dts-rollup.js';
32
32
  export { Annotation } from './_tsup-dts-rollup.js';
33
+ export { AnnotationsStoreProvider } from './_tsup-dts-rollup.js';
33
34
  export { useAnnotations } from './_tsup-dts-rollup.js';
34
35
  export { usePageRendered } from './_tsup-dts-rollup.js';
35
36
  export { LinkService } from './_tsup-dts-rollup.js';
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  import React, { createContext, memo, forwardRef, useMemo, cloneElement, useEffect, useRef, useState, useCallback, useContext, useLayoutEffect, createRef } from 'react';
4
4
  import { createPortal } from 'react-dom';
5
5
  import { useFloating, autoUpdate, offset, shift, useDismiss, useInteractions, inline, flip } from '@floating-ui/react';
6
- import { create, useStore, createStore } from 'zustand';
6
+ import { useStore, createStore } from 'zustand';
7
7
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
8
8
  import { useDebounce } from 'use-debounce';
9
9
  import { v4 } from 'uuid';
@@ -431,7 +431,7 @@ var AnnotationTooltip = ({
431
431
  )
432
432
  ] });
433
433
  };
434
- var useAnnotations = create((set) => ({
434
+ var createAnnotationStore = () => createStore((set) => ({
435
435
  annotations: [],
436
436
  addAnnotation: (annotation) => set((state) => ({
437
437
  annotations: [...state.annotations, annotation]
@@ -451,6 +451,25 @@ var useAnnotations = create((set) => ({
451
451
  })),
452
452
  setAnnotations: (annotations) => set({ annotations })
453
453
  }));
454
+ var AnnotationsStore = createZustandContext(
455
+ () => createAnnotationStore()
456
+ );
457
+ var fallbackStore = null;
458
+ var getFallbackStore = () => {
459
+ if (!fallbackStore) fallbackStore = createAnnotationStore();
460
+ return fallbackStore;
461
+ };
462
+ var AnnotationsStoreProvider = ({
463
+ children
464
+ }) => React.createElement(
465
+ AnnotationsStore.Provider,
466
+ { initialValue: void 0 },
467
+ children
468
+ );
469
+ var useAnnotations = () => {
470
+ const ctx = AnnotationsStore.useContext();
471
+ return useStore(ctx ?? getFallbackStore());
472
+ };
454
473
  var PDFPageNumberContext = createContext(0);
455
474
  var usePDFPageNumber = () => {
456
475
  return useContext(PDFPageNumberContext);
@@ -1604,6 +1623,7 @@ CanvasLayer.displayName = "CanvasLayer";
1604
1623
 
1605
1624
  // src/hooks/useSelectionDimensions.tsx
1606
1625
  var MERGE_THRESHOLD = 2;
1626
+ var LAYER_ATTRIBUTION_TOLERANCE_PX = 4;
1607
1627
  var getTextNodeClientRects = (range) => {
1608
1628
  const root = range.commonAncestorContainer;
1609
1629
  const ownerDoc = root.ownerDocument ?? document;
@@ -1665,8 +1685,9 @@ var mapSelectionRectsToLayers = (range) => {
1665
1685
  const layerForRect = (rect) => {
1666
1686
  const cx = rect.left + rect.width / 2;
1667
1687
  const cy = rect.top + rect.height / 2;
1688
+ const T = LAYER_ATTRIBUTION_TOLERANCE_PX;
1668
1689
  const geomMatch = textLayerEntries.find(
1669
- ({ rect: lr }) => cx >= lr.left - 1 && cx <= lr.right + 1 && cy >= lr.top - 1 && cy <= lr.bottom + 1
1690
+ ({ rect: lr }) => cx >= lr.left - T && cx <= lr.right + T && cy >= lr.top - T && cy <= lr.bottom + T
1670
1691
  );
1671
1692
  if (geomMatch) return geomMatch.el;
1672
1693
  const points = [
@@ -1688,8 +1709,6 @@ var mapSelectionRectsToLayers = (range) => {
1688
1709
  const layer = layerForRect(clientRect);
1689
1710
  if (!layer) continue;
1690
1711
  const layerRect = layer.getBoundingClientRect();
1691
- const fitsInLayer = clientRect.top >= layerRect.top - 1 && clientRect.bottom <= layerRect.bottom + 1 && clientRect.left >= layerRect.left - 1 && clientRect.right <= layerRect.right + 1;
1692
- if (!fitsInLayer) continue;
1693
1712
  const pageNumber = parseInt(
1694
1713
  layer.getAttribute("data-page-number") || "1",
1695
1714
  10
@@ -5962,4 +5981,4 @@ function mergeAdjacentRects(rects) {
5962
5981
  // src/hooks/usePageRendered.ts
5963
5982
  var usePageRendered = (pageNumber) => usePdf((state) => !!state.renderedPages[pageNumber]);
5964
5983
 
5965
- export { AnnotationHighlightLayer, AnnotationLayer, AnnotationTooltip, CanvasLayer, ColoredHighlightLayer, CurrentPage, CurrentZoom, CustomLayer, HighlightLayer, LinkService, NextPage, Outline, OutlineChildItems, OutlineItem, PDFLinkServiceContext, Page, Pages, PreviousPage, Root, Search, SelectionTooltip, TextLayer, Thumbnail, Thumbnails, TotalPages, ZoomIn, ZoomOut, calculateHighlightRects, useAnnotations, useCreatePDFLinkService, usePDFLinkService, usePDFPageNumber, usePageRendered, usePdf, usePdfJump, useSearch, useSelectionDimensions };
5984
+ export { AnnotationHighlightLayer, AnnotationLayer, AnnotationTooltip, AnnotationsStoreProvider, CanvasLayer, ColoredHighlightLayer, CurrentPage, CurrentZoom, CustomLayer, HighlightLayer, LinkService, NextPage, Outline, OutlineChildItems, OutlineItem, PDFLinkServiceContext, Page, Pages, PreviousPage, Root, Search, SelectionTooltip, TextLayer, Thumbnail, Thumbnails, TotalPages, ZoomIn, ZoomOut, calculateHighlightRects, useAnnotations, useCreatePDFLinkService, usePDFLinkService, usePDFPageNumber, usePageRendered, usePdf, usePdfJump, useSearch, useSelectionDimensions };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anaralabs/lector",
3
- "version": "3.11.1",
3
+ "version": "3.12.1",
4
4
  "description": "A headless PDF viewer for React",
5
5
  "author": "andrewdr (https://github.com/andrewdoro)",
6
6
  "license": "MIT",