@anaralabs/lector 3.3.0 → 3.4.0

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.
package/README.md CHANGED
@@ -50,6 +50,39 @@ export default function PDFViewer() {
50
50
  }
51
51
  ```
52
52
 
53
+ ## Local Development using PNPM and Yalc
54
+
55
+ When you are using "pnpm link", you are bound to use pnpm on your consumer project when you are developing locally.
56
+ With yalc, we are decoupling the need for pnpm and now the package can be tested with any package managers. Any
57
+ changes should be automatically published to yalc on save, forcing a rebuilt and updating the consumer project.
58
+
59
+ Install yalc globally:
60
+
61
+ ```
62
+ pnpm i yalc -g
63
+ ```
64
+
65
+ From lector:
66
+
67
+ ```bash
68
+ # navigate to lector package folder and install dependencies
69
+ pnpm i
70
+ # when you first start development, make sure you publish the package locally
71
+ yalc publish
72
+ # and run the project in development mode to start a watcher that rebuilds the project and pushes the changes locally on save
73
+ pnpm dev
74
+ ```
75
+
76
+ From consumer project:
77
+ (It doesn't really matter what package manager you are using)
78
+
79
+ ```bash
80
+ # add local package to your package.json of the consumer project using yalc
81
+ yalc add @anaralabs/lector
82
+ # or if you don't want to add the yalc package in your package.json
83
+ yalc link @anaralabs/lector
84
+ ```
85
+
53
86
  ## Features
54
87
 
55
88
  - 📱 Responsive and mobile-friendly
@@ -18,15 +18,47 @@ import { StoreApi } from 'zustand';
18
18
  import type { TypedArray } from 'pdfjs-dist/types/src/display/api';
19
19
  import { Virtualizer } from '@tanstack/react-virtual';
20
20
 
21
- export declare const AnnotationLayer: ({ renderForms, className, style, ...props }: AnnotationLayerParams & HTMLProps<HTMLDivElement>) => JSX.Element;
21
+ /**
22
+ * AnnotationLayer renders PDF annotations like links, highlights, and form fields.
23
+ *
24
+ * @param renderForms - Whether to render form fields in the annotation layer.
25
+ * @param externalLinksEnabled - Whether external links should be clickable. When false, external links won't open.
26
+ * @param jumpOptions - Options for page navigation behavior when clicking internal links.
27
+ * See `usePdfJump` hook for available options.
28
+ */
29
+ export declare const AnnotationLayer: ({ renderForms, externalLinksEnabled, jumpOptions, className, style, ...props }: AnnotationLayerParams & HTMLProps<HTMLDivElement>) => JSX.Element;
22
30
 
23
31
  declare interface AnnotationLayerParams {
24
32
  /**
25
33
  * Whether to render forms.
26
34
  */
27
35
  renderForms?: boolean;
36
+ /**
37
+ * Whether external links are enabled.
38
+ * If false, external links will not open.
39
+ * @default true
40
+ */
41
+ externalLinksEnabled?: boolean;
42
+ /**
43
+ * Options to pass to the jumpToPage function when navigating.
44
+ * See usePdfJump hook for available options.
45
+ * @default { behavior: "smooth", align: "start" }
46
+ */
47
+ jumpOptions?: Parameters<ReturnType<typeof usePdfJump>['jumpToPage']>[1];
28
48
  }
29
49
 
50
+ /**
51
+ * Calculates the highlight rectangles for a given text match.
52
+ *
53
+ * @param pageProxy - The PDF page proxy object
54
+ * @param textMatch - An object containing:
55
+ * - pageNumber: The page number where the match is found
56
+ * - text: The text content containing the match (usually a larger chunk of text)
57
+ * - matchIndex: The index within the text where the match starts
58
+ * - searchText: (Optional) The exact search term to highlight. If provided, only highlights
59
+ * this exact term instead of the entire text. If not provided, highlights the full text.
60
+ * @returns An array of HighlightRect objects representing the areas to highlight
61
+ */
30
62
  export declare function calculateHighlightRects(pageProxy: PDFPageProxy, textMatch: TextPosition): Promise<HighlightRect[]>;
31
63
 
32
64
  export declare const cancellable: <T extends Promise<unknown>>(promise: T) => {
@@ -40,6 +72,28 @@ export declare const CanvasLayer: ({ style, background, ...props }: HTMLProps<HT
40
72
 
41
73
  export declare const clamp: (value: number, min: number, max: number) => number;
42
74
 
75
+ declare type CollapsibleSelection = {
76
+ highlights: HighlightRect[];
77
+ text: string;
78
+ isCollapsed: boolean;
79
+ };
80
+
81
+ declare type ColoredHighlight = {
82
+ color: string;
83
+ rectangles: HighlightRect[];
84
+ pageNumber: number;
85
+ text: string;
86
+ uuid: string;
87
+ };
88
+ export { ColoredHighlight }
89
+ export { ColoredHighlight as ColoredHighlight_alias_1 }
90
+
91
+ export declare const ColoredHighlightLayer: ({ onHighlight, }: ColoredHighlightLayerProps) => JSX.Element;
92
+
93
+ declare type ColoredHighlightLayerProps = {
94
+ onHighlight?: (highlight: ColoredHighlight) => void;
95
+ };
96
+
43
97
  export declare const CurrentPage: ({ ...props }: HTMLProps<HTMLInputElement>) => JSX.Element;
44
98
 
45
99
  export declare const CurrentZoom: ({ ...props }: HTMLProps<HTMLInputElement>) => JSX.Element;
@@ -50,11 +104,15 @@ export declare const CustomLayer: ({ children, }: {
50
104
 
51
105
  export declare const firstMemo: <T>(first: boolean, memo: unknown, initializer: () => T) => T;
52
106
 
107
+ export declare const getEndOfHighlight: (selection: ColoredHighlight) => number;
108
+
53
109
  export declare const getFitWidthZoom: (containerWidth: number, viewports: PageViewport[], zoomOptions: {
54
110
  minZoom: number;
55
111
  maxZoom: number;
56
112
  }) => number;
57
113
 
114
+ export declare const getMidHeightOfHighlightLine: (selection: ColoredHighlight) => number;
115
+
58
116
  export declare const HighlightLayer: ForwardRefExoticComponent<HighlightLayerProps & RefAttributes<HTMLDivElement>>;
59
117
 
60
118
  declare interface HighlightLayerProps extends ComponentPropsWithoutRef<"div"> {
@@ -85,13 +143,28 @@ export declare class LinkService implements IPDFLinkService {
85
143
  _pdfDocumentProxy?: PDFDocumentProxy;
86
144
  externalLinkEnabled: boolean;
87
145
  isInPresentationMode: boolean;
146
+ _currentPageNumber: number;
147
+ _pageNavigationCallback?: (pageNumber: number) => void;
88
148
  get pdfDocumentProxy(): PDFDocumentProxy;
89
149
  constructor();
90
150
  get pagesCount(): number;
151
+ get page(): number;
152
+ set page(value: number);
153
+ setDocument(pdfDocument: PDFDocumentProxy): void;
154
+ setViewer(): void;
91
155
  getDestinationHash(dest: unknown[]): string;
92
- getAnchorUrl(): string;
156
+ getAnchorUrl(hash: string): string;
93
157
  addLinkAttributes(link: HTMLAnchorElement, url: string, newWindow?: boolean | undefined): void;
94
158
  goToDestination(dest: string | unknown[] | Promise<unknown[]>): Promise<void>;
159
+ executeNamedAction(): void;
160
+ navigateTo(dest: string | unknown[] | Promise<unknown[]>): void;
161
+ get rotation(): number;
162
+ set rotation(value: number);
163
+ goToPage(pageNumber: number): void;
164
+ setHash(hash: string): void;
165
+ executeSetOCGState(): void;
166
+ registerPageNavigationCallback(callback: (pageNumber: number) => void): void;
167
+ unregisterPageNavigationCallback(): void;
95
168
  }
96
169
 
97
170
  export declare const NextPage: () => void;
@@ -153,6 +226,9 @@ declare interface PDFState {
153
226
  getPdfPageProxy: (pageNumber: number) => PDFPageProxy;
154
227
  customSelectionRects: HighlightRect[];
155
228
  setCustomSelectionRects: (rects: HighlightRect[]) => void;
229
+ coloredHighlights: ColoredHighlight[];
230
+ addColoredHighlight: (value: ColoredHighlight) => void;
231
+ deleteColoredHighlight: (uuid: string) => void;
156
232
  }
157
233
 
158
234
  export declare const PDFStore: {
@@ -191,6 +267,7 @@ export declare interface SearchResult {
191
267
  score: number;
192
268
  matchIndex: number;
193
269
  isExactMatch: boolean;
270
+ searchText?: string;
194
271
  }
195
272
 
196
273
  export declare interface SearchResults {
@@ -199,6 +276,8 @@ export declare interface SearchResults {
199
276
  hasMoreResults: boolean;
200
277
  }
201
278
 
279
+ declare type Selection_2 = Omit<CollapsibleSelection, "isCollapsed">;
280
+
202
281
  export declare const SelectionTooltip: ({ children }: SelectionTooltipProps) => JSX.Element;
203
282
 
204
283
  declare interface SelectionTooltipProps {
@@ -218,6 +297,7 @@ declare interface TextPosition {
218
297
  pageNumber: number;
219
298
  text: string;
220
299
  matchIndex: number;
300
+ searchText?: string;
221
301
  }
222
302
 
223
303
  export declare const Thumbnail: ({ pageNumber, ...props }: HTMLProps<HTMLCanvasElement> & {
@@ -286,11 +366,8 @@ export declare const useSearch: () => {
286
366
  };
287
367
 
288
368
  export declare const useSelectionDimensions: () => {
289
- getDimension: () => {
290
- highlights: HighlightRect[];
291
- text: string;
292
- isCollapsed: boolean;
293
- } | undefined;
369
+ getDimension: () => CollapsibleSelection | undefined;
370
+ getSelection: () => Selection_2;
294
371
  };
295
372
 
296
373
  export declare const ZoomIn: ({ ...props }: HTMLProps<HTMLButtonElement>) => JSX.Element;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { AnnotationLayer } from './_tsup-dts-rollup.js';
2
2
  export { CanvasLayer } from './_tsup-dts-rollup.js';
3
+ export { ColoredHighlightLayer } from './_tsup-dts-rollup.js';
3
4
  export { CustomLayer } from './_tsup-dts-rollup.js';
4
5
  export { HighlightLayer } from './_tsup-dts-rollup.js';
5
6
  export { TextLayer } from './_tsup-dts-rollup.js';
@@ -27,5 +28,6 @@ export { useSearch } from './_tsup-dts-rollup.js';
27
28
  export { calculateHighlightRects } from './_tsup-dts-rollup.js';
28
29
  export { LinkService } from './_tsup-dts-rollup.js';
29
30
  export { useSelectionDimensions } from './_tsup-dts-rollup.js';
31
+ export { ColoredHighlight } from './_tsup-dts-rollup.js';
30
32
  export { HighlightRect } from './_tsup-dts-rollup.js';
31
33
  export { usePdf } from './_tsup-dts-rollup.js';