@embedpdf-editor/vue3-chapter-viewer 1.0.7-beta.5 → 1.0.7-beta.6

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.js CHANGED
@@ -91,7 +91,7 @@ function Ue$1(e14) {
91
91
  }
92
92
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
93
93
  }
94
- let Fe$1 = class Fe {
94
+ let Ne$1 = class Ne {
95
95
  constructor(e14) {
96
96
  this.cb = e14;
97
97
  }
@@ -99,7 +99,7 @@ let Fe$1 = class Fe {
99
99
  return this.cb(e14, t2);
100
100
  }
101
101
  };
102
- const ze$1 = "chapter-manager", Ge$1 = { id: ze$1, name: "Chapter Manager Plugin", version: "0.1.0", provides: ["chapter-manager"], requires: ["document-manager"], optional: [], defaultConfig: { manifest: { chapters: [] }, prefetchChapters: 1, maxConcurrentSegmentLoads: 1, unloadTimeoutMs: 6e4, autoActivateOnLoad: true } };
102
+ const Ge$1 = "chapter-manager", Re$1 = { id: Ge$1, name: "Chapter Manager Plugin", version: "0.1.0", provides: ["chapter-manager"], requires: ["document-manager"], optional: [], defaultConfig: { manifest: { chapters: [] }, prefetchChapters: 1, maxConcurrentSegmentLoads: 1, unloadTimeoutMs: 6e4, autoActivateOnLoad: true } };
103
103
  function _e$1(e14) {
104
104
  const [t2, s2] = e14.localPageRange;
105
105
  return s2 - t2 + 1;
@@ -317,10 +317,9 @@ const st$1 = class e extends BasePlugin {
317
317
  if (e14 <= 0 || 0 === this.manifest.chapters.length) return;
318
318
  const t2 = Date.now(), s2 = Math.min(e14, this.manifest.chapters.length);
319
319
  for (let n9 = 0; n9 < s2; n9++) {
320
- const e15 = this.manifest.chapters[n9].chapterId;
321
- this.chapterLastUsed.set(e15, t2);
322
- const s3 = this.chapterStatus.get(e15) ?? "idle";
323
- "idle" !== s3 && "closed" !== s3 && "error" !== s3 || this.ensureChapterLoaded(e15);
320
+ const e15 = this.manifest.chapters[n9];
321
+ this.chapterLastUsed.set(e15.chapterId, t2), this.resolveChapterUrls(e15).catch(() => {
322
+ });
324
323
  }
325
324
  }
326
325
  findChapter(e14) {
@@ -352,11 +351,7 @@ const st$1 = class e extends BasePlugin {
352
351
  }
353
352
  isSegmentLoadPending(e14, t2) {
354
353
  const s2 = this.resolveSegmentDocumentId(e14, t2);
355
- if (!s2) return false;
356
- if (this.pendingSegmentLoadPromises.has(s2)) return true;
357
- if (0 === t2 && this.pendingChapterLoadPromises.has(e14)) return true;
358
- const n9 = this.coreState.core.documents[s2];
359
- return "loading" === (null == n9 ? void 0 : n9.status);
354
+ return !!s2 && (!!this.pendingSegmentLoadPromises.has(s2) || !(0 !== t2 || !this.pendingChapterLoadPromises.has(e14)));
360
355
  }
361
356
  syncChapterStatusFromDocuments(e14) {
362
357
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -387,7 +382,7 @@ const st$1 = class e extends BasePlugin {
387
382
  clearVisibleSegmentBacklog() {
388
383
  this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
389
384
  }
390
- globalSegmentSortKey(e14, t2) {
385
+ segmentStartGlobalIndex(e14, t2) {
391
386
  var s2;
392
387
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
393
388
  const n9 = this.findChapter(e14);
@@ -400,12 +395,12 @@ const st$1 = class e extends BasePlugin {
400
395
  const t2 = [...e14].sort((e15, t3) => e15.priority - t3.priority), s2 = /* @__PURE__ */ new Set(), n9 = [];
401
396
  for (const r2 of t2) {
402
397
  const e15 = `${r2.chapterId}:${r2.segmentIndex}`;
403
- s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || this.isSegmentLoadPending(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
398
+ s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
404
399
  }
405
400
  this.visibleSegmentBacklog = n9, this.pumpVisibleSegmentBacklog();
406
401
  }
407
402
  pumpVisibleSegmentBacklog() {
408
- const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
403
+ const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
409
404
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
410
405
  const e15 = this.visibleSegmentBacklog.shift();
411
406
  this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
@@ -427,8 +422,8 @@ const st$1 = class e extends BasePlugin {
427
422
  if (!s3) return;
428
423
  const n10 = je$1(s3);
429
424
  if (n10 && t3 >= n10.segments.length) return;
430
- const r3 = `${e15}:${t3}`, a3 = this.globalSegmentSortKey(e15, t3), o3 = i2.get(r3);
431
- (void 0 === o3 || a3 < o3) && i2.set(r3, a3);
425
+ const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), l2 = Math.abs(o3 - r2), h3 = i2.get(a3);
426
+ (void 0 === h3 || l2 < h3) && i2.set(a3, l2);
432
427
  };
433
428
  for (const d2 of e14) {
434
429
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -649,7 +644,7 @@ const st$1 = class e extends BasePlugin {
649
644
  };
650
645
  st$1.id = "chapter-manager";
651
646
  let nt$1 = st$1;
652
- const rt$1 = { manifest: Ge$1, create: (e14) => new nt$1(ze$1, e14), reducer: (e14) => e14, initialState: {} }, at$1 = "chapter-scroll", it = { id: at$1, name: "Chapter Scroll Plugin", version: "0.1.0", provides: ["chapter-scroll"], requires: ["chapter-manager"], optional: [], defaultConfig: { placeholderPageHeight: 1200, placeholderPageWidth: 900, bufferSize: 2, pageGap: 20 } };
647
+ const rt$1 = { manifest: Re$1, create: (e14) => new nt$1(Ge$1, e14), reducer: (e14) => e14, initialState: {} }, at$1 = "chapter-scroll", it = { id: at$1, name: "Chapter Scroll Plugin", version: "0.1.0", provides: ["chapter-scroll"], requires: ["chapter-manager"], optional: [], defaultConfig: { placeholderPageHeight: 1200, placeholderPageWidth: 900, bufferSize: 2, pageGap: 20 } };
653
648
  function ot(e14) {
654
649
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
655
650
  }
@@ -899,10 +894,9 @@ const Mt$1 = class e2 extends BasePlugin {
899
894
  if (e14 <= 0 || 0 === this.manifest.chapters.length) return;
900
895
  const t2 = Date.now(), s2 = Math.min(e14, this.manifest.chapters.length);
901
896
  for (let n9 = 0; n9 < s2; n9++) {
902
- const e15 = this.manifest.chapters[n9].chapterId;
903
- this.chapterLastUsed.set(e15, t2);
904
- const s3 = this.chapterStatus.get(e15) ?? "idle";
905
- "idle" !== s3 && "closed" !== s3 && "error" !== s3 || this.ensureChapterLoaded(e15);
897
+ const e15 = this.manifest.chapters[n9];
898
+ this.chapterLastUsed.set(e15.chapterId, t2), this.resolveChapterUrls(e15).catch(() => {
899
+ });
906
900
  }
907
901
  }
908
902
  findChapter(e14) {
@@ -934,11 +928,7 @@ const Mt$1 = class e2 extends BasePlugin {
934
928
  }
935
929
  isSegmentLoadPending(e14, t2) {
936
930
  const s2 = this.resolveSegmentDocumentId(e14, t2);
937
- if (!s2) return false;
938
- if (this.pendingSegmentLoadPromises.has(s2)) return true;
939
- if (0 === t2 && this.pendingChapterLoadPromises.has(e14)) return true;
940
- const n9 = this.coreState.core.documents[s2];
941
- return "loading" === (null == n9 ? void 0 : n9.status);
931
+ return !!s2 && (!!this.pendingSegmentLoadPromises.has(s2) || !(0 !== t2 || !this.pendingChapterLoadPromises.has(e14)));
942
932
  }
943
933
  syncChapterStatusFromDocuments(e14) {
944
934
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -969,7 +959,7 @@ const Mt$1 = class e2 extends BasePlugin {
969
959
  clearVisibleSegmentBacklog() {
970
960
  this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
971
961
  }
972
- globalSegmentSortKey(e14, t2) {
962
+ segmentStartGlobalIndex(e14, t2) {
973
963
  var s2;
974
964
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
975
965
  const n9 = this.findChapter(e14);
@@ -982,12 +972,12 @@ const Mt$1 = class e2 extends BasePlugin {
982
972
  const t2 = [...e14].sort((e15, t3) => e15.priority - t3.priority), s2 = /* @__PURE__ */ new Set(), n9 = [];
983
973
  for (const r2 of t2) {
984
974
  const e15 = `${r2.chapterId}:${r2.segmentIndex}`;
985
- s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || this.isSegmentLoadPending(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
975
+ s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
986
976
  }
987
977
  this.visibleSegmentBacklog = n9, this.pumpVisibleSegmentBacklog();
988
978
  }
989
979
  pumpVisibleSegmentBacklog() {
990
- const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
980
+ const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
991
981
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
992
982
  const e15 = this.visibleSegmentBacklog.shift();
993
983
  this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
@@ -1009,8 +999,8 @@ const Mt$1 = class e2 extends BasePlugin {
1009
999
  if (!s3) return;
1010
1000
  const n10 = vt$1(s3);
1011
1001
  if (n10 && t3 >= n10.segments.length) return;
1012
- const r3 = `${e15}:${t3}`, a3 = this.globalSegmentSortKey(e15, t3), o3 = i2.get(r3);
1013
- (void 0 === o3 || a3 < o3) && i2.set(r3, a3);
1002
+ const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), l2 = Math.abs(o3 - r2), h3 = i2.get(a3);
1003
+ (void 0 === h3 || l2 < h3) && i2.set(a3, l2);
1014
1004
  };
1015
1005
  for (const d2 of e14) {
1016
1006
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -1242,13 +1232,13 @@ function Lt$1(e14, t2) {
1242
1232
  }
1243
1233
  const $t$1 = class $t extends BasePlugin {
1244
1234
  constructor(e14, t2) {
1245
- super(e14, t2), this.layoutChange$ = createBehaviorEmitter(void 0, Lt$1), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.loadPushRaf = null, this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(xt$1.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
1235
+ super(e14, t2), this.layoutChange$ = createBehaviorEmitter(void 0, Lt$1), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(xt$1.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
1246
1236
  }
1247
1237
  async initialize(e14) {
1248
1238
  this.config = { placeholderPageHeight: e14.placeholderPageHeight ?? 1200, placeholderPageWidth: e14.placeholderPageWidth ?? 900, bufferSize: e14.bufferSize ?? 2, pageGap: e14.pageGap ?? 20 }, this.virtualPageMap = this.chapterManager.provides().getVirtualPageMap(), this.rebuildOffsets(), this.chapterManagerUnsubs.push(this.chapterManager.provides().onManifestChange(({ map: e15 }) => {
1249
1239
  this.virtualPageMap = e15, this.pageSizes.clear(), this.lastLoadIndicesKey = "", this.rebuildOffsets(), this.syncReadyChapterSizes(), this.recomputeVisible();
1250
1240
  }), this.chapterManager.provides().onChapterStatusChange((e15) => {
1251
- "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads()));
1241
+ "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.pushVisibleLoads()));
1252
1242
  })), this.syncReadyChapterSizes();
1253
1243
  }
1254
1244
  isChapterDocumentReady(e14) {
@@ -1268,11 +1258,6 @@ const $t$1 = class $t extends BasePlugin {
1268
1258
  refreshLayout() {
1269
1259
  this.layoutChange$.emit(this.computeLayout());
1270
1260
  }
1271
- schedulePushVisibleLoads() {
1272
- null == this.loadPushRaf && (this.loadPushRaf = requestAnimationFrame(() => {
1273
- this.loadPushRaf = null, this.pushVisibleLoads();
1274
- }));
1275
- }
1276
1261
  pushVisibleLoads() {
1277
1262
  if (!this.virtualPageMap || 0 === this.currentVisible.length) return;
1278
1263
  const e14 = this.expandIndicesWithBuffer(this.currentVisible, this.virtualPageMap.totalPages);
@@ -1291,7 +1276,7 @@ const $t$1 = class $t extends BasePlugin {
1291
1276
  const { chapterId: s2 } = ht(e14);
1292
1277
  if (0 === ((null == (t2 = this.virtualPageMap) ? void 0 : t2.pagesOfChapter(s2).length) ?? 0)) return;
1293
1278
  const n9 = this.documentManager.provides();
1294
- n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads();
1279
+ n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.pushVisibleLoads();
1295
1280
  }
1296
1281
  onDocumentClosed(e14) {
1297
1282
  var t2;
@@ -1305,7 +1290,7 @@ const $t$1 = class $t extends BasePlugin {
1305
1290
  var e14;
1306
1291
  super.destroy();
1307
1292
  for (const t2 of this.chapterManagerUnsubs) t2();
1308
- this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, null != this.loadPushRaf && (cancelAnimationFrame(this.loadPushRaf), this.loadPushRaf = null), this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
1293
+ this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
1309
1294
  }
1310
1295
  getPageSize(e14) {
1311
1296
  return this.pageSizes.get(e14) ?? { width: this.config.placeholderPageWidth, height: this.config.placeholderPageHeight };
@@ -1414,7 +1399,7 @@ const $t$1 = class $t extends BasePlugin {
1414
1399
  const e15 = this.offsets[l], n10 = e15 + this.getPageSize(l).height, r3 = Math.max(0, Math.min(n10, s2) - Math.max(e15, t2));
1415
1400
  r3 > o2 && (o2 = r3, i2 = l);
1416
1401
  }
1417
- this.currentVisible = a2, this.currentGlobalPageIndex = i2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: i2 }), this.schedulePushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
1402
+ this.currentVisible = a2, this.currentGlobalPageIndex = i2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: i2 }), this.pushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
1418
1403
  }
1419
1404
  findFirstVisibleIndex(e14) {
1420
1405
  let t2 = 0, s2 = this.offsets.length - 1, n9 = 0;
@@ -1468,13 +1453,13 @@ function Tt$1(e14) {
1468
1453
  const [t2, s2] = e14.localPageRange;
1469
1454
  return s2 - t2 + 1;
1470
1455
  }
1471
- function Ft$1(e14) {
1456
+ function Nt$1(e14) {
1472
1457
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
1473
1458
  }
1474
- function Nt$1(e14) {
1459
+ function Ft$1(e14) {
1475
1460
  if (null != e14.segmentPageThreshold && e14.segmentPageThreshold > 0) return e14.segmentPageThreshold;
1476
1461
  const t2 = e14.source;
1477
- return Ft$1(t2) ? t2.segmentPageThreshold : null;
1462
+ return Nt$1(t2) ? t2.segmentPageThreshold : null;
1478
1463
  }
1479
1464
  function At$1(e14, t2) {
1480
1465
  return `${e14}#s${t2}`;
@@ -1488,10 +1473,10 @@ function Vt$1(e14) {
1488
1473
  function Ot$1(e14, t2) {
1489
1474
  return e14 <= 0 || t2 <= 0 ? 0 : Math.ceil(e14 / t2);
1490
1475
  }
1491
- function Rt$1(e14) {
1492
- const t2 = Nt$1(e14);
1476
+ function zt$1(e14) {
1477
+ const t2 = Ft$1(e14);
1493
1478
  if (!t2) return null;
1494
- const s2 = Tt$1(e14), n9 = Ot$1(s2, t2), r2 = Ft$1(e14.source) ? e14.source.urls : void 0, a2 = [];
1479
+ const s2 = Tt$1(e14), n9 = Ot$1(s2, t2), r2 = Nt$1(e14.source) ? e14.source.urls : void 0, a2 = [];
1495
1480
  for (let i2 = 0; i2 < n9; i2++) {
1496
1481
  const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
1497
1482
  if (n10 >= s2) break;
@@ -1499,8 +1484,8 @@ function Rt$1(e14) {
1499
1484
  }
1500
1485
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
1501
1486
  }
1502
- function zt$1(e14, t2) {
1503
- const s2 = Rt$1(e14);
1487
+ function Gt$1(e14, t2) {
1488
+ const s2 = zt$1(e14);
1504
1489
  if (!s2) return { documentId: e14.chapterId, pageIndex: t2 };
1505
1490
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
1506
1491
  if (!n9) {
@@ -1509,20 +1494,20 @@ function zt$1(e14, t2) {
1509
1494
  }
1510
1495
  return { documentId: n9.documentId, pageIndex: t2 - n9.localPageStart };
1511
1496
  }
1512
- function Gt$1(e14, t2) {
1513
- const s2 = Rt$1(e14);
1497
+ function Rt$1(e14, t2) {
1498
+ const s2 = zt$1(e14);
1514
1499
  if (!s2) return 0;
1515
1500
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
1516
1501
  return (null == n9 ? void 0 : n9.index) ?? Math.floor(t2 / s2.threshold);
1517
1502
  }
1518
1503
  function _t$1(e14) {
1519
1504
  var t2;
1520
- const s2 = Nt$1(e14);
1505
+ const s2 = Ft$1(e14);
1521
1506
  if (!s2) return [];
1522
1507
  const n9 = [], { chapterId: r2 } = e14, a2 = Tt$1(e14);
1523
1508
  s2 <= 0 && n9.push(`${r2}: segmentPageThreshold must be > 0`);
1524
1509
  const i2 = e14.source;
1525
- if (!Ft$1(i2)) return n9;
1510
+ if (!Nt$1(i2)) return n9;
1526
1511
  0 === i2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
1527
1512
  const o2 = Ot$1(a2, s2);
1528
1513
  i2.urls.length !== o2 && n9.push(`${r2}: source.urls.length (${i2.urls.length}) must equal ceil(pageCount/threshold) (${o2})`);
@@ -1631,10 +1616,10 @@ const Kt$1 = class e3 extends BasePlugin {
1631
1616
  t2 && (this.overlapStrategy = t2), this.setManifestInternal(e14);
1632
1617
  }, getManifest: () => this.manifest, getVirtualPageMap: () => this.virtualPageMap, setVisibleGlobalPages: (e14) => this.handleVisibleChange(e14), visibleRangeNeedsSegmentLoad: (e14) => this.visibleRangeNeedsSegmentLoad(e14), shouldShowChapterLoadError: (e14, t2) => this.shouldShowChapterLoadError(e14, t2), ensureChapterLoaded: (e14) => this.ensureChapterLoaded(e14), ensureSegmentLoaded: (e14, t2) => this.ensureSegmentLoaded(e14, t2), ensureAllSegmentsLoaded: (e14) => this.ensureAllSegmentsLoaded(e14), resolvePageDocument: (e14, t2) => {
1633
1618
  const s2 = this.findChapter(e14);
1634
- return s2 ? zt$1(s2, t2) : null;
1619
+ return s2 ? Gt$1(s2, t2) : null;
1635
1620
  }, getSegmentPlan: (e14) => {
1636
1621
  const t2 = this.findChapter(e14);
1637
- return t2 ? Rt$1(t2) : null;
1622
+ return t2 ? zt$1(t2) : null;
1638
1623
  }, isSegmentLoaded: (e14, t2) => this.isSegmentDocumentOpen(e14, t2), isSegmentLoadPending: (e14, t2) => this.isSegmentLoadPending(e14, t2), getChapterErrorMessage: (e14) => this.chapterErrorMessages.get(e14), getChapterStatus: (e14) => this.chapterStatus.get(e14) ?? "idle", getChapter: (e14) => this.findChapter(e14), onChapterStatusChange: this.statusChange$.on, onManifestChange: this.manifestChange$.on };
1639
1624
  }
1640
1625
  destroy() {
@@ -1681,10 +1666,9 @@ const Kt$1 = class e3 extends BasePlugin {
1681
1666
  if (e14 <= 0 || 0 === this.manifest.chapters.length) return;
1682
1667
  const t2 = Date.now(), s2 = Math.min(e14, this.manifest.chapters.length);
1683
1668
  for (let n9 = 0; n9 < s2; n9++) {
1684
- const e15 = this.manifest.chapters[n9].chapterId;
1685
- this.chapterLastUsed.set(e15, t2);
1686
- const s3 = this.chapterStatus.get(e15) ?? "idle";
1687
- "idle" !== s3 && "closed" !== s3 && "error" !== s3 || this.ensureChapterLoaded(e15);
1669
+ const e15 = this.manifest.chapters[n9];
1670
+ this.chapterLastUsed.set(e15.chapterId, t2), this.resolveChapterUrls(e15).catch(() => {
1671
+ });
1688
1672
  }
1689
1673
  }
1690
1674
  findChapter(e14) {
@@ -1692,7 +1676,7 @@ const Kt$1 = class e3 extends BasePlugin {
1692
1676
  }
1693
1677
  getSegmentPlanForChapter(e14) {
1694
1678
  const t2 = this.findChapter(e14);
1695
- return t2 ? Rt$1(t2) : null;
1679
+ return t2 ? zt$1(t2) : null;
1696
1680
  }
1697
1681
  anySegmentOpen(e14) {
1698
1682
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -1716,11 +1700,7 @@ const Kt$1 = class e3 extends BasePlugin {
1716
1700
  }
1717
1701
  isSegmentLoadPending(e14, t2) {
1718
1702
  const s2 = this.resolveSegmentDocumentId(e14, t2);
1719
- if (!s2) return false;
1720
- if (this.pendingSegmentLoadPromises.has(s2)) return true;
1721
- if (0 === t2 && this.pendingChapterLoadPromises.has(e14)) return true;
1722
- const n9 = this.coreState.core.documents[s2];
1723
- return "loading" === (null == n9 ? void 0 : n9.status);
1703
+ return !!s2 && (!!this.pendingSegmentLoadPromises.has(s2) || !(0 !== t2 || !this.pendingChapterLoadPromises.has(e14)));
1724
1704
  }
1725
1705
  syncChapterStatusFromDocuments(e14) {
1726
1706
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -1740,7 +1720,7 @@ const Kt$1 = class e3 extends BasePlugin {
1740
1720
  if (!e15) continue;
1741
1721
  const s2 = this.findChapter(e15.chapterId);
1742
1722
  if (!s2) continue;
1743
- const n9 = Gt$1(s2, e15.localPageIndex);
1723
+ const n9 = Rt$1(s2, e15.localPageIndex);
1744
1724
  if (!this.isSegmentDocumentOpen(e15.chapterId, n9)) return true;
1745
1725
  }
1746
1726
  return false;
@@ -1751,12 +1731,12 @@ const Kt$1 = class e3 extends BasePlugin {
1751
1731
  clearVisibleSegmentBacklog() {
1752
1732
  this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
1753
1733
  }
1754
- globalSegmentSortKey(e14, t2) {
1734
+ segmentStartGlobalIndex(e14, t2) {
1755
1735
  var s2;
1756
1736
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
1757
1737
  const n9 = this.findChapter(e14);
1758
1738
  if (!n9) return Number.MAX_SAFE_INTEGER;
1759
- const r2 = Rt$1(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, i2 = this.virtualPageMap.toGlobal(e14, a2);
1739
+ const r2 = zt$1(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, i2 = this.virtualPageMap.toGlobal(e14, a2);
1760
1740
  if (i2) return i2.globalPageIndex;
1761
1741
  return 1e4 * this.manifest.chapters.findIndex((t3) => t3.chapterId === e14) + t2;
1762
1742
  }
@@ -1764,12 +1744,12 @@ const Kt$1 = class e3 extends BasePlugin {
1764
1744
  const t2 = [...e14].sort((e15, t3) => e15.priority - t3.priority), s2 = /* @__PURE__ */ new Set(), n9 = [];
1765
1745
  for (const r2 of t2) {
1766
1746
  const e15 = `${r2.chapterId}:${r2.segmentIndex}`;
1767
- s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || this.isSegmentLoadPending(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
1747
+ s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
1768
1748
  }
1769
1749
  this.visibleSegmentBacklog = n9, this.pumpVisibleSegmentBacklog();
1770
1750
  }
1771
1751
  pumpVisibleSegmentBacklog() {
1772
- const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
1752
+ const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
1773
1753
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
1774
1754
  const e15 = this.visibleSegmentBacklog.shift();
1775
1755
  this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
@@ -1789,10 +1769,10 @@ const Kt$1 = class e3 extends BasePlugin {
1789
1769
  if (t3 < 0) return;
1790
1770
  const s3 = this.findChapter(e15);
1791
1771
  if (!s3) return;
1792
- const n10 = Rt$1(s3);
1772
+ const n10 = zt$1(s3);
1793
1773
  if (n10 && t3 >= n10.segments.length) return;
1794
- const r3 = `${e15}:${t3}`, a3 = this.globalSegmentSortKey(e15, t3), o3 = i2.get(r3);
1795
- (void 0 === o3 || a3 < o3) && i2.set(r3, a3);
1774
+ const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), l2 = Math.abs(o3 - r2), h3 = i2.get(a3);
1775
+ (void 0 === h3 || l2 < h3) && i2.set(a3, l2);
1796
1776
  };
1797
1777
  for (const d2 of e14) {
1798
1778
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -1800,7 +1780,7 @@ const Kt$1 = class e3 extends BasePlugin {
1800
1780
  a2.add(e15.chapterId);
1801
1781
  const t3 = this.findChapter(e15.chapterId);
1802
1782
  if (!t3) continue;
1803
- const s3 = Gt$1(t3, e15.localPageIndex);
1783
+ const s3 = Rt$1(t3, e15.localPageIndex);
1804
1784
  o2(e15.chapterId, s3), this.resolveChapterUrls(t3).catch(() => {
1805
1785
  });
1806
1786
  }
@@ -1809,7 +1789,7 @@ const Kt$1 = class e3 extends BasePlugin {
1809
1789
  if (l) {
1810
1790
  const e15 = this.findChapter(l.chapterId);
1811
1791
  if (e15) {
1812
- const t3 = Gt$1(e15, l.localPageIndex);
1792
+ const t3 = Rt$1(e15, l.localPageIndex);
1813
1793
  o2(l.chapterId, t3 + 1);
1814
1794
  }
1815
1795
  }
@@ -1821,7 +1801,7 @@ const Kt$1 = class e3 extends BasePlugin {
1821
1801
  if (this.scheduleVisibleSegmentLoads(h2), this.config.autoActivateOnLoad ?? 1) {
1822
1802
  const e15 = this.virtualPageMap.atIndex(r2);
1823
1803
  if (e15) {
1824
- const t3 = zt$1(this.findChapter(e15.chapterId), e15.localPageIndex);
1804
+ const t3 = Gt$1(this.findChapter(e15.chapterId), e15.localPageIndex);
1825
1805
  if (this.documentManager.provides().isDocumentOpen(t3.documentId)) {
1826
1806
  this.documentManager.provides().getActiveDocumentId() !== t3.documentId && this.documentManager.provides().setActiveDocument(t3.documentId);
1827
1807
  }
@@ -1857,7 +1837,7 @@ const Kt$1 = class e3 extends BasePlugin {
1857
1837
  this.prepareChapterForSegmentLoad(e14);
1858
1838
  const s2 = this.findChapter(e14);
1859
1839
  if (!s2) return Promise.resolve("error");
1860
- const n9 = Rt$1(s2);
1840
+ const n9 = zt$1(s2);
1861
1841
  if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
1862
1842
  if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
1863
1843
  const r2 = n9.segments[t2], a2 = r2.documentId;
@@ -1887,7 +1867,7 @@ const Kt$1 = class e3 extends BasePlugin {
1887
1867
  if (n9) return n9;
1888
1868
  const r2 = (async () => {
1889
1869
  const s3 = e14.source;
1890
- if (Ft$1(s3)) {
1870
+ if (Nt$1(s3)) {
1891
1871
  const e15 = [...s3.urls];
1892
1872
  return this.chapterUrlsCache.set(t2, e15), e15;
1893
1873
  }
@@ -1913,7 +1893,7 @@ const Kt$1 = class e3 extends BasePlugin {
1913
1893
  return (null == r2 ? void 0 : r2.openPdf) ? r2.openPdf(a2) : { url: n9 };
1914
1894
  }
1915
1895
  async resolvePdfPayload(t2, s2) {
1916
- const n9 = t2.source, r2 = this.config.chapterPdfLoader, a2 = Nt$1(t2);
1896
+ const n9 = t2.source, r2 = this.config.chapterPdfLoader, a2 = Ft$1(t2);
1917
1897
  if (a2) {
1918
1898
  const n10 = Ot$1(Tt$1(t2), a2);
1919
1899
  if (s2 < 0 || s2 >= n10) throw new Error(`Chapter "${t2.chapterId}" invalid segmentIndex ${s2} (expected 0..${n10 - 1})`);
@@ -1926,7 +1906,7 @@ const Kt$1 = class e3 extends BasePlugin {
1926
1906
  }
1927
1907
  if (0 !== s2) throw new Error(`Chapter "${t2.chapterId}" is not segmented`);
1928
1908
  if (n9) {
1929
- if (Ft$1(n9)) {
1909
+ if (Nt$1(n9)) {
1930
1910
  const e14 = await this.resolveChapterUrls(t2);
1931
1911
  return this.openPayloadFromUrl(t2, s2, e14);
1932
1912
  }
@@ -1995,7 +1975,7 @@ const Kt$1 = class e3 extends BasePlugin {
1995
1975
  });
1996
1976
  }
1997
1977
  closeChapter(e14) {
1998
- const t2 = this.findChapter(e14), s2 = this.documentManager.provides(), n9 = t2 ? Rt$1(t2) : null, r2 = (e15) => {
1978
+ const t2 = this.findChapter(e14), s2 = this.documentManager.provides(), n9 = t2 ? zt$1(t2) : null, r2 = (e15) => {
1999
1979
  if (s2.isDocumentOpen(e15)) {
2000
1980
  s2.closeDocument(e15).wait(() => {
2001
1981
  }, () => {
@@ -2262,10 +2242,9 @@ const gs$1 = class e4 extends BasePlugin {
2262
2242
  if (e14 <= 0 || 0 === this.manifest.chapters.length) return;
2263
2243
  const t2 = Date.now(), s2 = Math.min(e14, this.manifest.chapters.length);
2264
2244
  for (let n9 = 0; n9 < s2; n9++) {
2265
- const e15 = this.manifest.chapters[n9].chapterId;
2266
- this.chapterLastUsed.set(e15, t2);
2267
- const s3 = this.chapterStatus.get(e15) ?? "idle";
2268
- "idle" !== s3 && "closed" !== s3 && "error" !== s3 || this.ensureChapterLoaded(e15);
2245
+ const e15 = this.manifest.chapters[n9];
2246
+ this.chapterLastUsed.set(e15.chapterId, t2), this.resolveChapterUrls(e15).catch(() => {
2247
+ });
2269
2248
  }
2270
2249
  }
2271
2250
  findChapter(e14) {
@@ -2297,11 +2276,7 @@ const gs$1 = class e4 extends BasePlugin {
2297
2276
  }
2298
2277
  isSegmentLoadPending(e14, t2) {
2299
2278
  const s2 = this.resolveSegmentDocumentId(e14, t2);
2300
- if (!s2) return false;
2301
- if (this.pendingSegmentLoadPromises.has(s2)) return true;
2302
- if (0 === t2 && this.pendingChapterLoadPromises.has(e14)) return true;
2303
- const n9 = this.coreState.core.documents[s2];
2304
- return "loading" === (null == n9 ? void 0 : n9.status);
2279
+ return !!s2 && (!!this.pendingSegmentLoadPromises.has(s2) || !(0 !== t2 || !this.pendingChapterLoadPromises.has(e14)));
2305
2280
  }
2306
2281
  syncChapterStatusFromDocuments(e14) {
2307
2282
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -2332,7 +2307,7 @@ const gs$1 = class e4 extends BasePlugin {
2332
2307
  clearVisibleSegmentBacklog() {
2333
2308
  this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
2334
2309
  }
2335
- globalSegmentSortKey(e14, t2) {
2310
+ segmentStartGlobalIndex(e14, t2) {
2336
2311
  var s2;
2337
2312
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
2338
2313
  const n9 = this.findChapter(e14);
@@ -2345,12 +2320,12 @@ const gs$1 = class e4 extends BasePlugin {
2345
2320
  const t2 = [...e14].sort((e15, t3) => e15.priority - t3.priority), s2 = /* @__PURE__ */ new Set(), n9 = [];
2346
2321
  for (const r2 of t2) {
2347
2322
  const e15 = `${r2.chapterId}:${r2.segmentIndex}`;
2348
- s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || this.isSegmentLoadPending(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
2323
+ s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
2349
2324
  }
2350
2325
  this.visibleSegmentBacklog = n9, this.pumpVisibleSegmentBacklog();
2351
2326
  }
2352
2327
  pumpVisibleSegmentBacklog() {
2353
- const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
2328
+ const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
2354
2329
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
2355
2330
  const e15 = this.visibleSegmentBacklog.shift();
2356
2331
  this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
@@ -2372,8 +2347,8 @@ const gs$1 = class e4 extends BasePlugin {
2372
2347
  if (!s3) return;
2373
2348
  const n10 = as(s3);
2374
2349
  if (n10 && t3 >= n10.segments.length) return;
2375
- const r3 = `${e15}:${t3}`, a3 = this.globalSegmentSortKey(e15, t3), o3 = i2.get(r3);
2376
- (void 0 === o3 || a3 < o3) && i2.set(r3, a3);
2350
+ const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), l2 = Math.abs(o3 - r2), h3 = i2.get(a3);
2351
+ (void 0 === h3 || l2 < h3) && i2.set(a3, l2);
2377
2352
  };
2378
2353
  for (const d2 of e14) {
2379
2354
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -2605,13 +2580,13 @@ function ms$1(e14, t2) {
2605
2580
  }
2606
2581
  const fs$1 = class fs extends BasePlugin {
2607
2582
  constructor(e14, t2) {
2608
- super(e14, t2), this.layoutChange$ = createBehaviorEmitter(void 0, ms$1), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.loadPushRaf = null, this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(ps$1.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
2583
+ super(e14, t2), this.layoutChange$ = createBehaviorEmitter(void 0, ms$1), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(ps$1.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
2609
2584
  }
2610
2585
  async initialize(e14) {
2611
2586
  this.config = { placeholderPageHeight: e14.placeholderPageHeight ?? 1200, placeholderPageWidth: e14.placeholderPageWidth ?? 900, bufferSize: e14.bufferSize ?? 2, pageGap: e14.pageGap ?? 20 }, this.virtualPageMap = this.chapterManager.provides().getVirtualPageMap(), this.rebuildOffsets(), this.chapterManagerUnsubs.push(this.chapterManager.provides().onManifestChange(({ map: e15 }) => {
2612
2587
  this.virtualPageMap = e15, this.pageSizes.clear(), this.lastLoadIndicesKey = "", this.rebuildOffsets(), this.syncReadyChapterSizes(), this.recomputeVisible();
2613
2588
  }), this.chapterManager.provides().onChapterStatusChange((e15) => {
2614
- "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads()));
2589
+ "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.pushVisibleLoads()));
2615
2590
  })), this.syncReadyChapterSizes();
2616
2591
  }
2617
2592
  isChapterDocumentReady(e14) {
@@ -2631,11 +2606,6 @@ const fs$1 = class fs extends BasePlugin {
2631
2606
  refreshLayout() {
2632
2607
  this.layoutChange$.emit(this.computeLayout());
2633
2608
  }
2634
- schedulePushVisibleLoads() {
2635
- null == this.loadPushRaf && (this.loadPushRaf = requestAnimationFrame(() => {
2636
- this.loadPushRaf = null, this.pushVisibleLoads();
2637
- }));
2638
- }
2639
2609
  pushVisibleLoads() {
2640
2610
  if (!this.virtualPageMap || 0 === this.currentVisible.length) return;
2641
2611
  const e14 = this.expandIndicesWithBuffer(this.currentVisible, this.virtualPageMap.totalPages);
@@ -2654,7 +2624,7 @@ const fs$1 = class fs extends BasePlugin {
2654
2624
  const { chapterId: s2 } = Yt$1(e14);
2655
2625
  if (0 === ((null == (t2 = this.virtualPageMap) ? void 0 : t2.pagesOfChapter(s2).length) ?? 0)) return;
2656
2626
  const n9 = this.documentManager.provides();
2657
- n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads();
2627
+ n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.pushVisibleLoads();
2658
2628
  }
2659
2629
  onDocumentClosed(e14) {
2660
2630
  var t2;
@@ -2668,7 +2638,7 @@ const fs$1 = class fs extends BasePlugin {
2668
2638
  var e14;
2669
2639
  super.destroy();
2670
2640
  for (const t2 of this.chapterManagerUnsubs) t2();
2671
- this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, null != this.loadPushRaf && (cancelAnimationFrame(this.loadPushRaf), this.loadPushRaf = null), this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
2641
+ this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
2672
2642
  }
2673
2643
  getPageSize(e14) {
2674
2644
  return this.pageSizes.get(e14) ?? { width: this.config.placeholderPageWidth, height: this.config.placeholderPageHeight };
@@ -2777,7 +2747,7 @@ const fs$1 = class fs extends BasePlugin {
2777
2747
  const e15 = this.offsets[l], n10 = e15 + this.getPageSize(l).height, r3 = Math.max(0, Math.min(n10, s2) - Math.max(e15, t2));
2778
2748
  r3 > o2 && (o2 = r3, i2 = l);
2779
2749
  }
2780
- this.currentVisible = a2, this.currentGlobalPageIndex = i2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: i2 }), this.schedulePushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
2750
+ this.currentVisible = a2, this.currentGlobalPageIndex = i2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: i2 }), this.pushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
2781
2751
  }
2782
2752
  findFirstVisibleIndex(e14) {
2783
2753
  let t2 = 0, s2 = this.offsets.length - 1, n9 = 0;
@@ -3005,8 +2975,8 @@ function Ts$1(e14) {
3005
2975
  for (let l = 0; l < i2.urls.length; l++) (null == (t2 = i2.urls[l]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${l}] is empty`);
3006
2976
  return n9;
3007
2977
  }
3008
- const Fs$1 = { kind: "first-wins" };
3009
- function Ns$1(e14, t2, s2) {
2978
+ const Ns$1 = { kind: "first-wins" };
2979
+ function Fs$1(e14, t2, s2) {
3010
2980
  if (1 === t2.length) return t2[0].chapterId;
3011
2981
  switch (s2.kind) {
3012
2982
  case "first-wins":
@@ -3058,8 +3028,8 @@ let As$1 = class As {
3058
3028
  return s2 ? this._pages.indexOf(s2) : -1;
3059
3029
  }
3060
3030
  };
3061
- function Vs$1(e14, t2 = Fs$1) {
3062
- const s2 = (function(e15, t3 = Fs$1) {
3031
+ function Vs$1(e14, t2 = Ns$1) {
3032
+ const s2 = (function(e15, t3 = Ns$1) {
3063
3033
  const s3 = /* @__PURE__ */ new Map();
3064
3034
  for (const r3 of e15.chapters) {
3065
3035
  const [e16, t4] = r3.globalPageRange;
@@ -3069,7 +3039,7 @@ function Vs$1(e14, t2 = Fs$1) {
3069
3039
  }
3070
3040
  }
3071
3041
  const n10 = /* @__PURE__ */ new Map();
3072
- for (const [r3, a3] of s3) n10.set(r3, Ns$1(r3, a3, t3));
3042
+ for (const [r3, a3] of s3) n10.set(r3, Fs$1(r3, a3, t3));
3073
3043
  return n10;
3074
3044
  })(e14, t2), n9 = /* @__PURE__ */ new Map();
3075
3045
  for (const o2 of e14.chapters) n9.set(o2.chapterId, o2);
@@ -3085,10 +3055,10 @@ function Vs$1(e14, t2 = Fs$1) {
3085
3055
  }
3086
3056
  const Os$1 = class e6 extends BasePlugin {
3087
3057
  constructor(e14, t2) {
3088
- super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Fs$1, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
3058
+ super(e14, t2), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Ns$1, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
3089
3059
  }
3090
3060
  async initialize(e14) {
3091
- if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Fs$1, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
3061
+ if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Ns$1, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
3092
3062
  const { chapterId: t2 } = $s$1(e15.id);
3093
3063
  this.findChapter(t2) && (this.passwordAttempts.delete(t2), this.syncChapterStatusFromDocuments(t2));
3094
3064
  }), this.documentManager.provides().onDocumentClosed((e15) => {
@@ -3157,10 +3127,9 @@ const Os$1 = class e6 extends BasePlugin {
3157
3127
  if (e14 <= 0 || 0 === this.manifest.chapters.length) return;
3158
3128
  const t2 = Date.now(), s2 = Math.min(e14, this.manifest.chapters.length);
3159
3129
  for (let n9 = 0; n9 < s2; n9++) {
3160
- const e15 = this.manifest.chapters[n9].chapterId;
3161
- this.chapterLastUsed.set(e15, t2);
3162
- const s3 = this.chapterStatus.get(e15) ?? "idle";
3163
- "idle" !== s3 && "closed" !== s3 && "error" !== s3 || this.ensureChapterLoaded(e15);
3130
+ const e15 = this.manifest.chapters[n9];
3131
+ this.chapterLastUsed.set(e15.chapterId, t2), this.resolveChapterUrls(e15).catch(() => {
3132
+ });
3164
3133
  }
3165
3134
  }
3166
3135
  findChapter(e14) {
@@ -3192,11 +3161,7 @@ const Os$1 = class e6 extends BasePlugin {
3192
3161
  }
3193
3162
  isSegmentLoadPending(e14, t2) {
3194
3163
  const s2 = this.resolveSegmentDocumentId(e14, t2);
3195
- if (!s2) return false;
3196
- if (this.pendingSegmentLoadPromises.has(s2)) return true;
3197
- if (0 === t2 && this.pendingChapterLoadPromises.has(e14)) return true;
3198
- const n9 = this.coreState.core.documents[s2];
3199
- return "loading" === (null == n9 ? void 0 : n9.status);
3164
+ return !!s2 && (!!this.pendingSegmentLoadPromises.has(s2) || !(0 !== t2 || !this.pendingChapterLoadPromises.has(e14)));
3200
3165
  }
3201
3166
  syncChapterStatusFromDocuments(e14) {
3202
3167
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -3227,7 +3192,7 @@ const Os$1 = class e6 extends BasePlugin {
3227
3192
  clearVisibleSegmentBacklog() {
3228
3193
  this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
3229
3194
  }
3230
- globalSegmentSortKey(e14, t2) {
3195
+ segmentStartGlobalIndex(e14, t2) {
3231
3196
  var s2;
3232
3197
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
3233
3198
  const n9 = this.findChapter(e14);
@@ -3240,12 +3205,12 @@ const Os$1 = class e6 extends BasePlugin {
3240
3205
  const t2 = [...e14].sort((e15, t3) => e15.priority - t3.priority), s2 = /* @__PURE__ */ new Set(), n9 = [];
3241
3206
  for (const r2 of t2) {
3242
3207
  const e15 = `${r2.chapterId}:${r2.segmentIndex}`;
3243
- s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || this.isSegmentLoadPending(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
3208
+ s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
3244
3209
  }
3245
3210
  this.visibleSegmentBacklog = n9, this.pumpVisibleSegmentBacklog();
3246
3211
  }
3247
3212
  pumpVisibleSegmentBacklog() {
3248
- const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
3213
+ const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
3249
3214
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
3250
3215
  const e15 = this.visibleSegmentBacklog.shift();
3251
3216
  this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
@@ -3267,8 +3232,8 @@ const Os$1 = class e6 extends BasePlugin {
3267
3232
  if (!s3) return;
3268
3233
  const n10 = Ds$1(s3);
3269
3234
  if (n10 && t3 >= n10.segments.length) return;
3270
- const r3 = `${e15}:${t3}`, a3 = this.globalSegmentSortKey(e15, t3), o3 = i2.get(r3);
3271
- (void 0 === o3 || a3 < o3) && i2.set(r3, a3);
3235
+ const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), l2 = Math.abs(o3 - r2), h3 = i2.get(a3);
3236
+ (void 0 === h3 || l2 < h3) && i2.set(a3, l2);
3272
3237
  };
3273
3238
  for (const d2 of e14) {
3274
3239
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -3488,11 +3453,11 @@ const Os$1 = class e6 extends BasePlugin {
3488
3453
  }
3489
3454
  };
3490
3455
  Os$1.id = "chapter-manager";
3491
- let Rs$1 = Os$1;
3492
- function zs$1(e14) {
3456
+ let zs$1 = Os$1;
3457
+ function Gs$1(e14) {
3493
3458
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
3494
3459
  }
3495
- function Gs$1(e14, t2) {
3460
+ function Rs$1(e14, t2) {
3496
3461
  return `${e14}#s${t2}`;
3497
3462
  }
3498
3463
  function _s$1(e14) {
@@ -3505,7 +3470,7 @@ function Bs$1(e14) {
3505
3470
  const t2 = (function(e15) {
3506
3471
  if (null != e15.segmentPageThreshold && e15.segmentPageThreshold > 0) return e15.segmentPageThreshold;
3507
3472
  const t3 = e15.source;
3508
- return zs$1(t3) ? t3.segmentPageThreshold : null;
3473
+ return Gs$1(t3) ? t3.segmentPageThreshold : null;
3509
3474
  })(e14);
3510
3475
  if (!t2) return null;
3511
3476
  const s2 = (function(e15) {
@@ -3513,11 +3478,11 @@ function Bs$1(e14) {
3513
3478
  return s3 - t3 + 1;
3514
3479
  })(e14), n9 = (function(e15, t3) {
3515
3480
  return e15 <= 0 || t3 <= 0 ? 0 : Math.ceil(e15 / t3);
3516
- })(s2, t2), r2 = zs$1(e14.source) ? e14.source.urls : void 0, a2 = [];
3481
+ })(s2, t2), r2 = Gs$1(e14.source) ? e14.source.urls : void 0, a2 = [];
3517
3482
  for (let i2 = 0; i2 < n9; i2++) {
3518
3483
  const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
3519
3484
  if (n10 >= s2) break;
3520
- a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: Gs$1(e14.chapterId, i2) });
3485
+ a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: Rs$1(e14.chapterId, i2) });
3521
3486
  }
3522
3487
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
3523
3488
  }
@@ -3738,10 +3703,9 @@ const rn = class e7 extends BasePlugin {
3738
3703
  if (e14 <= 0 || 0 === this.manifest.chapters.length) return;
3739
3704
  const t2 = Date.now(), s2 = Math.min(e14, this.manifest.chapters.length);
3740
3705
  for (let n9 = 0; n9 < s2; n9++) {
3741
- const e15 = this.manifest.chapters[n9].chapterId;
3742
- this.chapterLastUsed.set(e15, t2);
3743
- const s3 = this.chapterStatus.get(e15) ?? "idle";
3744
- "idle" !== s3 && "closed" !== s3 && "error" !== s3 || this.ensureChapterLoaded(e15);
3706
+ const e15 = this.manifest.chapters[n9];
3707
+ this.chapterLastUsed.set(e15.chapterId, t2), this.resolveChapterUrls(e15).catch(() => {
3708
+ });
3745
3709
  }
3746
3710
  }
3747
3711
  findChapter(e14) {
@@ -3773,11 +3737,7 @@ const rn = class e7 extends BasePlugin {
3773
3737
  }
3774
3738
  isSegmentLoadPending(e14, t2) {
3775
3739
  const s2 = this.resolveSegmentDocumentId(e14, t2);
3776
- if (!s2) return false;
3777
- if (this.pendingSegmentLoadPromises.has(s2)) return true;
3778
- if (0 === t2 && this.pendingChapterLoadPromises.has(e14)) return true;
3779
- const n9 = this.coreState.core.documents[s2];
3780
- return "loading" === (null == n9 ? void 0 : n9.status);
3740
+ return !!s2 && (!!this.pendingSegmentLoadPromises.has(s2) || !(0 !== t2 || !this.pendingChapterLoadPromises.has(e14)));
3781
3741
  }
3782
3742
  syncChapterStatusFromDocuments(e14) {
3783
3743
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -3808,7 +3768,7 @@ const rn = class e7 extends BasePlugin {
3808
3768
  clearVisibleSegmentBacklog() {
3809
3769
  this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
3810
3770
  }
3811
- globalSegmentSortKey(e14, t2) {
3771
+ segmentStartGlobalIndex(e14, t2) {
3812
3772
  var s2;
3813
3773
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
3814
3774
  const n9 = this.findChapter(e14);
@@ -3821,12 +3781,12 @@ const rn = class e7 extends BasePlugin {
3821
3781
  const t2 = [...e14].sort((e15, t3) => e15.priority - t3.priority), s2 = /* @__PURE__ */ new Set(), n9 = [];
3822
3782
  for (const r2 of t2) {
3823
3783
  const e15 = `${r2.chapterId}:${r2.segmentIndex}`;
3824
- s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || this.isSegmentLoadPending(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
3784
+ s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
3825
3785
  }
3826
3786
  this.visibleSegmentBacklog = n9, this.pumpVisibleSegmentBacklog();
3827
3787
  }
3828
3788
  pumpVisibleSegmentBacklog() {
3829
- const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
3789
+ const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
3830
3790
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
3831
3791
  const e15 = this.visibleSegmentBacklog.shift();
3832
3792
  this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
@@ -3848,8 +3808,8 @@ const rn = class e7 extends BasePlugin {
3848
3808
  if (!s3) return;
3849
3809
  const n10 = Zs(s3);
3850
3810
  if (n10 && t3 >= n10.segments.length) return;
3851
- const r3 = `${e15}:${t3}`, a3 = this.globalSegmentSortKey(e15, t3), o3 = i2.get(r3);
3852
- (void 0 === o3 || a3 < o3) && i2.set(r3, a3);
3811
+ const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), l2 = Math.abs(o3 - r2), h3 = i2.get(a3);
3812
+ (void 0 === h3 || l2 < h3) && i2.set(a3, l2);
3853
3813
  };
3854
3814
  for (const d2 of e14) {
3855
3815
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -4081,13 +4041,13 @@ function on(e14, t2) {
4081
4041
  }
4082
4042
  const ln = class extends BasePlugin {
4083
4043
  constructor(e14, t2) {
4084
- super(e14, t2), this.layoutChange$ = createBehaviorEmitter(void 0, on), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.loadPushRaf = null, this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(an.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
4044
+ super(e14, t2), this.layoutChange$ = createBehaviorEmitter(void 0, on), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(an.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
4085
4045
  }
4086
4046
  async initialize(e14) {
4087
4047
  this.config = { placeholderPageHeight: e14.placeholderPageHeight ?? 1200, placeholderPageWidth: e14.placeholderPageWidth ?? 900, bufferSize: e14.bufferSize ?? 2, pageGap: e14.pageGap ?? 20 }, this.virtualPageMap = this.chapterManager.provides().getVirtualPageMap(), this.rebuildOffsets(), this.chapterManagerUnsubs.push(this.chapterManager.provides().onManifestChange(({ map: e15 }) => {
4088
4048
  this.virtualPageMap = e15, this.pageSizes.clear(), this.lastLoadIndicesKey = "", this.rebuildOffsets(), this.syncReadyChapterSizes(), this.recomputeVisible();
4089
4049
  }), this.chapterManager.provides().onChapterStatusChange((e15) => {
4090
- "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads()));
4050
+ "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.pushVisibleLoads()));
4091
4051
  })), this.syncReadyChapterSizes();
4092
4052
  }
4093
4053
  isChapterDocumentReady(e14) {
@@ -4107,11 +4067,6 @@ const ln = class extends BasePlugin {
4107
4067
  refreshLayout() {
4108
4068
  this.layoutChange$.emit(this.computeLayout());
4109
4069
  }
4110
- schedulePushVisibleLoads() {
4111
- null == this.loadPushRaf && (this.loadPushRaf = requestAnimationFrame(() => {
4112
- this.loadPushRaf = null, this.pushVisibleLoads();
4113
- }));
4114
- }
4115
4070
  pushVisibleLoads() {
4116
4071
  if (!this.virtualPageMap || 0 === this.currentVisible.length) return;
4117
4072
  const e14 = this.expandIndicesWithBuffer(this.currentVisible, this.virtualPageMap.totalPages);
@@ -4130,7 +4085,7 @@ const ln = class extends BasePlugin {
4130
4085
  const { chapterId: s2 } = _s$1(e14);
4131
4086
  if (0 === ((null == (t2 = this.virtualPageMap) ? void 0 : t2.pagesOfChapter(s2).length) ?? 0)) return;
4132
4087
  const n9 = this.documentManager.provides();
4133
- n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads();
4088
+ n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.pushVisibleLoads();
4134
4089
  }
4135
4090
  onDocumentClosed(e14) {
4136
4091
  var t2;
@@ -4144,7 +4099,7 @@ const ln = class extends BasePlugin {
4144
4099
  var e14;
4145
4100
  super.destroy();
4146
4101
  for (const t2 of this.chapterManagerUnsubs) t2();
4147
- this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, null != this.loadPushRaf && (cancelAnimationFrame(this.loadPushRaf), this.loadPushRaf = null), this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
4102
+ this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
4148
4103
  }
4149
4104
  getPageSize(e14) {
4150
4105
  return this.pageSizes.get(e14) ?? { width: this.config.placeholderPageWidth, height: this.config.placeholderPageHeight };
@@ -4253,7 +4208,7 @@ const ln = class extends BasePlugin {
4253
4208
  const e15 = this.offsets[l], n10 = e15 + this.getPageSize(l).height, r3 = Math.max(0, Math.min(n10, s2) - Math.max(e15, t2));
4254
4209
  r3 > o2 && (o2 = r3, i2 = l);
4255
4210
  }
4256
- this.currentVisible = a2, this.currentGlobalPageIndex = i2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: i2 }), this.schedulePushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
4211
+ this.currentVisible = a2, this.currentGlobalPageIndex = i2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: i2 }), this.pushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
4257
4212
  }
4258
4213
  findFirstVisibleIndex(e14) {
4259
4214
  let t2 = 0, s2 = this.offsets.length - 1, n9 = 0;
@@ -4348,7 +4303,7 @@ async function mn(e14, t2, s2) {
4348
4303
  }
4349
4304
  const fn = class e8 extends BasePlugin {
4350
4305
  constructor(e14, t2) {
4351
- super(e14, t2), this.change$ = createBehaviorEmitter([]), this.entries = /* @__PURE__ */ new Map(), this.chapterScroll = this.registry.getPlugin(hn.id), this.chapterManager = this.registry.getPlugin(Rs$1.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
4306
+ super(e14, t2), this.change$ = createBehaviorEmitter([]), this.entries = /* @__PURE__ */ new Map(), this.chapterScroll = this.registry.getPlugin(hn.id), this.chapterManager = this.registry.getPlugin(zs$1.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
4352
4307
  }
4353
4308
  async initialize(t2) {
4354
4309
  var s2;
@@ -4524,7 +4479,7 @@ const xn = ke$1(), Ln = {};
4524
4479
  function $n(e14) {
4525
4480
  return wn({ ...e14, toolbar: e14.toolbar ?? { enabledModes: [] }, features: ke$1(e14.features) });
4526
4481
  }
4527
- const kn = { overlapStrategy: { kind: "first-wins" }, passwordProvider: new Fe$1(async () => null), prefetchChapters: 1, maxConcurrentSegmentLoads: 1, loadDefaultStampLibrary: false, unloadTimeoutMs: 12e4, toolbar: { enabledModes: [] } };
4482
+ const kn = { overlapStrategy: { kind: "first-wins" }, passwordProvider: new Ne$1(async () => null), prefetchChapters: 1, maxConcurrentSegmentLoads: 1, loadDefaultStampLibrary: false, unloadTimeoutMs: 12e4, toolbar: { enabledModes: [] } };
4528
4483
  function Dn(e14) {
4529
4484
  return (function(e15) {
4530
4485
  return "callbacks" in e15.notes;
@@ -4541,10 +4496,10 @@ function Tn(e14) {
4541
4496
  const { features: t2, ...s2 } = En(Dn(e14));
4542
4497
  return s2;
4543
4498
  }
4544
- function Fn(e14, t2 = 0.5, s2 = 3) {
4499
+ function Nn(e14, t2 = 0.5, s2 = 3) {
4545
4500
  return Math.min(s2, Math.max(t2, e14));
4546
4501
  }
4547
- function Nn(e14, t2) {
4502
+ function Fn(e14, t2) {
4548
4503
  var _a2;
4549
4504
  if (t2 <= 0) return 1;
4550
4505
  let s2 = 0;
@@ -4560,7 +4515,7 @@ function An(e14, t2) {
4560
4515
  function Vn(e14, t2, s2, n9) {
4561
4516
  const r2 = An(t2, s2);
4562
4517
  if (r2 <= 0) return n9;
4563
- const a2 = Nn(e14, r2);
4518
+ const a2 = Fn(e14, r2);
4564
4519
  return Math.min(n9, a2);
4565
4520
  }
4566
4521
  function On(e14, t2, s2) {
@@ -4575,10 +4530,10 @@ function On(e14, t2, s2) {
4575
4530
  const r2 = e14.getDocument(t2.chapterId);
4576
4531
  r2 && s2(r2, t2.chapterId);
4577
4532
  }
4578
- function Rn(e14) {
4533
+ function zn(e14) {
4579
4534
  return e14 instanceof HTMLElement ? e14.clientWidth : 0;
4580
4535
  }
4581
- function zn(a2, i2, o2) {
4536
+ function Gn(a2, i2, o2) {
4582
4537
  const l = isRef(a2) ? a2 : computed(() => a2), { registry: h2 } = useRegistry(), { provides: d2 } = useCapability(DocumentManagerPlugin.id), { provides: c2 } = useCapability(nt$1.id), u2 = ref(P2()), g2 = ref(u2.value), p2 = ref(0), m2 = ref(false), f2 = () => {
4583
4538
  var _a2;
4584
4539
  return (_a2 = i2.value) == null ? void 0 : _a2.pageWidth;
@@ -4603,11 +4558,11 @@ function zn(a2, i2, o2) {
4603
4558
  });
4604
4559
  let S;
4605
4560
  function C2(e14) {
4606
- const t2 = I2.value, s2 = b2.value, n9 = Fn(e14, s2, t2);
4561
+ const t2 = I2.value, s2 = b2.value, n9 = Nn(e14, s2, t2);
4607
4562
  g2.value = n9, u2.value = n9, (function(e15, t3, s3, n10 = 0.5, r2 = 3) {
4608
4563
  const a3 = e15 == null ? void 0 : e15.getStore();
4609
4564
  if (!a3) return;
4610
- const i3 = Fn(s3, n10, r2);
4565
+ const i3 = Nn(s3, n10, r2);
4611
4566
  a3.dispatchToCore(setDefaultScale(i3));
4612
4567
  for (const o3 of t3.chapters) {
4613
4568
  const e16 = Ue$1(o3);
@@ -4621,7 +4576,7 @@ function zn(a2, i2, o2) {
4621
4576
  const t2 = c2.value.resolvePageDocument(e14, 0), s2 = d2.value.getDocument(t2.documentId);
4622
4577
  if (!s2) return;
4623
4578
  const n9 = An(p2.value, f2());
4624
- n9 <= 0 || C2(Nn(s2, n9));
4579
+ n9 <= 0 || C2(Fn(s2, n9));
4625
4580
  }
4626
4581
  function x(e14) {
4627
4582
  if (!m2.value) return v() ? y(e14) : C2(P2()), void (m2.value = true);
@@ -4633,10 +4588,10 @@ function zn(a2, i2, o2) {
4633
4588
  };
4634
4589
  let s2 = null;
4635
4590
  const n9 = new ResizeObserver(() => {
4636
- t2(Rn(s2));
4591
+ t2(zn(s2));
4637
4592
  }), r2 = () => {
4638
4593
  const r3 = e15.querySelector("[data-chapter-scroll-viewport]");
4639
- r3 !== s2 ? (s2 && n9.unobserve(s2), s2 = r3, s2 ? (n9.observe(s2), t2(Rn(s2))) : t2(0)) : t2(Rn(s2));
4594
+ r3 !== s2 ? (s2 && n9.unobserve(s2), s2 = r3, s2 ? (n9.observe(s2), t2(zn(s2))) : t2(0)) : t2(zn(s2));
4640
4595
  };
4641
4596
  r2();
4642
4597
  const a3 = new MutationObserver(r2);
@@ -4677,7 +4632,7 @@ function zn(a2, i2, o2) {
4677
4632
  return false !== ((_a2 = i2.value) == null ? void 0 : _a2.enabled);
4678
4633
  }, minZoom: b2, maxZoom: I2 };
4679
4634
  }
4680
- function Gn(e14, t2, s2) {
4635
+ function Rn(e14, t2, s2) {
4681
4636
  return Math.min(s2, Math.max(t2, e14));
4682
4637
  }
4683
4638
  function _n(e14, t2) {
@@ -4689,7 +4644,7 @@ function _n(e14, t2) {
4689
4644
  return !!n10 && (s2 == null ? void 0 : s2(), s2 = (function({ container: e15, getScale: t3, setScale: s3, minZoom: n11 = 0.5, maxZoom: r3 = 3 }) {
4690
4645
  let a3 = 0, i2 = 1, o2 = null, l = 1, h2 = 1;
4691
4646
  const d2 = (e16) => {
4692
- (e16.ctrlKey || e16.metaKey) && (e16.preventDefault(), null === o2 ? (h2 = t3(), l = 1) : clearTimeout(o2), l *= 1 - 0.01 * e16.deltaY, l = Gn(l, 0.1, 10), s3(Gn(h2 * l, n11, r3)), o2 = setTimeout(() => {
4647
+ (e16.ctrlKey || e16.metaKey) && (e16.preventDefault(), null === o2 ? (h2 = t3(), l = 1) : clearTimeout(o2), l *= 1 - 0.01 * e16.deltaY, l = Rn(l, 0.1, 10), s3(Rn(h2 * l, n11, r3)), o2 = setTimeout(() => {
4693
4648
  o2 = null;
4694
4649
  }, 120));
4695
4650
  }, c2 = /* @__PURE__ */ new Map(), u2 = (e16) => {
@@ -4701,7 +4656,7 @@ function _n(e14, t2) {
4701
4656
  if (c2.has(e16.pointerId) && (c2.set(e16.pointerId, { x: e16.clientX, y: e16.clientY }), 2 === c2.size && a3 > 0)) {
4702
4657
  e16.preventDefault();
4703
4658
  const [t4, o3] = [...c2.values()], l2 = Math.hypot(o3.x - t4.x, o3.y - t4.y);
4704
- s3(Gn(i2 * (l2 / a3), n11, r3));
4659
+ s3(Rn(i2 * (l2 / a3), n11, r3));
4705
4660
  }
4706
4661
  }, p2 = (e16) => {
4707
4662
  c2.delete(e16.pointerId), c2.size < 2 && (a3 = 0);
@@ -4725,7 +4680,7 @@ function Wn(e14) {
4725
4680
  }), l = computed(() => {
4726
4681
  var _a2;
4727
4682
  return (_a2 = e14.value) == null ? void 0 : _a2.zoom;
4728
- }), { scale: h2, setScale: d2, zoomEnabled: c2, minZoom: u2, maxZoom: g2 } = zn(o2, l, a2);
4683
+ }), { scale: h2, setScale: d2, zoomEnabled: c2, minZoom: u2, maxZoom: g2 } = Gn(o2, l, a2);
4729
4684
  return (function(e15, t2) {
4730
4685
  let s2 = null;
4731
4686
  watch([e15, t2], () => {
@@ -4984,10 +4939,9 @@ const hr = class e9 extends BasePlugin {
4984
4939
  if (e14 <= 0 || 0 === this.manifest.chapters.length) return;
4985
4940
  const t2 = Date.now(), s2 = Math.min(e14, this.manifest.chapters.length);
4986
4941
  for (let n9 = 0; n9 < s2; n9++) {
4987
- const e15 = this.manifest.chapters[n9].chapterId;
4988
- this.chapterLastUsed.set(e15, t2);
4989
- const s3 = this.chapterStatus.get(e15) ?? "idle";
4990
- "idle" !== s3 && "closed" !== s3 && "error" !== s3 || this.ensureChapterLoaded(e15);
4942
+ const e15 = this.manifest.chapters[n9];
4943
+ this.chapterLastUsed.set(e15.chapterId, t2), this.resolveChapterUrls(e15).catch(() => {
4944
+ });
4991
4945
  }
4992
4946
  }
4993
4947
  findChapter(e14) {
@@ -5019,11 +4973,7 @@ const hr = class e9 extends BasePlugin {
5019
4973
  }
5020
4974
  isSegmentLoadPending(e14, t2) {
5021
4975
  const s2 = this.resolveSegmentDocumentId(e14, t2);
5022
- if (!s2) return false;
5023
- if (this.pendingSegmentLoadPromises.has(s2)) return true;
5024
- if (0 === t2 && this.pendingChapterLoadPromises.has(e14)) return true;
5025
- const n9 = this.coreState.core.documents[s2];
5026
- return "loading" === (null == n9 ? void 0 : n9.status);
4976
+ return !!s2 && (!!this.pendingSegmentLoadPromises.has(s2) || !(0 !== t2 || !this.pendingChapterLoadPromises.has(e14)));
5027
4977
  }
5028
4978
  syncChapterStatusFromDocuments(e14) {
5029
4979
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -5054,7 +5004,7 @@ const hr = class e9 extends BasePlugin {
5054
5004
  clearVisibleSegmentBacklog() {
5055
5005
  this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
5056
5006
  }
5057
- globalSegmentSortKey(e14, t2) {
5007
+ segmentStartGlobalIndex(e14, t2) {
5058
5008
  var s2;
5059
5009
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
5060
5010
  const n9 = this.findChapter(e14);
@@ -5067,12 +5017,12 @@ const hr = class e9 extends BasePlugin {
5067
5017
  const t2 = [...e14].sort((e15, t3) => e15.priority - t3.priority), s2 = /* @__PURE__ */ new Set(), n9 = [];
5068
5018
  for (const r2 of t2) {
5069
5019
  const e15 = `${r2.chapterId}:${r2.segmentIndex}`;
5070
- s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || this.isSegmentLoadPending(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
5020
+ s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
5071
5021
  }
5072
5022
  this.visibleSegmentBacklog = n9, this.pumpVisibleSegmentBacklog();
5073
5023
  }
5074
5024
  pumpVisibleSegmentBacklog() {
5075
- const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
5025
+ const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
5076
5026
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
5077
5027
  const e15 = this.visibleSegmentBacklog.shift();
5078
5028
  this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
@@ -5094,8 +5044,8 @@ const hr = class e9 extends BasePlugin {
5094
5044
  if (!s3) return;
5095
5045
  const n10 = tr(s3);
5096
5046
  if (n10 && t3 >= n10.segments.length) return;
5097
- const r3 = `${e15}:${t3}`, a3 = this.globalSegmentSortKey(e15, t3), o3 = i2.get(r3);
5098
- (void 0 === o3 || a3 < o3) && i2.set(r3, a3);
5047
+ const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), l2 = Math.abs(o3 - r2), h3 = i2.get(a3);
5048
+ (void 0 === h3 || l2 < h3) && i2.set(a3, l2);
5099
5049
  };
5100
5050
  for (const d2 of e14) {
5101
5051
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -5327,13 +5277,13 @@ function cr(e14, t2) {
5327
5277
  }
5328
5278
  const ur = class extends BasePlugin {
5329
5279
  constructor(e14, t2) {
5330
- super(e14, t2), this.layoutChange$ = createBehaviorEmitter(void 0, cr), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.loadPushRaf = null, this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(dr.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
5280
+ super(e14, t2), this.layoutChange$ = createBehaviorEmitter(void 0, cr), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(dr.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
5331
5281
  }
5332
5282
  async initialize(e14) {
5333
5283
  this.config = { placeholderPageHeight: e14.placeholderPageHeight ?? 1200, placeholderPageWidth: e14.placeholderPageWidth ?? 900, bufferSize: e14.bufferSize ?? 2, pageGap: e14.pageGap ?? 20 }, this.virtualPageMap = this.chapterManager.provides().getVirtualPageMap(), this.rebuildOffsets(), this.chapterManagerUnsubs.push(this.chapterManager.provides().onManifestChange(({ map: e15 }) => {
5334
5284
  this.virtualPageMap = e15, this.pageSizes.clear(), this.lastLoadIndicesKey = "", this.rebuildOffsets(), this.syncReadyChapterSizes(), this.recomputeVisible();
5335
5285
  }), this.chapterManager.provides().onChapterStatusChange((e15) => {
5336
- "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads()));
5286
+ "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.pushVisibleLoads()));
5337
5287
  })), this.syncReadyChapterSizes();
5338
5288
  }
5339
5289
  isChapterDocumentReady(e14) {
@@ -5353,11 +5303,6 @@ const ur = class extends BasePlugin {
5353
5303
  refreshLayout() {
5354
5304
  this.layoutChange$.emit(this.computeLayout());
5355
5305
  }
5356
- schedulePushVisibleLoads() {
5357
- null == this.loadPushRaf && (this.loadPushRaf = requestAnimationFrame(() => {
5358
- this.loadPushRaf = null, this.pushVisibleLoads();
5359
- }));
5360
- }
5361
5306
  pushVisibleLoads() {
5362
5307
  if (!this.virtualPageMap || 0 === this.currentVisible.length) return;
5363
5308
  const e14 = this.expandIndicesWithBuffer(this.currentVisible, this.virtualPageMap.totalPages);
@@ -5376,7 +5321,7 @@ const ur = class extends BasePlugin {
5376
5321
  const { chapterId: s2 } = Kn(e14);
5377
5322
  if (0 === ((null == (t2 = this.virtualPageMap) ? void 0 : t2.pagesOfChapter(s2).length) ?? 0)) return;
5378
5323
  const n9 = this.documentManager.provides();
5379
- n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads();
5324
+ n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.pushVisibleLoads();
5380
5325
  }
5381
5326
  onDocumentClosed(e14) {
5382
5327
  var t2;
@@ -5390,7 +5335,7 @@ const ur = class extends BasePlugin {
5390
5335
  var e14;
5391
5336
  super.destroy();
5392
5337
  for (const t2 of this.chapterManagerUnsubs) t2();
5393
- this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, null != this.loadPushRaf && (cancelAnimationFrame(this.loadPushRaf), this.loadPushRaf = null), this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
5338
+ this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
5394
5339
  }
5395
5340
  getPageSize(e14) {
5396
5341
  return this.pageSizes.get(e14) ?? { width: this.config.placeholderPageWidth, height: this.config.placeholderPageHeight };
@@ -5499,7 +5444,7 @@ const ur = class extends BasePlugin {
5499
5444
  const e15 = this.offsets[l], n10 = e15 + this.getPageSize(l).height, r3 = Math.max(0, Math.min(n10, s2) - Math.max(e15, t2));
5500
5445
  r3 > o2 && (o2 = r3, i2 = l);
5501
5446
  }
5502
- this.currentVisible = a2, this.currentGlobalPageIndex = i2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: i2 }), this.schedulePushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
5447
+ this.currentVisible = a2, this.currentGlobalPageIndex = i2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: i2 }), this.pushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
5503
5448
  }
5504
5449
  findFirstVisibleIndex(e14) {
5505
5450
  let t2 = 0, s2 = this.offsets.length - 1, n9 = 0;
@@ -5787,10 +5732,9 @@ const Er = class e10 extends BasePlugin {
5787
5732
  if (e14 <= 0 || 0 === this.manifest.chapters.length) return;
5788
5733
  const t2 = Date.now(), s2 = Math.min(e14, this.manifest.chapters.length);
5789
5734
  for (let n9 = 0; n9 < s2; n9++) {
5790
- const e15 = this.manifest.chapters[n9].chapterId;
5791
- this.chapterLastUsed.set(e15, t2);
5792
- const s3 = this.chapterStatus.get(e15) ?? "idle";
5793
- "idle" !== s3 && "closed" !== s3 && "error" !== s3 || this.ensureChapterLoaded(e15);
5735
+ const e15 = this.manifest.chapters[n9];
5736
+ this.chapterLastUsed.set(e15.chapterId, t2), this.resolveChapterUrls(e15).catch(() => {
5737
+ });
5794
5738
  }
5795
5739
  }
5796
5740
  findChapter(e14) {
@@ -5822,11 +5766,7 @@ const Er = class e10 extends BasePlugin {
5822
5766
  }
5823
5767
  isSegmentLoadPending(e14, t2) {
5824
5768
  const s2 = this.resolveSegmentDocumentId(e14, t2);
5825
- if (!s2) return false;
5826
- if (this.pendingSegmentLoadPromises.has(s2)) return true;
5827
- if (0 === t2 && this.pendingChapterLoadPromises.has(e14)) return true;
5828
- const n9 = this.coreState.core.documents[s2];
5829
- return "loading" === (null == n9 ? void 0 : n9.status);
5769
+ return !!s2 && (!!this.pendingSegmentLoadPromises.has(s2) || !(0 !== t2 || !this.pendingChapterLoadPromises.has(e14)));
5830
5770
  }
5831
5771
  syncChapterStatusFromDocuments(e14) {
5832
5772
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -5857,7 +5797,7 @@ const Er = class e10 extends BasePlugin {
5857
5797
  clearVisibleSegmentBacklog() {
5858
5798
  this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
5859
5799
  }
5860
- globalSegmentSortKey(e14, t2) {
5800
+ segmentStartGlobalIndex(e14, t2) {
5861
5801
  var s2;
5862
5802
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
5863
5803
  const n9 = this.findChapter(e14);
@@ -5870,12 +5810,12 @@ const Er = class e10 extends BasePlugin {
5870
5810
  const t2 = [...e14].sort((e15, t3) => e15.priority - t3.priority), s2 = /* @__PURE__ */ new Set(), n9 = [];
5871
5811
  for (const r2 of t2) {
5872
5812
  const e15 = `${r2.chapterId}:${r2.segmentIndex}`;
5873
- s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || this.isSegmentLoadPending(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
5813
+ s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
5874
5814
  }
5875
5815
  this.visibleSegmentBacklog = n9, this.pumpVisibleSegmentBacklog();
5876
5816
  }
5877
5817
  pumpVisibleSegmentBacklog() {
5878
- const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
5818
+ const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
5879
5819
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
5880
5820
  const e15 = this.visibleSegmentBacklog.shift();
5881
5821
  this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
@@ -5897,8 +5837,8 @@ const Er = class e10 extends BasePlugin {
5897
5837
  if (!s3) return;
5898
5838
  const n10 = yr(s3);
5899
5839
  if (n10 && t3 >= n10.segments.length) return;
5900
- const r3 = `${e15}:${t3}`, a3 = this.globalSegmentSortKey(e15, t3), o3 = i2.get(r3);
5901
- (void 0 === o3 || a3 < o3) && i2.set(r3, a3);
5840
+ const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), l2 = Math.abs(o3 - r2), h3 = i2.get(a3);
5841
+ (void 0 === h3 || l2 < h3) && i2.set(a3, l2);
5902
5842
  };
5903
5843
  for (const d2 of e14) {
5904
5844
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -6122,10 +6062,10 @@ let Ur = Er;
6122
6062
  function Tr(e14) {
6123
6063
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
6124
6064
  }
6125
- function Fr(e14, t2) {
6065
+ function Nr(e14, t2) {
6126
6066
  return `${e14}#s${t2}`;
6127
6067
  }
6128
- function Nr(e14) {
6068
+ function Fr(e14) {
6129
6069
  const t2 = e14.lastIndexOf("#s");
6130
6070
  if (-1 === t2) return { chapterId: e14, segmentIndex: null };
6131
6071
  const s2 = e14.slice(0, t2), n9 = Number.parseInt(e14.slice(t2 + 2), 10);
@@ -6147,7 +6087,7 @@ function Ar(e14) {
6147
6087
  for (let i2 = 0; i2 < n9; i2++) {
6148
6088
  const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
6149
6089
  if (n10 >= s2) break;
6150
- a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: Fr(e14.chapterId, i2) });
6090
+ a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: Nr(e14.chapterId, i2) });
6151
6091
  }
6152
6092
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
6153
6093
  }
@@ -6158,15 +6098,15 @@ function Vr(e14) {
6158
6098
  function Or(e14) {
6159
6099
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
6160
6100
  }
6161
- function Rr(e14) {
6101
+ function zr(e14) {
6162
6102
  if (null != e14.segmentPageThreshold && e14.segmentPageThreshold > 0) return e14.segmentPageThreshold;
6163
6103
  const t2 = e14.source;
6164
6104
  return Or(t2) ? t2.segmentPageThreshold : null;
6165
6105
  }
6166
- function zr(e14, t2) {
6106
+ function Gr(e14, t2) {
6167
6107
  return `${e14}#s${t2}`;
6168
6108
  }
6169
- function Gr(e14) {
6109
+ function Rr(e14) {
6170
6110
  const t2 = e14.lastIndexOf("#s");
6171
6111
  if (-1 === t2) return { chapterId: e14, segmentIndex: null };
6172
6112
  const s2 = e14.slice(0, t2), n9 = Number.parseInt(e14.slice(t2 + 2), 10);
@@ -6176,13 +6116,13 @@ function _r(e14, t2) {
6176
6116
  return e14 <= 0 || t2 <= 0 ? 0 : Math.ceil(e14 / t2);
6177
6117
  }
6178
6118
  function Br(e14) {
6179
- const t2 = Rr(e14);
6119
+ const t2 = zr(e14);
6180
6120
  if (!t2) return null;
6181
6121
  const s2 = Vr(e14), n9 = _r(s2, t2), r2 = Or(e14.source) ? e14.source.urls : void 0, a2 = [];
6182
6122
  for (let i2 = 0; i2 < n9; i2++) {
6183
6123
  const n10 = i2 * t2, o2 = Math.min(n10 + t2 - 1, s2 - 1);
6184
6124
  if (n10 >= s2) break;
6185
- a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: zr(e14.chapterId, i2) });
6125
+ a2.push({ index: i2, url: (null == r2 ? void 0 : r2[i2]) ?? "", localPageStart: n10, localPageEnd: o2, documentId: Gr(e14.chapterId, i2) });
6186
6126
  }
6187
6127
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
6188
6128
  }
@@ -6204,7 +6144,7 @@ function qr(e14, t2) {
6204
6144
  }
6205
6145
  function Hr(e14) {
6206
6146
  var t2;
6207
- const s2 = Rr(e14);
6147
+ const s2 = zr(e14);
6208
6148
  if (!s2) return [];
6209
6149
  const n9 = [], { chapterId: r2 } = e14, a2 = Vr(e14);
6210
6150
  s2 <= 0 && n9.push(`${r2}: segmentPageThreshold must be > 0`);
@@ -6300,13 +6240,13 @@ const Yr = class e11 extends BasePlugin {
6300
6240
  }
6301
6241
  async initialize(e14) {
6302
6242
  if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Kr, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
6303
- const { chapterId: t2 } = Gr(e15.id);
6243
+ const { chapterId: t2 } = Rr(e15.id);
6304
6244
  this.findChapter(t2) && (this.passwordAttempts.delete(t2), this.syncChapterStatusFromDocuments(t2));
6305
6245
  }), this.documentManager.provides().onDocumentClosed((e15) => {
6306
- const { chapterId: t2 } = Gr(e15);
6246
+ const { chapterId: t2 } = Rr(e15);
6307
6247
  this.findChapter(t2) && this.syncChapterStatusFromDocuments(t2);
6308
6248
  }), this.documentManager.provides().onDocumentError((e15) => {
6309
- const { chapterId: t2 } = Gr(e15.documentId);
6249
+ const { chapterId: t2 } = Rr(e15.documentId);
6310
6250
  this.findChapter(t2) && this.handleDocumentError(e15.documentId, t2, e15.code, e15.message);
6311
6251
  })), this.setManifestInternal(e14.manifest), e14.unloadTimeoutMs && e14.unloadTimeoutMs > 0) {
6312
6252
  const t2 = Math.max(5e3, Math.floor(e14.unloadTimeoutMs / 4));
@@ -6368,10 +6308,9 @@ const Yr = class e11 extends BasePlugin {
6368
6308
  if (e14 <= 0 || 0 === this.manifest.chapters.length) return;
6369
6309
  const t2 = Date.now(), s2 = Math.min(e14, this.manifest.chapters.length);
6370
6310
  for (let n9 = 0; n9 < s2; n9++) {
6371
- const e15 = this.manifest.chapters[n9].chapterId;
6372
- this.chapterLastUsed.set(e15, t2);
6373
- const s3 = this.chapterStatus.get(e15) ?? "idle";
6374
- "idle" !== s3 && "closed" !== s3 && "error" !== s3 || this.ensureChapterLoaded(e15);
6311
+ const e15 = this.manifest.chapters[n9];
6312
+ this.chapterLastUsed.set(e15.chapterId, t2), this.resolveChapterUrls(e15).catch(() => {
6313
+ });
6375
6314
  }
6376
6315
  }
6377
6316
  findChapter(e14) {
@@ -6403,11 +6342,7 @@ const Yr = class e11 extends BasePlugin {
6403
6342
  }
6404
6343
  isSegmentLoadPending(e14, t2) {
6405
6344
  const s2 = this.resolveSegmentDocumentId(e14, t2);
6406
- if (!s2) return false;
6407
- if (this.pendingSegmentLoadPromises.has(s2)) return true;
6408
- if (0 === t2 && this.pendingChapterLoadPromises.has(e14)) return true;
6409
- const n9 = this.coreState.core.documents[s2];
6410
- return "loading" === (null == n9 ? void 0 : n9.status);
6345
+ return !!s2 && (!!this.pendingSegmentLoadPromises.has(s2) || !(0 !== t2 || !this.pendingChapterLoadPromises.has(e14)));
6411
6346
  }
6412
6347
  syncChapterStatusFromDocuments(e14) {
6413
6348
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -6438,7 +6373,7 @@ const Yr = class e11 extends BasePlugin {
6438
6373
  clearVisibleSegmentBacklog() {
6439
6374
  this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
6440
6375
  }
6441
- globalSegmentSortKey(e14, t2) {
6376
+ segmentStartGlobalIndex(e14, t2) {
6442
6377
  var s2;
6443
6378
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
6444
6379
  const n9 = this.findChapter(e14);
@@ -6451,12 +6386,12 @@ const Yr = class e11 extends BasePlugin {
6451
6386
  const t2 = [...e14].sort((e15, t3) => e15.priority - t3.priority), s2 = /* @__PURE__ */ new Set(), n9 = [];
6452
6387
  for (const r2 of t2) {
6453
6388
  const e15 = `${r2.chapterId}:${r2.segmentIndex}`;
6454
- s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || this.isSegmentLoadPending(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
6389
+ s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
6455
6390
  }
6456
6391
  this.visibleSegmentBacklog = n9, this.pumpVisibleSegmentBacklog();
6457
6392
  }
6458
6393
  pumpVisibleSegmentBacklog() {
6459
- const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
6394
+ const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
6460
6395
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
6461
6396
  const e15 = this.visibleSegmentBacklog.shift();
6462
6397
  this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
@@ -6478,8 +6413,8 @@ const Yr = class e11 extends BasePlugin {
6478
6413
  if (!s3) return;
6479
6414
  const n10 = Br(s3);
6480
6415
  if (n10 && t3 >= n10.segments.length) return;
6481
- const r3 = `${e15}:${t3}`, a3 = this.globalSegmentSortKey(e15, t3), o3 = i2.get(r3);
6482
- (void 0 === o3 || a3 < o3) && i2.set(r3, a3);
6416
+ const a3 = `${e15}:${t3}`, o3 = this.segmentStartGlobalIndex(e15, t3), l2 = Math.abs(o3 - r2), h3 = i2.get(a3);
6417
+ (void 0 === h3 || l2 < h3) && i2.set(a3, l2);
6483
6418
  };
6484
6419
  for (const d2 of e14) {
6485
6420
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -6600,7 +6535,7 @@ const Yr = class e11 extends BasePlugin {
6600
6535
  return (null == r2 ? void 0 : r2.openPdf) ? r2.openPdf(a2) : { url: n9 };
6601
6536
  }
6602
6537
  async resolvePdfPayload(t2, s2) {
6603
- const n9 = t2.source, r2 = this.config.chapterPdfLoader, a2 = Rr(t2);
6538
+ const n9 = t2.source, r2 = this.config.chapterPdfLoader, a2 = zr(t2);
6604
6539
  if (a2) {
6605
6540
  const n10 = _r(Vr(t2), a2);
6606
6541
  if (s2 < 0 || s2 >= n10) throw new Error(`Chapter "${t2.chapterId}" invalid segmentIndex ${s2} (expected 0..${n10 - 1})`);
@@ -6661,7 +6596,7 @@ const Yr = class e11 extends BasePlugin {
6661
6596
  async handleDocumentError(t2, s2, n9, r2) {
6662
6597
  if (this.pendingSegmentLoadPromises.has(t2)) return;
6663
6598
  if (n9 !== PdfErrorCode.Password) {
6664
- const e14 = this.getSegmentPlanForChapter(s2), { segmentIndex: n10 } = Gr(t2);
6599
+ const e14 = this.getSegmentPlanForChapter(s2), { segmentIndex: n10 } = Rr(t2);
6665
6600
  return void (e14 && 0 !== n10 && null !== n10 || this.updateStatus(s2, "error"));
6666
6601
  }
6667
6602
  if (!this.passwordProvider) return void this.updateStatus(s2, "password-required");
@@ -6711,13 +6646,13 @@ function Jr(e14, t2) {
6711
6646
  }
6712
6647
  const ea = class extends BasePlugin {
6713
6648
  constructor(e14, t2) {
6714
- super(e14, t2), this.layoutChange$ = createBehaviorEmitter(void 0, Jr), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.loadPushRaf = null, this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(Qr.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
6649
+ super(e14, t2), this.layoutChange$ = createBehaviorEmitter(void 0, Jr), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(Qr.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
6715
6650
  }
6716
6651
  async initialize(e14) {
6717
6652
  this.config = { placeholderPageHeight: e14.placeholderPageHeight ?? 1200, placeholderPageWidth: e14.placeholderPageWidth ?? 900, bufferSize: e14.bufferSize ?? 2, pageGap: e14.pageGap ?? 20 }, this.virtualPageMap = this.chapterManager.provides().getVirtualPageMap(), this.rebuildOffsets(), this.chapterManagerUnsubs.push(this.chapterManager.provides().onManifestChange(({ map: e15 }) => {
6718
6653
  this.virtualPageMap = e15, this.pageSizes.clear(), this.lastLoadIndicesKey = "", this.rebuildOffsets(), this.syncReadyChapterSizes(), this.recomputeVisible();
6719
6654
  }), this.chapterManager.provides().onChapterStatusChange((e15) => {
6720
- "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads()));
6655
+ "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.pushVisibleLoads()));
6721
6656
  })), this.syncReadyChapterSizes();
6722
6657
  }
6723
6658
  isChapterDocumentReady(e14) {
@@ -6737,11 +6672,6 @@ const ea = class extends BasePlugin {
6737
6672
  refreshLayout() {
6738
6673
  this.layoutChange$.emit(this.computeLayout());
6739
6674
  }
6740
- schedulePushVisibleLoads() {
6741
- null == this.loadPushRaf && (this.loadPushRaf = requestAnimationFrame(() => {
6742
- this.loadPushRaf = null, this.pushVisibleLoads();
6743
- }));
6744
- }
6745
6675
  pushVisibleLoads() {
6746
6676
  if (!this.virtualPageMap || 0 === this.currentVisible.length) return;
6747
6677
  const e14 = this.expandIndicesWithBuffer(this.currentVisible, this.virtualPageMap.totalPages);
@@ -6757,14 +6687,14 @@ const ea = class extends BasePlugin {
6757
6687
  }
6758
6688
  onDocumentLoaded(e14) {
6759
6689
  var t2;
6760
- const { chapterId: s2 } = Nr(e14);
6690
+ const { chapterId: s2 } = Fr(e14);
6761
6691
  if (0 === ((null == (t2 = this.virtualPageMap) ? void 0 : t2.pagesOfChapter(s2).length) ?? 0)) return;
6762
6692
  const n9 = this.documentManager.provides();
6763
- n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads();
6693
+ n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.pushVisibleLoads();
6764
6694
  }
6765
6695
  onDocumentClosed(e14) {
6766
6696
  var t2;
6767
- const { chapterId: s2 } = Nr(e14);
6697
+ const { chapterId: s2 } = Fr(e14);
6768
6698
  0 !== ((null == (t2 = this.virtualPageMap) ? void 0 : t2.pagesOfChapter(s2).length) ?? 0) && (this.dropChapterSizes(s2), this.refreshLayout());
6769
6699
  }
6770
6700
  buildCapability() {
@@ -6774,7 +6704,7 @@ const ea = class extends BasePlugin {
6774
6704
  var e14;
6775
6705
  super.destroy();
6776
6706
  for (const t2 of this.chapterManagerUnsubs) t2();
6777
- this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, null != this.loadPushRaf && (cancelAnimationFrame(this.loadPushRaf), this.loadPushRaf = null), this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
6707
+ this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
6778
6708
  }
6779
6709
  getPageSize(e14) {
6780
6710
  return this.pageSizes.get(e14) ?? { width: this.config.placeholderPageWidth, height: this.config.placeholderPageHeight };
@@ -6838,7 +6768,7 @@ const ea = class extends BasePlugin {
6838
6768
  onScaleChanged(e14) {
6839
6769
  const t2 = this.coreState.core.documents[e14];
6840
6770
  if (!t2 || "loaded" !== t2.status) return;
6841
- const { chapterId: s2 } = Nr(e14);
6771
+ const { chapterId: s2 } = Fr(e14);
6842
6772
  this.chapterManager.provides().getChapter(s2) && this.harvestChapterSizes(s2);
6843
6773
  }
6844
6774
  dropChapterSizes(e14) {
@@ -6883,7 +6813,7 @@ const ea = class extends BasePlugin {
6883
6813
  const e15 = this.offsets[l], n10 = e15 + this.getPageSize(l).height, r3 = Math.max(0, Math.min(n10, s2) - Math.max(e15, t2));
6884
6814
  r3 > o2 && (o2 = r3, i2 = l);
6885
6815
  }
6886
- this.currentVisible = a2, this.currentGlobalPageIndex = i2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: i2 }), this.schedulePushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
6816
+ this.currentVisible = a2, this.currentGlobalPageIndex = i2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: i2 }), this.pushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
6887
6817
  }
6888
6818
  findFirstVisibleIndex(e14) {
6889
6819
  let t2 = 0, s2 = this.offsets.length - 1, n9 = 0;
@@ -7312,15 +7242,15 @@ const Ma = /* @__PURE__ */ defineComponent({ __name: "HoverBookmarkLayer", props
7312
7242
  var _a2;
7313
7243
  return unref(u2) && e14.pdfPageWidth && e14.pdfPageHeight ? (openBlock(), createElementBlock("div", { key: 0, ref_key: "rootRef", ref: g2, style: { position: "absolute", inset: "0", zIndex: 14, pointerEvents: "none" } }, [D2.value && S.value && !$2.value ? (openBlock(), createElementBlock("div", { key: 0, style: normalizeStyle({ position: "absolute", left: `${D2.value.zoneLeft}px`, top: `${D2.value.zoneTop}px`, width: `${D2.value.size + 20}px`, height: `${(((_a2 = S.value[S.value.length - 1]) == null ? void 0 : _a2.size.height) ?? 0) * (e14.cssPageHeight / e14.pdfPageHeight) + D2.value.size + 12}px`, pointerEvents: "auto" }), onMouseenter: s2[0] || (s2[0] = (e15) => I2.value = S.value), onMouseleave: s2[1] || (s2[1] = (e15) => I2.value = null) }, [createElementVNode("button", { type: "button", title: "添加书签", style: normalizeStyle({ position: "absolute", left: D2.value.iconLeft - D2.value.zoneLeft + "px", top: D2.value.iconTop - D2.value.zoneTop + "px", width: `${D2.value.size}px`, height: `${D2.value.size}px`, cursor: "pointer", display: "flex", alignItems: "center", justifyContent: "center", background: "#3b82f6", color: "#fff", border: "none", borderRadius: "4px", boxShadow: "0 2px 8px rgba(37, 99, 235, 0.45)", fontSize: "12px", padding: 0 }), onClick: withModifiers(k2, ["stop"]) }, " 🔖 ", 4)], 36)) : createCommentVNode("", true)], 512)) : createCommentVNode("", true);
7314
7244
  };
7315
- } }), xa = { key: 0, width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", "aria-hidden": "" }, La = { key: 1, width: "24", height: "24", viewBox: "0 0 28 28", fill: "none", "aria-hidden": "" }, $a = { key: 0, x: "5", y: "4", width: "18", height: "20", rx: "3", fill: "#fde047" }, ka = { key: 1, d: "M6 22h16", stroke: "#dc2626", "stroke-width": "2.2", "stroke-linecap": "round" }, Da = { key: 2, d: "M6 22c2-2 3-2 5 0s3 2 5 0 3-2 5 0", stroke: "#dc2626", "stroke-width": "2", "stroke-linecap": "round", fill: "none" }, Ea = { key: 3, d: "M5 14h18", stroke: "#dc2626", "stroke-width": "2.2", "stroke-linecap": "round" }, Ua = { key: 2, width: "18", height: "18", viewBox: "0 0 24 24", fill: "none", "aria-hidden": "" }, Ta = /* @__PURE__ */ defineComponent({ __name: "SelectionToolbarIcons", props: { kind: {} }, setup: (e14) => (t2, s2) => "copy" === e14.kind ? (openBlock(), createElementBlock("svg", xa, [...s2[0] || (s2[0] = [createElementVNode("rect", { x: "8.5", y: "8.5", width: "11", height: "13", rx: "2.25", fill: "#f1f5f9", stroke: "#94a3b8", "stroke-width": "1.35" }, null, -1), createElementVNode("rect", { x: "4.5", y: "3.5", width: "11", height: "13", rx: "2.25", fill: "#ffffff", stroke: "#334155", "stroke-width": "1.5" }, null, -1), createElementVNode("path", { d: "M7.5 7h5.5a1.5 1.5 0 0 1 1.5 1.5V14", stroke: "#64748b", "stroke-width": "1.2", "stroke-linecap": "round" }, null, -1)])])) : "note" !== e14.kind ? (openBlock(), createElementBlock("svg", La, ["highlight" === e14.kind ? (openBlock(), createElementBlock("rect", $a)) : createCommentVNode("", true), s2[1] || (s2[1] = createElementVNode("text", { x: "14", y: "19", "text-anchor": "middle", "font-size": "15", "font-weight": "700", "font-family": "system-ui, -apple-system, sans-serif", fill: "#0f172a" }, " A ", -1)), "underline" === e14.kind ? (openBlock(), createElementBlock("path", ka)) : "squiggly" === e14.kind ? (openBlock(), createElementBlock("path", Da)) : "strikeout" === e14.kind ? (openBlock(), createElementBlock("path", Ea)) : createCommentVNode("", true)])) : (openBlock(), createElementBlock("svg", Ua, [...s2[2] || (s2[2] = [createElementVNode("path", { d: "M8 3h8l4 4v14H8V3z", fill: "#fef3c7", stroke: "#d97706", "stroke-width": "1.2", "stroke-linejoin": "round" }, null, -1), createElementVNode("path", { d: "M16 3v4h4", stroke: "#d97706", "stroke-width": "1.2" }, null, -1), createElementVNode("path", { d: "M10 12h6M10 16h4", stroke: "#92400e", "stroke-width": "1.2", "stroke-linecap": "round" }, null, -1)])])) }), Fa = ["title", "aria-label", "onMouseenter", "onClick"], Na = ["title", "onMouseenter", "onClick"], Aa = { style: { "font-size": "11px", "font-weight": "600" } }, Va = /* @__PURE__ */ defineComponent({ __name: "SelectionToolbarCard", props: { placementAbove: { type: Boolean }, anchorHeight: {}, hiddenBuiltinActions: {}, extraActions: {}, renderCopyIcon: { type: Function } }, emits: ["action"], setup(e14, { emit: n9 }) {
7245
+ } }), xa = { key: 0, width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", "aria-hidden": "" }, La = { key: 1, width: "24", height: "24", viewBox: "0 0 28 28", fill: "none", "aria-hidden": "" }, $a = { key: 0, x: "5", y: "4", width: "18", height: "20", rx: "3", fill: "#fde047" }, ka = { key: 1, d: "M6 22h16", stroke: "#dc2626", "stroke-width": "2.2", "stroke-linecap": "round" }, Da = { key: 2, d: "M6 22c2-2 3-2 5 0s3 2 5 0 3-2 5 0", stroke: "#dc2626", "stroke-width": "2", "stroke-linecap": "round", fill: "none" }, Ea = { key: 3, d: "M5 14h18", stroke: "#dc2626", "stroke-width": "2.2", "stroke-linecap": "round" }, Ua = { key: 2, width: "18", height: "18", viewBox: "0 0 24 24", fill: "none", "aria-hidden": "" }, Ta = /* @__PURE__ */ defineComponent({ __name: "SelectionToolbarIcons", props: { kind: {} }, setup: (e14) => (t2, s2) => "copy" === e14.kind ? (openBlock(), createElementBlock("svg", xa, [...s2[0] || (s2[0] = [createElementVNode("rect", { x: "8.5", y: "8.5", width: "11", height: "13", rx: "2.25", fill: "#f1f5f9", stroke: "#94a3b8", "stroke-width": "1.35" }, null, -1), createElementVNode("rect", { x: "4.5", y: "3.5", width: "11", height: "13", rx: "2.25", fill: "#ffffff", stroke: "#334155", "stroke-width": "1.5" }, null, -1), createElementVNode("path", { d: "M7.5 7h5.5a1.5 1.5 0 0 1 1.5 1.5V14", stroke: "#64748b", "stroke-width": "1.2", "stroke-linecap": "round" }, null, -1)])])) : "note" !== e14.kind ? (openBlock(), createElementBlock("svg", La, ["highlight" === e14.kind ? (openBlock(), createElementBlock("rect", $a)) : createCommentVNode("", true), s2[1] || (s2[1] = createElementVNode("text", { x: "14", y: "19", "text-anchor": "middle", "font-size": "15", "font-weight": "700", "font-family": "system-ui, -apple-system, sans-serif", fill: "#0f172a" }, " A ", -1)), "underline" === e14.kind ? (openBlock(), createElementBlock("path", ka)) : "squiggly" === e14.kind ? (openBlock(), createElementBlock("path", Da)) : "strikeout" === e14.kind ? (openBlock(), createElementBlock("path", Ea)) : createCommentVNode("", true)])) : (openBlock(), createElementBlock("svg", Ua, [...s2[2] || (s2[2] = [createElementVNode("path", { d: "M8 3h8l4 4v14H8V3z", fill: "#fef3c7", stroke: "#d97706", "stroke-width": "1.2", "stroke-linejoin": "round" }, null, -1), createElementVNode("path", { d: "M16 3v4h4", stroke: "#d97706", "stroke-width": "1.2" }, null, -1), createElementVNode("path", { d: "M10 12h6M10 16h4", stroke: "#92400e", "stroke-width": "1.2", "stroke-linecap": "round" }, null, -1)])])) }), Na = ["title", "aria-label", "onMouseenter", "onClick"], Fa = ["title", "onMouseenter", "onClick"], Aa = { style: { "font-size": "11px", "font-weight": "600" } }, Va = /* @__PURE__ */ defineComponent({ __name: "SelectionToolbarCard", props: { placementAbove: { type: Boolean }, anchorHeight: {}, hiddenBuiltinActions: {}, extraActions: {}, renderCopyIcon: { type: Function } }, emits: ["action"], setup(e14, { emit: n9 }) {
7316
7246
  const r2 = e14, a2 = n9, i2 = [{ id: "highlight", label: "高亮背景" }, { id: "underline", label: "下划线" }, { id: "squiggly", label: "波浪线" }, { id: "strikeout", label: "删除线" }], d2 = computed(() => new Set(r2.hiddenBuiltinActions ?? [])), p2 = computed(() => !d2.value.has("copy")), f2 = computed(() => i2.filter((e15) => !d2.value.has(e15.id))), v = computed(() => !d2.value.has("note")), I2 = computed(() => [...r2.extraActions ?? []].sort((e15, t2) => (e15.order ?? 0) - (t2.order ?? 0))), b2 = computed(() => f2.value.length > 0 || I2.value.length > 0), w2 = ref(null), M2 = computed(() => ({ position: "absolute", pointerEvents: "auto", left: "50%", transform: "translateX(-50%)", top: r2.placementAbove ? void 0 : `${r2.anchorHeight + 10}px`, bottom: r2.placementAbove ? `${r2.anchorHeight + 10}px` : void 0, display: "flex", alignItems: "center", gap: "2px", padding: "4px 6px", borderRadius: "12px", background: "rgba(255, 255, 255, 0.96)", border: "1px solid rgba(15, 23, 42, 0.06)", boxShadow: "0 2px 8px rgba(15, 23, 42, 0.08), 0 12px 32px rgba(15, 23, 42, 0.1)", backdropFilter: "blur(10px)", WebkitBackdropFilter: "blur(10px)" }));
7317
7247
  function x(e15, t2 = false) {
7318
7248
  return { display: "flex", alignItems: "center", justifyContent: "center", width: "32px", height: "32px", padding: 0, margin: 0, border: "none", borderRadius: "8px", background: w2.value === e15 ? t2 ? "#fffbeb" : "#f1f5f9" : "transparent", color: t2 ? "#b45309" : "#334155", cursor: "pointer", flexShrink: 0, WebkitAppearance: "none", appearance: "none" };
7319
7249
  }
7320
7250
  const L2 = { width: "1px", height: "20px", margin: "0 4px", background: "rgba(15, 23, 42, 0.1)", flexShrink: 0 };
7321
- return (t2, s2) => (openBlock(), createElementBlock("div", { role: "toolbar", "aria-label": "选区工具", style: normalizeStyle(M2.value) }, [p2.value ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [createElementVNode("button", { type: "button", title: "复制", "aria-label": "复制", style: normalizeStyle(x("copy")), onMouseenter: s2[0] || (s2[0] = (e15) => w2.value = "copy"), onMouseleave: s2[1] || (s2[1] = (e15) => w2.value = null), onClick: s2[2] || (s2[2] = (e15) => a2("action", "copy")) }, [e14.renderCopyIcon ? (openBlock(), createBlock(resolveDynamicComponent(e14.renderCopyIcon), { key: 0 })) : (openBlock(), createBlock(Ta, { key: 1, kind: "copy" }))], 36), b2.value ? (openBlock(), createElementBlock("span", { key: 0, style: L2, "aria-hidden": "" })) : createCommentVNode("", true)], 64)) : createCommentVNode("", true), (openBlock(true), createElementBlock(Fragment, null, renderList(f2.value, (e15) => (openBlock(), createElementBlock("button", { key: e15.id, type: "button", title: e15.label, "aria-label": e15.label, style: normalizeStyle(x(e15.id)), onMouseenter: (t3) => w2.value = e15.id, onMouseleave: s2[3] || (s2[3] = (e16) => w2.value = null), onClick: (t3) => a2("action", e15.id) }, [createVNode(Ta, { kind: e15.id }, null, 8, ["kind"])], 44, Fa))), 128)), (openBlock(true), createElementBlock(Fragment, null, renderList(I2.value, (e15) => (openBlock(), createElementBlock("button", { key: e15.id, type: "button", title: e15.label, style: normalizeStyle(x(e15.id)), onMouseenter: (t3) => w2.value = e15.id, onMouseleave: s2[4] || (s2[4] = (e16) => w2.value = null), onClick: (t3) => a2("action", e15.id) }, [createElementVNode("span", Aa, toDisplayString(e15.label.slice(0, 2)), 1)], 44, Na))), 128)), v.value ? (openBlock(), createElementBlock(Fragment, { key: 1 }, [b2.value || p2.value ? (openBlock(), createElementBlock("span", { key: 0, style: L2, "aria-hidden": "" })) : createCommentVNode("", true), createElementVNode("button", { type: "button", title: "添加笔记", "aria-label": "添加笔记", style: normalizeStyle(x("note", true)), onMouseenter: s2[5] || (s2[5] = (e15) => w2.value = "note"), onMouseleave: s2[6] || (s2[6] = (e15) => w2.value = null), onClick: s2[7] || (s2[7] = (e15) => a2("action", "note")) }, [createVNode(Ta, { kind: "note" })], 36)], 64)) : createCommentVNode("", true)], 4));
7251
+ return (t2, s2) => (openBlock(), createElementBlock("div", { role: "toolbar", "aria-label": "选区工具", style: normalizeStyle(M2.value) }, [p2.value ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [createElementVNode("button", { type: "button", title: "复制", "aria-label": "复制", style: normalizeStyle(x("copy")), onMouseenter: s2[0] || (s2[0] = (e15) => w2.value = "copy"), onMouseleave: s2[1] || (s2[1] = (e15) => w2.value = null), onClick: s2[2] || (s2[2] = (e15) => a2("action", "copy")) }, [e14.renderCopyIcon ? (openBlock(), createBlock(resolveDynamicComponent(e14.renderCopyIcon), { key: 0 })) : (openBlock(), createBlock(Ta, { key: 1, kind: "copy" }))], 36), b2.value ? (openBlock(), createElementBlock("span", { key: 0, style: L2, "aria-hidden": "" })) : createCommentVNode("", true)], 64)) : createCommentVNode("", true), (openBlock(true), createElementBlock(Fragment, null, renderList(f2.value, (e15) => (openBlock(), createElementBlock("button", { key: e15.id, type: "button", title: e15.label, "aria-label": e15.label, style: normalizeStyle(x(e15.id)), onMouseenter: (t3) => w2.value = e15.id, onMouseleave: s2[3] || (s2[3] = (e16) => w2.value = null), onClick: (t3) => a2("action", e15.id) }, [createVNode(Ta, { kind: e15.id }, null, 8, ["kind"])], 44, Na))), 128)), (openBlock(true), createElementBlock(Fragment, null, renderList(I2.value, (e15) => (openBlock(), createElementBlock("button", { key: e15.id, type: "button", title: e15.label, style: normalizeStyle(x(e15.id)), onMouseenter: (t3) => w2.value = e15.id, onMouseleave: s2[4] || (s2[4] = (e16) => w2.value = null), onClick: (t3) => a2("action", e15.id) }, [createElementVNode("span", Aa, toDisplayString(e15.label.slice(0, 2)), 1)], 44, Fa))), 128)), v.value ? (openBlock(), createElementBlock(Fragment, { key: 1 }, [b2.value || p2.value ? (openBlock(), createElementBlock("span", { key: 0, style: L2, "aria-hidden": "" })) : createCommentVNode("", true), createElementVNode("button", { type: "button", title: "添加笔记", "aria-label": "添加笔记", style: normalizeStyle(x("note", true)), onMouseenter: s2[5] || (s2[5] = (e15) => w2.value = "note"), onMouseleave: s2[6] || (s2[6] = (e15) => w2.value = null), onClick: s2[7] || (s2[7] = (e15) => a2("action", "note")) }, [createVNode(Ta, { kind: "note" })], 36)], 64)) : createCommentVNode("", true)], 4));
7322
7252
  } }), Oa = { highlight: "highlight", underline: "underline", squiggly: "squiggly", strikeout: "strikeout" };
7323
- const Ra = /* @__PURE__ */ defineComponent({ __name: "PdfChapterViewport", props: { className: {}, features: {}, buildSelectionMenu: {}, annotationSelectionMenu: {}, redactionSelectionMenu: {}, showNoteMarkers: { type: Boolean, default: true }, showBookmarkMarkers: { type: Boolean, default: true }, showRedactionLayer: { type: Boolean, default: false }, renderPageOverlay: {}, onExtraSelectionAction: {} }, setup(e14) {
7253
+ const za = /* @__PURE__ */ defineComponent({ __name: "PdfChapterViewport", props: { className: {}, features: {}, buildSelectionMenu: {}, annotationSelectionMenu: {}, redactionSelectionMenu: {}, showNoteMarkers: { type: Boolean, default: true }, showBookmarkMarkers: { type: Boolean, default: true }, showRedactionLayer: { type: Boolean, default: false }, renderPageOverlay: {}, onExtraSelectionAction: {} }, setup(e14) {
7324
7254
  const s2 = e14, n9 = computed(() => s2.features), { hostRef: r2 } = Wn(n9), a2 = computed(() => {
7325
7255
  var _a2, _b;
7326
7256
  return { background: ((_b = (_a2 = s2.features) == null ? void 0 : _a2.scrollViewport) == null ? void 0 : _b.background) ?? Se$1 };
@@ -7422,14 +7352,14 @@ const Ra = /* @__PURE__ */ defineComponent({ __name: "PdfChapterViewport", props
7422
7352
  var _a2, _b, _c, _d;
7423
7353
  return [createVNode(wa, { item: t3, features: e14.features, "show-note-markers": g2.value, "show-bookmark-markers": m2.value, "show-redaction-layer": e14.showRedactionLayer, "note-marker-ui": (_b = (_a2 = e14.features) == null ? void 0 : _a2.notes) == null ? void 0 : _b.marker, "bookmark-marker-ui": (_d = (_c = e14.features) == null ? void 0 : _c.bookmarks) == null ? void 0 : _d.marker, "build-selection-menu": C2.value, "create-note": unref(i2), "annotation-selection-menu": e14.annotationSelectionMenu, "redaction-selection-menu": e14.redactionSelectionMenu, "render-page-overlay": x }, null, 8, ["item", "features", "show-note-markers", "show-bookmark-markers", "show-redaction-layer", "note-marker-ui", "bookmark-marker-ui", "build-selection-menu", "create-note", "annotation-selection-menu", "redaction-selection-menu"])];
7424
7354
  }), _: 1 }, 8, ["class", "style"]), renderSlot(t2.$slots, "default")], 6));
7425
- } }), za = { key: 0, style: { display: "flex", flexDirection: "column", height: "100%", minHeight: 0, width: "100%" } }, Ga = /* @__PURE__ */ defineComponent({ __name: "ChapterPdfViewer", props: { engine: {}, options: {}, editorOptions: {}, features: {}, className: {}, viewportClassName: {}, onExtraSelectionAction: { type: Function } }, setup(e14) {
7355
+ } }), Ga = { key: 0, style: { display: "flex", flexDirection: "column", height: "100%", minHeight: 0, width: "100%" } }, Ra = /* @__PURE__ */ defineComponent({ __name: "ChapterPdfViewer", props: { engine: {}, options: {}, editorOptions: {}, features: {}, className: {}, viewportClassName: {}, onExtraSelectionAction: { type: Function } }, setup(e14) {
7426
7356
  const s2 = e14, n9 = computed(() => (function(e15) {
7427
7357
  if (e15.options) return Un(e15.options);
7428
7358
  if (!e15.editorOptions) throw new Error("ChapterPdfViewer: 请传入 `options`(推荐),或同时传入 `editorOptions` 与 `features`");
7429
7359
  const t2 = ke$1(e15.features), { plugins: s3 } = $n({ ...e15.editorOptions, features: t2 });
7430
7360
  return { plugins: s3, features: t2, editorOptions: { ...e15.editorOptions, features: t2 } };
7431
7361
  })({ engine: s2.engine, options: s2.options, editorOptions: s2.editorOptions, features: s2.features }));
7432
- return (t2, s3) => (openBlock(), createElementBlock("div", { class: normalizeClass(e14.className), style: normalizeStyle(e14.className ? void 0 : unref(Pe$1)) }, [createVNode(unref(EmbedPDF), { engine: e14.engine, plugins: n9.value.plugins }, { default: withCtx(({ pluginsReady: s4 }) => [s4 ? (openBlock(), createElementBlock("div", za, [renderSlot(t2.$slots, "prepend"), createVNode(Ra, { class: normalizeClass(e14.viewportClassName), style: { flex: 1, minHeight: 0 }, features: n9.value.features, "on-extra-selection-action": e14.onExtraSelectionAction }, null, 8, ["class", "features", "on-extra-selection-action"]), renderSlot(t2.$slots, "default")])) : createCommentVNode("", true)]), _: 3 }, 8, ["engine", "plugins"])], 6));
7362
+ return (t2, s3) => (openBlock(), createElementBlock("div", { class: normalizeClass(e14.className), style: normalizeStyle(e14.className ? void 0 : unref(Pe$1)) }, [createVNode(unref(EmbedPDF), { engine: e14.engine, plugins: n9.value.plugins }, { default: withCtx(({ pluginsReady: s4 }) => [s4 ? (openBlock(), createElementBlock("div", Ga, [renderSlot(t2.$slots, "prepend"), createVNode(za, { class: normalizeClass(e14.viewportClassName), style: { flex: 1, minHeight: 0 }, features: n9.value.features, "on-extra-selection-action": e14.onExtraSelectionAction }, null, 8, ["class", "features", "on-extra-selection-action"]), renderSlot(t2.$slots, "default")])) : createCommentVNode("", true)]), _: 3 }, 8, ["engine", "plugins"])], 6));
7433
7363
  } }), _a = { class: "chapter-tree-row" }, Ba = ["aria-expanded"], Wa = { key: 1, class: "chapter-tree-toggle-spacer", "aria-hidden": "" }, qa = { class: "chapter-tree-label" }, Ha = { class: "chapter-tree-meta" }, Ka = { key: 0 }, ja = (e14, t2) => {
7434
7364
  const s2 = e14.__vccOpts || e14;
7435
7365
  for (const [n9, r2] of t2) s2[n9] = r2;
@@ -7469,7 +7399,7 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
7469
7399
  },
7470
7400
  setup(__props) {
7471
7401
  return (_ctx, _cache) => {
7472
- return openBlock(), createBlock(unref(Ga), normalizeProps(guardReactiveProps(_ctx.$props)), createSlots({
7402
+ return openBlock(), createBlock(unref(Ra), normalizeProps(guardReactiveProps(_ctx.$props)), createSlots({
7473
7403
  default: withCtx(() => [
7474
7404
  renderSlot(_ctx.$slots, "default")
7475
7405
  ]),
@@ -7551,8 +7481,8 @@ function A(e14) {
7551
7481
  for (let h2 = 0; h2 < o2.urls.length; h2++) (null == (t2 = o2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
7552
7482
  return n9;
7553
7483
  }
7554
- const V = { kind: "first-wins" };
7555
- function O(e14, t2, s2) {
7484
+ const O = { kind: "first-wins" };
7485
+ function V(e14, t2, s2) {
7556
7486
  if (1 === t2.length) return t2[0].chapterId;
7557
7487
  switch (s2.kind) {
7558
7488
  case "first-wins":
@@ -7570,7 +7500,7 @@ function O(e14, t2, s2) {
7570
7500
  return s2.resolve(e14, t2);
7571
7501
  }
7572
7502
  }
7573
- let R$1 = class R {
7503
+ let G$1 = class G {
7574
7504
  constructor(e14) {
7575
7505
  this._pages = e14, this._byChapter = /* @__PURE__ */ new Map(), this._byGlobalNumber = /* @__PURE__ */ new Map();
7576
7506
  for (const t2 of e14) {
@@ -7604,8 +7534,8 @@ let R$1 = class R {
7604
7534
  return s2 ? this._pages.indexOf(s2) : -1;
7605
7535
  }
7606
7536
  };
7607
- function G$1(e14, t2 = V) {
7608
- const s2 = (function(e15, t3 = V) {
7537
+ function R$1(e14, t2 = O) {
7538
+ const s2 = (function(e15, t3 = O) {
7609
7539
  const s3 = /* @__PURE__ */ new Map();
7610
7540
  for (const r3 of e15.chapters) {
7611
7541
  const [e16, t4] = r3.globalPageRange;
@@ -7615,7 +7545,7 @@ function G$1(e14, t2 = V) {
7615
7545
  }
7616
7546
  }
7617
7547
  const n10 = /* @__PURE__ */ new Map();
7618
- for (const [r3, a3] of s3) n10.set(r3, O(r3, a3, t3));
7548
+ for (const [r3, a3] of s3) n10.set(r3, V(r3, a3, t3));
7619
7549
  return n10;
7620
7550
  })(e14, t2), n9 = /* @__PURE__ */ new Map();
7621
7551
  for (const i2 of e14.chapters) n9.set(i2.chapterId, i2);
@@ -7627,14 +7557,14 @@ function G$1(e14, t2 = V) {
7627
7557
  const [r3] = t3.globalPageRange, [h2] = t3.localPageRange, l = h2 + (i2 - r3);
7628
7558
  a2.push({ globalPageIndex: o2++, globalPageNumber: i2, chapterId: e15, localPageIndex: l });
7629
7559
  }
7630
- return new R$1(a2);
7560
+ return new G$1(a2);
7631
7561
  }
7632
7562
  const z = class n extends BasePlugin {
7633
7563
  constructor(e14, n9) {
7634
- super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = V, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
7564
+ super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = O, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
7635
7565
  }
7636
7566
  async initialize(e14) {
7637
- if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? V, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
7567
+ if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? O, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
7638
7568
  const { chapterId: t2 } = U(e15.id);
7639
7569
  this.findChapter(t2) && (this.passwordAttempts.delete(t2), this.syncChapterStatusFromDocuments(t2));
7640
7570
  }), this.documentManager.provides().onDocumentClosed((e15) => {
@@ -7696,17 +7626,16 @@ const z = class n extends BasePlugin {
7696
7626
  const r2 = new Set(this.chapterStatus.keys()), a2 = new Set(t2.chapters.map((e15) => e15.chapterId));
7697
7627
  for (const n9 of r2) a2.has(n9) || (this.closeChapter(n9), this.chapterStatus.delete(n9), this.chapterErrorMessages.delete(n9), this.chapterUrlsCache.delete(n9), this.pendingChapterUrlsPromises.delete(n9));
7698
7628
  for (const n9 of t2.chapters) this.chapterStatus.has(n9.chapterId) || this.chapterStatus.set(n9.chapterId, "idle");
7699
- this.manifest = t2, this.lastVisibleIndicesKey = "", this.clearVisibleSegmentBacklog(), this.virtualPageMap = G$1(t2, this.overlapStrategy), this.manifestChange$.emit({ manifest: t2, map: this.virtualPageMap }), this.eagerPrefetchFromManifest();
7629
+ this.manifest = t2, this.lastVisibleIndicesKey = "", this.clearVisibleSegmentBacklog(), this.virtualPageMap = R$1(t2, this.overlapStrategy), this.manifestChange$.emit({ manifest: t2, map: this.virtualPageMap }), this.eagerPrefetchFromManifest();
7700
7630
  }
7701
7631
  eagerPrefetchFromManifest() {
7702
7632
  const e14 = this.config.prefetchChapters ?? 0;
7703
7633
  if (e14 <= 0 || 0 === this.manifest.chapters.length) return;
7704
7634
  const t2 = Date.now(), s2 = Math.min(e14, this.manifest.chapters.length);
7705
7635
  for (let n9 = 0; n9 < s2; n9++) {
7706
- const e15 = this.manifest.chapters[n9].chapterId;
7707
- this.chapterLastUsed.set(e15, t2);
7708
- const s3 = this.chapterStatus.get(e15) ?? "idle";
7709
- "idle" !== s3 && "closed" !== s3 && "error" !== s3 || this.ensureChapterLoaded(e15);
7636
+ const e15 = this.manifest.chapters[n9];
7637
+ this.chapterLastUsed.set(e15.chapterId, t2), this.resolveChapterUrls(e15).catch(() => {
7638
+ });
7710
7639
  }
7711
7640
  }
7712
7641
  findChapter(e14) {
@@ -7738,11 +7667,7 @@ const z = class n extends BasePlugin {
7738
7667
  }
7739
7668
  isSegmentLoadPending(e14, t2) {
7740
7669
  const s2 = this.resolveSegmentDocumentId(e14, t2);
7741
- if (!s2) return false;
7742
- if (this.pendingSegmentLoadPromises.has(s2)) return true;
7743
- if (0 === t2 && this.pendingChapterLoadPromises.has(e14)) return true;
7744
- const n9 = this.coreState.core.documents[s2];
7745
- return "loading" === (null == n9 ? void 0 : n9.status);
7670
+ return !!s2 && (!!this.pendingSegmentLoadPromises.has(s2) || !(0 !== t2 || !this.pendingChapterLoadPromises.has(e14)));
7746
7671
  }
7747
7672
  syncChapterStatusFromDocuments(e14) {
7748
7673
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -7773,7 +7698,7 @@ const z = class n extends BasePlugin {
7773
7698
  clearVisibleSegmentBacklog() {
7774
7699
  this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
7775
7700
  }
7776
- globalSegmentSortKey(e14, t2) {
7701
+ segmentStartGlobalIndex(e14, t2) {
7777
7702
  var s2;
7778
7703
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
7779
7704
  const n9 = this.findChapter(e14);
@@ -7786,12 +7711,12 @@ const z = class n extends BasePlugin {
7786
7711
  const t2 = [...e14].sort((e15, t3) => e15.priority - t3.priority), s2 = /* @__PURE__ */ new Set(), n9 = [];
7787
7712
  for (const r2 of t2) {
7788
7713
  const e15 = `${r2.chapterId}:${r2.segmentIndex}`;
7789
- s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || this.isSegmentLoadPending(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
7714
+ s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
7790
7715
  }
7791
7716
  this.visibleSegmentBacklog = n9, this.pumpVisibleSegmentBacklog();
7792
7717
  }
7793
7718
  pumpVisibleSegmentBacklog() {
7794
- const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
7719
+ const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
7795
7720
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
7796
7721
  const e15 = this.visibleSegmentBacklog.shift();
7797
7722
  this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
@@ -7813,8 +7738,8 @@ const z = class n extends BasePlugin {
7813
7738
  if (!s3) return;
7814
7739
  const n10 = T(s3);
7815
7740
  if (n10 && t3 >= n10.segments.length) return;
7816
- const r3 = `${e15}:${t3}`, a3 = this.globalSegmentSortKey(e15, t3), i3 = o2.get(r3);
7817
- (void 0 === i3 || a3 < i3) && o2.set(r3, a3);
7741
+ const a3 = `${e15}:${t3}`, i3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(i3 - r2), l2 = o2.get(a3);
7742
+ (void 0 === l2 || h3 < l2) && o2.set(a3, h3);
7818
7743
  };
7819
7744
  for (const d2 of e14) {
7820
7745
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -8284,10 +8209,9 @@ const de = class n2 extends BasePlugin {
8284
8209
  if (e14 <= 0 || 0 === this.manifest.chapters.length) return;
8285
8210
  const t2 = Date.now(), s2 = Math.min(e14, this.manifest.chapters.length);
8286
8211
  for (let n9 = 0; n9 < s2; n9++) {
8287
- const e15 = this.manifest.chapters[n9].chapterId;
8288
- this.chapterLastUsed.set(e15, t2);
8289
- const s3 = this.chapterStatus.get(e15) ?? "idle";
8290
- "idle" !== s3 && "closed" !== s3 && "error" !== s3 || this.ensureChapterLoaded(e15);
8212
+ const e15 = this.manifest.chapters[n9];
8213
+ this.chapterLastUsed.set(e15.chapterId, t2), this.resolveChapterUrls(e15).catch(() => {
8214
+ });
8291
8215
  }
8292
8216
  }
8293
8217
  findChapter(e14) {
@@ -8319,11 +8243,7 @@ const de = class n2 extends BasePlugin {
8319
8243
  }
8320
8244
  isSegmentLoadPending(e14, t2) {
8321
8245
  const s2 = this.resolveSegmentDocumentId(e14, t2);
8322
- if (!s2) return false;
8323
- if (this.pendingSegmentLoadPromises.has(s2)) return true;
8324
- if (0 === t2 && this.pendingChapterLoadPromises.has(e14)) return true;
8325
- const n9 = this.coreState.core.documents[s2];
8326
- return "loading" === (null == n9 ? void 0 : n9.status);
8246
+ return !!s2 && (!!this.pendingSegmentLoadPromises.has(s2) || !(0 !== t2 || !this.pendingChapterLoadPromises.has(e14)));
8327
8247
  }
8328
8248
  syncChapterStatusFromDocuments(e14) {
8329
8249
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -8354,7 +8274,7 @@ const de = class n2 extends BasePlugin {
8354
8274
  clearVisibleSegmentBacklog() {
8355
8275
  this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
8356
8276
  }
8357
- globalSegmentSortKey(e14, t2) {
8277
+ segmentStartGlobalIndex(e14, t2) {
8358
8278
  var s2;
8359
8279
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
8360
8280
  const n9 = this.findChapter(e14);
@@ -8367,12 +8287,12 @@ const de = class n2 extends BasePlugin {
8367
8287
  const t2 = [...e14].sort((e15, t3) => e15.priority - t3.priority), s2 = /* @__PURE__ */ new Set(), n9 = [];
8368
8288
  for (const r2 of t2) {
8369
8289
  const e15 = `${r2.chapterId}:${r2.segmentIndex}`;
8370
- s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || this.isSegmentLoadPending(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
8290
+ s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
8371
8291
  }
8372
8292
  this.visibleSegmentBacklog = n9, this.pumpVisibleSegmentBacklog();
8373
8293
  }
8374
8294
  pumpVisibleSegmentBacklog() {
8375
- const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
8295
+ const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
8376
8296
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
8377
8297
  const e15 = this.visibleSegmentBacklog.shift();
8378
8298
  this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
@@ -8394,8 +8314,8 @@ const de = class n2 extends BasePlugin {
8394
8314
  if (!s3) return;
8395
8315
  const n10 = se(s3);
8396
8316
  if (n10 && t3 >= n10.segments.length) return;
8397
- const r3 = `${e15}:${t3}`, a3 = this.globalSegmentSortKey(e15, t3), i3 = o2.get(r3);
8398
- (void 0 === i3 || a3 < i3) && o2.set(r3, a3);
8317
+ const a3 = `${e15}:${t3}`, i3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(i3 - r2), l2 = o2.get(a3);
8318
+ (void 0 === l2 || h3 < l2) && o2.set(a3, h3);
8399
8319
  };
8400
8320
  for (const d2 of e14) {
8401
8321
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -8627,13 +8547,13 @@ function ue(e14, t2) {
8627
8547
  }
8628
8548
  const ge = class extends BasePlugin {
8629
8549
  constructor(e14, s2) {
8630
- super(e14, s2), this.layoutChange$ = createBehaviorEmitter(void 0, ue), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.loadPushRaf = null, this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(ce.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
8550
+ super(e14, s2), this.layoutChange$ = createBehaviorEmitter(void 0, ue), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(ce.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
8631
8551
  }
8632
8552
  async initialize(e14) {
8633
8553
  this.config = { placeholderPageHeight: e14.placeholderPageHeight ?? 1200, placeholderPageWidth: e14.placeholderPageWidth ?? 900, bufferSize: e14.bufferSize ?? 2, pageGap: e14.pageGap ?? 20 }, this.virtualPageMap = this.chapterManager.provides().getVirtualPageMap(), this.rebuildOffsets(), this.chapterManagerUnsubs.push(this.chapterManager.provides().onManifestChange(({ map: e15 }) => {
8634
8554
  this.virtualPageMap = e15, this.pageSizes.clear(), this.lastLoadIndicesKey = "", this.rebuildOffsets(), this.syncReadyChapterSizes(), this.recomputeVisible();
8635
8555
  }), this.chapterManager.provides().onChapterStatusChange((e15) => {
8636
- "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads()));
8556
+ "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.pushVisibleLoads()));
8637
8557
  })), this.syncReadyChapterSizes();
8638
8558
  }
8639
8559
  isChapterDocumentReady(e14) {
@@ -8653,11 +8573,6 @@ const ge = class extends BasePlugin {
8653
8573
  refreshLayout() {
8654
8574
  this.layoutChange$.emit(this.computeLayout());
8655
8575
  }
8656
- schedulePushVisibleLoads() {
8657
- null == this.loadPushRaf && (this.loadPushRaf = requestAnimationFrame(() => {
8658
- this.loadPushRaf = null, this.pushVisibleLoads();
8659
- }));
8660
- }
8661
8576
  pushVisibleLoads() {
8662
8577
  if (!this.virtualPageMap || 0 === this.currentVisible.length) return;
8663
8578
  const e14 = this.expandIndicesWithBuffer(this.currentVisible, this.virtualPageMap.totalPages);
@@ -8676,7 +8591,7 @@ const ge = class extends BasePlugin {
8676
8591
  const { chapterId: s2 } = j(e14);
8677
8592
  if (0 === ((null == (t2 = this.virtualPageMap) ? void 0 : t2.pagesOfChapter(s2).length) ?? 0)) return;
8678
8593
  const n9 = this.documentManager.provides();
8679
- n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads();
8594
+ n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.pushVisibleLoads();
8680
8595
  }
8681
8596
  onDocumentClosed(e14) {
8682
8597
  var t2;
@@ -8690,7 +8605,7 @@ const ge = class extends BasePlugin {
8690
8605
  var e14;
8691
8606
  super.destroy();
8692
8607
  for (const t2 of this.chapterManagerUnsubs) t2();
8693
- this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, null != this.loadPushRaf && (cancelAnimationFrame(this.loadPushRaf), this.loadPushRaf = null), this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
8608
+ this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
8694
8609
  }
8695
8610
  getPageSize(e14) {
8696
8611
  return this.pageSizes.get(e14) ?? { width: this.config.placeholderPageWidth, height: this.config.placeholderPageHeight };
@@ -8799,7 +8714,7 @@ const ge = class extends BasePlugin {
8799
8714
  const e15 = this.offsets[h2], n10 = e15 + this.getPageSize(h2).height, r3 = Math.max(0, Math.min(n10, s2) - Math.max(e15, t2));
8800
8715
  r3 > i2 && (i2 = r3, o2 = h2);
8801
8716
  }
8802
- this.currentVisible = a2, this.currentGlobalPageIndex = o2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: o2 }), this.schedulePushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
8717
+ this.currentVisible = a2, this.currentGlobalPageIndex = o2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: o2 }), this.pushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
8803
8718
  }
8804
8719
  findFirstVisibleIndex(e14) {
8805
8720
  let t2 = 0, s2 = this.offsets.length - 1, n9 = 0;
@@ -8846,17 +8761,17 @@ const ge = class extends BasePlugin {
8846
8761
  }
8847
8762
  };
8848
8763
  ge.id = "chapter-scroll";
8849
- function Pe(e14) {
8764
+ function Ie(e14) {
8850
8765
  const [t2, s2] = e14.localPageRange;
8851
8766
  return s2 - t2 + 1;
8852
8767
  }
8853
- function Ie(e14) {
8768
+ function Pe(e14) {
8854
8769
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
8855
8770
  }
8856
8771
  function be(e14) {
8857
8772
  if (null != e14.segmentPageThreshold && e14.segmentPageThreshold > 0) return e14.segmentPageThreshold;
8858
8773
  const t2 = e14.source;
8859
- return Ie(t2) ? t2.segmentPageThreshold : null;
8774
+ return Pe(t2) ? t2.segmentPageThreshold : null;
8860
8775
  }
8861
8776
  function Se(e14, t2) {
8862
8777
  return `${e14}#s${t2}`;
@@ -8867,13 +8782,13 @@ function Ce(e14) {
8867
8782
  const s2 = e14.slice(0, t2), n9 = Number.parseInt(e14.slice(t2 + 2), 10);
8868
8783
  return !Number.isFinite(n9) || n9 < 0 ? { chapterId: e14, segmentIndex: null } : { chapterId: s2, segmentIndex: n9 };
8869
8784
  }
8870
- function ye(e14, t2) {
8785
+ function we(e14, t2) {
8871
8786
  return e14 <= 0 || t2 <= 0 ? 0 : Math.ceil(e14 / t2);
8872
8787
  }
8873
- function we(e14) {
8788
+ function ye(e14) {
8874
8789
  const t2 = be(e14);
8875
8790
  if (!t2) return null;
8876
- const s2 = Pe(e14), n9 = ye(s2, t2), r2 = Ie(e14.source) ? e14.source.urls : void 0, a2 = [];
8791
+ const s2 = Ie(e14), n9 = we(s2, t2), r2 = Pe(e14.source) ? e14.source.urls : void 0, a2 = [];
8877
8792
  for (let o2 = 0; o2 < n9; o2++) {
8878
8793
  const n10 = o2 * t2, i2 = Math.min(n10 + t2 - 1, s2 - 1);
8879
8794
  if (n10 >= s2) break;
@@ -8882,7 +8797,7 @@ function we(e14) {
8882
8797
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
8883
8798
  }
8884
8799
  function Me(e14, t2) {
8885
- const s2 = we(e14);
8800
+ const s2 = ye(e14);
8886
8801
  if (!s2) return { documentId: e14.chapterId, pageIndex: t2 };
8887
8802
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
8888
8803
  if (!n9) {
@@ -8892,7 +8807,7 @@ function Me(e14, t2) {
8892
8807
  return { documentId: n9.documentId, pageIndex: t2 - n9.localPageStart };
8893
8808
  }
8894
8809
  function xe(e14, t2) {
8895
- const s2 = we(e14);
8810
+ const s2 = ye(e14);
8896
8811
  if (!s2) return 0;
8897
8812
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
8898
8813
  return (null == n9 ? void 0 : n9.index) ?? Math.floor(t2 / s2.threshold);
@@ -8901,12 +8816,12 @@ function Le(e14) {
8901
8816
  var t2;
8902
8817
  const s2 = be(e14);
8903
8818
  if (!s2) return [];
8904
- const n9 = [], { chapterId: r2 } = e14, a2 = Pe(e14);
8819
+ const n9 = [], { chapterId: r2 } = e14, a2 = Ie(e14);
8905
8820
  s2 <= 0 && n9.push(`${r2}: segmentPageThreshold must be > 0`);
8906
8821
  const o2 = e14.source;
8907
- if (!Ie(o2)) return n9;
8822
+ if (!Pe(o2)) return n9;
8908
8823
  0 === o2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
8909
- const i2 = ye(a2, s2);
8824
+ const i2 = we(a2, s2);
8910
8825
  o2.urls.length !== i2 && n9.push(`${r2}: source.urls.length (${o2.urls.length}) must equal ceil(pageCount/threshold) (${i2})`);
8911
8826
  for (let h2 = 0; h2 < o2.urls.length; h2++) (null == (t2 = o2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
8912
8827
  return n9;
@@ -9016,7 +8931,7 @@ const Ee = class n3 extends BasePlugin {
9016
8931
  return s2 ? Me(s2, t2) : null;
9017
8932
  }, getSegmentPlan: (e14) => {
9018
8933
  const t2 = this.findChapter(e14);
9019
- return t2 ? we(t2) : null;
8934
+ return t2 ? ye(t2) : null;
9020
8935
  }, isSegmentLoaded: (e14, t2) => this.isSegmentDocumentOpen(e14, t2), isSegmentLoadPending: (e14, t2) => this.isSegmentLoadPending(e14, t2), getChapterErrorMessage: (e14) => this.chapterErrorMessages.get(e14), getChapterStatus: (e14) => this.chapterStatus.get(e14) ?? "idle", getChapter: (e14) => this.findChapter(e14), onChapterStatusChange: this.statusChange$.on, onManifestChange: this.manifestChange$.on };
9021
8936
  }
9022
8937
  destroy() {
@@ -9063,10 +8978,9 @@ const Ee = class n3 extends BasePlugin {
9063
8978
  if (e14 <= 0 || 0 === this.manifest.chapters.length) return;
9064
8979
  const t2 = Date.now(), s2 = Math.min(e14, this.manifest.chapters.length);
9065
8980
  for (let n9 = 0; n9 < s2; n9++) {
9066
- const e15 = this.manifest.chapters[n9].chapterId;
9067
- this.chapterLastUsed.set(e15, t2);
9068
- const s3 = this.chapterStatus.get(e15) ?? "idle";
9069
- "idle" !== s3 && "closed" !== s3 && "error" !== s3 || this.ensureChapterLoaded(e15);
8981
+ const e15 = this.manifest.chapters[n9];
8982
+ this.chapterLastUsed.set(e15.chapterId, t2), this.resolveChapterUrls(e15).catch(() => {
8983
+ });
9070
8984
  }
9071
8985
  }
9072
8986
  findChapter(e14) {
@@ -9074,7 +8988,7 @@ const Ee = class n3 extends BasePlugin {
9074
8988
  }
9075
8989
  getSegmentPlanForChapter(e14) {
9076
8990
  const t2 = this.findChapter(e14);
9077
- return t2 ? we(t2) : null;
8991
+ return t2 ? ye(t2) : null;
9078
8992
  }
9079
8993
  anySegmentOpen(e14) {
9080
8994
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -9098,11 +9012,7 @@ const Ee = class n3 extends BasePlugin {
9098
9012
  }
9099
9013
  isSegmentLoadPending(e14, t2) {
9100
9014
  const s2 = this.resolveSegmentDocumentId(e14, t2);
9101
- if (!s2) return false;
9102
- if (this.pendingSegmentLoadPromises.has(s2)) return true;
9103
- if (0 === t2 && this.pendingChapterLoadPromises.has(e14)) return true;
9104
- const n9 = this.coreState.core.documents[s2];
9105
- return "loading" === (null == n9 ? void 0 : n9.status);
9015
+ return !!s2 && (!!this.pendingSegmentLoadPromises.has(s2) || !(0 !== t2 || !this.pendingChapterLoadPromises.has(e14)));
9106
9016
  }
9107
9017
  syncChapterStatusFromDocuments(e14) {
9108
9018
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -9133,12 +9043,12 @@ const Ee = class n3 extends BasePlugin {
9133
9043
  clearVisibleSegmentBacklog() {
9134
9044
  this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
9135
9045
  }
9136
- globalSegmentSortKey(e14, t2) {
9046
+ segmentStartGlobalIndex(e14, t2) {
9137
9047
  var s2;
9138
9048
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
9139
9049
  const n9 = this.findChapter(e14);
9140
9050
  if (!n9) return Number.MAX_SAFE_INTEGER;
9141
- const r2 = we(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, o2 = this.virtualPageMap.toGlobal(e14, a2);
9051
+ const r2 = ye(n9), a2 = (null == (s2 = null == r2 ? void 0 : r2.segments[t2]) ? void 0 : s2.localPageStart) ?? 0, o2 = this.virtualPageMap.toGlobal(e14, a2);
9142
9052
  if (o2) return o2.globalPageIndex;
9143
9053
  return 1e4 * this.manifest.chapters.findIndex((t3) => t3.chapterId === e14) + t2;
9144
9054
  }
@@ -9146,12 +9056,12 @@ const Ee = class n3 extends BasePlugin {
9146
9056
  const t2 = [...e14].sort((e15, t3) => e15.priority - t3.priority), s2 = /* @__PURE__ */ new Set(), n9 = [];
9147
9057
  for (const r2 of t2) {
9148
9058
  const e15 = `${r2.chapterId}:${r2.segmentIndex}`;
9149
- s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || this.isSegmentLoadPending(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
9059
+ s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
9150
9060
  }
9151
9061
  this.visibleSegmentBacklog = n9, this.pumpVisibleSegmentBacklog();
9152
9062
  }
9153
9063
  pumpVisibleSegmentBacklog() {
9154
- const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
9064
+ const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
9155
9065
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
9156
9066
  const e15 = this.visibleSegmentBacklog.shift();
9157
9067
  this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
@@ -9171,10 +9081,10 @@ const Ee = class n3 extends BasePlugin {
9171
9081
  if (t3 < 0) return;
9172
9082
  const s3 = this.findChapter(e15);
9173
9083
  if (!s3) return;
9174
- const n10 = we(s3);
9084
+ const n10 = ye(s3);
9175
9085
  if (n10 && t3 >= n10.segments.length) return;
9176
- const r3 = `${e15}:${t3}`, a3 = this.globalSegmentSortKey(e15, t3), i3 = o2.get(r3);
9177
- (void 0 === i3 || a3 < i3) && o2.set(r3, a3);
9086
+ const a3 = `${e15}:${t3}`, i3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(i3 - r2), l2 = o2.get(a3);
9087
+ (void 0 === l2 || h3 < l2) && o2.set(a3, h3);
9178
9088
  };
9179
9089
  for (const d2 of e14) {
9180
9090
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -9239,7 +9149,7 @@ const Ee = class n3 extends BasePlugin {
9239
9149
  this.prepareChapterForSegmentLoad(e14);
9240
9150
  const s2 = this.findChapter(e14);
9241
9151
  if (!s2) return Promise.resolve("error");
9242
- const n9 = we(s2);
9152
+ const n9 = ye(s2);
9243
9153
  if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
9244
9154
  if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
9245
9155
  const r2 = n9.segments[t2], a2 = r2.documentId;
@@ -9269,7 +9179,7 @@ const Ee = class n3 extends BasePlugin {
9269
9179
  if (n9) return n9;
9270
9180
  const r2 = (async () => {
9271
9181
  const s3 = e14.source;
9272
- if (Ie(s3)) {
9182
+ if (Pe(s3)) {
9273
9183
  const e15 = [...s3.urls];
9274
9184
  return this.chapterUrlsCache.set(t2, e15), e15;
9275
9185
  }
@@ -9297,7 +9207,7 @@ const Ee = class n3 extends BasePlugin {
9297
9207
  async resolvePdfPayload(e14, t2) {
9298
9208
  const s2 = e14.source, r2 = this.config.chapterPdfLoader, a2 = be(e14);
9299
9209
  if (a2) {
9300
- const s3 = ye(Pe(e14), a2);
9210
+ const s3 = we(Ie(e14), a2);
9301
9211
  if (t2 < 0 || t2 >= s3) throw new Error(`Chapter "${e14.chapterId}" invalid segmentIndex ${t2} (expected 0..${s3 - 1})`);
9302
9212
  const r3 = await this.resolveChapterUrls(e14);
9303
9213
  if (r3.length !== s3) {
@@ -9308,7 +9218,7 @@ const Ee = class n3 extends BasePlugin {
9308
9218
  }
9309
9219
  if (0 !== t2) throw new Error(`Chapter "${e14.chapterId}" is not segmented`);
9310
9220
  if (s2) {
9311
- if (Ie(s2)) {
9221
+ if (Pe(s2)) {
9312
9222
  const s3 = await this.resolveChapterUrls(e14);
9313
9223
  return this.openPayloadFromUrl(e14, t2, s3);
9314
9224
  }
@@ -9377,7 +9287,7 @@ const Ee = class n3 extends BasePlugin {
9377
9287
  });
9378
9288
  }
9379
9289
  closeChapter(e14) {
9380
- const t2 = this.findChapter(e14), s2 = this.documentManager.provides(), n9 = t2 ? we(t2) : null, r2 = (e15) => {
9290
+ const t2 = this.findChapter(e14), s2 = this.documentManager.provides(), n9 = t2 ? ye(t2) : null, r2 = (e15) => {
9381
9291
  if (s2.isDocumentOpen(e15)) {
9382
9292
  s2.closeDocument(e15).wait(() => {
9383
9293
  }, () => {
@@ -9395,10 +9305,10 @@ const Ee = class n3 extends BasePlugin {
9395
9305
  };
9396
9306
  Ee.id = "chapter-manager";
9397
9307
  let Te = Ee;
9398
- function Fe2(e14) {
9308
+ function Fe(e14) {
9399
9309
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
9400
9310
  }
9401
- function Ne(e14, t2) {
9311
+ function Ne2(e14, t2) {
9402
9312
  return `${e14}#s${t2}`;
9403
9313
  }
9404
9314
  function Ae(e14) {
@@ -9407,11 +9317,11 @@ function Ae(e14) {
9407
9317
  const s2 = e14.slice(0, t2), n9 = Number.parseInt(e14.slice(t2 + 2), 10);
9408
9318
  return !Number.isFinite(n9) || n9 < 0 ? { chapterId: e14, segmentIndex: null } : { chapterId: s2, segmentIndex: n9 };
9409
9319
  }
9410
- function Ve(e14) {
9320
+ function Oe(e14) {
9411
9321
  const t2 = (function(e15) {
9412
9322
  if (null != e15.segmentPageThreshold && e15.segmentPageThreshold > 0) return e15.segmentPageThreshold;
9413
9323
  const t3 = e15.source;
9414
- return Fe2(t3) ? t3.segmentPageThreshold : null;
9324
+ return Fe(t3) ? t3.segmentPageThreshold : null;
9415
9325
  })(e14);
9416
9326
  if (!t2) return null;
9417
9327
  const s2 = (function(e15) {
@@ -9419,25 +9329,25 @@ function Ve(e14) {
9419
9329
  return s3 - t3 + 1;
9420
9330
  })(e14), n9 = (function(e15, t3) {
9421
9331
  return e15 <= 0 || t3 <= 0 ? 0 : Math.ceil(e15 / t3);
9422
- })(s2, t2), r2 = Fe2(e14.source) ? e14.source.urls : void 0, a2 = [];
9332
+ })(s2, t2), r2 = Fe(e14.source) ? e14.source.urls : void 0, a2 = [];
9423
9333
  for (let o2 = 0; o2 < n9; o2++) {
9424
9334
  const n10 = o2 * t2, i2 = Math.min(n10 + t2 - 1, s2 - 1);
9425
9335
  if (n10 >= s2) break;
9426
- a2.push({ index: o2, url: (null == r2 ? void 0 : r2[o2]) ?? "", localPageStart: n10, localPageEnd: i2, documentId: Ne(e14.chapterId, o2) });
9336
+ a2.push({ index: o2, url: (null == r2 ? void 0 : r2[o2]) ?? "", localPageStart: n10, localPageEnd: i2, documentId: Ne2(e14.chapterId, o2) });
9427
9337
  }
9428
9338
  return { chapterId: e14.chapterId, threshold: t2, pageCount: s2, segments: a2 };
9429
9339
  }
9430
- function Oe(e14) {
9340
+ function Ve(e14) {
9431
9341
  const [t2, s2] = e14.localPageRange;
9432
9342
  return s2 - t2 + 1;
9433
9343
  }
9434
- function Re(e14) {
9344
+ function Ge(e14) {
9435
9345
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
9436
9346
  }
9437
- function Ge(e14) {
9347
+ function Re(e14) {
9438
9348
  if (null != e14.segmentPageThreshold && e14.segmentPageThreshold > 0) return e14.segmentPageThreshold;
9439
9349
  const t2 = e14.source;
9440
- return Re(t2) ? t2.segmentPageThreshold : null;
9350
+ return Ge(t2) ? t2.segmentPageThreshold : null;
9441
9351
  }
9442
9352
  function ze(e14, t2) {
9443
9353
  return `${e14}#s${t2}`;
@@ -9452,9 +9362,9 @@ function _e(e14, t2) {
9452
9362
  return e14 <= 0 || t2 <= 0 ? 0 : Math.ceil(e14 / t2);
9453
9363
  }
9454
9364
  function qe(e14) {
9455
- const t2 = Ge(e14);
9365
+ const t2 = Re(e14);
9456
9366
  if (!t2) return null;
9457
- const s2 = Oe(e14), n9 = _e(s2, t2), r2 = Re(e14.source) ? e14.source.urls : void 0, a2 = [];
9367
+ const s2 = Ve(e14), n9 = _e(s2, t2), r2 = Ge(e14.source) ? e14.source.urls : void 0, a2 = [];
9458
9368
  for (let o2 = 0; o2 < n9; o2++) {
9459
9369
  const n10 = o2 * t2, i2 = Math.min(n10 + t2 - 1, s2 - 1);
9460
9370
  if (n10 >= s2) break;
@@ -9480,12 +9390,12 @@ function Ke(e14, t2) {
9480
9390
  }
9481
9391
  function He(e14) {
9482
9392
  var t2;
9483
- const s2 = Ge(e14);
9393
+ const s2 = Re(e14);
9484
9394
  if (!s2) return [];
9485
- const n9 = [], { chapterId: r2 } = e14, a2 = Oe(e14);
9395
+ const n9 = [], { chapterId: r2 } = e14, a2 = Ve(e14);
9486
9396
  s2 <= 0 && n9.push(`${r2}: segmentPageThreshold must be > 0`);
9487
9397
  const o2 = e14.source;
9488
- if (!Re(o2)) return n9;
9398
+ if (!Ge(o2)) return n9;
9489
9399
  0 === o2.urls.length && n9.push(`${r2}: source.urls must not be empty`);
9490
9400
  const i2 = _e(a2, s2);
9491
9401
  o2.urls.length !== i2 && n9.push(`${r2}: source.urls.length (${o2.urls.length}) must equal ceil(pageCount/threshold) (${i2})`);
@@ -9644,10 +9554,9 @@ const Ye = class n4 extends BasePlugin {
9644
9554
  if (e14 <= 0 || 0 === this.manifest.chapters.length) return;
9645
9555
  const t2 = Date.now(), s2 = Math.min(e14, this.manifest.chapters.length);
9646
9556
  for (let n9 = 0; n9 < s2; n9++) {
9647
- const e15 = this.manifest.chapters[n9].chapterId;
9648
- this.chapterLastUsed.set(e15, t2);
9649
- const s3 = this.chapterStatus.get(e15) ?? "idle";
9650
- "idle" !== s3 && "closed" !== s3 && "error" !== s3 || this.ensureChapterLoaded(e15);
9557
+ const e15 = this.manifest.chapters[n9];
9558
+ this.chapterLastUsed.set(e15.chapterId, t2), this.resolveChapterUrls(e15).catch(() => {
9559
+ });
9651
9560
  }
9652
9561
  }
9653
9562
  findChapter(e14) {
@@ -9679,11 +9588,7 @@ const Ye = class n4 extends BasePlugin {
9679
9588
  }
9680
9589
  isSegmentLoadPending(e14, t2) {
9681
9590
  const s2 = this.resolveSegmentDocumentId(e14, t2);
9682
- if (!s2) return false;
9683
- if (this.pendingSegmentLoadPromises.has(s2)) return true;
9684
- if (0 === t2 && this.pendingChapterLoadPromises.has(e14)) return true;
9685
- const n9 = this.coreState.core.documents[s2];
9686
- return "loading" === (null == n9 ? void 0 : n9.status);
9591
+ return !!s2 && (!!this.pendingSegmentLoadPromises.has(s2) || !(0 !== t2 || !this.pendingChapterLoadPromises.has(e14)));
9687
9592
  }
9688
9593
  syncChapterStatusFromDocuments(e14) {
9689
9594
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -9714,7 +9619,7 @@ const Ye = class n4 extends BasePlugin {
9714
9619
  clearVisibleSegmentBacklog() {
9715
9620
  this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
9716
9621
  }
9717
- globalSegmentSortKey(e14, t2) {
9622
+ segmentStartGlobalIndex(e14, t2) {
9718
9623
  var s2;
9719
9624
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
9720
9625
  const n9 = this.findChapter(e14);
@@ -9727,12 +9632,12 @@ const Ye = class n4 extends BasePlugin {
9727
9632
  const t2 = [...e14].sort((e15, t3) => e15.priority - t3.priority), s2 = /* @__PURE__ */ new Set(), n9 = [];
9728
9633
  for (const r2 of t2) {
9729
9634
  const e15 = `${r2.chapterId}:${r2.segmentIndex}`;
9730
- s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || this.isSegmentLoadPending(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
9635
+ s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
9731
9636
  }
9732
9637
  this.visibleSegmentBacklog = n9, this.pumpVisibleSegmentBacklog();
9733
9638
  }
9734
9639
  pumpVisibleSegmentBacklog() {
9735
- const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
9640
+ const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
9736
9641
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
9737
9642
  const e15 = this.visibleSegmentBacklog.shift();
9738
9643
  this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
@@ -9754,8 +9659,8 @@ const Ye = class n4 extends BasePlugin {
9754
9659
  if (!s3) return;
9755
9660
  const n10 = qe(s3);
9756
9661
  if (n10 && t3 >= n10.segments.length) return;
9757
- const r3 = `${e15}:${t3}`, a3 = this.globalSegmentSortKey(e15, t3), i3 = o2.get(r3);
9758
- (void 0 === i3 || a3 < i3) && o2.set(r3, a3);
9662
+ const a3 = `${e15}:${t3}`, i3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(i3 - r2), l2 = o2.get(a3);
9663
+ (void 0 === l2 || h3 < l2) && o2.set(a3, h3);
9759
9664
  };
9760
9665
  for (const d2 of e14) {
9761
9666
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -9850,7 +9755,7 @@ const Ye = class n4 extends BasePlugin {
9850
9755
  if (n9) return n9;
9851
9756
  const r2 = (async () => {
9852
9757
  const s3 = e14.source;
9853
- if (Re(s3)) {
9758
+ if (Ge(s3)) {
9854
9759
  const e15 = [...s3.urls];
9855
9760
  return this.chapterUrlsCache.set(t2, e15), e15;
9856
9761
  }
@@ -9876,9 +9781,9 @@ const Ye = class n4 extends BasePlugin {
9876
9781
  return (null == r2 ? void 0 : r2.openPdf) ? r2.openPdf(a2) : { url: n9 };
9877
9782
  }
9878
9783
  async resolvePdfPayload(e14, t2) {
9879
- const s2 = e14.source, r2 = this.config.chapterPdfLoader, a2 = Ge(e14);
9784
+ const s2 = e14.source, r2 = this.config.chapterPdfLoader, a2 = Re(e14);
9880
9785
  if (a2) {
9881
- const s3 = _e(Oe(e14), a2);
9786
+ const s3 = _e(Ve(e14), a2);
9882
9787
  if (t2 < 0 || t2 >= s3) throw new Error(`Chapter "${e14.chapterId}" invalid segmentIndex ${t2} (expected 0..${s3 - 1})`);
9883
9788
  const r3 = await this.resolveChapterUrls(e14);
9884
9789
  if (r3.length !== s3) {
@@ -9889,7 +9794,7 @@ const Ye = class n4 extends BasePlugin {
9889
9794
  }
9890
9795
  if (0 !== t2) throw new Error(`Chapter "${e14.chapterId}" is not segmented`);
9891
9796
  if (s2) {
9892
- if (Re(s2)) {
9797
+ if (Ge(s2)) {
9893
9798
  const s3 = await this.resolveChapterUrls(e14);
9894
9799
  return this.openPayloadFromUrl(e14, t2, s3);
9895
9800
  }
@@ -9987,13 +9892,13 @@ function et2(e14, t2) {
9987
9892
  }
9988
9893
  const tt = class extends BasePlugin {
9989
9894
  constructor(e14, s2) {
9990
- super(e14, s2), this.layoutChange$ = createBehaviorEmitter(void 0, et2), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.loadPushRaf = null, this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(Ze.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
9895
+ super(e14, s2), this.layoutChange$ = createBehaviorEmitter(void 0, et2), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(Ze.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
9991
9896
  }
9992
9897
  async initialize(e14) {
9993
9898
  this.config = { placeholderPageHeight: e14.placeholderPageHeight ?? 1200, placeholderPageWidth: e14.placeholderPageWidth ?? 900, bufferSize: e14.bufferSize ?? 2, pageGap: e14.pageGap ?? 20 }, this.virtualPageMap = this.chapterManager.provides().getVirtualPageMap(), this.rebuildOffsets(), this.chapterManagerUnsubs.push(this.chapterManager.provides().onManifestChange(({ map: e15 }) => {
9994
9899
  this.virtualPageMap = e15, this.pageSizes.clear(), this.lastLoadIndicesKey = "", this.rebuildOffsets(), this.syncReadyChapterSizes(), this.recomputeVisible();
9995
9900
  }), this.chapterManager.provides().onChapterStatusChange((e15) => {
9996
- "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads()));
9901
+ "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.pushVisibleLoads()));
9997
9902
  })), this.syncReadyChapterSizes();
9998
9903
  }
9999
9904
  isChapterDocumentReady(e14) {
@@ -10003,7 +9908,7 @@ const tt = class extends BasePlugin {
10003
9908
  const s2 = this.chapterManager.provides(), n9 = s2.getChapter(e14);
10004
9909
  if (!n9) return true;
10005
9910
  const r2 = (function(e15, t3) {
10006
- const s3 = Ve(e15);
9911
+ const s3 = Oe(e15);
10007
9912
  if (!s3) return 0;
10008
9913
  const n10 = s3.segments.find((e16) => t3 >= e16.localPageStart && t3 <= e16.localPageEnd);
10009
9914
  return (null == n10 ? void 0 : n10.index) ?? Math.floor(t3 / s3.threshold);
@@ -10013,11 +9918,6 @@ const tt = class extends BasePlugin {
10013
9918
  refreshLayout() {
10014
9919
  this.layoutChange$.emit(this.computeLayout());
10015
9920
  }
10016
- schedulePushVisibleLoads() {
10017
- null == this.loadPushRaf && (this.loadPushRaf = requestAnimationFrame(() => {
10018
- this.loadPushRaf = null, this.pushVisibleLoads();
10019
- }));
10020
- }
10021
9921
  pushVisibleLoads() {
10022
9922
  if (!this.virtualPageMap || 0 === this.currentVisible.length) return;
10023
9923
  const e14 = this.expandIndicesWithBuffer(this.currentVisible, this.virtualPageMap.totalPages);
@@ -10036,7 +9936,7 @@ const tt = class extends BasePlugin {
10036
9936
  const { chapterId: s2 } = Ae(e14);
10037
9937
  if (0 === ((null == (t2 = this.virtualPageMap) ? void 0 : t2.pagesOfChapter(s2).length) ?? 0)) return;
10038
9938
  const n9 = this.documentManager.provides();
10039
- n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads();
9939
+ n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.pushVisibleLoads();
10040
9940
  }
10041
9941
  onDocumentClosed(e14) {
10042
9942
  var t2;
@@ -10050,7 +9950,7 @@ const tt = class extends BasePlugin {
10050
9950
  var e14;
10051
9951
  super.destroy();
10052
9952
  for (const t2 of this.chapterManagerUnsubs) t2();
10053
- this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, null != this.loadPushRaf && (cancelAnimationFrame(this.loadPushRaf), this.loadPushRaf = null), this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
9953
+ this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
10054
9954
  }
10055
9955
  getPageSize(e14) {
10056
9956
  return this.pageSizes.get(e14) ?? { width: this.config.placeholderPageWidth, height: this.config.placeholderPageHeight };
@@ -10092,7 +9992,7 @@ const tt = class extends BasePlugin {
10092
9992
  if (!this.virtualPageMap) return;
10093
9993
  const s2 = this.chapterManager.provides(), n9 = s2.getChapter(e14), r2 = this.virtualPageMap.pagesOfChapter(e14);
10094
9994
  if (!n9 || 0 === r2.length) return;
10095
- const a2 = Ve(n9), o2 = this.documentManager.provides(), i2 = [], h2 = (e15, t3, s3, n10) => {
9995
+ const a2 = Oe(n9), o2 = this.documentManager.provides(), i2 = [], h2 = (e15, t3, s3, n10) => {
10096
9996
  var r3;
10097
9997
  const a3 = o2.getDocument(e15);
10098
9998
  if (!(null == (r3 = null == a3 ? void 0 : a3.pages) ? void 0 : r3.length)) return false;
@@ -10159,7 +10059,7 @@ const tt = class extends BasePlugin {
10159
10059
  const e15 = this.offsets[h2], n10 = e15 + this.getPageSize(h2).height, r3 = Math.max(0, Math.min(n10, s2) - Math.max(e15, t2));
10160
10060
  r3 > i2 && (i2 = r3, o2 = h2);
10161
10061
  }
10162
- this.currentVisible = a2, this.currentGlobalPageIndex = o2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: o2 }), this.schedulePushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
10062
+ this.currentVisible = a2, this.currentGlobalPageIndex = o2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: o2 }), this.pushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
10163
10063
  }
10164
10064
  findFirstVisibleIndex(e14) {
10165
10065
  let t2 = 0, s2 = this.offsets.length - 1, n9 = 0;
@@ -10372,7 +10272,7 @@ function vt(e14, t2) {
10372
10272
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
10373
10273
  return (null == n9 ? void 0 : n9.index) ?? Math.floor(t2 / s2.threshold);
10374
10274
  }
10375
- function Pt(e14) {
10275
+ function It(e14) {
10376
10276
  var t2;
10377
10277
  const s2 = ct(e14);
10378
10278
  if (!s2) return [];
@@ -10386,7 +10286,7 @@ function Pt(e14) {
10386
10286
  for (let h2 = 0; h2 < o2.urls.length; h2++) (null == (t2 = o2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
10387
10287
  return n9;
10388
10288
  }
10389
- const It = { kind: "first-wins" };
10289
+ const Pt = { kind: "first-wins" };
10390
10290
  function bt(e14, t2, s2) {
10391
10291
  if (1 === t2.length) return t2[0].chapterId;
10392
10292
  switch (s2.kind) {
@@ -10439,8 +10339,8 @@ let St = class {
10439
10339
  return s2 ? this._pages.indexOf(s2) : -1;
10440
10340
  }
10441
10341
  };
10442
- function Ct(e14, t2 = It) {
10443
- const s2 = (function(e15, t3 = It) {
10342
+ function Ct(e14, t2 = Pt) {
10343
+ const s2 = (function(e15, t3 = Pt) {
10444
10344
  const s3 = /* @__PURE__ */ new Map();
10445
10345
  for (const r3 of e15.chapters) {
10446
10346
  const [e16, t4] = r3.globalPageRange;
@@ -10464,12 +10364,12 @@ function Ct(e14, t2 = It) {
10464
10364
  }
10465
10365
  return new St(a2);
10466
10366
  }
10467
- const yt2 = class n6 extends BasePlugin {
10367
+ const wt = class n6 extends BasePlugin {
10468
10368
  constructor(e14, n9) {
10469
- super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = It, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
10369
+ super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Pt, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
10470
10370
  }
10471
10371
  async initialize(e14) {
10472
- if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? It, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
10372
+ if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Pt, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
10473
10373
  const { chapterId: t2 } = gt(e15.id);
10474
10374
  this.findChapter(t2) && (this.passwordAttempts.delete(t2), this.syncChapterStatusFromDocuments(t2));
10475
10375
  }), this.documentManager.provides().onDocumentClosed((e15) => {
@@ -10523,7 +10423,7 @@ const yt2 = class n6 extends BasePlugin {
10523
10423
  s3.has(n9.chapterId) && t3.push(`Duplicate chapterId: ${n9.chapterId}`), s3.add(n9.chapterId);
10524
10424
  const [e16, r3] = n9.globalPageRange, [a3, o2] = n9.localPageRange;
10525
10425
  if (e16 > r3 && t3.push(`${n9.chapterId}: invalid globalPageRange [${e16},${r3}]`), a3 > o2 && t3.push(`${n9.chapterId}: invalid localPageRange [${a3},${o2}]`), r3 - e16 !== o2 - a3 && t3.push(`${n9.chapterId}: page-count mismatch (global ${r3 - e16 + 1} vs local ${o2 - a3 + 1})`), n9.ownedGlobalPages) for (const s4 of n9.ownedGlobalPages) (s4 < e16 || s4 > r3) && t3.push(`${n9.chapterId}: ownedGlobalPages contains ${s4} outside range`);
10526
- t3.push(...Pt(n9));
10426
+ t3.push(...It(n9));
10527
10427
  }
10528
10428
  return t3;
10529
10429
  })(t2);
@@ -10538,10 +10438,9 @@ const yt2 = class n6 extends BasePlugin {
10538
10438
  if (e14 <= 0 || 0 === this.manifest.chapters.length) return;
10539
10439
  const t2 = Date.now(), s2 = Math.min(e14, this.manifest.chapters.length);
10540
10440
  for (let n9 = 0; n9 < s2; n9++) {
10541
- const e15 = this.manifest.chapters[n9].chapterId;
10542
- this.chapterLastUsed.set(e15, t2);
10543
- const s3 = this.chapterStatus.get(e15) ?? "idle";
10544
- "idle" !== s3 && "closed" !== s3 && "error" !== s3 || this.ensureChapterLoaded(e15);
10441
+ const e15 = this.manifest.chapters[n9];
10442
+ this.chapterLastUsed.set(e15.chapterId, t2), this.resolveChapterUrls(e15).catch(() => {
10443
+ });
10545
10444
  }
10546
10445
  }
10547
10446
  findChapter(e14) {
@@ -10573,11 +10472,7 @@ const yt2 = class n6 extends BasePlugin {
10573
10472
  }
10574
10473
  isSegmentLoadPending(e14, t2) {
10575
10474
  const s2 = this.resolveSegmentDocumentId(e14, t2);
10576
- if (!s2) return false;
10577
- if (this.pendingSegmentLoadPromises.has(s2)) return true;
10578
- if (0 === t2 && this.pendingChapterLoadPromises.has(e14)) return true;
10579
- const n9 = this.coreState.core.documents[s2];
10580
- return "loading" === (null == n9 ? void 0 : n9.status);
10475
+ return !!s2 && (!!this.pendingSegmentLoadPromises.has(s2) || !(0 !== t2 || !this.pendingChapterLoadPromises.has(e14)));
10581
10476
  }
10582
10477
  syncChapterStatusFromDocuments(e14) {
10583
10478
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -10608,7 +10503,7 @@ const yt2 = class n6 extends BasePlugin {
10608
10503
  clearVisibleSegmentBacklog() {
10609
10504
  this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
10610
10505
  }
10611
- globalSegmentSortKey(e14, t2) {
10506
+ segmentStartGlobalIndex(e14, t2) {
10612
10507
  var s2;
10613
10508
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
10614
10509
  const n9 = this.findChapter(e14);
@@ -10621,12 +10516,12 @@ const yt2 = class n6 extends BasePlugin {
10621
10516
  const t2 = [...e14].sort((e15, t3) => e15.priority - t3.priority), s2 = /* @__PURE__ */ new Set(), n9 = [];
10622
10517
  for (const r2 of t2) {
10623
10518
  const e15 = `${r2.chapterId}:${r2.segmentIndex}`;
10624
- s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || this.isSegmentLoadPending(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
10519
+ s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
10625
10520
  }
10626
10521
  this.visibleSegmentBacklog = n9, this.pumpVisibleSegmentBacklog();
10627
10522
  }
10628
10523
  pumpVisibleSegmentBacklog() {
10629
- const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
10524
+ const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
10630
10525
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
10631
10526
  const e15 = this.visibleSegmentBacklog.shift();
10632
10527
  this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
@@ -10648,8 +10543,8 @@ const yt2 = class n6 extends BasePlugin {
10648
10543
  if (!s3) return;
10649
10544
  const n10 = mt(s3);
10650
10545
  if (n10 && t3 >= n10.segments.length) return;
10651
- const r3 = `${e15}:${t3}`, a3 = this.globalSegmentSortKey(e15, t3), i3 = o2.get(r3);
10652
- (void 0 === i3 || a3 < i3) && o2.set(r3, a3);
10546
+ const a3 = `${e15}:${t3}`, i3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(i3 - r2), l2 = o2.get(a3);
10547
+ (void 0 === l2 || h3 < l2) && o2.set(a3, h3);
10653
10548
  };
10654
10549
  for (const d2 of e14) {
10655
10550
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -10868,8 +10763,8 @@ const yt2 = class n6 extends BasePlugin {
10868
10763
  "error" === t2 && s2 ? this.chapterErrorMessages.set(e14, s2) : "loading" !== t2 && "loaded" !== t2 || this.chapterErrorMessages.delete(e14), n9 !== t2 && (this.chapterStatus.set(e14, t2), this.statusChange$.emit({ chapterId: e14, status: t2, ..."error" === t2 && s2 ? { error: { message: s2 } } : {} }));
10869
10764
  }
10870
10765
  };
10871
- yt2.id = "chapter-manager";
10872
- let wt = yt2;
10766
+ wt.id = "chapter-manager";
10767
+ let yt2 = wt;
10873
10768
  function Mt(e14) {
10874
10769
  return !!e14 && "urls" in e14 && Array.isArray(e14.urls);
10875
10770
  }
@@ -10947,13 +10842,13 @@ function At(e14, t2) {
10947
10842
  }
10948
10843
  return { documentId: n9.documentId, pageIndex: t2 - n9.localPageStart };
10949
10844
  }
10950
- function Vt(e14, t2) {
10845
+ function Ot(e14, t2) {
10951
10846
  const s2 = Nt(e14);
10952
10847
  if (!s2) return 0;
10953
10848
  const n9 = s2.segments.find((e15) => t2 >= e15.localPageStart && t2 <= e15.localPageEnd);
10954
10849
  return (null == n9 ? void 0 : n9.index) ?? Math.floor(t2 / s2.threshold);
10955
10850
  }
10956
- function Ot(e14) {
10851
+ function Vt(e14) {
10957
10852
  var t2;
10958
10853
  const s2 = Ut(e14);
10959
10854
  if (!s2) return [];
@@ -10967,8 +10862,8 @@ function Ot(e14) {
10967
10862
  for (let h2 = 0; h2 < o2.urls.length; h2++) (null == (t2 = o2.urls[h2]) ? void 0 : t2.trim()) || n9.push(`${r2}: source.urls[${h2}] is empty`);
10968
10863
  return n9;
10969
10864
  }
10970
- const Rt = { kind: "first-wins" };
10971
- function Gt(e14, t2, s2) {
10865
+ const Gt = { kind: "first-wins" };
10866
+ function Rt(e14, t2, s2) {
10972
10867
  if (1 === t2.length) return t2[0].chapterId;
10973
10868
  switch (s2.kind) {
10974
10869
  case "first-wins":
@@ -11020,8 +10915,8 @@ class zt {
11020
10915
  return s2 ? this._pages.indexOf(s2) : -1;
11021
10916
  }
11022
10917
  }
11023
- function Bt(e14, t2 = Rt) {
11024
- const s2 = (function(e15, t3 = Rt) {
10918
+ function Bt(e14, t2 = Gt) {
10919
+ const s2 = (function(e15, t3 = Gt) {
11025
10920
  const s3 = /* @__PURE__ */ new Map();
11026
10921
  for (const r3 of e15.chapters) {
11027
10922
  const [e16, t4] = r3.globalPageRange;
@@ -11031,7 +10926,7 @@ function Bt(e14, t2 = Rt) {
11031
10926
  }
11032
10927
  }
11033
10928
  const n10 = /* @__PURE__ */ new Map();
11034
- for (const [r3, a3] of s3) n10.set(r3, Gt(r3, a3, t3));
10929
+ for (const [r3, a3] of s3) n10.set(r3, Rt(r3, a3, t3));
11035
10930
  return n10;
11036
10931
  })(e14, t2), n9 = /* @__PURE__ */ new Map();
11037
10932
  for (const i2 of e14.chapters) n9.set(i2.chapterId, i2);
@@ -11047,10 +10942,10 @@ function Bt(e14, t2 = Rt) {
11047
10942
  }
11048
10943
  const _t = class n7 extends BasePlugin {
11049
10944
  constructor(e14, n9) {
11050
- super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Rt, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
10945
+ super(e14, n9), this.statusChange$ = createBehaviorEmitter(), this.manifestChange$ = createEmitter(), this.overlapStrategy = Gt, this.chapterStatus = /* @__PURE__ */ new Map(), this.chapterErrorMessages = /* @__PURE__ */ new Map(), this.chapterLastUsed = /* @__PURE__ */ new Map(), this.passwordAttempts = /* @__PURE__ */ new Map(), this.pendingChapterLoadPromises = /* @__PURE__ */ new Map(), this.pendingSegmentLoadPromises = /* @__PURE__ */ new Map(), this.chapterUrlsCache = /* @__PURE__ */ new Map(), this.pendingChapterUrlsPromises = /* @__PURE__ */ new Map(), this.unloadTimer = null, this.documentManagerUnsubs = [], this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0, this.lastVisibleIndicesKey = "", this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
11051
10946
  }
11052
10947
  async initialize(e14) {
11053
- if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Rt, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
10948
+ if (this.config = e14, this.passwordProvider = e14.passwordProvider, this.overlapStrategy = e14.overlapStrategy ?? Gt, this.documentManagerUnsubs.push(this.documentManager.provides().onDocumentOpened((e15) => {
11054
10949
  const { chapterId: t2 } = Tt(e15.id);
11055
10950
  this.findChapter(t2) && (this.passwordAttempts.delete(t2), this.syncChapterStatusFromDocuments(t2));
11056
10951
  }), this.documentManager.provides().onDocumentClosed((e15) => {
@@ -11104,7 +10999,7 @@ const _t = class n7 extends BasePlugin {
11104
10999
  s3.has(n9.chapterId) && t3.push(`Duplicate chapterId: ${n9.chapterId}`), s3.add(n9.chapterId);
11105
11000
  const [e16, r3] = n9.globalPageRange, [a3, o2] = n9.localPageRange;
11106
11001
  if (e16 > r3 && t3.push(`${n9.chapterId}: invalid globalPageRange [${e16},${r3}]`), a3 > o2 && t3.push(`${n9.chapterId}: invalid localPageRange [${a3},${o2}]`), r3 - e16 !== o2 - a3 && t3.push(`${n9.chapterId}: page-count mismatch (global ${r3 - e16 + 1} vs local ${o2 - a3 + 1})`), n9.ownedGlobalPages) for (const s4 of n9.ownedGlobalPages) (s4 < e16 || s4 > r3) && t3.push(`${n9.chapterId}: ownedGlobalPages contains ${s4} outside range`);
11107
- t3.push(...Ot(n9));
11002
+ t3.push(...Vt(n9));
11108
11003
  }
11109
11004
  return t3;
11110
11005
  })(t2);
@@ -11119,10 +11014,9 @@ const _t = class n7 extends BasePlugin {
11119
11014
  if (e14 <= 0 || 0 === this.manifest.chapters.length) return;
11120
11015
  const t2 = Date.now(), s2 = Math.min(e14, this.manifest.chapters.length);
11121
11016
  for (let n9 = 0; n9 < s2; n9++) {
11122
- const e15 = this.manifest.chapters[n9].chapterId;
11123
- this.chapterLastUsed.set(e15, t2);
11124
- const s3 = this.chapterStatus.get(e15) ?? "idle";
11125
- "idle" !== s3 && "closed" !== s3 && "error" !== s3 || this.ensureChapterLoaded(e15);
11017
+ const e15 = this.manifest.chapters[n9];
11018
+ this.chapterLastUsed.set(e15.chapterId, t2), this.resolveChapterUrls(e15).catch(() => {
11019
+ });
11126
11020
  }
11127
11021
  }
11128
11022
  findChapter(e14) {
@@ -11154,11 +11048,7 @@ const _t = class n7 extends BasePlugin {
11154
11048
  }
11155
11049
  isSegmentLoadPending(e14, t2) {
11156
11050
  const s2 = this.resolveSegmentDocumentId(e14, t2);
11157
- if (!s2) return false;
11158
- if (this.pendingSegmentLoadPromises.has(s2)) return true;
11159
- if (0 === t2 && this.pendingChapterLoadPromises.has(e14)) return true;
11160
- const n9 = this.coreState.core.documents[s2];
11161
- return "loading" === (null == n9 ? void 0 : n9.status);
11051
+ return !!s2 && (!!this.pendingSegmentLoadPromises.has(s2) || !(0 !== t2 || !this.pendingChapterLoadPromises.has(e14)));
11162
11052
  }
11163
11053
  syncChapterStatusFromDocuments(e14) {
11164
11054
  const t2 = this.getSegmentPlanForChapter(e14), s2 = this.documentManager.provides();
@@ -11178,7 +11068,7 @@ const _t = class n7 extends BasePlugin {
11178
11068
  if (!e15) continue;
11179
11069
  const s2 = this.findChapter(e15.chapterId);
11180
11070
  if (!s2) continue;
11181
- const n9 = Vt(s2, e15.localPageIndex);
11071
+ const n9 = Ot(s2, e15.localPageIndex);
11182
11072
  if (!this.isSegmentDocumentOpen(e15.chapterId, n9)) return true;
11183
11073
  }
11184
11074
  return false;
@@ -11189,7 +11079,7 @@ const _t = class n7 extends BasePlugin {
11189
11079
  clearVisibleSegmentBacklog() {
11190
11080
  this.visibleSegmentBacklog = [], this.activeVisibleSegmentLoads = 0;
11191
11081
  }
11192
- globalSegmentSortKey(e14, t2) {
11082
+ segmentStartGlobalIndex(e14, t2) {
11193
11083
  var s2;
11194
11084
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
11195
11085
  const n9 = this.findChapter(e14);
@@ -11202,12 +11092,12 @@ const _t = class n7 extends BasePlugin {
11202
11092
  const t2 = [...e14].sort((e15, t3) => e15.priority - t3.priority), s2 = /* @__PURE__ */ new Set(), n9 = [];
11203
11093
  for (const r2 of t2) {
11204
11094
  const e15 = `${r2.chapterId}:${r2.segmentIndex}`;
11205
- s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || this.isSegmentLoadPending(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
11095
+ s2.has(e15) || (s2.add(e15), this.isSegmentDocumentOpen(r2.chapterId, r2.segmentIndex) || n9.push({ chapterId: r2.chapterId, segmentIndex: r2.segmentIndex }));
11206
11096
  }
11207
11097
  this.visibleSegmentBacklog = n9, this.pumpVisibleSegmentBacklog();
11208
11098
  }
11209
11099
  pumpVisibleSegmentBacklog() {
11210
- const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
11100
+ const e14 = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
11211
11101
  for (; this.activeVisibleSegmentLoads < e14 && this.visibleSegmentBacklog.length > 0; ) {
11212
11102
  const e15 = this.visibleSegmentBacklog.shift();
11213
11103
  this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex) || (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex) || (this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
@@ -11229,8 +11119,8 @@ const _t = class n7 extends BasePlugin {
11229
11119
  if (!s3) return;
11230
11120
  const n10 = Nt(s3);
11231
11121
  if (n10 && t3 >= n10.segments.length) return;
11232
- const r3 = `${e15}:${t3}`, a3 = this.globalSegmentSortKey(e15, t3), i3 = o2.get(r3);
11233
- (void 0 === i3 || a3 < i3) && o2.set(r3, a3);
11122
+ const a3 = `${e15}:${t3}`, i3 = this.segmentStartGlobalIndex(e15, t3), h3 = Math.abs(i3 - r2), l2 = o2.get(a3);
11123
+ (void 0 === l2 || h3 < l2) && o2.set(a3, h3);
11234
11124
  };
11235
11125
  for (const d2 of e14) {
11236
11126
  const e15 = this.virtualPageMap.atIndex(d2);
@@ -11238,7 +11128,7 @@ const _t = class n7 extends BasePlugin {
11238
11128
  a2.add(e15.chapterId);
11239
11129
  const t3 = this.findChapter(e15.chapterId);
11240
11130
  if (!t3) continue;
11241
- const s3 = Vt(t3, e15.localPageIndex);
11131
+ const s3 = Ot(t3, e15.localPageIndex);
11242
11132
  i2(e15.chapterId, s3), this.resolveChapterUrls(t3).catch(() => {
11243
11133
  });
11244
11134
  }
@@ -11247,7 +11137,7 @@ const _t = class n7 extends BasePlugin {
11247
11137
  if (h2) {
11248
11138
  const e15 = this.findChapter(h2.chapterId);
11249
11139
  if (e15) {
11250
- const t3 = Vt(e15, h2.localPageIndex);
11140
+ const t3 = Ot(e15, h2.localPageIndex);
11251
11141
  i2(h2.chapterId, t3 + 1);
11252
11142
  }
11253
11143
  }
@@ -11462,13 +11352,13 @@ function Wt(e14, t2) {
11462
11352
  }
11463
11353
  const Kt = class extends BasePlugin {
11464
11354
  constructor(e14, s2) {
11465
- super(e14, s2), this.layoutChange$ = createBehaviorEmitter(void 0, Wt), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.loadPushRaf = null, this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(qt2.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
11355
+ super(e14, s2), this.layoutChange$ = createBehaviorEmitter(void 0, Wt), this.visibleChange$ = createBehaviorEmitter(), this.virtualPageMap = null, this.pageSizes = /* @__PURE__ */ new Map(), this.offsets = [], this.totalHeight = 0, this.totalWidth = 0, this.viewportEl = null, this.viewportObservers = null, this.currentVisible = [], this.currentGlobalPageIndex = 0, this.lastLoadIndicesKey = "", this.chapterManagerUnsubs = [], this.chapterManager = this.registry.getPlugin(qt2.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
11466
11356
  }
11467
11357
  async initialize(e14) {
11468
11358
  this.config = { placeholderPageHeight: e14.placeholderPageHeight ?? 1200, placeholderPageWidth: e14.placeholderPageWidth ?? 900, bufferSize: e14.bufferSize ?? 2, pageGap: e14.pageGap ?? 20 }, this.virtualPageMap = this.chapterManager.provides().getVirtualPageMap(), this.rebuildOffsets(), this.chapterManagerUnsubs.push(this.chapterManager.provides().onManifestChange(({ map: e15 }) => {
11469
11359
  this.virtualPageMap = e15, this.pageSizes.clear(), this.lastLoadIndicesKey = "", this.rebuildOffsets(), this.syncReadyChapterSizes(), this.recomputeVisible();
11470
11360
  }), this.chapterManager.provides().onChapterStatusChange((e15) => {
11471
- "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads()));
11361
+ "loaded" === e15.status && this.harvestChapterSizes(e15.chapterId), "loaded" !== e15.status && "closed" !== e15.status && "error" !== e15.status && "password-required" !== e15.status || (this.refreshLayout(), "loaded" === e15.status && this.viewportEl && (this.lastLoadIndicesKey = "", this.pushVisibleLoads()));
11472
11362
  })), this.syncReadyChapterSizes();
11473
11363
  }
11474
11364
  isChapterDocumentReady(e14) {
@@ -11488,11 +11378,6 @@ const Kt = class extends BasePlugin {
11488
11378
  refreshLayout() {
11489
11379
  this.layoutChange$.emit(this.computeLayout());
11490
11380
  }
11491
- schedulePushVisibleLoads() {
11492
- null == this.loadPushRaf && (this.loadPushRaf = requestAnimationFrame(() => {
11493
- this.loadPushRaf = null, this.pushVisibleLoads();
11494
- }));
11495
- }
11496
11381
  pushVisibleLoads() {
11497
11382
  if (!this.virtualPageMap || 0 === this.currentVisible.length) return;
11498
11383
  const e14 = this.expandIndicesWithBuffer(this.currentVisible, this.virtualPageMap.totalPages);
@@ -11511,7 +11396,7 @@ const Kt = class extends BasePlugin {
11511
11396
  const { chapterId: s2 } = Lt(e14);
11512
11397
  if (0 === ((null == (t2 = this.virtualPageMap) ? void 0 : t2.pagesOfChapter(s2).length) ?? 0)) return;
11513
11398
  const n9 = this.documentManager.provides();
11514
- n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.schedulePushVisibleLoads();
11399
+ n9.getActiveDocumentId() || n9.setActiveDocument(e14), this.harvestChapterSizes(s2), this.refreshLayout(), this.lastLoadIndicesKey = "", this.pushVisibleLoads();
11515
11400
  }
11516
11401
  onDocumentClosed(e14) {
11517
11402
  var t2;
@@ -11525,7 +11410,7 @@ const Kt = class extends BasePlugin {
11525
11410
  var e14;
11526
11411
  super.destroy();
11527
11412
  for (const t2 of this.chapterManagerUnsubs) t2();
11528
- this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, null != this.loadPushRaf && (cancelAnimationFrame(this.loadPushRaf), this.loadPushRaf = null), this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
11413
+ this.chapterManagerUnsubs = [], null == (e14 = this.viewportObservers) || e14.unbind(), this.viewportObservers = null, this.viewportEl = null, this.lastLoadIndicesKey = "", this.layoutChange$.clear(), this.visibleChange$.clear();
11529
11414
  }
11530
11415
  getPageSize(e14) {
11531
11416
  return this.pageSizes.get(e14) ?? { width: this.config.placeholderPageWidth, height: this.config.placeholderPageHeight };
@@ -11634,7 +11519,7 @@ const Kt = class extends BasePlugin {
11634
11519
  const e15 = this.offsets[h2], n10 = e15 + this.getPageSize(h2).height, r3 = Math.max(0, Math.min(n10, s2) - Math.max(e15, t2));
11635
11520
  r3 > i2 && (i2 = r3, o2 = h2);
11636
11521
  }
11637
- this.currentVisible = a2, this.currentGlobalPageIndex = o2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: o2 }), this.schedulePushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
11522
+ this.currentVisible = a2, this.currentGlobalPageIndex = o2, this.visibleChange$.emit({ visibleGlobalPageIndices: a2, currentGlobalPageIndex: o2 }), this.pushVisibleLoads(), this.layoutChange$.emit(this.computeLayout());
11638
11523
  }
11639
11524
  findFirstVisibleIndex(e14) {
11640
11525
  let t2 = 0, s2 = this.offsets.length - 1, n9 = 0;
@@ -11718,7 +11603,7 @@ async function Qt(e14, t2, s2) {
11718
11603
  }
11719
11604
  const Yt = class s extends BasePlugin {
11720
11605
  constructor(e14, s2) {
11721
- super(e14, s2), this.change$ = createBehaviorEmitter([]), this.entries = /* @__PURE__ */ new Map(), this.chapterScroll = this.registry.getPlugin(Ht.id), this.chapterManager = this.registry.getPlugin(wt.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
11606
+ super(e14, s2), this.change$ = createBehaviorEmitter([]), this.entries = /* @__PURE__ */ new Map(), this.chapterScroll = this.registry.getPlugin(Ht.id), this.chapterManager = this.registry.getPlugin(yt2.id), this.documentManager = this.registry.getPlugin(DocumentManagerPlugin.id);
11722
11607
  }
11723
11608
  async initialize(e14) {
11724
11609
  var t2;
@@ -11939,7 +11824,7 @@ async function As2(e14, t2, s2) {
11939
11824
  }
11940
11825
  return { version: 1, exportedAt: /* @__PURE__ */ (/* @__PURE__ */ new Date()).toISOString(), chapters: n9 };
11941
11826
  }
11942
- async function Vs(e14, t2, s2) {
11827
+ async function Os(e14, t2, s2) {
11943
11828
  const n9 = Us(s2), r2 = (s2 == null ? void 0 : s2.mode) ?? "replace", { annotation: a2, note: o2, bookmark: i2, chapterManager: h2 } = Ts(e14), l = t2.chapterId;
11944
11829
  if (n9.bookmarks && t2.bookmarks) {
11945
11830
  if (!i2) throw new Error("paragraph-bookmark 插件未注册");
@@ -11977,14 +11862,14 @@ async function Vs(e14, t2, s2) {
11977
11862
  }
11978
11863
  (s2 == null ? void 0 : s2.persistNotes) && o2 && await s2.persistNotes(o2.listAllNotes()), (s2 == null ? void 0 : s2.persistBookmarks) && i2 && await s2.persistBookmarks(i2.listBookmarks());
11979
11864
  }
11980
- async function Os(e14, t2, s2) {
11865
+ async function Vs(e14, t2, s2) {
11981
11866
  if (1 !== t2.version) throw new Error(`不支持的归档版本 ${t2.version},当前为 1`);
11982
- for (const [n9, r2] of Object.entries(t2.chapters)) await Vs(e14, { chapterId: n9, ...r2 }, s2);
11867
+ for (const [n9, r2] of Object.entries(t2.chapters)) await Os(e14, { chapterId: n9, ...r2 }, s2);
11983
11868
  }
11984
- function Rs(e14) {
11869
+ function Gs(e14) {
11985
11870
  return JSON.stringify(e14, null, 2);
11986
11871
  }
11987
- function Gs(e14) {
11872
+ function Rs(e14) {
11988
11873
  const t2 = JSON.parse(e14);
11989
11874
  if (!t2 || "object" != typeof t2 || !t2.chapters) throw new Error("无效的章节标注 JSON");
11990
11875
  return t2;
@@ -12002,7 +11887,7 @@ function _s(e14, t2) {
12002
11887
  r2.href = n9, r2.download = e14, r2.click(), URL.revokeObjectURL(n9);
12003
11888
  }
12004
11889
  function qs(e14, t2 = "chapter-annotations.json") {
12005
- _s(t2, Rs(e14));
11890
+ _s(t2, Gs(e14));
12006
11891
  }
12007
11892
  function Ws(e14, t2) {
12008
11893
  _s(t2 ?? `chapter-annotations-${e14.chapterId}.json`, zs(e14));
@@ -12324,18 +12209,17 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
12324
12209
  this.manifestChange$.emit({ manifest: normalizedManifest, map: this.virtualPageMap });
12325
12210
  this.eagerPrefetchFromManifest();
12326
12211
  }
12212
+ /** manifest 就绪后仅预取 URL 列表;open PDF 由视口/首章门控驱动,避免抢并发槽 */
12327
12213
  eagerPrefetchFromManifest() {
12328
12214
  const prefetch = this.config.prefetchChapters ?? 0;
12329
12215
  if (prefetch <= 0 || this.manifest.chapters.length === 0) return;
12330
12216
  const now = Date.now();
12331
12217
  const count = Math.min(prefetch, this.manifest.chapters.length);
12332
12218
  for (let i2 = 0; i2 < count; i2++) {
12333
- const chapterId = this.manifest.chapters[i2].chapterId;
12334
- this.chapterLastUsed.set(chapterId, now);
12335
- const st2 = this.chapterStatus.get(chapterId) ?? "idle";
12336
- if (st2 === "idle" || st2 === "closed" || st2 === "error") {
12337
- void this.ensureChapterLoaded(chapterId);
12338
- }
12219
+ const chapter = this.manifest.chapters[i2];
12220
+ this.chapterLastUsed.set(chapter.chapterId, now);
12221
+ void this.resolveChapterUrls(chapter).catch(() => {
12222
+ });
12339
12223
  }
12340
12224
  }
12341
12225
  findChapter(chapterId) {
@@ -12379,8 +12263,7 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
12379
12263
  if (segmentIndex === 0 && this.pendingChapterLoadPromises.has(chapterId)) {
12380
12264
  return true;
12381
12265
  }
12382
- const coreDoc = this.coreState.core.documents[documentId];
12383
- return (coreDoc == null ? void 0 : coreDoc.status) === "loading";
12266
+ return false;
12384
12267
  }
12385
12268
  syncChapterStatusFromDocuments(chapterId) {
12386
12269
  const plan = this.getSegmentPlanForChapter(chapterId);
@@ -12440,8 +12323,8 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
12440
12323
  this.visibleSegmentBacklog = [];
12441
12324
  this.activeVisibleSegmentLoads = 0;
12442
12325
  }
12443
- /** 分段在整本中的阅读顺序(globalPageIndex 升序 → 按章按段依次 open PDF) */
12444
- globalSegmentSortKey(chapterId, segmentIndex) {
12326
+ /** 分段起始页在整本中的 globalPageIndex(用于算与视口距离) */
12327
+ segmentStartGlobalIndex(chapterId, segmentIndex) {
12445
12328
  var _a2;
12446
12329
  if (!this.virtualPageMap) return Number.MAX_SAFE_INTEGER;
12447
12330
  const chapter = this.findChapter(chapterId);
@@ -12453,7 +12336,7 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
12453
12336
  const chIdx = this.manifest.chapters.findIndex((c2) => c2.chapterId === chapterId);
12454
12337
  return chIdx * 1e4 + segmentIndex;
12455
12338
  }
12456
- /** 按阅读顺序重建队列并立即 pump(替换旧 backlog,避免末尾多章同时 loading) */
12339
+ /** 按视口距离重建队列并立即 pump */
12457
12340
  scheduleVisibleSegmentLoads(ranked) {
12458
12341
  const sorted = [...ranked].sort((a2, b2) => a2.priority - b2.priority);
12459
12342
  const seen = /* @__PURE__ */ new Set();
@@ -12463,14 +12346,13 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
12463
12346
  if (seen.has(k2)) continue;
12464
12347
  seen.add(k2);
12465
12348
  if (this.isSegmentDocumentOpen(t2.chapterId, t2.segmentIndex)) continue;
12466
- if (this.isSegmentLoadPending(t2.chapterId, t2.segmentIndex)) continue;
12467
12349
  nextBacklog.push({ chapterId: t2.chapterId, segmentIndex: t2.segmentIndex });
12468
12350
  }
12469
12351
  this.visibleSegmentBacklog = nextBacklog;
12470
12352
  this.pumpVisibleSegmentBacklog();
12471
12353
  }
12472
12354
  pumpVisibleSegmentBacklog() {
12473
- const max = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 2);
12355
+ const max = Math.max(1, this.config.maxConcurrentSegmentLoads ?? 1);
12474
12356
  while (this.activeVisibleSegmentLoads < max && this.visibleSegmentBacklog.length > 0) {
12475
12357
  const t2 = this.visibleSegmentBacklog.shift();
12476
12358
  if (this.isSegmentDocumentOpen(t2.chapterId, t2.segmentIndex)) continue;
@@ -12507,7 +12389,8 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
12507
12389
  const plan = u(chapter);
12508
12390
  if (plan && segmentIndex >= plan.segments.length) return;
12509
12391
  const segKey = `${chapterId}:${segmentIndex}`;
12510
- const priority = this.globalSegmentSortKey(chapterId, segmentIndex);
12392
+ const segStart = this.segmentStartGlobalIndex(chapterId, segmentIndex);
12393
+ const priority = Math.abs(segStart - focalIdx);
12511
12394
  const prev = segmentPriorities.get(segKey);
12512
12395
  if (prev === void 0 || priority < prev) segmentPriorities.set(segKey, priority);
12513
12396
  };
@@ -12901,29 +12784,29 @@ const m = { kind: "first-wins" };
12901
12784
  function _(e14) {
12902
12785
  return "last" === e14 ? { kind: "last-wins" } : { kind: "first-wins" };
12903
12786
  }
12904
- function R2(e14) {
12787
+ function R(e14) {
12905
12788
  const t2 = e14.endPage - e14.startPage + 1;
12906
12789
  return { chapterId: e14.chapterId, title: e14.title, globalPageRange: [e14.startPage, e14.endPage], localPageRange: [0, t2 - 1], ...e14.source ? { source: e14.source } : {}, ...null != e14.segmentPageThreshold ? { segmentPageThreshold: e14.segmentPageThreshold } : {}, ...e14.encrypted ? { encrypted: e14.encrypted } : {} };
12907
12790
  }
12908
12791
  function C(e14) {
12909
12792
  const t2 = [], n9 = (e15) => {
12910
12793
  var _a2;
12911
- for (const r2 of e15) t2.push(R2(r2)), ((_a2 = r2.children) == null ? void 0 : _a2.length) && n9(r2.children);
12794
+ for (const r2 of e15) t2.push(R(r2)), ((_a2 = r2.children) == null ? void 0 : _a2.length) && n9(r2.children);
12912
12795
  };
12913
12796
  return n9(e14), t2;
12914
12797
  }
12915
- function G(e14) {
12798
+ function G2(e14) {
12916
12799
  return e14.map((e15) => {
12917
12800
  var _a2;
12918
- return { id: e15.chapterId, title: e15.title, startPage: e15.startPage, endPage: e15.endPage, children: ((_a2 = e15.children) == null ? void 0 : _a2.length) ? G(e15.children) : void 0 };
12801
+ return { id: e15.chapterId, title: e15.title, startPage: e15.startPage, endPage: e15.endPage, children: ((_a2 = e15.children) == null ? void 0 : _a2.length) ? G2(e15.children) : void 0 };
12919
12802
  });
12920
12803
  }
12921
12804
  function M(e14) {
12922
- return { tree: G(e14), manifest: { chapters: C(e14) } };
12805
+ return { tree: G2(e14), manifest: { chapters: C(e14) } };
12923
12806
  }
12924
12807
  export {
12925
12808
  Ls as CHAPTER_ANNOTATIONS_ARCHIVE_VERSION,
12926
- Fe$1 as CallbackPasswordProvider,
12809
+ Ne$1 as CallbackPasswordProvider,
12927
12810
  ChapterManagerPlugin,
12928
12811
  _sfc_main as ChapterPdfViewer,
12929
12812
  Ja as ChapterTreePanel,
@@ -12931,13 +12814,13 @@ export {
12931
12814
  Ln as DEFAULT_CHAPTER_VIEWER_CONFIG,
12932
12815
  xn as DEFAULT_CHAPTER_VIEWER_FEATURES,
12933
12816
  EmbedPDF2 as EmbedPDF,
12934
- Ra as PdfChapterViewport,
12817
+ za as PdfChapterViewport,
12935
12818
  xe$1 as applySelectionMarkup,
12936
12819
  M as buildChapterViewerCatalog,
12937
12820
  pn as buildParagraphBookmarkAnchor,
12938
- Rs as chapterAnnotationsArchiveToJson,
12821
+ Gs as chapterAnnotationsArchiveToJson,
12939
12822
  zs as chapterAnnotationsSnapshotToJson,
12940
- R2 as chapterDescriptorFromTreeNode,
12823
+ R as chapterDescriptorFromTreeNode,
12941
12824
  Ms as copyTextToClipboard,
12942
12825
  Un as createChapterViewerBundle,
12943
12826
  $n as createChapterViewerEditor,
@@ -12950,12 +12833,12 @@ export {
12950
12833
  As2 as exportAllChapterAnnotations,
12951
12834
  Ns as exportChapterAnnotations,
12952
12835
  C as flattenChapterTree,
12953
- Vs as importChapterAnnotations,
12954
- Os as importChapterAnnotationsArchive,
12836
+ Os as importChapterAnnotations,
12837
+ Vs as importChapterAnnotationsArchive,
12955
12838
  _ as overlapStrategyForSamePageOwner,
12956
- Gs as parseChapterAnnotationsArchiveJson,
12839
+ Rs as parseChapterAnnotationsArchiveJson,
12957
12840
  Bs as parseChapterAnnotationsSnapshotJson,
12958
- G as toChapterTreeNodes,
12841
+ G2 as toChapterTreeNodes,
12959
12842
  useCapability2 as useCapability,
12960
12843
  usePdfiumEngine,
12961
12844
  useRegistry2 as useRegistry