@anaralabs/lector 3.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Anara
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,82 @@
1
+ <p align="center">
2
+ <!-- <h1 align="center"><img src="https://www.anara.ai/icons/logo.png" width="196" /></h1> -->
3
+ <p align="center">
4
+ <i>Simple primitives to compose powerful PDF viewing experiences.<br>powered by <code><a href="https://mozilla.github.io/pdf.js/">PDF.js</a></code> and <code><a href="https://reactjs.org/">React</a></code></i>
5
+ </p>
6
+ </p>
7
+
8
+ # `lector`
9
+
10
+ A composable, headless PDF viewer toolkit for React applications, powered by `PDF.js`. Build feature-rich PDF viewing experiences with full control over the UI and functionality.
11
+
12
+ [![npm version](https://badge.fury.io/js/@anaralabs%2Flector.svg)](https://www.npmjs.com/package/@anaralabs/lector)
13
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @anaralabs/lector pdfjs-dist
19
+
20
+ # or with yarn
21
+ yarn add @anaralabs/lector pdfjs-dist
22
+
23
+ # or with pnpm
24
+ pnpm add @anaralabs/lector pdfjs-dist
25
+ ```
26
+
27
+ ## Basic Usage
28
+
29
+ Here's a simple example of how to create a basic PDF viewer:
30
+
31
+ ```tsx
32
+ import { CanvasLayer, Page, Pages, Root, TextLayer } from "@anaralabs/lector";
33
+ import "pdfjs-dist/web/pdf_viewer.css";
34
+
35
+ export default function PDFViewer() {
36
+ return (
37
+ <Root
38
+ source="/sample.pdf"
39
+ className="w-full h-[500px] border overflow-hidden rounded-lg"
40
+ loader={<div className="p-4">Loading...</div>}
41
+ >
42
+ <Pages className="p-4">
43
+ <Page>
44
+ <CanvasLayer />
45
+ <TextLayer />
46
+ </Page>
47
+ </Pages>
48
+ </Root>
49
+ );
50
+ }
51
+ ```
52
+
53
+ ## Features
54
+
55
+ - 📱 Responsive and mobile-friendly
56
+ - 🎨 Fully customizable UI components
57
+ - 🔍 Text selection and search functionality
58
+ - 📑 Page thumbnails and outline navigation
59
+ - 🌗 First-class dark mode support
60
+ - 🖱️ Pan and zoom controls
61
+ - 📝 Form filling support
62
+ - 🔗 Internal and external link handling
63
+
64
+ ## Contributing
65
+
66
+ We welcome contributions! Key areas we're focusing on:
67
+
68
+ 1. Performance optimizations
69
+ 2. Accessibility improvements
70
+ 3. Mobile/touch interactions
71
+ 4. Documentation and examples
72
+
73
+ ## Thanks
74
+
75
+ Special thanks to these open-source projects that provided inspiration:
76
+
77
+ - [react-pdf-headless](https://github.com/jkgenser/react-pdf-headless)
78
+ - [pdfreader](https://github.com/OnedocLabs/pdfreader)
79
+
80
+ ## License
81
+
82
+ MIT © [Unriddle AI]()
@@ -0,0 +1,303 @@
1
+ import { ComponentPropsWithoutRef } from 'react';
2
+ import { Context } from 'react';
3
+ import type { DocumentInitParameters } from 'pdfjs-dist/types/src/display/api';
4
+ import { ForwardRefExoticComponent } from 'react';
5
+ import { FunctionComponent } from 'react';
6
+ import { HTMLProps } from 'react';
7
+ import type { IPDFLinkService } from 'pdfjs-dist/types/web/interfaces';
8
+ import { JSX } from 'react/jsx-runtime';
9
+ import type { JSX as JSX_2 } from 'react';
10
+ import type { PageViewport } from 'pdfjs-dist';
11
+ import { PDFDocumentProxy } from 'pdfjs-dist';
12
+ import type { PDFPageProxy } from 'pdfjs-dist';
13
+ import { default as React_2 } from 'react';
14
+ import { ReactElement } from 'react';
15
+ import { ReactNode } from 'react';
16
+ import { RefAttributes } from 'react';
17
+ import { StoreApi } from 'zustand';
18
+ import type { TypedArray } from 'pdfjs-dist/types/src/display/api';
19
+ import { Virtualizer } from '@tanstack/react-virtual';
20
+
21
+ export declare const AnnotationLayer: ({ renderForms, className, style, ...props }: AnnotationLayerParams & HTMLProps<HTMLDivElement>) => JSX.Element;
22
+
23
+ declare interface AnnotationLayerParams {
24
+ /**
25
+ * Whether to render forms.
26
+ */
27
+ renderForms?: boolean;
28
+ }
29
+
30
+ export declare function calculateHighlightRects(pageProxy: PDFPageProxy, textMatch: TextPosition): Promise<HighlightRect[]>;
31
+
32
+ export declare const cancellable: <T extends Promise<unknown>>(promise: T) => {
33
+ promise: T;
34
+ cancel: () => void;
35
+ };
36
+
37
+ export declare const CanvasLayer: ({ style, ...props }: HTMLProps<HTMLCanvasElement>) => JSX.Element;
38
+
39
+ export declare const clamp: (value: number, min: number, max: number) => number;
40
+
41
+ export declare const CurrentPage: ({ ...props }: HTMLProps<HTMLInputElement>) => JSX.Element;
42
+
43
+ export declare const CurrentZoom: ({ ...props }: HTMLProps<HTMLInputElement>) => JSX.Element;
44
+
45
+ export declare const CustomLayer: ({ children, }: {
46
+ children: (pageNumber: number) => JSX_2.Element;
47
+ }) => JSX_2.Element;
48
+
49
+ export declare const firstMemo: <T>(first: boolean, memo: unknown, initializer: () => T) => T;
50
+
51
+ export declare const getFitWidthZoom: (containerWidth: number, viewports: PageViewport[], zoomOptions: {
52
+ minZoom: number;
53
+ maxZoom: number;
54
+ }) => number;
55
+
56
+ export declare const HighlightLayer: ForwardRefExoticComponent<HighlightLayerProps & RefAttributes<HTMLDivElement>>;
57
+
58
+ declare interface HighlightLayerProps extends ComponentPropsWithoutRef<"div"> {
59
+ asChild?: boolean;
60
+ }
61
+
62
+ declare type HighlightRect = {
63
+ pageNumber: number;
64
+ top: number;
65
+ left: number;
66
+ height: number;
67
+ width: number;
68
+ type?: "pixels" | "percent";
69
+ };
70
+ export { HighlightRect }
71
+ export { HighlightRect as HighlightRect_alias_1 }
72
+
73
+ export declare interface InitialPDFState {
74
+ pdfDocumentProxy: PDFDocumentProxy;
75
+ pageProxies: PDFPageProxy[];
76
+ viewports: Array<PageViewport>;
77
+ zoom: number;
78
+ isZoomFitWidth?: boolean;
79
+ zoomOptions?: ZoomOptions;
80
+ }
81
+
82
+ export declare class LinkService implements IPDFLinkService {
83
+ _pdfDocumentProxy?: PDFDocumentProxy;
84
+ externalLinkEnabled: boolean;
85
+ isInPresentationMode: boolean;
86
+ get pdfDocumentProxy(): PDFDocumentProxy;
87
+ constructor();
88
+ get pagesCount(): number;
89
+ getDestinationHash(dest: unknown[]): string;
90
+ getAnchorUrl(): string;
91
+ addLinkAttributes(link: HTMLAnchorElement, url: string, newWindow?: boolean | undefined): void;
92
+ goToDestination(dest: string | unknown[] | Promise<unknown[]>): Promise<void>;
93
+ }
94
+
95
+ export declare const NextPage: () => void;
96
+
97
+ export declare const Outline: ({ children, ...props }: HTMLProps<HTMLUListElement> & {
98
+ children: ReactElement<typeof OutlineItem>;
99
+ }) => JSX.Element;
100
+
101
+ export declare const OutlineChildItems: ({ ...props }: HTMLProps<HTMLUListElement> & {
102
+ children?: ReactElement<typeof OutlineItem>[];
103
+ }) => JSX.Element;
104
+
105
+ export declare const OutlineItem: FunctionComponent<OutlineItemProps>;
106
+
107
+ declare interface OutlineItemProps extends HTMLProps<HTMLDivElement> {
108
+ level?: number;
109
+ item?: OutlineItemType;
110
+ children?: ReactElement<typeof OutlineChildItems>;
111
+ outlineItem?: ReactElement<typeof OutlineItem>;
112
+ }
113
+
114
+ declare type OutlineItemType = NonNullable<ReturnType<typeof usePDFOutline>>[number];
115
+
116
+ export declare const Page: ({ children, pageNumber, style, ...props }: HTMLProps<HTMLDivElement> & {
117
+ children: ReactNode;
118
+ pageNumber?: number;
119
+ }) => JSX.Element;
120
+
121
+ export declare const Pages: ({ children, gap, virtualizerOptions, initialOffset, onOffsetChange, ...props }: HTMLProps<HTMLDivElement> & {
122
+ virtualizerOptions?: {
123
+ overscan?: number;
124
+ };
125
+ gap?: number;
126
+ children: ReactElement;
127
+ initialOffset?: number;
128
+ onOffsetChange?: (offset: number) => void;
129
+ }) => JSX.Element;
130
+
131
+ declare interface PDFState {
132
+ pdfDocumentProxy: PDFDocumentProxy;
133
+ zoom: number;
134
+ updateZoom: (zoom: number | ((prevZoom: number) => number), isZoomFitWidth?: boolean) => void;
135
+ isZoomFitWidth: boolean;
136
+ zoomFitWidth: () => void;
137
+ isPinching: boolean;
138
+ setIsPinching: (isPinching: boolean) => void;
139
+ currentPage: number;
140
+ setCurrentPage: (pageNumber: number) => void;
141
+ viewports: Array<PageViewport>;
142
+ viewportRef: React.MutableRefObject<HTMLDivElement | null>;
143
+ pageProxies: PDFPageProxy[];
144
+ textContent: TextContent[];
145
+ setTextContent: (textContents: TextContent[]) => void;
146
+ zoomOptions: Required<ZoomOptions>;
147
+ virtualizer: PDFVirtualizer | null;
148
+ setVirtualizer: (virtualizer: PDFVirtualizer) => void;
149
+ highlights: HighlightRect[];
150
+ setHighlight: (higlights: HighlightRect[]) => void;
151
+ getPdfPageProxy: (pageNumber: number) => PDFPageProxy;
152
+ customSelectionRects: HighlightRect[];
153
+ setCustomSelectionRects: (rects: HighlightRect[]) => void;
154
+ }
155
+
156
+ export declare const PDFStore: {
157
+ useContext: () => StoreApi<PDFState>;
158
+ Context: Context<StoreApi<PDFState>>;
159
+ Provider: (props: {
160
+ children?: React.ReactNode;
161
+ initialValue: InitialPDFState;
162
+ }) => JSX.Element;
163
+ };
164
+
165
+ export declare type PDFVirtualizer = Virtualizer<any, any>;
166
+
167
+ export declare const PreviousPage: () => void;
168
+
169
+ export declare const Root: ForwardRefExoticComponent<Omit<HTMLProps<HTMLDivElement> & usePDFDocumentParams & {
170
+ loader?: ReactNode;
171
+ }, "ref"> & RefAttributes<unknown>>;
172
+
173
+ export declare const Search: ({ children, loading }: SearchProps) => ReactNode;
174
+
175
+ declare interface SearchOptions {
176
+ threshold?: number;
177
+ limit?: number;
178
+ textSize?: number;
179
+ }
180
+
181
+ declare interface SearchProps {
182
+ children: React.ReactNode;
183
+ loading?: React.ReactNode;
184
+ }
185
+
186
+ export declare interface SearchResult {
187
+ pageNumber: number;
188
+ text: string;
189
+ score: number;
190
+ matchIndex: number;
191
+ isExactMatch: boolean;
192
+ }
193
+
194
+ export declare interface SearchResults {
195
+ exactMatches: SearchResult[];
196
+ fuzzyMatches: SearchResult[];
197
+ hasMoreResults: boolean;
198
+ }
199
+
200
+ export declare const SelectionTooltip: ({ children }: SelectionTooltipProps) => JSX.Element;
201
+
202
+ declare interface SelectionTooltipProps {
203
+ children: React_2.ReactNode;
204
+ }
205
+
206
+ export declare type Source = string | URL | TypedArray | ArrayBuffer | DocumentInitParameters;
207
+
208
+ declare type TextContent = {
209
+ pageNumber: number;
210
+ text: string;
211
+ };
212
+
213
+ export declare const TextLayer: ({ className, style, ...props }: HTMLProps<HTMLDivElement>) => JSX.Element;
214
+
215
+ declare interface TextPosition {
216
+ pageNumber: number;
217
+ text: string;
218
+ matchIndex: number;
219
+ }
220
+
221
+ export declare const Thumbnail: ({ pageNumber, ...props }: HTMLProps<HTMLCanvasElement> & {
222
+ pageNumber?: number;
223
+ }) => JSX.Element;
224
+
225
+ export declare const Thumbnails: ({ children, ...props }: HTMLProps<HTMLDivElement> & {
226
+ children: ReactElement<typeof Thumbnail>;
227
+ }) => JSX.Element;
228
+
229
+ export declare const TotalPages: ({ ...props }: HTMLProps<HTMLDivElement>) => JSX.Element;
230
+
231
+ declare const usePdf: <T>(selector: (state: PDFState) => T) => T;
232
+ export { usePdf }
233
+ export { usePdf as usePdf_alias_1 }
234
+
235
+ export declare const usePDFDocumentContext: ({ onDocumentLoad, source, initialRotation, isZoomFitWidth, zoom, zoomOptions, }: usePDFDocumentParams) => {
236
+ initialState: InitialPDFState | null | undefined;
237
+ };
238
+
239
+ export declare interface usePDFDocumentParams {
240
+ /**
241
+ * The URL of the PDF file to load.
242
+ */
243
+ source: Source;
244
+ onDocumentLoad?: ({ proxy, source, }: {
245
+ proxy: PDFDocumentProxy;
246
+ source: Source;
247
+ }) => void;
248
+ initialRotation?: number;
249
+ isZoomFitWidth?: boolean;
250
+ zoom?: number;
251
+ zoomOptions?: ZoomOptions;
252
+ }
253
+
254
+ export declare const usePdfJump: () => {
255
+ jumpToPage: (pageIndex: number, options?: {
256
+ align?: "start" | "center" | "end" | "auto";
257
+ behavior?: "auto" | "smooth";
258
+ }) => void;
259
+ jumpToOffset: (offset: number) => void;
260
+ jumpToHighlightRects: (rects: HighlightRect[], type: "pixels" | "percent", align?: "start" | "center", additionalOffset?: number) => void;
261
+ };
262
+
263
+ declare const usePDFOutline: () => {
264
+ title: string;
265
+ bold: boolean;
266
+ italic: boolean;
267
+ color: Uint8ClampedArray;
268
+ dest: string | Array<any> | null;
269
+ url: string | null;
270
+ unsafeUrl: string | undefined;
271
+ newWindow: boolean | undefined;
272
+ count: number | undefined;
273
+ items: Array<any>;
274
+ }[] | undefined;
275
+
276
+ export declare const useSearch: () => {
277
+ textContent: {
278
+ pageNumber: number;
279
+ text: string;
280
+ }[];
281
+ keywords: string[];
282
+ searchResults: SearchResults;
283
+ search: (searchText: string, options?: SearchOptions) => SearchResults;
284
+ };
285
+
286
+ export declare const useSelectionDimensions: () => {
287
+ getDimension: () => {
288
+ highlights: HighlightRect[];
289
+ text: string;
290
+ isCollapsed: boolean;
291
+ } | undefined;
292
+ };
293
+
294
+ export declare const ZoomIn: ({ ...props }: HTMLProps<HTMLButtonElement>) => JSX.Element;
295
+
296
+ export declare interface ZoomOptions {
297
+ minZoom?: number;
298
+ maxZoom?: number;
299
+ }
300
+
301
+ export declare const ZoomOut: ({ ...props }: HTMLProps<HTMLButtonElement>) => JSX.Element;
302
+
303
+ export { }
@@ -0,0 +1,31 @@
1
+ export { AnnotationLayer } from './_tsup-dts-rollup.js';
2
+ export { CanvasLayer } from './_tsup-dts-rollup.js';
3
+ export { CustomLayer } from './_tsup-dts-rollup.js';
4
+ export { HighlightLayer } from './_tsup-dts-rollup.js';
5
+ export { TextLayer } from './_tsup-dts-rollup.js';
6
+ export { Outline } from './_tsup-dts-rollup.js';
7
+ export { OutlineChildItems } from './_tsup-dts-rollup.js';
8
+ export { OutlineItem } from './_tsup-dts-rollup.js';
9
+ export { Page } from './_tsup-dts-rollup.js';
10
+ export { CurrentPage } from './_tsup-dts-rollup.js';
11
+ export { NextPage } from './_tsup-dts-rollup.js';
12
+ export { PreviousPage } from './_tsup-dts-rollup.js';
13
+ export { TotalPages } from './_tsup-dts-rollup.js';
14
+ export { Pages } from './_tsup-dts-rollup.js';
15
+ export { Root } from './_tsup-dts-rollup.js';
16
+ export { Search } from './_tsup-dts-rollup.js';
17
+ export { SelectionTooltip } from './_tsup-dts-rollup.js';
18
+ export { Thumbnail } from './_tsup-dts-rollup.js';
19
+ export { Thumbnails } from './_tsup-dts-rollup.js';
20
+ export { CurrentZoom } from './_tsup-dts-rollup.js';
21
+ export { ZoomIn } from './_tsup-dts-rollup.js';
22
+ export { ZoomOut } from './_tsup-dts-rollup.js';
23
+ export { usePdfJump } from './_tsup-dts-rollup.js';
24
+ export { SearchResult } from './_tsup-dts-rollup.js';
25
+ export { SearchResults } from './_tsup-dts-rollup.js';
26
+ export { useSearch } from './_tsup-dts-rollup.js';
27
+ export { calculateHighlightRects } from './_tsup-dts-rollup.js';
28
+ export { LinkService } from './_tsup-dts-rollup.js';
29
+ export { useSelectionDimensions } from './_tsup-dts-rollup.js';
30
+ export { HighlightRect } from './_tsup-dts-rollup.js';
31
+ export { usePdf } from './_tsup-dts-rollup.js';