@embedpdf/plugin-tiling 1.0.10 → 1.0.12
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 -269
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -86
- package/dist/index.js +21 -35
- package/dist/index.js.map +1 -1
- package/dist/lib/actions.d.ts +18 -0
- package/dist/lib/index.d.ts +8 -0
- package/dist/lib/manifest.d.ts +4 -0
- package/dist/lib/reducer.d.ts +5 -0
- package/dist/lib/tiling-plugin.d.ts +17 -0
- package/dist/lib/types.d.ts +46 -0
- package/dist/lib/utils.d.ts +8 -0
- package/dist/preact/adapter.d.ts +5 -0
- package/dist/preact/core.d.ts +1 -0
- package/dist/preact/index.cjs +2 -128
- package/dist/preact/index.cjs.map +1 -1
- package/dist/preact/index.d.ts +1 -23
- package/dist/preact/index.js +11 -19
- package/dist/preact/index.js.map +1 -1
- package/dist/react/adapter.d.ts +2 -0
- package/dist/react/core.d.ts +1 -0
- package/dist/react/index.cjs +2 -128
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.ts +1 -23
- package/dist/react/index.js +10 -19
- package/dist/react/index.js.map +1 -1
- package/dist/shared-preact/components/index.d.ts +1 -0
- package/dist/shared-preact/components/tile-img.d.ts +9 -0
- package/dist/shared-preact/components/tiling-layer.d.ts +8 -0
- package/dist/shared-preact/hooks/index.d.ts +1 -0
- package/dist/shared-preact/hooks/use-tiling.d.ts +11 -0
- package/dist/shared-preact/index.d.ts +2 -0
- package/dist/shared-react/components/index.d.ts +1 -0
- package/dist/shared-react/components/tile-img.d.ts +9 -0
- package/dist/shared-react/components/tiling-layer.d.ts +8 -0
- package/dist/shared-react/hooks/index.d.ts +1 -0
- package/dist/shared-react/hooks/use-tiling.d.ts +11 -0
- package/dist/shared-react/index.d.ts +2 -0
- package/dist/vue/components/index.d.ts +2 -0
- package/dist/vue/components/tile-img.vue.d.ts +13 -0
- package/dist/vue/components/tiling-layer.vue.d.ts +8 -0
- package/dist/vue/hooks/index.d.ts +1 -0
- package/dist/vue/hooks/use-tiling.d.ts +5 -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 +118 -0
- package/dist/vue/index.js.map +1 -0
- package/package.json +25 -14
- package/dist/index.d.cts +0 -86
- package/dist/preact/index.d.cts +0 -23
- package/dist/react/index.d.cts +0 -23
package/dist/index.cjs
CHANGED
|
@@ -1,269 +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/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
TILING_PLUGIN_ID: () => TILING_PLUGIN_ID,
|
|
24
|
-
TilingPlugin: () => TilingPlugin,
|
|
25
|
-
TilingPluginPackage: () => TilingPluginPackage,
|
|
26
|
-
manifest: () => manifest
|
|
27
|
-
});
|
|
28
|
-
module.exports = __toCommonJS(index_exports);
|
|
29
|
-
|
|
30
|
-
// src/lib/manifest.ts
|
|
31
|
-
var TILING_PLUGIN_ID = "tiling";
|
|
32
|
-
var manifest = {
|
|
33
|
-
id: TILING_PLUGIN_ID,
|
|
34
|
-
name: "Tiling Plugin",
|
|
35
|
-
version: "1.0.0",
|
|
36
|
-
provides: ["tiling"],
|
|
37
|
-
requires: ["render", "scroll", "viewport"],
|
|
38
|
-
optional: [],
|
|
39
|
-
defaultConfig: {
|
|
40
|
-
enabled: true,
|
|
41
|
-
tileSize: 768,
|
|
42
|
-
overlapPx: 2.5,
|
|
43
|
-
extraRings: 0
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
// src/lib/actions.ts
|
|
48
|
-
var UPDATE_VISIBLE_TILES = "UPDATE_VISIBLE_TILES";
|
|
49
|
-
var MARK_TILE_STATUS = "MARK_TILE_STATUS";
|
|
50
|
-
var updateVisibleTiles = (tiles) => ({
|
|
51
|
-
type: UPDATE_VISIBLE_TILES,
|
|
52
|
-
payload: tiles
|
|
53
|
-
});
|
|
54
|
-
var markTileStatus = (pageIndex, tileId, status) => ({ type: MARK_TILE_STATUS, payload: { pageIndex, tileId, status } });
|
|
55
|
-
|
|
56
|
-
// src/lib/reducer.ts
|
|
57
|
-
var initialState = {
|
|
58
|
-
visibleTiles: {}
|
|
59
|
-
};
|
|
60
|
-
var tilingReducer = (state, action) => {
|
|
61
|
-
switch (action.type) {
|
|
62
|
-
case UPDATE_VISIBLE_TILES: {
|
|
63
|
-
const incoming = action.payload;
|
|
64
|
-
const nextPages = { ...state.visibleTiles };
|
|
65
|
-
for (const key in incoming) {
|
|
66
|
-
const pageIndex = Number(key);
|
|
67
|
-
const newTiles = incoming[pageIndex];
|
|
68
|
-
const prevTiles = nextPages[pageIndex] ?? [];
|
|
69
|
-
const prevScale = prevTiles.find((t) => !t.isFallback)?.srcScale;
|
|
70
|
-
const newScale = newTiles[0].srcScale;
|
|
71
|
-
const zoomChanged = prevScale !== void 0 && prevScale !== newScale;
|
|
72
|
-
if (zoomChanged) {
|
|
73
|
-
const promoted = prevTiles.filter((t) => !t.isFallback && t.status === "ready").map((t) => ({ ...t, isFallback: true }));
|
|
74
|
-
const fallbackToCarry = promoted.length > 0 ? [] : prevTiles.filter((t) => t.isFallback);
|
|
75
|
-
nextPages[pageIndex] = [...fallbackToCarry, ...promoted, ...newTiles];
|
|
76
|
-
} else {
|
|
77
|
-
const newIds = new Set(newTiles.map((t) => t.id));
|
|
78
|
-
const keepers = [];
|
|
79
|
-
const seenIds = /* @__PURE__ */ new Set();
|
|
80
|
-
for (const t of prevTiles) {
|
|
81
|
-
if (t.isFallback) {
|
|
82
|
-
keepers.push(t);
|
|
83
|
-
seenIds.add(t.id);
|
|
84
|
-
} else if (newIds.has(t.id)) {
|
|
85
|
-
keepers.push(t);
|
|
86
|
-
seenIds.add(t.id);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
for (const t of newTiles) {
|
|
90
|
-
if (!seenIds.has(t.id)) keepers.push(t);
|
|
91
|
-
}
|
|
92
|
-
nextPages[pageIndex] = keepers;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
return { ...state, visibleTiles: nextPages };
|
|
96
|
-
}
|
|
97
|
-
case MARK_TILE_STATUS: {
|
|
98
|
-
const { pageIndex, tileId, status } = action.payload;
|
|
99
|
-
const tiles = state.visibleTiles[pageIndex]?.map(
|
|
100
|
-
(t) => t.id === tileId ? { ...t, status } : t
|
|
101
|
-
) ?? [];
|
|
102
|
-
const newTiles = tiles.filter((t) => !t.isFallback);
|
|
103
|
-
const allReady = newTiles.every((t) => t.status === "ready");
|
|
104
|
-
const finalTiles = allReady ? newTiles : tiles;
|
|
105
|
-
return {
|
|
106
|
-
...state,
|
|
107
|
-
visibleTiles: { ...state.visibleTiles, [pageIndex]: finalTiles }
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
default:
|
|
111
|
-
return state;
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
// src/lib/tiling-plugin.ts
|
|
116
|
-
var import_core = require("@embedpdf/core");
|
|
117
|
-
var import_models2 = require("@embedpdf/models");
|
|
118
|
-
|
|
119
|
-
// src/lib/utils.ts
|
|
120
|
-
var import_models = require("@embedpdf/models");
|
|
121
|
-
function calculateTilesForPage({
|
|
122
|
-
tileSize = 768,
|
|
123
|
-
overlapPx = 2.5,
|
|
124
|
-
extraRings = 0,
|
|
125
|
-
scale,
|
|
126
|
-
rotation,
|
|
127
|
-
page,
|
|
128
|
-
metric
|
|
129
|
-
}) {
|
|
130
|
-
const pageW = page.size.width * scale;
|
|
131
|
-
const pageH = page.size.height * scale;
|
|
132
|
-
const step = tileSize - overlapPx;
|
|
133
|
-
const containerSize = (0, import_models.transformSize)(page.size, rotation, scale);
|
|
134
|
-
const rotatedVisRect = {
|
|
135
|
-
origin: { x: metric.scaled.pageX, y: metric.scaled.pageY },
|
|
136
|
-
size: { width: metric.scaled.visibleWidth, height: metric.scaled.visibleHeight }
|
|
137
|
-
};
|
|
138
|
-
const unrotatedVisRect = (0, import_models.restoreRect)(containerSize, rotatedVisRect, rotation, 1);
|
|
139
|
-
const visLeft = unrotatedVisRect.origin.x;
|
|
140
|
-
const visTop = unrotatedVisRect.origin.y;
|
|
141
|
-
const visRight = visLeft + unrotatedVisRect.size.width;
|
|
142
|
-
const visBottom = visTop + unrotatedVisRect.size.height;
|
|
143
|
-
const maxCol = Math.floor((pageW - 1) / step);
|
|
144
|
-
const maxRow = Math.floor((pageH - 1) / step);
|
|
145
|
-
const startCol = Math.max(0, Math.floor(visLeft / step) - extraRings);
|
|
146
|
-
const endCol = Math.min(maxCol, Math.floor((visRight - 1) / step) + extraRings);
|
|
147
|
-
const startRow = Math.max(0, Math.floor(visTop / step) - extraRings);
|
|
148
|
-
const endRow = Math.min(maxRow, Math.floor((visBottom - 1) / step) + extraRings);
|
|
149
|
-
const tiles = [];
|
|
150
|
-
for (let col = startCol; col <= endCol; col++) {
|
|
151
|
-
const xScreen = col * step;
|
|
152
|
-
const wScreen = Math.min(tileSize, pageW - xScreen);
|
|
153
|
-
const xPage = xScreen / scale;
|
|
154
|
-
const wPage = wScreen / scale;
|
|
155
|
-
for (let row = startRow; row <= endRow; row++) {
|
|
156
|
-
const yScreen = row * step;
|
|
157
|
-
const hScreen = Math.min(tileSize, pageH - yScreen);
|
|
158
|
-
const yPage = yScreen / scale;
|
|
159
|
-
const hPage = hScreen / scale;
|
|
160
|
-
tiles.push({
|
|
161
|
-
id: `p${page.index}-${scale}-x${xScreen}-y${yScreen}-w${wScreen}-h${hScreen}`,
|
|
162
|
-
col,
|
|
163
|
-
row,
|
|
164
|
-
pageRect: { origin: { x: xPage, y: yPage }, size: { width: wPage, height: hPage } },
|
|
165
|
-
screenRect: {
|
|
166
|
-
origin: { x: xScreen, y: yScreen },
|
|
167
|
-
size: { width: wScreen, height: hScreen }
|
|
168
|
-
},
|
|
169
|
-
status: "queued",
|
|
170
|
-
srcScale: scale,
|
|
171
|
-
isFallback: false
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
return tiles;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// src/lib/tiling-plugin.ts
|
|
179
|
-
var TilingPlugin = class extends import_core.BasePlugin {
|
|
180
|
-
constructor(id, registry, config) {
|
|
181
|
-
super(id, registry);
|
|
182
|
-
this.tileRendering$ = (0, import_core.createBehaviorEmitter)();
|
|
183
|
-
this.config = config;
|
|
184
|
-
this.renderCapability = this.registry.getPlugin("render").provides();
|
|
185
|
-
this.scrollCapability = this.registry.getPlugin("scroll").provides();
|
|
186
|
-
this.viewportCapability = this.registry.getPlugin("viewport").provides();
|
|
187
|
-
this.scrollCapability.onScroll((scrollMetrics) => this.calculateVisibleTiles(scrollMetrics), {
|
|
188
|
-
mode: "throttle",
|
|
189
|
-
wait: 500,
|
|
190
|
-
throttleMode: "trailing"
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
async initialize() {
|
|
194
|
-
}
|
|
195
|
-
onCoreStoreUpdated(oldState, newState) {
|
|
196
|
-
if (oldState.core.scale !== newState.core.scale) {
|
|
197
|
-
this.calculateVisibleTiles(
|
|
198
|
-
this.scrollCapability.getMetrics(this.viewportCapability.getMetrics())
|
|
199
|
-
);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
calculateVisibleTiles(scrollMetrics) {
|
|
203
|
-
if (!this.config.enabled) {
|
|
204
|
-
this.dispatch(updateVisibleTiles([]));
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
const scale = this.coreState.core.scale;
|
|
208
|
-
const rotation = this.coreState.core.rotation;
|
|
209
|
-
const visibleTiles = {};
|
|
210
|
-
for (const scrollMetric of scrollMetrics.pageVisibilityMetrics) {
|
|
211
|
-
const pageIndex = scrollMetric.pageNumber - 1;
|
|
212
|
-
const page = this.coreState.core.document?.pages[pageIndex];
|
|
213
|
-
if (!page) continue;
|
|
214
|
-
const tiles = calculateTilesForPage({
|
|
215
|
-
page,
|
|
216
|
-
metric: scrollMetric,
|
|
217
|
-
scale,
|
|
218
|
-
rotation,
|
|
219
|
-
tileSize: this.config.tileSize,
|
|
220
|
-
overlapPx: this.config.overlapPx,
|
|
221
|
-
extraRings: this.config.extraRings
|
|
222
|
-
});
|
|
223
|
-
visibleTiles[pageIndex] = tiles;
|
|
224
|
-
}
|
|
225
|
-
this.dispatch(updateVisibleTiles(visibleTiles));
|
|
226
|
-
}
|
|
227
|
-
onStoreUpdated(_prevState, newState) {
|
|
228
|
-
this.tileRendering$.emit(newState.visibleTiles);
|
|
229
|
-
}
|
|
230
|
-
buildCapability() {
|
|
231
|
-
return {
|
|
232
|
-
renderTile: this.renderTile.bind(this),
|
|
233
|
-
onTileRendering: this.tileRendering$.on
|
|
234
|
-
};
|
|
235
|
-
}
|
|
236
|
-
renderTile(options) {
|
|
237
|
-
if (!this.renderCapability) {
|
|
238
|
-
throw new Error("Render capability not available.");
|
|
239
|
-
}
|
|
240
|
-
this.dispatch(markTileStatus(options.pageIndex, options.tile.id, "rendering"));
|
|
241
|
-
const task = this.renderCapability.renderPageRect({
|
|
242
|
-
pageIndex: options.pageIndex,
|
|
243
|
-
rect: options.tile.pageRect,
|
|
244
|
-
scaleFactor: options.tile.srcScale,
|
|
245
|
-
dpr: options.dpr
|
|
246
|
-
});
|
|
247
|
-
task.wait(() => {
|
|
248
|
-
this.dispatch(markTileStatus(options.pageIndex, options.tile.id, "ready"));
|
|
249
|
-
}, import_models2.ignore);
|
|
250
|
-
return task;
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
TilingPlugin.id = "tiling";
|
|
254
|
-
|
|
255
|
-
// src/lib/index.ts
|
|
256
|
-
var TilingPluginPackage = {
|
|
257
|
-
manifest,
|
|
258
|
-
create: (registry, _engine, config) => new TilingPlugin(TILING_PLUGIN_ID, registry, config),
|
|
259
|
-
reducer: (state, action) => tilingReducer(state, action),
|
|
260
|
-
initialState
|
|
261
|
-
};
|
|
262
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
263
|
-
0 && (module.exports = {
|
|
264
|
-
TILING_PLUGIN_ID,
|
|
265
|
-
TilingPlugin,
|
|
266
|
-
TilingPluginPackage,
|
|
267
|
-
manifest
|
|
268
|
-
});
|
|
269
|
-
//# sourceMappingURL=index.cjs.map
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("@embedpdf/core"),i=require("@embedpdf/models"),t="tiling",s={id:t,name:"Tiling Plugin",version:"1.0.0",provides:["tiling"],requires:["render","scroll","viewport"],optional:[],defaultConfig:{enabled:!0,tileSize:768,overlapPx:2.5,extraRings:0}},r="UPDATE_VISIBLE_TILES",a="MARK_TILE_STATUS",l=e=>({type:r,payload:e}),o=(e,i,t)=>({type:a,payload:{pageIndex:e,tileId:i,status:t}});function n({tileSize:e=768,overlapPx:t=2.5,extraRings:s=0,scale:r,rotation:a,page:l,metric:o}){const n=l.size.width*r,c=l.size.height*r,d=e-t,h=i.transformSize(l.size,a,r),g={origin:{x:o.scaled.pageX,y:o.scaled.pageY},size:{width:o.scaled.visibleWidth,height:o.scaled.visibleHeight}},p=i.restoreRect(h,g,a,1),u=p.origin.x,b=p.origin.y,f=u+p.size.width,v=b+p.size.height,y=Math.floor((n-1)/d),x=Math.floor((c-1)/d),m=Math.max(0,Math.floor(u/d)-s),S=Math.min(y,Math.floor((f-1)/d)+s),T=Math.max(0,Math.floor(b/d)-s),w=Math.min(x,Math.floor((v-1)/d)+s),M=[];for(let i=m;i<=S;i++){const t=i*d,s=Math.min(e,n-t),a=t/r,o=s/r;for(let n=T;n<=w;n++){const h=n*d,g=Math.min(e,c-h),p=h/r,u=g/r;M.push({id:`p${l.index}-${r}-x${t}-y${h}-w${s}-h${g}`,col:i,row:n,pageRect:{origin:{x:a,y:p},size:{width:o,height:u}},screenRect:{origin:{x:t,y:h},size:{width:s,height:g}},status:"queued",srcScale:r,isFallback:!1})}}return M}const c=class extends e.BasePlugin{constructor(i,t,s){super(i,t),this.tileRendering$=e.createBehaviorEmitter(),this.config=s,this.renderCapability=this.registry.getPlugin("render").provides(),this.scrollCapability=this.registry.getPlugin("scroll").provides(),this.viewportCapability=this.registry.getPlugin("viewport").provides(),this.scrollCapability.onScroll((e=>this.calculateVisibleTiles(e)),{mode:"throttle",wait:500,throttleMode:"trailing"})}async initialize(){}onCoreStoreUpdated(e,i){e.core.scale!==i.core.scale&&this.calculateVisibleTiles(this.scrollCapability.getMetrics(this.viewportCapability.getMetrics()))}calculateVisibleTiles(e){var i;if(!this.config.enabled)return void this.dispatch(l([]));const t=this.coreState.core.scale,s=this.coreState.core.rotation,r={};for(const a of e.pageVisibilityMetrics){const e=a.pageNumber-1,l=null==(i=this.coreState.core.document)?void 0:i.pages[e];if(!l)continue;const o=n({page:l,metric:a,scale:t,rotation:s,tileSize:this.config.tileSize,overlapPx:this.config.overlapPx,extraRings:this.config.extraRings});r[e]=o}this.dispatch(l(r))}onStoreUpdated(e,i){this.tileRendering$.emit(i.visibleTiles)}buildCapability(){return{renderTile:this.renderTile.bind(this),onTileRendering:this.tileRendering$.on}}renderTile(e){if(!this.renderCapability)throw new Error("Render capability not available.");this.dispatch(o(e.pageIndex,e.tile.id,"rendering"));const t=this.renderCapability.renderPageRect({pageIndex:e.pageIndex,rect:e.tile.pageRect,scaleFactor:e.tile.srcScale,dpr:e.dpr});return t.wait((()=>{this.dispatch(o(e.pageIndex,e.tile.id,"ready"))}),i.ignore),t}};c.id="tiling";let d=c;const h={manifest:s,create:(e,i,s)=>new d(t,e,s),reducer:(e,i)=>((e,i)=>{var t,s;switch(i.type){case r:{const s=i.payload,r={...e.visibleTiles};for(const e in s){const i=Number(e),a=s[i],l=r[i]??[],o=null==(t=l.find((e=>!e.isFallback)))?void 0:t.srcScale,n=a[0].srcScale;if(void 0!==o&&o!==n){const e=l.filter((e=>!e.isFallback&&"ready"===e.status)).map((e=>({...e,isFallback:!0}))),t=e.length>0?[]:l.filter((e=>e.isFallback));r[i]=[...t,...e,...a]}else{const e=new Set(a.map((e=>e.id))),t=[],s=new Set;for(const i of l)(i.isFallback||e.has(i.id))&&(t.push(i),s.add(i.id));for(const i of a)s.has(i.id)||t.push(i);r[i]=t}}return{...e,visibleTiles:r}}case a:{const{pageIndex:t,tileId:r,status:a}=i.payload,l=(null==(s=e.visibleTiles[t])?void 0:s.map((e=>e.id===r?{...e,status:a}:e)))??[],o=l.filter((e=>!e.isFallback)),n=o.every((e=>"ready"===e.status))?o:l;return{...e,visibleTiles:{...e.visibleTiles,[t]:n}}}default:return e}})(e,i),initialState:{visibleTiles:{}}};exports.TILING_PLUGIN_ID=t,exports.TilingPlugin=d,exports.TilingPluginPackage=h,exports.manifest=s;
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/lib/manifest.ts","../src/lib/actions.ts","../src/lib/reducer.ts","../src/lib/tiling-plugin.ts","../src/lib/utils.ts","../src/lib/index.ts"],"sourcesContent":["export * from './lib';\n","import { PluginManifest } from '@embedpdf/core';\n\nimport { TilingPluginConfig } from './types';\n\nexport const TILING_PLUGIN_ID = 'tiling';\n\nexport const manifest: PluginManifest<TilingPluginConfig> = {\n id: TILING_PLUGIN_ID,\n name: 'Tiling Plugin',\n version: '1.0.0',\n provides: ['tiling'],\n requires: ['render', 'scroll', 'viewport'],\n optional: [],\n defaultConfig: {\n enabled: true,\n tileSize: 768,\n overlapPx: 2.5,\n extraRings: 0,\n },\n};\n","import { Tile, TileStatus } from './types';\n\nexport const UPDATE_VISIBLE_TILES = 'UPDATE_VISIBLE_TILES';\nexport const MARK_TILE_STATUS = 'MARK_TILE_STATUS';\n\nexport type UpdateVisibleTilesAction = {\n type: typeof UPDATE_VISIBLE_TILES;\n payload: Record<number, Tile[]>;\n};\n\nexport type MarkTileStatusAction = {\n type: typeof MARK_TILE_STATUS;\n payload: { pageIndex: number; tileId: string; status: TileStatus };\n};\n\nexport type TilingAction = UpdateVisibleTilesAction | MarkTileStatusAction;\n\nexport const updateVisibleTiles = (tiles: Record<number, Tile[]>): UpdateVisibleTilesAction => ({\n type: UPDATE_VISIBLE_TILES,\n payload: tiles,\n});\n\nexport const markTileStatus = (\n pageIndex: number,\n tileId: string,\n status: TileStatus,\n): MarkTileStatusAction => ({ type: MARK_TILE_STATUS, payload: { pageIndex, tileId, status } });\n","import { Reducer } from '@embedpdf/core';\n\nimport { UPDATE_VISIBLE_TILES, MARK_TILE_STATUS, TilingAction } from './actions';\nimport { Tile, TilingState } from './types';\n\nexport const initialState: TilingState = {\n visibleTiles: {},\n};\n\nexport const tilingReducer: Reducer<TilingState, TilingAction> = (state, action) => {\n switch (action.type) {\n case UPDATE_VISIBLE_TILES: {\n const incoming = action.payload; // Record<number, Tile[]>\n const nextPages = { ...state.visibleTiles };\n\n for (const key in incoming) {\n const pageIndex = Number(key);\n const newTiles = incoming[pageIndex]; // all isFallback=false\n const prevTiles = nextPages[pageIndex] ?? [];\n\n const prevScale = prevTiles.find((t) => !t.isFallback)?.srcScale;\n const newScale = newTiles[0].srcScale;\n const zoomChanged = prevScale !== undefined && prevScale !== newScale;\n\n if (zoomChanged) {\n /* 1️⃣ ready tiles from the old zoom → new fallback */\n const promoted = prevTiles\n .filter((t) => !t.isFallback && t.status === 'ready')\n .map((t) => ({ ...t, isFallback: true }));\n\n /* 2️⃣ decide which fallback tiles to keep */\n const fallbackToCarry = promoted.length > 0 ? [] : prevTiles.filter((t) => t.isFallback);\n\n /* 3️⃣ final list = (maybe-kept fallback) + promoted + newTiles */\n nextPages[pageIndex] = [...fallbackToCarry, ...promoted, ...newTiles];\n } else {\n /* same zoom → keep current fallback, replace visible */\n const newIds = new Set(newTiles.map((t) => t.id));\n const keepers: Tile[] = []; // where we’ll collect surviving tiles\n const seenIds = new Set<string>();\n\n /* 2️⃣ loop prevTiles once */\n for (const t of prevTiles) {\n if (t.isFallback) {\n keepers.push(t); // always keep fallback\n seenIds.add(t.id);\n } else if (newIds.has(t.id)) {\n keepers.push(t); // keep old visible tile (preserves status)\n seenIds.add(t.id);\n }\n }\n\n /* 3️⃣ append *brand-new* tiles (not yet kept) */\n for (const t of newTiles) {\n if (!seenIds.has(t.id)) keepers.push(t);\n }\n\n /* 4️⃣ store result */\n nextPages[pageIndex] = keepers;\n }\n }\n\n return { ...state, visibleTiles: nextPages };\n }\n\n case MARK_TILE_STATUS: {\n const { pageIndex, tileId, status } = action.payload;\n const tiles =\n state.visibleTiles[pageIndex]?.map((t) =>\n t.id === tileId ? ({ ...t, status } as Tile) : t,\n ) ?? [];\n\n const newTiles = tiles.filter((t) => !t.isFallback);\n const allReady = newTiles.every((t) => t.status === 'ready');\n const finalTiles = allReady ? newTiles : tiles;\n\n return {\n ...state,\n visibleTiles: { ...state.visibleTiles, [pageIndex]: finalTiles },\n };\n }\n\n default:\n return state;\n }\n};\n","import {\n BasePlugin,\n CoreState,\n createBehaviorEmitter,\n PluginRegistry,\n StoreState,\n} from '@embedpdf/core';\nimport { ignore } from '@embedpdf/models';\nimport { RenderCapability, RenderPlugin } from '@embedpdf/plugin-render';\nimport { ScrollCapability, ScrollMetrics, ScrollPlugin } from '@embedpdf/plugin-scroll';\nimport { ViewportCapability, ViewportPlugin } from '@embedpdf/plugin-viewport';\n\nimport { markTileStatus, updateVisibleTiles } from './actions';\nimport {\n TilingPluginConfig,\n TilingCapability,\n Tile,\n RenderTileOptions,\n TilingState,\n} from './types';\nimport { calculateTilesForPage } from './utils';\n\nexport class TilingPlugin extends BasePlugin<TilingPluginConfig, TilingCapability> {\n static readonly id = 'tiling' as const;\n\n private readonly tileRendering$ = createBehaviorEmitter<Record<number, Tile[]>>();\n\n private config: TilingPluginConfig;\n private renderCapability: RenderCapability;\n private scrollCapability: ScrollCapability;\n private viewportCapability: ViewportCapability;\n\n constructor(id: string, registry: PluginRegistry, config: TilingPluginConfig) {\n super(id, registry);\n\n this.config = config;\n\n this.renderCapability = this.registry.getPlugin<RenderPlugin>('render')!.provides();\n this.scrollCapability = this.registry.getPlugin<ScrollPlugin>('scroll')!.provides();\n this.viewportCapability = this.registry.getPlugin<ViewportPlugin>('viewport')!.provides();\n\n this.scrollCapability.onScroll((scrollMetrics) => this.calculateVisibleTiles(scrollMetrics), {\n mode: 'throttle',\n wait: 500,\n throttleMode: 'trailing',\n });\n }\n\n async initialize(): Promise<void> {\n // Fetch dependencies from the registry if needed\n }\n\n protected onCoreStoreUpdated(\n oldState: StoreState<CoreState>,\n newState: StoreState<CoreState>,\n ): void {\n if (oldState.core.scale !== newState.core.scale) {\n this.calculateVisibleTiles(\n this.scrollCapability.getMetrics(this.viewportCapability.getMetrics()),\n );\n }\n }\n\n private calculateVisibleTiles(scrollMetrics: ScrollMetrics): void {\n if (!this.config.enabled) {\n this.dispatch(updateVisibleTiles([]));\n return;\n }\n\n const scale = this.coreState.core.scale;\n const rotation = this.coreState.core.rotation;\n const visibleTiles: { [pageIndex: number]: Tile[] } = {};\n\n for (const scrollMetric of scrollMetrics.pageVisibilityMetrics) {\n const pageIndex = scrollMetric.pageNumber - 1; // Convert to 0-based index\n const page = this.coreState.core.document?.pages[pageIndex];\n if (!page) continue;\n\n // Calculate tiles for the page using the utility function\n const tiles = calculateTilesForPage({\n page,\n metric: scrollMetric,\n scale,\n rotation,\n tileSize: this.config.tileSize,\n overlapPx: this.config.overlapPx,\n extraRings: this.config.extraRings,\n });\n\n visibleTiles[pageIndex] = tiles;\n }\n\n this.dispatch(updateVisibleTiles(visibleTiles));\n }\n\n override onStoreUpdated(_prevState: TilingState, newState: TilingState): void {\n this.tileRendering$.emit(newState.visibleTiles);\n }\n\n protected buildCapability(): TilingCapability {\n return {\n renderTile: this.renderTile.bind(this),\n onTileRendering: this.tileRendering$.on,\n };\n }\n\n private renderTile(options: RenderTileOptions) {\n if (!this.renderCapability) {\n throw new Error('Render capability not available.');\n }\n\n this.dispatch(markTileStatus(options.pageIndex, options.tile.id, 'rendering'));\n\n const task = this.renderCapability.renderPageRect({\n pageIndex: options.pageIndex,\n rect: options.tile.pageRect,\n scaleFactor: options.tile.srcScale,\n dpr: options.dpr,\n });\n\n task.wait(() => {\n this.dispatch(markTileStatus(options.pageIndex, options.tile.id, 'ready'));\n }, ignore);\n\n return task;\n }\n}\n","import { Rect, restoreRect, transformSize } from '@embedpdf/models';\nimport { CalculateTilesForPageOptions, Tile } from './types';\n\n/**\n * Build a grid where neighbouring tiles overlap by `overlapPx`\n * (screen pixels). Inner tiles keep the full `tileSize`, edge\n * tiles are clipped to the page bounds. All screen-space values\n * are rounded to **integers** to avoid sub-pixel seams.\n */\nexport function calculateTilesForPage({\n tileSize = 768,\n overlapPx = 2.5,\n extraRings = 0,\n scale,\n rotation,\n page,\n metric,\n}: CalculateTilesForPageOptions): Tile[] {\n /* ---- work in screen-pixel space -------------------------------- */\n const pageW = page.size.width * scale; // px\n const pageH = page.size.height * scale; // px\n\n const step = tileSize - overlapPx; // shift between tiles\n\n const containerSize = transformSize(page.size, rotation, scale);\n const rotatedVisRect: Rect = {\n origin: { x: metric.scaled.pageX, y: metric.scaled.pageY },\n size: { width: metric.scaled.visibleWidth, height: metric.scaled.visibleHeight },\n };\n const unrotatedVisRect = restoreRect(containerSize, rotatedVisRect, rotation, 1);\n\n const visLeft = unrotatedVisRect.origin.x;\n const visTop = unrotatedVisRect.origin.y;\n const visRight = visLeft + unrotatedVisRect.size.width;\n const visBottom = visTop + unrotatedVisRect.size.height;\n\n const maxCol = Math.floor((pageW - 1) / step);\n const maxRow = Math.floor((pageH - 1) / step);\n\n const startCol = Math.max(0, Math.floor(visLeft / step) - extraRings);\n const endCol = Math.min(maxCol, Math.floor((visRight - 1) / step) + extraRings);\n const startRow = Math.max(0, Math.floor(visTop / step) - extraRings);\n const endRow = Math.min(maxRow, Math.floor((visBottom - 1) / step) + extraRings);\n\n /* ---- build tiles ---------------------------------------------- */\n const tiles: Tile[] = [];\n\n for (let col = startCol; col <= endCol; col++) {\n const xScreen = col * step; // px (integer)\n const wScreen = Math.min(tileSize, pageW - xScreen); // px (≤ tileSize)\n\n const xPage = xScreen / scale; // pt (may be frac.)\n const wPage = wScreen / scale; // pt\n\n for (let row = startRow; row <= endRow; row++) {\n const yScreen = row * step;\n const hScreen = Math.min(tileSize, pageH - yScreen);\n\n const yPage = yScreen / scale;\n const hPage = hScreen / scale;\n\n tiles.push({\n id: `p${page.index}-${scale}-x${xScreen}-y${yScreen}-w${wScreen}-h${hScreen}`,\n col,\n row,\n pageRect: { origin: { x: xPage, y: yPage }, size: { width: wPage, height: hPage } },\n screenRect: {\n origin: { x: xScreen, y: yScreen },\n size: { width: wScreen, height: hScreen },\n },\n status: 'queued',\n srcScale: scale,\n isFallback: false,\n });\n }\n }\n\n return tiles;\n}\n","import { PluginPackage } from '@embedpdf/core';\n\nimport { TilingAction } from './actions';\nimport { manifest, TILING_PLUGIN_ID } from './manifest';\nimport { initialState, tilingReducer } from './reducer';\nimport { TilingPlugin } from './tiling-plugin';\nimport { TilingPluginConfig, TilingState } from './types';\n\nexport const TilingPluginPackage: PluginPackage<\n TilingPlugin,\n TilingPluginConfig,\n TilingState,\n TilingAction\n> = {\n manifest,\n create: (registry, _engine, config) => new TilingPlugin(TILING_PLUGIN_ID, registry, config),\n reducer: (state, action) => tilingReducer(state, action),\n initialState,\n};\n\nexport * from './tiling-plugin';\nexport * from './types';\nexport * from './manifest';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACIO,IAAM,mBAAmB;AAEzB,IAAM,WAA+C;AAAA,EAC1D,IAAI;AAAA,EACJ,MAAM;AAAA,EACN,SAAS;AAAA,EACT,UAAU,CAAC,QAAQ;AAAA,EACnB,UAAU,CAAC,UAAU,UAAU,UAAU;AAAA,EACzC,UAAU,CAAC;AAAA,EACX,eAAe;AAAA,IACb,SAAS;AAAA,IACT,UAAU;AAAA,IACV,WAAW;AAAA,IACX,YAAY;AAAA,EACd;AACF;;;ACjBO,IAAM,uBAAuB;AAC7B,IAAM,mBAAmB;AAczB,IAAM,qBAAqB,CAAC,WAA6D;AAAA,EAC9F,MAAM;AAAA,EACN,SAAS;AACX;AAEO,IAAM,iBAAiB,CAC5B,WACA,QACA,YAC0B,EAAE,MAAM,kBAAkB,SAAS,EAAE,WAAW,QAAQ,OAAO,EAAE;;;ACrBtF,IAAM,eAA4B;AAAA,EACvC,cAAc,CAAC;AACjB;AAEO,IAAM,gBAAoD,CAAC,OAAO,WAAW;AAClF,UAAQ,OAAO,MAAM;AAAA,IACnB,KAAK,sBAAsB;AACzB,YAAM,WAAW,OAAO;AACxB,YAAM,YAAY,EAAE,GAAG,MAAM,aAAa;AAE1C,iBAAW,OAAO,UAAU;AAC1B,cAAM,YAAY,OAAO,GAAG;AAC5B,cAAM,WAAW,SAAS,SAAS;AACnC,cAAM,YAAY,UAAU,SAAS,KAAK,CAAC;AAE3C,cAAM,YAAY,UAAU,KAAK,CAAC,MAAM,CAAC,EA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AAA,EAEU,kBAAoC;AAC5C,WAAO;AAAA,MACL,YAAY,KAAK,WAAW,KAAK,IAAI;AAAA,MACrC,iBAAiB,KAAK,eAAe;AAAA,IACvC;AAAA,EACF;AAAA,EAEQ,WAAW,SAA4B;AAC7C,QAAI,CAAC,KAAK,kBAAkB;AAC1B,YAAM,IAAI,MAAM,kCAAkC;AAAA,IACpD;AAEA,SAAK,SAAS,eAAe,QAAQ,WAAW,QAAQ,KAAK,IAAI,WAAW,CAAC;AAE7E,UAAM,OAAO,KAAK,iBAAiB,eAAe;AAAA,MAChD,WAAW,QAAQ;AAAA,MACnB,MAAM,QAAQ,KAAK;AAAA,MACnB,aAAa,QAAQ,KAAK;AAAA,MAC1B,KAAK,QAAQ;AAAA,IACf,CAAC;AAED,SAAK,KAAK,MAAM;AACd,WAAK,SAAS,eAAe,QAAQ,WAAW,QAAQ,KAAK,IAAI,OAAO,CAAC;AAAA,IAC3E,GAAG,qBAAM;AAET,WAAO;AAAA,EACT;AACF;AAxGa,aACK,KAAK;;;AEfhB,IAAM,sBAKT;AAAA,EACF;AAAA,EACA,QAAQ,CAAC,UAAU,SAAS,WAAW,IAAI,aAAa,kBAAkB,UAAU,MAAM;AAAA,EAC1F,SAAS,CAAC,OAAO,WAAW,cAAc,OAAO,MAAM;AAAA,EACvD;AACF;","names":["import_models"]}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/lib/manifest.ts","../src/lib/actions.ts","../src/lib/utils.ts","../src/lib/tiling-plugin.ts","../src/lib/index.ts","../src/lib/reducer.ts"],"sourcesContent":["import { PluginManifest } from '@embedpdf/core';\n\nimport { TilingPluginConfig } from './types';\n\nexport const TILING_PLUGIN_ID = 'tiling';\n\nexport const manifest: PluginManifest<TilingPluginConfig> = {\n id: TILING_PLUGIN_ID,\n name: 'Tiling Plugin',\n version: '1.0.0',\n provides: ['tiling'],\n requires: ['render', 'scroll', 'viewport'],\n optional: [],\n defaultConfig: {\n enabled: true,\n tileSize: 768,\n overlapPx: 2.5,\n extraRings: 0,\n },\n};\n","import { Tile, TileStatus } from './types';\n\nexport const UPDATE_VISIBLE_TILES = 'UPDATE_VISIBLE_TILES';\nexport const MARK_TILE_STATUS = 'MARK_TILE_STATUS';\n\nexport type UpdateVisibleTilesAction = {\n type: typeof UPDATE_VISIBLE_TILES;\n payload: Record<number, Tile[]>;\n};\n\nexport type MarkTileStatusAction = {\n type: typeof MARK_TILE_STATUS;\n payload: { pageIndex: number; tileId: string; status: TileStatus };\n};\n\nexport type TilingAction = UpdateVisibleTilesAction | MarkTileStatusAction;\n\nexport const updateVisibleTiles = (tiles: Record<number, Tile[]>): UpdateVisibleTilesAction => ({\n type: UPDATE_VISIBLE_TILES,\n payload: tiles,\n});\n\nexport const markTileStatus = (\n pageIndex: number,\n tileId: string,\n status: TileStatus,\n): MarkTileStatusAction => ({ type: MARK_TILE_STATUS, payload: { pageIndex, tileId, status } });\n","import { Rect, restoreRect, transformSize } from '@embedpdf/models';\nimport { CalculateTilesForPageOptions, Tile } from './types';\n\n/**\n * Build a grid where neighbouring tiles overlap by `overlapPx`\n * (screen pixels). Inner tiles keep the full `tileSize`, edge\n * tiles are clipped to the page bounds. All screen-space values\n * are rounded to **integers** to avoid sub-pixel seams.\n */\nexport function calculateTilesForPage({\n tileSize = 768,\n overlapPx = 2.5,\n extraRings = 0,\n scale,\n rotation,\n page,\n metric,\n}: CalculateTilesForPageOptions): Tile[] {\n /* ---- work in screen-pixel space -------------------------------- */\n const pageW = page.size.width * scale; // px\n const pageH = page.size.height * scale; // px\n\n const step = tileSize - overlapPx; // shift between tiles\n\n const containerSize = transformSize(page.size, rotation, scale);\n const rotatedVisRect: Rect = {\n origin: { x: metric.scaled.pageX, y: metric.scaled.pageY },\n size: { width: metric.scaled.visibleWidth, height: metric.scaled.visibleHeight },\n };\n const unrotatedVisRect = restoreRect(containerSize, rotatedVisRect, rotation, 1);\n\n const visLeft = unrotatedVisRect.origin.x;\n const visTop = unrotatedVisRect.origin.y;\n const visRight = visLeft + unrotatedVisRect.size.width;\n const visBottom = visTop + unrotatedVisRect.size.height;\n\n const maxCol = Math.floor((pageW - 1) / step);\n const maxRow = Math.floor((pageH - 1) / step);\n\n const startCol = Math.max(0, Math.floor(visLeft / step) - extraRings);\n const endCol = Math.min(maxCol, Math.floor((visRight - 1) / step) + extraRings);\n const startRow = Math.max(0, Math.floor(visTop / step) - extraRings);\n const endRow = Math.min(maxRow, Math.floor((visBottom - 1) / step) + extraRings);\n\n /* ---- build tiles ---------------------------------------------- */\n const tiles: Tile[] = [];\n\n for (let col = startCol; col <= endCol; col++) {\n const xScreen = col * step; // px (integer)\n const wScreen = Math.min(tileSize, pageW - xScreen); // px (≤ tileSize)\n\n const xPage = xScreen / scale; // pt (may be frac.)\n const wPage = wScreen / scale; // pt\n\n for (let row = startRow; row <= endRow; row++) {\n const yScreen = row * step;\n const hScreen = Math.min(tileSize, pageH - yScreen);\n\n const yPage = yScreen / scale;\n const hPage = hScreen / scale;\n\n tiles.push({\n id: `p${page.index}-${scale}-x${xScreen}-y${yScreen}-w${wScreen}-h${hScreen}`,\n col,\n row,\n pageRect: { origin: { x: xPage, y: yPage }, size: { width: wPage, height: hPage } },\n screenRect: {\n origin: { x: xScreen, y: yScreen },\n size: { width: wScreen, height: hScreen },\n },\n status: 'queued',\n srcScale: scale,\n isFallback: false,\n });\n }\n }\n\n return tiles;\n}\n","import {\n BasePlugin,\n CoreState,\n createBehaviorEmitter,\n PluginRegistry,\n StoreState,\n} from '@embedpdf/core';\nimport { ignore } from '@embedpdf/models';\nimport { RenderCapability, RenderPlugin } from '@embedpdf/plugin-render';\nimport { ScrollCapability, ScrollMetrics, ScrollPlugin } from '@embedpdf/plugin-scroll';\nimport { ViewportCapability, ViewportPlugin } from '@embedpdf/plugin-viewport';\n\nimport { markTileStatus, updateVisibleTiles } from './actions';\nimport {\n TilingPluginConfig,\n TilingCapability,\n Tile,\n RenderTileOptions,\n TilingState,\n} from './types';\nimport { calculateTilesForPage } from './utils';\n\nexport class TilingPlugin extends BasePlugin<TilingPluginConfig, TilingCapability> {\n static readonly id = 'tiling' as const;\n\n private readonly tileRendering$ = createBehaviorEmitter<Record<number, Tile[]>>();\n\n private config: TilingPluginConfig;\n private renderCapability: RenderCapability;\n private scrollCapability: ScrollCapability;\n private viewportCapability: ViewportCapability;\n\n constructor(id: string, registry: PluginRegistry, config: TilingPluginConfig) {\n super(id, registry);\n\n this.config = config;\n\n this.renderCapability = this.registry.getPlugin<RenderPlugin>('render')!.provides();\n this.scrollCapability = this.registry.getPlugin<ScrollPlugin>('scroll')!.provides();\n this.viewportCapability = this.registry.getPlugin<ViewportPlugin>('viewport')!.provides();\n\n this.scrollCapability.onScroll((scrollMetrics) => this.calculateVisibleTiles(scrollMetrics), {\n mode: 'throttle',\n wait: 500,\n throttleMode: 'trailing',\n });\n }\n\n async initialize(): Promise<void> {\n // Fetch dependencies from the registry if needed\n }\n\n protected onCoreStoreUpdated(\n oldState: StoreState<CoreState>,\n newState: StoreState<CoreState>,\n ): void {\n if (oldState.core.scale !== newState.core.scale) {\n this.calculateVisibleTiles(\n this.scrollCapability.getMetrics(this.viewportCapability.getMetrics()),\n );\n }\n }\n\n private calculateVisibleTiles(scrollMetrics: ScrollMetrics): void {\n if (!this.config.enabled) {\n this.dispatch(updateVisibleTiles([]));\n return;\n }\n\n const scale = this.coreState.core.scale;\n const rotation = this.coreState.core.rotation;\n const visibleTiles: { [pageIndex: number]: Tile[] } = {};\n\n for (const scrollMetric of scrollMetrics.pageVisibilityMetrics) {\n const pageIndex = scrollMetric.pageNumber - 1; // Convert to 0-based index\n const page = this.coreState.core.document?.pages[pageIndex];\n if (!page) continue;\n\n // Calculate tiles for the page using the utility function\n const tiles = calculateTilesForPage({\n page,\n metric: scrollMetric,\n scale,\n rotation,\n tileSize: this.config.tileSize,\n overlapPx: this.config.overlapPx,\n extraRings: this.config.extraRings,\n });\n\n visibleTiles[pageIndex] = tiles;\n }\n\n this.dispatch(updateVisibleTiles(visibleTiles));\n }\n\n override onStoreUpdated(_prevState: TilingState, newState: TilingState): void {\n this.tileRendering$.emit(newState.visibleTiles);\n }\n\n protected buildCapability(): TilingCapability {\n return {\n renderTile: this.renderTile.bind(this),\n onTileRendering: this.tileRendering$.on,\n };\n }\n\n private renderTile(options: RenderTileOptions) {\n if (!this.renderCapability) {\n throw new Error('Render capability not available.');\n }\n\n this.dispatch(markTileStatus(options.pageIndex, options.tile.id, 'rendering'));\n\n const task = this.renderCapability.renderPageRect({\n pageIndex: options.pageIndex,\n rect: options.tile.pageRect,\n scaleFactor: options.tile.srcScale,\n dpr: options.dpr,\n });\n\n task.wait(() => {\n this.dispatch(markTileStatus(options.pageIndex, options.tile.id, 'ready'));\n }, ignore);\n\n return task;\n }\n}\n","import { PluginPackage } from '@embedpdf/core';\n\nimport { TilingAction } from './actions';\nimport { manifest, TILING_PLUGIN_ID } from './manifest';\nimport { initialState, tilingReducer } from './reducer';\nimport { TilingPlugin } from './tiling-plugin';\nimport { TilingPluginConfig, TilingState } from './types';\n\nexport const TilingPluginPackage: PluginPackage<\n TilingPlugin,\n TilingPluginConfig,\n TilingState,\n TilingAction\n> = {\n manifest,\n create: (registry, _engine, config) => new TilingPlugin(TILING_PLUGIN_ID, registry, config),\n reducer: (state, action) => tilingReducer(state, action),\n initialState,\n};\n\nexport * from './tiling-plugin';\nexport * from './types';\nexport * from './manifest';\n","import { Reducer } from '@embedpdf/core';\n\nimport { UPDATE_VISIBLE_TILES, MARK_TILE_STATUS, TilingAction } from './actions';\nimport { Tile, TilingState } from './types';\n\nexport const initialState: TilingState = {\n visibleTiles: {},\n};\n\nexport const tilingReducer: Reducer<TilingState, TilingAction> = (state, action) => {\n switch (action.type) {\n case UPDATE_VISIBLE_TILES: {\n const incoming = action.payload; // Record<number, Tile[]>\n const nextPages = { ...state.visibleTiles };\n\n for (const key in incoming) {\n const pageIndex = Number(key);\n const newTiles = incoming[pageIndex]; // all isFallback=false\n const prevTiles = nextPages[pageIndex] ?? [];\n\n const prevScale = prevTiles.find((t) => !t.isFallback)?.srcScale;\n const newScale = newTiles[0].srcScale;\n const zoomChanged = prevScale !== undefined && prevScale !== newScale;\n\n if (zoomChanged) {\n /* 1️⃣ ready tiles from the old zoom → new fallback */\n const promoted = prevTiles\n .filter((t) => !t.isFallback && t.status === 'ready')\n .map((t) => ({ ...t, isFallback: true }));\n\n /* 2️⃣ decide which fallback tiles to keep */\n const fallbackToCarry = promoted.length > 0 ? [] : prevTiles.filter((t) => t.isFallback);\n\n /* 3️⃣ final list = (maybe-kept fallback) + promoted + newTiles */\n nextPages[pageIndex] = [...fallbackToCarry, ...promoted, ...newTiles];\n } else {\n /* same zoom → keep current fallback, replace visible */\n const newIds = new Set(newTiles.map((t) => t.id));\n const keepers: Tile[] = []; // where we’ll collect surviving tiles\n const seenIds = new Set<string>();\n\n /* 2️⃣ loop prevTiles once */\n for (const t of prevTiles) {\n if (t.isFallback) {\n keepers.push(t); // always keep fallback\n seenIds.add(t.id);\n } else if (newIds.has(t.id)) {\n keepers.push(t); // keep old visible tile (preserves status)\n seenIds.add(t.id);\n }\n }\n\n /* 3️⃣ append *brand-new* tiles (not yet kept) */\n for (const t of newTiles) {\n if (!seenIds.has(t.id)) keepers.push(t);\n }\n\n /* 4️⃣ store result */\n nextPages[pageIndex] = keepers;\n }\n }\n\n return { ...state, visibleTiles: nextPages };\n }\n\n case MARK_TILE_STATUS: {\n const { pageIndex, tileId, status } = action.payload;\n const tiles =\n state.visibleTiles[pageIndex]?.map((t) =>\n t.id === tileId ? ({ ...t, status } as Tile) : t,\n ) ?? [];\n\n const newTiles = tiles.filter((t) => !t.isFallback);\n const allReady = newTiles.every((t) => t.status === 'ready');\n const finalTiles = allReady ? newTiles : tiles;\n\n return {\n ...state,\n visibleTiles: { ...state.visibleTiles, [pageIndex]: finalTiles },\n };\n }\n\n default:\n return state;\n }\n};\n"],"names":["TILING_PLUGIN_ID","manifest","id","name","version","provides","requires","optional","defaultConfig","enabled","tileSize","overlapPx","extraRings","UPDATE_VISIBLE_TILES","MARK_TILE_STATUS","updateVisibleTiles","tiles","type","payload","markTileStatus","pageIndex","tileId","status","calculateTilesForPage","scale","rotation","page","metric","pageW","size","width","pageH","height","step","containerSize","transformSize","rotatedVisRect","origin","x","scaled","pageX","y","pageY","visibleWidth","visibleHeight","unrotatedVisRect","restoreRect","visLeft","visTop","visRight","visBottom","maxCol","Math","floor","maxRow","startCol","max","endCol","min","startRow","endRow","col","xScreen","wScreen","xPage","wPage","row","yScreen","hScreen","yPage","hPage","push","index","pageRect","screenRect","srcScale","isFallback","_TilingPlugin","BasePlugin","constructor","registry","config","super","this","tileRendering$","createBehaviorEmitter","renderCapability","getPlugin","scrollCapability","viewportCapability","onScroll","scrollMetrics","calculateVisibleTiles","mode","wait","throttleMode","initialize","onCoreStoreUpdated","oldState","newState","core","getMetrics","dispatch","coreState","visibleTiles","scrollMetric","pageVisibilityMetrics","pageNumber","_a","document","pages","onStoreUpdated","_prevState","emit","buildCapability","renderTile","bind","onTileRendering","on","options","Error","tile","task","renderPageRect","rect","scaleFactor","dpr","ignore","TilingPlugin","TilingPluginPackage","create","_engine","reducer","state","action","incoming","nextPages","key","Number","newTiles","prevTiles","prevScale","find","t","newScale","promoted","filter","map","fallbackToCarry","length","newIds","Set","keepers","seenIds","has","add","_b","finalTiles","every","tilingReducer","initialState"],"mappings":"gJAIaA,EAAmB,SAEnBC,EAA+C,CAC1DC,GAAIF,EACJG,KAAM,gBACNC,QAAS,QACTC,SAAU,CAAC,UACXC,SAAU,CAAC,SAAU,SAAU,YAC/BC,SAAU,GACVC,cAAe,CACbC,SAAS,EACTC,SAAU,IACVC,UAAW,IACXC,WAAY,ICfHC,EAAuB,uBACvBC,EAAmB,mBAcnBC,EAAsBC,IAA6D,CAC9FC,KAAMJ,EACNK,QAASF,IAGEG,EAAiB,CAC5BC,EACAC,EACAC,KAC0B,CAAEL,KAAMH,EAAkBI,QAAS,CAAEE,YAAWC,SAAQC,YCjB7E,SAASC,GAAsBb,S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7I,KAAOmB,EAAU,IAAK0H,EAAGzH,UAAoByH,MAC5C,GAEDJ,EAAW3H,EAAMkI,QAAQH,IAAOA,EAAEnE,aAElCiF,EADWlB,EAASmB,OAAOf,GAAmB,UAAbA,EAAEzH,SACXqH,EAAW3H,EAElC,MAAA,IACFqH,EACH7B,aAAc,IAAK6B,EAAM7B,aAAcpF,CAACA,GAAYyI,GACtD,CAGF,QACS,OAAAxB,EAAA,EDnEiB0B,CAAc1B,EAAOC,GACjD0B,aCZuC,CACvCxD,aAAc,CAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,86 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { Rect, Task, PdfErrorReason, Rotation, PdfPageObject } from '@embedpdf/models';
|
|
3
|
-
import { PageVisibilityMetrics } from '@embedpdf/plugin-scroll';
|
|
4
|
-
|
|
5
|
-
interface TilingPluginConfig extends BasePluginConfig {
|
|
6
|
-
tileSize: number;
|
|
7
|
-
overlapPx: number;
|
|
8
|
-
extraRings: number;
|
|
9
|
-
}
|
|
10
|
-
interface VisibleRect {
|
|
11
|
-
pageX: number;
|
|
12
|
-
pageY: number;
|
|
13
|
-
visibleWidth: number;
|
|
14
|
-
visibleHeight: number;
|
|
15
|
-
}
|
|
16
|
-
type TileStatus = 'queued' | 'rendering' | 'ready';
|
|
17
|
-
interface Tile {
|
|
18
|
-
status: TileStatus;
|
|
19
|
-
screenRect: Rect;
|
|
20
|
-
pageRect: Rect;
|
|
21
|
-
isFallback: boolean;
|
|
22
|
-
srcScale: number;
|
|
23
|
-
col: number;
|
|
24
|
-
row: number;
|
|
25
|
-
id: string;
|
|
26
|
-
}
|
|
27
|
-
interface TilingState {
|
|
28
|
-
visibleTiles: Record<number, Tile[]>;
|
|
29
|
-
}
|
|
30
|
-
interface TilingCapability {
|
|
31
|
-
renderTile: (options: RenderTileOptions) => Task<Blob, PdfErrorReason>;
|
|
32
|
-
onTileRendering: EventHook<Record<number, Tile[]>>;
|
|
33
|
-
}
|
|
34
|
-
interface CalculateTilesForPageOptions {
|
|
35
|
-
tileSize: number;
|
|
36
|
-
overlapPx: number;
|
|
37
|
-
extraRings: number;
|
|
38
|
-
scale: number;
|
|
39
|
-
rotation: Rotation;
|
|
40
|
-
page: PdfPageObject;
|
|
41
|
-
metric: PageVisibilityMetrics;
|
|
42
|
-
}
|
|
43
|
-
interface RenderTileOptions {
|
|
44
|
-
pageIndex: number;
|
|
45
|
-
tile: Tile;
|
|
46
|
-
dpr: number;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
declare const UPDATE_VISIBLE_TILES = "UPDATE_VISIBLE_TILES";
|
|
50
|
-
declare const MARK_TILE_STATUS = "MARK_TILE_STATUS";
|
|
51
|
-
type UpdateVisibleTilesAction = {
|
|
52
|
-
type: typeof UPDATE_VISIBLE_TILES;
|
|
53
|
-
payload: Record<number, Tile[]>;
|
|
54
|
-
};
|
|
55
|
-
type MarkTileStatusAction = {
|
|
56
|
-
type: typeof MARK_TILE_STATUS;
|
|
57
|
-
payload: {
|
|
58
|
-
pageIndex: number;
|
|
59
|
-
tileId: string;
|
|
60
|
-
status: TileStatus;
|
|
61
|
-
};
|
|
62
|
-
};
|
|
63
|
-
type TilingAction = UpdateVisibleTilesAction | MarkTileStatusAction;
|
|
64
|
-
|
|
65
|
-
declare class TilingPlugin extends BasePlugin<TilingPluginConfig, TilingCapability> {
|
|
66
|
-
static readonly id: "tiling";
|
|
67
|
-
private readonly tileRendering$;
|
|
68
|
-
private config;
|
|
69
|
-
private renderCapability;
|
|
70
|
-
private scrollCapability;
|
|
71
|
-
private viewportCapability;
|
|
72
|
-
constructor(id: string, registry: PluginRegistry, config: TilingPluginConfig);
|
|
73
|
-
initialize(): Promise<void>;
|
|
74
|
-
protected onCoreStoreUpdated(oldState: StoreState<CoreState>, newState: StoreState<CoreState>): void;
|
|
75
|
-
private calculateVisibleTiles;
|
|
76
|
-
onStoreUpdated(_prevState: TilingState, newState: TilingState): void;
|
|
77
|
-
protected buildCapability(): TilingCapability;
|
|
78
|
-
private renderTile;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
declare const TILING_PLUGIN_ID = "tiling";
|
|
82
|
-
declare const manifest: PluginManifest<TilingPluginConfig>;
|
|
83
|
-
|
|
84
|
-
declare const TilingPluginPackage: PluginPackage<TilingPlugin, TilingPluginConfig, TilingState, TilingAction>;
|
|
85
|
-
|
|
86
|
-
export { type CalculateTilesForPageOptions, type RenderTileOptions, TILING_PLUGIN_ID, type Tile, type TileStatus, type TilingCapability, TilingPlugin, type TilingPluginConfig, TilingPluginPackage, type TilingState, type VisibleRect, manifest };
|
|
1
|
+
export * from './lib';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { BasePlugin, createBehaviorEmitter } from "@embedpdf/core";
|
|
2
|
+
import { transformSize, restoreRect, ignore } from "@embedpdf/models";
|
|
3
|
+
const TILING_PLUGIN_ID = "tiling";
|
|
4
|
+
const manifest = {
|
|
4
5
|
id: TILING_PLUGIN_ID,
|
|
5
6
|
name: "Tiling Plugin",
|
|
6
7
|
version: "1.0.0",
|
|
@@ -14,21 +15,18 @@ var manifest = {
|
|
|
14
15
|
extraRings: 0
|
|
15
16
|
}
|
|
16
17
|
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
var MARK_TILE_STATUS = "MARK_TILE_STATUS";
|
|
21
|
-
var updateVisibleTiles = (tiles) => ({
|
|
18
|
+
const UPDATE_VISIBLE_TILES = "UPDATE_VISIBLE_TILES";
|
|
19
|
+
const MARK_TILE_STATUS = "MARK_TILE_STATUS";
|
|
20
|
+
const updateVisibleTiles = (tiles) => ({
|
|
22
21
|
type: UPDATE_VISIBLE_TILES,
|
|
23
22
|
payload: tiles
|
|
24
23
|
});
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
// src/lib/reducer.ts
|
|
28
|
-
var initialState = {
|
|
24
|
+
const markTileStatus = (pageIndex, tileId, status) => ({ type: MARK_TILE_STATUS, payload: { pageIndex, tileId, status } });
|
|
25
|
+
const initialState = {
|
|
29
26
|
visibleTiles: {}
|
|
30
27
|
};
|
|
31
|
-
|
|
28
|
+
const tilingReducer = (state, action) => {
|
|
29
|
+
var _a, _b;
|
|
32
30
|
switch (action.type) {
|
|
33
31
|
case UPDATE_VISIBLE_TILES: {
|
|
34
32
|
const incoming = action.payload;
|
|
@@ -37,7 +35,7 @@ var tilingReducer = (state, action) => {
|
|
|
37
35
|
const pageIndex = Number(key);
|
|
38
36
|
const newTiles = incoming[pageIndex];
|
|
39
37
|
const prevTiles = nextPages[pageIndex] ?? [];
|
|
40
|
-
const prevScale = prevTiles.find((t) => !t.isFallback)
|
|
38
|
+
const prevScale = (_a = prevTiles.find((t) => !t.isFallback)) == null ? void 0 : _a.srcScale;
|
|
41
39
|
const newScale = newTiles[0].srcScale;
|
|
42
40
|
const zoomChanged = prevScale !== void 0 && prevScale !== newScale;
|
|
43
41
|
if (zoomChanged) {
|
|
@@ -67,9 +65,9 @@ var tilingReducer = (state, action) => {
|
|
|
67
65
|
}
|
|
68
66
|
case MARK_TILE_STATUS: {
|
|
69
67
|
const { pageIndex, tileId, status } = action.payload;
|
|
70
|
-
const tiles = state.visibleTiles[pageIndex]
|
|
68
|
+
const tiles = ((_b = state.visibleTiles[pageIndex]) == null ? void 0 : _b.map(
|
|
71
69
|
(t) => t.id === tileId ? { ...t, status } : t
|
|
72
|
-
) ?? [];
|
|
70
|
+
)) ?? [];
|
|
73
71
|
const newTiles = tiles.filter((t) => !t.isFallback);
|
|
74
72
|
const allReady = newTiles.every((t) => t.status === "ready");
|
|
75
73
|
const finalTiles = allReady ? newTiles : tiles;
|
|
@@ -82,16 +80,6 @@ var tilingReducer = (state, action) => {
|
|
|
82
80
|
return state;
|
|
83
81
|
}
|
|
84
82
|
};
|
|
85
|
-
|
|
86
|
-
// src/lib/tiling-plugin.ts
|
|
87
|
-
import {
|
|
88
|
-
BasePlugin,
|
|
89
|
-
createBehaviorEmitter
|
|
90
|
-
} from "@embedpdf/core";
|
|
91
|
-
import { ignore } from "@embedpdf/models";
|
|
92
|
-
|
|
93
|
-
// src/lib/utils.ts
|
|
94
|
-
import { restoreRect, transformSize } from "@embedpdf/models";
|
|
95
83
|
function calculateTilesForPage({
|
|
96
84
|
tileSize = 768,
|
|
97
85
|
overlapPx = 2.5,
|
|
@@ -148,9 +136,7 @@ function calculateTilesForPage({
|
|
|
148
136
|
}
|
|
149
137
|
return tiles;
|
|
150
138
|
}
|
|
151
|
-
|
|
152
|
-
// src/lib/tiling-plugin.ts
|
|
153
|
-
var TilingPlugin = class extends BasePlugin {
|
|
139
|
+
const _TilingPlugin = class _TilingPlugin extends BasePlugin {
|
|
154
140
|
constructor(id, registry, config) {
|
|
155
141
|
super(id, registry);
|
|
156
142
|
this.tileRendering$ = createBehaviorEmitter();
|
|
@@ -174,6 +160,7 @@ var TilingPlugin = class extends BasePlugin {
|
|
|
174
160
|
}
|
|
175
161
|
}
|
|
176
162
|
calculateVisibleTiles(scrollMetrics) {
|
|
163
|
+
var _a;
|
|
177
164
|
if (!this.config.enabled) {
|
|
178
165
|
this.dispatch(updateVisibleTiles([]));
|
|
179
166
|
return;
|
|
@@ -183,7 +170,7 @@ var TilingPlugin = class extends BasePlugin {
|
|
|
183
170
|
const visibleTiles = {};
|
|
184
171
|
for (const scrollMetric of scrollMetrics.pageVisibilityMetrics) {
|
|
185
172
|
const pageIndex = scrollMetric.pageNumber - 1;
|
|
186
|
-
const page = this.coreState.core.document
|
|
173
|
+
const page = (_a = this.coreState.core.document) == null ? void 0 : _a.pages[pageIndex];
|
|
187
174
|
if (!page) continue;
|
|
188
175
|
const tiles = calculateTilesForPage({
|
|
189
176
|
page,
|
|
@@ -224,10 +211,9 @@ var TilingPlugin = class extends BasePlugin {
|
|
|
224
211
|
return task;
|
|
225
212
|
}
|
|
226
213
|
};
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
var TilingPluginPackage = {
|
|
214
|
+
_TilingPlugin.id = "tiling";
|
|
215
|
+
let TilingPlugin = _TilingPlugin;
|
|
216
|
+
const TilingPluginPackage = {
|
|
231
217
|
manifest,
|
|
232
218
|
create: (registry, _engine, config) => new TilingPlugin(TILING_PLUGIN_ID, registry, config),
|
|
233
219
|
reducer: (state, action) => tilingReducer(state, action),
|
|
@@ -239,4 +225,4 @@ export {
|
|
|
239
225
|
TilingPluginPackage,
|
|
240
226
|
manifest
|
|
241
227
|
};
|
|
242
|
-
//# sourceMappingURL=index.js.map
|
|
228
|
+
//# sourceMappingURL=index.js.map
|