@embedpdf/core 1.0.11 → 1.0.13
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/dist/index.cjs +2 -1335
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -585
- package/dist/index.js +55 -84
- package/dist/index.js.map +1 -1
- package/dist/lib/base/base-plugin.d.ts +98 -0
- package/dist/lib/index.d.ts +14 -0
- package/dist/lib/registry/plugin-registry.d.ts +100 -0
- package/dist/lib/store/actions.d.ts +39 -0
- package/dist/lib/store/index.d.ts +6 -0
- package/dist/lib/store/initial-state.d.ts +11 -0
- package/dist/lib/store/plugin-store.d.ts +48 -0
- package/dist/lib/store/reducer.d.ts +4 -0
- package/dist/lib/store/selectors.d.ts +3 -0
- package/dist/lib/store/store.d.ts +105 -0
- package/dist/lib/store/types.d.ts +28 -0
- package/dist/lib/types/errors.d.ts +21 -0
- package/dist/lib/types/plugin.d.ts +49 -0
- package/dist/lib/utils/dependency-resolver.d.ts +6 -0
- package/dist/lib/utils/event-control.d.ts +23 -0
- package/dist/lib/utils/eventing.d.ts +19 -0
- package/dist/lib/utils/math.d.ts +22 -0
- package/dist/lib/utils/plugin-helpers.d.ts +6 -0
- package/dist/lib/utils/typed-object.d.ts +12 -0
- package/dist/preact/adapter.d.ts +4 -0
- package/dist/preact/index.cjs +2 -188
- package/dist/preact/index.cjs.map +1 -1
- package/dist/preact/index.d.ts +1 -71
- package/dist/preact/index.js +11 -27
- package/dist/preact/index.js.map +1 -1
- package/dist/react/adapter.d.ts +2 -0
- package/dist/react/index.cjs +2 -188
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.ts +1 -70
- package/dist/react/index.js +11 -28
- package/dist/react/index.js.map +1 -1
- package/dist/shared-preact/components/embed-pdf.d.ts +12 -0
- package/dist/shared-preact/components/index.d.ts +1 -0
- package/dist/shared-preact/context.d.ts +7 -0
- package/dist/shared-preact/hooks/index.d.ts +5 -0
- package/dist/shared-preact/hooks/use-capability.d.ts +16 -0
- package/dist/shared-preact/hooks/use-core-state.d.ts +6 -0
- package/dist/shared-preact/hooks/use-plugin.d.ts +16 -0
- package/dist/shared-preact/hooks/use-registry.d.ts +6 -0
- package/dist/shared-preact/hooks/use-store-state.d.ts +6 -0
- package/dist/shared-preact/index.d.ts +3 -0
- package/dist/shared-react/components/embed-pdf.d.ts +12 -0
- package/dist/shared-react/components/index.d.ts +1 -0
- package/dist/shared-react/context.d.ts +7 -0
- package/dist/shared-react/hooks/index.d.ts +5 -0
- package/dist/shared-react/hooks/use-capability.d.ts +16 -0
- package/dist/shared-react/hooks/use-core-state.d.ts +6 -0
- package/dist/shared-react/hooks/use-plugin.d.ts +16 -0
- package/dist/shared-react/hooks/use-registry.d.ts +6 -0
- package/dist/shared-react/hooks/use-store-state.d.ts +6 -0
- package/dist/shared-react/index.d.ts +3 -0
- package/dist/vue/components/embed-pdf.vue.d.ts +23 -0
- package/dist/vue/components/index.d.ts +1 -0
- package/dist/vue/composables/index.d.ts +5 -0
- package/dist/vue/composables/use-capability.d.ts +15 -0
- package/dist/vue/composables/use-core-state.d.ts +2 -0
- package/dist/vue/composables/use-plugin.d.ts +8 -0
- package/dist/vue/composables/use-registry.d.ts +1 -0
- package/dist/vue/composables/use-store-state.d.ts +10 -0
- package/dist/vue/context.d.ts +8 -0
- package/dist/vue/index.cjs +2 -0
- package/dist/vue/index.cjs.map +1 -0
- package/dist/vue/index.d.ts +2 -0
- package/dist/vue/index.js +115 -0
- package/dist/vue/index.js.map +1 -0
- package/package.json +20 -12
- package/dist/index.d.cts +0 -585
- package/dist/preact/index.d.cts +0 -71
- package/dist/react/index.d.cts +0 -70
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/preact/index.ts","../../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","../../src/preact/hooks/use-core-state.ts"],"sourcesContent":["export * from './context';\nexport * from './components';\nexport * from './hooks';\n","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, useRef } 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 const initRef = useRef<EmbedPDFProps['onInitialized']>(onInitialized);\n\n useEffect(() => {\n initRef.current = onInitialized; // update without triggering re-runs\n }, [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 initRef.current?.(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, 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","import { useState, useEffect } from 'preact/hooks';\nimport { CoreState, arePropsEqual } from '@embedpdf/core';\nimport { useRegistry } from './use-registry';\n\n/**\n * Hook that provides access to the current core state\n * and re-renders the component only when the core state changes\n */\nexport function useCoreState(): CoreState | null {\n const { registry } = useRegistry();\n const [coreState, setCoreState] = useState<CoreState | null>(null);\n\n useEffect(() => {\n if (!registry) return;\n\n const store = registry.getStore();\n\n // Get initial core state\n setCoreState(store.getState().core);\n\n // Create a single subscription that handles all core actions\n const unsubscribe = store.subscribe((action, newState, oldState) => {\n // Only update if it's a core action and the core state changed\n if (store.isCoreAction(action) && !arePropsEqual(newState.core, oldState.core)) {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AO,SAAS;AAAA,IAC1B;AAAA,IACA;AAAA,EACF;AACF;;;ACrCA,IAAAC,gBAAoC;AAQ7B,SAAS,gBAAqD;AACnE,QAAM,EAAE,SAAS,IAAI,YAAY;AACjC,QAAM,CAAC,OAAO,QAAQ,QAAI,wBAA+B,IAAI;AAE7D,+BAAU,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;;;AC3BA,IAAAC,gBAAoC;AACpC,IAAAC,eAAyC;AAOlC,SAAS,eAAiC;AAC/C,QAAM,EAAE,SAAS,IAAI,YAAY;AACjC,QAAM,CAAC,WAAW,YAAY,QAAI,wBAA2B,IAAI;AAEjE,+BAAU,MAAM;AACd,QAAI,CAAC,SAAU;AAEf,UAAM,QAAQ,SAAS,SAAS;AAGhC,iBAAa,MAAM,SAAS,EAAE,IAAI;AAGlC,UAAM,cAAc,MAAM,UAAU,CAAC,QAAQ,UAAU,aAAa;AAElE,UAAI,MAAM,aAAa,MAAM,KAAK,KAAC,4BAAc,SAAS,MAAM,SAAS,IAAI,GAAG;AAC9E,qBAAa,SAAS,IAAI;AAAA,MAC5B;AAAA,IACF,CAAC;AAED,WAAO,MAAM,YAAY;AAAA,EAC3B,GAAG,CAAC,QAAQ,CAAC;AAEb,SAAO;AACT;","names":["import_hooks","import_hooks","import_hooks","import_core"]}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/shared/context.ts","../../src/shared/hooks/use-registry.ts","../../src/shared/hooks/use-plugin.ts","../../src/shared/components/embed-pdf.tsx","../../src/shared/hooks/use-capability.ts","../../src/shared/hooks/use-core-state.ts","../../src/shared/hooks/use-store-state.ts"],"sourcesContent":["import { createContext } from '@framework';\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 { useContext } from '@framework';\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 { useState, useEffect, useRef, ReactNode } from '@framework';\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: ReactNode | ((state: PDFContextState) => 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 const initRef = useRef<EmbedPDFProps['onInitialized']>(onInitialized);\n\n useEffect(() => {\n initRef.current = onInitialized; // update without triggering re-runs\n }, [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\n /* always call the *latest* callback */\n await initRef.current?.(pdfViewer);\n\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, 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 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 '@framework';\nimport { CoreState, arePropsEqual } from '@embedpdf/core';\nimport { useRegistry } from './use-registry';\n\n/**\n * Hook that provides access to the current core state\n * and re-renders the component only when the core state changes\n */\nexport function useCoreState(): CoreState | null {\n const { registry } = useRegistry();\n const [coreState, setCoreState] = useState<CoreState | null>(null);\n\n useEffect(() => {\n if (!registry) return;\n\n const store = registry.getStore();\n\n // Get initial core state\n setCoreState(store.getState().core);\n\n // Create a single subscription that handles all core actions\n const unsubscribe = store.subscribe((action, newState, oldState) => {\n // Only update if it's a core action and the core state changed\n if (store.isCoreAction(action) && !arePropsEqual(newState.core, oldState.core)) {\n setCoreState(newState.core);\n }\n });\n\n return () => unsubscribe();\n }, [registry]);\n\n return coreState;\n}\n","import { useState, useEffect } from '@framework';\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"],"names":["PDFContext","createContext","registry","isInitializing","pluginsReady","useRegistry","contextValue","useContext","Error","usePlugin","pluginId","plugin","isLoading","ready","Promise","getPlugin","engine","onInitialized","plugins","children","setRegistry","useState","setIsInitializing","setPluginsReady","initRef","useRef","jsx","useEffect","current","pdfViewer","PluginRegistry","registerPluginBatch","async","initialize","isDestroyed","_a","call","then","catch","console","error","destroy","Provider","value","provides","coreState","setCoreState","store","getStore","getState","core","unsubscribe","subscribe","action","newState","oldState","isCoreAction","arePropsEqual","state","setState","_action"],"mappings":"gMASaA,EAAaC,EAAAA,cAA+B,CACvDC,SAAU,KACVC,gBAAgB,EAChBC,cAAc,ICLT,SAASC,IACR,MAAAC,EAAeC,aAAWP,GAGhC,QAAqB,IAAjBM,EACI,MAAA,IAAIE,MAAM,2DAGZ,MAAAN,SAAEA,EAAUC,eAAAA,GAAmBG,EAGrC,GAAIH,EACK,OAAAG,EAIT,GAAiB,OAAbJ,EACI,MAAA,IAAIM,MAAM,8CAGX,OAAAF,CACT,CCXO,SAASG,EAAgCC,GACxC,MAAAR,SAAEA,GAAaG,IAErB,GAAiB,OAAbH,EACK,MAAA,CACLS,OAAQ,KACRC,WAAW,EACXC,MAAO,IAAIC,SAAQ,UAIjB,MAAAH,EAAST,EAASa,UAAaL,GAErC,IAAKC,EACH,MAAM,IAAIH,MAAM,UAAUE,eAGrB,MAAA,CACLC,SACAC,WAAW,EACXC,MAAOF,EAAOE,QAElB,kBCzBO,UAAkBG,OAAEA,EAAAC,cAAQA,EAAeC,QAAAA,EAAAC,SAASA,IACzD,MAAOjB,EAAUkB,GAAeC,EAAAA,SAAgC,OACzDlB,EAAgBmB,GAAqBD,EAAAA,UAAkB,IACvDjB,EAAcmB,GAAmBF,EAAAA,UAAkB,GACpDG,EAAUC,SAAuCR,GA+CrDS,OA7CFC,EAAAA,WAAU,KACRH,EAAQI,QAAUX,CAAA,GACjB,CAACA,IAEJU,EAAAA,WAAU,KACF,MAAAE,EAAY,IAAIC,EAAAA,eAAed,GACrCa,EAAUE,oBAAoBb,GA8B9B,MA5BmBc,uBACXH,EAAUI,a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,WAAWC,YAG7B,MAAME,EAAcjD,EAAS8C,WAAWI,WAAU,CAACQ,EAASN,KAC1DK,EAASL,EAAyB,IAGpC,MAAO,IAAMH,GAAY,GACxB,CAACjD,IAEGwD,CACT"}
|
package/dist/preact/index.d.ts
CHANGED
|
@@ -1,71 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { ComponentChildren, h } from 'preact';
|
|
3
|
-
import { PluginRegistry, PluginBatchRegistration, IPlugin, BasePlugin, CoreState, StoreState } from '@embedpdf/core';
|
|
4
|
-
import { PdfEngine } from '@embedpdf/models';
|
|
5
|
-
|
|
6
|
-
interface PDFContextState {
|
|
7
|
-
registry: PluginRegistry | null;
|
|
8
|
-
isInitializing: boolean;
|
|
9
|
-
pluginsReady: boolean;
|
|
10
|
-
}
|
|
11
|
-
declare const PDFContext: preact.Context<PDFContextState>;
|
|
12
|
-
|
|
13
|
-
/** @jsxImportSource preact */
|
|
14
|
-
|
|
15
|
-
interface EmbedPDFProps {
|
|
16
|
-
engine: PdfEngine;
|
|
17
|
-
onInitialized?: (registry: PluginRegistry) => Promise<void>;
|
|
18
|
-
plugins: PluginBatchRegistration<IPlugin<any>, any>[];
|
|
19
|
-
children: ComponentChildren | ((state: PDFContextState) => ComponentChildren);
|
|
20
|
-
}
|
|
21
|
-
declare function EmbedPDF({ engine, onInitialized, plugins, children }: EmbedPDFProps): h.JSX.Element;
|
|
22
|
-
|
|
23
|
-
type CapabilityState<T extends BasePlugin> = {
|
|
24
|
-
provides: ReturnType<NonNullable<T['provides']>> | null;
|
|
25
|
-
isLoading: boolean;
|
|
26
|
-
ready: Promise<void>;
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Hook to access a plugin's capability.
|
|
30
|
-
* @param pluginId The ID of the plugin to access
|
|
31
|
-
* @returns The capability provided by the plugin or null during initialization
|
|
32
|
-
* @example
|
|
33
|
-
* // Get zoom capability
|
|
34
|
-
* const zoom = useCapability<ZoomPlugin>(ZoomPlugin.id);
|
|
35
|
-
*/
|
|
36
|
-
declare function useCapability<T extends BasePlugin>(pluginId: T['id']): CapabilityState<T>;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Hook to access the PDF registry.
|
|
40
|
-
* @returns The PDF registry or null during initialization
|
|
41
|
-
*/
|
|
42
|
-
declare function useRegistry(): PDFContextState;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Hook that provides access to the current global store state
|
|
46
|
-
* and re-renders the component when the state changes
|
|
47
|
-
*/
|
|
48
|
-
declare function useStoreState<T = CoreState>(): StoreState<T> | null;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Hook that provides access to the current core state
|
|
52
|
-
* and re-renders the component only when the core state changes
|
|
53
|
-
*/
|
|
54
|
-
declare function useCoreState(): CoreState | null;
|
|
55
|
-
|
|
56
|
-
type PluginState<T extends BasePlugin> = {
|
|
57
|
-
plugin: T | null;
|
|
58
|
-
isLoading: boolean;
|
|
59
|
-
ready: Promise<void>;
|
|
60
|
-
};
|
|
61
|
-
/**
|
|
62
|
-
* Hook to access a plugin.
|
|
63
|
-
* @param pluginId The ID of the plugin to access
|
|
64
|
-
* @returns The plugin or null during initialization
|
|
65
|
-
* @example
|
|
66
|
-
* // Get zoom plugin
|
|
67
|
-
* const zoom = usePlugin<ZoomPlugin>(ZoomPlugin.id);
|
|
68
|
-
*/
|
|
69
|
-
declare function usePlugin<T extends BasePlugin>(pluginId: T['id']): PluginState<T>;
|
|
70
|
-
|
|
71
|
-
export { EmbedPDF, PDFContext, type PDFContextState, useCapability, useCoreState, usePlugin, useRegistry, useStoreState };
|
|
1
|
+
export * from '../shared-preact';
|
package/dist/preact/index.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
// src/preact/context.ts
|
|
2
1
|
import { createContext } from "preact";
|
|
3
|
-
|
|
2
|
+
import { useState, useRef, useEffect, useContext } from "preact/hooks";
|
|
3
|
+
import { jsx } from "preact/jsx-runtime";
|
|
4
|
+
import { PluginRegistry, arePropsEqual } from "@embedpdf/core";
|
|
5
|
+
const PDFContext = createContext({
|
|
4
6
|
registry: null,
|
|
5
7
|
isInitializing: true,
|
|
6
8
|
pluginsReady: false
|
|
7
9
|
});
|
|
8
|
-
|
|
9
|
-
// src/preact/components/embed-pdf.tsx
|
|
10
|
-
import { useState, useEffect, useRef } from "preact/hooks";
|
|
11
|
-
import { PluginRegistry } from "@embedpdf/core";
|
|
12
|
-
import { jsx } from "preact/jsx-runtime";
|
|
13
10
|
function EmbedPDF({ engine, onInitialized, plugins, children }) {
|
|
14
11
|
const [registry, setRegistry] = useState(null);
|
|
15
12
|
const [isInitializing, setIsInitializing] = useState(true);
|
|
@@ -22,11 +19,12 @@ function EmbedPDF({ engine, onInitialized, plugins, children }) {
|
|
|
22
19
|
const pdfViewer = new PluginRegistry(engine);
|
|
23
20
|
pdfViewer.registerPluginBatch(plugins);
|
|
24
21
|
const initialize = async () => {
|
|
22
|
+
var _a;
|
|
25
23
|
await pdfViewer.initialize();
|
|
26
24
|
if (pdfViewer.isDestroyed()) {
|
|
27
25
|
return;
|
|
28
26
|
}
|
|
29
|
-
await initRef.current
|
|
27
|
+
await ((_a = initRef.current) == null ? void 0 : _a.call(initRef, pdfViewer));
|
|
30
28
|
if (pdfViewer.isDestroyed()) {
|
|
31
29
|
return;
|
|
32
30
|
}
|
|
@@ -48,9 +46,6 @@ function EmbedPDF({ engine, onInitialized, plugins, children }) {
|
|
|
48
46
|
}, [engine, plugins]);
|
|
49
47
|
return /* @__PURE__ */ jsx(PDFContext.Provider, { value: { registry, isInitializing, pluginsReady }, children: typeof children === "function" ? children({ registry, isInitializing, pluginsReady }) : children });
|
|
50
48
|
}
|
|
51
|
-
|
|
52
|
-
// src/preact/hooks/use-registry.ts
|
|
53
|
-
import { useContext } from "preact/hooks";
|
|
54
49
|
function useRegistry() {
|
|
55
50
|
const contextValue = useContext(PDFContext);
|
|
56
51
|
if (contextValue === void 0) {
|
|
@@ -65,8 +60,6 @@ function useRegistry() {
|
|
|
65
60
|
}
|
|
66
61
|
return contextValue;
|
|
67
62
|
}
|
|
68
|
-
|
|
69
|
-
// src/preact/hooks/use-plugin.ts
|
|
70
63
|
function usePlugin(pluginId) {
|
|
71
64
|
const { registry } = useRegistry();
|
|
72
65
|
if (registry === null) {
|
|
@@ -87,8 +80,6 @@ function usePlugin(pluginId) {
|
|
|
87
80
|
ready: plugin.ready()
|
|
88
81
|
};
|
|
89
82
|
}
|
|
90
|
-
|
|
91
|
-
// src/preact/hooks/use-capability.ts
|
|
92
83
|
function useCapability(pluginId) {
|
|
93
84
|
const { plugin, isLoading, ready } = usePlugin(pluginId);
|
|
94
85
|
if (!plugin) {
|
|
@@ -107,13 +98,10 @@ function useCapability(pluginId) {
|
|
|
107
98
|
ready
|
|
108
99
|
};
|
|
109
100
|
}
|
|
110
|
-
|
|
111
|
-
// src/preact/hooks/use-store-state.ts
|
|
112
|
-
import { useState as useState2, useEffect as useEffect2 } from "preact/hooks";
|
|
113
101
|
function useStoreState() {
|
|
114
102
|
const { registry } = useRegistry();
|
|
115
|
-
const [state, setState] =
|
|
116
|
-
|
|
103
|
+
const [state, setState] = useState(null);
|
|
104
|
+
useEffect(() => {
|
|
117
105
|
if (!registry) return;
|
|
118
106
|
setState(registry.getStore().getState());
|
|
119
107
|
const unsubscribe = registry.getStore().subscribe((_action, newState) => {
|
|
@@ -123,14 +111,10 @@ function useStoreState() {
|
|
|
123
111
|
}, [registry]);
|
|
124
112
|
return state;
|
|
125
113
|
}
|
|
126
|
-
|
|
127
|
-
// src/preact/hooks/use-core-state.ts
|
|
128
|
-
import { useState as useState3, useEffect as useEffect3 } from "preact/hooks";
|
|
129
|
-
import { arePropsEqual } from "@embedpdf/core";
|
|
130
114
|
function useCoreState() {
|
|
131
115
|
const { registry } = useRegistry();
|
|
132
|
-
const [coreState, setCoreState] =
|
|
133
|
-
|
|
116
|
+
const [coreState, setCoreState] = useState(null);
|
|
117
|
+
useEffect(() => {
|
|
134
118
|
if (!registry) return;
|
|
135
119
|
const store = registry.getStore();
|
|
136
120
|
setCoreState(store.getState().core);
|
|
@@ -152,4 +136,4 @@ export {
|
|
|
152
136
|
useRegistry,
|
|
153
137
|
useStoreState
|
|
154
138
|
};
|
|
155
|
-
//# sourceMappingURL=index.js.map
|
|
139
|
+
//# sourceMappingURL=index.js.map
|
package/dist/preact/index.js.map
CHANGED
|
@@ -1 +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","../../src/preact/hooks/use-core-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, useRef } 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 const initRef = useRef<EmbedPDFProps['onInitialized']>(onInitialized);\n\n useEffect(() => {\n initRef.current = onInitialized; // update without triggering re-runs\n }, [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 initRef.current?.(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, 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","import { useState, useEffect } from 'preact/hooks';\nimport { CoreState, arePropsEqual } from '@embedpdf/core';\nimport { useRegistry } from './use-registry';\n\n/**\n * Hook that provides access to the current core state\n * and re-renders the component only when the core state changes\n */\nexport function useCoreState(): CoreState | null {\n const { registry } = useRegistry();\n const [coreState, setCoreState] = useState<CoreState | null>(null);\n\n useEffect(() => {\n if (!registry) return;\n\n const store = registry.getStore();\n\n // Get initial core state\n setCoreState(store.getState().core);\n\n // Create a single subscription that handles all core actions\n const unsubscribe = store.subscribe((action, newState, oldState) => {\n // Only update if it's a core action and the core state changed\n if (store.isCoreAction(action) && !arePropsEqual(newState.core, oldState.core)) {\n setCoreState(newState.core);\n }\n });\n\n return () => unsubscribe();\n }, [registry]);\n\n return coreState;\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,WAAwB,cAAc;AAEzD,SAAS,sBAAsB;AA4D3B;AAhDG,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;AAC/D,QAAM,UAAU,OAAuC,aAAa;AAEpE,YAAU,MAAM;AACd,YAAQ,UAAU;AAAA,EACpB,GAAG,CAAC,aAAa,CAAC;AAElB,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,QAAQ,UAAU,SAAS;AAEjC,UAAI,UAAU,YAAY,GAAG;AAC3B;AAAA,MACF;AAEA,gBAAU,aAAa,EAAE,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AA,MAC5B;AAAA,IACF,CAAC;AAED,WAAO,MAAM,YAAY;AAAA,EAC3B,GAAG,CAAC,QAAQ,CAAC;AAEb,SAAO;AACT;","names":["useState","useEffect","useState","useEffect","useState","useEffect","useState","useEffect"]}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/shared/context.ts","../../src/shared/components/embed-pdf.tsx","../../src/shared/hooks/use-registry.ts","../../src/shared/hooks/use-plugin.ts","../../src/shared/hooks/use-capability.ts","../../src/shared/hooks/use-store-state.ts","../../src/shared/hooks/use-core-state.ts"],"sourcesContent":["import { createContext } from '@framework';\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 { useState, useEffect, useRef, ReactNode } from '@framework';\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: ReactNode | ((state: PDFContextState) => 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 const initRef = useRef<EmbedPDFProps['onInitialized']>(onInitialized);\n\n useEffect(() => {\n initRef.current = onInitialized; // update without triggering re-runs\n }, [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\n /* always call the *latest* callback */\n await initRef.current?.(pdfViewer);\n\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, 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 '@framework';\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 '@framework';\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","import { useState, useEffect } from '@framework';\nimport { CoreState, arePropsEqual } from '@embedpdf/core';\nimport { useRegistry } from './use-registry';\n\n/**\n * Hook that provides access to the current core state\n * and re-renders the component only when the core state changes\n */\nexport function useCoreState(): CoreState | null {\n const { registry } = useRegistry();\n const [coreState, setCoreState] = useState<CoreState | null>(null);\n\n useEffect(() => {\n if (!registry) return;\n\n const store = registry.getStore();\n\n // Get initial core state\n setCoreState(store.getState().core);\n\n // Create a single subscription that handles all core actions\n const unsubscribe = store.subscribe((action, newState, oldState) => {\n // Only update if it's a core action and the core state changed\n if (store.isCoreAction(action) && !arePropsEqual(newState.core, oldState.core)) {\n setCoreState(newState.core);\n }\n });\n\n return () => unsubscribe();\n }, [registry]);\n\n return coreState;\n}\n"],"names":[],"mappings":";;;;AASO,MAAM,aAAa,cAA+B;AAAA,EACvD,UAAU;AAAA,EACV,gBAAgB;AAAA,EAChB,cAAc;AAChB,CAAC;ACCM,SAAS,SAAS,EAAE,QAAQ,eAAe,SAAS,YAA2B;AACpF,QAAM,CAAC,UAAU,WAAW,IAAI,SAAgC,IAAI;AACpE,QAAM,CAAC,gBAAgB,iBAAiB,IAAI,SAAkB,IAAI;AAClE,QAAM,CAAC,cAAc,eAAe,IAAI,SAAkB,KAAK;AACzD,QAAA,UAAU,OAAuC,aAAa;AAEpE,YAAU,MAAM;AACd,YAAQ,UAAU;AAAA,EAAA,GACjB,CAAC,aAAa,CAAC;AAElB,YAAU,MAAM;AACR,UAAA,YAAY,IAAI,eAAe,MAAM;AAC3C,cAAU,oBAAoB,OAAO;AAErC,UAAM,aAAa,YAAY;;AAC7B,YAAM,UAAU,WAAW;AAEvB,UAAA,UAAU,eAAe;AAC3B;AAAA,MAAA;AAII,cAAA,aAAQ,YAAR,iCAAkB;AAGpB,UAAA,UAAU,eAAe;AAC3B;AAAA,MAAA;AAGQ,gBAAA,eAAe,KAAK,MAAM;AAC9B,YAAA,CAAC,UAAU,eAAe;AAC5B,0BAAgB,IAAI;AAAA,QAAA;AAAA,MACtB,CACD;AAGD,kBAAY,SAAS;AACrB,wBAAkB,KAAK;AAAA,IACzB;AAEW,iBAAE,MAAM,QAAQ,KAAK;AAEhC,WAAO,MAAM;AACX,gBAAU,QAAQ;AAClB,kBAAY,IAAI;AAChB,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AAAA,IACL,UAAU,OAAO,SAAS;AAAA,IAC1B;AAAA,IACA;AAAA,EACF;AACF;AC7BO,SAAS,gBAAqD;AAC7D,QAAA,EAAE,SAAS,IAAI,YAAY;AACjC,QAAM,CAAC,OAAO,QAAQ,IAAI,SAA+B,IAAI;AAE7D,YAAU,MAAM;AACd,QAAI,CAAC,SAAU;AAGf,aAAS,SAAS,SAAW,EAAA,SAAA,CAA2B;AAGxD,UAAM,cAAc,SAAS,SAAA,EAAW,UAAU,CAAC,SAAS,aAAa;AACvE,eAAS,QAAyB;AAAA,IAAA,CACnC;AAED,WAAO,MAAM,YAAY;AAAA,EAAA,GACxB,CAAC,QAAQ,CAAC;AAEN,SAAA;AACT;ACnBO,SAAS,eAAiC;AACzC,QAAA,EAAE,SAAS,IAAI,YAAY;AACjC,QAAM,CAAC,WAAW,YAAY,IAAI,SAA2B,IAAI;AAEjE,YAAU,MAAM;AACd,QAAI,CAAC,SAAU;AAET,UAAA,QAAQ,SAAS,SAAS;AAGnB,iBAAA,MAAM,SAAS,EAAE,IAAI;AAGlC,UAAM,cAAc,MAAM,UAAU,CAAC,QAAQ,UAAU,aAAa;AAE9D,UAAA,MAAM,aAAa,MAAM,KAAK,CAAC,cAAc,SAAS,MAAM,SAAS,IAAI,GAAG;AAC9E,qBAAa,SAAS,IAAI;AAAA,MAAA;AAAA,IAC5B,CACD;AAED,WAAO,MAAM,YAAY;AAAA,EAAA,GACxB,CAAC,QAAQ,CAAC;AAEN,SAAA;AACT;"}
|
package/dist/react/index.cjs
CHANGED
|
@@ -1,188 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
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
|
-
useCoreState: () => useCoreState,
|
|
27
|
-
usePlugin: () => usePlugin,
|
|
28
|
-
useRegistry: () => useRegistry,
|
|
29
|
-
useStoreState: () => useStoreState
|
|
30
|
-
});
|
|
31
|
-
module.exports = __toCommonJS(react_exports);
|
|
32
|
-
|
|
33
|
-
// src/react/context.ts
|
|
34
|
-
var import_react = require("react");
|
|
35
|
-
var PDFContext = (0, import_react.createContext)({
|
|
36
|
-
registry: null,
|
|
37
|
-
isInitializing: true,
|
|
38
|
-
pluginsReady: false
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
// src/react/components/embed-pdf.tsx
|
|
42
|
-
var import_react2 = require("react");
|
|
43
|
-
var import_core = require("@embedpdf/core");
|
|
44
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
45
|
-
function EmbedPDF({ engine, onInitialized, plugins, children }) {
|
|
46
|
-
const [registry, setRegistry] = (0, import_react2.useState)(null);
|
|
47
|
-
const [isInitializing, setIsInitializing] = (0, import_react2.useState)(true);
|
|
48
|
-
const [pluginsReady, setPluginsReady] = (0, import_react2.useState)(false);
|
|
49
|
-
const initRef = (0, import_react2.useRef)(onInitialized);
|
|
50
|
-
(0, import_react2.useEffect)(() => {
|
|
51
|
-
initRef.current = onInitialized;
|
|
52
|
-
}, [onInitialized]);
|
|
53
|
-
(0, import_react2.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/react/hooks/use-registry.ts
|
|
85
|
-
var import_react3 = require("react");
|
|
86
|
-
function useRegistry() {
|
|
87
|
-
const contextValue = (0, import_react3.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/react/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/react/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/react/hooks/use-store-state.ts
|
|
144
|
-
var import_react4 = require("react");
|
|
145
|
-
function useStoreState() {
|
|
146
|
-
const { registry } = useRegistry();
|
|
147
|
-
const [state, setState] = (0, import_react4.useState)(null);
|
|
148
|
-
(0, import_react4.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/react/hooks/use-core-state.ts
|
|
160
|
-
var import_react5 = require("react");
|
|
161
|
-
var import_core2 = require("@embedpdf/core");
|
|
162
|
-
function useCoreState() {
|
|
163
|
-
const { registry } = useRegistry();
|
|
164
|
-
const [coreState, setCoreState] = (0, import_react5.useState)(null);
|
|
165
|
-
(0, import_react5.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("react"),t=require("react/jsx-runtime"),r=require("@embedpdf/core"),i=e.createContext({registry:null,isInitializing:!0,pluginsReady:!1});function n(){const t=e.useContext(i);if(void 0===t)throw new Error("useCapability must be used within a PDFContext.Provider");const{registry:r,isInitializing:n}=t;if(n)return t;if(null===r)throw new Error("PDF registry failed to initialize properly");return t}function s(e){const{registry:t}=n();if(null===t)return{plugin:null,isLoading:!0,ready:new Promise((()=>{}))};const r=t.getPlugin(e);if(!r)throw new Error(`Plugin ${e} not found`);return{plugin:r,isLoading:!1,ready:r.ready()}}exports.EmbedPDF=function({engine:n,onInitialized:s,plugins:o,children:u}){const[a,l]=e.useState(null),[c,g]=e.useState(!0),[d,y]=e.useState(!1),f=e.useRef(s);return e.useEffect((()=>{f.current=s}),[s]),e.useEffect((()=>{const e=new r.PluginRegistry(n);e.registerPluginBatch(o);return(async()=>{var t;await e.initialize(),e.isDestroyed()||(await(null==(t=f.current)?void 0:t.call(f,e)),e.isDestroyed()||(e.pluginsReady().then((()=>{e.isDestroyed()||y(!0)})),l(e),g(!1)))})().catch(console.error),()=>{e.destroy(),l(null),g(!0),y(!1)}}),[n,o]),t.jsx(i.Provider,{value:{registry:a,isInitializing:c,pluginsReady:d},children:"function"==typeof u?u({registry:a,isInitializing:c,pluginsReady:d}):u})},exports.PDFContext=i,exports.useCapability=function(e){const{plugin:t,isLoading:r,ready:i}=s(e);if(!t)return{provides:null,isLoading:r,ready:i};if(!t.provides)throw new Error(`Plugin ${e} does not provide a capability`);return{provides:t.provides(),isLoading:r,ready:i}},exports.useCoreState=function(){const{registry:t}=n(),[i,s]=e.useState(null);return e.useEffect((()=>{if(!t)return;const e=t.getStore();s(e.getState().core);const i=e.subscribe(((t,i,n)=>{e.isCoreAction(t)&&!r.arePropsEqual(i.core,n.core)&&s(i.core)}));return()=>i()}),[t]),i},exports.usePlugin=s,exports.useRegistry=n,exports.useStoreState=function(){const{registry:t}=n(),[r,i]=e.useState(null);return e.useEffect((()=>{if(!t)return;i(t.getStore().getState());const e=t.getStore().subscribe(((e,t)=>{i(t)}));return()=>e()}),[t]),r};
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
package/dist/react/index.cjs.map
CHANGED
|
@@ -1 +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","../../src/react/hooks/use-core-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, useRef } 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 const initRef = useRef<EmbedPDFProps['onInitialized']>(onInitialized);\n\n useEffect(() => {\n initRef.current = onInitialized; // update without triggering re-runs\n }, [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\n /* always call the *latest* callback */\n await initRef.current?.(pdfViewer);\n\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, 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","import { useState, useEffect } from 'react';\nimport { CoreState, arePropsEqual } from '@embedpdf/core';\nimport { useRegistry } from './use-registry';\n\n/**\n * Hook that provides access to the current core state\n * and re-renders the component only when the core state changes\n */\nexport function useCoreState(): CoreState | null {\n const { registry } = useRegistry();\n const [coreState, setCoreState] = useState<CoreState | null>(null);\n\n useEffect(() => {\n if (!registry) return;\n\n const store = registry.getStore();\n\n // Get initial core state\n setCoreState(store.getState().core);\n\n // Create a single subscription that handles all core actions\n const unsubscribe = store.subscribe((action, newState, oldState) => {\n // Only update if it's a core action and the core state changed\n if (store.isCoreAction(action) && !arePropsEqual(newState.core, oldState.core)) {\n setCoreState(newState.core);\n }\n });\n\n return () => unsubscribe();\n }, [registry]);\n\n return coreState;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;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,gBAAgE;AAEhE,kBAA+B;AA+D3B;AAnDG,SAAS,SAAS,EAAE,QAAQ,eAAe,SAAS,SAAS,GAAkB;AACpF,QAAM,CAAC,UAAU,WAAW,QAAI,wBAAgC,IAAI;AACpE,QAAM,CAAC,gBAAgB,iBAAiB,QAAI,wBAAkB,IAAI;AAClE,QAAM,CAAC,cAAc,eAAe,QAAI,wBAAkB,KAAK;AAC/D,QAAM,cAAU,sBAAuC,aAAa;AAEpE,+BAAU,MAAM;AACd,YAAQ,UAAU;AAAA,EACpB,GAAG,CAAC,aAAa,CAAC;AAElB,+BAAU,MAAM;AACd,UAAM,YAAY,IAAI,2BAAe,MAAM;AAC3C,cAAU,oBAAoB,OAAO;AAErC,UAAM,aAAa,YAAY;AAC7B,YAAM,UAAU,WAAW;AAE3B,UAAI,UAAU,YAAY,GAAG;AAC3B;AAAA,MACF;AAGA,YAAM,QAAQ,UAAU,SAAS;AAGjC,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,M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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,IAAAC,gBAAoC;AAQ7B,SAAS,gBAAqD;AACnE,QAAM,EAAE,SAAS,IAAI,YAAY;AACjC,QAAM,CAAC,OAAO,QAAQ,QAAI,wBAA+B,IAAI;AAE7D,+BAAU,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;;;AC3BA,IAAAC,gBAAoC;AACpC,IAAAC,eAAyC;AAOlC,SAAS,eAAiC;AAC/C,QAAM,EAAE,SAAS,IAAI,YAAY;AACjC,QAAM,CAAC,WAAW,YAAY,QAAI,wBAA2B,IAAI;AAEjE,+BAAU,MAAM;AACd,QAAI,CAAC,SAAU;AAEf,UAAM,QAAQ,SAAS,SAAS;AAGhC,iBAAa,MAAM,SAAS,EAAE,IAAI;AAGlC,UAAM,cAAc,MAAM,UAAU,CAAC,QAAQ,UAAU,aAAa;AAElE,UAAI,MAAM,aAAa,MAAM,KAAK,KAAC,4BAAc,SAAS,MAAM,SAAS,IAAI,GAAG;AAC9E,qBAAa,SAAS,IAAI;AAAA,MAC5B;AAAA,IACF,CAAC;AAED,WAAO,MAAM,YAAY;AAAA,EAC3B,GAAG,CAAC,QAAQ,CAAC;AAEb,SAAO;AACT;","names":["import_react","import_react","import_react","import_react","import_core"]}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/shared/context.ts","../../src/shared/hooks/use-registry.ts","../../src/shared/hooks/use-plugin.ts","../../src/shared/components/embed-pdf.tsx","../../src/shared/hooks/use-capability.ts","../../src/shared/hooks/use-core-state.ts","../../src/shared/hooks/use-store-state.ts"],"sourcesContent":["import { createContext } from '@framework';\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 { useContext } from '@framework';\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 { useState, useEffect, useRef, ReactNode } from '@framework';\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: ReactNode | ((state: PDFContextState) => 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 const initRef = useRef<EmbedPDFProps['onInitialized']>(onInitialized);\n\n useEffect(() => {\n initRef.current = onInitialized; // update without triggering re-runs\n }, [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\n /* always call the *latest* callback */\n await initRef.current?.(pdfViewer);\n\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, 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 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 '@framework';\nimport { CoreState, arePropsEqual } from '@embedpdf/core';\nimport { useRegistry } from './use-registry';\n\n/**\n * Hook that provides access to the current core state\n * and re-renders the component only when the core state changes\n */\nexport function useCoreState(): CoreState | null {\n const { registry } = useRegistry();\n const [coreState, setCoreState] = useState<CoreState | null>(null);\n\n useEffect(() => {\n if (!registry) return;\n\n const store = registry.getStore();\n\n // Get initial core state\n setCoreState(store.getState().core);\n\n // Create a single subscription that handles all core actions\n const unsubscribe = store.subscribe((action, newState, oldState) => {\n // Only update if it's a core action and the core state changed\n if (store.isCoreAction(action) && !arePropsEqual(newState.core, oldState.core)) {\n setCoreState(newState.core);\n }\n });\n\n return () => unsubscribe();\n }, [registry]);\n\n return coreState;\n}\n","import { useState, useEffect } from '@framework';\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"],"names":["PDFContext","createContext","registry","isInitializing","pluginsReady","useRegistry","contextValue","useContext","Error","usePlugin","pluginId","plugin","isLoading","ready","Promise","getPlugin","engine","onInitialized","plugins","children","setRegistry","useState","setIsInitializing","setPluginsReady","initRef","useRef","jsx","useEffect","current","pdfViewer","PluginRegistry","registerPluginBatch","async","initialize","isDestroyed","_a","call","then","catch","console","error","destroy","Provider","value","provides","coreState","setCoreState","store","getStore","getState","core","unsubscribe","subscribe","action","newState","oldState","isCoreAction","arePropsEqual","state","setState","_action"],"mappings":"oKASaA,EAAaC,EAAAA,cAA+B,CACvDC,SAAU,KACVC,gBAAgB,EAChBC,cAAc,ICLT,SAASC,IACR,MAAAC,EAAeC,aAAWP,GAGhC,QAAqB,IAAjBM,EACI,MAAA,IAAIE,MAAM,2DAGZ,MAAAN,SAAEA,EAAUC,eAAAA,GAAmBG,EAGrC,GAAIH,EACK,OAAAG,EAIT,GAAiB,OAAbJ,EACI,MAAA,IAAIM,MAAM,8CAGX,OAAAF,CACT,CCXO,SAASG,EAAgCC,GACxC,MAAAR,SAAEA,GAAaG,IAErB,GAAiB,OAAbH,EACK,MAAA,CACLS,OAAQ,KACRC,WAAW,EACXC,MAAO,IAAIC,SAAQ,UAIjB,MAAAH,EAAST,EAASa,UAAaL,GAErC,IAAKC,EACH,MAAM,IAAIH,MAAM,UAAUE,eAGrB,MAAA,CACLC,SACAC,WAAW,EACXC,MAAOF,EAAOE,QAElB,kBCzBO,UAAkBG,OAAEA,EAAAC,cAAQA,EAAeC,QAAAA,EAAAC,SAASA,IACzD,MAAOjB,EAAUkB,GAAeC,EAAAA,SAAgC,OACzDlB,EAAgBmB,GAAqBD,EAAAA,UAAkB,IACvDjB,EAAcmB,GAAmBF,EAAAA,UAAkB,GACpDG,EAAUC,SAAuCR,GA+CrDS,OA7CFC,EAAAA,WAAU,KACRH,EAAQI,QAAUX,CA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,MAAA3C,SAAEA,GAAaG,KACdqD,EAAOC,GAAYtC,EAAAA,SAA+B,MAgBlD,OAdPM,EAAAA,WAAU,KACR,IAAKzB,EAAU,OAGfyD,EAASzD,EAAS8C,WAAWC,YAG7B,MAAME,EAAcjD,EAAS8C,WAAWI,WAAU,CAACQ,EAASN,KAC1DK,EAASL,EAAyB,IAGpC,MAAO,IAAMH,GAAY,GACxB,CAACjD,IAEGwD,CACT"}
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,70 +1 @@
|
|
|
1
|
-
|
|
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
|
-
/**
|
|
50
|
-
* Hook that provides access to the current core state
|
|
51
|
-
* and re-renders the component only when the core state changes
|
|
52
|
-
*/
|
|
53
|
-
declare function useCoreState(): CoreState | null;
|
|
54
|
-
|
|
55
|
-
type PluginState<T extends BasePlugin> = {
|
|
56
|
-
plugin: T | null;
|
|
57
|
-
isLoading: boolean;
|
|
58
|
-
ready: Promise<void>;
|
|
59
|
-
};
|
|
60
|
-
/**
|
|
61
|
-
* Hook to access a plugin.
|
|
62
|
-
* @param pluginId The ID of the plugin to access
|
|
63
|
-
* @returns The plugin or null during initialization
|
|
64
|
-
* @example
|
|
65
|
-
* // Get zoom plugin
|
|
66
|
-
* const zoom = usePlugin<ZoomPlugin>(ZoomPlugin.id);
|
|
67
|
-
*/
|
|
68
|
-
declare function usePlugin<T extends BasePlugin>(pluginId: T['id']): PluginState<T>;
|
|
69
|
-
|
|
70
|
-
export { EmbedPDF, PDFContext, type PDFContextState, useCapability, useCoreState, usePlugin, useRegistry, useStoreState };
|
|
1
|
+
export * from '../shared-react';
|