@embedpdf/plugin-tiling 1.5.0 → 2.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +137 -47
- package/dist/index.js.map +1 -1
- package/dist/lib/actions.d.ts +27 -7
- package/dist/lib/reducer.d.ts +2 -1
- package/dist/lib/tiling-plugin.d.ts +10 -5
- package/dist/lib/types.d.ts +14 -2
- package/dist/preact/index.cjs +1 -1
- package/dist/preact/index.cjs.map +1 -1
- package/dist/preact/index.js +30 -10
- package/dist/preact/index.js.map +1 -1
- package/dist/react/index.cjs +1 -1
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.js +30 -10
- package/dist/react/index.js.map +1 -1
- package/dist/shared/components/tile-img.d.ts +2 -1
- package/dist/shared/components/tiling-layer.d.ts +3 -2
- package/dist/shared-preact/components/tile-img.d.ts +2 -1
- package/dist/shared-preact/components/tiling-layer.d.ts +3 -2
- package/dist/shared-react/components/tile-img.d.ts +2 -1
- package/dist/shared-react/components/tiling-layer.d.ts +3 -2
- package/dist/svelte/components/TileImg.svelte.d.ts +1 -0
- package/dist/svelte/components/TilingLayer.svelte.d.ts +2 -1
- package/dist/svelte/index.cjs +1 -1
- package/dist/svelte/index.cjs.map +1 -1
- package/dist/svelte/index.js +35 -30
- package/dist/svelte/index.js.map +1 -1
- package/dist/vue/components/tile-img.vue.d.ts +3 -1
- package/dist/vue/components/tiling-layer.vue.d.ts +5 -3
- package/dist/vue/index.cjs +1 -1
- package/dist/vue/index.cjs.map +1 -1
- package/dist/vue/index.js +50 -24
- package/dist/vue/index.js.map +1 -1
- package/package.json +10 -10
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BasePlugin,
|
|
1
|
+
import { BasePlugin, PluginRegistry, RefreshPagesAction } from '@embedpdf/core';
|
|
2
2
|
import { TilingPluginConfig, TilingCapability, TilingState } from './types';
|
|
3
|
-
export declare class TilingPlugin extends BasePlugin<TilingPluginConfig, TilingCapability> {
|
|
3
|
+
export declare class TilingPlugin extends BasePlugin<TilingPluginConfig, TilingCapability, TilingState> {
|
|
4
4
|
static readonly id: "tiling";
|
|
5
5
|
private readonly tileRendering$;
|
|
6
6
|
private config;
|
|
@@ -8,11 +8,16 @@ export declare class TilingPlugin extends BasePlugin<TilingPluginConfig, TilingC
|
|
|
8
8
|
private scrollCapability;
|
|
9
9
|
private viewportCapability;
|
|
10
10
|
constructor(id: string, registry: PluginRegistry, config: TilingPluginConfig);
|
|
11
|
-
|
|
11
|
+
protected onDocumentLoadingStarted(documentId: string): void;
|
|
12
|
+
protected onDocumentClosed(documentId: string): void;
|
|
13
|
+
protected onScaleChanged(documentId: string): void;
|
|
14
|
+
protected onRotationChanged(documentId: string): void;
|
|
15
|
+
private recalculateTilesForDocument;
|
|
16
|
+
recalculateTiles(payload: RefreshPagesAction['payload']): Promise<void>;
|
|
12
17
|
initialize(): Promise<void>;
|
|
13
|
-
protected onCoreStoreUpdated(oldState: StoreState<CoreState>, newState: StoreState<CoreState>): void;
|
|
14
18
|
private calculateVisibleTiles;
|
|
15
|
-
onStoreUpdated(
|
|
19
|
+
onStoreUpdated(prevState: TilingState, newState: TilingState): void;
|
|
16
20
|
protected buildCapability(): TilingCapability;
|
|
21
|
+
private createTilingScope;
|
|
17
22
|
private renderTile;
|
|
18
23
|
}
|
package/dist/lib/types.d.ts
CHANGED
|
@@ -23,13 +23,25 @@ export interface Tile {
|
|
|
23
23
|
row: number;
|
|
24
24
|
id: string;
|
|
25
25
|
}
|
|
26
|
-
export interface
|
|
26
|
+
export interface TilingDocumentState {
|
|
27
27
|
visibleTiles: Record<number, Tile[]>;
|
|
28
28
|
}
|
|
29
|
-
export interface
|
|
29
|
+
export interface TilingState {
|
|
30
|
+
documents: Record<string, TilingDocumentState>;
|
|
31
|
+
}
|
|
32
|
+
export interface TilingEvent {
|
|
33
|
+
documentId: string;
|
|
34
|
+
tiles: Record<number, Tile[]>;
|
|
35
|
+
}
|
|
36
|
+
export interface TilingScope {
|
|
30
37
|
renderTile: (options: RenderTileOptions) => Task<Blob, PdfErrorReason>;
|
|
31
38
|
onTileRendering: EventHook<Record<number, Tile[]>>;
|
|
32
39
|
}
|
|
40
|
+
export interface TilingCapability {
|
|
41
|
+
renderTile: (options: RenderTileOptions, documentId?: string) => Task<Blob, PdfErrorReason>;
|
|
42
|
+
forDocument(documentId: string): TilingScope;
|
|
43
|
+
onTileRendering: EventHook<TilingEvent>;
|
|
44
|
+
}
|
|
33
45
|
export interface CalculateTilesForPageOptions {
|
|
34
46
|
tileSize: number;
|
|
35
47
|
overlapPx: number;
|
package/dist/preact/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("@embedpdf/core/preact"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("@embedpdf/core/preact"),t=require("@embedpdf/plugin-tiling"),r=require("preact/jsx-runtime");require("preact");const n=require("preact/hooks"),i=require("@embedpdf/models"),c=()=>e.useCapability(t.TilingPlugin.id);function o({documentId:e,pageIndex:t,tile:o,dpr:s,scale:l}){const{provides:u}=c(),d=n.useMemo(()=>null==u?void 0:u.forDocument(e),[u,e]),[a,p]=n.useState(),g=n.useRef(null),f=l/o.srcScale;n.useEffect(()=>{if("ready"===o.status&&g.current)return;if(!d)return;const e=d.renderTile({pageIndex:t,tile:o,dpr:s});return e.wait(e=>{const t=URL.createObjectURL(e);g.current=t,p(t)},i.ignore),()=>{g.current?(URL.revokeObjectURL(g.current),g.current=null):e.abort({code:i.PdfErrorCode.Cancelled,message:"canceled render task"})}},[d,t,o.id]);return a?r.jsx("img",{src:a,onLoad:()=>{g.current&&(URL.revokeObjectURL(g.current),g.current=null)},style:{position:"absolute",left:o.screenRect.origin.x*f,top:o.screenRect.origin.y*f,width:o.screenRect.size.width*f,height:o.screenRect.size.height*f,display:"block"}}):null}exports.TilingLayer=function({documentId:t,pageIndex:i,scale:s,style:l,...u}){const{provides:d}=c(),a=e.useDocumentState(t),[p,g]=n.useState([]);n.useEffect(()=>{if(d)return d.onTileRendering(e=>{e.documentId===t&&g(e.tiles[i]??[])})},[d,t,i]);const f=n.useMemo(()=>void 0!==s?s:(null==a?void 0:a.scale)??1,[s,null==a?void 0:a.scale]);return r.jsx("div",{style:{...l},...u,children:null==p?void 0:p.map(e=>r.jsx(o,{documentId:t,pageIndex:i,tile:e,dpr:window.devicePixelRatio,scale:f},e.id))})},exports.useTilingCapability=c,exports.useTilingPlugin=()=>e.usePlugin(t.TilingPlugin.id),Object.keys(t).forEach(e=>{"default"===e||Object.prototype.hasOwnProperty.call(exports,e)||Object.defineProperty(exports,e,{enumerable:!0,get:()=>t[e]})});
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../src/shared/hooks/use-tiling.ts","../../src/shared/components/tile-img.tsx","../../src/shared/components/tiling-layer.tsx"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/@framework';\nimport { TilingPlugin } from '@embedpdf/plugin-tiling';\n\nexport const useTilingPlugin = () => usePlugin<TilingPlugin>(TilingPlugin.id);\nexport const useTilingCapability = () => useCapability<TilingPlugin>(TilingPlugin.id);\n","import { ignore, PdfErrorCode } from '@embedpdf/models';\nimport { Tile } from '@embedpdf/plugin-tiling';\nimport { useEffect, useRef, useState } from '@framework';\n\nimport { useTilingCapability } from '../hooks/use-tiling';\n\ninterface TileImgProps {\n pageIndex: number;\n tile: Tile;\n dpr: number;\n scale: number;\n}\n\nexport function TileImg({ pageIndex, tile, dpr, scale }: TileImgProps) {\n const { provides: tilingCapability } = useTilingCapability();\n\n const [url, setUrl] = useState<string>();\n const urlRef = useRef<string | null>(null);\n\n const relativeScale = scale / tile.srcScale;\n\n /* kick off render exactly once per tile */\n useEffect(() => {\n if (tile.status === 'ready' && urlRef.current) return; // already done\n if (!
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/shared/hooks/use-tiling.ts","../../src/shared/components/tile-img.tsx","../../src/shared/components/tiling-layer.tsx"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/@framework';\nimport { TilingPlugin } from '@embedpdf/plugin-tiling';\n\nexport const useTilingPlugin = () => usePlugin<TilingPlugin>(TilingPlugin.id);\nexport const useTilingCapability = () => useCapability<TilingPlugin>(TilingPlugin.id);\n","import { ignore, PdfErrorCode } from '@embedpdf/models';\nimport { Tile } from '@embedpdf/plugin-tiling';\nimport { useEffect, useMemo, useRef, useState } from '@framework';\n\nimport { useTilingCapability } from '../hooks/use-tiling';\n\ninterface TileImgProps {\n documentId: string;\n pageIndex: number;\n tile: Tile;\n dpr: number;\n scale: number;\n}\n\nexport function TileImg({ documentId, pageIndex, tile, dpr, scale }: TileImgProps) {\n const { provides: tilingCapability } = useTilingCapability();\n const scope = useMemo(\n () => tilingCapability?.forDocument(documentId),\n [tilingCapability, documentId],\n );\n\n const [url, setUrl] = useState<string>();\n const urlRef = useRef<string | null>(null);\n\n const relativeScale = scale / tile.srcScale;\n\n /* kick off render exactly once per tile */\n useEffect(() => {\n if (tile.status === 'ready' && urlRef.current) return; // already done\n if (!scope) return;\n const task = scope.renderTile({ pageIndex, tile, dpr });\n task.wait((blob) => {\n const objectUrl = URL.createObjectURL(blob);\n urlRef.current = objectUrl;\n setUrl(objectUrl);\n }, ignore);\n\n return () => {\n if (urlRef.current) {\n URL.revokeObjectURL(urlRef.current);\n urlRef.current = null;\n } else {\n task.abort({\n code: PdfErrorCode.Cancelled,\n message: 'canceled render task',\n });\n }\n };\n }, [scope, pageIndex, tile.id]); // id includes scale, so unique\n\n const handleImageLoad = () => {\n if (urlRef.current) {\n URL.revokeObjectURL(urlRef.current);\n urlRef.current = null;\n }\n };\n\n if (!url) return null; // could render a placeholder\n return (\n <img\n src={url}\n onLoad={handleImageLoad}\n style={{\n position: 'absolute',\n left: tile.screenRect.origin.x * relativeScale,\n top: tile.screenRect.origin.y * relativeScale,\n width: tile.screenRect.size.width * relativeScale,\n height: tile.screenRect.size.height * relativeScale,\n display: 'block',\n }}\n />\n );\n}\n","import { Tile } from '@embedpdf/plugin-tiling';\nimport { useEffect, useState, HTMLAttributes, CSSProperties, useMemo } from '@framework';\n\nimport { TileImg } from './tile-img';\nimport { useTilingCapability } from '../hooks/use-tiling';\nimport { useDocumentState } from '@embedpdf/core/@framework';\n\ntype TilingLayoutProps = Omit<HTMLAttributes<HTMLDivElement>, 'style'> & {\n documentId: string;\n pageIndex: number;\n scale?: number;\n style?: CSSProperties;\n};\n\nexport function TilingLayer({\n documentId,\n pageIndex,\n scale: scaleOverride,\n style,\n ...props\n}: TilingLayoutProps) {\n const { provides: tilingProvides } = useTilingCapability();\n const documentState = useDocumentState(documentId);\n const [tiles, setTiles] = useState<Tile[]>([]);\n\n useEffect(() => {\n if (tilingProvides) {\n return tilingProvides.onTileRendering((event) => {\n if (event.documentId === documentId) {\n setTiles(event.tiles[pageIndex] ?? []);\n }\n });\n }\n }, [tilingProvides, documentId, pageIndex]);\n\n const actualScale = useMemo(() => {\n if (scaleOverride !== undefined) return scaleOverride;\n return documentState?.scale ?? 1;\n }, [scaleOverride, documentState?.scale]);\n\n return (\n <div\n style={{\n ...style,\n }}\n {...props}\n >\n {tiles?.map((tile) => (\n <TileImg\n key={tile.id}\n documentId={documentId}\n pageIndex={pageIndex}\n tile={tile}\n dpr={window.devicePixelRatio}\n scale={actualScale}\n />\n ))}\n </div>\n );\n}\n"],"names":["useTilingCapability","useCapability","TilingPlugin","id","TileImg","documentId","pageIndex","tile","dpr","scale","provides","tilingCapability","scope","useMemo","forDocument","url","setUrl","useState","urlRef","useRef","relativeScale","srcScale","useEffect","status","current","task","renderTile","wait","blob","objectUrl","URL","createObjectURL","ignore","revokeObjectURL","abort","code","PdfErrorCode","Cancelled","message","jsx","src","onLoad","style","position","left","screenRect","origin","x","top","y","width","size","height","display","scaleOverride","props","tilingProvides","documentState","useDocumentState","tiles","setTiles","onTileRendering","event","actualScale","children","map","window","devicePixelRatio","usePlugin"],"mappings":"8QAIaA,EAAsB,IAAMC,gBAA4BC,EAAAA,aAAaC,ICU3E,SAASC,GAAQC,WAAEA,EAAAC,UAAYA,OAAWC,EAAAC,IAAMA,EAAAC,MAAKA,IAC1D,MAAQC,SAAUC,GAAqBX,IACjCY,EAAQC,EAAAA,QACZ,UAAMF,WAAkBG,YAAYT,GACpC,CAACM,EAAkBN,KAGdU,EAAKC,GAAUC,aAChBC,EAASC,EAAAA,OAAsB,MAE/BC,EAAgBX,EAAQF,EAAKc,SAGnCC,EAAAA,UAAU,KACR,GAAoB,UAAhBf,EAAKgB,QAAsBL,EAAOM,QAAS,OAC/C,IAAKZ,EAAO,OACZ,MAAMa,EAAOb,EAAMc,WAAW,CAAEpB,YAAWC,OAAMC,QAOjD,OANAiB,EAAKE,KAAMC,IACT,MAAMC,EAAYC,IAAIC,gBAAgBH,GACtCV,EAAOM,QAAUK,EACjBb,EAAOa,IACNG,EAAAA,QAEI,KACDd,EAAOM,SACTM,IAAIG,gBAAgBf,EAAOM,SAC3BN,EAAOM,QAAU,MAEjBC,EAAKS,MAAM,CACTC,KAAMC,EAAAA,aAAaC,UACnBC,QAAS,2BAId,CAAC1B,EAAON,EAAWC,EAAKJ,KAS3B,OAAKY,EAEHwB,EAAAA,IAAC,MAAA,CACCC,IAAKzB,EACL0B,OAXoB,KAClBvB,EAAOM,UACTM,IAAIG,gBAAgBf,EAAOM,SAC3BN,EAAOM,QAAU,OASjBkB,MAAO,CACLC,SAAU,WACVC,KAAMrC,EAAKsC,WAAWC,OAAOC,EAAI3B,EACjC4B,IAAKzC,EAAKsC,WAAWC,OAAOG,EAAI7B,EAChC8B,MAAO3C,EAAKsC,WAAWM,KAAKD,MAAQ9B,EACpCgC,OAAQ7C,EAAKsC,WAAWM,KAAKC,OAAShC,EACtCiC,QAAS,WAXE,IAenB,qBC1DO,UAAqBhD,WAC1BA,EAAAC,UACAA,EACAG,MAAO6C,EAAAZ,MACPA,KACGa,IAEH,MAAQ7C,SAAU8C,GAAmBxD,IAC/ByD,EAAgBC,EAAAA,iBAAiBrD,IAChCsD,EAAOC,GAAY3C,EAAAA,SAAiB,IAE3CK,EAAAA,UAAU,KACR,GAAIkC,EACF,OAAOA,EAAeK,gBAAiBC,IACjCA,EAAMzD,aAAeA,GACvBuD,EAASE,EAAMH,MAAMrD,IAAc,OAIxC,CAACkD,EAAgBnD,EAAYC,IAEhC,MAAMyD,EAAclD,EAAAA,QAAQ,SACJ,IAAlByC,EAAoCA,SACjCG,WAAehD,QAAS,EAC9B,CAAC6C,EAAe,MAAAG,OAAA,EAAAA,EAAehD,QAElC,OACE8B,EAAAA,IAAC,MAAA,CACCG,MAAO,IACFA,MAEDa,EAEHS,SAAA,MAAAL,OAAA,EAAAA,EAAOM,IAAK1D,GACXgC,EAAAA,IAACnC,EAAA,CAECC,aACAC,YACAC,OACAC,IAAK0D,OAAOC,iBACZ1D,MAAOsD,GALFxD,EAAKJ,MAUpB,wDFxD+B,IAAMiE,YAAwBlE,EAAAA,aAAaC"}
|
package/dist/preact/index.js
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
|
-
import { usePlugin, useCapability } from "@embedpdf/core/preact";
|
|
1
|
+
import { usePlugin, useCapability, useDocumentState } from "@embedpdf/core/preact";
|
|
2
2
|
import { TilingPlugin } from "@embedpdf/plugin-tiling";
|
|
3
3
|
export * from "@embedpdf/plugin-tiling";
|
|
4
4
|
import { jsx } from "preact/jsx-runtime";
|
|
5
5
|
import "preact";
|
|
6
|
-
import { useState, useRef, useEffect } from "preact/hooks";
|
|
6
|
+
import { useMemo, useState, useRef, useEffect } from "preact/hooks";
|
|
7
7
|
import { ignore, PdfErrorCode } from "@embedpdf/models";
|
|
8
8
|
const useTilingPlugin = () => usePlugin(TilingPlugin.id);
|
|
9
9
|
const useTilingCapability = () => useCapability(TilingPlugin.id);
|
|
10
|
-
function TileImg({ pageIndex, tile, dpr, scale }) {
|
|
10
|
+
function TileImg({ documentId, pageIndex, tile, dpr, scale }) {
|
|
11
11
|
const { provides: tilingCapability } = useTilingCapability();
|
|
12
|
+
const scope = useMemo(
|
|
13
|
+
() => tilingCapability == null ? void 0 : tilingCapability.forDocument(documentId),
|
|
14
|
+
[tilingCapability, documentId]
|
|
15
|
+
);
|
|
12
16
|
const [url, setUrl] = useState();
|
|
13
17
|
const urlRef = useRef(null);
|
|
14
18
|
const relativeScale = scale / tile.srcScale;
|
|
15
19
|
useEffect(() => {
|
|
16
20
|
if (tile.status === "ready" && urlRef.current) return;
|
|
17
|
-
if (!
|
|
18
|
-
const task =
|
|
21
|
+
if (!scope) return;
|
|
22
|
+
const task = scope.renderTile({ pageIndex, tile, dpr });
|
|
19
23
|
task.wait((blob) => {
|
|
20
24
|
const objectUrl = URL.createObjectURL(blob);
|
|
21
25
|
urlRef.current = objectUrl;
|
|
@@ -32,7 +36,7 @@ function TileImg({ pageIndex, tile, dpr, scale }) {
|
|
|
32
36
|
});
|
|
33
37
|
}
|
|
34
38
|
};
|
|
35
|
-
}, [pageIndex, tile.id]);
|
|
39
|
+
}, [scope, pageIndex, tile.id]);
|
|
36
40
|
const handleImageLoad = () => {
|
|
37
41
|
if (urlRef.current) {
|
|
38
42
|
URL.revokeObjectURL(urlRef.current);
|
|
@@ -56,14 +60,29 @@ function TileImg({ pageIndex, tile, dpr, scale }) {
|
|
|
56
60
|
}
|
|
57
61
|
);
|
|
58
62
|
}
|
|
59
|
-
function TilingLayer({
|
|
63
|
+
function TilingLayer({
|
|
64
|
+
documentId,
|
|
65
|
+
pageIndex,
|
|
66
|
+
scale: scaleOverride,
|
|
67
|
+
style,
|
|
68
|
+
...props
|
|
69
|
+
}) {
|
|
60
70
|
const { provides: tilingProvides } = useTilingCapability();
|
|
71
|
+
const documentState = useDocumentState(documentId);
|
|
61
72
|
const [tiles, setTiles] = useState([]);
|
|
62
73
|
useEffect(() => {
|
|
63
74
|
if (tilingProvides) {
|
|
64
|
-
return tilingProvides.onTileRendering((
|
|
75
|
+
return tilingProvides.onTileRendering((event) => {
|
|
76
|
+
if (event.documentId === documentId) {
|
|
77
|
+
setTiles(event.tiles[pageIndex] ?? []);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
65
80
|
}
|
|
66
|
-
}, [tilingProvides]);
|
|
81
|
+
}, [tilingProvides, documentId, pageIndex]);
|
|
82
|
+
const actualScale = useMemo(() => {
|
|
83
|
+
if (scaleOverride !== void 0) return scaleOverride;
|
|
84
|
+
return (documentState == null ? void 0 : documentState.scale) ?? 1;
|
|
85
|
+
}, [scaleOverride, documentState == null ? void 0 : documentState.scale]);
|
|
67
86
|
return /* @__PURE__ */ jsx(
|
|
68
87
|
"div",
|
|
69
88
|
{
|
|
@@ -74,10 +93,11 @@ function TilingLayer({ pageIndex, scale, style, ...props }) {
|
|
|
74
93
|
children: tiles == null ? void 0 : tiles.map((tile) => /* @__PURE__ */ jsx(
|
|
75
94
|
TileImg,
|
|
76
95
|
{
|
|
96
|
+
documentId,
|
|
77
97
|
pageIndex,
|
|
78
98
|
tile,
|
|
79
99
|
dpr: window.devicePixelRatio,
|
|
80
|
-
scale
|
|
100
|
+
scale: actualScale
|
|
81
101
|
},
|
|
82
102
|
tile.id
|
|
83
103
|
))
|
package/dist/preact/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/shared/hooks/use-tiling.ts","../../src/shared/components/tile-img.tsx","../../src/shared/components/tiling-layer.tsx"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/@framework';\nimport { TilingPlugin } from '@embedpdf/plugin-tiling';\n\nexport const useTilingPlugin = () => usePlugin<TilingPlugin>(TilingPlugin.id);\nexport const useTilingCapability = () => useCapability<TilingPlugin>(TilingPlugin.id);\n","import { ignore, PdfErrorCode } from '@embedpdf/models';\nimport { Tile } from '@embedpdf/plugin-tiling';\nimport { useEffect, useRef, useState } from '@framework';\n\nimport { useTilingCapability } from '../hooks/use-tiling';\n\ninterface TileImgProps {\n pageIndex: number;\n tile: Tile;\n dpr: number;\n scale: number;\n}\n\nexport function TileImg({ pageIndex, tile, dpr, scale }: TileImgProps) {\n const { provides: tilingCapability } = useTilingCapability();\n\n const [url, setUrl] = useState<string>();\n const urlRef = useRef<string | null>(null);\n\n const relativeScale = scale / tile.srcScale;\n\n /* kick off render exactly once per tile */\n useEffect(() => {\n if (tile.status === 'ready' && urlRef.current) return; // already done\n if (!
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/shared/hooks/use-tiling.ts","../../src/shared/components/tile-img.tsx","../../src/shared/components/tiling-layer.tsx"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/@framework';\nimport { TilingPlugin } from '@embedpdf/plugin-tiling';\n\nexport const useTilingPlugin = () => usePlugin<TilingPlugin>(TilingPlugin.id);\nexport const useTilingCapability = () => useCapability<TilingPlugin>(TilingPlugin.id);\n","import { ignore, PdfErrorCode } from '@embedpdf/models';\nimport { Tile } from '@embedpdf/plugin-tiling';\nimport { useEffect, useMemo, useRef, useState } from '@framework';\n\nimport { useTilingCapability } from '../hooks/use-tiling';\n\ninterface TileImgProps {\n documentId: string;\n pageIndex: number;\n tile: Tile;\n dpr: number;\n scale: number;\n}\n\nexport function TileImg({ documentId, pageIndex, tile, dpr, scale }: TileImgProps) {\n const { provides: tilingCapability } = useTilingCapability();\n const scope = useMemo(\n () => tilingCapability?.forDocument(documentId),\n [tilingCapability, documentId],\n );\n\n const [url, setUrl] = useState<string>();\n const urlRef = useRef<string | null>(null);\n\n const relativeScale = scale / tile.srcScale;\n\n /* kick off render exactly once per tile */\n useEffect(() => {\n if (tile.status === 'ready' && urlRef.current) return; // already done\n if (!scope) return;\n const task = scope.renderTile({ pageIndex, tile, dpr });\n task.wait((blob) => {\n const objectUrl = URL.createObjectURL(blob);\n urlRef.current = objectUrl;\n setUrl(objectUrl);\n }, ignore);\n\n return () => {\n if (urlRef.current) {\n URL.revokeObjectURL(urlRef.current);\n urlRef.current = null;\n } else {\n task.abort({\n code: PdfErrorCode.Cancelled,\n message: 'canceled render task',\n });\n }\n };\n }, [scope, pageIndex, tile.id]); // id includes scale, so unique\n\n const handleImageLoad = () => {\n if (urlRef.current) {\n URL.revokeObjectURL(urlRef.current);\n urlRef.current = null;\n }\n };\n\n if (!url) return null; // could render a placeholder\n return (\n <img\n src={url}\n onLoad={handleImageLoad}\n style={{\n position: 'absolute',\n left: tile.screenRect.origin.x * relativeScale,\n top: tile.screenRect.origin.y * relativeScale,\n width: tile.screenRect.size.width * relativeScale,\n height: tile.screenRect.size.height * relativeScale,\n display: 'block',\n }}\n />\n );\n}\n","import { Tile } from '@embedpdf/plugin-tiling';\nimport { useEffect, useState, HTMLAttributes, CSSProperties, useMemo } from '@framework';\n\nimport { TileImg } from './tile-img';\nimport { useTilingCapability } from '../hooks/use-tiling';\nimport { useDocumentState } from '@embedpdf/core/@framework';\n\ntype TilingLayoutProps = Omit<HTMLAttributes<HTMLDivElement>, 'style'> & {\n documentId: string;\n pageIndex: number;\n scale?: number;\n style?: CSSProperties;\n};\n\nexport function TilingLayer({\n documentId,\n pageIndex,\n scale: scaleOverride,\n style,\n ...props\n}: TilingLayoutProps) {\n const { provides: tilingProvides } = useTilingCapability();\n const documentState = useDocumentState(documentId);\n const [tiles, setTiles] = useState<Tile[]>([]);\n\n useEffect(() => {\n if (tilingProvides) {\n return tilingProvides.onTileRendering((event) => {\n if (event.documentId === documentId) {\n setTiles(event.tiles[pageIndex] ?? []);\n }\n });\n }\n }, [tilingProvides, documentId, pageIndex]);\n\n const actualScale = useMemo(() => {\n if (scaleOverride !== undefined) return scaleOverride;\n return documentState?.scale ?? 1;\n }, [scaleOverride, documentState?.scale]);\n\n return (\n <div\n style={{\n ...style,\n }}\n {...props}\n >\n {tiles?.map((tile) => (\n <TileImg\n key={tile.id}\n documentId={documentId}\n pageIndex={pageIndex}\n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ED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("@embedpdf/core/react"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("@embedpdf/core/react"),t=require("@embedpdf/plugin-tiling"),r=require("react/jsx-runtime"),n=require("react"),i=require("@embedpdf/models"),c=()=>e.useCapability(t.TilingPlugin.id);function l({documentId:e,pageIndex:t,tile:l,dpr:o,scale:s}){const{provides:u}=c(),d=n.useMemo(()=>null==u?void 0:u.forDocument(e),[u,e]),[a,p]=n.useState(),g=n.useRef(null),f=s/l.srcScale;n.useEffect(()=>{if("ready"===l.status&&g.current)return;if(!d)return;const e=d.renderTile({pageIndex:t,tile:l,dpr:o});return e.wait(e=>{const t=URL.createObjectURL(e);g.current=t,p(t)},i.ignore),()=>{g.current?(URL.revokeObjectURL(g.current),g.current=null):e.abort({code:i.PdfErrorCode.Cancelled,message:"canceled render task"})}},[d,t,l.id]);return a?r.jsx("img",{src:a,onLoad:()=>{g.current&&(URL.revokeObjectURL(g.current),g.current=null)},style:{position:"absolute",left:l.screenRect.origin.x*f,top:l.screenRect.origin.y*f,width:l.screenRect.size.width*f,height:l.screenRect.size.height*f,display:"block"}}):null}exports.TilingLayer=function({documentId:t,pageIndex:i,scale:o,style:s,...u}){const{provides:d}=c(),a=e.useDocumentState(t),[p,g]=n.useState([]);n.useEffect(()=>{if(d)return d.onTileRendering(e=>{e.documentId===t&&g(e.tiles[i]??[])})},[d,t,i]);const f=n.useMemo(()=>void 0!==o?o:(null==a?void 0:a.scale)??1,[o,null==a?void 0:a.scale]);return r.jsx("div",{style:{...s},...u,children:null==p?void 0:p.map(e=>r.jsx(l,{documentId:t,pageIndex:i,tile:e,dpr:window.devicePixelRatio,scale:f},e.id))})},exports.useTilingCapability=c,exports.useTilingPlugin=()=>e.usePlugin(t.TilingPlugin.id),Object.keys(t).forEach(e=>{"default"===e||Object.prototype.hasOwnProperty.call(exports,e)||Object.defineProperty(exports,e,{enumerable:!0,get:()=>t[e]})});
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/react/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../src/shared/hooks/use-tiling.ts","../../src/shared/components/tile-img.tsx","../../src/shared/components/tiling-layer.tsx"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/@framework';\nimport { TilingPlugin } from '@embedpdf/plugin-tiling';\n\nexport const useTilingPlugin = () => usePlugin<TilingPlugin>(TilingPlugin.id);\nexport const useTilingCapability = () => useCapability<TilingPlugin>(TilingPlugin.id);\n","import { ignore, PdfErrorCode } from '@embedpdf/models';\nimport { Tile } from '@embedpdf/plugin-tiling';\nimport { useEffect, useRef, useState } from '@framework';\n\nimport { useTilingCapability } from '../hooks/use-tiling';\n\ninterface TileImgProps {\n pageIndex: number;\n tile: Tile;\n dpr: number;\n scale: number;\n}\n\nexport function TileImg({ pageIndex, tile, dpr, scale }: TileImgProps) {\n const { provides: tilingCapability } = useTilingCapability();\n\n const [url, setUrl] = useState<string>();\n const urlRef = useRef<string | null>(null);\n\n const relativeScale = scale / tile.srcScale;\n\n /* kick off render exactly once per tile */\n useEffect(() => {\n if (tile.status === 'ready' && urlRef.current) return; // already done\n if (!
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/shared/hooks/use-tiling.ts","../../src/shared/components/tile-img.tsx","../../src/shared/components/tiling-layer.tsx"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/@framework';\nimport { TilingPlugin } from '@embedpdf/plugin-tiling';\n\nexport const useTilingPlugin = () => usePlugin<TilingPlugin>(TilingPlugin.id);\nexport const useTilingCapability = () => useCapability<TilingPlugin>(TilingPlugin.id);\n","import { ignore, PdfErrorCode } from '@embedpdf/models';\nimport { Tile } from '@embedpdf/plugin-tiling';\nimport { useEffect, useMemo, useRef, useState } from '@framework';\n\nimport { useTilingCapability } from '../hooks/use-tiling';\n\ninterface TileImgProps {\n documentId: string;\n pageIndex: number;\n tile: Tile;\n dpr: number;\n scale: number;\n}\n\nexport function TileImg({ documentId, pageIndex, tile, dpr, scale }: TileImgProps) {\n const { provides: tilingCapability } = useTilingCapability();\n const scope = useMemo(\n () => tilingCapability?.forDocument(documentId),\n [tilingCapability, documentId],\n );\n\n const [url, setUrl] = useState<string>();\n const urlRef = useRef<string | null>(null);\n\n const relativeScale = scale / tile.srcScale;\n\n /* kick off render exactly once per tile */\n useEffect(() => {\n if (tile.status === 'ready' && urlRef.current) return; // already done\n if (!scope) return;\n const task = scope.renderTile({ pageIndex, tile, dpr });\n task.wait((blob) => {\n const objectUrl = URL.createObjectURL(blob);\n urlRef.current = objectUrl;\n setUrl(objectUrl);\n }, ignore);\n\n return () => {\n if (urlRef.current) {\n URL.revokeObjectURL(urlRef.current);\n urlRef.current = null;\n } else {\n task.abort({\n code: PdfErrorCode.Cancelled,\n message: 'canceled render task',\n });\n }\n };\n }, [scope, pageIndex, tile.id]); // id includes scale, so unique\n\n const handleImageLoad = () => {\n if (urlRef.current) {\n URL.revokeObjectURL(urlRef.current);\n urlRef.current = null;\n }\n };\n\n if (!url) return null; // could render a placeholder\n return (\n <img\n src={url}\n onLoad={handleImageLoad}\n style={{\n position: 'absolute',\n left: tile.screenRect.origin.x * relativeScale,\n top: tile.screenRect.origin.y * relativeScale,\n width: tile.screenRect.size.width * relativeScale,\n height: tile.screenRect.size.height * relativeScale,\n display: 'block',\n }}\n />\n );\n}\n","import { Tile } from '@embedpdf/plugin-tiling';\nimport { useEffect, useState, HTMLAttributes, CSSProperties, useMemo } from '@framework';\n\nimport { TileImg } from './tile-img';\nimport { useTilingCapability } from '../hooks/use-tiling';\nimport { useDocumentState } from '@embedpdf/core/@framework';\n\ntype TilingLayoutProps = Omit<HTMLAttributes<HTMLDivElement>, 'style'> & {\n documentId: string;\n pageIndex: number;\n scale?: number;\n style?: CSSProperties;\n};\n\nexport function TilingLayer({\n documentId,\n pageIndex,\n scale: scaleOverride,\n style,\n ...props\n}: TilingLayoutProps) {\n const { provides: tilingProvides } = useTilingCapability();\n const documentState = useDocumentState(documentId);\n const [tiles, setTiles] = useState<Tile[]>([]);\n\n useEffect(() => {\n if (tilingProvides) {\n return tilingProvides.onTileRendering((event) => {\n if (event.documentId === documentId) {\n setTiles(event.tiles[pageIndex] ?? []);\n }\n });\n }\n }, [tilingProvides, documentId, pageIndex]);\n\n const actualScale = useMemo(() => {\n if (scaleOverride !== undefined) return scaleOverride;\n return documentState?.scale ?? 1;\n }, [scaleOverride, documentState?.scale]);\n\n return (\n <div\n style={{\n ...style,\n }}\n {...props}\n >\n {tiles?.map((tile) => (\n <TileImg\n key={tile.id}\n documentId={documentId}\n pageIndex={pageIndex}\n tile={tile}\n dpr={window.devicePixelRatio}\n scale={actualScale}\n />\n ))}\n </div>\n );\n}\n"],"names":["useTilingCapability","useCapability","TilingPlugin","id","TileImg","documentId","pageIndex","tile","dpr","scale","provides","tilingCapability","scope","useMemo","forDocument","url","setUrl","useState","urlRef","useRef","relativeScale","srcScale","useEffect","status","current","task","renderTile","wait","blob","objectUrl","URL","createObjectURL","ignore","revokeObjectURL","abort","code","PdfErrorCode","Cancelled","message","jsx","src","onLoad","style","position","left","screenRect","origin","x","top","y","width","size","height","display","scaleOverride","props","tilingProvides","documentState","useDocumentState","tiles","setTiles","onTileRendering","event","actualScale","children","map","window","devicePixelRatio","usePlugin"],"mappings":"6OAIaA,EAAsB,IAAMC,gBAA4BC,EAAAA,aAAaC,ICU3E,SAASC,GAAQC,WAAEA,EAAAC,UAAYA,OAAWC,EAAAC,IAAMA,EAAAC,MAAKA,IAC1D,MAAQC,SAAUC,GAAqBX,IACjCY,EAAQC,EAAAA,QACZ,UAAMF,WAAkBG,YAAYT,GACpC,CAACM,EAAkBN,KAGdU,EAAKC,GAAUC,aAChBC,EAASC,EAAAA,OAAsB,MAE/BC,EAAgBX,EAAQF,EAAKc,SAGnCC,EA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AAC6C,EAAe,MAAAG,OAAA,EAAAA,EAAehD,QAElC,OACE8B,EAAAA,IAAC,MAAA,CACCG,MAAO,IACFA,MAEDa,EAEHS,SAAA,MAAAL,OAAA,EAAAA,EAAOM,IAAK1D,GACXgC,EAAAA,IAACnC,EAAA,CAECC,aACAC,YACAC,OACAC,IAAK0D,OAAOC,iBACZ1D,MAAOsD,GALFxD,EAAKJ,MAUpB,wDFxD+B,IAAMiE,YAAwBlE,EAAAA,aAAaC"}
|
package/dist/react/index.js
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
import { usePlugin, useCapability } from "@embedpdf/core/react";
|
|
1
|
+
import { usePlugin, useCapability, useDocumentState } from "@embedpdf/core/react";
|
|
2
2
|
import { TilingPlugin } from "@embedpdf/plugin-tiling";
|
|
3
3
|
export * from "@embedpdf/plugin-tiling";
|
|
4
4
|
import { jsx } from "react/jsx-runtime";
|
|
5
|
-
import { useState, useRef, useEffect } from "react";
|
|
5
|
+
import { useMemo, useState, useRef, useEffect } from "react";
|
|
6
6
|
import { ignore, PdfErrorCode } from "@embedpdf/models";
|
|
7
7
|
const useTilingPlugin = () => usePlugin(TilingPlugin.id);
|
|
8
8
|
const useTilingCapability = () => useCapability(TilingPlugin.id);
|
|
9
|
-
function TileImg({ pageIndex, tile, dpr, scale }) {
|
|
9
|
+
function TileImg({ documentId, pageIndex, tile, dpr, scale }) {
|
|
10
10
|
const { provides: tilingCapability } = useTilingCapability();
|
|
11
|
+
const scope = useMemo(
|
|
12
|
+
() => tilingCapability == null ? void 0 : tilingCapability.forDocument(documentId),
|
|
13
|
+
[tilingCapability, documentId]
|
|
14
|
+
);
|
|
11
15
|
const [url, setUrl] = useState();
|
|
12
16
|
const urlRef = useRef(null);
|
|
13
17
|
const relativeScale = scale / tile.srcScale;
|
|
14
18
|
useEffect(() => {
|
|
15
19
|
if (tile.status === "ready" && urlRef.current) return;
|
|
16
|
-
if (!
|
|
17
|
-
const task =
|
|
20
|
+
if (!scope) return;
|
|
21
|
+
const task = scope.renderTile({ pageIndex, tile, dpr });
|
|
18
22
|
task.wait((blob) => {
|
|
19
23
|
const objectUrl = URL.createObjectURL(blob);
|
|
20
24
|
urlRef.current = objectUrl;
|
|
@@ -31,7 +35,7 @@ function TileImg({ pageIndex, tile, dpr, scale }) {
|
|
|
31
35
|
});
|
|
32
36
|
}
|
|
33
37
|
};
|
|
34
|
-
}, [pageIndex, tile.id]);
|
|
38
|
+
}, [scope, pageIndex, tile.id]);
|
|
35
39
|
const handleImageLoad = () => {
|
|
36
40
|
if (urlRef.current) {
|
|
37
41
|
URL.revokeObjectURL(urlRef.current);
|
|
@@ -55,14 +59,29 @@ function TileImg({ pageIndex, tile, dpr, scale }) {
|
|
|
55
59
|
}
|
|
56
60
|
);
|
|
57
61
|
}
|
|
58
|
-
function TilingLayer({
|
|
62
|
+
function TilingLayer({
|
|
63
|
+
documentId,
|
|
64
|
+
pageIndex,
|
|
65
|
+
scale: scaleOverride,
|
|
66
|
+
style,
|
|
67
|
+
...props
|
|
68
|
+
}) {
|
|
59
69
|
const { provides: tilingProvides } = useTilingCapability();
|
|
70
|
+
const documentState = useDocumentState(documentId);
|
|
60
71
|
const [tiles, setTiles] = useState([]);
|
|
61
72
|
useEffect(() => {
|
|
62
73
|
if (tilingProvides) {
|
|
63
|
-
return tilingProvides.onTileRendering((
|
|
74
|
+
return tilingProvides.onTileRendering((event) => {
|
|
75
|
+
if (event.documentId === documentId) {
|
|
76
|
+
setTiles(event.tiles[pageIndex] ?? []);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
64
79
|
}
|
|
65
|
-
}, [tilingProvides]);
|
|
80
|
+
}, [tilingProvides, documentId, pageIndex]);
|
|
81
|
+
const actualScale = useMemo(() => {
|
|
82
|
+
if (scaleOverride !== void 0) return scaleOverride;
|
|
83
|
+
return (documentState == null ? void 0 : documentState.scale) ?? 1;
|
|
84
|
+
}, [scaleOverride, documentState == null ? void 0 : documentState.scale]);
|
|
66
85
|
return /* @__PURE__ */ jsx(
|
|
67
86
|
"div",
|
|
68
87
|
{
|
|
@@ -73,10 +92,11 @@ function TilingLayer({ pageIndex, scale, style, ...props }) {
|
|
|
73
92
|
children: tiles == null ? void 0 : tiles.map((tile) => /* @__PURE__ */ jsx(
|
|
74
93
|
TileImg,
|
|
75
94
|
{
|
|
95
|
+
documentId,
|
|
76
96
|
pageIndex,
|
|
77
97
|
tile,
|
|
78
98
|
dpr: window.devicePixelRatio,
|
|
79
|
-
scale
|
|
99
|
+
scale: actualScale
|
|
80
100
|
},
|
|
81
101
|
tile.id
|
|
82
102
|
))
|
package/dist/react/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/shared/hooks/use-tiling.ts","../../src/shared/components/tile-img.tsx","../../src/shared/components/tiling-layer.tsx"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/@framework';\nimport { TilingPlugin } from '@embedpdf/plugin-tiling';\n\nexport const useTilingPlugin = () => usePlugin<TilingPlugin>(TilingPlugin.id);\nexport const useTilingCapability = () => useCapability<TilingPlugin>(TilingPlugin.id);\n","import { ignore, PdfErrorCode } from '@embedpdf/models';\nimport { Tile } from '@embedpdf/plugin-tiling';\nimport { useEffect, useRef, useState } from '@framework';\n\nimport { useTilingCapability } from '../hooks/use-tiling';\n\ninterface TileImgProps {\n pageIndex: number;\n tile: Tile;\n dpr: number;\n scale: number;\n}\n\nexport function TileImg({ pageIndex, tile, dpr, scale }: TileImgProps) {\n const { provides: tilingCapability } = useTilingCapability();\n\n const [url, setUrl] = useState<string>();\n const urlRef = useRef<string | null>(null);\n\n const relativeScale = scale / tile.srcScale;\n\n /* kick off render exactly once per tile */\n useEffect(() => {\n if (tile.status === 'ready' && urlRef.current) return; // already done\n if (!
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/shared/hooks/use-tiling.ts","../../src/shared/components/tile-img.tsx","../../src/shared/components/tiling-layer.tsx"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/@framework';\nimport { TilingPlugin } from '@embedpdf/plugin-tiling';\n\nexport const useTilingPlugin = () => usePlugin<TilingPlugin>(TilingPlugin.id);\nexport const useTilingCapability = () => useCapability<TilingPlugin>(TilingPlugin.id);\n","import { ignore, PdfErrorCode } from '@embedpdf/models';\nimport { Tile } from '@embedpdf/plugin-tiling';\nimport { useEffect, useMemo, useRef, useState } from '@framework';\n\nimport { useTilingCapability } from '../hooks/use-tiling';\n\ninterface TileImgProps {\n documentId: string;\n pageIndex: number;\n tile: Tile;\n dpr: number;\n scale: number;\n}\n\nexport function TileImg({ documentId, pageIndex, tile, dpr, scale }: TileImgProps) {\n const { provides: tilingCapability } = useTilingCapability();\n const scope = useMemo(\n () => tilingCapability?.forDocument(documentId),\n [tilingCapability, documentId],\n );\n\n const [url, setUrl] = useState<string>();\n const urlRef = useRef<string | null>(null);\n\n const relativeScale = scale / tile.srcScale;\n\n /* kick off render exactly once per tile */\n useEffect(() => {\n if (tile.status === 'ready' && urlRef.current) return; // already done\n if (!scope) return;\n const task = scope.renderTile({ pageIndex, tile, dpr });\n task.wait((blob) => {\n const objectUrl = URL.createObjectURL(blob);\n urlRef.current = objectUrl;\n setUrl(objectUrl);\n }, ignore);\n\n return () => {\n if (urlRef.current) {\n URL.revokeObjectURL(urlRef.current);\n urlRef.current = null;\n } else {\n task.abort({\n code: PdfErrorCode.Cancelled,\n message: 'canceled render task',\n });\n }\n };\n }, [scope, pageIndex, tile.id]); // id includes scale, so unique\n\n const handleImageLoad = () => {\n if (urlRef.current) {\n URL.revokeObjectURL(urlRef.current);\n urlRef.current = null;\n }\n };\n\n if (!url) return null; // could render a placeholder\n return (\n <img\n src={url}\n onLoad={handleImageLoad}\n style={{\n position: 'absolute',\n left: tile.screenRect.origin.x * relativeScale,\n top: tile.screenRect.origin.y * relativeScale,\n width: tile.screenRect.size.width * relativeScale,\n height: tile.screenRect.size.height * relativeScale,\n display: 'block',\n }}\n />\n );\n}\n","import { Tile } from '@embedpdf/plugin-tiling';\nimport { useEffect, useState, HTMLAttributes, CSSProperties, useMemo } from '@framework';\n\nimport { TileImg } from './tile-img';\nimport { useTilingCapability } from '../hooks/use-tiling';\nimport { useDocumentState } from '@embedpdf/core/@framework';\n\ntype TilingLayoutProps = Omit<HTMLAttributes<HTMLDivElement>, 'style'> & {\n documentId: string;\n pageIndex: number;\n scale?: number;\n style?: CSSProperties;\n};\n\nexport function TilingLayer({\n documentId,\n pageIndex,\n scale: scaleOverride,\n style,\n ...props\n}: TilingLayoutProps) {\n const { provides: tilingProvides } = useTilingCapability();\n const documentState = useDocumentState(documentId);\n const [tiles, setTiles] = useState<Tile[]>([]);\n\n useEffect(() => {\n if (tilingProvides) {\n return tilingProvides.onTileRendering((event) => {\n if (event.documentId === documentId) {\n setTiles(event.tiles[pageIndex] ?? []);\n }\n });\n }\n }, [tilingProvides, documentId, pageIndex]);\n\n const actualScale = useMemo(() => {\n if (scaleOverride !== undefined) return scaleOverride;\n return documentState?.scale ?? 1;\n }, [scaleOverride, documentState?.scale]);\n\n return (\n <div\n style={{\n ...style,\n }}\n {...props}\n >\n {tiles?.map((tile) => (\n <TileImg\n key={tile.id}\n documentId={documentId}\n pageIndex={pageIndex}\n tile={tile}\n dpr={window.devicePixelRatio}\n scale={actualScale}\n />\n ))}\n </div>\n );\n}\n"],"names":[],"mappings":";;;;;;AAGO,MAAM,kBAAkB,MAAM,UAAwB,aAAa,EAAE;AACrE,MAAM,sBAAsB,MAAM,cAA4B,aAAa,EAAE;ACU7E,SAAS,QAAQ,EAAE,YAAY,WAAW,MAAM,KAAK,SAAuB;AACjF,QAAM,EAAE,UAAU,iBAAA,IAAqB,oBAAA;AACvC,QAAM,QAAQ;AAAA,IACZ,MAAM,qDAAkB,YAAY;AAAA,IACpC,CAAC,kBAAkB,UAAU;AAAA,EAAA;AAG/B,QAAM,CAAC,KAAK,MAAM,IAAI,SAAA;AACtB,QAAM,SAAS,OAAsB,IAAI;AAEzC,QAAM,gBAAgB,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QAAQ,IAAI,SAAiB,CAAA,CAAE;AAE7C,YAAU,MAAM;AACd,QAAI,gBAAgB;AAClB,aAAO,eAAe,gBAAgB,CAAC,UAAU;AAC/C,YAAI,MAAM,eAAe,YAAY;AACnC,mBAAS,MAAM,MAAM,SAAS,KAAK,CAAA,CAAE;AAAA,QACvC;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF,GAAG,CAAC,gBAAgB,YAAY,SAAS,CAAC;AAE1C,QAAM,cAAc,QAAQ,MAAM;AAChC,QAAI,kBAAkB,OAAW,QAAO;AACxC,YAAO,+CAAe,UAAS;AAAA,EACjC,GAAG,CAAC,eAAe,+CAAe,KAAK,CAAC;AAExC,SACE;AAAA,IAAC;AAAA,IAAA;AAAA,MACC,OAAO;AAAA,QACL,GAAG;AAAA,MAAA;AAAA,MAEJ,GAAG;AAAA,MAEH,UAAA,+BAAO,IAAI,CAAC,SACX;AAAA,QAAC;AAAA,QAAA;AAAA,UAEC;AAAA,UACA;AAAA,UACA;AAAA,UACA,KAAK,OAAO;AAAA,UACZ,OAAO;AAAA,QAAA;AAAA,QALF,KAAK;AAAA,MAAA;AAAA,IAOb;AAAA,EAAA;AAGP;"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Tile } from '../../index.ts';
|
|
2
2
|
interface TileImgProps {
|
|
3
|
+
documentId: string;
|
|
3
4
|
pageIndex: number;
|
|
4
5
|
tile: Tile;
|
|
5
6
|
dpr: number;
|
|
6
7
|
scale: number;
|
|
7
8
|
}
|
|
8
|
-
export declare function TileImg({ pageIndex, tile, dpr, scale }: TileImgProps): import("react/jsx-runtime").JSX.Element | null;
|
|
9
|
+
export declare function TileImg({ documentId, pageIndex, tile, dpr, scale }: TileImgProps): import("react/jsx-runtime").JSX.Element | null;
|
|
9
10
|
export {};
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { HTMLAttributes, CSSProperties } from '../../react/adapter.ts';
|
|
2
2
|
type TilingLayoutProps = Omit<HTMLAttributes<HTMLDivElement>, 'style'> & {
|
|
3
|
+
documentId: string;
|
|
3
4
|
pageIndex: number;
|
|
4
|
-
scale
|
|
5
|
+
scale?: number;
|
|
5
6
|
style?: CSSProperties;
|
|
6
7
|
};
|
|
7
|
-
export declare function TilingLayer({ pageIndex, scale, style, ...props }: TilingLayoutProps): import("react/jsx-runtime").JSX.Element;
|
|
8
|
+
export declare function TilingLayer({ documentId, pageIndex, scale: scaleOverride, style, ...props }: TilingLayoutProps): import("react/jsx-runtime").JSX.Element;
|
|
8
9
|
export {};
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Tile } from '../../lib/index.ts';
|
|
2
2
|
interface TileImgProps {
|
|
3
|
+
documentId: string;
|
|
3
4
|
pageIndex: number;
|
|
4
5
|
tile: Tile;
|
|
5
6
|
dpr: number;
|
|
6
7
|
scale: number;
|
|
7
8
|
}
|
|
8
|
-
export declare function TileImg({ pageIndex, tile, dpr, scale }: TileImgProps): import("preact").JSX.Element | null;
|
|
9
|
+
export declare function TileImg({ documentId, pageIndex, tile, dpr, scale }: TileImgProps): import("preact").JSX.Element | null;
|
|
9
10
|
export {};
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { HTMLAttributes, CSSProperties } from '../../preact/adapter.ts';
|
|
2
2
|
type TilingLayoutProps = Omit<HTMLAttributes<HTMLDivElement>, 'style'> & {
|
|
3
|
+
documentId: string;
|
|
3
4
|
pageIndex: number;
|
|
4
|
-
scale
|
|
5
|
+
scale?: number;
|
|
5
6
|
style?: CSSProperties;
|
|
6
7
|
};
|
|
7
|
-
export declare function TilingLayer({ pageIndex, scale, style, ...props }: TilingLayoutProps): import("preact").JSX.Element;
|
|
8
|
+
export declare function TilingLayer({ documentId, pageIndex, scale: scaleOverride, style, ...props }: TilingLayoutProps): import("preact").JSX.Element;
|
|
8
9
|
export {};
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Tile } from '../../lib/index.ts';
|
|
2
2
|
interface TileImgProps {
|
|
3
|
+
documentId: string;
|
|
3
4
|
pageIndex: number;
|
|
4
5
|
tile: Tile;
|
|
5
6
|
dpr: number;
|
|
6
7
|
scale: number;
|
|
7
8
|
}
|
|
8
|
-
export declare function TileImg({ pageIndex, tile, dpr, scale }: TileImgProps): import("react/jsx-runtime").JSX.Element | null;
|
|
9
|
+
export declare function TileImg({ documentId, pageIndex, tile, dpr, scale }: TileImgProps): import("react/jsx-runtime").JSX.Element | null;
|
|
9
10
|
export {};
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { HTMLAttributes, CSSProperties } from '../../react/adapter.ts';
|
|
2
2
|
type TilingLayoutProps = Omit<HTMLAttributes<HTMLDivElement>, 'style'> & {
|
|
3
|
+
documentId: string;
|
|
3
4
|
pageIndex: number;
|
|
4
|
-
scale
|
|
5
|
+
scale?: number;
|
|
5
6
|
style?: CSSProperties;
|
|
6
7
|
};
|
|
7
|
-
export declare function TilingLayer({ pageIndex, scale, style, ...props }: TilingLayoutProps): import("react/jsx-runtime").JSX.Element;
|
|
8
|
+
export declare function TilingLayer({ documentId, pageIndex, scale: scaleOverride, style, ...props }: TilingLayoutProps): import("react/jsx-runtime").JSX.Element;
|
|
8
9
|
export {};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { HTMLAttributes } from 'svelte/elements';
|
|
2
2
|
type TilingLayoutProps = HTMLAttributes<HTMLDivElement> & {
|
|
3
|
+
documentId: string;
|
|
3
4
|
pageIndex: number;
|
|
4
|
-
scale
|
|
5
|
+
scale?: number;
|
|
5
6
|
class?: string;
|
|
6
7
|
};
|
|
7
8
|
declare const TilingLayer: import('svelte', { with: { "resolution-mode": "import" } }).Component<TilingLayoutProps, {}, "">;
|
package/dist/svelte/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"}),require("svelte/internal/disclose-version");const e=require("svelte/internal/client"),t=require("@embedpdf/plugin-tiling"),r=require("@embedpdf/core/svelte"),i=require("@embedpdf/models"),n=require("svelte");function
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"}),require("svelte/internal/disclose-version");const e=require("svelte/internal/client"),t=require("@embedpdf/plugin-tiling"),r=require("@embedpdf/core/svelte"),i=require("@embedpdf/models"),n=require("svelte");function s(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e)for(const r in e)if("default"!==r){const i=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,i.get?i:{enumerable:!0,get:()=>e[r]})}return t.default=e,Object.freeze(t)}const l=s(e),o=()=>r.useCapability(t.TilingPlugin.id);var c=l.from_html('<img alt=""/>');function d(e,t){l.push(t,!0);const r=o(),s=l.derived(()=>{var e;return(null==(e=r.provides)?void 0:e.forDocument(t.documentId))??null});let d=l.state(""),a=null;const g=l.derived(()=>t.tile.id),u=l.derived(()=>t.tile.srcScale),p=l.derived(()=>t.tile.screenRect),f=l.derived(()=>t.scale/l.get(u));l.user_effect(()=>{l.get(g);const e=t.pageIndex;if(a)return;if(!l.get(s))return;const r=n.untrack(()=>{return e=t.tile,{...e,pageRect:{origin:{x:e.pageRect.origin.x,y:e.pageRect.origin.y},size:{width:e.pageRect.size.width,height:e.pageRect.size.height}},screenRect:{origin:{x:e.screenRect.origin.x,y:e.screenRect.origin.y},size:{width:e.screenRect.size.width,height:e.screenRect.size.height}}};var e}),o=l.get(s).renderTile({pageIndex:e,tile:r,dpr:t.dpr});return o.wait(e=>{const t=URL.createObjectURL(e);a=t,l.set(d,t,!0)},i.ignore),()=>{a?(URL.revokeObjectURL(a),a=null):o.abort({code:i.PdfErrorCode.Cancelled,message:"canceled render task"})}});const v=()=>{a&&(URL.revokeObjectURL(a),a=null)};var m=l.comment(),h=l.first_child(m),b=e=>{var t=c();let r;l.template_effect(()=>{l.set_attribute(t,"src",l.get(d)),r=l.set_style(t,"",r,{position:"absolute",left:l.get(p).origin.x*l.get(f)+"px",top:l.get(p).origin.y*l.get(f)+"px",width:l.get(p).size.width*l.get(f)+"px",height:l.get(p).size.height*l.get(f)+"px",display:"block"})}),l.event("load",t,v),l.replay_events(t),l.append(e,t)};l.if(h,e=>{l.get(d)&&e(b)}),l.append(e,m),l.pop()}var a=l.from_html("<div></div>");exports.TileImg=d,exports.TilingLayer=function(e,t){l.push(t,!0);let i=l.rest_props(t,["$$slots","$$events","$$legacy","documentId","pageIndex","scale","class"]);const n=o(),s=r.useDocumentState(()=>t.documentId);let c=l.state(l.proxy([]));const g=l.derived(()=>{var e;return void 0!==t.scale?t.scale:(null==(e=s.current)?void 0:e.scale)??1});l.user_effect(()=>{if(n.provides)return n.provides.onTileRendering(e=>{e.documentId===t.documentId&&l.set(c,e.tiles[t.pageIndex]??[],!0)})});var u=a();l.attribute_effect(u,()=>({class:t.class,...i})),l.each(u,21,()=>l.get(c),e=>e.id,(e,r)=>{d(e,{get documentId(){return t.documentId},get pageIndex(){return t.pageIndex},get tile(){return l.get(r)},dpr:window.devicePixelRatio,get scale(){return l.get(g)}})}),l.reset(u),l.append(e,u),l.pop()},exports.useTilingCapability=o,exports.useTilingPlugin=()=>r.usePlugin(t.TilingPlugin.id),Object.keys(t).forEach(e=>{"default"===e||Object.prototype.hasOwnProperty.call(exports,e)||Object.defineProperty(exports,e,{enumerable:!0,get:()=>t[e]})});
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"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 pageIndex: number;\n tile: Tile;\n dpr: number;\n scale: number;\n }\n\n let { pageIndex, tile, dpr, scale }: TileImgProps = $props();\n const tilingCapability = useTilingCapability();\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 (!
|
|
1
|
+
{"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","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","left","top","consequent","restProps","rest_props","documentState","useDocumentState","documentId","tiles","state","proxy","actualScale","derived","scale","current","onTileRendering","event","each","div","window","devicePixelRatio","usePlugin"],"mappings":"wjBAIaA,EAAsB,IAAMC,gBAA4BC,EAAAA,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