@embedpdf/core 1.0.10 → 1.0.12

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.
Files changed (74) hide show
  1. package/dist/index.cjs +2 -1335
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.ts +1 -585
  4. package/dist/index.js +55 -84
  5. package/dist/index.js.map +1 -1
  6. package/dist/lib/base/base-plugin.d.ts +98 -0
  7. package/dist/lib/index.d.ts +14 -0
  8. package/dist/lib/registry/plugin-registry.d.ts +100 -0
  9. package/dist/lib/store/actions.d.ts +39 -0
  10. package/dist/lib/store/index.d.ts +6 -0
  11. package/dist/lib/store/initial-state.d.ts +11 -0
  12. package/dist/lib/store/plugin-store.d.ts +48 -0
  13. package/dist/lib/store/reducer.d.ts +4 -0
  14. package/dist/lib/store/selectors.d.ts +3 -0
  15. package/dist/lib/store/store.d.ts +105 -0
  16. package/dist/lib/store/types.d.ts +28 -0
  17. package/dist/lib/types/errors.d.ts +21 -0
  18. package/dist/lib/types/plugin.d.ts +49 -0
  19. package/dist/lib/utils/dependency-resolver.d.ts +6 -0
  20. package/dist/lib/utils/event-control.d.ts +23 -0
  21. package/dist/lib/utils/eventing.d.ts +19 -0
  22. package/dist/lib/utils/math.d.ts +22 -0
  23. package/dist/lib/utils/plugin-helpers.d.ts +6 -0
  24. package/dist/lib/utils/typed-object.d.ts +12 -0
  25. package/dist/preact/adapter.d.ts +4 -0
  26. package/dist/preact/index.cjs +2 -188
  27. package/dist/preact/index.cjs.map +1 -1
  28. package/dist/preact/index.d.ts +1 -71
  29. package/dist/preact/index.js +11 -27
  30. package/dist/preact/index.js.map +1 -1
  31. package/dist/react/adapter.d.ts +2 -0
  32. package/dist/react/index.cjs +2 -188
  33. package/dist/react/index.cjs.map +1 -1
  34. package/dist/react/index.d.ts +1 -70
  35. package/dist/react/index.js +11 -28
  36. package/dist/react/index.js.map +1 -1
  37. package/dist/shared-preact/components/embed-pdf.d.ts +12 -0
  38. package/dist/shared-preact/components/index.d.ts +1 -0
  39. package/dist/shared-preact/context.d.ts +7 -0
  40. package/dist/shared-preact/hooks/index.d.ts +5 -0
  41. package/dist/shared-preact/hooks/use-capability.d.ts +16 -0
  42. package/dist/shared-preact/hooks/use-core-state.d.ts +6 -0
  43. package/dist/shared-preact/hooks/use-plugin.d.ts +16 -0
  44. package/dist/shared-preact/hooks/use-registry.d.ts +6 -0
  45. package/dist/shared-preact/hooks/use-store-state.d.ts +6 -0
  46. package/dist/shared-preact/index.d.ts +3 -0
  47. package/dist/shared-react/components/embed-pdf.d.ts +12 -0
  48. package/dist/shared-react/components/index.d.ts +1 -0
  49. package/dist/shared-react/context.d.ts +7 -0
  50. package/dist/shared-react/hooks/index.d.ts +5 -0
  51. package/dist/shared-react/hooks/use-capability.d.ts +16 -0
  52. package/dist/shared-react/hooks/use-core-state.d.ts +6 -0
  53. package/dist/shared-react/hooks/use-plugin.d.ts +16 -0
  54. package/dist/shared-react/hooks/use-registry.d.ts +6 -0
  55. package/dist/shared-react/hooks/use-store-state.d.ts +6 -0
  56. package/dist/shared-react/index.d.ts +3 -0
  57. package/dist/vue/components/embed-pdf.vue.d.ts +23 -0
  58. package/dist/vue/components/index.d.ts +1 -0
  59. package/dist/vue/composables/index.d.ts +5 -0
  60. package/dist/vue/composables/use-capability.d.ts +15 -0
  61. package/dist/vue/composables/use-core-state.d.ts +2 -0
  62. package/dist/vue/composables/use-plugin.d.ts +8 -0
  63. package/dist/vue/composables/use-registry.d.ts +1 -0
  64. package/dist/vue/composables/use-store-state.d.ts +10 -0
  65. package/dist/vue/context.d.ts +8 -0
  66. package/dist/vue/index.cjs +2 -0
  67. package/dist/vue/index.cjs.map +1 -0
  68. package/dist/vue/index.d.ts +2 -0
  69. package/dist/vue/index.js +115 -0
  70. package/dist/vue/index.js.map +1 -0
  71. package/package.json +20 -12
  72. package/dist/index.d.cts +0 -585
  73. package/dist/preact/index.d.cts +0 -71
  74. package/dist/react/index.d.cts +0 -70
@@ -0,0 +1,39 @@
1
+ import { PdfDocumentObject, PdfPageObject, Rotation } from '@embedpdf/models';
2
+ export declare const LOAD_DOCUMENT = "LOAD_DOCUMENT";
3
+ export declare const SET_DOCUMENT = "SET_DOCUMENT";
4
+ export declare const SET_DOCUMENT_ERROR = "SET_DOCUMENT_ERROR";
5
+ export declare const SET_SCALE = "SET_SCALE";
6
+ export declare const SET_ROTATION = "SET_ROTATION";
7
+ export declare const SET_PAGES = "SET_PAGES";
8
+ export declare const CORE_ACTION_TYPES: readonly ["LOAD_DOCUMENT", "SET_DOCUMENT", "SET_DOCUMENT_ERROR", "SET_SCALE", "SET_ROTATION", "SET_PAGES"];
9
+ export interface LoadDocumentAction {
10
+ type: typeof LOAD_DOCUMENT;
11
+ }
12
+ export interface SetDocumentAction {
13
+ type: typeof SET_DOCUMENT;
14
+ payload: PdfDocumentObject;
15
+ }
16
+ export interface SetDocumentErrorAction {
17
+ type: typeof SET_DOCUMENT_ERROR;
18
+ payload: string;
19
+ }
20
+ export interface SetScaleAction {
21
+ type: typeof SET_SCALE;
22
+ payload: number;
23
+ }
24
+ export interface SetRotationAction {
25
+ type: typeof SET_ROTATION;
26
+ payload: Rotation;
27
+ }
28
+ export interface SetPagesAction {
29
+ type: typeof SET_PAGES;
30
+ payload: PdfPageObject[][];
31
+ }
32
+ export type DocumentAction = LoadDocumentAction | SetDocumentAction | SetDocumentErrorAction | SetScaleAction | SetRotationAction | SetPagesAction;
33
+ export type CoreAction = DocumentAction;
34
+ export declare const loadDocument: () => CoreAction;
35
+ export declare const setDocument: (document: PdfDocumentObject) => CoreAction;
36
+ export declare const setDocumentError: (error: string) => CoreAction;
37
+ export declare const setScale: (scale: number) => CoreAction;
38
+ export declare const setRotation: (rotation: Rotation) => CoreAction;
39
+ export declare const setPages: (pages: PdfPageObject[][]) => CoreAction;
@@ -0,0 +1,6 @@
1
+ export * from './store';
2
+ export * from './plugin-store';
3
+ export * from './types';
4
+ export * from './initial-state';
5
+ export * from './actions';
6
+ export * from './selectors';
@@ -0,0 +1,11 @@
1
+ import { PdfDocumentObject, PdfPageObject, Rotation } from '@embedpdf/models';
2
+ import { PluginRegistryConfig } from '../types/plugin';
3
+ export interface CoreState {
4
+ scale: number;
5
+ rotation: Rotation;
6
+ document: PdfDocumentObject | null;
7
+ pages: PdfPageObject[][];
8
+ loading: boolean;
9
+ error: string | null;
10
+ }
11
+ export declare const initialCoreState: (config?: PluginRegistryConfig) => CoreState;
@@ -0,0 +1,48 @@
1
+ import { Store } from './store';
2
+ import { Action } from './types';
3
+ /**
4
+ * A type-safe store handle for plugins, providing access to plugin-specific state and actions.
5
+ */
6
+ export declare class PluginStore<PluginState, PluginAction extends Action> {
7
+ private store;
8
+ private pluginId;
9
+ /**
10
+ * Initializes the PluginStore with the main store and plugin ID.
11
+ * @param store The main store instance.
12
+ * @param pluginId The unique identifier for the plugin.
13
+ */
14
+ constructor(store: Store<any, any>, pluginId: string);
15
+ /**
16
+ * Gets the current state of the plugin.
17
+ * @returns The plugin's state.
18
+ */
19
+ getState(): PluginState;
20
+ /**
21
+ * Dispatches an action for the plugin and returns the *new* global state.
22
+ * If you only need the plugin’s updated state, call `getState()` afterward.
23
+ * @param action The action to dispatch.
24
+ * @returns The updated global store state (after plugin reducer).
25
+ */
26
+ dispatch(action: PluginAction): PluginState;
27
+ /**
28
+ * Subscribes to state changes only for this specific plugin.
29
+ * You now receive (action, newPluginState, oldPluginState) in the callback.
30
+ *
31
+ * @param listener The callback to invoke when plugin state changes.
32
+ * @returns A function to unsubscribe the listener.
33
+ */
34
+ subscribeToState(listener: (action: PluginAction, newState: PluginState, oldState: PluginState) => void): () => void;
35
+ /**
36
+ * Subscribes to a specific action type for the plugin.
37
+ * This still uses the main store's `onAction`, so you get the *global*
38
+ * old/new store states there. If you specifically want old/new plugin state,
39
+ * use `subscribeToState` instead.
40
+ *
41
+ * @param type The action type to listen for.
42
+ * @param handler The callback to invoke when the action occurs.
43
+ * @returns A function to unsubscribe the handler.
44
+ */
45
+ onAction<T extends PluginAction['type']>(type: T, handler: (action: Extract<PluginAction, {
46
+ type: T;
47
+ }>, state: PluginState, oldState: PluginState) => void): () => void;
48
+ }
@@ -0,0 +1,4 @@
1
+ import { Reducer } from './types';
2
+ import { CoreState } from './initial-state';
3
+ import { CoreAction } from './actions';
4
+ export declare const coreReducer: Reducer<CoreState, CoreAction>;
@@ -0,0 +1,3 @@
1
+ import { CoreState } from './initial-state';
2
+ import { PdfPageObjectWithRotatedSize } from '@embedpdf/models';
3
+ export declare const getPagesWithRotatedSize: (state: CoreState) => PdfPageObjectWithRotatedSize[][];
@@ -0,0 +1,105 @@
1
+ import { Reducer, Action, StoreState, StoreListener, PluginListener } from './types';
2
+ import { PluginStore } from './plugin-store';
3
+ /**
4
+ * A generic, type-safe store class managing core and plugin states, reducers, and subscriptions.
5
+ * @template CoreState The type of the core state.
6
+ * @template CoreAction The type of actions handled by core reducers (extends Action).
7
+ */
8
+ export declare class Store<CoreState, CoreAction extends Action = Action> {
9
+ initialCoreState: CoreState;
10
+ private state;
11
+ private coreReducer;
12
+ private pluginReducers;
13
+ private listeners;
14
+ private pluginListeners;
15
+ /**
16
+ * Initializes the store with the provided core state.
17
+ * @param reducer The core reducer function
18
+ * @param initialCoreState The initial core state
19
+ */
20
+ constructor(reducer: Reducer<CoreState, CoreAction>, initialCoreState: CoreState);
21
+ /**
22
+ * Adds a reducer for a plugin-specific state.
23
+ * @param pluginId The unique identifier for the plugin.
24
+ * @param reducer The reducer function for the plugin state.
25
+ * @param initialState The initial state for the plugin.
26
+ */
27
+ addPluginReducer<PluginState>(pluginId: string, reducer: Reducer<PluginState, Action>, initialState: PluginState): void;
28
+ /**
29
+ * Dispatches an action *only* to the core reducer.
30
+ * Notifies the global store listeners with (action, newState, oldState).
31
+ *
32
+ * @param action The action to dispatch, typed as CoreAction
33
+ * @returns The updated *global* store state
34
+ */
35
+ dispatchToCore(action: CoreAction): StoreState<CoreState>;
36
+ /**
37
+ * Dispatches an action *only* to a specific plugin.
38
+ * Optionally notifies global store listeners if `notifyGlobal` is true.
39
+ * Always notifies plugin-specific listeners with (action, newPluginState, oldPluginState).
40
+ *
41
+ * @param pluginId The plugin identifier
42
+ * @param action The plugin action to dispatch
43
+ * @param notifyGlobal Whether to also notify global store listeners
44
+ * @returns The updated *global* store state
45
+ */
46
+ dispatchToPlugin<PluginAction extends Action>(pluginId: string, action: PluginAction, notifyGlobal?: boolean): any;
47
+ /**
48
+ * Dispatches an action to update the state using:
49
+ * - the core reducer (if it's a CoreAction)
50
+ * - *all* plugin reducers (regardless of action type), with no global notify for each plugin
51
+ *
52
+ * Returns the new *global* store state after all reducers have processed the action.
53
+ *
54
+ * @param action The action to dispatch (can be CoreAction or any Action).
55
+ */
56
+ dispatch(action: CoreAction | Action): StoreState<CoreState>;
57
+ /**
58
+ * Returns a shallow copy of the current state.
59
+ * @returns The current store state.
60
+ */
61
+ getState(): StoreState<CoreState>;
62
+ /**
63
+ * Subscribes a listener to *global* state changes.
64
+ * The callback signature is now (action, newState, oldState).
65
+ *
66
+ * @param listener The callback to invoke on state changes
67
+ * @returns A function to unsubscribe the listener
68
+ */
69
+ subscribe(listener: StoreListener<CoreState>): () => void;
70
+ /**
71
+ * Subscribes a listener to *plugin-specific* state changes.
72
+ * The callback signature is now (action, newPluginState, oldPluginState).
73
+ *
74
+ * @param pluginId The unique identifier for the plugin.
75
+ * @param listener The callback to invoke on plugin state changes.
76
+ * @returns A function to unsubscribe the listener.
77
+ */
78
+ subscribeToPlugin(pluginId: string, listener: PluginListener): () => void;
79
+ /**
80
+ * Subscribes to a specific action type (only from the core's action union).
81
+ * The callback signature is (action, newState, oldState).
82
+ *
83
+ * @param type The action type to listen for.
84
+ * @param handler The callback to invoke when the action occurs.
85
+ * @returns A function to unsubscribe the handler.
86
+ */
87
+ onAction<T extends CoreAction['type']>(type: T, handler: (action: Extract<CoreAction, {
88
+ type: T;
89
+ }>, state: StoreState<CoreState>, oldState: StoreState<CoreState>) => void): () => void;
90
+ /**
91
+ * Gets a PluginStore handle for a specific plugin.
92
+ * @param pluginId The unique identifier for the plugin.
93
+ * @returns A PluginStore instance for the plugin.
94
+ */
95
+ getPluginStore<PluginState, PluginAction extends Action>(pluginId: string): PluginStore<PluginState, PluginAction>;
96
+ /**
97
+ * Helper method to check if an action is a CoreAction.
98
+ * Adjust if you have a more refined way to differentiate CoreAction vs. any other Action.
99
+ */
100
+ isCoreAction(action: Action): action is CoreAction;
101
+ /**
102
+ * Destroy the store: drop every listener and plugin reducer
103
+ */
104
+ destroy(): void;
105
+ }
@@ -0,0 +1,28 @@
1
+ /** Represents an action with a type and optional payload */
2
+ export interface Action {
3
+ type: string;
4
+ payload?: any;
5
+ }
6
+ /** A reducer function that updates a specific state based on an action */
7
+ export type Reducer<State, Action> = (state: State, action: Action) => State;
8
+ /**
9
+ * Represents the overall store state, with a typed core and plugin states
10
+ */
11
+ export interface StoreState<CoreState> {
12
+ core: CoreState;
13
+ plugins: Record<string, any>;
14
+ }
15
+ /**
16
+ * The signature of a main-store listener. You now receive:
17
+ * - `action` => The action that was dispatched
18
+ * - `newState` => The store state *after* the update
19
+ * - `oldState` => The store state *before* the update
20
+ */
21
+ export type StoreListener<CoreState> = (action: Action, newState: StoreState<CoreState>, oldState: StoreState<CoreState>) => void;
22
+ /**
23
+ * The signature of a plugin-store listener. You now receive:
24
+ * - `action` => The action that was dispatched
25
+ * - `newPluginState` => The plugin state *after* the update
26
+ * - `oldPluginState` => The plugin state *before* the update
27
+ */
28
+ export type PluginListener = (action: Action, newPluginState: any, oldPluginState: any) => void;
@@ -0,0 +1,21 @@
1
+ export declare class PluginRegistrationError extends Error {
2
+ constructor(message: string);
3
+ }
4
+ export declare class PluginNotFoundError extends Error {
5
+ constructor(message: string);
6
+ }
7
+ export declare class CircularDependencyError extends Error {
8
+ constructor(message: string);
9
+ }
10
+ export declare class CapabilityNotFoundError extends Error {
11
+ constructor(message: string);
12
+ }
13
+ export declare class CapabilityConflictError extends Error {
14
+ constructor(message: string);
15
+ }
16
+ export declare class PluginInitializationError extends Error {
17
+ constructor(message: string);
18
+ }
19
+ export declare class PluginConfigurationError extends Error {
20
+ constructor(message: string);
21
+ }
@@ -0,0 +1,49 @@
1
+ import { PluginRegistry } from '../registry/plugin-registry';
2
+ import { PdfEngine, Rotation } from '@embedpdf/models';
3
+ import { Action, Reducer } from '../store/types';
4
+ import { CoreState } from '../store';
5
+ export interface IPlugin<TConfig = unknown> {
6
+ id: string;
7
+ initialize?(config: TConfig): Promise<void>;
8
+ destroy?(): Promise<void> | void;
9
+ provides?(): any;
10
+ postInitialize?(): Promise<void>;
11
+ ready?(): Promise<void>;
12
+ }
13
+ export interface BasePluginConfig {
14
+ enabled?: boolean;
15
+ }
16
+ export interface PluginRegistryConfig {
17
+ rotation?: Rotation;
18
+ scale?: number;
19
+ }
20
+ export interface PluginManifest<TConfig = unknown> {
21
+ id: string;
22
+ name: string;
23
+ version: string;
24
+ provides: string[];
25
+ requires: string[];
26
+ optional: string[];
27
+ defaultConfig: TConfig;
28
+ metadata?: {
29
+ description?: string;
30
+ author?: string;
31
+ homepage?: string;
32
+ [key: string]: unknown;
33
+ };
34
+ }
35
+ export interface PluginPackage<T extends IPlugin<TConfig>, TConfig = unknown, TState = unknown, TAction extends Action = Action> {
36
+ manifest: PluginManifest<TConfig>;
37
+ create(registry: PluginRegistry, engine: PdfEngine, config: TConfig): T;
38
+ reducer: Reducer<TState, TAction>;
39
+ initialState: TState | ((coreState: CoreState, config: TConfig) => TState);
40
+ }
41
+ export type PluginStatus = 'registered' | 'active' | 'error' | 'disabled' | 'unregistered';
42
+ export interface PluginBatchRegistration<T extends IPlugin<TConfig>, TConfig = unknown, TState = unknown, TAction extends Action = Action> {
43
+ package: PluginPackage<T, TConfig, TState, TAction>;
44
+ config?: Partial<TConfig>;
45
+ }
46
+ export interface GlobalStoreState<TPlugins extends Record<string, any> = {}> {
47
+ core: CoreState;
48
+ plugins: TPlugins;
49
+ }
@@ -0,0 +1,6 @@
1
+ export declare class DependencyResolver {
2
+ private dependencyGraph;
3
+ addNode(id: string, dependencies?: string[]): void;
4
+ private hasCircularDependencies;
5
+ resolveLoadOrder(): string[];
6
+ }
@@ -0,0 +1,23 @@
1
+ export type EventHandler<T> = (data: T) => void;
2
+ export interface BaseEventControlOptions {
3
+ wait: number;
4
+ }
5
+ export interface DebounceOptions extends BaseEventControlOptions {
6
+ mode: 'debounce';
7
+ }
8
+ export interface ThrottleOptions extends BaseEventControlOptions {
9
+ mode: 'throttle';
10
+ throttleMode?: 'leading-trailing' | 'trailing';
11
+ }
12
+ export type EventControlOptions = DebounceOptions | ThrottleOptions;
13
+ export declare class EventControl<T> {
14
+ private handler;
15
+ private options;
16
+ private timeoutId?;
17
+ private lastRun;
18
+ constructor(handler: EventHandler<T>, options: EventControlOptions);
19
+ handle: (data: T) => void;
20
+ private debounce;
21
+ private throttle;
22
+ destroy(): void;
23
+ }
@@ -0,0 +1,19 @@
1
+ import { EventControlOptions } from './event-control';
2
+ export type Listener<T = any> = (value: T) => void;
3
+ export type Unsubscribe = () => void;
4
+ export type EventListener<T> = ((listener: Listener<T>) => Unsubscribe) | ((listener: Listener<T>, options?: EventControlOptions) => Unsubscribe);
5
+ export type EventHook<T = any> = EventListener<T>;
6
+ export interface Emitter<T = any> {
7
+ emit(value?: T): void;
8
+ on(listener: Listener<T>): Unsubscribe;
9
+ off(listener: Listener<T>): void;
10
+ clear(): void;
11
+ }
12
+ export declare function createEmitter<T = any>(): Emitter<T>;
13
+ export interface BehaviorEmitter<T = any> extends Omit<Emitter<T>, 'on' | 'off'> {
14
+ readonly value?: T;
15
+ on: EventHook<T>;
16
+ off(listener: Listener<T>): void;
17
+ select<U>(selector: (v: T) => U, equality?: (a: U, b: U) => boolean): EventHook<U>;
18
+ }
19
+ export declare function createBehaviorEmitter<T = any>(initial?: T, equality?: (a: T, b: T) => boolean): BehaviorEmitter<T>;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Restrict a numeric value to the inclusive range [min, max].
3
+ *
4
+ * @example
5
+ * clamp( 5, 0, 10) // 5
6
+ * clamp(-3, 0, 10) // 0
7
+ * clamp(17, 0, 10) // 10
8
+ */
9
+ export declare function clamp(value: number, min: number, max: number): number;
10
+ /**
11
+ * Deeply compares two values (objects, arrays, primitives)
12
+ * with the following rules:
13
+ * - Objects are compared ignoring property order.
14
+ * - Arrays are compared ignoring element order (multiset comparison).
15
+ * - Primitives are compared by strict equality.
16
+ * - null/undefined are treated as normal primitives.
17
+ *
18
+ * @param a First value
19
+ * @param b Second value
20
+ * @param visited Used internally to detect cycles
21
+ */
22
+ export declare function arePropsEqual(a: any, b: any, visited?: Set<any>): boolean;
@@ -0,0 +1,6 @@
1
+ import { Action } from '../store';
2
+ import { IPlugin, PluginBatchRegistration, PluginPackage } from '../types/plugin';
3
+ /**
4
+ * Helper function to create a properly typed plugin registration
5
+ */
6
+ export declare function createPluginRegistration<T extends IPlugin<TConfig>, TConfig, TState, TAction extends Action>(pluginPackage: PluginPackage<T, TConfig, TState, TAction>, config?: Partial<TConfig>): PluginBatchRegistration<T, TConfig, any, any>;
@@ -0,0 +1,12 @@
1
+ type EnumKey = string | number;
2
+ /**
3
+ * Iterate over a Record whose keys are enum members (numeric or string),
4
+ * getting back a fully-typed `[key, value]` tuple array.
5
+ *
6
+ * Usage:
7
+ * for (const [subtype, defaults] of enumEntries(this.state.toolDefaults)) {
8
+ * // subtype is inferred as keyof ToolDefaultsBySubtype
9
+ * }
10
+ */
11
+ export declare function enumEntries<E extends EnumKey, V>(record: Record<E, V>): Array<[E, V]>;
12
+ export {};
@@ -0,0 +1,4 @@
1
+ import { createContext } from 'preact';
2
+ export { useEffect, useRef, useState, useCallback, useMemo, useContext } from 'preact/hooks';
3
+ export type { ComponentChildren as ReactNode, JSX } from 'preact';
4
+ export { createContext };
@@ -1,188 +1,2 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/preact/index.ts
21
- var preact_exports = {};
22
- __export(preact_exports, {
23
- EmbedPDF: () => EmbedPDF,
24
- PDFContext: () => PDFContext,
25
- useCapability: () => useCapability,
26
- useCoreState: () => useCoreState,
27
- usePlugin: () => usePlugin,
28
- useRegistry: () => useRegistry,
29
- useStoreState: () => useStoreState
30
- });
31
- module.exports = __toCommonJS(preact_exports);
32
-
33
- // src/preact/context.ts
34
- var import_preact = require("preact");
35
- var PDFContext = (0, import_preact.createContext)({
36
- registry: null,
37
- isInitializing: true,
38
- pluginsReady: false
39
- });
40
-
41
- // src/preact/components/embed-pdf.tsx
42
- var import_hooks = require("preact/hooks");
43
- var import_core = require("@embedpdf/core");
44
- var import_jsx_runtime = require("preact/jsx-runtime");
45
- function EmbedPDF({ engine, onInitialized, plugins, children }) {
46
- const [registry, setRegistry] = (0, import_hooks.useState)(null);
47
- const [isInitializing, setIsInitializing] = (0, import_hooks.useState)(true);
48
- const [pluginsReady, setPluginsReady] = (0, import_hooks.useState)(false);
49
- const initRef = (0, import_hooks.useRef)(onInitialized);
50
- (0, import_hooks.useEffect)(() => {
51
- initRef.current = onInitialized;
52
- }, [onInitialized]);
53
- (0, import_hooks.useEffect)(() => {
54
- const pdfViewer = new import_core.PluginRegistry(engine);
55
- pdfViewer.registerPluginBatch(plugins);
56
- const initialize = async () => {
57
- await pdfViewer.initialize();
58
- if (pdfViewer.isDestroyed()) {
59
- return;
60
- }
61
- await initRef.current?.(pdfViewer);
62
- if (pdfViewer.isDestroyed()) {
63
- return;
64
- }
65
- pdfViewer.pluginsReady().then(() => {
66
- if (!pdfViewer.isDestroyed()) {
67
- setPluginsReady(true);
68
- }
69
- });
70
- setRegistry(pdfViewer);
71
- setIsInitializing(false);
72
- };
73
- initialize().catch(console.error);
74
- return () => {
75
- pdfViewer.destroy();
76
- setRegistry(null);
77
- setIsInitializing(true);
78
- setPluginsReady(false);
79
- };
80
- }, [engine, plugins]);
81
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PDFContext.Provider, { value: { registry, isInitializing, pluginsReady }, children: typeof children === "function" ? children({ registry, isInitializing, pluginsReady }) : children });
82
- }
83
-
84
- // src/preact/hooks/use-registry.ts
85
- var import_hooks2 = require("preact/hooks");
86
- function useRegistry() {
87
- const contextValue = (0, import_hooks2.useContext)(PDFContext);
88
- if (contextValue === void 0) {
89
- throw new Error("useCapability must be used within a PDFContext.Provider");
90
- }
91
- const { registry, isInitializing } = contextValue;
92
- if (isInitializing) {
93
- return contextValue;
94
- }
95
- if (registry === null) {
96
- throw new Error("PDF registry failed to initialize properly");
97
- }
98
- return contextValue;
99
- }
100
-
101
- // src/preact/hooks/use-plugin.ts
102
- function usePlugin(pluginId) {
103
- const { registry } = useRegistry();
104
- if (registry === null) {
105
- return {
106
- plugin: null,
107
- isLoading: true,
108
- ready: new Promise(() => {
109
- })
110
- };
111
- }
112
- const plugin = registry.getPlugin(pluginId);
113
- if (!plugin) {
114
- throw new Error(`Plugin ${pluginId} not found`);
115
- }
116
- return {
117
- plugin,
118
- isLoading: false,
119
- ready: plugin.ready()
120
- };
121
- }
122
-
123
- // src/preact/hooks/use-capability.ts
124
- function useCapability(pluginId) {
125
- const { plugin, isLoading, ready } = usePlugin(pluginId);
126
- if (!plugin) {
127
- return {
128
- provides: null,
129
- isLoading,
130
- ready
131
- };
132
- }
133
- if (!plugin.provides) {
134
- throw new Error(`Plugin ${pluginId} does not provide a capability`);
135
- }
136
- return {
137
- provides: plugin.provides(),
138
- isLoading,
139
- ready
140
- };
141
- }
142
-
143
- // src/preact/hooks/use-store-state.ts
144
- var import_hooks3 = require("preact/hooks");
145
- function useStoreState() {
146
- const { registry } = useRegistry();
147
- const [state, setState] = (0, import_hooks3.useState)(null);
148
- (0, import_hooks3.useEffect)(() => {
149
- if (!registry) return;
150
- setState(registry.getStore().getState());
151
- const unsubscribe = registry.getStore().subscribe((_action, newState) => {
152
- setState(newState);
153
- });
154
- return () => unsubscribe();
155
- }, [registry]);
156
- return state;
157
- }
158
-
159
- // src/preact/hooks/use-core-state.ts
160
- var import_hooks4 = require("preact/hooks");
161
- var import_core2 = require("@embedpdf/core");
162
- function useCoreState() {
163
- const { registry } = useRegistry();
164
- const [coreState, setCoreState] = (0, import_hooks4.useState)(null);
165
- (0, import_hooks4.useEffect)(() => {
166
- if (!registry) return;
167
- const store = registry.getStore();
168
- setCoreState(store.getState().core);
169
- const unsubscribe = store.subscribe((action, newState, oldState) => {
170
- if (store.isCoreAction(action) && !(0, import_core2.arePropsEqual)(newState.core, oldState.core)) {
171
- setCoreState(newState.core);
172
- }
173
- });
174
- return () => unsubscribe();
175
- }, [registry]);
176
- return coreState;
177
- }
178
- // Annotate the CommonJS export names for ESM import in node:
179
- 0 && (module.exports = {
180
- EmbedPDF,
181
- PDFContext,
182
- useCapability,
183
- useCoreState,
184
- usePlugin,
185
- useRegistry,
186
- useStoreState
187
- });
188
- //# sourceMappingURL=index.cjs.map
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("preact"),r=require("preact/hooks"),t=require("preact/jsx-runtime"),i=require("@embedpdf/core"),n=e.createContext({registry:null,isInitializing:!0,pluginsReady:!1});function s(){const e=r.useContext(n);if(void 0===e)throw new Error("useCapability must be used within a PDFContext.Provider");const{registry:t,isInitializing:i}=e;if(i)return e;if(null===t)throw new Error("PDF registry failed to initialize properly");return e}function o(e){const{registry:r}=s();if(null===r)return{plugin:null,isLoading:!0,ready:new Promise((()=>{}))};const t=r.getPlugin(e);if(!t)throw new Error(`Plugin ${e} not found`);return{plugin:t,isLoading:!1,ready:t.ready()}}exports.EmbedPDF=function({engine:e,onInitialized:s,plugins:o,children:u}){const[a,l]=r.useState(null),[c,g]=r.useState(!0),[d,p]=r.useState(!1),y=r.useRef(s);return r.useEffect((()=>{y.current=s}),[s]),r.useEffect((()=>{const r=new i.PluginRegistry(e);r.registerPluginBatch(o);return(async()=>{var e;await r.initialize(),r.isDestroyed()||(await(null==(e=y.current)?void 0:e.call(y,r)),r.isDestroyed()||(r.pluginsReady().then((()=>{r.isDestroyed()||p(!0)})),l(r),g(!1)))})().catch(console.error),()=>{r.destroy(),l(null),g(!0),p(!1)}}),[e,o]),t.jsx(n.Provider,{value:{registry:a,isInitializing:c,pluginsReady:d},children:"function"==typeof u?u({registry:a,isInitializing:c,pluginsReady:d}):u})},exports.PDFContext=n,exports.useCapability=function(e){const{plugin:r,isLoading:t,ready:i}=o(e);if(!r)return{provides:null,isLoading:t,ready:i};if(!r.provides)throw new Error(`Plugin ${e} does not provide a capability`);return{provides:r.provides(),isLoading:t,ready:i}},exports.useCoreState=function(){const{registry:e}=s(),[t,n]=r.useState(null);return r.useEffect((()=>{if(!e)return;const r=e.getStore();n(r.getState().core);const t=r.subscribe(((e,t,s)=>{r.isCoreAction(e)&&!i.arePropsEqual(t.core,s.core)&&n(t.core)}));return()=>t()}),[e]),t},exports.usePlugin=o,exports.useRegistry=s,exports.useStoreState=function(){const{registry:e}=s(),[t,i]=r.useState(null);return r.useEffect((()=>{if(!e)return;i(e.getStore().getState());const r=e.getStore().subscribe(((e,r)=>{i(r)}));return()=>r()}),[e]),t};
2
+ //# sourceMappingURL=index.cjs.map