@prose-reader/core 1.192.0 → 1.193.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.
@@ -902,7 +902,6 @@
902
902
  constructor(params) {
903
903
  super();
904
904
  this.triggerSubject = new rxjs.Subject();
905
- this.lastLayoutDims = void 0;
906
905
  this.stateSubject = new rxjs.BehaviorSubject(`idle`);
907
906
  this.unload$ = this.triggerSubject.pipe(
908
907
  rxjs.withLatestFrom(this.stateSubject),
@@ -1034,57 +1033,8 @@
1034
1033
  })
1035
1034
  );
1036
1035
  }
1037
- /**
1038
- * @important
1039
- *
1040
- * If renderer returns undefined as dimensions we will use the previous
1041
- * layout dimensions as fallback. This ensure minmum layout shift during
1042
- * load / unload of items and improve stability..
1043
- */
1044
1036
  layout(params) {
1045
- return rxjs.defer(() => this.onLayout(params)).pipe(
1046
- rxjs.map((dims) => {
1047
- var _a, _b, _c, _d;
1048
- const { height: defaultHeight, width: defaultWidth } = this.context.getPageSize();
1049
- if (dims) {
1050
- const { height, width } = dims;
1051
- if (height < defaultHeight || width < defaultWidth) {
1052
- Report.warn(
1053
- `Your height or width is smaller than the page size. Please check your rendering.`
1054
- );
1055
- }
1056
- this.lastLayoutDims = {
1057
- height,
1058
- width,
1059
- pageSize: this.context.getPageSize()
1060
- };
1061
- return this.lastLayoutDims;
1062
- }
1063
- const hasPageSizeChanged = ((_a = this.lastLayoutDims) == null ? void 0 : _a.pageSize.width) !== this.context.getPageSize().width || ((_b = this.lastLayoutDims) == null ? void 0 : _b.pageSize.height) !== this.context.getPageSize().height;
1064
- if (this.renditionLayout === `pre-paginated` || // in the case the page size change, we cannot use the old size reliably
1065
- // we have to drop it and use the default one
1066
- hasPageSizeChanged) {
1067
- this.lastLayoutDims = {
1068
- height: this.context.getPageSize().height,
1069
- width: params.minimumWidth,
1070
- pageSize: this.context.getPageSize()
1071
- };
1072
- } else {
1073
- this.lastLayoutDims = {
1074
- height: Math.max(
1075
- defaultHeight,
1076
- ((_c = this.lastLayoutDims) == null ? void 0 : _c.height) ?? defaultHeight
1077
- ),
1078
- width: Math.max(
1079
- defaultWidth,
1080
- ((_d = this.lastLayoutDims) == null ? void 0 : _d.width) ?? defaultWidth
1081
- ),
1082
- pageSize: this.context.getPageSize()
1083
- };
1084
- }
1085
- return this.lastLayoutDims;
1086
- })
1087
- );
1037
+ return rxjs.defer(() => this.onLayout(params)).pipe();
1088
1038
  }
1089
1039
  destroy() {
1090
1040
  super.destroy();
@@ -8389,6 +8339,125 @@
8389
8339
  this.layoutSubject.complete();
8390
8340
  }
8391
8341
  }
8342
+ class SpineItemLayout extends DestroyableClass {
8343
+ constructor(item, containerElement, context, hookManager, renderer) {
8344
+ super();
8345
+ this.item = item;
8346
+ this.containerElement = containerElement;
8347
+ this.context = context;
8348
+ this.hookManager = hookManager;
8349
+ this.renderer = renderer;
8350
+ this.layoutTriggerSubject = new rxjs.Subject();
8351
+ this.lastLayout = null;
8352
+ this.applyDimsAfterLayout = ({
8353
+ blankPagePosition,
8354
+ minimumWidth
8355
+ }) => (stream) => {
8356
+ return stream.pipe(
8357
+ rxjs.map((dims) => {
8358
+ var _a;
8359
+ const trustableLastLayout = shared.isShallowEqual(
8360
+ (_a = this.lastLayout) == null ? void 0 : _a.pageSize,
8361
+ this.context.getPageSize()
8362
+ ) ? this.lastLayout : void 0;
8363
+ const { width: previousWidth, height: previousHeight } = trustableLastLayout ?? {};
8364
+ const { width = previousWidth, height = previousHeight } = dims ?? {};
8365
+ const { width: pageSizeWidth, height: pageSizeHeight } = this.context.getPageSize();
8366
+ const safeWidth = this.validateDimension(
8367
+ width ?? pageSizeWidth,
8368
+ pageSizeWidth,
8369
+ minimumWidth
8370
+ );
8371
+ const safeHeight = this.validateDimension(
8372
+ height ?? pageSizeHeight,
8373
+ pageSizeHeight,
8374
+ pageSizeHeight
8375
+ );
8376
+ this.lastLayout = {
8377
+ width: safeWidth,
8378
+ height: safeHeight,
8379
+ pageSize: this.context.getPageSize()
8380
+ };
8381
+ this.containerElement.style.width = `${safeWidth}px`;
8382
+ this.containerElement.style.height = `${safeHeight}px`;
8383
+ this.hookManager.execute(`item.onAfterLayout`, void 0, {
8384
+ blankPagePosition,
8385
+ item: this.item,
8386
+ minimumWidth
8387
+ });
8388
+ return { width: safeWidth, height: safeHeight };
8389
+ })
8390
+ );
8391
+ };
8392
+ this.layout = (params) => {
8393
+ const nextResult = deferNextResult(this.layout$.pipe(rxjs.first()));
8394
+ this.layoutTriggerSubject.next(params);
8395
+ return nextResult();
8396
+ };
8397
+ this.adjustPositionOfElement = ({
8398
+ right,
8399
+ left,
8400
+ top
8401
+ }) => {
8402
+ if (right !== void 0) {
8403
+ this.containerElement.style.right = `${right}px`;
8404
+ } else {
8405
+ this.containerElement.style.removeProperty(`right`);
8406
+ }
8407
+ if (left !== void 0) {
8408
+ this.containerElement.style.left = `${left}px`;
8409
+ } else {
8410
+ this.containerElement.style.removeProperty(`left`);
8411
+ }
8412
+ if (top !== void 0) {
8413
+ this.containerElement.style.top = `${top}px`;
8414
+ } else {
8415
+ this.containerElement.style.removeProperty(`top`);
8416
+ }
8417
+ };
8418
+ this.layoutProcess$ = this.layoutTriggerSubject.pipe(
8419
+ rxjs.switchMap((params) => {
8420
+ const { blankPagePosition, minimumWidth, spreadPosition } = params;
8421
+ this.hookManager.execute(`item.onBeforeLayout`, void 0, {
8422
+ blankPagePosition,
8423
+ item: this.item,
8424
+ minimumWidth
8425
+ });
8426
+ const rendererLayout$ = this.renderer.layout({
8427
+ blankPagePosition,
8428
+ minPageSpread: minimumWidth / this.context.getPageSize().width,
8429
+ minimumWidth,
8430
+ spreadPosition
8431
+ });
8432
+ return rxjs.merge(
8433
+ rxjs.of({ type: "start" }),
8434
+ rendererLayout$.pipe(
8435
+ this.applyDimsAfterLayout(params),
8436
+ rxjs.map(
8437
+ (data) => ({
8438
+ type: "end",
8439
+ data
8440
+ })
8441
+ )
8442
+ )
8443
+ );
8444
+ }),
8445
+ rxjs.share()
8446
+ );
8447
+ this.layout$ = this.layoutProcess$.pipe(
8448
+ rxjs.filter((event) => event.type === `end`),
8449
+ rxjs.map((event) => event.data),
8450
+ rxjs.share()
8451
+ );
8452
+ }
8453
+ validateDimension(value, pageSize, minimum) {
8454
+ if (value <= 0) return minimum;
8455
+ const maxValue = Math.max(value, minimum);
8456
+ const multiplier = Math.ceil(maxValue / pageSize);
8457
+ const adjustedValue = multiplier * pageSize;
8458
+ return Math.max(adjustedValue, pageSize);
8459
+ }
8460
+ }
8392
8461
  class DefaultRenderer extends DocumentRenderer {
8393
8462
  onUnload() {
8394
8463
  return rxjs.EMPTY;
@@ -8419,28 +8488,6 @@
8419
8488
  this.settings = settings;
8420
8489
  this.hookManager = hookManager;
8421
8490
  this.index = index;
8422
- this.layoutTriggerSubject = new rxjs.Subject();
8423
- this.adjustPositionOfElement = ({
8424
- right,
8425
- left,
8426
- top
8427
- }) => {
8428
- if (right !== void 0) {
8429
- this.containerElement.style.right = `${right}px`;
8430
- } else {
8431
- this.containerElement.style.removeProperty(`right`);
8432
- }
8433
- if (left !== void 0) {
8434
- this.containerElement.style.left = `${left}px`;
8435
- } else {
8436
- this.containerElement.style.removeProperty(`left`);
8437
- }
8438
- if (top !== void 0) {
8439
- this.containerElement.style.top = `${top}px`;
8440
- } else {
8441
- this.containerElement.style.removeProperty(`top`);
8442
- }
8443
- };
8444
8491
  this.getBoundingRectOfElementFromSelector = (selector) => {
8445
8492
  var _a2, _b2, _c, _d;
8446
8493
  const frameElement = this.renderer.getDocumentFrame();
@@ -8451,11 +8498,6 @@
8451
8498
  return (_d = (_c = frameElement.contentDocument) == null ? void 0 : _c.querySelector(selector)) == null ? void 0 : _d.getBoundingClientRect();
8452
8499
  }
8453
8500
  };
8454
- this.layout = (params) => {
8455
- const nextResult = deferNextResult(this.layout$.pipe(operators.first()));
8456
- this.layoutTriggerSubject.next(params);
8457
- return nextResult();
8458
- };
8459
8501
  this.load = () => {
8460
8502
  this.renderer.load();
8461
8503
  };
@@ -8499,52 +8541,21 @@
8499
8541
  resourcesHandler: this.resourcesHandler
8500
8542
  };
8501
8543
  this.renderer = rendererFactory ? rendererFactory(rendererParams) : new DefaultRenderer(rendererParams);
8502
- const layoutProcess$ = this.layoutTriggerSubject.pipe(
8503
- operators.switchMap(({ blankPagePosition, minimumWidth, spreadPosition }) => {
8504
- this.hookManager.execute(`item.onBeforeLayout`, void 0, {
8505
- blankPagePosition,
8506
- item: this.item,
8507
- minimumWidth
8508
- });
8509
- const layout$ = this.renderer.layout({
8510
- blankPagePosition,
8511
- minPageSpread: minimumWidth / this.context.getPageSize().width,
8512
- minimumWidth,
8513
- spreadPosition
8514
- });
8515
- return rxjs.merge(
8516
- rxjs.of({ type: "start" }),
8517
- layout$.pipe(
8518
- operators.map(({ height, width }) => {
8519
- this.containerElement.style.width = `${width}px`;
8520
- this.containerElement.style.height = `${height}px`;
8521
- this.hookManager.execute(`item.onAfterLayout`, void 0, {
8522
- blankPagePosition,
8523
- item: this.item,
8524
- minimumWidth
8525
- });
8526
- return {
8527
- type: "end",
8528
- data: { width, height }
8529
- };
8530
- })
8531
- )
8532
- );
8533
- }),
8534
- operators.share()
8544
+ this.spineItemLayout = new SpineItemLayout(
8545
+ item,
8546
+ this.containerElement,
8547
+ context,
8548
+ hookManager,
8549
+ this.renderer
8535
8550
  );
8536
- this.isReady$ = layoutProcess$.pipe(
8551
+ this.isReady$ = this.spineItemLayout.layoutProcess$.pipe(
8537
8552
  operators.withLatestFrom(this.renderer.isLoaded$),
8538
8553
  operators.map(([event, loaded]) => !!(event.type === `end` && loaded)),
8539
8554
  operators.startWith(false),
8540
8555
  operators.distinctUntilChanged(),
8541
8556
  operators.shareReplay({ refCount: true, bufferSize: 1 })
8542
8557
  );
8543
- this.layout$ = layoutProcess$.pipe(
8544
- operators.filter((event) => event.type === `end`),
8545
- operators.map((event) => event.data),
8546
- operators.share()
8547
- );
8558
+ this.layout$ = this.spineItemLayout.layout$;
8548
8559
  this.needsLayout$ = rxjs.merge(this.unloaded$, this.loaded$);
8549
8560
  rxjs.merge(
8550
8561
  /**
@@ -8554,8 +8565,10 @@
8554
8565
  * to layout changes may rely on the isReady value.
8555
8566
  */
8556
8567
  this.isReady$,
8557
- this.layout$
8568
+ this.spineItemLayout.layout$
8558
8569
  ).pipe(operators.takeUntil(this.destroy$)).subscribe();
8570
+ this.layout = this.spineItemLayout.layout;
8571
+ this.adjustPositionOfElement = this.spineItemLayout.adjustPositionOfElement;
8559
8572
  }
8560
8573
  get element() {
8561
8574
  return this.containerElement;