@embedpdf/core 1.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 +21 -0
- package/README.md +40 -0
- package/dist/index.cjs +1308 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +567 -0
- package/dist/index.d.ts +567 -0
- package/dist/index.js +1252 -0
- package/dist/index.js.map +1 -0
- package/dist/preact/index.cjs +163 -0
- package/dist/preact/index.cjs.map +1 -0
- package/dist/preact/index.d.cts +65 -0
- package/dist/preact/index.d.ts +65 -0
- package/dist/preact/index.js +131 -0
- package/dist/preact/index.js.map +1 -0
- package/dist/react/index.cjs +163 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +64 -0
- package/dist/react/index.d.ts +64 -0
- package/dist/react/index.js +131 -0
- package/dist/react/index.js.map +1 -0
- package/package.json +54 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/preact/context.ts","../../src/preact/components/embed-pdf.tsx","../../src/preact/hooks/use-registry.ts","../../src/preact/hooks/use-plugin.ts","../../src/preact/hooks/use-capability.ts","../../src/preact/hooks/use-store-state.ts"],"sourcesContent":["import { createContext } from 'preact';\nimport type { PluginRegistry } from '@embedpdf/core';\n\nexport interface PDFContextState {\n registry: PluginRegistry | null;\n isInitializing: boolean;\n pluginsReady: boolean;\n}\n\nexport const PDFContext = createContext<PDFContextState>({\n registry: null,\n isInitializing: true,\n pluginsReady: false,\n});\n","/** @jsxImportSource preact */\nimport { h, ComponentChildren } from 'preact';\nimport { useState, useEffect, useCallback } from 'preact/hooks';\nimport { PdfEngine } from '@embedpdf/models';\nimport { PluginRegistry } from '@embedpdf/core';\nimport type { IPlugin, PluginBatchRegistration } from '@embedpdf/core';\n\nimport { PDFContext, PDFContextState } from '../context';\n\ninterface EmbedPDFProps {\n engine: PdfEngine;\n onInitialized: (registry: PluginRegistry) => Promise<void>;\n plugins: PluginBatchRegistration<IPlugin<any>, any>[];\n children: ComponentChildren | ((state: PDFContextState) => ComponentChildren);\n}\n\nexport function EmbedPDF({ engine, onInitialized, plugins, children }: EmbedPDFProps) {\n const [registry, setRegistry] = useState<PluginRegistry | null>(null);\n const [isInitializing, setIsInitializing] = useState<boolean>(true);\n const [pluginsReady, setPluginsReady] = useState<boolean>(false);\n\n const stableOnInit = useCallback(onInitialized, [onInitialized]);\n\n useEffect(() => {\n const pdfViewer = new PluginRegistry(engine);\n pdfViewer.registerPluginBatch(plugins);\n\n const initialize = async () => {\n await pdfViewer.initialize();\n // if the registry is destroyed, don't do anything\n if (pdfViewer.isDestroyed()) {\n return;\n }\n await stableOnInit(pdfViewer);\n // if the registry is destroyed, don't do anything\n if (pdfViewer.isDestroyed()) {\n return;\n }\n\n pdfViewer.pluginsReady().then(() => {\n if (!pdfViewer.isDestroyed()) {\n setPluginsReady(true);\n }\n });\n\n // Provide the registry to children via context\n setRegistry(pdfViewer);\n setIsInitializing(false);\n };\n\n initialize().catch(console.error);\n\n return () => {\n pdfViewer.destroy();\n setRegistry(null);\n setIsInitializing(true);\n setPluginsReady(false);\n };\n }, [engine, stableOnInit, plugins]);\n\n return (\n <PDFContext.Provider value={{ registry, isInitializing, pluginsReady }}>\n {typeof children === 'function'\n ? children({ registry, isInitializing, pluginsReady })\n : children}\n </PDFContext.Provider>\n );\n}\n","import { useContext } from 'preact/hooks';\nimport { PDFContext, PDFContextState } from '../context';\n\n/**\n * Hook to access the PDF registry.\n * @returns The PDF registry or null during initialization\n */\nexport function useRegistry(): PDFContextState {\n const contextValue = useContext(PDFContext);\n\n // Error if used outside of context\n if (contextValue === undefined) {\n throw new Error('useCapability must be used within a PDFContext.Provider');\n }\n\n const { registry, isInitializing } = contextValue;\n\n // During initialization, return null instead of throwing an error\n if (isInitializing) {\n return contextValue;\n }\n\n // At this point, initialization is complete but registry is still null, which is unexpected\n if (registry === null) {\n throw new Error('PDF registry failed to initialize properly');\n }\n\n return contextValue;\n}\n","import type { BasePlugin } from '@embedpdf/core';\nimport { useRegistry } from './use-registry';\n\ntype PluginState<T extends BasePlugin> = {\n plugin: T | null;\n isLoading: boolean;\n ready: Promise<void>;\n};\n\n/**\n * Hook to access a plugin.\n * @param pluginId The ID of the plugin to access\n * @returns The plugin or null during initialization\n * @example\n * // Get zoom plugin\n * const zoom = usePlugin<ZoomPlugin>(ZoomPlugin.id);\n */\nexport function usePlugin<T extends BasePlugin>(pluginId: T['id']): PluginState<T> {\n const { registry } = useRegistry();\n\n if (registry === null) {\n return {\n plugin: null,\n isLoading: true,\n ready: new Promise(() => {}),\n };\n }\n\n const plugin = registry.getPlugin<T>(pluginId);\n\n if (!plugin) {\n throw new Error(`Plugin ${pluginId} not found`);\n }\n\n return {\n plugin,\n isLoading: false,\n ready: plugin.ready(),\n };\n}\n","import type { BasePlugin } from '@embedpdf/core';\nimport { usePlugin } from './use-plugin';\n\ntype CapabilityState<T extends BasePlugin> = {\n provides: ReturnType<NonNullable<T['provides']>> | null;\n isLoading: boolean;\n ready: Promise<void>;\n};\n\n/**\n * Hook to access a plugin's capability.\n * @param pluginId The ID of the plugin to access\n * @returns The capability provided by the plugin or null during initialization\n * @example\n * // Get zoom capability\n * const zoom = useCapability<ZoomPlugin>(ZoomPlugin.id);\n */\nexport function useCapability<T extends BasePlugin>(pluginId: T['id']): CapabilityState<T> {\n const { plugin, isLoading, ready } = usePlugin<T>(pluginId);\n\n if (!plugin) {\n return {\n provides: null,\n isLoading,\n ready,\n };\n }\n\n if (!plugin.provides) {\n throw new Error(`Plugin ${pluginId} does not provide a capability`);\n }\n\n return {\n provides: plugin.provides() as ReturnType<NonNullable<T['provides']>>,\n isLoading,\n ready,\n };\n}\n","import { useState, useEffect } from 'preact/hooks';\nimport { CoreState, PluginRegistry, StoreState } from '@embedpdf/core';\nimport { useRegistry } from './use-registry';\n\n/**\n * Hook that provides access to the current global store state\n * and re-renders the component when the state changes\n */\nexport function useStoreState<T = CoreState>(): StoreState<T> | null {\n const { registry } = useRegistry();\n const [state, setState] = useState<StoreState<T> | null>(null);\n\n useEffect(() => {\n if (!registry) return;\n\n // Get initial state\n setState(registry.getStore().getState() as StoreState<T>);\n\n // Subscribe to store changes\n const unsubscribe = registry.getStore().subscribe((_action, newState) => {\n setState(newState as StoreState<T>);\n });\n\n return () => unsubscribe();\n }, [registry]);\n\n return state;\n}\n"],"mappings":";AAAA,SAAS,qBAAqB;AASvB,IAAM,aAAa,cAA+B;AAAA,EACvD,UAAU;AAAA,EACV,gBAAgB;AAAA,EAChB,cAAc;AAChB,CAAC;;;ACXD,SAAS,UAAU,WAAW,mBAAmB;AAEjD,SAAS,sBAAsB;AAyD3B;AA7CG,SAAS,SAAS,EAAE,QAAQ,eAAe,SAAS,SAAS,GAAkB;AACpF,QAAM,CAAC,UAAU,WAAW,IAAI,SAAgC,IAAI;AACpE,QAAM,CAAC,gBAAgB,iBAAiB,IAAI,SAAkB,IAAI;AAClE,QAAM,CAAC,cAAc,eAAe,IAAI,SAAkB,KAAK;AAE/D,QAAM,eAAe,YAAY,eAAe,CAAC,aAAa,CAAC;AAE/D,YAAU,MAAM;AACd,UAAM,YAAY,IAAI,eAAe,MAAM;AAC3C,cAAU,oBAAoB,OAAO;AAErC,UAAM,aAAa,YAAY;AAC7B,YAAM,UAAU,WAAW;AAE3B,UAAI,UAAU,YAAY,GAAG;AAC3B;AAAA,MACF;AACA,YAAM,aAAa,SAAS;AAE5B,UAAI,UAAU,YAAY,GAAG;AAC3B;AAAA,MACF;AAEA,gBAAU,aAAa,EAAE,KAAK,MAAM;AAClC,YAAI,CAAC,UAAU,YAAY,GAAG;AAC5B,0BAAgB,IAAI;AAAA,QACtB;AAAA,MACF,CAAC;AAGD,kBAAY,SAAS;AACrB,wBAAkB,KAAK;AAAA,IACzB;AAEA,eAAW,EAAE,MAAM,QAAQ,KAAK;AAEhC,WAAO,MAAM;AACX,gBAAU,QAAQ;AAClB,kBAAY,IAAI;AAChB,wBAAkB,IAAI;AACtB,sBAAgB,KAAK;A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AAA,IACL,UAAU,OAAO,SAAS;AAAA,IAC1B;AAAA,IACA;AAAA,EACF;AACF;;;ACrCA,SAAS,YAAAA,WAAU,aAAAC,kBAAiB;AAQ7B,SAAS,gBAAqD;AACnE,QAAM,EAAE,SAAS,IAAI,YAAY;AACjC,QAAM,CAAC,OAAO,QAAQ,IAAIC,UAA+B,IAAI;AAE7D,EAAAC,WAAU,MAAM;AACd,QAAI,CAAC,SAAU;AAGf,aAAS,SAAS,SAAS,EAAE,SAAS,CAAkB;AAGxD,UAAM,cAAc,SAAS,SAAS,EAAE,UAAU,CAAC,SAAS,aAAa;AACvE,eAAS,QAAyB;AAAA,IACpC,CAAC;AAED,WAAO,MAAM,YAAY;AAAA,EAC3B,GAAG,CAAC,QAAQ,CAAC;AAEb,SAAO;AACT;","names":["useState","useEffect","useState","useEffect"]}
|
|
@@ -0,0 +1,163 @@
|
|
|
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/react/index.ts
|
|
21
|
+
var react_exports = {};
|
|
22
|
+
__export(react_exports, {
|
|
23
|
+
EmbedPDF: () => EmbedPDF,
|
|
24
|
+
PDFContext: () => PDFContext,
|
|
25
|
+
useCapability: () => useCapability,
|
|
26
|
+
usePlugin: () => usePlugin,
|
|
27
|
+
useRegistry: () => useRegistry,
|
|
28
|
+
useStoreState: () => useStoreState
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(react_exports);
|
|
31
|
+
|
|
32
|
+
// src/react/context.ts
|
|
33
|
+
var import_react = require("react");
|
|
34
|
+
var PDFContext = (0, import_react.createContext)({
|
|
35
|
+
registry: null,
|
|
36
|
+
isInitializing: true,
|
|
37
|
+
pluginsReady: false
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// src/react/components/embed-pdf.tsx
|
|
41
|
+
var import_react2 = require("react");
|
|
42
|
+
var import_core = require("@embedpdf/core");
|
|
43
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
44
|
+
function EmbedPDF({ engine, onInitialized, plugins, children }) {
|
|
45
|
+
const [registry, setRegistry] = (0, import_react2.useState)(null);
|
|
46
|
+
const [isInitializing, setIsInitializing] = (0, import_react2.useState)(true);
|
|
47
|
+
const [pluginsReady, setPluginsReady] = (0, import_react2.useState)(false);
|
|
48
|
+
const stableOnInit = (0, import_react2.useCallback)(onInitialized, [onInitialized]);
|
|
49
|
+
(0, import_react2.useEffect)(() => {
|
|
50
|
+
const pdfViewer = new import_core.PluginRegistry(engine);
|
|
51
|
+
pdfViewer.registerPluginBatch(plugins);
|
|
52
|
+
const initialize = async () => {
|
|
53
|
+
await pdfViewer.initialize();
|
|
54
|
+
if (pdfViewer.isDestroyed()) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
await stableOnInit(pdfViewer);
|
|
58
|
+
if (pdfViewer.isDestroyed()) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
pdfViewer.pluginsReady().then(() => {
|
|
62
|
+
if (!pdfViewer.isDestroyed()) {
|
|
63
|
+
setPluginsReady(true);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
setRegistry(pdfViewer);
|
|
67
|
+
setIsInitializing(false);
|
|
68
|
+
};
|
|
69
|
+
initialize().catch(console.error);
|
|
70
|
+
return () => {
|
|
71
|
+
pdfViewer.destroy();
|
|
72
|
+
setRegistry(null);
|
|
73
|
+
setIsInitializing(true);
|
|
74
|
+
setPluginsReady(false);
|
|
75
|
+
};
|
|
76
|
+
}, [engine, stableOnInit, plugins]);
|
|
77
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PDFContext.Provider, { value: { registry, isInitializing, pluginsReady }, children: typeof children === "function" ? children({ registry, isInitializing, pluginsReady }) : children });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/react/hooks/use-registry.ts
|
|
81
|
+
var import_react3 = require("react");
|
|
82
|
+
function useRegistry() {
|
|
83
|
+
const contextValue = (0, import_react3.useContext)(PDFContext);
|
|
84
|
+
if (contextValue === void 0) {
|
|
85
|
+
throw new Error("useCapability must be used within a PDFContext.Provider");
|
|
86
|
+
}
|
|
87
|
+
const { registry, isInitializing } = contextValue;
|
|
88
|
+
if (isInitializing) {
|
|
89
|
+
return contextValue;
|
|
90
|
+
}
|
|
91
|
+
if (registry === null) {
|
|
92
|
+
throw new Error("PDF registry failed to initialize properly");
|
|
93
|
+
}
|
|
94
|
+
return contextValue;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// src/react/hooks/use-plugin.ts
|
|
98
|
+
function usePlugin(pluginId) {
|
|
99
|
+
const { registry } = useRegistry();
|
|
100
|
+
if (registry === null) {
|
|
101
|
+
return {
|
|
102
|
+
plugin: null,
|
|
103
|
+
isLoading: true,
|
|
104
|
+
ready: new Promise(() => {
|
|
105
|
+
})
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
const plugin = registry.getPlugin(pluginId);
|
|
109
|
+
if (!plugin) {
|
|
110
|
+
throw new Error(`Plugin ${pluginId} not found`);
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
plugin,
|
|
114
|
+
isLoading: false,
|
|
115
|
+
ready: plugin.ready()
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// src/react/hooks/use-capability.ts
|
|
120
|
+
function useCapability(pluginId) {
|
|
121
|
+
const { plugin, isLoading, ready } = usePlugin(pluginId);
|
|
122
|
+
if (!plugin) {
|
|
123
|
+
return {
|
|
124
|
+
provides: null,
|
|
125
|
+
isLoading,
|
|
126
|
+
ready
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
if (!plugin.provides) {
|
|
130
|
+
throw new Error(`Plugin ${pluginId} does not provide a capability`);
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
provides: plugin.provides(),
|
|
134
|
+
isLoading,
|
|
135
|
+
ready
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// src/react/hooks/use-store-state.ts
|
|
140
|
+
var import_react4 = require("react");
|
|
141
|
+
function useStoreState() {
|
|
142
|
+
const { registry } = useRegistry();
|
|
143
|
+
const [state, setState] = (0, import_react4.useState)(null);
|
|
144
|
+
(0, import_react4.useEffect)(() => {
|
|
145
|
+
if (!registry) return;
|
|
146
|
+
setState(registry.getStore().getState());
|
|
147
|
+
const unsubscribe = registry.getStore().subscribe((_action, newState) => {
|
|
148
|
+
setState(newState);
|
|
149
|
+
});
|
|
150
|
+
return () => unsubscribe();
|
|
151
|
+
}, [registry]);
|
|
152
|
+
return state;
|
|
153
|
+
}
|
|
154
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
155
|
+
0 && (module.exports = {
|
|
156
|
+
EmbedPDF,
|
|
157
|
+
PDFContext,
|
|
158
|
+
useCapability,
|
|
159
|
+
usePlugin,
|
|
160
|
+
useRegistry,
|
|
161
|
+
useStoreState
|
|
162
|
+
});
|
|
163
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/react/index.ts","../../src/react/context.ts","../../src/react/components/embed-pdf.tsx","../../src/react/hooks/use-registry.ts","../../src/react/hooks/use-plugin.ts","../../src/react/hooks/use-capability.ts","../../src/react/hooks/use-store-state.ts"],"sourcesContent":["export * from './context';\nexport * from './components';\nexport * from './hooks';\n","import { createContext } from 'react';\nimport type { PluginRegistry } from '@embedpdf/core';\n\nexport interface PDFContextState {\n registry: PluginRegistry | null;\n isInitializing: boolean;\n pluginsReady: boolean;\n}\n\nexport const PDFContext = createContext<PDFContextState>({\n registry: null,\n isInitializing: true,\n pluginsReady: false,\n});\n","import React, { useState, useEffect, useCallback } from 'react';\nimport { PdfEngine } from '@embedpdf/models';\nimport { PluginRegistry } from '@embedpdf/core';\nimport type { IPlugin, PluginBatchRegistration } from '@embedpdf/core';\n\nimport { PDFContext, PDFContextState } from '../context';\n\ninterface EmbedPDFProps {\n engine: PdfEngine;\n onInitialized: (registry: PluginRegistry) => Promise<void>;\n plugins: PluginBatchRegistration<IPlugin<any>, any>[];\n children: React.ReactNode | ((state: PDFContextState) => React.ReactNode);\n}\n\nexport function EmbedPDF({ engine, onInitialized, plugins, children }: EmbedPDFProps) {\n const [registry, setRegistry] = useState<PluginRegistry | null>(null);\n const [isInitializing, setIsInitializing] = useState<boolean>(true);\n const [pluginsReady, setPluginsReady] = useState<boolean>(false);\n\n const stableOnInit = useCallback(onInitialized, [onInitialized]);\n\n useEffect(() => {\n const pdfViewer = new PluginRegistry(engine);\n pdfViewer.registerPluginBatch(plugins);\n\n const initialize = async () => {\n await pdfViewer.initialize();\n // if the registry is destroyed, don't do anything\n if (pdfViewer.isDestroyed()) {\n return;\n }\n await stableOnInit(pdfViewer);\n // if the registry is destroyed, don't do anything\n if (pdfViewer.isDestroyed()) {\n return;\n }\n\n pdfViewer.pluginsReady().then(() => {\n if (!pdfViewer.isDestroyed()) {\n setPluginsReady(true);\n }\n });\n\n // Provide the registry to children via context\n setRegistry(pdfViewer);\n setIsInitializing(false);\n };\n\n initialize().catch(console.error);\n\n return () => {\n pdfViewer.destroy();\n setRegistry(null);\n setIsInitializing(true);\n setPluginsReady(false);\n };\n }, [engine, stableOnInit, plugins]);\n\n return (\n <PDFContext.Provider value={{ registry, isInitializing, pluginsReady }}>\n {typeof children === 'function'\n ? children({ registry, isInitializing, pluginsReady })\n : children}\n </PDFContext.Provider>\n );\n}\n","import { useContext } from 'react';\nimport { PDFContext, PDFContextState } from '../context';\n\n/**\n * Hook to access the PDF registry.\n * @returns The PDF registry or null during initialization\n */\nexport function useRegistry(): PDFContextState {\n const contextValue = useContext(PDFContext);\n\n // Error if used outside of context\n if (contextValue === undefined) {\n throw new Error('useCapability must be used within a PDFContext.Provider');\n }\n\n const { registry, isInitializing } = contextValue;\n\n // During initialization, return null instead of throwing an error\n if (isInitializing) {\n return contextValue;\n }\n\n // At this point, initialization is complete but registry is still null, which is unexpected\n if (registry === null) {\n throw new Error('PDF registry failed to initialize properly');\n }\n\n return contextValue;\n}\n","import type { BasePlugin } from '@embedpdf/core';\nimport { useRegistry } from './use-registry';\n\ntype PluginState<T extends BasePlugin> = {\n plugin: T | null;\n isLoading: boolean;\n ready: Promise<void>;\n};\n\n/**\n * Hook to access a plugin.\n * @param pluginId The ID of the plugin to access\n * @returns The plugin or null during initialization\n * @example\n * // Get zoom plugin\n * const zoom = usePlugin<ZoomPlugin>(ZoomPlugin.id);\n */\nexport function usePlugin<T extends BasePlugin>(pluginId: T['id']): PluginState<T> {\n const { registry } = useRegistry();\n\n if (registry === null) {\n return {\n plugin: null,\n isLoading: true,\n ready: new Promise(() => {}),\n };\n }\n\n const plugin = registry.getPlugin<T>(pluginId);\n\n if (!plugin) {\n throw new Error(`Plugin ${pluginId} not found`);\n }\n\n return {\n plugin,\n isLoading: false,\n ready: plugin.ready(),\n };\n}\n","import type { BasePlugin } from '@embedpdf/core';\nimport { usePlugin } from './use-plugin';\n\ntype CapabilityState<T extends BasePlugin> = {\n provides: ReturnType<NonNullable<T['provides']>> | null;\n isLoading: boolean;\n ready: Promise<void>;\n};\n\n/**\n * Hook to access a plugin's capability.\n * @param pluginId The ID of the plugin to access\n * @returns The capability provided by the plugin or null during initialization\n * @example\n * // Get zoom capability\n * const zoom = useCapability<ZoomPlugin>(ZoomPlugin.id);\n */\nexport function useCapability<T extends BasePlugin>(pluginId: T['id']): CapabilityState<T> {\n const { plugin, isLoading, ready } = usePlugin<T>(pluginId);\n\n if (!plugin) {\n return {\n provides: null,\n isLoading,\n ready,\n };\n }\n\n if (!plugin.provides) {\n throw new Error(`Plugin ${pluginId} does not provide a capability`);\n }\n\n return {\n provides: plugin.provides() as ReturnType<NonNullable<T['provides']>>,\n isLoading,\n ready,\n };\n}\n","import { useState, useEffect } from 'react';\nimport { CoreState, StoreState } from '@embedpdf/core';\nimport { useRegistry } from './use-registry';\n\n/**\n * Hook that provides access to the current global store state\n * and re-renders the component when the state changes\n */\nexport function useStoreState<T = CoreState>(): StoreState<T> | null {\n const { registry } = useRegistry();\n const [state, setState] = useState<StoreState<T> | null>(null);\n\n useEffect(() => {\n if (!registry) return;\n\n // Get initial state\n setState(registry.getStore().getState() as StoreState<T>);\n\n // Subscribe to store changes\n const unsubscribe = registry.getStore().subscribe((_action, newState) => {\n setState(newState as StoreState<T>);\n });\n\n return () => unsubscribe();\n }, [registry]);\n\n return state;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAA8B;AASvB,IAAM,iBAAa,4BAA+B;AAAA,EACvD,UAAU;AAAA,EACV,gBAAgB;AAAA,EAChB,cAAc;AAChB,CAAC;;;ACbD,IAAAA,gBAAwD;AAExD,kBAA+B;AAyD3B;AA7CG,SAAS,SAAS,EAAE,QAAQ,eAAe,SAAS,SAAS,GAAkB;AACpF,QAAM,CAAC,UAAU,WAAW,QAAI,wBAAgC,IAAI;AACpE,QAAM,CAAC,gBAAgB,iBAAiB,QAAI,wBAAk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+
import React__default from 'react';
|
|
3
|
+
import { PluginRegistry, PluginBatchRegistration, IPlugin, BasePlugin, CoreState, StoreState } from '@embedpdf/core';
|
|
4
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
5
|
+
import { PdfEngine } from '@embedpdf/models';
|
|
6
|
+
|
|
7
|
+
interface PDFContextState {
|
|
8
|
+
registry: PluginRegistry | null;
|
|
9
|
+
isInitializing: boolean;
|
|
10
|
+
pluginsReady: boolean;
|
|
11
|
+
}
|
|
12
|
+
declare const PDFContext: React.Context<PDFContextState>;
|
|
13
|
+
|
|
14
|
+
interface EmbedPDFProps {
|
|
15
|
+
engine: PdfEngine;
|
|
16
|
+
onInitialized: (registry: PluginRegistry) => Promise<void>;
|
|
17
|
+
plugins: PluginBatchRegistration<IPlugin<any>, any>[];
|
|
18
|
+
children: React__default.ReactNode | ((state: PDFContextState) => React__default.ReactNode);
|
|
19
|
+
}
|
|
20
|
+
declare function EmbedPDF({ engine, onInitialized, plugins, children }: EmbedPDFProps): react_jsx_runtime.JSX.Element;
|
|
21
|
+
|
|
22
|
+
type CapabilityState<T extends BasePlugin> = {
|
|
23
|
+
provides: ReturnType<NonNullable<T['provides']>> | null;
|
|
24
|
+
isLoading: boolean;
|
|
25
|
+
ready: Promise<void>;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Hook to access a plugin's capability.
|
|
29
|
+
* @param pluginId The ID of the plugin to access
|
|
30
|
+
* @returns The capability provided by the plugin or null during initialization
|
|
31
|
+
* @example
|
|
32
|
+
* // Get zoom capability
|
|
33
|
+
* const zoom = useCapability<ZoomPlugin>(ZoomPlugin.id);
|
|
34
|
+
*/
|
|
35
|
+
declare function useCapability<T extends BasePlugin>(pluginId: T['id']): CapabilityState<T>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Hook to access the PDF registry.
|
|
39
|
+
* @returns The PDF registry or null during initialization
|
|
40
|
+
*/
|
|
41
|
+
declare function useRegistry(): PDFContextState;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Hook that provides access to the current global store state
|
|
45
|
+
* and re-renders the component when the state changes
|
|
46
|
+
*/
|
|
47
|
+
declare function useStoreState<T = CoreState>(): StoreState<T> | null;
|
|
48
|
+
|
|
49
|
+
type PluginState<T extends BasePlugin> = {
|
|
50
|
+
plugin: T | null;
|
|
51
|
+
isLoading: boolean;
|
|
52
|
+
ready: Promise<void>;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Hook to access a plugin.
|
|
56
|
+
* @param pluginId The ID of the plugin to access
|
|
57
|
+
* @returns The plugin or null during initialization
|
|
58
|
+
* @example
|
|
59
|
+
* // Get zoom plugin
|
|
60
|
+
* const zoom = usePlugin<ZoomPlugin>(ZoomPlugin.id);
|
|
61
|
+
*/
|
|
62
|
+
declare function usePlugin<T extends BasePlugin>(pluginId: T['id']): PluginState<T>;
|
|
63
|
+
|
|
64
|
+
export { EmbedPDF, PDFContext, type PDFContextState, useCapability, usePlugin, useRegistry, useStoreState };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import React__default from 'react';
|
|
3
|
+
import { PluginRegistry, PluginBatchRegistration, IPlugin, BasePlugin, CoreState, StoreState } from '@embedpdf/core';
|
|
4
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
5
|
+
import { PdfEngine } from '@embedpdf/models';
|
|
6
|
+
|
|
7
|
+
interface PDFContextState {
|
|
8
|
+
registry: PluginRegistry | null;
|
|
9
|
+
isInitializing: boolean;
|
|
10
|
+
pluginsReady: boolean;
|
|
11
|
+
}
|
|
12
|
+
declare const PDFContext: React.Context<PDFContextState>;
|
|
13
|
+
|
|
14
|
+
interface EmbedPDFProps {
|
|
15
|
+
engine: PdfEngine;
|
|
16
|
+
onInitialized: (registry: PluginRegistry) => Promise<void>;
|
|
17
|
+
plugins: PluginBatchRegistration<IPlugin<any>, any>[];
|
|
18
|
+
children: React__default.ReactNode | ((state: PDFContextState) => React__default.ReactNode);
|
|
19
|
+
}
|
|
20
|
+
declare function EmbedPDF({ engine, onInitialized, plugins, children }: EmbedPDFProps): react_jsx_runtime.JSX.Element;
|
|
21
|
+
|
|
22
|
+
type CapabilityState<T extends BasePlugin> = {
|
|
23
|
+
provides: ReturnType<NonNullable<T['provides']>> | null;
|
|
24
|
+
isLoading: boolean;
|
|
25
|
+
ready: Promise<void>;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Hook to access a plugin's capability.
|
|
29
|
+
* @param pluginId The ID of the plugin to access
|
|
30
|
+
* @returns The capability provided by the plugin or null during initialization
|
|
31
|
+
* @example
|
|
32
|
+
* // Get zoom capability
|
|
33
|
+
* const zoom = useCapability<ZoomPlugin>(ZoomPlugin.id);
|
|
34
|
+
*/
|
|
35
|
+
declare function useCapability<T extends BasePlugin>(pluginId: T['id']): CapabilityState<T>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Hook to access the PDF registry.
|
|
39
|
+
* @returns The PDF registry or null during initialization
|
|
40
|
+
*/
|
|
41
|
+
declare function useRegistry(): PDFContextState;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Hook that provides access to the current global store state
|
|
45
|
+
* and re-renders the component when the state changes
|
|
46
|
+
*/
|
|
47
|
+
declare function useStoreState<T = CoreState>(): StoreState<T> | null;
|
|
48
|
+
|
|
49
|
+
type PluginState<T extends BasePlugin> = {
|
|
50
|
+
plugin: T | null;
|
|
51
|
+
isLoading: boolean;
|
|
52
|
+
ready: Promise<void>;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Hook to access a plugin.
|
|
56
|
+
* @param pluginId The ID of the plugin to access
|
|
57
|
+
* @returns The plugin or null during initialization
|
|
58
|
+
* @example
|
|
59
|
+
* // Get zoom plugin
|
|
60
|
+
* const zoom = usePlugin<ZoomPlugin>(ZoomPlugin.id);
|
|
61
|
+
*/
|
|
62
|
+
declare function usePlugin<T extends BasePlugin>(pluginId: T['id']): PluginState<T>;
|
|
63
|
+
|
|
64
|
+
export { EmbedPDF, PDFContext, type PDFContextState, useCapability, usePlugin, useRegistry, useStoreState };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// src/react/context.ts
|
|
2
|
+
import { createContext } from "react";
|
|
3
|
+
var PDFContext = createContext({
|
|
4
|
+
registry: null,
|
|
5
|
+
isInitializing: true,
|
|
6
|
+
pluginsReady: false
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
// src/react/components/embed-pdf.tsx
|
|
10
|
+
import { useState, useEffect, useCallback } from "react";
|
|
11
|
+
import { PluginRegistry } from "@embedpdf/core";
|
|
12
|
+
import { jsx } from "react/jsx-runtime";
|
|
13
|
+
function EmbedPDF({ engine, onInitialized, plugins, children }) {
|
|
14
|
+
const [registry, setRegistry] = useState(null);
|
|
15
|
+
const [isInitializing, setIsInitializing] = useState(true);
|
|
16
|
+
const [pluginsReady, setPluginsReady] = useState(false);
|
|
17
|
+
const stableOnInit = useCallback(onInitialized, [onInitialized]);
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
const pdfViewer = new PluginRegistry(engine);
|
|
20
|
+
pdfViewer.registerPluginBatch(plugins);
|
|
21
|
+
const initialize = async () => {
|
|
22
|
+
await pdfViewer.initialize();
|
|
23
|
+
if (pdfViewer.isDestroyed()) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
await stableOnInit(pdfViewer);
|
|
27
|
+
if (pdfViewer.isDestroyed()) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
pdfViewer.pluginsReady().then(() => {
|
|
31
|
+
if (!pdfViewer.isDestroyed()) {
|
|
32
|
+
setPluginsReady(true);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
setRegistry(pdfViewer);
|
|
36
|
+
setIsInitializing(false);
|
|
37
|
+
};
|
|
38
|
+
initialize().catch(console.error);
|
|
39
|
+
return () => {
|
|
40
|
+
pdfViewer.destroy();
|
|
41
|
+
setRegistry(null);
|
|
42
|
+
setIsInitializing(true);
|
|
43
|
+
setPluginsReady(false);
|
|
44
|
+
};
|
|
45
|
+
}, [engine, stableOnInit, plugins]);
|
|
46
|
+
return /* @__PURE__ */ jsx(PDFContext.Provider, { value: { registry, isInitializing, pluginsReady }, children: typeof children === "function" ? children({ registry, isInitializing, pluginsReady }) : children });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// src/react/hooks/use-registry.ts
|
|
50
|
+
import { useContext } from "react";
|
|
51
|
+
function useRegistry() {
|
|
52
|
+
const contextValue = useContext(PDFContext);
|
|
53
|
+
if (contextValue === void 0) {
|
|
54
|
+
throw new Error("useCapability must be used within a PDFContext.Provider");
|
|
55
|
+
}
|
|
56
|
+
const { registry, isInitializing } = contextValue;
|
|
57
|
+
if (isInitializing) {
|
|
58
|
+
return contextValue;
|
|
59
|
+
}
|
|
60
|
+
if (registry === null) {
|
|
61
|
+
throw new Error("PDF registry failed to initialize properly");
|
|
62
|
+
}
|
|
63
|
+
return contextValue;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/react/hooks/use-plugin.ts
|
|
67
|
+
function usePlugin(pluginId) {
|
|
68
|
+
const { registry } = useRegistry();
|
|
69
|
+
if (registry === null) {
|
|
70
|
+
return {
|
|
71
|
+
plugin: null,
|
|
72
|
+
isLoading: true,
|
|
73
|
+
ready: new Promise(() => {
|
|
74
|
+
})
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
const plugin = registry.getPlugin(pluginId);
|
|
78
|
+
if (!plugin) {
|
|
79
|
+
throw new Error(`Plugin ${pluginId} not found`);
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
plugin,
|
|
83
|
+
isLoading: false,
|
|
84
|
+
ready: plugin.ready()
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/react/hooks/use-capability.ts
|
|
89
|
+
function useCapability(pluginId) {
|
|
90
|
+
const { plugin, isLoading, ready } = usePlugin(pluginId);
|
|
91
|
+
if (!plugin) {
|
|
92
|
+
return {
|
|
93
|
+
provides: null,
|
|
94
|
+
isLoading,
|
|
95
|
+
ready
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
if (!plugin.provides) {
|
|
99
|
+
throw new Error(`Plugin ${pluginId} does not provide a capability`);
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
provides: plugin.provides(),
|
|
103
|
+
isLoading,
|
|
104
|
+
ready
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/react/hooks/use-store-state.ts
|
|
109
|
+
import { useState as useState2, useEffect as useEffect2 } from "react";
|
|
110
|
+
function useStoreState() {
|
|
111
|
+
const { registry } = useRegistry();
|
|
112
|
+
const [state, setState] = useState2(null);
|
|
113
|
+
useEffect2(() => {
|
|
114
|
+
if (!registry) return;
|
|
115
|
+
setState(registry.getStore().getState());
|
|
116
|
+
const unsubscribe = registry.getStore().subscribe((_action, newState) => {
|
|
117
|
+
setState(newState);
|
|
118
|
+
});
|
|
119
|
+
return () => unsubscribe();
|
|
120
|
+
}, [registry]);
|
|
121
|
+
return state;
|
|
122
|
+
}
|
|
123
|
+
export {
|
|
124
|
+
EmbedPDF,
|
|
125
|
+
PDFContext,
|
|
126
|
+
useCapability,
|
|
127
|
+
usePlugin,
|
|
128
|
+
useRegistry,
|
|
129
|
+
useStoreState
|
|
130
|
+
};
|
|
131
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/react/context.ts","../../src/react/components/embed-pdf.tsx","../../src/react/hooks/use-registry.ts","../../src/react/hooks/use-plugin.ts","../../src/react/hooks/use-capability.ts","../../src/react/hooks/use-store-state.ts"],"sourcesContent":["import { createContext } from 'react';\nimport type { PluginRegistry } from '@embedpdf/core';\n\nexport interface PDFContextState {\n registry: PluginRegistry | null;\n isInitializing: boolean;\n pluginsReady: boolean;\n}\n\nexport const PDFContext = createContext<PDFContextState>({\n registry: null,\n isInitializing: true,\n pluginsReady: false,\n});\n","import React, { useState, useEffect, useCallback } from 'react';\nimport { PdfEngine } from '@embedpdf/models';\nimport { PluginRegistry } from '@embedpdf/core';\nimport type { IPlugin, PluginBatchRegistration } from '@embedpdf/core';\n\nimport { PDFContext, PDFContextState } from '../context';\n\ninterface EmbedPDFProps {\n engine: PdfEngine;\n onInitialized: (registry: PluginRegistry) => Promise<void>;\n plugins: PluginBatchRegistration<IPlugin<any>, any>[];\n children: React.ReactNode | ((state: PDFContextState) => React.ReactNode);\n}\n\nexport function EmbedPDF({ engine, onInitialized, plugins, children }: EmbedPDFProps) {\n const [registry, setRegistry] = useState<PluginRegistry | null>(null);\n const [isInitializing, setIsInitializing] = useState<boolean>(true);\n const [pluginsReady, setPluginsReady] = useState<boolean>(false);\n\n const stableOnInit = useCallback(onInitialized, [onInitialized]);\n\n useEffect(() => {\n const pdfViewer = new PluginRegistry(engine);\n pdfViewer.registerPluginBatch(plugins);\n\n const initialize = async () => {\n await pdfViewer.initialize();\n // if the registry is destroyed, don't do anything\n if (pdfViewer.isDestroyed()) {\n return;\n }\n await stableOnInit(pdfViewer);\n // if the registry is destroyed, don't do anything\n if (pdfViewer.isDestroyed()) {\n return;\n }\n\n pdfViewer.pluginsReady().then(() => {\n if (!pdfViewer.isDestroyed()) {\n setPluginsReady(true);\n }\n });\n\n // Provide the registry to children via context\n setRegistry(pdfViewer);\n setIsInitializing(false);\n };\n\n initialize().catch(console.error);\n\n return () => {\n pdfViewer.destroy();\n setRegistry(null);\n setIsInitializing(true);\n setPluginsReady(false);\n };\n }, [engine, stableOnInit, plugins]);\n\n return (\n <PDFContext.Provider value={{ registry, isInitializing, pluginsReady }}>\n {typeof children === 'function'\n ? children({ registry, isInitializing, pluginsReady })\n : children}\n </PDFContext.Provider>\n );\n}\n","import { useContext } from 'react';\nimport { PDFContext, PDFContextState } from '../context';\n\n/**\n * Hook to access the PDF registry.\n * @returns The PDF registry or null during initialization\n */\nexport function useRegistry(): PDFContextState {\n const contextValue = useContext(PDFContext);\n\n // Error if used outside of context\n if (contextValue === undefined) {\n throw new Error('useCapability must be used within a PDFContext.Provider');\n }\n\n const { registry, isInitializing } = contextValue;\n\n // During initialization, return null instead of throwing an error\n if (isInitializing) {\n return contextValue;\n }\n\n // At this point, initialization is complete but registry is still null, which is unexpected\n if (registry === null) {\n throw new Error('PDF registry failed to initialize properly');\n }\n\n return contextValue;\n}\n","import type { BasePlugin } from '@embedpdf/core';\nimport { useRegistry } from './use-registry';\n\ntype PluginState<T extends BasePlugin> = {\n plugin: T | null;\n isLoading: boolean;\n ready: Promise<void>;\n};\n\n/**\n * Hook to access a plugin.\n * @param pluginId The ID of the plugin to access\n * @returns The plugin or null during initialization\n * @example\n * // Get zoom plugin\n * const zoom = usePlugin<ZoomPlugin>(ZoomPlugin.id);\n */\nexport function usePlugin<T extends BasePlugin>(pluginId: T['id']): PluginState<T> {\n const { registry } = useRegistry();\n\n if (registry === null) {\n return {\n plugin: null,\n isLoading: true,\n ready: new Promise(() => {}),\n };\n }\n\n const plugin = registry.getPlugin<T>(pluginId);\n\n if (!plugin) {\n throw new Error(`Plugin ${pluginId} not found`);\n }\n\n return {\n plugin,\n isLoading: false,\n ready: plugin.ready(),\n };\n}\n","import type { BasePlugin } from '@embedpdf/core';\nimport { usePlugin } from './use-plugin';\n\ntype CapabilityState<T extends BasePlugin> = {\n provides: ReturnType<NonNullable<T['provides']>> | null;\n isLoading: boolean;\n ready: Promise<void>;\n};\n\n/**\n * Hook to access a plugin's capability.\n * @param pluginId The ID of the plugin to access\n * @returns The capability provided by the plugin or null during initialization\n * @example\n * // Get zoom capability\n * const zoom = useCapability<ZoomPlugin>(ZoomPlugin.id);\n */\nexport function useCapability<T extends BasePlugin>(pluginId: T['id']): CapabilityState<T> {\n const { plugin, isLoading, ready } = usePlugin<T>(pluginId);\n\n if (!plugin) {\n return {\n provides: null,\n isLoading,\n ready,\n };\n }\n\n if (!plugin.provides) {\n throw new Error(`Plugin ${pluginId} does not provide a capability`);\n }\n\n return {\n provides: plugin.provides() as ReturnType<NonNullable<T['provides']>>,\n isLoading,\n ready,\n };\n}\n","import { useState, useEffect } from 'react';\nimport { CoreState, StoreState } from '@embedpdf/core';\nimport { useRegistry } from './use-registry';\n\n/**\n * Hook that provides access to the current global store state\n * and re-renders the component when the state changes\n */\nexport function useStoreState<T = CoreState>(): StoreState<T> | null {\n const { registry } = useRegistry();\n const [state, setState] = useState<StoreState<T> | null>(null);\n\n useEffect(() => {\n if (!registry) return;\n\n // Get initial state\n setState(registry.getStore().getState() as StoreState<T>);\n\n // Subscribe to store changes\n const unsubscribe = registry.getStore().subscribe((_action, newState) => {\n setState(newState as StoreState<T>);\n });\n\n return () => unsubscribe();\n }, [registry]);\n\n return state;\n}\n"],"mappings":";AAAA,SAAS,qBAAqB;AASvB,IAAM,aAAa,cAA+B;AAAA,EACvD,UAAU;AAAA,EACV,gBAAgB;AAAA,EAChB,cAAc;AAChB,CAAC;;;ACbD,SAAgB,UAAU,WAAW,mBAAmB;AAExD,SAAS,sBAAsB;AAyD3B;AA7CG,SAAS,SAAS,EAAE,QAAQ,eAAe,SAAS,SAAS,GAAkB;AACpF,QAAM,CAAC,UAAU,WAAW,IAAI,SAAgC,IAAI;AACpE,QAAM,CAAC,gBAAgB,iBAAiB,IAAI,SAAkB,IAAI;AAClE,QAAM,CAAC,cAAc,eAAe,IAAI,SAAkB,KAAK;AAE/D,QAAM,eAAe,YAAY,eAAe,CAAC,aAAa,CAAC;AAE/D,YAAU,MAAM;AACd,UAAM,YAAY,IAAI,eAAe,MAAM;AAC3C,cAAU,oBAAoB,OAAO;AAErC,UAAM,aAAa,YAAY;AAC7B,YAAM,UAAU,WAAW;AAE3B,UAAI,UAAU,YAAY,GAAG;AAC3B;AAAA,MACF;AACA,YAAM,aAAa,SAAS;AAE5B,UAAI,UAAU,YAAY,GAAG;AAC3B;AAAA,MACF;AAEA,gBAAU,aAAa,EAAE,KAAK,M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tBO,SAAS,cAAoC,UAAuC;AACzF,QAAM,EAAE,QAAQ,WAAW,MAAM,IAAI,UAAa,QAAQ;AAE1D,MAAI,CAAC,QAAQ;AACX,WAAO;AAAA,MACL,UAAU;AAAA,MACV;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,MAAI,CAAC,OAAO,UAAU;AACpB,UAAM,IAAI,MAAM,UAAU,QAAQ,gCAAgC;AAAA,EACpE;AAEA,SAAO;AAAA,IACL,UAAU,OAAO,SAAS;AAAA,IAC1B;AAAA,IACA;AAAA,EACF;AACF;;;ACrCA,SAAS,YAAAA,WAAU,aAAAC,kBAAiB;AAQ7B,SAAS,gBAAqD;AACnE,QAAM,EAAE,SAAS,IAAI,YAAY;AACjC,QAAM,CAAC,OAAO,QAAQ,IAAIC,UAA+B,IAAI;AAE7D,EAAAC,WAAU,MAAM;AACd,QAAI,CAAC,SAAU;AAGf,aAAS,SAAS,SAAS,EAAE,SAAS,CAAkB;AAGxD,UAAM,cAAc,SAAS,SAAS,EAAE,UAAU,CAAC,SAAS,aAAa;AACvE,eAAS,QAAyB;AAAA,IACpC,CAAC;AAED,WAAO,MAAM,YAAY;AAAA,EAC3B,GAAG,CAAC,QAAQ,CAAC;AAEb,SAAO;AACT;","names":["useState","useEffect","useState","useEffect"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@embedpdf/core",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
},
|
|
14
|
+
"./react": {
|
|
15
|
+
"types": "./dist/react/index.d.ts",
|
|
16
|
+
"import": "./dist/react/index.js",
|
|
17
|
+
"require": "./dist/react/index.cjs"
|
|
18
|
+
},
|
|
19
|
+
"./preact": {
|
|
20
|
+
"types": "./dist/preact/index.d.ts",
|
|
21
|
+
"import": "./dist/preact/index.js",
|
|
22
|
+
"require": "./dist/preact/index.cjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"tsup": "^8.0.0",
|
|
27
|
+
"typescript": "^5.0.0",
|
|
28
|
+
"@types/react": "^18.2.0"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@embedpdf/models": "1.0.0",
|
|
32
|
+
"@embedpdf/engines": "1.0.0"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": ">=16.8.0",
|
|
36
|
+
"react-dom": ">=16.8.0",
|
|
37
|
+
"preact": "^10.26.4"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"README.md"
|
|
42
|
+
],
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "PROJECT_CWD=$(pwd) pnpm -w p:build",
|
|
48
|
+
"build:watch": "PROJECT_CWD=$(pwd) pnpm -w p:build:watch",
|
|
49
|
+
"clean": "PROJECT_CWD=$(pwd) pnpm -w p:clean",
|
|
50
|
+
"lint": "PROJECT_CWD=$(pwd) pnpm -w p:lint",
|
|
51
|
+
"lint:fix": "PROJECT_CWD=$(pwd) pnpm -w p:lint:fix",
|
|
52
|
+
"typecheck": "PROJECT_CWD=$(pwd) pnpm -w p:typecheck"
|
|
53
|
+
}
|
|
54
|
+
}
|