{"version":3,"file":"index.cjs","sources":["../../src/shared/hooks/use-search.ts","../../src/shared/components/search-layer.tsx"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/@framework';\nimport {\n  SearchPlugin,\n  SearchDocumentState,\n  SearchScope,\n  initialSearchDocumentState,\n} from '@embedpdf/plugin-search';\nimport { useEffect, useMemo, useState } from '@framework';\n\nexport const useSearchPlugin = () => usePlugin<SearchPlugin>(SearchPlugin.id);\nexport const useSearchCapability = () => useCapability<SearchPlugin>(SearchPlugin.id);\n\nexport const useSearch = (\n  documentId: string,\n): {\n  state: SearchDocumentState;\n  provides: SearchScope | null;\n} => {\n  const { provides } = useSearchCapability();\n  const [searchState, setSearchState] = useState<SearchDocumentState>(initialSearchDocumentState);\n\n  const scope = useMemo(() => provides?.forDocument(documentId), [provides, documentId]);\n\n  useEffect(() => {\n    if (!scope) {\n      setSearchState(initialSearchDocumentState);\n      return;\n    }\n    // Set initial state\n    setSearchState(scope.getState());\n    // Subscribe to changes\n    return scope.onStateChange((state) => setSearchState(state));\n  }, [scope]);\n\n  return {\n    state: searchState,\n    provides: scope ?? null,\n  };\n};\n","import { useEffect, useMemo, useState, HTMLAttributes, CSSProperties } from '@framework';\nimport { useDocumentState } from '@embedpdf/core/@framework';\nimport { SearchResultState } from '@embedpdf/plugin-search';\n\nimport { useSearchCapability } from '../hooks';\n\ntype SearchLayoutProps = Omit<HTMLAttributes<HTMLDivElement>, 'style'> & {\n  documentId: string;\n  pageIndex: number;\n  scale?: number;\n  highlightColor?: string;\n  activeHighlightColor?: string;\n  style?: CSSProperties;\n};\n\nexport function SearchLayer({\n  documentId,\n  pageIndex,\n  scale: scaleOverride,\n  style,\n  highlightColor = '#FFFF00',\n  activeHighlightColor = '#FFBF00',\n  ...props\n}: SearchLayoutProps) {\n  const { provides: searchProvides } = useSearchCapability();\n  const [searchResultState, setSearchResultState] = useState<SearchResultState | null>(null);\n  const documentState = useDocumentState(documentId);\n\n  const scope = useMemo(\n    () => searchProvides?.forDocument(documentId),\n    [searchProvides, documentId],\n  );\n\n  const actualScale = useMemo(() => {\n    if (scaleOverride !== undefined) return scaleOverride;\n    return documentState?.scale ?? 1;\n  }, [scaleOverride, documentState?.scale]);\n\n  useEffect(() => {\n    if (!scope) {\n      setSearchResultState(null);\n      return;\n    }\n    // Set initial state\n    const currentState = scope.getState();\n    setSearchResultState({\n      results: currentState.results,\n      activeResultIndex: currentState.activeResultIndex,\n      showAllResults: currentState.showAllResults,\n      active: currentState.active,\n    });\n    // Subscribe to changes\n    return scope.onSearchResultStateChange((state) => {\n      setSearchResultState(state);\n    });\n  }, [scope]);\n\n  if (!searchResultState || !searchResultState.active) {\n    return null;\n  }\n\n  // Filter results for current page while preserving original indices\n  const pageResults = searchResultState.results\n    .map((result, originalIndex) => ({ result, originalIndex }))\n    .filter(({ result }) => result.pageIndex === pageIndex);\n\n  // Decide which results to show\n  const resultsToShow = pageResults.filter(\n    ({ originalIndex }) =>\n      searchResultState.showAllResults || originalIndex === searchResultState.activeResultIndex,\n  );\n\n  return (\n    <div\n      style={{\n        ...style,\n        pointerEvents: 'none',\n      }}\n      {...props}\n    >\n      {resultsToShow.map(({ result, originalIndex }) =>\n        result.rects.map((rect, rectIndex) => (\n          <div\n            key={`${originalIndex}-${rectIndex}`}\n            style={{\n              position: 'absolute',\n              top: rect.origin.y * actualScale,\n              left: rect.origin.x * actualScale,\n              width: rect.size.width * actualScale,\n              height: rect.size.height * actualScale,\n              backgroundColor:\n                originalIndex === searchResultState.activeResultIndex\n                  ? activeHighlightColor\n                  : highlightColor,\n              mixBlendMode: 'multiply',\n              transform: 'scale(1.02)',\n              transformOrigin: 'center',\n              transition: 'opacity .3s ease-in-out',\n              opacity: 1,\n            }}\n          ></div>\n        )),\n      )}\n    </div>\n  );\n}\n"],"names":["useSearchCapability","useCapability","SearchPlugin","id","documentId","pageIndex","scale","scaleOverride","style","highlightColor","activeHighlightColor","props","provides","searchProvides","searchResultState","setSearchResultState","useState","documentState","useDocumentState","scope","useMemo","forDocument","actualScale","useEffect","currentState","getState","results","activeResultIndex","showAllResults","active","onSearchResultStateChange","state","resultsToShow","map","result","originalIndex","filter","jsx","pointerEvents","children","rects","rect","rectIndex","position","top","origin","y","left","x","width","size","height","backgroundColor","mixBlendMode","transform","transformOrigin","transition","opacity","searchState","setSearchState","initialSearchDocumentState","onStateChange","usePlugin"],"mappings":"gPAUaA,EAAsB,IAAMC,gBAA4BC,EAAAA,aAAaC,wBCK3E,UAAqBC,WAC1BA,EAAAC,UACAA,EACAC,MAAOC,EAAAC,MACPA,EAAAC,eACAA,EAAiB,UAAAC,qBACjBA,EAAuB,aACpBC,IAEH,MAAQC,SAAUC,GAAmBb,KAC9Bc,EAAmBC,GAAwBC,EAAAA,SAAmC,MAC/EC,EAAgBC,EAAAA,iBAAiBd,GAEjCe,EAAQC,EAAAA,QACZ,UAAMP,WAAgBQ,YAAYjB,GAClC,CAACS,EAAgBT,IAGbkB,EAAcF,EAAAA,QAAQ,aACtBb,EAAoCA,SACjCU,WAAeX,QAAS,EAC9B,CAACC,EAAe,MAAAU,OAAA,EAAAA,EAAeX,QAqBlC,GAnBAiB,EAAAA,UAAU,KACR,IAAKJ,EAEH,YADAJ,EAAqB,MAIvB,MAAMS,EAAeL,EAAMM,WAQ3B,OAPAV,EAAqB,CACnBW,QAASF,EAAaE,QACtBC,kBAAmBH,EAAaG,kBAChCC,eAAgBJ,EAAaI,eAC7BC,OAAQL,EAAaK,SAGhBV,EAAMW,0BAA2BC,IACtChB,EAAqBgB,MAEtB,CAACZ,KAECL,IAAsBA,EAAkBe,OAC3C,OAAO,KAIT,MAKMG,EALclB,EAAkBY,QACnCO,IAAI,CAACC,EAAQC,MAAqBD,SAAQC,mBAC1CC,OAAO,EAAGF,YAAaA,EAAO7B,YAAcA,GAGb+B,OAChC,EAAGD,mBACDrB,EAAkBc,gBAAkBO,IAAkBrB,EAAkBa,mBAG5E,OACEU,EAAAA,IAAC,MAAA,CACC7B,MAAO,IACFA,EACH8B,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