{"version":3,"file":"index.cjs","sources":["../../src/shared/hooks/use-scroll.ts","../../src/shared/components/scroller.tsx"],"sourcesContent":["import { useState, useEffect } from '@framework';\nimport { useCapability, usePlugin } from '@embedpdf/core/@framework';\nimport { ScrollPlugin, ScrollScope } from '@embedpdf/plugin-scroll';\n\nexport const useScrollPlugin = () => usePlugin<ScrollPlugin>(ScrollPlugin.id);\nexport const useScrollCapability = () => useCapability<ScrollPlugin>(ScrollPlugin.id);\n\n// Define the return type explicitly to maintain type safety\ninterface UseScrollReturn {\n  provides: ScrollScope | null;\n  state: {\n    currentPage: number;\n    totalPages: number;\n  };\n}\n\nexport const useScroll = (documentId: string): UseScrollReturn => {\n  const { provides } = useScrollCapability();\n  const [currentPage, setCurrentPage] = useState(1);\n  const [totalPages, setTotalPages] = useState(1);\n\n  useEffect(() => {\n    if (!provides || !documentId) return;\n\n    const scope = provides.forDocument(documentId);\n    setCurrentPage(scope.getCurrentPage());\n    setTotalPages(scope.getTotalPages());\n\n    return provides.onPageChange((event) => {\n      if (event.documentId === documentId) {\n        setCurrentPage(event.pageNumber);\n        setTotalPages(event.totalPages);\n      }\n    });\n  }, [provides, documentId]);\n\n  return {\n    // New format (preferred)\n    provides: provides?.forDocument(documentId) ?? null,\n    state: {\n      currentPage,\n      totalPages,\n    },\n  };\n};\n","import { ReactNode, useEffect, useState, HTMLAttributes, useLayoutEffect } from '@framework';\nimport { ScrollStrategy, ScrollerLayout, PageLayout } from '@embedpdf/plugin-scroll';\n\nimport { useScrollPlugin } from '../hooks';\n\ntype ScrollerProps = HTMLAttributes<HTMLDivElement> & {\n  documentId: string;\n  renderPage: (props: PageLayout) => ReactNode;\n};\n\nexport function Scroller({ documentId, renderPage, ...props }: ScrollerProps) {\n  const { plugin: scrollPlugin } = useScrollPlugin();\n  const [layoutData, setLayoutData] = useState<{\n    layout: ScrollerLayout | null;\n    docId: string | null;\n  }>({ layout: null, docId: null });\n\n  useEffect(() => {\n    if (!scrollPlugin || !documentId) return;\n\n    // When we get new data, store it along with the current documentId\n    const unsubscribe = scrollPlugin.onScrollerData(documentId, (newLayout) => {\n      setLayoutData({ layout: newLayout, docId: documentId });\n    });\n\n    // When the component unmounts or documentId changes, clear the state\n    return () => {\n      unsubscribe();\n      setLayoutData({ layout: null, docId: null });\n      scrollPlugin.clearLayoutReady(documentId);\n    };\n  }, [scrollPlugin, documentId]);\n\n  const scrollerLayout = layoutData.docId === documentId ? layoutData.layout : null;\n\n  useLayoutEffect(() => {\n    if (!scrollPlugin || !documentId || !scrollerLayout) return;\n\n    scrollPlugin.setLayoutReady(documentId);\n  }, [scrollPlugin, documentId, scrollerLayout]);\n\n  if (!scrollerLayout) return null;\n\n  return (\n    <div\n      {...props}\n      style={{\n        width: `${scrollerLayout.totalWidth}px`,\n        height: `${scrollerLayout.totalHeight}px`,\n        position: 'relative',\n        boxSizing: 'border-box',\n        margin: '0 auto',\n        ...(scrollerLayout.strategy === ScrollStrategy.Horizontal && {\n          display: 'flex',\n          flexDirection: 'row',\n        }),\n      }}\n    >\n      <div\n        style={{\n          ...(scrollerLayout.strategy === ScrollStrategy.Horizontal\n            ? {\n                width: scrollerLayout.startSpacing,\n                height: '100%',\n                flexShrink: 0,\n              }\n            : {\n                height: scrollerLayout.startSpacing,\n                width: '100%',\n              }),\n        }}\n      />\n      <div\n        style={{\n          gap: scrollerLayout.pageGap,\n          display: 'flex',\n          alignItems: 'center',\n          position: 'relative',\n          boxSizing: 'border-box',\n          ...(scrollerLayout.strategy === ScrollStrategy.Horizontal\n            ? {\n                flexDirection: 'row',\n                minHeight: '100%',\n              }\n            : {\n                flexDirection: 'column',\n                minWidth: 'fit-content',\n              }),\n        }}\n      >\n        {scrollerLayout.items.map((item) => (\n          <div\n            key={item.pageNumbers[0]}\n            style={{\n              display: 'flex',\n              justifyContent: 'center',\n              gap: scrollerLayout.pageGap,\n            }}\n          >\n            {item.pageLayouts.map((layout) => (\n              <div\n                key={layout.pageNumber}\n                style={{\n                  width: `${layout.rotatedWidth}px`,\n                  height: `${layout.rotatedHeight}px`,\n                  position: 'relative',\n                  zIndex: layout.elevated ? 1 : undefined,\n                }}\n              >\n                {renderPage({\n                  ...layout,\n                })}\n              </div>\n            ))}\n          </div>\n        ))}\n      </div>\n      <div\n        style={{\n          ...(scrollerLayout.strategy === ScrollStrategy.Horizontal\n            ? {\n                width: scrollerLayout.endSpacing,\n                height: '100%',\n                flexShrink: 0,\n              }\n            : {\n                height: scrollerLayout.endSpacing,\n                width: '100%',\n              }),\n        }}\n      />\n    </div>\n  );\n}\n"],"names":["useScrollPlugin","usePlugin","ScrollPlugin","id","useScrollCapability","useCapability","documentId","renderPage","props","plugin","scrollPlugin","layoutData","setLayoutData","useState","layout","docId","useEffect","unsubscribe","onScrollerData","newLayout","clearLayoutReady","scrollerLayout","useLayoutEffect","setLayoutReady","jsxs","style","width","totalWidth","height","totalHeight","position","boxSizing","margin","strategy","ScrollStrategy","Horizontal","display","flexDirection","children","jsx","startSpacing","flexShrink","gap","pageGap","alignItems","minHeight","minWidth","items","map","item","justifyContent","pageLayouts","rotatedWidth","rotatedHeight","zIndex","elevated","pageNumber","pageNumbers","endSpacing","provides","currentPage","setCurrentPage","totalPages","setTotalPages","scope","forDocument","getCurrentPage","getTotalPages","onPageChange","event","state"],"mappings":"0OAIaA,EAAkB,IAAMC,YAAwBC,EAAAA,aAAaC,IAC7DC,EAAsB,IAAMC,gBAA4BH,EAAAA,aAAaC,qBCK3E,UAAkBG,WAAEA,EAAAC,WAAYA,KAAeC,IACpD,MAAQC,OAAQC,GAAiBV,KAC1BW,EAAYC,GAAiBC,EAAAA,SAGjC,CAAEC,OAAQ,KAAMC,MAAO,OAE1BC,EAAAA,UAAU,KACR,IAAKN,IAAiBJ,EAAY,OAGlC,MAAMW,EAAcP,EAAaQ,eAAeZ,EAAaa,IAC3DP,EAAc,CAAEE,OAAQK,EAAWJ,MAAOT,MAI5C,MAAO,KACLW,IACAL,EAAc,CAAEE,OAAQ,KAAMC,MAAO,OACrCL,EA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