@prose-reader/core 1.202.0 → 1.204.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -128,9 +128,9 @@ export declare const createReaderWithEnhancers: (options: Partial<import('./sett
128
128
  }, "state" | "state$"> & {
129
129
  state$: import('rxjs').Observable<import('./pagination/Pagination').PaginationInfo & import('./enhancers/pagination/types').ExtraPaginationInfo>;
130
130
  state: import('./pagination/Pagination').PaginationInfo & import('./enhancers/pagination/types').ExtraPaginationInfo;
131
- } & {
132
- locate: <T extends import('./enhancers/pagination/locate').LocatableResource>(resources: T[]) => import('rxjs').Observable<(import('./enhancers/pagination/locate').ConsolidatedResource & T)[]>;
133
131
  };
132
+ locateResources: import('./enhancers/pagination/ResourcesLocator').ResourcesLocator["locateMultiple"];
133
+ locateResource: import('./enhancers/pagination/ResourcesLocator').ResourcesLocator["locate"];
134
134
  } & {
135
135
  theme: {
136
136
  set: (theme: import('.').Theme) => void;
@@ -1,6 +1,7 @@
1
1
  import { UserNavigationEntry } from '../../../navigation/types';
2
2
  import { Reader } from '../../../reader';
3
3
  import { SpinePosition } from '../../../spine/types';
4
+ import { SpineItemReference } from '../../../spineItem/SpineItem';
4
5
  export declare class ManualNavigator {
5
6
  protected reader: Reader;
6
7
  movingLastDelta: {
@@ -20,7 +21,7 @@ export declare class ManualNavigator {
20
21
  animate: boolean;
21
22
  }): void;
22
23
  goToSpineItem({ indexOrId, ...rest }: {
23
- indexOrId: number | string;
24
+ indexOrId: SpineItemReference;
24
25
  } & Pick<UserNavigationEntry, "animation">): void;
25
26
  goToNextSpineItem(): void;
26
27
  goToPreviousSpineItem(): void;
@@ -0,0 +1,34 @@
1
+ import { Observable } from 'rxjs';
2
+ import { Reader } from '../../reader';
3
+ import { SpineItem } from '../../spineItem/SpineItem';
4
+ type CfiLocatableResource = {
5
+ cfi: string;
6
+ endCfi?: string;
7
+ };
8
+ export type LocatableResource = SpineItem | CfiLocatableResource;
9
+ export type ConsolidatedResource = CfiLocatableResource & {
10
+ itemIndex?: number;
11
+ itemPageIndex?: number;
12
+ absolutePageIndex?: number;
13
+ startNode?: Node;
14
+ startOffset?: number;
15
+ range?: Range;
16
+ };
17
+ export declare const consolidate: (resource: ConsolidatedResource, reader: Reader) => Observable<ConsolidatedResource>;
18
+ export declare class ResourcesLocator {
19
+ private reader;
20
+ constructor(reader: Reader);
21
+ locate: <T extends LocatableResource>(resource: T, options?: {
22
+ mode?: "shallow" | "load";
23
+ }) => Observable<{
24
+ resource: T;
25
+ meta: ConsolidatedResource;
26
+ }>;
27
+ locateMultiple: <T extends LocatableResource>(resources: T[], options?: {
28
+ mode?: "shallow" | "load";
29
+ }) => Observable<{
30
+ resource: T;
31
+ meta: ConsolidatedResource;
32
+ }[]>;
33
+ }
34
+ export {};
@@ -1,14 +1,4 @@
1
- import { Observable } from 'rxjs';
2
- import { PaginationInfo } from '../../pagination/Pagination';
3
1
  import { LayoutEnhancerOutput } from '../layout/layoutEnhancer';
4
2
  import { EnhancerOutput, RootEnhancer } from '../types/enhancer';
5
- import { ConsolidatedResource, LocatableResource } from './locate';
6
- import { ExtraPaginationInfo } from './types';
7
- export declare const paginationEnhancer: <InheritOptions, InheritOutput extends EnhancerOutput<RootEnhancer> & LayoutEnhancerOutput, PaginationOutput extends Omit<InheritOutput["pagination"], "state$" | "state"> & {
8
- state$: Observable<PaginationInfo & ExtraPaginationInfo>;
9
- state: PaginationInfo & ExtraPaginationInfo;
10
- }>(next: (options: InheritOptions) => InheritOutput) => (options: InheritOptions) => Omit<InheritOutput, "pagination"> & {
11
- pagination: PaginationOutput & {
12
- locate: <T extends LocatableResource>(resources: T[]) => Observable<(ConsolidatedResource & T)[]>;
13
- };
14
- };
3
+ import { PaginationEnhancerAPI } from './types';
4
+ export declare const paginationEnhancer: <InheritOptions, InheritOutput extends EnhancerOutput<RootEnhancer> & LayoutEnhancerOutput, PaginationOutput extends PaginationEnhancerAPI<InheritOutput>>(next: (options: InheritOptions) => InheritOutput) => (options: InheritOptions) => PaginationOutput;
@@ -1,4 +1,7 @@
1
+ import { Observable } from 'rxjs';
1
2
  import { PaginationInfo } from '../../pagination/Pagination';
3
+ import { EnhancerOutput, RootEnhancer } from '../types/enhancer';
4
+ import { ResourcesLocator } from './ResourcesLocator';
2
5
  import { ChapterInfo } from './chapters';
3
6
  export type ExtraPaginationInfo = {
4
7
  beginChapterInfo: ChapterInfo | undefined;
@@ -12,3 +15,11 @@ export type ExtraPaginationInfo = {
12
15
  isUsingSpread: boolean;
13
16
  };
14
17
  export type EnhancerPaginationInto = PaginationInfo & ExtraPaginationInfo;
18
+ export type PaginationEnhancerAPI<InheritOutput extends EnhancerOutput<RootEnhancer>> = Omit<InheritOutput, "pagination"> & {
19
+ pagination: Omit<InheritOutput["pagination"], "state$" | "state"> & {
20
+ state$: Observable<PaginationInfo & ExtraPaginationInfo>;
21
+ state: PaginationInfo & ExtraPaginationInfo;
22
+ };
23
+ locateResources: ResourcesLocator["locateMultiple"];
24
+ locateResource: ResourcesLocator["locate"];
25
+ };
package/dist/index.js CHANGED
@@ -677,6 +677,17 @@ const observeMutation = (target, options) => {
677
677
  return () => observer.disconnect();
678
678
  });
679
679
  };
680
+ function observeIntersection(element, options) {
681
+ return new Observable((observer) => {
682
+ const intersectionObserver = new IntersectionObserver((entries) => {
683
+ observer.next(entries);
684
+ }, options);
685
+ intersectionObserver.observe(element);
686
+ return () => {
687
+ intersectionObserver.disconnect();
688
+ };
689
+ });
690
+ }
680
691
  class SettingsManagerOverload {
681
692
  constructor(initialSettings, settingsManager) {
682
693
  this.settingsManager = settingsManager;
@@ -934,7 +945,7 @@ class DestroyableClass {
934
945
  this.destroySubject.complete();
935
946
  }
936
947
  }
937
- class DocumentRenderer extends DestroyableClass {
948
+ const _DocumentRenderer = class _DocumentRenderer extends DestroyableClass {
938
949
  constructor(params) {
939
950
  super();
940
951
  this.triggerSubject = new Subject();
@@ -1024,6 +1035,12 @@ class DocumentRenderer extends DestroyableClass {
1024
1035
  );
1025
1036
  merge(unload$).pipe(takeUntil(this.destroy$)).subscribe();
1026
1037
  }
1038
+ setDocumentContainer(element) {
1039
+ this._documentContainer = element;
1040
+ this._documentContainer.classList.add(
1041
+ _DocumentRenderer.DOCUMENT_CONTAINER_CLASS_NAME
1042
+ );
1043
+ }
1027
1044
  attach() {
1028
1045
  if (this.documentContainer) {
1029
1046
  this.containerElement.appendChild(this.documentContainer);
@@ -1031,8 +1048,8 @@ class DocumentRenderer extends DestroyableClass {
1031
1048
  }
1032
1049
  detach() {
1033
1050
  var _a;
1034
- (_a = this.documentContainer) == null ? void 0 : _a.remove();
1035
- this.documentContainer = void 0;
1051
+ (_a = this._documentContainer) == null ? void 0 : _a.remove();
1052
+ this._documentContainer = void 0;
1036
1053
  }
1037
1054
  get state$() {
1038
1055
  return this.stateSubject;
@@ -1070,13 +1087,16 @@ class DocumentRenderer extends DestroyableClass {
1070
1087
  );
1071
1088
  }
1072
1089
  layout(params) {
1073
- return defer(() => this.onLayout(params)).pipe();
1090
+ return defer(() => this.onLayout(params));
1074
1091
  }
1075
1092
  destroy() {
1076
1093
  this.unload();
1077
1094
  this.stateSubject.complete();
1078
1095
  super.destroy();
1079
1096
  }
1097
+ get documentContainer() {
1098
+ return this._documentContainer;
1099
+ }
1080
1100
  get writingMode() {
1081
1101
  return void 0;
1082
1102
  }
@@ -1094,7 +1114,9 @@ class DocumentRenderer extends DestroyableClass {
1094
1114
  }
1095
1115
  return ((_a = this.context.manifest) == null ? void 0 : _a.renditionLayout) ?? "reflowable";
1096
1116
  }
1097
- }
1117
+ };
1118
+ _DocumentRenderer.DOCUMENT_CONTAINER_CLASS_NAME = `prose-reader-document-container`;
1119
+ let DocumentRenderer = _DocumentRenderer;
1098
1120
  const defaultGetResource = (item) => new URL(item.href);
1099
1121
  class ResourceHandler {
1100
1122
  constructor(item, settings) {
@@ -1887,7 +1909,7 @@ class HtmlRenderer extends DocumentRenderer {
1887
1909
  }
1888
1910
  onCreateDocument() {
1889
1911
  const frameElement = createFrameElement();
1890
- this.documentContainer = frameElement;
1912
+ this.setDocumentContainer(frameElement);
1891
1913
  return of(frameElement);
1892
1914
  }
1893
1915
  onLoadDocument() {
@@ -2554,7 +2576,7 @@ class ImageRenderer extends DocumentRenderer {
2554
2576
  const imgElement = this.containerElement.ownerDocument.createElement(`img`);
2555
2577
  return from(this.resourcesHandler.getResource()).pipe(
2556
2578
  switchMap((responseOrUrl) => {
2557
- this.documentContainer = imgElement;
2579
+ this.setDocumentContainer(imgElement);
2558
2580
  imgElement.style.objectFit = `contain`;
2559
2581
  imgElement.style.userSelect = `none`;
2560
2582
  if (responseOrUrl instanceof URL) {
@@ -3040,7 +3062,7 @@ class ManualNavigator {
3040
3062
  return;
3041
3063
  }
3042
3064
  this.reader.navigation.navigate({
3043
- spineItem: indexOrId,
3065
+ spineItem: spineItem.index,
3044
3066
  ...rest
3045
3067
  });
3046
3068
  }
@@ -3314,91 +3336,118 @@ const navigationEnhancer = (next) => (options) => {
3314
3336
  }
3315
3337
  };
3316
3338
  };
3317
- const NAMESPACE$4 = `paginationEnhancer`;
3318
- const consolidate = (item, reader) => {
3319
- var _a;
3320
- let itemPageIndex = (_a = item.meta) == null ? void 0 : _a.itemPageIndex;
3321
- const { itemIndex } = reader.cfi.parseCfi(item.cfi);
3322
- const spineItem = reader.spineItemsManager.get(itemIndex);
3323
- if (!spineItem) return of({ ...item, meta: { ...item.meta, itemIndex } });
3324
- if (spineItem.renditionLayout === `pre-paginated`) {
3325
- itemPageIndex = 0;
3339
+ const getItemAnchor = (item) => `|[prose~anchor~${encodeURIComponent(item.index)}]`;
3340
+ const getRootCfi = (spineItem) => {
3341
+ const itemAnchor = getItemAnchor(spineItem.item);
3342
+ return `epubcfi(/0${itemAnchor})`;
3343
+ };
3344
+ const toCfiLocatableResource = (reader, resource) => {
3345
+ if ("cfi" in resource) {
3346
+ return resource;
3326
3347
  }
3348
+ const item = reader.spineItemsManager.get(resource);
3349
+ if (!item) {
3350
+ throw new Error(`Spine item not found`);
3351
+ }
3352
+ return {
3353
+ cfi: getRootCfi(item)
3354
+ };
3355
+ };
3356
+ const consolidate = (resource, reader) => {
3357
+ let itemPageIndex = resource == null ? void 0 : resource.itemPageIndex;
3358
+ const { itemIndex } = reader.cfi.parseCfi(resource.cfi);
3359
+ const spineItem = reader.spineItemsManager.get(itemIndex);
3360
+ if (!spineItem) return of({ ...resource, itemIndex });
3327
3361
  return idle().pipe(
3328
3362
  withLatestFrom(spineItem.isReady$),
3329
3363
  map(([, isSpineItemReady]) => {
3330
- var _a2, _b, _c;
3364
+ var _a;
3331
3365
  let range = void 0;
3332
- const { node: startNode, offset: startOffset } = reader.cfi.resolveCfi({ cfi: item.cfi }) ?? {};
3333
- if (spineItem.renditionLayout !== `pre-paginated` && startNode) {
3366
+ const { node: startNode, offset: startOffset } = reader.cfi.resolveCfi({ cfi: resource.cfi }) ?? {};
3367
+ const reflowableItemWithFoundNode = spineItem.renditionLayout !== `pre-paginated` && startNode;
3368
+ if (reflowableItemWithFoundNode) {
3334
3369
  itemPageIndex = reader.spine.locator.spineItemLocator.getSpineItemPageIndexFromNode(
3335
3370
  startNode,
3336
3371
  startOffset ?? 0,
3337
3372
  spineItem
3338
3373
  ) ?? itemPageIndex;
3339
3374
  }
3340
- if (startNode && item.endCfi) {
3341
- const { node: endNode, offset: endOffset } = reader.cfi.resolveCfi({ cfi: item.cfi }) ?? {};
3375
+ if (startNode && resource.endCfi) {
3376
+ const { node: endNode, offset: endOffset } = reader.cfi.resolveCfi({ cfi: resource.cfi }) ?? {};
3342
3377
  if (endNode && isSpineItemReady) {
3343
- range = (_a2 = startNode == null ? void 0 : startNode.ownerDocument) == null ? void 0 : _a2.createRange();
3378
+ range = (_a = startNode == null ? void 0 : startNode.ownerDocument) == null ? void 0 : _a.createRange();
3344
3379
  range == null ? void 0 : range.setStart(startNode, startOffset ?? 0);
3345
3380
  range == null ? void 0 : range.setEnd(endNode, endOffset ?? 0);
3346
3381
  }
3347
3382
  }
3348
- let absolutePageIndex = (_b = item.meta) == null ? void 0 : _b.absolutePageIndex;
3349
- if (itemPageIndex !== void 0) {
3350
- absolutePageIndex = reader.spine.locator.getAbsolutePageIndexFromPageIndex({
3351
- pageIndex: itemPageIndex,
3352
- spineItemOrId: spineItem
3353
- }) ?? ((_c = item.meta) == null ? void 0 : _c.absolutePageIndex);
3383
+ let absolutePageIndex = resource == null ? void 0 : resource.absolutePageIndex;
3384
+ if (itemPageIndex === void 0) {
3385
+ itemPageIndex = 0;
3354
3386
  }
3387
+ absolutePageIndex = reader.spine.locator.getAbsolutePageIndexFromPageIndex({
3388
+ pageIndex: itemPageIndex,
3389
+ spineItemOrId: spineItem
3390
+ }) ?? (resource == null ? void 0 : resource.absolutePageIndex);
3355
3391
  return {
3356
- ...item,
3357
- meta: {
3358
- ...item.meta,
3359
- range,
3360
- itemIndex,
3361
- startNode,
3362
- startOffset,
3363
- absolutePageIndex,
3364
- itemPageIndex
3365
- }
3392
+ ...resource,
3393
+ range,
3394
+ itemIndex,
3395
+ startNode,
3396
+ startOffset,
3397
+ absolutePageIndex,
3398
+ itemPageIndex
3366
3399
  };
3367
3400
  })
3368
3401
  );
3369
3402
  };
3370
- const createLocator = (reader) => (resources) => {
3371
- return deferIdle(() => {
3372
- if (!resources.length) return of(resources);
3373
- const consolidate$ = reader.spine.spineLayout.layout$.pipe(
3374
- debounceTime$1(10),
3375
- startWith$1(null),
3376
- switchScan((acc$) => {
3377
- return forkJoin(
3378
- acc$.map(
3379
- (resource) => deferIdle(() => consolidate(resource, reader)).pipe(
3380
- map((value) => value)
3381
- )
3382
- )
3403
+ class ResourcesLocator {
3404
+ constructor(reader) {
3405
+ this.reader = reader;
3406
+ this.locate = (resource, options = {}) => {
3407
+ const cfiConsolidatedResource = {
3408
+ resource,
3409
+ meta: toCfiLocatableResource(this.reader, resource)
3410
+ };
3411
+ return deferIdle(() => {
3412
+ var _a;
3413
+ const consolidate$ = this.reader.spine.spineLayout.layout$.pipe(
3414
+ debounceTime$1(10),
3415
+ startWith$1(cfiConsolidatedResource),
3416
+ switchScan((acc) => {
3417
+ return consolidate(acc.meta, this.reader).pipe(
3418
+ map((consolidatedResource) => ({
3419
+ ...acc,
3420
+ meta: consolidatedResource
3421
+ }))
3422
+ );
3423
+ }, cfiConsolidatedResource)
3383
3424
  );
3384
- }, resources)
3385
- );
3386
- const reflowableItemIndexes = resources.map((item) => reader.cfi.parseCfi(item.cfi).itemIndex).filter(isDefined).filter((index) => {
3387
- var _a;
3388
- return ((_a = reader.spineItemsManager.get(index)) == null ? void 0 : _a.renditionLayout) === `reflowable`;
3389
- });
3390
- const release = reader.spine.spineItemsLoader.forceOpen(
3391
- reflowableItemIndexes
3392
- );
3393
- return consolidate$.pipe(
3394
- finalize(() => {
3395
- setTimeout(() => {
3396
- release();
3397
- }, 1e3);
3398
- })
3399
- );
3400
- });
3401
- };
3425
+ const itemIndex = (_a = this.reader.cfi.parseCfi(
3426
+ cfiConsolidatedResource.meta.cfi
3427
+ )) == null ? void 0 : _a.itemIndex;
3428
+ const item = this.reader.spineItemsManager.get(itemIndex);
3429
+ const isReflowable = (item == null ? void 0 : item.renditionLayout) === `reflowable`;
3430
+ const release = options.mode === "shallow" || !isReflowable || !item ? () => {
3431
+ } : this.reader.spine.spineItemsLoader.forceOpen([item.index]);
3432
+ return consolidate$.pipe(
3433
+ finalize(() => {
3434
+ setTimeout(() => {
3435
+ release();
3436
+ }, 1e3);
3437
+ })
3438
+ );
3439
+ });
3440
+ };
3441
+ this.locateMultiple = (resources, options = {}) => {
3442
+ return deferIdle(
3443
+ () => combineLatest(
3444
+ resources.map((resource) => this.locate(resource, options))
3445
+ )
3446
+ );
3447
+ };
3448
+ }
3449
+ }
3450
+ const NAMESPACE$4 = `paginationEnhancer`;
3402
3451
  const buildChaptersInfo = (href, tocItem, manifest) => {
3403
3452
  const spineItemIndex = manifest.spineItems.findIndex(
3404
3453
  (item) => item.href === href
@@ -3674,14 +3723,15 @@ const paginationEnhancer = (next) => (options) => {
3674
3723
  const reader = next(options);
3675
3724
  const { paginationInfo$, getPaginationInfo } = trackPaginationInfo(reader);
3676
3725
  paginationInfo$.pipe(takeUntil(reader.$.destroy$)).subscribe();
3677
- const locate = createLocator(reader);
3726
+ const resourcesLocator = new ResourcesLocator(reader);
3678
3727
  return {
3679
3728
  ...reader,
3729
+ locateResources: resourcesLocator.locateMultiple,
3730
+ locateResource: resourcesLocator.locate,
3680
3731
  pagination: {
3681
3732
  ...reader.pagination,
3682
3733
  getState: () => getPaginationInfo(),
3683
- state$: paginationInfo$,
3684
- locate
3734
+ state$: paginationInfo$
3685
3735
  }
3686
3736
  };
3687
3737
  };
@@ -4391,7 +4441,6 @@ const zoomEnhancer = (next) => (options) => {
4391
4441
  }
4392
4442
  };
4393
4443
  };
4394
- const getItemAnchor = (item) => `|[prose~anchor~${encodeURIComponent(item.index)}]`;
4395
4444
  const ELEMENT_NODE = Node.ELEMENT_NODE;
4396
4445
  const TEXT_NODE = Node.TEXT_NODE;
4397
4446
  const CDATA_SECTION_NODE = Node.CDATA_SECTION_NODE;
@@ -5284,10 +5333,6 @@ const generateCfi = (node, offset, item) => {
5284
5333
  `${getItemAnchor(item)}|${offsetAnchor}`
5285
5334
  );
5286
5335
  };
5287
- const getRootCfi = (spineItem) => {
5288
- const itemAnchor = getItemAnchor(spineItem.item);
5289
- return `epubcfi(/0${itemAnchor})`;
5290
- };
5291
5336
  const generateCfiForSpineItemPage = Report.measurePerformance(
5292
5337
  `getCfi`,
5293
5338
  10,
@@ -9293,6 +9338,7 @@ export {
9293
9338
  isHtmlTagElement,
9294
9339
  isShallowEqual2 as isShallowEqual,
9295
9340
  mapKeysTo,
9341
+ observeIntersection,
9296
9342
  observeMutation,
9297
9343
  observeResize,
9298
9344
  removeCSS,