{"version":3,"file":"index.cjs","sources":["../../src/svelte/hooks/use-tiling.ts","../../src/svelte/components/TileImg.svelte","../../src/svelte/components/TilingLayer.svelte"],"sourcesContent":["import { TilingPlugin } from '@embedpdf/plugin-tiling';\nimport { useCapability, usePlugin } from '@embedpdf/core/svelte';\n\nexport const useTilingPlugin = () => usePlugin<TilingPlugin>(TilingPlugin.id);\nexport const useTilingCapability = () => useCapability<TilingPlugin>(TilingPlugin.id);\n","<script lang=\"ts\">\n  import type { Tile } from '@embedpdf/plugin-tiling';\n  import { useTilingCapability } from '../hooks';\n  import { ignore, PdfErrorCode } from '@embedpdf/models';\n  import { untrack } from 'svelte';\n\n  interface TileImgProps {\n    documentId: string;\n    pageIndex: number;\n    tile: Tile;\n    dpr: number;\n    scale: number;\n  }\n\n  let { documentId, pageIndex, tile, dpr, scale }: TileImgProps = $props();\n  const tilingCapability = useTilingCapability();\n\n  // Derived scoped capability for the specific document\n  const scope = $derived(tilingCapability.provides?.forDocument(documentId) ?? null);\n\n  let url = $state<string>('');\n  // urlRef is NOT reactive - similar to React's useRef\n  let urlRef: string | null = null;\n\n  // Capture these values once per tile change\n  const tileId = $derived(tile.id);\n  const tileSrcScale = $derived(tile.srcScale);\n  const tileScreenRect = $derived(tile.screenRect);\n  const relativeScale = $derived(scale / tileSrcScale);\n\n  const createPlainTile = (t: Tile): Tile => ({\n    ...t,\n    pageRect: {\n      origin: { x: t.pageRect.origin.x, y: t.pageRect.origin.y },\n      size: { width: t.pageRect.size.width, height: t.pageRect.size.height },\n    },\n    screenRect: {\n      origin: { x: t.screenRect.origin.x, y: t.screenRect.origin.y },\n      size: { width: t.screenRect.size.width, height: t.screenRect.size.height },\n    },\n  });\n\n  /* kick off render exactly once per tile */\n  $effect(() => {\n    // Track only tileId and pageIndex as dependencies (like React's [pageIndex, tile.id])\n    const _tileId = tileId;\n    const _pageIndex = pageIndex;\n\n    // Check if we already have a URL for this tile (already rendered)\n    if (urlRef) return;\n\n    if (!scope) return;\n\n    // Clone to avoid reactive proxies that Web Workers cannot clone\n    const plainTile = untrack(() => createPlainTile(tile));\n    const task = scope.renderTile({\n      pageIndex: _pageIndex,\n      tile: plainTile,\n      dpr,\n    });\n    task.wait((blob) => {\n      const objectUrl = URL.createObjectURL(blob);\n      urlRef = objectUrl;\n      url = objectUrl;\n    }, ignore);\n\n    return () => {\n      if (urlRef) {\n        URL.revokeObjectURL(urlRef);\n        urlRef = null;\n      } else {\n        task.abort({\n          code: PdfErrorCode.Cancelled,\n          message: 'canceled render task',\n        });\n      }\n    };\n  });\n\n  const handleImageLoad = () => {\n    if (urlRef) {\n      URL.revokeObjectURL(urlRef);\n      urlRef = null;\n    }\n  };\n</script>\n\n{#if url}\n  <img\n    src={url}\n    alt=\"\"\n    onload={handleImageLoad}\n    style:position=\"absolute\"\n    style:left={`${tileScreenRect.origin.x * relativeScale}px`}\n    style:top={`${tileScreenRect.origin.y * relativeScale}px`}\n    style:width={`${tileScreenRect.size.width * relativeScale}px`}\n    style:height={`${tileScreenRect.size.height * relativeScale}px`}\n    style:display=\"block\"\n  />\n{/if}\n","<script lang=\"ts\">\n  import type { Tile } from '@embedpdf/plugin-tiling';\n  import { useDocumentState } from '@embedpdf/core/svelte';\n  import type { HTMLAttributes } from 'svelte/elements';\n  import TileImg from './TileImg.svelte';\n  import { useTilingCapability } from '../hooks';\n\n  type TilingLayoutProps = HTMLAttributes<HTMLDivElement> & {\n    documentId: string;\n    pageIndex: number;\n    scale?: number;\n    class?: string;\n  };\n\n  let {\n    documentId,\n    pageIndex,\n    scale: scaleOverride,\n    class: propsClass,\n    ...restProps\n  }: TilingLayoutProps = $props();\n\n  const tilingCapability = useTilingCapability();\n  const documentState = useDocumentState(() => documentId);\n\n  let tiles = $state<Tile[]>([]);\n\n  const actualScale = $derived(\n    scaleOverride !== undefined ? scaleOverride : (documentState.current?.scale ?? 1),\n  );\n\n  $effect(() => {\n    if (!tilingCapability.provides) return;\n    return tilingCapability.provides.onTileRendering((event) => {\n      if (event.documentId === documentId) {\n        tiles = event.tiles[pageIndex] ?? [];\n      }\n    });\n  });\n</script>\n\n<div class={propsClass} {...restProps}>\n  {#each tiles as tile (tile.id)}\n    <TileImg {documentId} {pageIndex} {tile} dpr={window.devicePixelRatio} scale={actualScale} />\n  {/each}\n</div>\n"],"names":["useTilingCapability","useCapability","TilingPlugin","id","tilingCapability","scope","_a","provides","forDocument","url","$","state","urlRef","tileId","tileSrcScale","srcScale","tileScreenRect","screenRect","relativeScale","user_effect","_pageIndex","$$props","pageIndex","plainTile","untrack","createPlainTile","t","tile","pageRect","origin","x","y","size","width","height","task","get","renderTile","dpr","wait","blob","objectUrl","URL","createObjectURL","set","ignore","revokeObjectURL","abort","code","PdfErrorCode","Cancelled","message","handleImageLoad","img","root_1","set_attribute","styles","left","top","event","consequent","restProps","rest_props","documentState","useDocumentState","documentId","tiles","proxy","actualScale","derived","scale","current","onTileRendering","div","root","attribute_effect","$$anchor","TileImg","window","devicePixelRatio","usePlugin"],"mappings":"wjBAIaA,EAAsB,IAAMC,gBAA4BC,EAAAA,aAAaC,oECW1E,MAAAC,EAAmBJ,IAGnBK,uBAAiB,OAAA,OAAAC,EAAAF,EAAiBG,eAAjB,EAAAD,EAA2BE,4BAA2B,WAEzEC,EAAMC,EAAAC,MAAe,IAErBC,EAAwB,KAGtB,MAAAC,uBAAuBV,IACvBW,uBAA6BC,UAC7BC,uBAA+BC,YAC/BC,8BAAiCJ,IAevCJ,EAAAS,YAAO,WAEWN,SACVO,EAAUC,EAAAC,aAGZV,EAAM,iBAELP,GAAK,aAGJkB,EAAYC,EAAAA,QAAO,KAAOC,OAxBTC,EAwBwBL,EAAAM,KAxBxB,IACpBD,EACHE,SAAQ,CACNC,QAAUC,EAAGJ,EAAEE,SAASC,OAAOC,EAAGC,EAAGL,EAAEE,SAASC,OAAOE,GACvDC,MAAQC,MAAOP,EAAEE,SAASI,KAAKC,MAAOC,OAAQR,EAAEE,SAASI,KAAKE,SAEhEjB,WAAU,CACRY,QAAUC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