@prose-reader/core 1.203.0 → 1.205.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,8 @@ 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
+ locateResource: import('./enhancers/pagination/ResourcesLocator').ResourcesLocator["locateResource"];
134
133
  } & {
135
134
  theme: {
136
135
  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,37 @@
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
+ type Options = {
19
+ mode?: "shallow" | "load";
20
+ };
21
+ export declare class ResourcesLocator {
22
+ private reader;
23
+ constructor(reader: Reader);
24
+ locate: <T extends LocatableResource>(resource: T, options: Options) => Observable<{
25
+ resource: T;
26
+ meta: ConsolidatedResource;
27
+ }>;
28
+ locateResource<T extends LocatableResource>(resource: T, options?: Options): Observable<{
29
+ resource: T;
30
+ meta: ConsolidatedResource;
31
+ }>;
32
+ locateResource<T extends LocatableResource>(resource: T[], options?: Options): Observable<{
33
+ resource: T;
34
+ meta: ConsolidatedResource;
35
+ }[]>;
36
+ }
37
+ 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,10 @@ 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
+ locateResource: ResourcesLocator["locateResource"];
24
+ };
package/dist/index.js CHANGED
@@ -1087,7 +1087,7 @@ const _DocumentRenderer = class _DocumentRenderer extends DestroyableClass {
1087
1087
  );
1088
1088
  }
1089
1089
  layout(params) {
1090
- return defer(() => this.onLayout(params)).pipe();
1090
+ return defer(() => this.onLayout(params));
1091
1091
  }
1092
1092
  destroy() {
1093
1093
  this.unload();
@@ -3062,7 +3062,7 @@ class ManualNavigator {
3062
3062
  return;
3063
3063
  }
3064
3064
  this.reader.navigation.navigate({
3065
- spineItem: indexOrId,
3065
+ spineItem: spineItem.index,
3066
3066
  ...rest
3067
3067
  });
3068
3068
  }
@@ -3336,91 +3336,119 @@ const navigationEnhancer = (next) => (options) => {
3336
3336
  }
3337
3337
  };
3338
3338
  };
3339
- const NAMESPACE$4 = `paginationEnhancer`;
3340
- const consolidate = (item, reader) => {
3341
- var _a;
3342
- let itemPageIndex = (_a = item.meta) == null ? void 0 : _a.itemPageIndex;
3343
- const { itemIndex } = reader.cfi.parseCfi(item.cfi);
3344
- const spineItem = reader.spineItemsManager.get(itemIndex);
3345
- if (!spineItem) return of({ ...item, meta: { ...item.meta, itemIndex } });
3346
- if (spineItem.renditionLayout === `pre-paginated`) {
3347
- 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;
3347
+ }
3348
+ const item = reader.spineItemsManager.get(resource);
3349
+ if (!item) {
3350
+ throw new Error(`Spine item not found`);
3348
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 });
3349
3361
  return idle().pipe(
3350
3362
  withLatestFrom(spineItem.isReady$),
3351
3363
  map(([, isSpineItemReady]) => {
3352
- var _a2, _b, _c;
3364
+ var _a;
3353
3365
  let range = void 0;
3354
- const { node: startNode, offset: startOffset } = reader.cfi.resolveCfi({ cfi: item.cfi }) ?? {};
3355
- 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) {
3356
3369
  itemPageIndex = reader.spine.locator.spineItemLocator.getSpineItemPageIndexFromNode(
3357
3370
  startNode,
3358
3371
  startOffset ?? 0,
3359
3372
  spineItem
3360
3373
  ) ?? itemPageIndex;
3361
3374
  }
3362
- if (startNode && item.endCfi) {
3363
- 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 }) ?? {};
3364
3377
  if (endNode && isSpineItemReady) {
3365
- 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();
3366
3379
  range == null ? void 0 : range.setStart(startNode, startOffset ?? 0);
3367
3380
  range == null ? void 0 : range.setEnd(endNode, endOffset ?? 0);
3368
3381
  }
3369
3382
  }
3370
- let absolutePageIndex = (_b = item.meta) == null ? void 0 : _b.absolutePageIndex;
3371
- if (itemPageIndex !== void 0) {
3372
- absolutePageIndex = reader.spine.locator.getAbsolutePageIndexFromPageIndex({
3373
- pageIndex: itemPageIndex,
3374
- spineItemOrId: spineItem
3375
- }) ?? ((_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;
3376
3386
  }
3387
+ absolutePageIndex = reader.spine.locator.getAbsolutePageIndexFromPageIndex({
3388
+ pageIndex: itemPageIndex,
3389
+ spineItemOrId: spineItem
3390
+ }) ?? (resource == null ? void 0 : resource.absolutePageIndex);
3377
3391
  return {
3378
- ...item,
3379
- meta: {
3380
- ...item.meta,
3381
- range,
3382
- itemIndex,
3383
- startNode,
3384
- startOffset,
3385
- absolutePageIndex,
3386
- itemPageIndex
3387
- }
3392
+ ...resource,
3393
+ range,
3394
+ itemIndex,
3395
+ startNode,
3396
+ startOffset,
3397
+ absolutePageIndex,
3398
+ itemPageIndex
3388
3399
  };
3389
3400
  })
3390
3401
  );
3391
3402
  };
3392
- const createLocator = (reader) => (resources) => {
3393
- return deferIdle(() => {
3394
- if (!resources.length) return of(resources);
3395
- const consolidate$ = reader.spine.spineLayout.layout$.pipe(
3396
- debounceTime$1(10),
3397
- startWith$1(null),
3398
- switchScan((acc$) => {
3399
- return forkJoin(
3400
- acc$.map(
3401
- (resource) => deferIdle(() => consolidate(resource, reader)).pipe(
3402
- map((value) => value)
3403
- )
3404
- )
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)
3405
3424
  );
3406
- }, resources)
3407
- );
3408
- const reflowableItemIndexes = resources.map((item) => reader.cfi.parseCfi(item.cfi).itemIndex).filter(isDefined).filter((index) => {
3409
- var _a;
3410
- return ((_a = reader.spineItemsManager.get(index)) == null ? void 0 : _a.renditionLayout) === `reflowable`;
3411
- });
3412
- const release = reader.spine.spineItemsLoader.forceOpen(
3413
- reflowableItemIndexes
3414
- );
3415
- return consolidate$.pipe(
3416
- finalize(() => {
3417
- setTimeout(() => {
3418
- release();
3419
- }, 1e3);
3420
- })
3421
- );
3422
- });
3423
- };
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
+ }
3442
+ locateResource(resource, options) {
3443
+ if (Array.isArray(resource)) {
3444
+ return deferIdle(
3445
+ () => combineLatest(resource.map((item) => this.locate(item, options ?? {})))
3446
+ );
3447
+ }
3448
+ return this.locate(resource, options ?? {});
3449
+ }
3450
+ }
3451
+ const NAMESPACE$4 = `paginationEnhancer`;
3424
3452
  const buildChaptersInfo = (href, tocItem, manifest) => {
3425
3453
  const spineItemIndex = manifest.spineItems.findIndex(
3426
3454
  (item) => item.href === href
@@ -3696,14 +3724,14 @@ const paginationEnhancer = (next) => (options) => {
3696
3724
  const reader = next(options);
3697
3725
  const { paginationInfo$, getPaginationInfo } = trackPaginationInfo(reader);
3698
3726
  paginationInfo$.pipe(takeUntil(reader.$.destroy$)).subscribe();
3699
- const locate = createLocator(reader);
3727
+ const resourcesLocator = new ResourcesLocator(reader);
3700
3728
  return {
3701
3729
  ...reader,
3730
+ locateResource: resourcesLocator.locateResource.bind(resourcesLocator),
3702
3731
  pagination: {
3703
3732
  ...reader.pagination,
3704
3733
  getState: () => getPaginationInfo(),
3705
- state$: paginationInfo$,
3706
- locate
3734
+ state$: paginationInfo$
3707
3735
  }
3708
3736
  };
3709
3737
  };
@@ -4413,7 +4441,6 @@ const zoomEnhancer = (next) => (options) => {
4413
4441
  }
4414
4442
  };
4415
4443
  };
4416
- const getItemAnchor = (item) => `|[prose~anchor~${encodeURIComponent(item.index)}]`;
4417
4444
  const ELEMENT_NODE = Node.ELEMENT_NODE;
4418
4445
  const TEXT_NODE = Node.TEXT_NODE;
4419
4446
  const CDATA_SECTION_NODE = Node.CDATA_SECTION_NODE;
@@ -5306,10 +5333,6 @@ const generateCfi = (node, offset, item) => {
5306
5333
  `${getItemAnchor(item)}|${offsetAnchor}`
5307
5334
  );
5308
5335
  };
5309
- const getRootCfi = (spineItem) => {
5310
- const itemAnchor = getItemAnchor(spineItem.item);
5311
- return `epubcfi(/0${itemAnchor})`;
5312
- };
5313
5336
  const generateCfiForSpineItemPage = Report.measurePerformance(
5314
5337
  `getCfi`,
5315
5338
  10,