html-flip-book-vanilla 0.0.0-alpha.47 → 0.0.0-alpha.48

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.
@@ -62,6 +62,7 @@ declare class FlipBook {
62
62
  getMouseMode(): MouseMode;
63
63
  setMouseMode(mode: MouseMode): void;
64
64
  private handlePageMouseDown;
65
+ private handlePageDragStart;
65
66
  getDownloadConfig(): DownloadConfig | undefined;
66
67
  private buildCoverPageIndicesSet;
67
68
  private recalculateSizes;
@@ -1 +1 @@
1
- {"version":3,"file":"flipbook.d.ts","sourceRoot":"","sources":["../src/flipbook.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAC;AACtB,OAAO,iBAAiB,CAAC;AACzB,OAAO,mBAAmB,CAAC;AAI3B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,KAAK,EACX,eAAe,EAGf,SAAS,EAGT,MAAM,qBAAqB,CAAC;AAG7B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AA8CtD,cAAM,QAAQ;IACb,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,OAAO,CAAC,YAAY,CAAqB;IACzC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAwC;IACxE,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAG/B;IACF,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAwB;IAClD,OAAO,CAAC,SAAS,CAAqB;IACtC,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAA8B;IACjE,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA0B;IAC9D,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC,CAA8B;IAC7D,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAmC;IACnE,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAmC;IAClE,OAAO,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AAS,EAAE,MAAM,GAAG,IAAI;IAiFnC,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAcpC,OAAO,CAAC,kBAAkB;IAQ1B,OAAO;CAiCP;AAED,OAAO,EAAE,QAAQ,EAAE,KAAK,aAAa,EAAE,CAAC;AACxC,YAAY,EACX,gBAAgB,EAChB,aAAa,EACb,SAAS,EACT,iBAAiB,EACjB,cAAc,GACd,MAAM,qBAAqB,CAAC"}
package/dist/flipbook.js CHANGED
@@ -342,7 +342,7 @@ class w {
342
342
  return `${this.width}x${this.height}`;
343
343
  }
344
344
  }
345
- const z = 500, V = 0.18, X = 0.12, Z = 16, W = Date.now().toString(36);
345
+ const z = 500, V = 0.18, Z = 0.12, X = 16, W = Date.now().toString(36);
346
346
  let j = 0;
347
347
  class x {
348
348
  bookElement;
@@ -466,6 +466,10 @@ class x {
466
466
  handlePageMouseDown = (t) => {
467
467
  this._suppressNextClick = !1, (t.type === "mousedown" || t.pointerType === "mouse") && (this.gestureStartedInNoFlipZone = this.isInsideNoFlipZone(t.target), this.gestureStartedInNoFlipZone && this.clearHoverShadow()), this.mouseMode === "select" && t instanceof MouseEvent && t.button === 0 && (t.type !== "pointerdown" || t.pointerType === "mouse") && t.stopPropagation();
468
468
  };
469
+ /** Keep native image/link dragging from taking over a page-turn gesture. */
470
+ handlePageDragStart = (t) => {
471
+ this.mouseMode === "turn" && !this.isInsideNoFlipZone(t.target) && t.preventDefault();
472
+ };
469
473
  /** Download config (handlers and filename hints). Used by toolbar. */
470
474
  getDownloadConfig() {
471
475
  return this.downloadConfig;
@@ -510,7 +514,7 @@ class x {
510
514
  throw new Error("No pages found in flipbook");
511
515
  this.pageElements = Array.from(n);
512
516
  for (const h of this.pageElements)
513
- h.addEventListener("pointerdown", this.handlePageMouseDown), h.addEventListener("mousedown", this.handlePageMouseDown);
517
+ h.addEventListener("pointerdown", this.handlePageMouseDown), h.addEventListener("mousedown", this.handlePageMouseDown), h.addEventListener("dragstart", this.handlePageDragStart, !0);
514
518
  this.leaves.splice(0, this.leaves.length);
515
519
  const s = Math.ceil(this.pagesCount / 2), o = this.buildCoverPageIndicesSet();
516
520
  this.pageElements.forEach((h, a) => {
@@ -755,7 +759,7 @@ class x {
755
759
  }
756
760
  this.touchDirectionLock === "horizontal" && t.preventDefault();
757
761
  };
758
- handleMouseMove = k(Z, (t) => {
762
+ handleMouseMove = k(X, (t) => {
759
763
  if (!this.bookElement) return;
760
764
  if (this.mouseMode === "select" || t.buttons !== 0 || this.isDragging || this.currentManualFlip || this.activeFlips.size > 0) {
761
765
  this.clearHoverShadow();
@@ -776,7 +780,7 @@ class x {
776
780
  this.clearHoverShadow();
777
781
  return;
778
782
  }
779
- const F = (1 - Math.min(1, m / u)) * X;
783
+ const F = (1 - Math.min(1, m / u)) * Z;
780
784
  this.hoveredLeaf && this.hoveredLeaf !== a && this.hoveredLeaf.setHoverShadow(0), this.hoveredLeaf !== a && a.setFoldOrigin((n - d) / l), this.hoveredLeaf = a, a.setHoverShadow(F);
781
785
  });
782
786
  handleMouseLeave = () => {
@@ -972,7 +976,7 @@ class x {
972
976
  destroy() {
973
977
  if (typeof window < "u" && this._boundPopstate && (window.removeEventListener("popstate", this._boundPopstate, !0), this._boundPopstate = void 0), this.resizeObserver && (this.resizeObserver.disconnect(), this.resizeObserver = void 0), this.hammer && (this.hammer.destroy(), this.hammer = void 0), this.bookElement) {
974
978
  for (const t of this.pageElements)
975
- t.removeEventListener("pointerdown", this.handlePageMouseDown), t.removeEventListener("mousedown", this.handlePageMouseDown);
979
+ t.removeEventListener("pointerdown", this.handlePageMouseDown), t.removeEventListener("mousedown", this.handlePageMouseDown), t.removeEventListener("dragstart", this.handlePageDragStart, !0);
976
980
  this.bookElement.removeEventListener("touchstart", this.handleTouchStart), this.bookElement.removeEventListener("touchmove", this.handleTouchMove), this.bookElement.removeEventListener(
977
981
  "mousemove",
978
982
  this.handleMouseMove
@@ -1 +1 @@
1
- {"version":3,"file":"flipbook.js","sources":["../src/flip-direction.ts","../src/page-fold.ts","../src/leaf.ts","../src/aspect-ratio.ts","../src/size.ts","../src/flipbook.ts"],"sourcesContent":["export enum FlipDirection {\n\tForward = \"Forward\",\n\tBackward = \"Backward\",\n\tNone = \"None\",\n}\n","export interface Point {\n\tx: number;\n\ty: number;\n}\n\nexport interface PageFold {\n\tdestination: Point;\n\tfront: Point[];\n\tfolded: Point[];\n\treflection: [number, number, number, number, number, number];\n\tnormal: Point;\n\tmidpoint: Point;\n\tstrength: number;\n}\n\nfunction foldStrength(position: number): number {\n\treturn position === 0 || position === 1 ? 0 : Math.sin(Math.PI * position);\n}\n\nfunction clamp(value: number, lower: number, upper: number): number {\n\treturn Math.max(lower, Math.min(upper, value));\n}\n\nfunction progress(value: number): number {\n\treturn Number.isNaN(value) ? 0 : clamp(value, 0, 1);\n}\n\nfunction dimension(value: number): number {\n\treturn Number.isFinite(value) && value > 0 ? Math.min(value, Number.MAX_SAFE_INTEGER) : 1;\n}\n\nfunction clipAtCrease(polygon: Point[], normal: Point, midpoint: Point, side: number): Point[] {\n\tconst clipped: Point[] = [];\n\tconst distance = (point: Point) =>\n\t\tside * ((point.x - midpoint.x) * normal.x + (point.y - midpoint.y) * normal.y);\n\tlet previous = polygon[polygon.length - 1];\n\tlet previousDistance = distance(previous);\n\n\tfor (const current of polygon) {\n\t\tconst currentDistance = distance(current);\n\t\tconst previousInside = previousDistance >= 0;\n\t\tconst currentInside = currentDistance >= 0;\n\t\tif (previousInside !== currentInside) {\n\t\t\tconst fraction = previousDistance / (previousDistance - currentDistance);\n\t\t\tclipped.push({\n\t\t\t\tx: previous.x + (current.x - previous.x) * fraction,\n\t\t\t\ty: previous.y + (current.y - previous.y) * fraction,\n\t\t\t});\n\t\t}\n\t\tif (currentInside) clipped.push(current);\n\t\tprevious = current;\n\t\tpreviousDistance = currentDistance;\n\t}\n\n\tconst unique = clipped.filter((point, index) => {\n\t\tconst next = clipped[(index + 1) % clipped.length];\n\t\treturn point.x !== next.x || point.y !== next.y;\n\t});\n\treturn unique.length < 3 ? [] : unique;\n}\n\nfunction constrainDestination(\n\twidth: number,\n\theight: number,\n\ttarget: Point,\n\tcorner: \"top\" | \"bottom\",\n): Point {\n\tconst cornerY = corner === \"top\" ? 0 : height;\n\tconst destination = {\n\t\tx: Number.isFinite(target.x) ? target.x : width,\n\t\ty: Number.isFinite(target.y) ? target.y : cornerY,\n\t};\n\tconst anchors = [\n\t\t{ x: 0, y: cornerY, radius: width },\n\t\t{ x: 0, y: height - cornerY, radius: Math.hypot(width, height) },\n\t];\n\tconst tolerance = Number.EPSILON * Math.max(width, height, 1) * 8;\n\tconst inside = (point: Point, anchor: (typeof anchors)[number], allowedError = tolerance) =>\n\t\tMath.hypot(point.x - anchor.x, point.y - anchor.y) <= anchor.radius + allowedError;\n\tif (anchors.every((anchor) => inside(destination, anchor, 0))) return destination;\n\n\t// A closest point lies on an admissible circle arc or at one of their intersections.\n\tconst candidates = [\n\t\t{ x: width, y: cornerY },\n\t\t{ x: -width, y: cornerY },\n\t];\n\tfor (const anchor of anchors) {\n\t\tconst dx = destination.x - anchor.x;\n\t\tconst dy = destination.y - anchor.y;\n\t\tconst scale = Math.max(Math.abs(dx), Math.abs(dy));\n\t\tif (scale === 0) continue;\n\t\tconst length = Math.hypot(dx / scale, dy / scale);\n\t\tconst candidate = {\n\t\t\tx: anchor.x + ((dx / scale) * anchor.radius) / length,\n\t\t\ty: anchor.y + ((dy / scale) * anchor.radius) / length,\n\t\t};\n\t\tif (anchors.every((other) => inside(candidate, other))) candidates.push(candidate);\n\t}\n\tconst scale = Math.max(width, height, Math.abs(destination.x), Math.abs(destination.y), 1);\n\t// Remove the common target-distance term and scale before comparing large requests.\n\tconst score = (point: Point) =>\n\t\t(point.x * point.x + point.y * point.y) / scale -\n\t\t2 * ((destination.x / scale) * point.x + (destination.y / scale) * point.y);\n\treturn candidates.reduce((nearest, candidate) =>\n\t\tscore(candidate) < score(nearest) ? candidate : nearest,\n\t);\n}\n\n/** Return the lifted corner's constrained position for an automatic turn. */\nexport function getPageFoldDestination(\n\twidth: number,\n\theight: number,\n\tposition: number,\n\tcorner: \"top\" | \"bottom\",\n\tpointerOffset = 0,\n): Point {\n\tconst w = dimension(width);\n\tconst h = dimension(height);\n\tconst p = progress(position);\n\tconst strength = foldStrength(p);\n\tconst inward = corner === \"top\" ? 1 : -1;\n\tconst cornerY = corner === \"top\" ? 0 : h;\n\tconst offset = Number.isFinite(pointerOffset) ? pointerOffset : 0;\n\tconst travel = clamp(\n\t\tstrength * Math.min(h * 0.18, w * 0.28) + inward * offset * strength * h,\n\t\t0,\n\t\th * 0.6,\n\t);\n\treturn constrainDestination(w, h, { x: w * (1 - 2 * p), y: cornerY + inward * travel }, corner);\n}\n\n/** Reflect a lifted corner toward a real pointer destination, with the entire spine fixed. */\nexport function computePageFoldFromPoint(\n\twidth: number,\n\theight: number,\n\ttarget: Point,\n\tcorner: \"top\" | \"bottom\",\n): PageFold {\n\tconst w = dimension(width);\n\tconst h = dimension(height);\n\tconst cornerY = corner === \"top\" ? 0 : h;\n\tconst destination = constrainDestination(w, h, target, corner);\n\n\tconst deltaX = w - destination.x;\n\tconst deltaY = cornerY - destination.y;\n\tconst separation = Math.hypot(deltaX, deltaY);\n\tconst normal =\n\t\tseparation === 0 ? { x: 1, y: 0 } : { x: deltaX / separation, y: deltaY / separation };\n\tconst midpoint = {\n\t\tx: (w + destination.x) / 2,\n\t\ty: (cornerY + destination.y) / 2,\n\t};\n\tconst projection = midpoint.x * normal.x + midpoint.y * normal.y;\n\tconst reflection: PageFold[\"reflection\"] = [\n\t\t1 - 2 * normal.x * normal.x,\n\t\t-2 * normal.x * normal.y,\n\t\t-2 * normal.x * normal.y,\n\t\t1 - 2 * normal.y * normal.y,\n\t\t2 * normal.x * projection,\n\t\t2 * normal.y * projection,\n\t];\n\tconst sheet = [\n\t\t{ x: 0, y: 0 },\n\t\t{ x: w, y: 0 },\n\t\t{ x: w, y: h },\n\t\t{ x: 0, y: h },\n\t];\n\treturn {\n\t\tdestination,\n\t\tfront: clipAtCrease(sheet, normal, midpoint, -1),\n\t\tfolded: clipAtCrease(sheet, normal, midpoint, 1),\n\t\treflection,\n\t\tnormal,\n\t\tmidpoint,\n\t\tstrength: foldStrength(progress((w - destination.x) / (2 * w))),\n\t};\n}\n\n/** Reflect the lifted region across a crease in coordinates with the spine at x = 0. */\nexport function computePageFold(\n\twidth: number,\n\theight: number,\n\tposition: number,\n\tcorner: \"top\" | \"bottom\",\n\tpointerOffset = 0,\n): PageFold {\n\tconst destination = getPageFoldDestination(width, height, position, corner, pointerOffset);\n\treturn {\n\t\t...computePageFoldFromPoint(width, height, destination, corner),\n\t\tstrength: foldStrength(progress(position)),\n\t};\n}\n\n/** Match the resting speed at each end while maintaining an exact final position. */\nexport function easePageTurn(value: number): number {\n\tconst p = progress(value);\n\treturn p === 0 || p === 1 ? p : (1 - Math.cos(Math.PI * p)) / 2;\n}\n","import { throttle } from \"throttle-debounce\";\nimport type { IntRange } from \"type-fest\";\nimport { FlipDirection } from \"./flip-direction\";\nimport {\n\tcomputePageFold,\n\tcomputePageFoldFromPoint,\n\teasePageTurn,\n\ttype PageFold,\n\ttype Point,\n} from \"./page-fold\";\n\n/** Schedule a frame callback; fallback for test envs where requestAnimationFrame is missing. Called at invoke time so test mocks apply. */\nfunction scheduleFrame(cb: (timestamp: number) => void): number {\n\treturn typeof requestAnimationFrame !== \"undefined\"\n\t\t? requestAnimationFrame(cb)\n\t\t: (setTimeout(() => cb(performance.now()), 0) as unknown as number);\n}\n\n/** Multiplier for shadow intensity (slightly stronger than base) */\nconst SHADOW_STRENGTH_MULTIPLIER = 1.1;\n/** Multiplier for highlight intensity (slightly weaker than base) */\nconst HIGHLIGHT_STRENGTH_MULTIPLIER = 0.9;\n\n// number between 1 to infinity\nexport type DegreesPerSecond = IntRange<1, typeof Infinity>;\nexport type FlipPosition = IntRange<0, 2>;\nexport const FLIPPED = true;\nexport const NOT_FLIPPED = false;\nexport class Leaf {\n\tprivate currentAnimation: Promise<void> | null = null;\n\tprivate targetFlipPosition: FlipPosition | null = null;\n\tprivate wrappedFlipPosition: number;\n\tprivate hoverShadow = 0;\n\t/** Ref-count for nested beginFlip/endFlip pairs (drag wrapping animation). */\n\tprivate flipRefCount = 0;\n\tprivate foldOrigin = 0.75;\n\tprivate foldPointer = 0.75;\n\tprivate manualNeedsSettlement = false;\n\tprivate manualFoldPoint: Point | undefined;\n\n\tsetFoldOrigin(normalizedY: number): void {\n\t\tthis.foldOrigin = Math.max(0, Math.min(1, normalizedY));\n\t\tthis.foldPointer = this.foldOrigin;\n\t}\n\n\tsetFoldPointer(normalizedY: number): void {\n\t\tthis.foldPointer = Math.max(0, Math.min(1, normalizedY));\n\t}\n\n\t/** Follow a held pointer without queuing animations or reporting a completed turn. */\n\tsetManualPosition(position: number): void {\n\t\tthis.manualFoldPoint = undefined;\n\t\tconst previous = this.flipPosition;\n\t\tthis.updatePosition(position);\n\t\tthis.manualNeedsSettlement ||= previous !== this.flipPosition;\n\t\tthis.applyTransform(this.flipPosition);\n\t}\n\n\t/** Track the lifted corner in page coordinates, including movement beyond the page edges. */\n\tsetManualFold(point: Point): void {\n\t\tconst size = this.getPaperSize();\n\t\tif (!size) {\n\t\t\tthis.setManualPosition(Number.isFinite(point.x) ? (1 - point.x) / 2 : this.flipPosition);\n\t\t\treturn;\n\t\t}\n\t\tconst fold = computePageFoldFromPoint(\n\t\t\tsize.width,\n\t\t\tsize.height,\n\t\t\t{ x: point.x * size.width, y: point.y * size.height },\n\t\t\tthis.foldOrigin < 0.5 ? \"top\" : \"bottom\",\n\t\t);\n\t\tthis.updateManualFold({\n\t\t\tx: fold.destination.x / size.width,\n\t\t\ty: fold.destination.y / size.height,\n\t\t});\n\t}\n\n\tprivate updateManualFold(point: Point): void {\n\t\tconst previousPosition = this.flipPosition;\n\t\tconst previousPoint = this.manualFoldPoint;\n\t\tthis.updatePosition((1 - point.x) / 2);\n\t\tthis.manualFoldPoint = this.flipPosition === 0 || this.flipPosition === 1 ? undefined : point;\n\t\tthis.manualNeedsSettlement ||=\n\t\t\tpreviousPosition !== this.flipPosition ||\n\t\t\t(previousPoint !== undefined && previousPoint.y !== point.y);\n\t\tthis.applyTransform(this.flipPosition);\n\t}\n\n\tconstructor(\n\t\treadonly index: number,\n\t\treadonly pages: [HTMLElement, HTMLElement | undefined],\n\t\tisFlipped: boolean,\n\t\tprivate readonly bookProperties: {\n\t\t\tisLTR: boolean;\n\t\t\tpagesCount: number;\n\t\t\tleavesCount: number;\n\t\t\tpageShadow: boolean;\n\t\t\tsnapshotDuringFlip: boolean;\n\t\t},\n\t\tprivate readonly onTurned: (direction: FlipDirection) => void,\n\t) {\n\t\tthis.wrappedFlipPosition = isFlipped ? 1 : 0;\n\t\t// TODO: rethink this\n\t\t// if(isFlipped) {\n\t\t// // this.flipToPosition(1);\n\t\t// }\n\t}\n\n\tget isTurned(): boolean {\n\t\treturn this.flipPosition === 1;\n\t}\n\tget isTurning(): boolean {\n\t\treturn this.flipPosition !== 0;\n\t}\n\tget isCover(): boolean {\n\t\treturn this.isFirst || this.isLast;\n\t}\n\tget isFirst(): boolean {\n\t\treturn this.index === 0;\n\t}\n\tget isLast(): boolean {\n\t\treturn this.index === this.bookProperties.leavesCount - 1;\n\t}\n\tset flipPosition(value: number) {\n\t\tthis.updatePosition(value);\n\t\tif (this.flipPosition === 0 || this.flipPosition === 1) {\n\t\t\tthis.manualNeedsSettlement = false;\n\t\t}\n\t}\n\tprivate updatePosition(value: number): void {\n\t\tthis.wrappedFlipPosition = Math.max(0, Math.min(1, value)) as FlipPosition;\n\t\tif (this.wrappedFlipPosition === 0 || this.wrappedFlipPosition === 1) {\n\t\t\tthis.manualFoldPoint = undefined;\n\t\t}\n\t}\n\tget flipPosition(): number {\n\t\treturn this.wrappedFlipPosition;\n\t}\n\n\tsetHoverShadow(progress: number): void {\n\t\tconst clamped = Math.max(0, Math.min(1, progress));\n\t\tif (this.hoverShadow === clamped) return;\n\t\tthis.hoverShadow = clamped;\n\t\tthis.applyTransform(this.flipPosition);\n\t}\n\n\t/**\n\t * Signal that a flip interaction is starting (drag or animation).\n\t * Ref-counted: the snapshot class is added on the first call and\n\t * removed only when every `beginFlip` has a matching `endFlip`.\n\t * This keeps the GPU layer and scrollbar/selection lockdown stable\n\t * across the full drag→release→animation cycle.\n\t */\n\tbeginFlip(): void {\n\t\tthis.flipRefCount++;\n\t\tif (this.flipRefCount !== 1) return; // already promoted\n\t\tfor (const page of this.pages) {\n\t\t\tif (!page) continue;\n\t\t\tpage.style.willChange = \"transform\";\n\t\t\tif (this.bookProperties.snapshotDuringFlip) {\n\t\t\t\tpage.classList.add(\"page--flipping\");\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Signal that a flip interaction finished (drag or animation).\n\t * Only releases the GPU layer when every `beginFlip` is balanced.\n\t */\n\tendFlip(): void {\n\t\tthis.flipRefCount = Math.max(0, this.flipRefCount - 1);\n\t\tif (this.flipRefCount !== 0) return; // still active\n\t\tif (!this.manualFoldPoint || this.flipPosition === 0 || this.flipPosition === 1) {\n\t\t\tthis.manualFoldPoint = undefined;\n\t\t\tthis.foldOrigin = 0.75;\n\t\t\tthis.foldPointer = this.foldOrigin;\n\t\t}\n\t\tfor (const page of this.pages) {\n\t\t\tif (!page) continue;\n\t\t\tpage.style.willChange = \"\";\n\t\t\tif (this.bookProperties.snapshotDuringFlip) {\n\t\t\t\tpage.classList.remove(\"page--flipping\");\n\t\t\t}\n\t\t}\n\t}\n\n\tasync flipToPosition(\n\t\tflipPosition: FlipPosition,\n\t\tvelocity: DegreesPerSecond = 225 as DegreesPerSecond,\n\t) {\n\t\tif (this.currentAnimation) {\n\t\t\tawait this.currentAnimation;\n\t\t}\n\n\t\tif (this.flipPosition === flipPosition) {\n\t\t\tif (flipPosition === 0 || flipPosition === 1) {\n\t\t\t\tthis.manualFoldPoint = undefined;\n\t\t\t\tthis.applyTransform(flipPosition);\n\t\t\t}\n\t\t\tif (this.manualNeedsSettlement && (flipPosition === 0 || flipPosition === 1)) {\n\t\t\t\tthis.manualNeedsSettlement = false;\n\t\t\t\tthis.onTurned(flipPosition === 1 ? FlipDirection.Forward : FlipDirection.Backward);\n\t\t\t}\n\t\t\treturn Promise.resolve();\n\t\t}\n\n\t\tif (this.targetFlipPosition === flipPosition) {\n\t\t\treturn this.currentAnimation ?? Promise.resolve();\n\t\t}\n\n\t\tthis.targetFlipPosition = flipPosition;\n\t\t// Promote to GPU layer before animation starts for smooth compositing\n\t\tthis.beginFlip();\n\n\t\tthis.currentAnimation = new Promise<void>((resolve) => {\n\t\t\tconst currentFlipPosition = this.flipPosition;\n\t\t\tconst startingFoldPoint = this.manualFoldPoint && { ...this.manualFoldPoint };\n\t\t\tconst targetFoldPoint = {\n\t\t\t\tx: 1 - 2 * flipPosition,\n\t\t\t\ty: this.foldOrigin < 0.5 ? 0 : 1,\n\t\t\t};\n\t\t\tconst distance = Math.abs(flipPosition - currentFlipPosition);\n\t\t\tconst duration = ((distance * 180) / velocity) * 1000; // duration in milliseconds\n\n\t\t\tconst start = performance.now();\n\t\t\tconst step = (timestamp: number) => {\n\t\t\t\tconst elapsed = timestamp - start;\n\n\t\t\t\tif (elapsed < 0) {\n\t\t\t\t\tscheduleFrame(step);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tconst progress = Math.min(elapsed / duration, 1);\n\t\t\t\tconst easedProgress = easePageTurn(progress);\n\t\t\t\tconst newPosition =\n\t\t\t\t\tcurrentFlipPosition + easedProgress * (flipPosition - currentFlipPosition);\n\t\t\t\tif (startingFoldPoint) {\n\t\t\t\t\tthis.manualFoldPoint = {\n\t\t\t\t\t\tx: startingFoldPoint.x + easedProgress * (targetFoldPoint.x - startingFoldPoint.x),\n\t\t\t\t\t\ty: startingFoldPoint.y + easedProgress * (targetFoldPoint.y - startingFoldPoint.y),\n\t\t\t\t\t};\n\t\t\t\t\tif (progress === 1 && (flipPosition === 0 || flipPosition === 1)) {\n\t\t\t\t\t\tthis.manualFoldPoint = undefined;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Ensure the new position is within valid bounds [0, 1]\n\t\t\t\tthis.flipPosition = Math.max(0, Math.min(1, newPosition)) as FlipPosition;\n\t\t\t\tthis.applyTransform(newPosition);\n\t\t\t\tif (progress < 1) {\n\t\t\t\t\tscheduleFrame(step);\n\t\t\t\t} else {\n\t\t\t\t\tthis.currentAnimation = null;\n\t\t\t\t\tthis.targetFlipPosition = null;\n\t\t\t\t\tthis.manualNeedsSettlement = false;\n\t\t\t\t\tif (this.flipPosition === 0 || this.flipPosition === 1) {\n\t\t\t\t\t\tthis.onTurned(this.flipPosition === 1 ? FlipDirection.Forward : FlipDirection.Backward);\n\t\t\t\t\t}\n\t\t\t\t\t// Release GPU layer after animation completes\n\t\t\t\t\tthis.endFlip();\n\t\t\t\t\tresolve();\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tscheduleFrame(step);\n\t\t});\n\n\t\treturn this.currentAnimation;\n\t}\n\n\tasync efficientFlipToPosition(\n\t\tflipPosition: FlipPosition,\n\t\tvelocity: DegreesPerSecond = 20000 as DegreesPerSecond,\n\t) {\n\t\treturn throttle(1, this.flipToPosition.bind(this))(flipPosition, velocity);\n\t}\n\n\tapplyTransform(position: number): void {\n\t\tconst clampedPosition = Math.max(0, Math.min(1, position));\n\t\tconst { pageShadow } = this.bookProperties;\n\t\tconst previewPosition =\n\t\t\tclampedPosition === 0\n\t\t\t\t? this.hoverShadow * 0.22\n\t\t\t\t: clampedPosition === 1\n\t\t\t\t\t? 1 - this.hoverShadow * 0.22\n\t\t\t\t\t: clampedPosition;\n\t\tconst width = Number.parseFloat(this.pages[0].style.width);\n\t\tconst height = Number.parseFloat(this.pages[0].style.height);\n\t\tconst paperSize = this.getPaperSize();\n\t\tif (paperSize && this.manualFoldPoint) {\n\t\t\tconst fold = computePageFoldFromPoint(\n\t\t\t\twidth,\n\t\t\t\theight,\n\t\t\t\t{ x: this.manualFoldPoint.x * width, y: this.manualFoldPoint.y * height },\n\t\t\t\tthis.foldOrigin < 0.5 ? \"top\" : \"bottom\",\n\t\t\t);\n\t\t\tthis.manualFoldPoint = { x: fold.destination.x / width, y: fold.destination.y / height };\n\t\t\tthis.updatePosition((1 - this.manualFoldPoint.x) / 2);\n\t\t\tif (this.flipPosition > 0 && this.flipPosition < 1) {\n\t\t\t\tthis.applyPaperFold(width, height, this.flipPosition, fold);\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t\tconst usePaperFold = paperSize && previewPosition > 0 && previewPosition < 1;\n\t\tif (usePaperFold) {\n\t\t\tthis.applyPaperFold(width, height, previewPosition, undefined, clampedPosition === 1);\n\t\t\treturn;\n\t\t}\n\n\t\t// Only compute shadow values when shadow is enabled — skipping these\n\t\t// saves CSS custom-property mutations per page per animation frame.\n\t\tlet shadowStrength = 0;\n\t\tlet highlightStrength = 0;\n\t\tif (pageShadow) {\n\t\t\tconst shadowFromFlip = Math.sin(clampedPosition * Math.PI);\n\t\t\tconst shadowProgress = Math.max(shadowFromFlip, this.hoverShadow);\n\t\t\tshadowStrength = Math.min(1, shadowProgress * SHADOW_STRENGTH_MULTIPLIER);\n\t\t\thighlightStrength = Math.min(1, shadowProgress * HIGHLIGHT_STRENGTH_MULTIPLIER);\n\t\t}\n\n\t\tthis.pages.forEach((page, index) => {\n\t\t\tconst isLTR = this.bookProperties.isLTR;\n\t\t\tif (!page) return;\n\t\t\tpage.classList.remove(\"page--folded\");\n\t\t\tpage.style.clipPath = \"\";\n\t\t\tpage.style.removeProperty(\"--fold-shading\");\n\n\t\t\tconst isOdd = (index % 2) + 1 === 1;\n\t\t\tconst degrees = isOdd\n\t\t\t\t? isLTR\n\t\t\t\t\t? clampedPosition > 0.5\n\t\t\t\t\t\t? 180 - clampedPosition * 180\n\t\t\t\t\t\t: -clampedPosition * 180\n\t\t\t\t\t: clampedPosition > 0.5\n\t\t\t\t\t\t? -(180 - clampedPosition * 180)\n\t\t\t\t\t\t: clampedPosition * 180\n\t\t\t\t: isLTR\n\t\t\t\t\t? clampedPosition < 0.5\n\t\t\t\t\t\t? -clampedPosition * 180\n\t\t\t\t\t\t: 180 - clampedPosition * 180\n\t\t\t\t\t: clampedPosition < 0.5\n\t\t\t\t\t\t? clampedPosition * 180\n\t\t\t\t\t\t: -(180 - clampedPosition * 180);\n\t\t\tconst rotateY = `${degrees}deg`;\n\t\t\tconst translateX = `${isOdd ? (isLTR ? \"100%\" : \"-100%\") : \"0px\"}`;\n\t\t\tconst scaleX = isOdd ? (clampedPosition > 0.5 ? -1 : 1) : clampedPosition < 0.5 ? -1 : 1;\n\t\t\tconst origin = isOdd ? (isLTR ? \"left\" : \"right\") : isLTR ? \"right\" : \"left\";\n\n\t\t\tpage.style.transform = `translateX(${translateX})rotateY(${rotateY})scaleX(${scaleX})`;\n\t\t\tpage.style.transformOrigin = origin;\n\t\t\tpage.style.zIndex = this.getSurfaceZIndex(page, clampedPosition, index, false);\n\n\t\t\tif (pageShadow) {\n\t\t\t\tconst edge = origin === \"left\" ? \"right\" : \"left\";\n\t\t\t\tpage.style.setProperty(\"--inner-shadow-shadow\", shadowStrength.toFixed(3));\n\t\t\t\tpage.style.setProperty(\"--inner-shadow-highlight\", highlightStrength.toFixed(3));\n\t\t\t\tpage.style.setProperty(\"--inner-shadow-edge\", edge);\n\t\t\t}\n\t\t});\n\t}\n\n\tprivate getPaperSize(): { width: number; height: number } | undefined {\n\t\tconst width = Number.parseFloat(this.pages[0].style.width);\n\t\tconst height = Number.parseFloat(this.pages[0].style.height);\n\t\tif (\n\t\t\t!this.pages[1] ||\n\t\t\tthis.pages.some((page) => page?.dataset.isCoverPage === \"1\") ||\n\t\t\t!Number.isFinite(width) ||\n\t\t\t!Number.isFinite(height) ||\n\t\t\twidth <= 0 ||\n\t\t\theight <= 0\n\t\t) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn { width, height };\n\t}\n\n\tprivate getSurfaceZIndex(\n\t\tpage: HTMLElement,\n\t\tposition: number,\n\t\tside: number,\n\t\tfolded: boolean,\n\t): string {\n\t\tif (position === 0 || position === 1) {\n\t\t\treturn `${\n\t\t\t\tposition === 1\n\t\t\t\t\t? page.dataset.pageIndex\n\t\t\t\t\t: this.bookProperties.pagesCount - Number(page.dataset.pageIndex)\n\t\t\t}`;\n\t\t}\n\t\t// All moving leaves share a stack above the flat pages, including rigid boards.\n\t\t// Leaf order reverses across the spine; rigid faces also swap their visible side.\n\t\tconst beforeSpine = position < 0.5;\n\t\tconst leafOrder = beforeSpine ? -this.index * 2 : this.index * 2;\n\t\tconst faceOrder = folded || !beforeSpine ? side : 1 - side;\n\t\treturn `${this.bookProperties.pagesCount * 2 + leafOrder + faceOrder}`;\n\t}\n\n\tprivate applyPaperFold(\n\t\twidth: number,\n\t\theight: number,\n\t\tposition: number,\n\t\tmanualFold?: PageFold,\n\t\tbackwardPreview = false,\n\t): void {\n\t\tconst fold =\n\t\t\tmanualFold ??\n\t\t\tcomputePageFold(\n\t\t\t\twidth,\n\t\t\t\theight,\n\t\t\t\tbackwardPreview ? 1 - position : position,\n\t\t\t\tthis.foldOrigin < 0.5 ? \"top\" : \"bottom\",\n\t\t\t\tthis.foldPointer - this.foldOrigin,\n\t\t\t);\n\t\tconst [a, b, c, d, tx, ty] = fold.reflection;\n\t\t// A resting back face curls from its own outer corner, with its body stationary.\n\t\tconst isLTR = backwardPreview ? !this.bookProperties.isLTR : this.bookProperties.isLTR;\n\t\tconst offsetX = backwardPreview ? 0 : isLTR ? width : -width;\n\t\tfor (let side = 0; side < this.pages.length; side++) {\n\t\t\tconst page = this.pages[side];\n\t\t\tif (!page) continue;\n\t\t\tconst front = side === (backwardPreview ? 1 : 0);\n\t\t\tconst mirror = front ? !isLTR : isLTR;\n\t\t\tconst polygon = (front ? fold.front : fold.folded).map((point) => ({\n\t\t\t\tx: mirror ? width - point.x : point.x,\n\t\t\t\ty: point.y,\n\t\t\t}));\n\t\t\tpage.classList.add(\"page--folded\");\n\t\t\tpage.style.transformOrigin = \"0px 0px\";\n\t\t\tpage.style.transform = front\n\t\t\t\t? `translateX(${offsetX}px)`\n\t\t\t\t: isLTR\n\t\t\t\t\t? `matrix(${-a}, ${-b}, ${c}, ${d}, ${offsetX + a * width + tx}, ${b * width + ty})`\n\t\t\t\t\t: `matrix(${-a}, ${b}, ${-c}, ${d}, ${offsetX + width - tx}, ${ty})`;\n\t\t\tpage.style.clipPath =\n\t\t\t\tpolygon.length < 3\n\t\t\t\t\t? \"polygon(0px 0px, 0px 0px, 0px 0px)\"\n\t\t\t\t\t: `polygon(${polygon.map((point) => `${point.x.toFixed(3)}px ${point.y.toFixed(3)}px`).join(\", \")})`;\n\t\t\tpage.style.zIndex = this.getSurfaceZIndex(\n\t\t\t\tpage,\n\t\t\t\tposition,\n\t\t\t\tbackwardPreview ? 1 - side : side,\n\t\t\t\ttrue,\n\t\t\t);\n\t\t\tif (this.bookProperties.pageShadow) {\n\t\t\t\tthis.applyCreaseShading(\n\t\t\t\t\tpage,\n\t\t\t\t\twidth,\n\t\t\t\t\theight,\n\t\t\t\t\tfold.normal,\n\t\t\t\t\tfold.midpoint,\n\t\t\t\t\tmirror,\n\t\t\t\t\tfold.strength,\n\t\t\t\t\tfront,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate applyCreaseShading(\n\t\tpage: HTMLElement,\n\t\twidth: number,\n\t\theight: number,\n\t\tnormal: Point,\n\t\tmidpoint: Point,\n\t\tmirror: boolean,\n\t\tstrength: number,\n\t\tfront: boolean,\n\t): void {\n\t\tconst nx = mirror ? -normal.x : normal.x;\n\t\tconst mx = mirror ? width - midpoint.x : midpoint.x;\n\t\tconst span = Math.abs(nx) * width + Math.abs(normal.y) * height;\n\t\tconst minimum = Math.min(0, nx * width) + Math.min(0, normal.y * height);\n\t\tconst stop = ((nx * mx + normal.y * midpoint.y - minimum) / span) * 100;\n\t\tconst angle = (Math.atan2(nx, -normal.y) * 180) / Math.PI;\n\t\tconst band = Math.min(18, (100 * Math.min(width * 0.12, 48)) / span);\n\t\tconst opacity = (strength * (front ? 0.24 : 0.3)).toFixed(3);\n\t\tpage.style.setProperty(\n\t\t\t\"--fold-shading\",\n\t\t\tfront\n\t\t\t\t? `linear-gradient(${angle}deg, transparent ${stop - band}%, rgba(0,0,0,${opacity}) ${stop}%, transparent ${stop + 0.5}%)`\n\t\t\t\t: `linear-gradient(${angle}deg, rgba(0,0,0,${opacity}) ${stop}%, rgba(255,255,255,${(strength * 0.35).toFixed(3)}) ${stop + band * 0.3}%, transparent ${stop + band}%)`,\n\t\t);\n\t}\n}\n","export interface AspectRatio {\n\twidth: number;\n\theight: number;\n}\nexport class AspectRatioImpl implements AspectRatio {\n\tstatic from(aspectRatio: AspectRatio) {\n\t\treturn new AspectRatioImpl(aspectRatio.width, aspectRatio.height);\n\t}\n\tconstructor(\n\t\tpublic readonly width: number,\n\t\tpublic readonly height: number,\n\t) {}\n\tget value(): number {\n\t\treturn this.width / this.height;\n\t}\n}\n","import { type AspectRatio, AspectRatioImpl } from \"./aspect-ratio\";\n\nexport class Size {\n\taspectRatio: AspectRatioImpl;\n\taspectRatioFit(rhsAspectRatio: AspectRatio) {\n\t\tconst rhsAspectRatioValue = AspectRatioImpl.from(rhsAspectRatio).value;\n\t\treturn this.aspectRatio.value > rhsAspectRatioValue\n\t\t\t? new Size(this.height * rhsAspectRatioValue, this.height)\n\t\t\t: new Size(this.width, this.width / rhsAspectRatioValue);\n\t}\n\tconstructor(\n\t\tpublic readonly width: number,\n\t\tpublic readonly height: number,\n\t) {\n\t\tthis.aspectRatio = new AspectRatioImpl(width, height);\n\t}\n\tget asString(): string {\n\t\treturn `${this.width}x${this.height}`;\n\t}\n}\n","import \"./pages.scss\";\nimport \"./flipbook.scss\";\nimport \"./mouse-mode.scss\";\nimport Hammer from \"hammerjs\";\nimport { throttle } from \"throttle-debounce\";\nimport type { AspectRatio } from \"./aspect-ratio\";\nimport type { DownloadConfig } from \"./download/types\";\nimport type {\n\tFlipBookOptions,\n\tFlipPageSemantic,\n\tHistoryMapper,\n\tMouseMode,\n\tPageFlipDirection,\n\tPageFlipParams,\n} from \"./flip-book-options\";\nimport { FlipDirection } from \"./flip-direction\";\nimport { type FlipPosition, Leaf } from \"./leaf\";\nimport type { PageSemantics } from \"./page-semantics\";\nimport { Size } from \"./size\";\nimport { setTocPageIndex } from \"./store\";\n\n/** Default threshold for fast flip detection (in ms) */\nconst DEFAULT_FAST_DELTA = 500;\n\n/** Percentage of a page's width that triggers its edge preview */\nconst EDGE_ZONE_RATIO = 0.18;\n/** Maximum hover shadow strength (0-1) */\nconst HOVER_STRENGTH_MAX = 0.12;\n/** Throttle interval for mouse move handler in ms */\nconst MOUSE_MOVE_THROTTLE_MS = 16;\nconst historyOwnerEpoch = Date.now().toString(36);\nlet nextHistoryOwner = 0;\n\n/** State for a single flip operation - enables concurrent page flipping */\ninterface FlipState {\n\tleaf: Leaf;\n\tdirection: FlipDirection;\n\tstartingPos: number;\n\tstartingY?: number;\n\tcornerY?: number;\n\tdelta: number;\n\tisDuringAutoFlip: boolean;\n}\n\n/**\n * Core FlipBook class for creating realistic page-flip animations.\n *\n * @example\n * ```typescript\n * import { FlipBook } from 'html-flip-book-vanilla';\n *\n * const flipBook = new FlipBook({\n * pagesCount: 10,\n * direction: 'ltr',\n * });\n *\n * flipBook.render('#book-container');\n *\n * // Navigate programmatically\n * await flipBook.flipNext();\n * await flipBook.flipToPage(5);\n * ```\n */\nclass FlipBook {\n\tbookElement?: HTMLElement;\n\tprivate pageElements: HTMLElement[] = [];\n\tprivate readonly pagesCount: number;\n\tprivate readonly leafAspectRatio: AspectRatio = { width: 2, height: 3 };\n\tprivate readonly coverAspectRatio: AspectRatio = {\n\t\twidth: 2.15,\n\t\theight: 3.15,\n\t};\n\tprivate readonly direction: \"rtl\" | \"ltr\" = \"ltr\";\n\tprivate mouseMode: MouseMode = \"turn\";\n\tprivate readonly fastDeltaThreshold: number = DEFAULT_FAST_DELTA;\n\tprivate readonly initialTurnedLeaves: Set<number> = new Set();\n\tprivate readonly onPageChanged?: (pageIndex: number) => void;\n\tprivate readonly onPageFlipping?: (params: PageFlipParams) => void;\n\tprivate readonly onPageFlipped?: (params: PageFlipParams) => void;\n\tprivate readonly historyMapper?: HistoryMapper;\n\tprivate lastFlipParams: PageFlipParams | undefined;\n\tprivate _historyInitialized = false;\n\tprivate _isRestoringFromHistory = false;\n\tprivate _boundPopstate: ((event: PopStateEvent) => void) | undefined;\n\tprivate readonly _historyOwner = `${historyOwnerEpoch}-${++nextHistoryOwner}`;\n\tprivate readonly pageSemantics: PageSemantics | undefined;\n\tprivate readonly leavesBuffer?: number;\n\tprivate readonly coverPageIndices?: number[] | \"auto\";\n\tprivate readonly downloadConfig: DownloadConfig | undefined;\n\tprivate readonly pageShadow: boolean;\n\tprivate readonly snapshotDuringFlip: boolean;\n\tprivate leaves: Leaf[] = [];\n\t// flipping state - supports concurrent page flipping\n\tprivate activeFlips: Map<number, FlipState> = new Map();\n\tprivate pendingFlipStartingPos = 0;\n\tprivate pendingFlipStartingY: number | undefined;\n\tprivate pendingFlipDirection: FlipDirection = FlipDirection.None;\n\tprivate isDragging = false;\n\tprivate gestureStartedInNoFlipZone = false;\n\t/** Set after a drag ends; the next click event is suppressed so child buttons/links don't fire. */\n\tprivate _suppressNextClick = false;\n\tprivate hoveredLeaf: Leaf | undefined;\n\ttouchStartingPos = { x: 0, y: 0 };\n\t/** True when the current touch started inside a [data-flipbook-no-flip] element (e.g. carousel). */\n\tprivate touchStartedInNoFlipZone = false;\n\t/**\n\t * Direction lock for the current touch gesture.\n\t * Once the first significant movement (>5 px) determines the dominant axis,\n\t * the lock stays for the rest of the gesture. 'vertical' means native scroll\n\t * wins — Hammer pan events are rejected. 'horizontal' means every touchmove\n\t * gets preventDefault() so Hammer can drive the page flip.\n\t */\n\ttouchDirectionLock: \"none\" | \"vertical\" | \"horizontal\" = \"none\";\n\tprivate static readonly DIRECTION_LOCK_THRESHOLD = 5;\n\tprivate prevVisiblePageIndices: [number] | [number, number] | undefined;\n\t// Hammer instance for cleanup\n\tprivate hammer: HammerManager | undefined;\n\t// Resize observer for re-layout on container size changes\n\tprivate resizeObserver: ResizeObserver | undefined;\n\n\tprivate static readonly NO_FLIP_SELECTOR = \"[data-flipbook-no-flip]\";\n\n\tprivate isInsideNoFlipZone(el: EventTarget | null): boolean {\n\t\treturn el instanceof Element && el.closest(FlipBook.NO_FLIP_SELECTOR) != null;\n\t}\n\n\tprivate getSemanticForPage(pageIndex: number): FlipPageSemantic | undefined {\n\t\tif (!this.pageSemantics) return undefined;\n\t\treturn {\n\t\t\tsemanticName: this.pageSemantics.indexToSemanticName(pageIndex),\n\t\t\ttitle: this.pageSemantics.indexToTitle(pageIndex),\n\t\t};\n\t}\n\n\tprivate buildPageFlipParams(\n\t\tleftmostPageIndex: number,\n\t\tdirection: PageFlipDirection,\n\t): PageFlipParams {\n\t\tconst rightPageIndex = Math.min(leftmostPageIndex + 1, this.pagesCount - 1);\n\t\tconst pageIndices: [number, number] = [leftmostPageIndex, rightPageIndex];\n\t\tconst semantics: [FlipPageSemantic | undefined, FlipPageSemantic | undefined] = [\n\t\t\tthis.getSemanticForPage(leftmostPageIndex),\n\t\t\tthis.getSemanticForPage(rightPageIndex),\n\t\t];\n\t\tconst leafIndex = Math.floor(leftmostPageIndex / 2);\n\t\treturn { leafIndex, pageIndices, semantics, direction };\n\t}\n\n\tprivate syncHistoryAndNotifyFlipped(isInitial: boolean): void {\n\t\tconst pageIndex = this.currentPageIndex;\n\t\tconst semantic = this.getSemanticForPage(pageIndex);\n\t\tif (this.onPageChanged) this.onPageChanged(pageIndex);\n\t\tconst params = this.lastFlipParams ?? this.buildPageFlipParams(pageIndex, \"forward\");\n\t\tif (this.onPageFlipped) this.onPageFlipped(params);\n\t\tif (typeof window !== \"undefined\" && this.historyMapper && !this._isRestoringFromHistory) {\n\t\t\tconst route = this.historyMapper.pageToRoute(pageIndex, semantic);\n\t\t\t// When pageToRoute returns null the consumer signals \"this page has\n\t\t\t// no meaningful URL\" (e.g. cover, TOC). Skip the pushState entirely\n\t\t\t// to avoid framework-level routing side-effects.\n\t\t\tif (route == null) return;\n\t\t\t// Keep the current entry's framework state when writing a book route.\n\t\t\t// Next.js, for example, marks SPA entries in history.state and reloads\n\t\t\t// the document on Back if that marker is missing.\n\t\t\tconst currentState = window.history.state;\n\t\t\tconst state =\n\t\t\t\tcurrentState !== null && typeof currentState === \"object\" && !Array.isArray(currentState)\n\t\t\t\t\t? { ...currentState, route, __flipBookOwner: this._historyOwner }\n\t\t\t\t\t: { route, __flipBookOwner: this._historyOwner };\n\t\t\tconst url = route.startsWith(\"#\")\n\t\t\t\t? `${window.location.pathname}${window.location.search}${route}`\n\t\t\t\t: route;\n\t\t\t// Use the *native* History.prototype methods rather than\n\t\t\t// window.history.pushState/replaceState. Frameworks like Next.js\n\t\t\t// monkey-patch the instance methods to intercept URL changes and\n\t\t\t// trigger full soft navigations (RSC fetch + React re-render).\n\t\t\t// That is catastrophically expensive during rapid page flips.\n\t\t\t// Calling the prototype method bypasses the patch while still\n\t\t\t// updating the browser URL bar and history stack correctly.\n\t\t\tif (isInitial || !this._historyInitialized) {\n\t\t\t\tHistory.prototype.replaceState.call(window.history, state, \"\", url);\n\t\t\t\tthis._historyInitialized = true;\n\t\t\t} else {\n\t\t\t\tHistory.prototype.pushState.call(window.history, state, \"\", url);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate handlePopstate = (event: PopStateEvent): void => {\n\t\tif (!this.historyMapper || typeof window === \"undefined\") return;\n\t\t// Framework routers also observe popstate. A route written by this book\n\t\t// already has its page restored below; letting a router traverse it again\n\t\t// can remount the entire book despite the navigation being same-document.\n\t\tif (event.state?.__flipBookOwner !== this._historyOwner) return;\n\t\tconst route =\n\t\t\t(typeof window.history.state === \"object\" && window.history.state?.route) ||\n\t\t\twindow.location.pathname + window.location.search + window.location.hash;\n\t\tconst pageIndex = this.historyMapper.routeToPage(route);\n\t\tif (pageIndex !== null && pageIndex >= 0 && pageIndex < this.pagesCount) {\n\t\t\tevent.stopImmediatePropagation();\n\t\t\tthis.restorePage(pageIndex);\n\t\t}\n\t};\n\n\tprivate get isLTR(): boolean {\n\t\treturn this.direction === \"ltr\";\n\t}\n\n\tprivate get currentOrTurningLeaves(): [Leaf | undefined, Leaf | undefined] {\n\t\tlet secondLeafIndex = -1;\n\t\tfor (let i = this.leaves.length - 1; i >= 0; i--) {\n\t\t\tconst leaf = this.leaves[i];\n\t\t\tif (leaf.isTurned || leaf.isTurning) {\n\t\t\t\tsecondLeafIndex = leaf.index + 1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn secondLeafIndex === -1\n\t\t\t? [undefined, this.leaves[0]]\n\t\t\t: secondLeafIndex === this.leaves.length\n\t\t\t\t? [this.leaves[secondLeafIndex - 1], undefined]\n\t\t\t\t: [this.leaves[secondLeafIndex - 1], this.leaves[secondLeafIndex]];\n\t}\n\n\tconstructor(options: FlipBookOptions) {\n\t\tthis.pagesCount = options.pagesCount;\n\t\tthis.leafAspectRatio = options.leafAspectRatio || this.leafAspectRatio;\n\t\tthis.coverAspectRatio = options.coverAspectRatio || this.coverAspectRatio;\n\t\tthis.direction = options.direction || this.direction;\n\t\tthis.mouseMode = options.mouseMode ?? \"turn\";\n\t\tthis.fastDeltaThreshold = options.fastDeltaThreshold ?? this.fastDeltaThreshold;\n\t\tthis.initialTurnedLeaves = new Set(options.initialTurnedLeaves ?? []);\n\t\tthis.pageSemantics = options.pageSemantics;\n\t\tthis.onPageChanged = options.onPageChanged;\n\t\tthis.onPageFlipping = options.onPageFlipping;\n\t\tthis.onPageFlipped = options.onPageFlipped;\n\t\tthis.historyMapper = options.enableHistory !== false ? options.historyMapper : undefined;\n\t\tthis.leavesBuffer = options.leavesBuffer;\n\t\tthis.coverPageIndices = options.coverPageIndices;\n\t\tthis.downloadConfig = options.enableDownload !== false ? options.downloadConfig : undefined;\n\t\tthis.pageShadow = options.pageShadow ?? true;\n\t\tthis.snapshotDuringFlip = options.snapshotDuringFlip ?? true;\n\t\tthis._tocPageIndex = options.tocPageIndex ?? 4;\n\t\t// Keep the module-level store in sync for backward-compat\n\t\tsetTocPageIndex(this._tocPageIndex);\n\t}\n\n\tprivate _tocPageIndex = 4;\n\n\t/** Get the configured table of contents page index. */\n\tgetTocPageIndex(): number {\n\t\treturn this._tocPageIndex;\n\t}\n\n\tgetMouseMode(): MouseMode {\n\t\treturn this.mouseMode;\n\t}\n\n\tsetMouseMode(mode: MouseMode): void {\n\t\tthis.mouseMode = mode;\n\t\tthis.bookElement?.classList.toggle(\"flipbook--select-text\", mode === \"select\");\n\t\tif (mode === \"select\") this.clearHoverShadow();\n\t}\n\n\t/** Keep mouse selection native by stopping the page's down event before Hammer sees it. */\n\tprivate handlePageMouseDown = (event: Event): void => {\n\t\t// A swipe may emit no click. A fresh press must not inherit suppression\n\t\t// intended only for the previous gesture's synthetic click.\n\t\tthis._suppressNextClick = false;\n\t\tif (event.type === \"mousedown\" || (event as PointerEvent).pointerType === \"mouse\") {\n\t\t\tthis.gestureStartedInNoFlipZone = this.isInsideNoFlipZone(event.target);\n\t\t\tif (this.gestureStartedInNoFlipZone) this.clearHoverShadow();\n\t\t}\n\t\tif (\n\t\t\tthis.mouseMode === \"select\" &&\n\t\t\tevent instanceof MouseEvent &&\n\t\t\tevent.button === 0 &&\n\t\t\t(event.type !== \"pointerdown\" || (event as PointerEvent).pointerType === \"mouse\")\n\t\t) {\n\t\t\tevent.stopPropagation();\n\t\t}\n\t};\n\n\t/** Download config (handlers and filename hints). Used by toolbar. */\n\tgetDownloadConfig(): DownloadConfig | undefined {\n\t\treturn this.downloadConfig;\n\t}\n\n\t/** Build the set of page indices that use cover sizing. */\n\tprivate buildCoverPageIndicesSet(): Set<number> {\n\t\tconst coverPageIndicesSet = new Set<number>();\n\t\tif (this.coverPageIndices) {\n\t\t\tconst indices =\n\t\t\t\tthis.coverPageIndices === \"auto\" ? [0, this.pagesCount - 1] : this.coverPageIndices;\n\t\t\tfor (const pageIdx of indices) {\n\t\t\t\tcoverPageIndicesSet.add(pageIdx);\n\t\t\t}\n\t\t}\n\t\treturn coverPageIndicesSet;\n\t}\n\n\t/**\n\t * Recalculate page sizes from the current container dimensions and\n\t * re-apply width/height/position styles. Called during initial render\n\t * and automatically on container resize (e.g. entering fullscreen).\n\t */\n\tprivate recalculateSizes(): void {\n\t\tif (!this.bookElement || !this.pageElements.length) return;\n\n\t\tconst maxCoverSize = new Size(this.bookElement.clientWidth / 2, this.bookElement.clientHeight);\n\t\tconst coverSize = maxCoverSize.aspectRatioFit(this.coverAspectRatio);\n\t\tconst leafSize = new Size(\n\t\t\t(coverSize.width * this.leafAspectRatio.width) / this.coverAspectRatio.width,\n\t\t\t(coverSize.height * this.leafAspectRatio.height) / this.coverAspectRatio.height,\n\t\t);\n\t\tthis.bookElement.style.perspective = `${Math.min(leafSize.width * 2, leafSize.height) * 2}px`;\n\n\t\tconst containerWidth = this.bookElement.clientWidth;\n\t\tconst containerHeight = this.bookElement.clientHeight;\n\n\t\tfor (const pageElement of this.pageElements) {\n\t\t\tconst isCoverPage = pageElement.dataset.isCoverPage === \"1\";\n\t\t\tconst pageSize = isCoverPage ? coverSize : leafSize;\n\n\t\t\tpageElement.style.width = `${pageSize.width}px`;\n\t\t\tpageElement.style.height = `${pageSize.height}px`;\n\t\t\tpageElement.style[this.isLTR ? \"left\" : \"right\"] =\n\t\t\t\t`${(containerWidth - 2 * pageSize.width) / 2}px`;\n\t\t\tpageElement.style.top = `${(containerHeight - pageSize.height) / 2}px`;\n\t\t}\n\t\tfor (const leaf of this.leaves) {\n\t\t\tleaf.applyTransform(leaf.flipPosition);\n\t\t}\n\t}\n\n\trender(selector: string, debug = false) {\n\t\tconst bookElement = document.querySelector(selector);\n\t\tif (!bookElement) {\n\t\t\tthrow new Error(`Couldn't find container with selector: ${selector}`);\n\t\t}\n\t\tthis.bookElement = bookElement as HTMLElement;\n\t\tthis.setMouseMode(this.mouseMode);\n\t\tif (!this.bookElement.classList.contains(\"flipbook\")) {\n\t\t\tthis.bookElement.classList.add(\"flipbook\");\n\t\t}\n\t\tif (!this.pageShadow) {\n\t\t\tthis.bookElement.classList.add(\"flipbook--no-page-shadow\");\n\t\t}\n\n\t\tconst pageElements = bookElement.querySelectorAll(\".page\");\n\t\tif (!pageElements.length) {\n\t\t\tthrow new Error(\"No pages found in flipbook\");\n\t\t}\n\t\tthis.pageElements = Array.from(pageElements) as HTMLElement[];\n\t\tfor (const page of this.pageElements) {\n\t\t\tpage.addEventListener(\"pointerdown\", this.handlePageMouseDown);\n\t\t\tpage.addEventListener(\"mousedown\", this.handlePageMouseDown);\n\t\t}\n\t\tthis.leaves.splice(0, this.leaves.length);\n\t\tconst leavesCount = Math.ceil(this.pagesCount / 2);\n\n\t\t// Determine which specific pages are cover exteriors (use coverSize).\n\t\t// Interior sides of cover leaves use leafSize — physically, the endpaper is\n\t\t// glued to the inside of the board and matches the text-block page size.\n\t\tconst coverPageIndicesSet = this.buildCoverPageIndicesSet();\n\n\t\t// Set data attributes before recalculateSizes() so it can read dataset.isCoverPage\n\t\t// to apply the correct cover vs leaf dimensions on the very first layout pass.\n\t\tthis.pageElements.forEach((pageElement, pageIndex) => {\n\t\t\tpageElement.dataset.pageIndex = pageIndex.toString();\n\t\t\tpageElement.dataset.pageSemanticName =\n\t\t\t\tthis.pageSemantics?.indexToSemanticName(pageIndex) ?? \"\";\n\t\t\tpageElement.dataset.pageTitle = this.pageSemantics?.indexToTitle(pageIndex) ?? \"\";\n\t\t\tpageElement.dataset.isCoverPage = coverPageIndicesSet.has(pageIndex) ? \"1\" : \"\";\n\t\t});\n\n\t\t// Compute and apply sizes (also called on resize).\n\t\t// Must run after dataset.isCoverPage is set so covers get coverSize.\n\t\tthis.recalculateSizes();\n\n\t\tthis.pageElements.forEach((pageElement, pageIndex) => {\n\t\t\tconst leafIndex = Math.floor(pageIndex / 2);\n\n\t\t\tconst isOddPage = (pageIndex + 1) % 2 === 1;\n\t\t\tconst isInitiallyTurned = this.initialTurnedLeaves.has(leafIndex);\n\n\t\t\t// Determine current page based on initial turned leaves\n\t\t\t// Current pages are the first two visible pages after all initially turned leaves\n\t\t\tconst firstVisibleLeafIndex =\n\t\t\t\tthis.initialTurnedLeaves.size > 0 ? Math.max(...this.initialTurnedLeaves) + 1 : 0;\n\t\t\tconst isCurrentPage =\n\t\t\t\tleafIndex === firstVisibleLeafIndex ||\n\t\t\t\t(leafIndex === firstVisibleLeafIndex - 1 && isInitiallyTurned);\n\n\t\t\tpageElement.classList.add(\n\t\t\t\tisOddPage ? \"odd\" : \"even\",\n\t\t\t\t...(isCurrentPage ? [\"current-page\"] : []),\n\t\t\t);\n\t\t\tif (isOddPage) {\n\t\t\t\tthis.leaves[leafIndex] = new Leaf(\n\t\t\t\t\tleafIndex,\n\t\t\t\t\t[pageElement, undefined],\n\t\t\t\t\tisInitiallyTurned,\n\t\t\t\t\t{\n\t\t\t\t\t\tisLTR: this.isLTR,\n\t\t\t\t\t\tleavesCount: leavesCount,\n\t\t\t\t\t\tpagesCount: this.pagesCount,\n\t\t\t\t\t\tpageShadow: this.pageShadow,\n\t\t\t\t\t\tsnapshotDuringFlip: this.snapshotDuringFlip,\n\t\t\t\t\t},\n\t\t\t\t\t(direction: FlipDirection) => {\n\t\t\t\t\t\tconst currentVisiblePageIndices: [number] | [number, number] =\n\t\t\t\t\t\t\tdirection === FlipDirection.Forward\n\t\t\t\t\t\t\t\t? pageIndex + 2 === this.pagesCount\n\t\t\t\t\t\t\t\t\t? [pageIndex + 1]\n\t\t\t\t\t\t\t\t\t: [pageIndex + 1, pageIndex + 2]\n\t\t\t\t\t\t\t\t: pageIndex === 0\n\t\t\t\t\t\t\t\t\t? [pageIndex]\n\t\t\t\t\t\t\t\t\t: [pageIndex - 1, pageIndex];\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tthis.prevVisiblePageIndices &&\n\t\t\t\t\t\t\tthis.prevVisiblePageIndices.length === currentVisiblePageIndices.length &&\n\t\t\t\t\t\t\tcurrentVisiblePageIndices.every((v, i) => v === this.prevVisiblePageIndices?.[i])\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst prevVisiblePageIndices = this.prevVisiblePageIndices;\n\t\t\t\t\t\tthis.prevVisiblePageIndices = currentVisiblePageIndices;\n\n\t\t\t\t\t\t// TODO expose to outside using https://github.com/open-draft/strict-event-emitter, and just be a consumer internally\n\t\t\t\t\t\tthis.onTurned(currentVisiblePageIndices, prevVisiblePageIndices);\n\t\t\t\t\t},\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\t// Even page (back side of leaf) — attach to the leaf created above.\n\t\t\t\tthis.leaves[leafIndex].pages[1] = pageElement;\n\n\t\t\t\t// Both pages of the leaf are now available.\n\t\t\t\t// Use applyTransform to set the correct initial transform — this ensures\n\t\t\t\t// the transforms exactly match what applyTransform produces during flips.\n\t\t\t\t// Previously, hardcoded transforms used rotateY(±180deg) for turned pages,\n\t\t\t\t// which made them invisible due to backface-visibility: hidden.\n\t\t\t\tconst initialPosition = isInitiallyTurned ? 1 : 0;\n\t\t\t\tthis.leaves[leafIndex].applyTransform(initialPosition);\n\t\t\t}\n\t\t});\n\n\t\t// Set initial visible page indices based on initially turned leaves.\n\t\t// Must match the post-flip model: the visible spread is the back of\n\t\t// the last turned leaf (odd page) and the front of the next un-turned\n\t\t// leaf (even page) → [lastTurnedLeaf*2+1, lastTurnedLeaf*2+2].\n\t\tconst lastTurnedLeafIndex =\n\t\t\tthis.initialTurnedLeaves.size > 0 ? Math.max(...this.initialTurnedLeaves) : -1;\n\n\t\tif (lastTurnedLeafIndex < 0) {\n\t\t\tthis.prevVisiblePageIndices = 1 < this.pagesCount ? [0, 1] : [0];\n\t\t} else {\n\t\t\tconst firstVisiblePageIndex = lastTurnedLeafIndex * 2 + 1;\n\t\t\tif (firstVisiblePageIndex >= this.pagesCount) {\n\t\t\t\t// All leaves are turned — show the last page(s)\n\t\t\t\tconst lastLeafIndex = Math.ceil(this.pagesCount / 2) - 1;\n\t\t\t\tconst lastLeafFirstPage = lastLeafIndex * 2;\n\t\t\t\tthis.prevVisiblePageIndices =\n\t\t\t\t\tlastLeafFirstPage + 1 < this.pagesCount\n\t\t\t\t\t\t? [lastLeafFirstPage, lastLeafFirstPage + 1]\n\t\t\t\t\t\t: [lastLeafFirstPage];\n\t\t\t} else {\n\t\t\t\tthis.prevVisiblePageIndices =\n\t\t\t\t\tfirstVisiblePageIndex + 1 < this.pagesCount\n\t\t\t\t\t\t? [firstVisiblePageIndex, firstVisiblePageIndex + 1]\n\t\t\t\t\t\t: [firstVisiblePageIndex];\n\t\t\t}\n\t\t}\n\n\t\t// Observe container size changes and re-layout automatically.\n\t\tif (typeof ResizeObserver !== \"undefined\") {\n\t\t\tthis.resizeObserver = new ResizeObserver(() => {\n\t\t\t\tthis.recalculateSizes();\n\t\t\t});\n\t\t\tthis.resizeObserver.observe(this.bookElement);\n\t\t}\n\n\t\tthis.hammer = new Hammer(this.bookElement, { touchAction: \"pan-y\" });\n\t\t// Mouse pulls can move in both axes while touch keeps native vertical scrolling.\n\t\tthis.hammer.get(\"pan\").set({ direction: Hammer.DIRECTION_ALL });\n\t\tthis.hammer.on(\"panstart\", this.onDragStart.bind(this));\n\t\tthis.hammer.on(\"panmove\", this.onDragUpdate.bind(this));\n\t\tthis.hammer.on(\"panend\", this.onDragEnd.bind(this));\n\t\tthis.hammer.on(\"pancancel\", (event) => this.onDragEnd(event, true));\n\t\tthis.bookElement.addEventListener(\"touchstart\", this.handleTouchStart, {\n\t\t\tpassive: false,\n\t\t});\n\t\tthis.bookElement.addEventListener(\"touchmove\", this.handleTouchMove, {\n\t\t\tpassive: false,\n\t\t});\n\t\tthis.bookElement.addEventListener(\n\t\t\t\"mousemove\",\n\t\t\tthis.handleMouseMove as unknown as EventListener,\n\t\t);\n\t\tthis.bookElement.addEventListener(\"mouseleave\", this.handleMouseLeave as EventListener);\n\t\t// Suppress click events that fire immediately after a drag gesture.\n\t\t// This prevents child interactive elements (buttons, links) from\n\t\t// activating when the user was actually performing a page flip.\n\t\tthis.bookElement.addEventListener(\n\t\t\t\"click\",\n\t\t\tthis.handleClickAfterDrag as EventListener,\n\t\t\ttrue, // capture phase — intercept before the click reaches any child\n\t\t);\n\t\t// Apply initial leaves buffer visibility\n\t\tthis.updateLeavesBufferVisibility();\n\t\tif (debug) this.fillDebugBar();\n\n\t\t// History: initial replaceState and popstate listener\n\t\tif (typeof window !== \"undefined\" && this.historyMapper) {\n\t\t\tthis.syncHistoryAndNotifyFlipped(true);\n\t\t\tthis._boundPopstate = this.handlePopstate.bind(this);\n\t\t\twindow.addEventListener(\"popstate\", this._boundPopstate, true);\n\t\t}\n\t}\n\n\t/**\n\t * Update visibility of leaves based on the buffer setting.\n\t * Leaves outside the buffer range are hidden for performance.\n\t */\n\tprivate updateLeavesBufferVisibility(): void {\n\t\tif (this.leavesBuffer === undefined) {\n\t\t\treturn; // No buffer - all leaves stay visible\n\t\t}\n\n\t\tconst leavesCount = this.leaves.length;\n\t\tif (leavesCount === 0) return;\n\n\t\t// Find the current leaf index (the first non-turned leaf, or last leaf if all turned)\n\t\tconst [leftLeaf, rightLeaf] = this.currentOrTurningLeaves;\n\t\tconst currentLeafIndex = rightLeaf?.index ?? leftLeaf?.index ?? 0;\n\n\t\t// Calculate buffer range\n\t\tconst bufferStart = Math.max(0, currentLeafIndex - this.leavesBuffer);\n\t\tconst bufferEnd = Math.min(leavesCount - 1, currentLeafIndex + this.leavesBuffer);\n\n\t\t// Cover leaves must always stay visible — they are cover-sized and sit behind\n\t\t// all leaf-sized pages, acting as the physical book boards. Hiding them would\n\t\t// remove the realistic \"frame\" effect.\n\t\tconst coverLeafIndices = new Set<number>();\n\t\tif (this.coverPageIndices) {\n\t\t\tconst indices =\n\t\t\t\tthis.coverPageIndices === \"auto\" ? [0, this.pagesCount - 1] : this.coverPageIndices;\n\t\t\tfor (const pageIdx of indices) {\n\t\t\t\tcoverLeafIndices.add(Math.floor(pageIdx / 2));\n\t\t\t}\n\t\t}\n\n\t\t// Update visibility of all leaves\n\t\tfor (let i = 0; i < leavesCount; i++) {\n\t\t\tconst leaf = this.leaves[i];\n\t\t\tconst isWithinBuffer = i >= bufferStart && i <= bufferEnd;\n\t\t\tconst isCoverLeaf = coverLeafIndices.has(i);\n\n\t\t\t// Cover leaves are always visible; other leaves follow the buffer range.\n\t\t\tconst visible = isCoverLeaf || isWithinBuffer;\n\n\t\t\t// Update both pages of the leaf\n\t\t\tfor (const page of leaf.pages) {\n\t\t\t\tif (page) {\n\t\t\t\t\tpage.style.display = visible ? \"\" : \"none\";\n\t\t\t\t\t// Use content-visibility to skip rendering of off-screen pages.\n\t\t\t\t\t// \"hidden\" is more aggressive than display:none — the browser\n\t\t\t\t\t// also skips layout/paint entirely, reducing memory pressure.\n\t\t\t\t\tpage.style.contentVisibility = visible ? \"visible\" : \"hidden\";\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate fillDebugBar() {\n\t\tconst debugBar = document.createElement(\"div\");\n\t\tdebugBar.className = \"flipbook-debug-bar flipbook-debug-bar--hidden\";\n\t\tthis.bookElement?.appendChild(debugBar);\n\t\tsetInterval(() => {\n\t\t\t// Populate debug bar with relevant information (throttled to reduce flicker)\n\t\t\tconst activeFlipsInfo = Array.from(this.activeFlips.entries())\n\t\t\t\t.map(([idx, state]) => `${idx}:${state.leaf.flipPosition.toFixed(2)}`)\n\t\t\t\t.join(\", \");\n\t\t\tdebugBar.innerHTML = `\n <div>Direction: ${this.isLTR ? \"LTR\" : \"RTL\"}</div>\n <div>Active Flips: ${this.activeFlips.size} [${activeFlipsInfo}]</div>\n <div>Pending Flip dir: ${this.pendingFlipDirection}</div>\n `;\n\t\t}, 200);\n\t}\n\n\t/**\n\t * Get the current manually-controlled flip state (the one being dragged).\n\t * Returns undefined if no manual flip is in progress.\n\t */\n\tprivate get currentManualFlip(): FlipState | undefined {\n\t\tfor (const state of this.activeFlips.values()) {\n\t\t\tif (!state.isDuringAutoFlip) {\n\t\t\t\treturn state;\n\t\t\t}\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Find the next available leaf for flipping in the given direction.\n\t * Excludes leaves that are already being flipped.\n\t */\n\tprivate getNextAvailableLeaf(direction: FlipDirection): Leaf | undefined {\n\t\tif (direction === FlipDirection.Forward) {\n\t\t\t// Find the rightmost leaf that's not already flipping\n\t\t\t// Start from the current right leaf and go forward\n\t\t\tconst [, rightLeaf] = this.currentOrTurningLeaves;\n\t\t\tif (!rightLeaf) return undefined;\n\n\t\t\t// Find a leaf that's not in activeFlips\n\t\t\tfor (let i = rightLeaf.index; i < this.leaves.length; i++) {\n\t\t\t\tconst leaf = this.leaves[i];\n\t\t\t\tif (!this.activeFlips.has(leaf.index) && !leaf.isTurned) {\n\t\t\t\t\treturn leaf;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn undefined;\n\t\t} else if (direction === FlipDirection.Backward) {\n\t\t\t// Find the leftmost leaf that's not already flipping\n\t\t\tconst [leftLeaf] = this.currentOrTurningLeaves;\n\t\t\tif (!leftLeaf) return undefined;\n\n\t\t\t// Find a leaf that's not in activeFlips\n\t\t\tfor (let i = leftLeaf.index; i >= 0; i--) {\n\t\t\t\tconst leaf = this.leaves[i];\n\t\t\t\tif (!this.activeFlips.has(leaf.index) && (leaf.isTurned || leaf.isTurning)) {\n\t\t\t\t\treturn leaf;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn undefined;\n\t\t}\n\t\treturn undefined;\n\t}\n\n\tprivate onDragStart(event: HammerInput) {\n\t\tconst isTouch = event.pointerType === \"touch\" || event.srcEvent?.type.startsWith(\"touch\");\n\t\tif (isTouch && this.touchDirectionLock === \"none\") {\n\t\t\tconst dx = Number.isFinite(event.deltaX) ? Math.abs(event.deltaX) : 0;\n\t\t\tconst dy = Number.isFinite(event.deltaY) ? Math.abs(event.deltaY) : 0;\n\t\t\tif (Math.max(dx, dy) > FlipBook.DIRECTION_LOCK_THRESHOLD) {\n\t\t\t\tthis.touchDirectionLock = dx >= dy ? \"horizontal\" : \"vertical\";\n\t\t\t}\n\t\t}\n\t\t// Reject pan events that belong to a touch gesture locked to vertical\n\t\t// (i.e. the user is scrolling, not flipping).\n\t\tif (this.touchDirectionLock === \"vertical\" && event.pointerType !== \"mouse\") {\n\t\t\treturn;\n\t\t}\n\t\t// Do not start flip when gesture begins inside a no-flip zone (e.g. horizontal carousel)\n\t\tif (\n\t\t\tthis.gestureStartedInNoFlipZone ||\n\t\t\tthis.isInsideNoFlipZone(event.srcEvent?.target ?? null)\n\t\t) {\n\t\t\tthis.gestureStartedInNoFlipZone = true;\n\t\t\treturn;\n\t\t}\n\t\t// Allow starting a new flip even if others are in auto-flip mode\n\t\t// Only block if we already have a manual flip in progress\n\t\tif (this.currentManualFlip) {\n\t\t\tthis.pendingFlipDirection = FlipDirection.None;\n\t\t\tthis.pendingFlipStartingPos = 0;\n\t\t\tthis.pendingFlipStartingY = undefined;\n\t\t\treturn;\n\t\t}\n\t\tthis.isDragging = true;\n\t\tthis.clearHoverShadow();\n\t\tthis.pendingFlipStartingPos =\n\t\t\tevent.center.x - (Number.isFinite(event.deltaX) ? event.deltaX : 0);\n\t\tthis.pendingFlipStartingY = Number.isFinite(event.center.y)\n\t\t\t? event.center.y - (Number.isFinite(event.deltaY) ? event.deltaY : 0)\n\t\t\t: undefined;\n\t\tthis.pendingFlipDirection = FlipDirection.None;\n\t\tif (\n\t\t\t(Number.isFinite(event.deltaX) && event.deltaX !== 0) ||\n\t\t\t(Number.isFinite(event.deltaY) && event.deltaY !== 0)\n\t\t) {\n\t\t\tthis.onDragUpdate(event);\n\t\t}\n\t}\n\n\tprivate onDragUpdate(event: HammerInput) {\n\t\tif (this.gestureStartedInNoFlipZone) return;\n\t\t// Reject pan events that belong to a touch gesture locked to vertical.\n\t\tif (this.touchDirectionLock === \"vertical\" && event.pointerType !== \"mouse\") {\n\t\t\treturn;\n\t\t}\n\n\t\t// Get or create the current manual flip state\n\t\tlet flipState = this.currentManualFlip;\n\t\tif (!flipState && this.isInsideNoFlipZone(event.srcEvent?.target ?? null)) return;\n\n\t\tconst currentPos = event.center.x;\n\t\tconst bookWidth = this.bookElement?.clientWidth ?? 0;\n\t\tif (bookWidth <= 0) return;\n\n\t\t// Calculate delta\n\t\tconst startingPos = flipState?.startingPos ?? this.pendingFlipStartingPos;\n\t\tconst delta = this.isLTR ? startingPos - currentPos : currentPos - startingPos;\n\t\tconst verticalMove =\n\t\t\tNumber.isFinite(event.center.y) && this.pendingFlipStartingY !== undefined\n\t\t\t\t? event.center.y - this.pendingFlipStartingY\n\t\t\t\t: 0;\n\t\tif (\n\t\t\t!flipState &&\n\t\t\tdelta === 0 &&\n\t\t\t(!this.isDragging || verticalMove === 0 || event.pointerType === \"touch\")\n\t\t)\n\t\t\treturn;\n\n\t\t// Determine direction on first meaningful movement\n\t\tconst isRightSide =\n\t\t\tstartingPos >= (this.bookElement?.getBoundingClientRect().left ?? 0) + bookWidth / 2;\n\t\tconst direction =\n\t\t\tflipState?.direction ??\n\t\t\t(this.pendingFlipDirection !== FlipDirection.None\n\t\t\t\t? this.pendingFlipDirection\n\t\t\t\t: delta === 0\n\t\t\t\t\t? this.isLTR === isRightSide\n\t\t\t\t\t\t? FlipDirection.Forward\n\t\t\t\t\t\t: FlipDirection.Backward\n\t\t\t\t\t: delta > 0\n\t\t\t\t\t\t? FlipDirection.Forward\n\t\t\t\t\t\t: FlipDirection.Backward);\n\n\t\t// Lock in direction for this flip\n\t\tif (this.pendingFlipDirection === FlipDirection.None) {\n\t\t\tthis.pendingFlipDirection = direction;\n\t\t}\n\n\t\t// Block starting a new flip in opposite direction while auto-flip is in progress\n\t\t// This prevents visual glitches when swiping backward while a forward flip is animating\n\t\tif (!flipState) {\n\t\t\tfor (const state of this.activeFlips.values()) {\n\t\t\t\tif (state.isDuringAutoFlip && state.direction !== direction) {\n\t\t\t\t\t// There's an auto-flip in the opposite direction - block this new flip\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// If we don't have a flip state yet, create one\n\t\tif (!flipState) {\n\t\t\tconst leaf = this.getNextAvailableLeaf(direction);\n\t\t\tif (!leaf) return;\n\n\t\t\t// Acquire the flip lock for the full drag cycle; the matching\n\t\t\t// endFlip() is deferred until the release animation completes\n\t\t\t// in onDragEnd, so the snapshot/scrollbar lockdown stays stable.\n\t\t\tleaf.beginFlip();\n\t\t\tconst originY = this.getFoldPointerY(\n\t\t\t\tleaf,\n\t\t\t\tdirection,\n\t\t\t\tthis.pendingFlipStartingY ?? event.center.y,\n\t\t\t);\n\t\t\tleaf.setFoldOrigin(originY);\n\n\t\t\tflipState = {\n\t\t\t\tleaf,\n\t\t\t\tdirection,\n\t\t\t\tstartingPos,\n\t\t\t\tstartingY:\n\t\t\t\t\tthis.pendingFlipStartingY ??\n\t\t\t\t\t(Number.isFinite(event.center.y) ? event.center.y : undefined),\n\t\t\t\tcornerY: originY < 0.5 ? 0 : 1,\n\t\t\t\tdelta: 0,\n\t\t\t\tisDuringAutoFlip: false,\n\t\t\t};\n\t\t\tthis.activeFlips.set(leaf.index, flipState);\n\t\t}\n\n\t\t// Update delta\n\t\tflipState.delta = delta;\n\t\tconst page =\n\t\t\tflipState.direction === FlipDirection.Backward\n\t\t\t\t? (flipState.leaf.pages[1] ?? flipState.leaf.pages[0])\n\t\t\t\t: flipState.leaf.pages[0];\n\t\tconst pageWidth = Number.parseFloat(page.style.width);\n\t\tconst pageHeight = Number.parseFloat(page.style.height);\n\t\tconst hasDimensions =\n\t\t\tNumber.isFinite(pageWidth) && pageWidth > 0 && Number.isFinite(pageHeight) && pageHeight > 0;\n\t\tconst rigid = flipState.leaf.pages.some((side) => side?.dataset.isCoverPage === \"1\");\n\t\tif (hasDimensions && !rigid && flipState.leaf.pages[1]) {\n\t\t\tconst dy =\n\t\t\t\tNumber.isFinite(event.center.y) && flipState.startingY !== undefined\n\t\t\t\t\t? event.center.y - flipState.startingY\n\t\t\t\t\t: 0;\n\t\t\tflipState.leaf.setManualFold({\n\t\t\t\tx: (flipState.direction === FlipDirection.Forward ? 1 : -1) - delta / pageWidth,\n\t\t\t\ty: (flipState.cornerY ?? 1) + dy / pageHeight,\n\t\t\t});\n\t\t} else {\n\t\t\tconst travel = Number.isFinite(pageWidth) && pageWidth > 0 ? pageWidth * 2 : bookWidth;\n\t\t\tconst position =\n\t\t\t\tflipState.direction === FlipDirection.Forward ? delta / travel : 1 + delta / travel;\n\t\t\tflipState.leaf.setManualPosition(Math.max(0, Math.min(1, position)));\n\t\t}\n\t}\n\n\tprivate getFoldPointerY(leaf: Leaf, direction: FlipDirection, pointerY: number): number {\n\t\tconst page =\n\t\t\tdirection === FlipDirection.Backward ? (leaf.pages[1] ?? leaf.pages[0]) : leaf.pages[0];\n\t\tconst height = Number.parseFloat(page.style.height);\n\t\tconst top = Number.parseFloat(page.style.top);\n\t\tif (!Number.isFinite(pointerY) || !Number.isFinite(height) || height <= 0) return 0.75;\n\t\tconst bookTop = this.bookElement?.getBoundingClientRect().top ?? 0;\n\t\treturn Math.max(\n\t\t\t0,\n\t\t\tMath.min(1, (pointerY - bookTop - (Number.isFinite(top) ? top : 0)) / height),\n\t\t);\n\t}\n\n\tprivate onDragEnd(event: HammerInput, cancelled = false) {\n\t\tthis.gestureStartedInNoFlipZone = false;\n\t\t// Suppress the click that the browser fires after pointerup following a drag.\n\t\tthis._suppressNextClick = true;\n\n\t\tconst flipState = this.currentManualFlip;\n\t\tif (!flipState) {\n\t\t\tthis.pendingFlipDirection = FlipDirection.None;\n\t\t\tthis.pendingFlipStartingPos = 0;\n\t\t\tthis.pendingFlipStartingY = undefined;\n\t\t\tthis.isDragging = false;\n\t\t\tthis.clearHoverShadow();\n\t\t\treturn;\n\t\t}\n\n\t\tconst ppsX = event.velocityX * 1000; // pixels per second\n\t\tlet flipTo: FlipPosition;\n\n\t\tswitch (flipState.direction) {\n\t\t\tcase FlipDirection.Forward:\n\t\t\t\tif (\n\t\t\t\t\t!cancelled &&\n\t\t\t\t\t((this.isLTR ? ppsX < -this.fastDeltaThreshold : ppsX > this.fastDeltaThreshold) ||\n\t\t\t\t\t\tflipState.leaf.flipPosition >= 0.5)\n\t\t\t\t) {\n\t\t\t\t\tflipTo = 1;\n\t\t\t\t} else {\n\t\t\t\t\tflipTo = 0;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase FlipDirection.Backward:\n\t\t\t\tif (\n\t\t\t\t\t!cancelled &&\n\t\t\t\t\t((this.isLTR ? ppsX > this.fastDeltaThreshold : ppsX < -this.fastDeltaThreshold) ||\n\t\t\t\t\t\tflipState.leaf.flipPosition <= 0.5)\n\t\t\t\t) {\n\t\t\t\t\tflipTo = 0;\n\t\t\t\t} else {\n\t\t\t\t\tflipTo = 1;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\t// Safety: release the drag-level flip lock if direction is unexpected.\n\t\t\t\tflipState.leaf.endFlip();\n\t\t\t\tthis.activeFlips.delete(flipState.leaf.index);\n\t\t\t\tthis.pendingFlipDirection = FlipDirection.None;\n\t\t\t\tthis.pendingFlipStartingPos = 0;\n\t\t\t\tthis.pendingFlipStartingY = undefined;\n\t\t\t\tthis.isDragging = false;\n\t\t\t\treturn;\n\t\t}\n\n\t\t// Mark as auto-flip and reset pending state\n\t\tflipState.isDuringAutoFlip = true;\n\t\tthis.pendingFlipDirection = FlipDirection.None;\n\t\tthis.pendingFlipStartingPos = 0;\n\t\tthis.pendingFlipStartingY = undefined;\n\t\tthis.isDragging = false;\n\n\t\tconst targetPageIndex =\n\t\t\tflipTo === 1 ? 2 * (flipState.leaf.index + 1) : 2 * flipState.leaf.index;\n\t\tconst direction: PageFlipDirection = flipTo === 1 ? \"forward\" : \"backward\";\n\t\tthis.lastFlipParams = this.buildPageFlipParams(targetPageIndex, direction);\n\t\tif (this.onPageFlipping) this.onPageFlipping(this.lastFlipParams);\n\n\t\t// Complete the flip asynchronously - don't block new flips!\n\t\tflipState.leaf.flipToPosition(flipTo).then(() => {\n\t\t\t// Release the drag-level flip lock acquired in onDragUpdate.\n\t\t\t// The animation's own beginFlip/endFlip is ref-counted inside, so\n\t\t\t// the snapshot stays active until this final endFlip() zeroes it.\n\t\t\tflipState.leaf.endFlip();\n\t\t\tthis.activeFlips.delete(flipState.leaf.index);\n\t\t});\n\t}\n\n\tprivate handleTouchStart = (e: TouchEvent) => {\n\t\tthis._suppressNextClick = false;\n\t\tif (e.touches.length > 1) {\n\t\t\treturn;\n\t\t}\n\t\tconst touch = e.touches[0];\n\t\tthis.touchStartingPos = { x: touch.pageX, y: touch.pageY };\n\t\tthis.touchStartedInNoFlipZone = this.isInsideNoFlipZone(e.target);\n\t\tthis.gestureStartedInNoFlipZone = this.touchStartedInNoFlipZone;\n\t\tthis.touchDirectionLock = \"none\";\n\t};\n\n\tprivate handleTouchMove = (e: TouchEvent) => {\n\t\tif (e.touches.length > 1) {\n\t\t\treturn;\n\t\t}\n\t\t// Allow horizontal scroll (e.g. carousel) when touch started inside a no-flip zone\n\t\tif (this.touchStartedInNoFlipZone) {\n\t\t\treturn;\n\t\t}\n\t\tconst touch = e.touches[0];\n\t\tconst deltaX = touch.pageX - this.touchStartingPos.x;\n\t\tconst deltaY = touch.pageY - this.touchStartingPos.y;\n\n\t\t// Lock direction on the first significant movement. Once locked the\n\t\t// decision is final for the rest of this gesture — prevents the old\n\t\t// bug where accumulated vertical delta kept overruling a late\n\t\t// horizontal change-of-direction within the same gesture.\n\t\tif (this.touchDirectionLock === \"none\") {\n\t\t\tconst absDx = Math.abs(deltaX);\n\t\t\tconst absDy = Math.abs(deltaY);\n\t\t\tif (absDx > FlipBook.DIRECTION_LOCK_THRESHOLD || absDy > FlipBook.DIRECTION_LOCK_THRESHOLD) {\n\t\t\t\tthis.touchDirectionLock = absDx >= absDy ? \"horizontal\" : \"vertical\";\n\t\t\t}\n\t\t}\n\n\t\tif (this.touchDirectionLock === \"horizontal\") {\n\t\t\te.preventDefault();\n\t\t}\n\t\t// 'vertical' or 'none' → let the browser handle native scroll\n\t};\n\n\tprivate handleMouseMove = throttle(MOUSE_MOVE_THROTTLE_MS, (event: MouseEvent) => {\n\t\tif (!this.bookElement) return;\n\t\tif (\n\t\t\tthis.mouseMode === \"select\" ||\n\t\t\tevent.buttons !== 0 ||\n\t\t\tthis.isDragging ||\n\t\t\tthis.currentManualFlip ||\n\t\t\tthis.activeFlips.size > 0\n\t\t) {\n\t\t\tthis.clearHoverShadow();\n\t\t\treturn;\n\t\t}\n\n\t\tconst rect = this.bookElement.getBoundingClientRect();\n\t\tif (rect.width <= 0 || rect.height <= 0) {\n\t\t\tthis.clearHoverShadow();\n\t\t\treturn;\n\t\t}\n\n\t\tconst x = ((event.clientX - rect.left) * this.bookElement.clientWidth) / rect.width;\n\t\tconst y = ((event.clientY - rect.top) * this.bookElement.clientHeight) / rect.height;\n\t\tconst spineX = this.bookElement.clientWidth / 2;\n\t\tconst isRightSide = x >= spineX;\n\t\tconst isForward = this.isLTR === isRightSide;\n\t\tconst direction = isForward ? FlipDirection.Forward : FlipDirection.Backward;\n\t\tconst leaf = this.getNextAvailableLeaf(direction);\n\t\tif (!leaf) {\n\t\t\tthis.clearHoverShadow();\n\t\t\treturn;\n\t\t}\n\n\t\t// Use resting page dimensions: the preview itself transforms the page bounds.\n\t\tconst page = isForward ? leaf.pages[0] : (leaf.pages[1] ?? leaf.pages[0]);\n\t\tconst width = Number.parseFloat(page.style.width);\n\t\tconst height = Number.parseFloat(page.style.height);\n\t\tconst top = Number.parseFloat(page.style.top);\n\t\tconst distanceFromEdge = isRightSide ? spineX + width - x : x - (spineX - width);\n\t\tconst edgeZone = width * EDGE_ZONE_RATIO;\n\t\tif (\n\t\t\t!Number.isFinite(distanceFromEdge) ||\n\t\t\t!Number.isFinite(y) ||\n\t\t\t!Number.isFinite(height) ||\n\t\t\t!Number.isFinite(top) ||\n\t\t\twidth <= 0 ||\n\t\t\theight <= 0 ||\n\t\t\tdistanceFromEdge < 0 ||\n\t\t\tdistanceFromEdge > edgeZone ||\n\t\t\ty < top ||\n\t\t\ty > top + height\n\t\t) {\n\t\t\tthis.clearHoverShadow();\n\t\t\treturn;\n\t\t}\n\t\tconst edgeProgress = 1 - Math.min(1, distanceFromEdge / edgeZone);\n\t\tconst hoverStrength = edgeProgress * HOVER_STRENGTH_MAX;\n\n\t\tif (this.hoveredLeaf && this.hoveredLeaf !== leaf) {\n\t\t\tthis.hoveredLeaf.setHoverShadow(0);\n\t\t}\n\t\tif (this.hoveredLeaf !== leaf) {\n\t\t\tleaf.setFoldOrigin((y - top) / height);\n\t\t}\n\t\tthis.hoveredLeaf = leaf;\n\t\tleaf.setHoverShadow(hoverStrength);\n\t});\n\n\tprivate handleMouseLeave = () => {\n\t\tthis.handleMouseMove.cancel({ upcomingOnly: true });\n\t\tthis.clearHoverShadow();\n\t};\n\n\t/**\n\t * Capture-phase click handler that suppresses clicks fired by the browser\n\t * immediately after a drag gesture ends. Without this, releasing the pointer\n\t * on an interactive element (button, link) inside a page would trigger both\n\t * the flip animation AND the element's click handler.\n\t */\n\tprivate handleClickAfterDrag = (e: Event) => {\n\t\tif (this._suppressNextClick) {\n\t\t\tthis._suppressNextClick = false;\n\t\t\te.stopPropagation();\n\t\t\te.preventDefault();\n\t\t}\n\t};\n\n\tprivate clearHoverShadow() {\n\t\tif (this.hoveredLeaf) {\n\t\t\tthis.hoveredLeaf.setHoverShadow(0);\n\t\t\tthis.hoveredLeaf = undefined;\n\t\t}\n\t}\n\tprivate onTurned(\n\t\tcurrentVisiblePageIndices: [number] | [number, number],\n\t\tprevVisibilePageIndices?: [number] | [number, number],\n\t) {\n\t\tfor (let i = 0; i < this.pageElements.length; i++) {\n\t\t\tconst pageElement = this.pageElements[i];\n\t\t\tconst inCurrent = currentVisiblePageIndices.includes(i);\n\t\t\tconst inPrev = prevVisibilePageIndices?.includes(i);\n\t\t\tconst action = inCurrent\n\t\t\t\t? pageElement.classList.add\n\t\t\t\t: !prevVisibilePageIndices || !inPrev\n\t\t\t\t\t? () => null\n\t\t\t\t\t: pageElement.classList.remove;\n\t\t\taction.bind(pageElement.classList)(\"current-page\");\n\t\t}\n\t\t// Update leaves buffer visibility after turn completes\n\t\tthis.updateLeavesBufferVisibility();\n\t\t// Notify React/consumer and sync history\n\t\tthis.syncHistoryAndNotifyFlipped(false);\n\t\t// TODO expose to outside using https://github.com/open-draft/strict-event-emitter, and just be a consumer internally.\n\t\t// TODO: set prev-page / next-page classes for prev/next pages as accordingally\n\t}\n\t/**\n\t * Get the index of the current (leftmost visible) page.\n\t * Returns 0 if no pages are visible.\n\t */\n\tget currentPageIndex(): number {\n\t\treturn this.prevVisiblePageIndices?.[0] ?? 0;\n\t}\n\n\t/**\n\t * Get the total number of pages in the flipbook.\n\t */\n\tget totalPages(): number {\n\t\treturn this.pagesCount;\n\t}\n\n\t/**\n\t * Check if the book is currently showing the first page(s).\n\t */\n\tget isFirstPage(): boolean {\n\t\treturn this.currentPageIndex === 0;\n\t}\n\n\t/**\n\t * Check if the book is currently showing the last page(s).\n\t */\n\tget isLastPage(): boolean {\n\t\tconst lastPageIndex = this.pagesCount - 1;\n\t\treturn this.prevVisiblePageIndices?.includes(lastPageIndex) ?? false;\n\t}\n\n\t/**\n\t * Check if there's an active auto-flip in the given direction.\n\t */\n\tprivate hasActiveAutoFlipInDirection(direction: FlipDirection): boolean {\n\t\tfor (const state of this.activeFlips.values()) {\n\t\t\tif (state.isDuringAutoFlip && state.direction === direction) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n\n\t/**\n\t * Animate flip to the next page.\n\t * @returns Promise that resolves when the flip animation completes\n\t */\n\tasync flipNext(): Promise<void> {\n\t\t// Block if there's an active auto-flip in the opposite direction\n\t\tif (this.hasActiveAutoFlipInDirection(FlipDirection.Backward)) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst leaf = this.getNextAvailableLeaf(FlipDirection.Forward);\n\t\tif (!leaf) return;\n\t\tthis.clearHoverShadow();\n\n\t\tconst flipState: FlipState = {\n\t\t\tleaf,\n\t\t\tdirection: FlipDirection.Forward,\n\t\t\tstartingPos: 0,\n\t\t\tdelta: 0,\n\t\t\tisDuringAutoFlip: true,\n\t\t};\n\t\tthis.activeFlips.set(leaf.index, flipState);\n\t\tconst targetPageIndex = this.currentPageIndex + 2;\n\t\tthis.lastFlipParams = this.buildPageFlipParams(targetPageIndex, \"forward\");\n\t\tif (this.onPageFlipping) this.onPageFlipping(this.lastFlipParams);\n\n\t\ttry {\n\t\t\tawait leaf.flipToPosition(1);\n\t\t} finally {\n\t\t\tthis.activeFlips.delete(leaf.index);\n\t\t}\n\t}\n\n\t/**\n\t * Animate flip to the previous page.\n\t * @returns Promise that resolves when the flip animation completes\n\t */\n\tasync flipPrev(): Promise<void> {\n\t\t// Block if there's an active auto-flip in the opposite direction\n\t\tif (this.hasActiveAutoFlipInDirection(FlipDirection.Forward)) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst leaf = this.getNextAvailableLeaf(FlipDirection.Backward);\n\t\tif (!leaf) return;\n\t\tthis.clearHoverShadow();\n\n\t\tconst flipState: FlipState = {\n\t\t\tleaf,\n\t\t\tdirection: FlipDirection.Backward,\n\t\t\tstartingPos: 0,\n\t\t\tdelta: 0,\n\t\t\tisDuringAutoFlip: true,\n\t\t};\n\t\tthis.activeFlips.set(leaf.index, flipState);\n\t\tconst targetPageIndex = this.currentPageIndex - 2;\n\t\tthis.lastFlipParams = this.buildPageFlipParams(Math.max(0, targetPageIndex), \"backward\");\n\t\tif (this.onPageFlipping) this.onPageFlipping(this.lastFlipParams);\n\n\t\ttry {\n\t\t\tawait leaf.flipToPosition(0);\n\t\t} finally {\n\t\t\tthis.activeFlips.delete(leaf.index);\n\t\t}\n\t}\n\n\t/**\n\t * Animate to a specific page index.\n\t * Flips through pages sequentially to reach the target.\n\t * @param pageIndex - The target page index (0-based)\n\t * @returns Promise that resolves when all flip animations complete\n\t */\n\tasync flipToPage(pageIndex: number): Promise<void> {\n\t\tif (pageIndex < 0 || pageIndex >= this.pagesCount) {\n\t\t\tconsole.warn(\n\t\t\t\t`flipToPage: Invalid page index ${pageIndex}. Must be between 0 and ${this.pagesCount - 1}.`,\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\t// Compute the last-turned-leaf index that makes pageIndex visible,\n\t\t// using the same formula as jumpToPage.\n\t\tconst targetLastTurned = pageIndex <= 0 ? -1 : Math.floor((pageIndex - 1) / 2);\n\t\tconst currentLastTurned =\n\t\t\tthis.currentPageIndex <= 0 ? -1 : Math.floor((this.currentPageIndex - 1) / 2);\n\n\t\tif (targetLastTurned === currentLastTurned) {\n\t\t\treturn; // Already at the target spread\n\t\t}\n\n\t\tif (targetLastTurned > currentLastTurned) {\n\t\t\tconst steps = targetLastTurned - currentLastTurned;\n\t\t\tfor (let i = 0; i < steps; i++) {\n\t\t\t\tawait this.flipNext();\n\t\t\t}\n\t\t} else {\n\t\t\tconst steps = currentLastTurned - targetLastTurned;\n\t\t\tfor (let i = 0; i < steps; i++) {\n\t\t\t\tawait this.flipPrev();\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Jump to a specific page instantly without animation.\n\t * @param pageIndex - The target page index (0-based)\n\t */\n\tjumpToPage(pageIndex: number): void {\n\t\tif (pageIndex < 0 || pageIndex >= this.pagesCount) {\n\t\t\tconsole.warn(\n\t\t\t\t`jumpToPage: Invalid page index ${pageIndex}. Must be between 0 and ${this.pagesCount - 1}.`,\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\tconst isLastPage = pageIndex === this.pagesCount - 1;\n\t\tconst isOddPage = pageIndex % 2 === 1;\n\t\t// If targeting the last page and it's odd (back of leaf), close the book reversed\n\t\tconst closeBookReversed = isLastPage && isOddPage;\n\n\t\t// Determine which leaf should be the last one turned.\n\t\t// A leaf's pages are visible as follows:\n\t\t// - At position 0 (not turned): its even page (front) is visible\n\t\t// - At position 1 (turned): its odd page (back) is visible\n\t\t// The visible spread after turning leaf k is [2k+1, 2k+2]:\n\t\t// back of leaf k (odd) + front of leaf k+1 (even).\n\t\t// For page 0, no leaves need to be turned (initial spread [0, 1]).\n\t\tlet lastTurnedLeafIndex: number;\n\t\tif (pageIndex <= 0) {\n\t\t\tlastTurnedLeafIndex = -1; // No leaves turned\n\t\t} else if (closeBookReversed) {\n\t\t\tlastTurnedLeafIndex = this.leaves.length - 1; // All leaves turned\n\t\t} else {\n\t\t\tlastTurnedLeafIndex = Math.floor((pageIndex - 1) / 2);\n\t\t}\n\n\t\t// Set all leaves up to lastTurnedLeafIndex as turned, all after as not turned\n\t\tfor (let i = 0; i < this.leaves.length; i++) {\n\t\t\tconst leaf = this.leaves[i];\n\t\t\tconst shouldBeTurned = i <= lastTurnedLeafIndex;\n\n\t\t\tif (shouldBeTurned && !leaf.isTurned) {\n\t\t\t\t// Turn this leaf instantly\n\t\t\t\tleaf.flipPosition = 1;\n\t\t\t\tthis.applyLeafTransform(leaf, 1);\n\t\t\t} else if (!shouldBeTurned && leaf.isTurned) {\n\t\t\t\t// Unturn this leaf instantly\n\t\t\t\tleaf.flipPosition = 0;\n\t\t\t\tthis.applyLeafTransform(leaf, 0);\n\t\t\t}\n\t\t}\n\n\t\t// Update visible page indices\n\t\tif (closeBookReversed) {\n\t\t\t// Closed reversed: only show the last page\n\t\t\tthis.prevVisiblePageIndices = [pageIndex];\n\t\t} else if (lastTurnedLeafIndex < 0) {\n\t\t\t// No leaves turned: initial spread\n\t\t\tthis.prevVisiblePageIndices = 1 < this.pagesCount ? [0, 1] : [0];\n\t\t} else {\n\t\t\tconst firstVisiblePageIndex = lastTurnedLeafIndex * 2 + 1;\n\t\t\tthis.prevVisiblePageIndices =\n\t\t\t\tfirstVisiblePageIndex + 1 < this.pagesCount\n\t\t\t\t\t? [firstVisiblePageIndex, firstVisiblePageIndex + 1]\n\t\t\t\t\t: [firstVisiblePageIndex];\n\t\t}\n\n\t\t// Update current-page classes\n\t\tfor (let i = 0; i < this.pageElements.length; i++) {\n\t\t\tconst pageElement = this.pageElements[i];\n\t\t\tif (this.prevVisiblePageIndices.includes(i)) {\n\t\t\t\tpageElement.classList.add(\"current-page\");\n\t\t\t} else {\n\t\t\t\tpageElement.classList.remove(\"current-page\");\n\t\t\t}\n\t\t}\n\n\t\t// Update leaves buffer visibility\n\t\tthis.updateLeavesBufferVisibility();\n\n\t\t// Notify callback and sync history\n\t\tthis.syncHistoryAndNotifyFlipped(false);\n\t}\n\n\t/**\n\t * Show a page selected by an external router or browser history entry without\n\t * adding another history entry. Page-change callbacks still run.\n\t */\n\trestorePage(pageIndex: number): void {\n\t\tconst wasRestoring = this._isRestoringFromHistory;\n\t\tthis._isRestoringFromHistory = true;\n\t\ttry {\n\t\t\tthis.jumpToPage(pageIndex);\n\t\t} finally {\n\t\t\tthis._isRestoringFromHistory = wasRestoring;\n\t\t}\n\t}\n\n\t/**\n\t * Apply transform styles to a leaf's pages for a given flip position.\n\t * Used for instant positioning (jumpToPage).\n\t */\n\tprivate applyLeafTransform(leaf: Leaf, position: number): void {\n\t\tleaf.applyTransform(position);\n\t}\n\n\t/**\n\t * Clean up event listeners and Hammer instance.\n\t * Should be called when the FlipBook is no longer needed.\n\t */\n\tdestroy() {\n\t\tif (typeof window !== \"undefined\" && this._boundPopstate) {\n\t\t\twindow.removeEventListener(\"popstate\", this._boundPopstate, true);\n\t\t\tthis._boundPopstate = undefined;\n\t\t}\n\t\tif (this.resizeObserver) {\n\t\t\tthis.resizeObserver.disconnect();\n\t\t\tthis.resizeObserver = undefined;\n\t\t}\n\t\tif (this.hammer) {\n\t\t\tthis.hammer.destroy();\n\t\t\tthis.hammer = undefined;\n\t\t}\n\t\tif (this.bookElement) {\n\t\t\tfor (const page of this.pageElements) {\n\t\t\t\tpage.removeEventListener(\"pointerdown\", this.handlePageMouseDown);\n\t\t\t\tpage.removeEventListener(\"mousedown\", this.handlePageMouseDown);\n\t\t\t}\n\t\t\tthis.bookElement.removeEventListener(\"touchstart\", this.handleTouchStart as EventListener);\n\t\t\tthis.bookElement.removeEventListener(\"touchmove\", this.handleTouchMove as EventListener);\n\t\t\tthis.bookElement.removeEventListener(\n\t\t\t\t\"mousemove\",\n\t\t\t\tthis.handleMouseMove as unknown as EventListener,\n\t\t\t);\n\t\t\tthis.bookElement.removeEventListener(\"mouseleave\", this.handleMouseLeave as EventListener);\n\t\t\tthis.bookElement.removeEventListener(\n\t\t\t\t\"click\",\n\t\t\t\tthis.handleClickAfterDrag as EventListener,\n\t\t\t\ttrue,\n\t\t\t);\n\t\t}\n\t}\n}\n\nexport { FlipBook, type PageSemantics };\nexport type {\n\tFlipPageSemantic,\n\tHistoryMapper,\n\tMouseMode,\n\tPageFlipDirection,\n\tPageFlipParams,\n} from \"./flip-book-options\";\n"],"names":["FlipDirection","foldStrength","position","clamp","value","lower","upper","progress","dimension","clipAtCrease","polygon","normal","midpoint","side","clipped","distance","point","previous","previousDistance","current","currentDistance","previousInside","currentInside","fraction","unique","index","next","constrainDestination","width","height","target","corner","cornerY","destination","anchors","tolerance","inside","anchor","allowedError","candidates","dx","dy","scale","length","candidate","other","score","nearest","getPageFoldDestination","pointerOffset","w","h","p","strength","inward","offset","travel","computePageFoldFromPoint","deltaX","deltaY","separation","projection","reflection","sheet","computePageFold","easePageTurn","scheduleFrame","cb","SHADOW_STRENGTH_MULTIPLIER","HIGHLIGHT_STRENGTH_MULTIPLIER","Leaf","pages","isFlipped","bookProperties","onTurned","normalizedY","size","fold","previousPosition","previousPoint","clamped","page","flipPosition","velocity","resolve","currentFlipPosition","startingFoldPoint","targetFoldPoint","duration","start","step","timestamp","elapsed","easedProgress","newPosition","throttle","clampedPosition","pageShadow","previewPosition","paperSize","shadowStrength","highlightStrength","shadowFromFlip","shadowProgress","isLTR","isOdd","rotateY","translateX","scaleX","origin","edge","folded","beforeSpine","leafOrder","faceOrder","manualFold","backwardPreview","a","b","c","d","tx","ty","offsetX","front","mirror","nx","mx","span","minimum","stop","angle","band","opacity","AspectRatioImpl","aspectRatio","Size","rhsAspectRatio","rhsAspectRatioValue","DEFAULT_FAST_DELTA","EDGE_ZONE_RATIO","HOVER_STRENGTH_MAX","MOUSE_MOVE_THROTTLE_MS","historyOwnerEpoch","nextHistoryOwner","FlipBook","el","pageIndex","leftmostPageIndex","direction","rightPageIndex","pageIndices","semantics","isInitial","semantic","params","route","currentState","state","url","event","secondLeafIndex","i","leaf","options","setTocPageIndex","mode","coverPageIndicesSet","indices","pageIdx","coverSize","leafSize","containerWidth","containerHeight","pageElement","pageSize","selector","debug","bookElement","pageElements","leavesCount","leafIndex","isOddPage","isInitiallyTurned","firstVisibleLeafIndex","isCurrentPage","currentVisiblePageIndices","prevVisiblePageIndices","initialPosition","lastTurnedLeafIndex","firstVisiblePageIndex","lastLeafFirstPage","Hammer","leftLeaf","rightLeaf","currentLeafIndex","bufferStart","bufferEnd","coverLeafIndices","isWithinBuffer","visible","debugBar","activeFlipsInfo","idx","flipState","currentPos","bookWidth","startingPos","delta","verticalMove","isRightSide","originY","pageWidth","pageHeight","hasDimensions","rigid","pointerY","top","bookTop","cancelled","ppsX","flipTo","targetPageIndex","e","touch","absDx","absDy","rect","x","y","spineX","isForward","distanceFromEdge","edgeZone","hoverStrength","prevVisibilePageIndices","inCurrent","inPrev","lastPageIndex","targetLastTurned","currentLastTurned","steps","isLastPage","closeBookReversed","shouldBeTurned","wasRestoring"],"mappings":";;;AAAO,IAAKA,sBAAAA,OACXA,EAAA,UAAU,WACVA,EAAA,WAAW,YACXA,EAAA,OAAO,QAHIA,IAAAA,KAAA,CAAA,CAAA;ACeZ,SAASC,EAAaC,GAA0B;AAC/C,SAAOA,MAAa,KAAKA,MAAa,IAAI,IAAI,KAAK,IAAI,KAAK,KAAKA,CAAQ;AAC1E;AAEA,SAASC,EAAMC,GAAeC,GAAeC,GAAuB;AACnE,SAAO,KAAK,IAAID,GAAO,KAAK,IAAIC,GAAOF,CAAK,CAAC;AAC9C;AAEA,SAASG,EAASH,GAAuB;AACxC,SAAO,OAAO,MAAMA,CAAK,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AAA,EAMQ,mBAAmBlF,GAAYjK,GAAwB;AAC9D,IAAAiK,EAAK,eAAejK,CAAQ;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,UAAU;AAaT,QAZI,OAAO,SAAW,OAAe,KAAK,mBACzC,OAAO,oBAAoB,YAAY,KAAK,gBAAgB,EAAI,GAChE,KAAK,iBAAiB,SAEnB,KAAK,mBACR,KAAK,eAAe,WAAA,GACpB,KAAK,iBAAiB,SAEnB,KAAK,WACR,KAAK,OAAO,QAAA,GACZ,KAAK,SAAS,SAEX,KAAK,aAAa;AACrB,iBAAW+E,KAAQ,KAAK;AACvB,QAAAA,EAAK,oBAAoB,eAAe,KAAK,mBAAmB,GAChEA,EAAK,oBAAoB,aAAa,KAAK,mBAAmB;AAE/D,WAAK,YAAY,oBAAoB,cAAc,KAAK,gBAAiC,GACzF,KAAK,YAAY,oBAAoB,aAAa,KAAK,eAAgC,GACvF,KAAK,YAAY;AAAA,QAChB;AAAA,QACA,KAAK;AAAA,MAAA,GAEN,KAAK,YAAY,oBAAoB,cAAc,KAAK,gBAAiC,GACzF,KAAK,YAAY;AAAA,QAChB;AAAA,QACA,KAAK;AAAA,QACL;AAAA,MAAA;AAAA,IAEF;AAAA,EACD;AACD;"}
1
+ {"version":3,"file":"flipbook.js","sources":["../src/flip-direction.ts","../src/page-fold.ts","../src/leaf.ts","../src/aspect-ratio.ts","../src/size.ts","../src/flipbook.ts"],"sourcesContent":["export enum FlipDirection {\n\tForward = \"Forward\",\n\tBackward = \"Backward\",\n\tNone = \"None\",\n}\n","export interface Point {\n\tx: number;\n\ty: number;\n}\n\nexport interface PageFold {\n\tdestination: Point;\n\tfront: Point[];\n\tfolded: Point[];\n\treflection: [number, number, number, number, number, number];\n\tnormal: Point;\n\tmidpoint: Point;\n\tstrength: number;\n}\n\nfunction foldStrength(position: number): number {\n\treturn position === 0 || position === 1 ? 0 : Math.sin(Math.PI * position);\n}\n\nfunction clamp(value: number, lower: number, upper: number): number {\n\treturn Math.max(lower, Math.min(upper, value));\n}\n\nfunction progress(value: number): number {\n\treturn Number.isNaN(value) ? 0 : clamp(value, 0, 1);\n}\n\nfunction dimension(value: number): number {\n\treturn Number.isFinite(value) && value > 0 ? Math.min(value, Number.MAX_SAFE_INTEGER) : 1;\n}\n\nfunction clipAtCrease(polygon: Point[], normal: Point, midpoint: Point, side: number): Point[] {\n\tconst clipped: Point[] = [];\n\tconst distance = (point: Point) =>\n\t\tside * ((point.x - midpoint.x) * normal.x + (point.y - midpoint.y) * normal.y);\n\tlet previous = polygon[polygon.length - 1];\n\tlet previousDistance = distance(previous);\n\n\tfor (const current of polygon) {\n\t\tconst currentDistance = distance(current);\n\t\tconst previousInside = previousDistance >= 0;\n\t\tconst currentInside = currentDistance >= 0;\n\t\tif (previousInside !== currentInside) {\n\t\t\tconst fraction = previousDistance / (previousDistance - currentDistance);\n\t\t\tclipped.push({\n\t\t\t\tx: previous.x + (current.x - previous.x) * fraction,\n\t\t\t\ty: previous.y + (current.y - previous.y) * fraction,\n\t\t\t});\n\t\t}\n\t\tif (currentInside) clipped.push(current);\n\t\tprevious = current;\n\t\tpreviousDistance = currentDistance;\n\t}\n\n\tconst unique = clipped.filter((point, index) => {\n\t\tconst next = clipped[(index + 1) % clipped.length];\n\t\treturn point.x !== next.x || point.y !== next.y;\n\t});\n\treturn unique.length < 3 ? [] : unique;\n}\n\nfunction constrainDestination(\n\twidth: number,\n\theight: number,\n\ttarget: Point,\n\tcorner: \"top\" | \"bottom\",\n): Point {\n\tconst cornerY = corner === \"top\" ? 0 : height;\n\tconst destination = {\n\t\tx: Number.isFinite(target.x) ? target.x : width,\n\t\ty: Number.isFinite(target.y) ? target.y : cornerY,\n\t};\n\tconst anchors = [\n\t\t{ x: 0, y: cornerY, radius: width },\n\t\t{ x: 0, y: height - cornerY, radius: Math.hypot(width, height) },\n\t];\n\tconst tolerance = Number.EPSILON * Math.max(width, height, 1) * 8;\n\tconst inside = (point: Point, anchor: (typeof anchors)[number], allowedError = tolerance) =>\n\t\tMath.hypot(point.x - anchor.x, point.y - anchor.y) <= anchor.radius + allowedError;\n\tif (anchors.every((anchor) => inside(destination, anchor, 0))) return destination;\n\n\t// A closest point lies on an admissible circle arc or at one of their intersections.\n\tconst candidates = [\n\t\t{ x: width, y: cornerY },\n\t\t{ x: -width, y: cornerY },\n\t];\n\tfor (const anchor of anchors) {\n\t\tconst dx = destination.x - anchor.x;\n\t\tconst dy = destination.y - anchor.y;\n\t\tconst scale = Math.max(Math.abs(dx), Math.abs(dy));\n\t\tif (scale === 0) continue;\n\t\tconst length = Math.hypot(dx / scale, dy / scale);\n\t\tconst candidate = {\n\t\t\tx: anchor.x + ((dx / scale) * anchor.radius) / length,\n\t\t\ty: anchor.y + ((dy / scale) * anchor.radius) / length,\n\t\t};\n\t\tif (anchors.every((other) => inside(candidate, other))) candidates.push(candidate);\n\t}\n\tconst scale = Math.max(width, height, Math.abs(destination.x), Math.abs(destination.y), 1);\n\t// Remove the common target-distance term and scale before comparing large requests.\n\tconst score = (point: Point) =>\n\t\t(point.x * point.x + point.y * point.y) / scale -\n\t\t2 * ((destination.x / scale) * point.x + (destination.y / scale) * point.y);\n\treturn candidates.reduce((nearest, candidate) =>\n\t\tscore(candidate) < score(nearest) ? candidate : nearest,\n\t);\n}\n\n/** Return the lifted corner's constrained position for an automatic turn. */\nexport function getPageFoldDestination(\n\twidth: number,\n\theight: number,\n\tposition: number,\n\tcorner: \"top\" | \"bottom\",\n\tpointerOffset = 0,\n): Point {\n\tconst w = dimension(width);\n\tconst h = dimension(height);\n\tconst p = progress(position);\n\tconst strength = foldStrength(p);\n\tconst inward = corner === \"top\" ? 1 : -1;\n\tconst cornerY = corner === \"top\" ? 0 : h;\n\tconst offset = Number.isFinite(pointerOffset) ? pointerOffset : 0;\n\tconst travel = clamp(\n\t\tstrength * Math.min(h * 0.18, w * 0.28) + inward * offset * strength * h,\n\t\t0,\n\t\th * 0.6,\n\t);\n\treturn constrainDestination(w, h, { x: w * (1 - 2 * p), y: cornerY + inward * travel }, corner);\n}\n\n/** Reflect a lifted corner toward a real pointer destination, with the entire spine fixed. */\nexport function computePageFoldFromPoint(\n\twidth: number,\n\theight: number,\n\ttarget: Point,\n\tcorner: \"top\" | \"bottom\",\n): PageFold {\n\tconst w = dimension(width);\n\tconst h = dimension(height);\n\tconst cornerY = corner === \"top\" ? 0 : h;\n\tconst destination = constrainDestination(w, h, target, corner);\n\n\tconst deltaX = w - destination.x;\n\tconst deltaY = cornerY - destination.y;\n\tconst separation = Math.hypot(deltaX, deltaY);\n\tconst normal =\n\t\tseparation === 0 ? { x: 1, y: 0 } : { x: deltaX / separation, y: deltaY / separation };\n\tconst midpoint = {\n\t\tx: (w + destination.x) / 2,\n\t\ty: (cornerY + destination.y) / 2,\n\t};\n\tconst projection = midpoint.x * normal.x + midpoint.y * normal.y;\n\tconst reflection: PageFold[\"reflection\"] = [\n\t\t1 - 2 * normal.x * normal.x,\n\t\t-2 * normal.x * normal.y,\n\t\t-2 * normal.x * normal.y,\n\t\t1 - 2 * normal.y * normal.y,\n\t\t2 * normal.x * projection,\n\t\t2 * normal.y * projection,\n\t];\n\tconst sheet = [\n\t\t{ x: 0, y: 0 },\n\t\t{ x: w, y: 0 },\n\t\t{ x: w, y: h },\n\t\t{ x: 0, y: h },\n\t];\n\treturn {\n\t\tdestination,\n\t\tfront: clipAtCrease(sheet, normal, midpoint, -1),\n\t\tfolded: clipAtCrease(sheet, normal, midpoint, 1),\n\t\treflection,\n\t\tnormal,\n\t\tmidpoint,\n\t\tstrength: foldStrength(progress((w - destination.x) / (2 * w))),\n\t};\n}\n\n/** Reflect the lifted region across a crease in coordinates with the spine at x = 0. */\nexport function computePageFold(\n\twidth: number,\n\theight: number,\n\tposition: number,\n\tcorner: \"top\" | \"bottom\",\n\tpointerOffset = 0,\n): PageFold {\n\tconst destination = getPageFoldDestination(width, height, position, corner, pointerOffset);\n\treturn {\n\t\t...computePageFoldFromPoint(width, height, destination, corner),\n\t\tstrength: foldStrength(progress(position)),\n\t};\n}\n\n/** Match the resting speed at each end while maintaining an exact final position. */\nexport function easePageTurn(value: number): number {\n\tconst p = progress(value);\n\treturn p === 0 || p === 1 ? p : (1 - Math.cos(Math.PI * p)) / 2;\n}\n","import { throttle } from \"throttle-debounce\";\nimport type { IntRange } from \"type-fest\";\nimport { FlipDirection } from \"./flip-direction\";\nimport {\n\tcomputePageFold,\n\tcomputePageFoldFromPoint,\n\teasePageTurn,\n\ttype PageFold,\n\ttype Point,\n} from \"./page-fold\";\n\n/** Schedule a frame callback; fallback for test envs where requestAnimationFrame is missing. Called at invoke time so test mocks apply. */\nfunction scheduleFrame(cb: (timestamp: number) => void): number {\n\treturn typeof requestAnimationFrame !== \"undefined\"\n\t\t? requestAnimationFrame(cb)\n\t\t: (setTimeout(() => cb(performance.now()), 0) as unknown as number);\n}\n\n/** Multiplier for shadow intensity (slightly stronger than base) */\nconst SHADOW_STRENGTH_MULTIPLIER = 1.1;\n/** Multiplier for highlight intensity (slightly weaker than base) */\nconst HIGHLIGHT_STRENGTH_MULTIPLIER = 0.9;\n\n// number between 1 to infinity\nexport type DegreesPerSecond = IntRange<1, typeof Infinity>;\nexport type FlipPosition = IntRange<0, 2>;\nexport const FLIPPED = true;\nexport const NOT_FLIPPED = false;\nexport class Leaf {\n\tprivate currentAnimation: Promise<void> | null = null;\n\tprivate targetFlipPosition: FlipPosition | null = null;\n\tprivate wrappedFlipPosition: number;\n\tprivate hoverShadow = 0;\n\t/** Ref-count for nested beginFlip/endFlip pairs (drag wrapping animation). */\n\tprivate flipRefCount = 0;\n\tprivate foldOrigin = 0.75;\n\tprivate foldPointer = 0.75;\n\tprivate manualNeedsSettlement = false;\n\tprivate manualFoldPoint: Point | undefined;\n\n\tsetFoldOrigin(normalizedY: number): void {\n\t\tthis.foldOrigin = Math.max(0, Math.min(1, normalizedY));\n\t\tthis.foldPointer = this.foldOrigin;\n\t}\n\n\tsetFoldPointer(normalizedY: number): void {\n\t\tthis.foldPointer = Math.max(0, Math.min(1, normalizedY));\n\t}\n\n\t/** Follow a held pointer without queuing animations or reporting a completed turn. */\n\tsetManualPosition(position: number): void {\n\t\tthis.manualFoldPoint = undefined;\n\t\tconst previous = this.flipPosition;\n\t\tthis.updatePosition(position);\n\t\tthis.manualNeedsSettlement ||= previous !== this.flipPosition;\n\t\tthis.applyTransform(this.flipPosition);\n\t}\n\n\t/** Track the lifted corner in page coordinates, including movement beyond the page edges. */\n\tsetManualFold(point: Point): void {\n\t\tconst size = this.getPaperSize();\n\t\tif (!size) {\n\t\t\tthis.setManualPosition(Number.isFinite(point.x) ? (1 - point.x) / 2 : this.flipPosition);\n\t\t\treturn;\n\t\t}\n\t\tconst fold = computePageFoldFromPoint(\n\t\t\tsize.width,\n\t\t\tsize.height,\n\t\t\t{ x: point.x * size.width, y: point.y * size.height },\n\t\t\tthis.foldOrigin < 0.5 ? \"top\" : \"bottom\",\n\t\t);\n\t\tthis.updateManualFold({\n\t\t\tx: fold.destination.x / size.width,\n\t\t\ty: fold.destination.y / size.height,\n\t\t});\n\t}\n\n\tprivate updateManualFold(point: Point): void {\n\t\tconst previousPosition = this.flipPosition;\n\t\tconst previousPoint = this.manualFoldPoint;\n\t\tthis.updatePosition((1 - point.x) / 2);\n\t\tthis.manualFoldPoint = this.flipPosition === 0 || this.flipPosition === 1 ? undefined : point;\n\t\tthis.manualNeedsSettlement ||=\n\t\t\tpreviousPosition !== this.flipPosition ||\n\t\t\t(previousPoint !== undefined && previousPoint.y !== point.y);\n\t\tthis.applyTransform(this.flipPosition);\n\t}\n\n\tconstructor(\n\t\treadonly index: number,\n\t\treadonly pages: [HTMLElement, HTMLElement | undefined],\n\t\tisFlipped: boolean,\n\t\tprivate readonly bookProperties: {\n\t\t\tisLTR: boolean;\n\t\t\tpagesCount: number;\n\t\t\tleavesCount: number;\n\t\t\tpageShadow: boolean;\n\t\t\tsnapshotDuringFlip: boolean;\n\t\t},\n\t\tprivate readonly onTurned: (direction: FlipDirection) => void,\n\t) {\n\t\tthis.wrappedFlipPosition = isFlipped ? 1 : 0;\n\t\t// TODO: rethink this\n\t\t// if(isFlipped) {\n\t\t// // this.flipToPosition(1);\n\t\t// }\n\t}\n\n\tget isTurned(): boolean {\n\t\treturn this.flipPosition === 1;\n\t}\n\tget isTurning(): boolean {\n\t\treturn this.flipPosition !== 0;\n\t}\n\tget isCover(): boolean {\n\t\treturn this.isFirst || this.isLast;\n\t}\n\tget isFirst(): boolean {\n\t\treturn this.index === 0;\n\t}\n\tget isLast(): boolean {\n\t\treturn this.index === this.bookProperties.leavesCount - 1;\n\t}\n\tset flipPosition(value: number) {\n\t\tthis.updatePosition(value);\n\t\tif (this.flipPosition === 0 || this.flipPosition === 1) {\n\t\t\tthis.manualNeedsSettlement = false;\n\t\t}\n\t}\n\tprivate updatePosition(value: number): void {\n\t\tthis.wrappedFlipPosition = Math.max(0, Math.min(1, value)) as FlipPosition;\n\t\tif (this.wrappedFlipPosition === 0 || this.wrappedFlipPosition === 1) {\n\t\t\tthis.manualFoldPoint = undefined;\n\t\t}\n\t}\n\tget flipPosition(): number {\n\t\treturn this.wrappedFlipPosition;\n\t}\n\n\tsetHoverShadow(progress: number): void {\n\t\tconst clamped = Math.max(0, Math.min(1, progress));\n\t\tif (this.hoverShadow === clamped) return;\n\t\tthis.hoverShadow = clamped;\n\t\tthis.applyTransform(this.flipPosition);\n\t}\n\n\t/**\n\t * Signal that a flip interaction is starting (drag or animation).\n\t * Ref-counted: the snapshot class is added on the first call and\n\t * removed only when every `beginFlip` has a matching `endFlip`.\n\t * This keeps the GPU layer and scrollbar/selection lockdown stable\n\t * across the full drag→release→animation cycle.\n\t */\n\tbeginFlip(): void {\n\t\tthis.flipRefCount++;\n\t\tif (this.flipRefCount !== 1) return; // already promoted\n\t\tfor (const page of this.pages) {\n\t\t\tif (!page) continue;\n\t\t\tpage.style.willChange = \"transform\";\n\t\t\tif (this.bookProperties.snapshotDuringFlip) {\n\t\t\t\tpage.classList.add(\"page--flipping\");\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Signal that a flip interaction finished (drag or animation).\n\t * Only releases the GPU layer when every `beginFlip` is balanced.\n\t */\n\tendFlip(): void {\n\t\tthis.flipRefCount = Math.max(0, this.flipRefCount - 1);\n\t\tif (this.flipRefCount !== 0) return; // still active\n\t\tif (!this.manualFoldPoint || this.flipPosition === 0 || this.flipPosition === 1) {\n\t\t\tthis.manualFoldPoint = undefined;\n\t\t\tthis.foldOrigin = 0.75;\n\t\t\tthis.foldPointer = this.foldOrigin;\n\t\t}\n\t\tfor (const page of this.pages) {\n\t\t\tif (!page) continue;\n\t\t\tpage.style.willChange = \"\";\n\t\t\tif (this.bookProperties.snapshotDuringFlip) {\n\t\t\t\tpage.classList.remove(\"page--flipping\");\n\t\t\t}\n\t\t}\n\t}\n\n\tasync flipToPosition(\n\t\tflipPosition: FlipPosition,\n\t\tvelocity: DegreesPerSecond = 225 as DegreesPerSecond,\n\t) {\n\t\tif (this.currentAnimation) {\n\t\t\tawait this.currentAnimation;\n\t\t}\n\n\t\tif (this.flipPosition === flipPosition) {\n\t\t\tif (flipPosition === 0 || flipPosition === 1) {\n\t\t\t\tthis.manualFoldPoint = undefined;\n\t\t\t\tthis.applyTransform(flipPosition);\n\t\t\t}\n\t\t\tif (this.manualNeedsSettlement && (flipPosition === 0 || flipPosition === 1)) {\n\t\t\t\tthis.manualNeedsSettlement = false;\n\t\t\t\tthis.onTurned(flipPosition === 1 ? FlipDirection.Forward : FlipDirection.Backward);\n\t\t\t}\n\t\t\treturn Promise.resolve();\n\t\t}\n\n\t\tif (this.targetFlipPosition === flipPosition) {\n\t\t\treturn this.currentAnimation ?? Promise.resolve();\n\t\t}\n\n\t\tthis.targetFlipPosition = flipPosition;\n\t\t// Promote to GPU layer before animation starts for smooth compositing\n\t\tthis.beginFlip();\n\n\t\tthis.currentAnimation = new Promise<void>((resolve) => {\n\t\t\tconst currentFlipPosition = this.flipPosition;\n\t\t\tconst startingFoldPoint = this.manualFoldPoint && { ...this.manualFoldPoint };\n\t\t\tconst targetFoldPoint = {\n\t\t\t\tx: 1 - 2 * flipPosition,\n\t\t\t\ty: this.foldOrigin < 0.5 ? 0 : 1,\n\t\t\t};\n\t\t\tconst distance = Math.abs(flipPosition - currentFlipPosition);\n\t\t\tconst duration = ((distance * 180) / velocity) * 1000; // duration in milliseconds\n\n\t\t\tconst start = performance.now();\n\t\t\tconst step = (timestamp: number) => {\n\t\t\t\tconst elapsed = timestamp - start;\n\n\t\t\t\tif (elapsed < 0) {\n\t\t\t\t\tscheduleFrame(step);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tconst progress = Math.min(elapsed / duration, 1);\n\t\t\t\tconst easedProgress = easePageTurn(progress);\n\t\t\t\tconst newPosition =\n\t\t\t\t\tcurrentFlipPosition + easedProgress * (flipPosition - currentFlipPosition);\n\t\t\t\tif (startingFoldPoint) {\n\t\t\t\t\tthis.manualFoldPoint = {\n\t\t\t\t\t\tx: startingFoldPoint.x + easedProgress * (targetFoldPoint.x - startingFoldPoint.x),\n\t\t\t\t\t\ty: startingFoldPoint.y + easedProgress * (targetFoldPoint.y - startingFoldPoint.y),\n\t\t\t\t\t};\n\t\t\t\t\tif (progress === 1 && (flipPosition === 0 || flipPosition === 1)) {\n\t\t\t\t\t\tthis.manualFoldPoint = undefined;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Ensure the new position is within valid bounds [0, 1]\n\t\t\t\tthis.flipPosition = Math.max(0, Math.min(1, newPosition)) as FlipPosition;\n\t\t\t\tthis.applyTransform(newPosition);\n\t\t\t\tif (progress < 1) {\n\t\t\t\t\tscheduleFrame(step);\n\t\t\t\t} else {\n\t\t\t\t\tthis.currentAnimation = null;\n\t\t\t\t\tthis.targetFlipPosition = null;\n\t\t\t\t\tthis.manualNeedsSettlement = false;\n\t\t\t\t\tif (this.flipPosition === 0 || this.flipPosition === 1) {\n\t\t\t\t\t\tthis.onTurned(this.flipPosition === 1 ? FlipDirection.Forward : FlipDirection.Backward);\n\t\t\t\t\t}\n\t\t\t\t\t// Release GPU layer after animation completes\n\t\t\t\t\tthis.endFlip();\n\t\t\t\t\tresolve();\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tscheduleFrame(step);\n\t\t});\n\n\t\treturn this.currentAnimation;\n\t}\n\n\tasync efficientFlipToPosition(\n\t\tflipPosition: FlipPosition,\n\t\tvelocity: DegreesPerSecond = 20000 as DegreesPerSecond,\n\t) {\n\t\treturn throttle(1, this.flipToPosition.bind(this))(flipPosition, velocity);\n\t}\n\n\tapplyTransform(position: number): void {\n\t\tconst clampedPosition = Math.max(0, Math.min(1, position));\n\t\tconst { pageShadow } = this.bookProperties;\n\t\tconst previewPosition =\n\t\t\tclampedPosition === 0\n\t\t\t\t? this.hoverShadow * 0.22\n\t\t\t\t: clampedPosition === 1\n\t\t\t\t\t? 1 - this.hoverShadow * 0.22\n\t\t\t\t\t: clampedPosition;\n\t\tconst width = Number.parseFloat(this.pages[0].style.width);\n\t\tconst height = Number.parseFloat(this.pages[0].style.height);\n\t\tconst paperSize = this.getPaperSize();\n\t\tif (paperSize && this.manualFoldPoint) {\n\t\t\tconst fold = computePageFoldFromPoint(\n\t\t\t\twidth,\n\t\t\t\theight,\n\t\t\t\t{ x: this.manualFoldPoint.x * width, y: this.manualFoldPoint.y * height },\n\t\t\t\tthis.foldOrigin < 0.5 ? \"top\" : \"bottom\",\n\t\t\t);\n\t\t\tthis.manualFoldPoint = { x: fold.destination.x / width, y: fold.destination.y / height };\n\t\t\tthis.updatePosition((1 - this.manualFoldPoint.x) / 2);\n\t\t\tif (this.flipPosition > 0 && this.flipPosition < 1) {\n\t\t\t\tthis.applyPaperFold(width, height, this.flipPosition, fold);\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t\tconst usePaperFold = paperSize && previewPosition > 0 && previewPosition < 1;\n\t\tif (usePaperFold) {\n\t\t\tthis.applyPaperFold(width, height, previewPosition, undefined, clampedPosition === 1);\n\t\t\treturn;\n\t\t}\n\n\t\t// Only compute shadow values when shadow is enabled — skipping these\n\t\t// saves CSS custom-property mutations per page per animation frame.\n\t\tlet shadowStrength = 0;\n\t\tlet highlightStrength = 0;\n\t\tif (pageShadow) {\n\t\t\tconst shadowFromFlip = Math.sin(clampedPosition * Math.PI);\n\t\t\tconst shadowProgress = Math.max(shadowFromFlip, this.hoverShadow);\n\t\t\tshadowStrength = Math.min(1, shadowProgress * SHADOW_STRENGTH_MULTIPLIER);\n\t\t\thighlightStrength = Math.min(1, shadowProgress * HIGHLIGHT_STRENGTH_MULTIPLIER);\n\t\t}\n\n\t\tthis.pages.forEach((page, index) => {\n\t\t\tconst isLTR = this.bookProperties.isLTR;\n\t\t\tif (!page) return;\n\t\t\tpage.classList.remove(\"page--folded\");\n\t\t\tpage.style.clipPath = \"\";\n\t\t\tpage.style.removeProperty(\"--fold-shading\");\n\n\t\t\tconst isOdd = (index % 2) + 1 === 1;\n\t\t\tconst degrees = isOdd\n\t\t\t\t? isLTR\n\t\t\t\t\t? clampedPosition > 0.5\n\t\t\t\t\t\t? 180 - clampedPosition * 180\n\t\t\t\t\t\t: -clampedPosition * 180\n\t\t\t\t\t: clampedPosition > 0.5\n\t\t\t\t\t\t? -(180 - clampedPosition * 180)\n\t\t\t\t\t\t: clampedPosition * 180\n\t\t\t\t: isLTR\n\t\t\t\t\t? clampedPosition < 0.5\n\t\t\t\t\t\t? -clampedPosition * 180\n\t\t\t\t\t\t: 180 - clampedPosition * 180\n\t\t\t\t\t: clampedPosition < 0.5\n\t\t\t\t\t\t? clampedPosition * 180\n\t\t\t\t\t\t: -(180 - clampedPosition * 180);\n\t\t\tconst rotateY = `${degrees}deg`;\n\t\t\tconst translateX = `${isOdd ? (isLTR ? \"100%\" : \"-100%\") : \"0px\"}`;\n\t\t\tconst scaleX = isOdd ? (clampedPosition > 0.5 ? -1 : 1) : clampedPosition < 0.5 ? -1 : 1;\n\t\t\tconst origin = isOdd ? (isLTR ? \"left\" : \"right\") : isLTR ? \"right\" : \"left\";\n\n\t\t\tpage.style.transform = `translateX(${translateX})rotateY(${rotateY})scaleX(${scaleX})`;\n\t\t\tpage.style.transformOrigin = origin;\n\t\t\tpage.style.zIndex = this.getSurfaceZIndex(page, clampedPosition, index, false);\n\n\t\t\tif (pageShadow) {\n\t\t\t\tconst edge = origin === \"left\" ? \"right\" : \"left\";\n\t\t\t\tpage.style.setProperty(\"--inner-shadow-shadow\", shadowStrength.toFixed(3));\n\t\t\t\tpage.style.setProperty(\"--inner-shadow-highlight\", highlightStrength.toFixed(3));\n\t\t\t\tpage.style.setProperty(\"--inner-shadow-edge\", edge);\n\t\t\t}\n\t\t});\n\t}\n\n\tprivate getPaperSize(): { width: number; height: number } | undefined {\n\t\tconst width = Number.parseFloat(this.pages[0].style.width);\n\t\tconst height = Number.parseFloat(this.pages[0].style.height);\n\t\tif (\n\t\t\t!this.pages[1] ||\n\t\t\tthis.pages.some((page) => page?.dataset.isCoverPage === \"1\") ||\n\t\t\t!Number.isFinite(width) ||\n\t\t\t!Number.isFinite(height) ||\n\t\t\twidth <= 0 ||\n\t\t\theight <= 0\n\t\t) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn { width, height };\n\t}\n\n\tprivate getSurfaceZIndex(\n\t\tpage: HTMLElement,\n\t\tposition: number,\n\t\tside: number,\n\t\tfolded: boolean,\n\t): string {\n\t\tif (position === 0 || position === 1) {\n\t\t\treturn `${\n\t\t\t\tposition === 1\n\t\t\t\t\t? page.dataset.pageIndex\n\t\t\t\t\t: this.bookProperties.pagesCount - Number(page.dataset.pageIndex)\n\t\t\t}`;\n\t\t}\n\t\t// All moving leaves share a stack above the flat pages, including rigid boards.\n\t\t// Leaf order reverses across the spine; rigid faces also swap their visible side.\n\t\tconst beforeSpine = position < 0.5;\n\t\tconst leafOrder = beforeSpine ? -this.index * 2 : this.index * 2;\n\t\tconst faceOrder = folded || !beforeSpine ? side : 1 - side;\n\t\treturn `${this.bookProperties.pagesCount * 2 + leafOrder + faceOrder}`;\n\t}\n\n\tprivate applyPaperFold(\n\t\twidth: number,\n\t\theight: number,\n\t\tposition: number,\n\t\tmanualFold?: PageFold,\n\t\tbackwardPreview = false,\n\t): void {\n\t\tconst fold =\n\t\t\tmanualFold ??\n\t\t\tcomputePageFold(\n\t\t\t\twidth,\n\t\t\t\theight,\n\t\t\t\tbackwardPreview ? 1 - position : position,\n\t\t\t\tthis.foldOrigin < 0.5 ? \"top\" : \"bottom\",\n\t\t\t\tthis.foldPointer - this.foldOrigin,\n\t\t\t);\n\t\tconst [a, b, c, d, tx, ty] = fold.reflection;\n\t\t// A resting back face curls from its own outer corner, with its body stationary.\n\t\tconst isLTR = backwardPreview ? !this.bookProperties.isLTR : this.bookProperties.isLTR;\n\t\tconst offsetX = backwardPreview ? 0 : isLTR ? width : -width;\n\t\tfor (let side = 0; side < this.pages.length; side++) {\n\t\t\tconst page = this.pages[side];\n\t\t\tif (!page) continue;\n\t\t\tconst front = side === (backwardPreview ? 1 : 0);\n\t\t\tconst mirror = front ? !isLTR : isLTR;\n\t\t\tconst polygon = (front ? fold.front : fold.folded).map((point) => ({\n\t\t\t\tx: mirror ? width - point.x : point.x,\n\t\t\t\ty: point.y,\n\t\t\t}));\n\t\t\tpage.classList.add(\"page--folded\");\n\t\t\tpage.style.transformOrigin = \"0px 0px\";\n\t\t\tpage.style.transform = front\n\t\t\t\t? `translateX(${offsetX}px)`\n\t\t\t\t: isLTR\n\t\t\t\t\t? `matrix(${-a}, ${-b}, ${c}, ${d}, ${offsetX + a * width + tx}, ${b * width + ty})`\n\t\t\t\t\t: `matrix(${-a}, ${b}, ${-c}, ${d}, ${offsetX + width - tx}, ${ty})`;\n\t\t\tpage.style.clipPath =\n\t\t\t\tpolygon.length < 3\n\t\t\t\t\t? \"polygon(0px 0px, 0px 0px, 0px 0px)\"\n\t\t\t\t\t: `polygon(${polygon.map((point) => `${point.x.toFixed(3)}px ${point.y.toFixed(3)}px`).join(\", \")})`;\n\t\t\tpage.style.zIndex = this.getSurfaceZIndex(\n\t\t\t\tpage,\n\t\t\t\tposition,\n\t\t\t\tbackwardPreview ? 1 - side : side,\n\t\t\t\ttrue,\n\t\t\t);\n\t\t\tif (this.bookProperties.pageShadow) {\n\t\t\t\tthis.applyCreaseShading(\n\t\t\t\t\tpage,\n\t\t\t\t\twidth,\n\t\t\t\t\theight,\n\t\t\t\t\tfold.normal,\n\t\t\t\t\tfold.midpoint,\n\t\t\t\t\tmirror,\n\t\t\t\t\tfold.strength,\n\t\t\t\t\tfront,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate applyCreaseShading(\n\t\tpage: HTMLElement,\n\t\twidth: number,\n\t\theight: number,\n\t\tnormal: Point,\n\t\tmidpoint: Point,\n\t\tmirror: boolean,\n\t\tstrength: number,\n\t\tfront: boolean,\n\t): void {\n\t\tconst nx = mirror ? -normal.x : normal.x;\n\t\tconst mx = mirror ? width - midpoint.x : midpoint.x;\n\t\tconst span = Math.abs(nx) * width + Math.abs(normal.y) * height;\n\t\tconst minimum = Math.min(0, nx * width) + Math.min(0, normal.y * height);\n\t\tconst stop = ((nx * mx + normal.y * midpoint.y - minimum) / span) * 100;\n\t\tconst angle = (Math.atan2(nx, -normal.y) * 180) / Math.PI;\n\t\tconst band = Math.min(18, (100 * Math.min(width * 0.12, 48)) / span);\n\t\tconst opacity = (strength * (front ? 0.24 : 0.3)).toFixed(3);\n\t\tpage.style.setProperty(\n\t\t\t\"--fold-shading\",\n\t\t\tfront\n\t\t\t\t? `linear-gradient(${angle}deg, transparent ${stop - band}%, rgba(0,0,0,${opacity}) ${stop}%, transparent ${stop + 0.5}%)`\n\t\t\t\t: `linear-gradient(${angle}deg, rgba(0,0,0,${opacity}) ${stop}%, rgba(255,255,255,${(strength * 0.35).toFixed(3)}) ${stop + band * 0.3}%, transparent ${stop + band}%)`,\n\t\t);\n\t}\n}\n","export interface AspectRatio {\n\twidth: number;\n\theight: number;\n}\nexport class AspectRatioImpl implements AspectRatio {\n\tstatic from(aspectRatio: AspectRatio) {\n\t\treturn new AspectRatioImpl(aspectRatio.width, aspectRatio.height);\n\t}\n\tconstructor(\n\t\tpublic readonly width: number,\n\t\tpublic readonly height: number,\n\t) {}\n\tget value(): number {\n\t\treturn this.width / this.height;\n\t}\n}\n","import { type AspectRatio, AspectRatioImpl } from \"./aspect-ratio\";\n\nexport class Size {\n\taspectRatio: AspectRatioImpl;\n\taspectRatioFit(rhsAspectRatio: AspectRatio) {\n\t\tconst rhsAspectRatioValue = AspectRatioImpl.from(rhsAspectRatio).value;\n\t\treturn this.aspectRatio.value > rhsAspectRatioValue\n\t\t\t? new Size(this.height * rhsAspectRatioValue, this.height)\n\t\t\t: new Size(this.width, this.width / rhsAspectRatioValue);\n\t}\n\tconstructor(\n\t\tpublic readonly width: number,\n\t\tpublic readonly height: number,\n\t) {\n\t\tthis.aspectRatio = new AspectRatioImpl(width, height);\n\t}\n\tget asString(): string {\n\t\treturn `${this.width}x${this.height}`;\n\t}\n}\n","import \"./pages.scss\";\nimport \"./flipbook.scss\";\nimport \"./mouse-mode.scss\";\nimport Hammer from \"hammerjs\";\nimport { throttle } from \"throttle-debounce\";\nimport type { AspectRatio } from \"./aspect-ratio\";\nimport type { DownloadConfig } from \"./download/types\";\nimport type {\n\tFlipBookOptions,\n\tFlipPageSemantic,\n\tHistoryMapper,\n\tMouseMode,\n\tPageFlipDirection,\n\tPageFlipParams,\n} from \"./flip-book-options\";\nimport { FlipDirection } from \"./flip-direction\";\nimport { type FlipPosition, Leaf } from \"./leaf\";\nimport type { PageSemantics } from \"./page-semantics\";\nimport { Size } from \"./size\";\nimport { setTocPageIndex } from \"./store\";\n\n/** Default threshold for fast flip detection (in ms) */\nconst DEFAULT_FAST_DELTA = 500;\n\n/** Percentage of a page's width that triggers its edge preview */\nconst EDGE_ZONE_RATIO = 0.18;\n/** Maximum hover shadow strength (0-1) */\nconst HOVER_STRENGTH_MAX = 0.12;\n/** Throttle interval for mouse move handler in ms */\nconst MOUSE_MOVE_THROTTLE_MS = 16;\nconst historyOwnerEpoch = Date.now().toString(36);\nlet nextHistoryOwner = 0;\n\n/** State for a single flip operation - enables concurrent page flipping */\ninterface FlipState {\n\tleaf: Leaf;\n\tdirection: FlipDirection;\n\tstartingPos: number;\n\tstartingY?: number;\n\tcornerY?: number;\n\tdelta: number;\n\tisDuringAutoFlip: boolean;\n}\n\n/**\n * Core FlipBook class for creating realistic page-flip animations.\n *\n * @example\n * ```typescript\n * import { FlipBook } from 'html-flip-book-vanilla';\n *\n * const flipBook = new FlipBook({\n * pagesCount: 10,\n * direction: 'ltr',\n * });\n *\n * flipBook.render('#book-container');\n *\n * // Navigate programmatically\n * await flipBook.flipNext();\n * await flipBook.flipToPage(5);\n * ```\n */\nclass FlipBook {\n\tbookElement?: HTMLElement;\n\tprivate pageElements: HTMLElement[] = [];\n\tprivate readonly pagesCount: number;\n\tprivate readonly leafAspectRatio: AspectRatio = { width: 2, height: 3 };\n\tprivate readonly coverAspectRatio: AspectRatio = {\n\t\twidth: 2.15,\n\t\theight: 3.15,\n\t};\n\tprivate readonly direction: \"rtl\" | \"ltr\" = \"ltr\";\n\tprivate mouseMode: MouseMode = \"turn\";\n\tprivate readonly fastDeltaThreshold: number = DEFAULT_FAST_DELTA;\n\tprivate readonly initialTurnedLeaves: Set<number> = new Set();\n\tprivate readonly onPageChanged?: (pageIndex: number) => void;\n\tprivate readonly onPageFlipping?: (params: PageFlipParams) => void;\n\tprivate readonly onPageFlipped?: (params: PageFlipParams) => void;\n\tprivate readonly historyMapper?: HistoryMapper;\n\tprivate lastFlipParams: PageFlipParams | undefined;\n\tprivate _historyInitialized = false;\n\tprivate _isRestoringFromHistory = false;\n\tprivate _boundPopstate: ((event: PopStateEvent) => void) | undefined;\n\tprivate readonly _historyOwner = `${historyOwnerEpoch}-${++nextHistoryOwner}`;\n\tprivate readonly pageSemantics: PageSemantics | undefined;\n\tprivate readonly leavesBuffer?: number;\n\tprivate readonly coverPageIndices?: number[] | \"auto\";\n\tprivate readonly downloadConfig: DownloadConfig | undefined;\n\tprivate readonly pageShadow: boolean;\n\tprivate readonly snapshotDuringFlip: boolean;\n\tprivate leaves: Leaf[] = [];\n\t// flipping state - supports concurrent page flipping\n\tprivate activeFlips: Map<number, FlipState> = new Map();\n\tprivate pendingFlipStartingPos = 0;\n\tprivate pendingFlipStartingY: number | undefined;\n\tprivate pendingFlipDirection: FlipDirection = FlipDirection.None;\n\tprivate isDragging = false;\n\tprivate gestureStartedInNoFlipZone = false;\n\t/** Set after a drag ends; the next click event is suppressed so child buttons/links don't fire. */\n\tprivate _suppressNextClick = false;\n\tprivate hoveredLeaf: Leaf | undefined;\n\ttouchStartingPos = { x: 0, y: 0 };\n\t/** True when the current touch started inside a [data-flipbook-no-flip] element (e.g. carousel). */\n\tprivate touchStartedInNoFlipZone = false;\n\t/**\n\t * Direction lock for the current touch gesture.\n\t * Once the first significant movement (>5 px) determines the dominant axis,\n\t * the lock stays for the rest of the gesture. 'vertical' means native scroll\n\t * wins — Hammer pan events are rejected. 'horizontal' means every touchmove\n\t * gets preventDefault() so Hammer can drive the page flip.\n\t */\n\ttouchDirectionLock: \"none\" | \"vertical\" | \"horizontal\" = \"none\";\n\tprivate static readonly DIRECTION_LOCK_THRESHOLD = 5;\n\tprivate prevVisiblePageIndices: [number] | [number, number] | undefined;\n\t// Hammer instance for cleanup\n\tprivate hammer: HammerManager | undefined;\n\t// Resize observer for re-layout on container size changes\n\tprivate resizeObserver: ResizeObserver | undefined;\n\n\tprivate static readonly NO_FLIP_SELECTOR = \"[data-flipbook-no-flip]\";\n\n\tprivate isInsideNoFlipZone(el: EventTarget | null): boolean {\n\t\treturn el instanceof Element && el.closest(FlipBook.NO_FLIP_SELECTOR) != null;\n\t}\n\n\tprivate getSemanticForPage(pageIndex: number): FlipPageSemantic | undefined {\n\t\tif (!this.pageSemantics) return undefined;\n\t\treturn {\n\t\t\tsemanticName: this.pageSemantics.indexToSemanticName(pageIndex),\n\t\t\ttitle: this.pageSemantics.indexToTitle(pageIndex),\n\t\t};\n\t}\n\n\tprivate buildPageFlipParams(\n\t\tleftmostPageIndex: number,\n\t\tdirection: PageFlipDirection,\n\t): PageFlipParams {\n\t\tconst rightPageIndex = Math.min(leftmostPageIndex + 1, this.pagesCount - 1);\n\t\tconst pageIndices: [number, number] = [leftmostPageIndex, rightPageIndex];\n\t\tconst semantics: [FlipPageSemantic | undefined, FlipPageSemantic | undefined] = [\n\t\t\tthis.getSemanticForPage(leftmostPageIndex),\n\t\t\tthis.getSemanticForPage(rightPageIndex),\n\t\t];\n\t\tconst leafIndex = Math.floor(leftmostPageIndex / 2);\n\t\treturn { leafIndex, pageIndices, semantics, direction };\n\t}\n\n\tprivate syncHistoryAndNotifyFlipped(isInitial: boolean): void {\n\t\tconst pageIndex = this.currentPageIndex;\n\t\tconst semantic = this.getSemanticForPage(pageIndex);\n\t\tif (this.onPageChanged) this.onPageChanged(pageIndex);\n\t\tconst params = this.lastFlipParams ?? this.buildPageFlipParams(pageIndex, \"forward\");\n\t\tif (this.onPageFlipped) this.onPageFlipped(params);\n\t\tif (typeof window !== \"undefined\" && this.historyMapper && !this._isRestoringFromHistory) {\n\t\t\tconst route = this.historyMapper.pageToRoute(pageIndex, semantic);\n\t\t\t// When pageToRoute returns null the consumer signals \"this page has\n\t\t\t// no meaningful URL\" (e.g. cover, TOC). Skip the pushState entirely\n\t\t\t// to avoid framework-level routing side-effects.\n\t\t\tif (route == null) return;\n\t\t\t// Keep the current entry's framework state when writing a book route.\n\t\t\t// Next.js, for example, marks SPA entries in history.state and reloads\n\t\t\t// the document on Back if that marker is missing.\n\t\t\tconst currentState = window.history.state;\n\t\t\tconst state =\n\t\t\t\tcurrentState !== null && typeof currentState === \"object\" && !Array.isArray(currentState)\n\t\t\t\t\t? { ...currentState, route, __flipBookOwner: this._historyOwner }\n\t\t\t\t\t: { route, __flipBookOwner: this._historyOwner };\n\t\t\tconst url = route.startsWith(\"#\")\n\t\t\t\t? `${window.location.pathname}${window.location.search}${route}`\n\t\t\t\t: route;\n\t\t\t// Use the *native* History.prototype methods rather than\n\t\t\t// window.history.pushState/replaceState. Frameworks like Next.js\n\t\t\t// monkey-patch the instance methods to intercept URL changes and\n\t\t\t// trigger full soft navigations (RSC fetch + React re-render).\n\t\t\t// That is catastrophically expensive during rapid page flips.\n\t\t\t// Calling the prototype method bypasses the patch while still\n\t\t\t// updating the browser URL bar and history stack correctly.\n\t\t\tif (isInitial || !this._historyInitialized) {\n\t\t\t\tHistory.prototype.replaceState.call(window.history, state, \"\", url);\n\t\t\t\tthis._historyInitialized = true;\n\t\t\t} else {\n\t\t\t\tHistory.prototype.pushState.call(window.history, state, \"\", url);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate handlePopstate = (event: PopStateEvent): void => {\n\t\tif (!this.historyMapper || typeof window === \"undefined\") return;\n\t\t// Framework routers also observe popstate. A route written by this book\n\t\t// already has its page restored below; letting a router traverse it again\n\t\t// can remount the entire book despite the navigation being same-document.\n\t\tif (event.state?.__flipBookOwner !== this._historyOwner) return;\n\t\tconst route =\n\t\t\t(typeof window.history.state === \"object\" && window.history.state?.route) ||\n\t\t\twindow.location.pathname + window.location.search + window.location.hash;\n\t\tconst pageIndex = this.historyMapper.routeToPage(route);\n\t\tif (pageIndex !== null && pageIndex >= 0 && pageIndex < this.pagesCount) {\n\t\t\tevent.stopImmediatePropagation();\n\t\t\tthis.restorePage(pageIndex);\n\t\t}\n\t};\n\n\tprivate get isLTR(): boolean {\n\t\treturn this.direction === \"ltr\";\n\t}\n\n\tprivate get currentOrTurningLeaves(): [Leaf | undefined, Leaf | undefined] {\n\t\tlet secondLeafIndex = -1;\n\t\tfor (let i = this.leaves.length - 1; i >= 0; i--) {\n\t\t\tconst leaf = this.leaves[i];\n\t\t\tif (leaf.isTurned || leaf.isTurning) {\n\t\t\t\tsecondLeafIndex = leaf.index + 1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn secondLeafIndex === -1\n\t\t\t? [undefined, this.leaves[0]]\n\t\t\t: secondLeafIndex === this.leaves.length\n\t\t\t\t? [this.leaves[secondLeafIndex - 1], undefined]\n\t\t\t\t: [this.leaves[secondLeafIndex - 1], this.leaves[secondLeafIndex]];\n\t}\n\n\tconstructor(options: FlipBookOptions) {\n\t\tthis.pagesCount = options.pagesCount;\n\t\tthis.leafAspectRatio = options.leafAspectRatio || this.leafAspectRatio;\n\t\tthis.coverAspectRatio = options.coverAspectRatio || this.coverAspectRatio;\n\t\tthis.direction = options.direction || this.direction;\n\t\tthis.mouseMode = options.mouseMode ?? \"turn\";\n\t\tthis.fastDeltaThreshold = options.fastDeltaThreshold ?? this.fastDeltaThreshold;\n\t\tthis.initialTurnedLeaves = new Set(options.initialTurnedLeaves ?? []);\n\t\tthis.pageSemantics = options.pageSemantics;\n\t\tthis.onPageChanged = options.onPageChanged;\n\t\tthis.onPageFlipping = options.onPageFlipping;\n\t\tthis.onPageFlipped = options.onPageFlipped;\n\t\tthis.historyMapper = options.enableHistory !== false ? options.historyMapper : undefined;\n\t\tthis.leavesBuffer = options.leavesBuffer;\n\t\tthis.coverPageIndices = options.coverPageIndices;\n\t\tthis.downloadConfig = options.enableDownload !== false ? options.downloadConfig : undefined;\n\t\tthis.pageShadow = options.pageShadow ?? true;\n\t\tthis.snapshotDuringFlip = options.snapshotDuringFlip ?? true;\n\t\tthis._tocPageIndex = options.tocPageIndex ?? 4;\n\t\t// Keep the module-level store in sync for backward-compat\n\t\tsetTocPageIndex(this._tocPageIndex);\n\t}\n\n\tprivate _tocPageIndex = 4;\n\n\t/** Get the configured table of contents page index. */\n\tgetTocPageIndex(): number {\n\t\treturn this._tocPageIndex;\n\t}\n\n\tgetMouseMode(): MouseMode {\n\t\treturn this.mouseMode;\n\t}\n\n\tsetMouseMode(mode: MouseMode): void {\n\t\tthis.mouseMode = mode;\n\t\tthis.bookElement?.classList.toggle(\"flipbook--select-text\", mode === \"select\");\n\t\tif (mode === \"select\") this.clearHoverShadow();\n\t}\n\n\t/** Keep mouse selection native by stopping the page's down event before Hammer sees it. */\n\tprivate handlePageMouseDown = (event: Event): void => {\n\t\t// A swipe may emit no click. A fresh press must not inherit suppression\n\t\t// intended only for the previous gesture's synthetic click.\n\t\tthis._suppressNextClick = false;\n\t\tif (event.type === \"mousedown\" || (event as PointerEvent).pointerType === \"mouse\") {\n\t\t\tthis.gestureStartedInNoFlipZone = this.isInsideNoFlipZone(event.target);\n\t\t\tif (this.gestureStartedInNoFlipZone) this.clearHoverShadow();\n\t\t}\n\t\tif (\n\t\t\tthis.mouseMode === \"select\" &&\n\t\t\tevent instanceof MouseEvent &&\n\t\t\tevent.button === 0 &&\n\t\t\t(event.type !== \"pointerdown\" || (event as PointerEvent).pointerType === \"mouse\")\n\t\t) {\n\t\t\tevent.stopPropagation();\n\t\t}\n\t};\n\n\t/** Keep native image/link dragging from taking over a page-turn gesture. */\n\tprivate handlePageDragStart = (event: Event): void => {\n\t\tif (this.mouseMode === \"turn\" && !this.isInsideNoFlipZone(event.target)) {\n\t\t\tevent.preventDefault();\n\t\t}\n\t};\n\n\t/** Download config (handlers and filename hints). Used by toolbar. */\n\tgetDownloadConfig(): DownloadConfig | undefined {\n\t\treturn this.downloadConfig;\n\t}\n\n\t/** Build the set of page indices that use cover sizing. */\n\tprivate buildCoverPageIndicesSet(): Set<number> {\n\t\tconst coverPageIndicesSet = new Set<number>();\n\t\tif (this.coverPageIndices) {\n\t\t\tconst indices =\n\t\t\t\tthis.coverPageIndices === \"auto\" ? [0, this.pagesCount - 1] : this.coverPageIndices;\n\t\t\tfor (const pageIdx of indices) {\n\t\t\t\tcoverPageIndicesSet.add(pageIdx);\n\t\t\t}\n\t\t}\n\t\treturn coverPageIndicesSet;\n\t}\n\n\t/**\n\t * Recalculate page sizes from the current container dimensions and\n\t * re-apply width/height/position styles. Called during initial render\n\t * and automatically on container resize (e.g. entering fullscreen).\n\t */\n\tprivate recalculateSizes(): void {\n\t\tif (!this.bookElement || !this.pageElements.length) return;\n\n\t\tconst maxCoverSize = new Size(this.bookElement.clientWidth / 2, this.bookElement.clientHeight);\n\t\tconst coverSize = maxCoverSize.aspectRatioFit(this.coverAspectRatio);\n\t\tconst leafSize = new Size(\n\t\t\t(coverSize.width * this.leafAspectRatio.width) / this.coverAspectRatio.width,\n\t\t\t(coverSize.height * this.leafAspectRatio.height) / this.coverAspectRatio.height,\n\t\t);\n\t\tthis.bookElement.style.perspective = `${Math.min(leafSize.width * 2, leafSize.height) * 2}px`;\n\n\t\tconst containerWidth = this.bookElement.clientWidth;\n\t\tconst containerHeight = this.bookElement.clientHeight;\n\n\t\tfor (const pageElement of this.pageElements) {\n\t\t\tconst isCoverPage = pageElement.dataset.isCoverPage === \"1\";\n\t\t\tconst pageSize = isCoverPage ? coverSize : leafSize;\n\n\t\t\tpageElement.style.width = `${pageSize.width}px`;\n\t\t\tpageElement.style.height = `${pageSize.height}px`;\n\t\t\tpageElement.style[this.isLTR ? \"left\" : \"right\"] =\n\t\t\t\t`${(containerWidth - 2 * pageSize.width) / 2}px`;\n\t\t\tpageElement.style.top = `${(containerHeight - pageSize.height) / 2}px`;\n\t\t}\n\t\tfor (const leaf of this.leaves) {\n\t\t\tleaf.applyTransform(leaf.flipPosition);\n\t\t}\n\t}\n\n\trender(selector: string, debug = false) {\n\t\tconst bookElement = document.querySelector(selector);\n\t\tif (!bookElement) {\n\t\t\tthrow new Error(`Couldn't find container with selector: ${selector}`);\n\t\t}\n\t\tthis.bookElement = bookElement as HTMLElement;\n\t\tthis.setMouseMode(this.mouseMode);\n\t\tif (!this.bookElement.classList.contains(\"flipbook\")) {\n\t\t\tthis.bookElement.classList.add(\"flipbook\");\n\t\t}\n\t\tif (!this.pageShadow) {\n\t\t\tthis.bookElement.classList.add(\"flipbook--no-page-shadow\");\n\t\t}\n\n\t\tconst pageElements = bookElement.querySelectorAll(\".page\");\n\t\tif (!pageElements.length) {\n\t\t\tthrow new Error(\"No pages found in flipbook\");\n\t\t}\n\t\tthis.pageElements = Array.from(pageElements) as HTMLElement[];\n\t\tfor (const page of this.pageElements) {\n\t\t\tpage.addEventListener(\"pointerdown\", this.handlePageMouseDown);\n\t\t\tpage.addEventListener(\"mousedown\", this.handlePageMouseDown);\n\t\t\tpage.addEventListener(\"dragstart\", this.handlePageDragStart, true);\n\t\t}\n\t\tthis.leaves.splice(0, this.leaves.length);\n\t\tconst leavesCount = Math.ceil(this.pagesCount / 2);\n\n\t\t// Determine which specific pages are cover exteriors (use coverSize).\n\t\t// Interior sides of cover leaves use leafSize — physically, the endpaper is\n\t\t// glued to the inside of the board and matches the text-block page size.\n\t\tconst coverPageIndicesSet = this.buildCoverPageIndicesSet();\n\n\t\t// Set data attributes before recalculateSizes() so it can read dataset.isCoverPage\n\t\t// to apply the correct cover vs leaf dimensions on the very first layout pass.\n\t\tthis.pageElements.forEach((pageElement, pageIndex) => {\n\t\t\tpageElement.dataset.pageIndex = pageIndex.toString();\n\t\t\tpageElement.dataset.pageSemanticName =\n\t\t\t\tthis.pageSemantics?.indexToSemanticName(pageIndex) ?? \"\";\n\t\t\tpageElement.dataset.pageTitle = this.pageSemantics?.indexToTitle(pageIndex) ?? \"\";\n\t\t\tpageElement.dataset.isCoverPage = coverPageIndicesSet.has(pageIndex) ? \"1\" : \"\";\n\t\t});\n\n\t\t// Compute and apply sizes (also called on resize).\n\t\t// Must run after dataset.isCoverPage is set so covers get coverSize.\n\t\tthis.recalculateSizes();\n\n\t\tthis.pageElements.forEach((pageElement, pageIndex) => {\n\t\t\tconst leafIndex = Math.floor(pageIndex / 2);\n\n\t\t\tconst isOddPage = (pageIndex + 1) % 2 === 1;\n\t\t\tconst isInitiallyTurned = this.initialTurnedLeaves.has(leafIndex);\n\n\t\t\t// Determine current page based on initial turned leaves\n\t\t\t// Current pages are the first two visible pages after all initially turned leaves\n\t\t\tconst firstVisibleLeafIndex =\n\t\t\t\tthis.initialTurnedLeaves.size > 0 ? Math.max(...this.initialTurnedLeaves) + 1 : 0;\n\t\t\tconst isCurrentPage =\n\t\t\t\tleafIndex === firstVisibleLeafIndex ||\n\t\t\t\t(leafIndex === firstVisibleLeafIndex - 1 && isInitiallyTurned);\n\n\t\t\tpageElement.classList.add(\n\t\t\t\tisOddPage ? \"odd\" : \"even\",\n\t\t\t\t...(isCurrentPage ? [\"current-page\"] : []),\n\t\t\t);\n\t\t\tif (isOddPage) {\n\t\t\t\tthis.leaves[leafIndex] = new Leaf(\n\t\t\t\t\tleafIndex,\n\t\t\t\t\t[pageElement, undefined],\n\t\t\t\t\tisInitiallyTurned,\n\t\t\t\t\t{\n\t\t\t\t\t\tisLTR: this.isLTR,\n\t\t\t\t\t\tleavesCount: leavesCount,\n\t\t\t\t\t\tpagesCount: this.pagesCount,\n\t\t\t\t\t\tpageShadow: this.pageShadow,\n\t\t\t\t\t\tsnapshotDuringFlip: this.snapshotDuringFlip,\n\t\t\t\t\t},\n\t\t\t\t\t(direction: FlipDirection) => {\n\t\t\t\t\t\tconst currentVisiblePageIndices: [number] | [number, number] =\n\t\t\t\t\t\t\tdirection === FlipDirection.Forward\n\t\t\t\t\t\t\t\t? pageIndex + 2 === this.pagesCount\n\t\t\t\t\t\t\t\t\t? [pageIndex + 1]\n\t\t\t\t\t\t\t\t\t: [pageIndex + 1, pageIndex + 2]\n\t\t\t\t\t\t\t\t: pageIndex === 0\n\t\t\t\t\t\t\t\t\t? [pageIndex]\n\t\t\t\t\t\t\t\t\t: [pageIndex - 1, pageIndex];\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tthis.prevVisiblePageIndices &&\n\t\t\t\t\t\t\tthis.prevVisiblePageIndices.length === currentVisiblePageIndices.length &&\n\t\t\t\t\t\t\tcurrentVisiblePageIndices.every((v, i) => v === this.prevVisiblePageIndices?.[i])\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst prevVisiblePageIndices = this.prevVisiblePageIndices;\n\t\t\t\t\t\tthis.prevVisiblePageIndices = currentVisiblePageIndices;\n\n\t\t\t\t\t\t// TODO expose to outside using https://github.com/open-draft/strict-event-emitter, and just be a consumer internally\n\t\t\t\t\t\tthis.onTurned(currentVisiblePageIndices, prevVisiblePageIndices);\n\t\t\t\t\t},\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\t// Even page (back side of leaf) — attach to the leaf created above.\n\t\t\t\tthis.leaves[leafIndex].pages[1] = pageElement;\n\n\t\t\t\t// Both pages of the leaf are now available.\n\t\t\t\t// Use applyTransform to set the correct initial transform — this ensures\n\t\t\t\t// the transforms exactly match what applyTransform produces during flips.\n\t\t\t\t// Previously, hardcoded transforms used rotateY(±180deg) for turned pages,\n\t\t\t\t// which made them invisible due to backface-visibility: hidden.\n\t\t\t\tconst initialPosition = isInitiallyTurned ? 1 : 0;\n\t\t\t\tthis.leaves[leafIndex].applyTransform(initialPosition);\n\t\t\t}\n\t\t});\n\n\t\t// Set initial visible page indices based on initially turned leaves.\n\t\t// Must match the post-flip model: the visible spread is the back of\n\t\t// the last turned leaf (odd page) and the front of the next un-turned\n\t\t// leaf (even page) → [lastTurnedLeaf*2+1, lastTurnedLeaf*2+2].\n\t\tconst lastTurnedLeafIndex =\n\t\t\tthis.initialTurnedLeaves.size > 0 ? Math.max(...this.initialTurnedLeaves) : -1;\n\n\t\tif (lastTurnedLeafIndex < 0) {\n\t\t\tthis.prevVisiblePageIndices = 1 < this.pagesCount ? [0, 1] : [0];\n\t\t} else {\n\t\t\tconst firstVisiblePageIndex = lastTurnedLeafIndex * 2 + 1;\n\t\t\tif (firstVisiblePageIndex >= this.pagesCount) {\n\t\t\t\t// All leaves are turned — show the last page(s)\n\t\t\t\tconst lastLeafIndex = Math.ceil(this.pagesCount / 2) - 1;\n\t\t\t\tconst lastLeafFirstPage = lastLeafIndex * 2;\n\t\t\t\tthis.prevVisiblePageIndices =\n\t\t\t\t\tlastLeafFirstPage + 1 < this.pagesCount\n\t\t\t\t\t\t? [lastLeafFirstPage, lastLeafFirstPage + 1]\n\t\t\t\t\t\t: [lastLeafFirstPage];\n\t\t\t} else {\n\t\t\t\tthis.prevVisiblePageIndices =\n\t\t\t\t\tfirstVisiblePageIndex + 1 < this.pagesCount\n\t\t\t\t\t\t? [firstVisiblePageIndex, firstVisiblePageIndex + 1]\n\t\t\t\t\t\t: [firstVisiblePageIndex];\n\t\t\t}\n\t\t}\n\n\t\t// Observe container size changes and re-layout automatically.\n\t\tif (typeof ResizeObserver !== \"undefined\") {\n\t\t\tthis.resizeObserver = new ResizeObserver(() => {\n\t\t\t\tthis.recalculateSizes();\n\t\t\t});\n\t\t\tthis.resizeObserver.observe(this.bookElement);\n\t\t}\n\n\t\tthis.hammer = new Hammer(this.bookElement, { touchAction: \"pan-y\" });\n\t\t// Mouse pulls can move in both axes while touch keeps native vertical scrolling.\n\t\tthis.hammer.get(\"pan\").set({ direction: Hammer.DIRECTION_ALL });\n\t\tthis.hammer.on(\"panstart\", this.onDragStart.bind(this));\n\t\tthis.hammer.on(\"panmove\", this.onDragUpdate.bind(this));\n\t\tthis.hammer.on(\"panend\", this.onDragEnd.bind(this));\n\t\tthis.hammer.on(\"pancancel\", (event) => this.onDragEnd(event, true));\n\t\tthis.bookElement.addEventListener(\"touchstart\", this.handleTouchStart, {\n\t\t\tpassive: false,\n\t\t});\n\t\tthis.bookElement.addEventListener(\"touchmove\", this.handleTouchMove, {\n\t\t\tpassive: false,\n\t\t});\n\t\tthis.bookElement.addEventListener(\n\t\t\t\"mousemove\",\n\t\t\tthis.handleMouseMove as unknown as EventListener,\n\t\t);\n\t\tthis.bookElement.addEventListener(\"mouseleave\", this.handleMouseLeave as EventListener);\n\t\t// Suppress click events that fire immediately after a drag gesture.\n\t\t// This prevents child interactive elements (buttons, links) from\n\t\t// activating when the user was actually performing a page flip.\n\t\tthis.bookElement.addEventListener(\n\t\t\t\"click\",\n\t\t\tthis.handleClickAfterDrag as EventListener,\n\t\t\ttrue, // capture phase — intercept before the click reaches any child\n\t\t);\n\t\t// Apply initial leaves buffer visibility\n\t\tthis.updateLeavesBufferVisibility();\n\t\tif (debug) this.fillDebugBar();\n\n\t\t// History: initial replaceState and popstate listener\n\t\tif (typeof window !== \"undefined\" && this.historyMapper) {\n\t\t\tthis.syncHistoryAndNotifyFlipped(true);\n\t\t\tthis._boundPopstate = this.handlePopstate.bind(this);\n\t\t\twindow.addEventListener(\"popstate\", this._boundPopstate, true);\n\t\t}\n\t}\n\n\t/**\n\t * Update visibility of leaves based on the buffer setting.\n\t * Leaves outside the buffer range are hidden for performance.\n\t */\n\tprivate updateLeavesBufferVisibility(): void {\n\t\tif (this.leavesBuffer === undefined) {\n\t\t\treturn; // No buffer - all leaves stay visible\n\t\t}\n\n\t\tconst leavesCount = this.leaves.length;\n\t\tif (leavesCount === 0) return;\n\n\t\t// Find the current leaf index (the first non-turned leaf, or last leaf if all turned)\n\t\tconst [leftLeaf, rightLeaf] = this.currentOrTurningLeaves;\n\t\tconst currentLeafIndex = rightLeaf?.index ?? leftLeaf?.index ?? 0;\n\n\t\t// Calculate buffer range\n\t\tconst bufferStart = Math.max(0, currentLeafIndex - this.leavesBuffer);\n\t\tconst bufferEnd = Math.min(leavesCount - 1, currentLeafIndex + this.leavesBuffer);\n\n\t\t// Cover leaves must always stay visible — they are cover-sized and sit behind\n\t\t// all leaf-sized pages, acting as the physical book boards. Hiding them would\n\t\t// remove the realistic \"frame\" effect.\n\t\tconst coverLeafIndices = new Set<number>();\n\t\tif (this.coverPageIndices) {\n\t\t\tconst indices =\n\t\t\t\tthis.coverPageIndices === \"auto\" ? [0, this.pagesCount - 1] : this.coverPageIndices;\n\t\t\tfor (const pageIdx of indices) {\n\t\t\t\tcoverLeafIndices.add(Math.floor(pageIdx / 2));\n\t\t\t}\n\t\t}\n\n\t\t// Update visibility of all leaves\n\t\tfor (let i = 0; i < leavesCount; i++) {\n\t\t\tconst leaf = this.leaves[i];\n\t\t\tconst isWithinBuffer = i >= bufferStart && i <= bufferEnd;\n\t\t\tconst isCoverLeaf = coverLeafIndices.has(i);\n\n\t\t\t// Cover leaves are always visible; other leaves follow the buffer range.\n\t\t\tconst visible = isCoverLeaf || isWithinBuffer;\n\n\t\t\t// Update both pages of the leaf\n\t\t\tfor (const page of leaf.pages) {\n\t\t\t\tif (page) {\n\t\t\t\t\tpage.style.display = visible ? \"\" : \"none\";\n\t\t\t\t\t// Use content-visibility to skip rendering of off-screen pages.\n\t\t\t\t\t// \"hidden\" is more aggressive than display:none — the browser\n\t\t\t\t\t// also skips layout/paint entirely, reducing memory pressure.\n\t\t\t\t\tpage.style.contentVisibility = visible ? \"visible\" : \"hidden\";\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate fillDebugBar() {\n\t\tconst debugBar = document.createElement(\"div\");\n\t\tdebugBar.className = \"flipbook-debug-bar flipbook-debug-bar--hidden\";\n\t\tthis.bookElement?.appendChild(debugBar);\n\t\tsetInterval(() => {\n\t\t\t// Populate debug bar with relevant information (throttled to reduce flicker)\n\t\t\tconst activeFlipsInfo = Array.from(this.activeFlips.entries())\n\t\t\t\t.map(([idx, state]) => `${idx}:${state.leaf.flipPosition.toFixed(2)}`)\n\t\t\t\t.join(\", \");\n\t\t\tdebugBar.innerHTML = `\n <div>Direction: ${this.isLTR ? \"LTR\" : \"RTL\"}</div>\n <div>Active Flips: ${this.activeFlips.size} [${activeFlipsInfo}]</div>\n <div>Pending Flip dir: ${this.pendingFlipDirection}</div>\n `;\n\t\t}, 200);\n\t}\n\n\t/**\n\t * Get the current manually-controlled flip state (the one being dragged).\n\t * Returns undefined if no manual flip is in progress.\n\t */\n\tprivate get currentManualFlip(): FlipState | undefined {\n\t\tfor (const state of this.activeFlips.values()) {\n\t\t\tif (!state.isDuringAutoFlip) {\n\t\t\t\treturn state;\n\t\t\t}\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Find the next available leaf for flipping in the given direction.\n\t * Excludes leaves that are already being flipped.\n\t */\n\tprivate getNextAvailableLeaf(direction: FlipDirection): Leaf | undefined {\n\t\tif (direction === FlipDirection.Forward) {\n\t\t\t// Find the rightmost leaf that's not already flipping\n\t\t\t// Start from the current right leaf and go forward\n\t\t\tconst [, rightLeaf] = this.currentOrTurningLeaves;\n\t\t\tif (!rightLeaf) return undefined;\n\n\t\t\t// Find a leaf that's not in activeFlips\n\t\t\tfor (let i = rightLeaf.index; i < this.leaves.length; i++) {\n\t\t\t\tconst leaf = this.leaves[i];\n\t\t\t\tif (!this.activeFlips.has(leaf.index) && !leaf.isTurned) {\n\t\t\t\t\treturn leaf;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn undefined;\n\t\t} else if (direction === FlipDirection.Backward) {\n\t\t\t// Find the leftmost leaf that's not already flipping\n\t\t\tconst [leftLeaf] = this.currentOrTurningLeaves;\n\t\t\tif (!leftLeaf) return undefined;\n\n\t\t\t// Find a leaf that's not in activeFlips\n\t\t\tfor (let i = leftLeaf.index; i >= 0; i--) {\n\t\t\t\tconst leaf = this.leaves[i];\n\t\t\t\tif (!this.activeFlips.has(leaf.index) && (leaf.isTurned || leaf.isTurning)) {\n\t\t\t\t\treturn leaf;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn undefined;\n\t\t}\n\t\treturn undefined;\n\t}\n\n\tprivate onDragStart(event: HammerInput) {\n\t\tconst isTouch = event.pointerType === \"touch\" || event.srcEvent?.type.startsWith(\"touch\");\n\t\tif (isTouch && this.touchDirectionLock === \"none\") {\n\t\t\tconst dx = Number.isFinite(event.deltaX) ? Math.abs(event.deltaX) : 0;\n\t\t\tconst dy = Number.isFinite(event.deltaY) ? Math.abs(event.deltaY) : 0;\n\t\t\tif (Math.max(dx, dy) > FlipBook.DIRECTION_LOCK_THRESHOLD) {\n\t\t\t\tthis.touchDirectionLock = dx >= dy ? \"horizontal\" : \"vertical\";\n\t\t\t}\n\t\t}\n\t\t// Reject pan events that belong to a touch gesture locked to vertical\n\t\t// (i.e. the user is scrolling, not flipping).\n\t\tif (this.touchDirectionLock === \"vertical\" && event.pointerType !== \"mouse\") {\n\t\t\treturn;\n\t\t}\n\t\t// Do not start flip when gesture begins inside a no-flip zone (e.g. horizontal carousel)\n\t\tif (\n\t\t\tthis.gestureStartedInNoFlipZone ||\n\t\t\tthis.isInsideNoFlipZone(event.srcEvent?.target ?? null)\n\t\t) {\n\t\t\tthis.gestureStartedInNoFlipZone = true;\n\t\t\treturn;\n\t\t}\n\t\t// Allow starting a new flip even if others are in auto-flip mode\n\t\t// Only block if we already have a manual flip in progress\n\t\tif (this.currentManualFlip) {\n\t\t\tthis.pendingFlipDirection = FlipDirection.None;\n\t\t\tthis.pendingFlipStartingPos = 0;\n\t\t\tthis.pendingFlipStartingY = undefined;\n\t\t\treturn;\n\t\t}\n\t\tthis.isDragging = true;\n\t\tthis.clearHoverShadow();\n\t\tthis.pendingFlipStartingPos =\n\t\t\tevent.center.x - (Number.isFinite(event.deltaX) ? event.deltaX : 0);\n\t\tthis.pendingFlipStartingY = Number.isFinite(event.center.y)\n\t\t\t? event.center.y - (Number.isFinite(event.deltaY) ? event.deltaY : 0)\n\t\t\t: undefined;\n\t\tthis.pendingFlipDirection = FlipDirection.None;\n\t\tif (\n\t\t\t(Number.isFinite(event.deltaX) && event.deltaX !== 0) ||\n\t\t\t(Number.isFinite(event.deltaY) && event.deltaY !== 0)\n\t\t) {\n\t\t\tthis.onDragUpdate(event);\n\t\t}\n\t}\n\n\tprivate onDragUpdate(event: HammerInput) {\n\t\tif (this.gestureStartedInNoFlipZone) return;\n\t\t// Reject pan events that belong to a touch gesture locked to vertical.\n\t\tif (this.touchDirectionLock === \"vertical\" && event.pointerType !== \"mouse\") {\n\t\t\treturn;\n\t\t}\n\n\t\t// Get or create the current manual flip state\n\t\tlet flipState = this.currentManualFlip;\n\t\tif (!flipState && this.isInsideNoFlipZone(event.srcEvent?.target ?? null)) return;\n\n\t\tconst currentPos = event.center.x;\n\t\tconst bookWidth = this.bookElement?.clientWidth ?? 0;\n\t\tif (bookWidth <= 0) return;\n\n\t\t// Calculate delta\n\t\tconst startingPos = flipState?.startingPos ?? this.pendingFlipStartingPos;\n\t\tconst delta = this.isLTR ? startingPos - currentPos : currentPos - startingPos;\n\t\tconst verticalMove =\n\t\t\tNumber.isFinite(event.center.y) && this.pendingFlipStartingY !== undefined\n\t\t\t\t? event.center.y - this.pendingFlipStartingY\n\t\t\t\t: 0;\n\t\tif (\n\t\t\t!flipState &&\n\t\t\tdelta === 0 &&\n\t\t\t(!this.isDragging || verticalMove === 0 || event.pointerType === \"touch\")\n\t\t)\n\t\t\treturn;\n\n\t\t// Determine direction on first meaningful movement\n\t\tconst isRightSide =\n\t\t\tstartingPos >= (this.bookElement?.getBoundingClientRect().left ?? 0) + bookWidth / 2;\n\t\tconst direction =\n\t\t\tflipState?.direction ??\n\t\t\t(this.pendingFlipDirection !== FlipDirection.None\n\t\t\t\t? this.pendingFlipDirection\n\t\t\t\t: delta === 0\n\t\t\t\t\t? this.isLTR === isRightSide\n\t\t\t\t\t\t? FlipDirection.Forward\n\t\t\t\t\t\t: FlipDirection.Backward\n\t\t\t\t\t: delta > 0\n\t\t\t\t\t\t? FlipDirection.Forward\n\t\t\t\t\t\t: FlipDirection.Backward);\n\n\t\t// Lock in direction for this flip\n\t\tif (this.pendingFlipDirection === FlipDirection.None) {\n\t\t\tthis.pendingFlipDirection = direction;\n\t\t}\n\n\t\t// Block starting a new flip in opposite direction while auto-flip is in progress\n\t\t// This prevents visual glitches when swiping backward while a forward flip is animating\n\t\tif (!flipState) {\n\t\t\tfor (const state of this.activeFlips.values()) {\n\t\t\t\tif (state.isDuringAutoFlip && state.direction !== direction) {\n\t\t\t\t\t// There's an auto-flip in the opposite direction - block this new flip\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// If we don't have a flip state yet, create one\n\t\tif (!flipState) {\n\t\t\tconst leaf = this.getNextAvailableLeaf(direction);\n\t\t\tif (!leaf) return;\n\n\t\t\t// Acquire the flip lock for the full drag cycle; the matching\n\t\t\t// endFlip() is deferred until the release animation completes\n\t\t\t// in onDragEnd, so the snapshot/scrollbar lockdown stays stable.\n\t\t\tleaf.beginFlip();\n\t\t\tconst originY = this.getFoldPointerY(\n\t\t\t\tleaf,\n\t\t\t\tdirection,\n\t\t\t\tthis.pendingFlipStartingY ?? event.center.y,\n\t\t\t);\n\t\t\tleaf.setFoldOrigin(originY);\n\n\t\t\tflipState = {\n\t\t\t\tleaf,\n\t\t\t\tdirection,\n\t\t\t\tstartingPos,\n\t\t\t\tstartingY:\n\t\t\t\t\tthis.pendingFlipStartingY ??\n\t\t\t\t\t(Number.isFinite(event.center.y) ? event.center.y : undefined),\n\t\t\t\tcornerY: originY < 0.5 ? 0 : 1,\n\t\t\t\tdelta: 0,\n\t\t\t\tisDuringAutoFlip: false,\n\t\t\t};\n\t\t\tthis.activeFlips.set(leaf.index, flipState);\n\t\t}\n\n\t\t// Update delta\n\t\tflipState.delta = delta;\n\t\tconst page =\n\t\t\tflipState.direction === FlipDirection.Backward\n\t\t\t\t? (flipState.leaf.pages[1] ?? flipState.leaf.pages[0])\n\t\t\t\t: flipState.leaf.pages[0];\n\t\tconst pageWidth = Number.parseFloat(page.style.width);\n\t\tconst pageHeight = Number.parseFloat(page.style.height);\n\t\tconst hasDimensions =\n\t\t\tNumber.isFinite(pageWidth) && pageWidth > 0 && Number.isFinite(pageHeight) && pageHeight > 0;\n\t\tconst rigid = flipState.leaf.pages.some((side) => side?.dataset.isCoverPage === \"1\");\n\t\tif (hasDimensions && !rigid && flipState.leaf.pages[1]) {\n\t\t\tconst dy =\n\t\t\t\tNumber.isFinite(event.center.y) && flipState.startingY !== undefined\n\t\t\t\t\t? event.center.y - flipState.startingY\n\t\t\t\t\t: 0;\n\t\t\tflipState.leaf.setManualFold({\n\t\t\t\tx: (flipState.direction === FlipDirection.Forward ? 1 : -1) - delta / pageWidth,\n\t\t\t\ty: (flipState.cornerY ?? 1) + dy / pageHeight,\n\t\t\t});\n\t\t} else {\n\t\t\tconst travel = Number.isFinite(pageWidth) && pageWidth > 0 ? pageWidth * 2 : bookWidth;\n\t\t\tconst position =\n\t\t\t\tflipState.direction === FlipDirection.Forward ? delta / travel : 1 + delta / travel;\n\t\t\tflipState.leaf.setManualPosition(Math.max(0, Math.min(1, position)));\n\t\t}\n\t}\n\n\tprivate getFoldPointerY(leaf: Leaf, direction: FlipDirection, pointerY: number): number {\n\t\tconst page =\n\t\t\tdirection === FlipDirection.Backward ? (leaf.pages[1] ?? leaf.pages[0]) : leaf.pages[0];\n\t\tconst height = Number.parseFloat(page.style.height);\n\t\tconst top = Number.parseFloat(page.style.top);\n\t\tif (!Number.isFinite(pointerY) || !Number.isFinite(height) || height <= 0) return 0.75;\n\t\tconst bookTop = this.bookElement?.getBoundingClientRect().top ?? 0;\n\t\treturn Math.max(\n\t\t\t0,\n\t\t\tMath.min(1, (pointerY - bookTop - (Number.isFinite(top) ? top : 0)) / height),\n\t\t);\n\t}\n\n\tprivate onDragEnd(event: HammerInput, cancelled = false) {\n\t\tthis.gestureStartedInNoFlipZone = false;\n\t\t// Suppress the click that the browser fires after pointerup following a drag.\n\t\tthis._suppressNextClick = true;\n\n\t\tconst flipState = this.currentManualFlip;\n\t\tif (!flipState) {\n\t\t\tthis.pendingFlipDirection = FlipDirection.None;\n\t\t\tthis.pendingFlipStartingPos = 0;\n\t\t\tthis.pendingFlipStartingY = undefined;\n\t\t\tthis.isDragging = false;\n\t\t\tthis.clearHoverShadow();\n\t\t\treturn;\n\t\t}\n\n\t\tconst ppsX = event.velocityX * 1000; // pixels per second\n\t\tlet flipTo: FlipPosition;\n\n\t\tswitch (flipState.direction) {\n\t\t\tcase FlipDirection.Forward:\n\t\t\t\tif (\n\t\t\t\t\t!cancelled &&\n\t\t\t\t\t((this.isLTR ? ppsX < -this.fastDeltaThreshold : ppsX > this.fastDeltaThreshold) ||\n\t\t\t\t\t\tflipState.leaf.flipPosition >= 0.5)\n\t\t\t\t) {\n\t\t\t\t\tflipTo = 1;\n\t\t\t\t} else {\n\t\t\t\t\tflipTo = 0;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase FlipDirection.Backward:\n\t\t\t\tif (\n\t\t\t\t\t!cancelled &&\n\t\t\t\t\t((this.isLTR ? ppsX > this.fastDeltaThreshold : ppsX < -this.fastDeltaThreshold) ||\n\t\t\t\t\t\tflipState.leaf.flipPosition <= 0.5)\n\t\t\t\t) {\n\t\t\t\t\tflipTo = 0;\n\t\t\t\t} else {\n\t\t\t\t\tflipTo = 1;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\t// Safety: release the drag-level flip lock if direction is unexpected.\n\t\t\t\tflipState.leaf.endFlip();\n\t\t\t\tthis.activeFlips.delete(flipState.leaf.index);\n\t\t\t\tthis.pendingFlipDirection = FlipDirection.None;\n\t\t\t\tthis.pendingFlipStartingPos = 0;\n\t\t\t\tthis.pendingFlipStartingY = undefined;\n\t\t\t\tthis.isDragging = false;\n\t\t\t\treturn;\n\t\t}\n\n\t\t// Mark as auto-flip and reset pending state\n\t\tflipState.isDuringAutoFlip = true;\n\t\tthis.pendingFlipDirection = FlipDirection.None;\n\t\tthis.pendingFlipStartingPos = 0;\n\t\tthis.pendingFlipStartingY = undefined;\n\t\tthis.isDragging = false;\n\n\t\tconst targetPageIndex =\n\t\t\tflipTo === 1 ? 2 * (flipState.leaf.index + 1) : 2 * flipState.leaf.index;\n\t\tconst direction: PageFlipDirection = flipTo === 1 ? \"forward\" : \"backward\";\n\t\tthis.lastFlipParams = this.buildPageFlipParams(targetPageIndex, direction);\n\t\tif (this.onPageFlipping) this.onPageFlipping(this.lastFlipParams);\n\n\t\t// Complete the flip asynchronously - don't block new flips!\n\t\tflipState.leaf.flipToPosition(flipTo).then(() => {\n\t\t\t// Release the drag-level flip lock acquired in onDragUpdate.\n\t\t\t// The animation's own beginFlip/endFlip is ref-counted inside, so\n\t\t\t// the snapshot stays active until this final endFlip() zeroes it.\n\t\t\tflipState.leaf.endFlip();\n\t\t\tthis.activeFlips.delete(flipState.leaf.index);\n\t\t});\n\t}\n\n\tprivate handleTouchStart = (e: TouchEvent) => {\n\t\tthis._suppressNextClick = false;\n\t\tif (e.touches.length > 1) {\n\t\t\treturn;\n\t\t}\n\t\tconst touch = e.touches[0];\n\t\tthis.touchStartingPos = { x: touch.pageX, y: touch.pageY };\n\t\tthis.touchStartedInNoFlipZone = this.isInsideNoFlipZone(e.target);\n\t\tthis.gestureStartedInNoFlipZone = this.touchStartedInNoFlipZone;\n\t\tthis.touchDirectionLock = \"none\";\n\t};\n\n\tprivate handleTouchMove = (e: TouchEvent) => {\n\t\tif (e.touches.length > 1) {\n\t\t\treturn;\n\t\t}\n\t\t// Allow horizontal scroll (e.g. carousel) when touch started inside a no-flip zone\n\t\tif (this.touchStartedInNoFlipZone) {\n\t\t\treturn;\n\t\t}\n\t\tconst touch = e.touches[0];\n\t\tconst deltaX = touch.pageX - this.touchStartingPos.x;\n\t\tconst deltaY = touch.pageY - this.touchStartingPos.y;\n\n\t\t// Lock direction on the first significant movement. Once locked the\n\t\t// decision is final for the rest of this gesture — prevents the old\n\t\t// bug where accumulated vertical delta kept overruling a late\n\t\t// horizontal change-of-direction within the same gesture.\n\t\tif (this.touchDirectionLock === \"none\") {\n\t\t\tconst absDx = Math.abs(deltaX);\n\t\t\tconst absDy = Math.abs(deltaY);\n\t\t\tif (absDx > FlipBook.DIRECTION_LOCK_THRESHOLD || absDy > FlipBook.DIRECTION_LOCK_THRESHOLD) {\n\t\t\t\tthis.touchDirectionLock = absDx >= absDy ? \"horizontal\" : \"vertical\";\n\t\t\t}\n\t\t}\n\n\t\tif (this.touchDirectionLock === \"horizontal\") {\n\t\t\te.preventDefault();\n\t\t}\n\t\t// 'vertical' or 'none' → let the browser handle native scroll\n\t};\n\n\tprivate handleMouseMove = throttle(MOUSE_MOVE_THROTTLE_MS, (event: MouseEvent) => {\n\t\tif (!this.bookElement) return;\n\t\tif (\n\t\t\tthis.mouseMode === \"select\" ||\n\t\t\tevent.buttons !== 0 ||\n\t\t\tthis.isDragging ||\n\t\t\tthis.currentManualFlip ||\n\t\t\tthis.activeFlips.size > 0\n\t\t) {\n\t\t\tthis.clearHoverShadow();\n\t\t\treturn;\n\t\t}\n\n\t\tconst rect = this.bookElement.getBoundingClientRect();\n\t\tif (rect.width <= 0 || rect.height <= 0) {\n\t\t\tthis.clearHoverShadow();\n\t\t\treturn;\n\t\t}\n\n\t\tconst x = ((event.clientX - rect.left) * this.bookElement.clientWidth) / rect.width;\n\t\tconst y = ((event.clientY - rect.top) * this.bookElement.clientHeight) / rect.height;\n\t\tconst spineX = this.bookElement.clientWidth / 2;\n\t\tconst isRightSide = x >= spineX;\n\t\tconst isForward = this.isLTR === isRightSide;\n\t\tconst direction = isForward ? FlipDirection.Forward : FlipDirection.Backward;\n\t\tconst leaf = this.getNextAvailableLeaf(direction);\n\t\tif (!leaf) {\n\t\t\tthis.clearHoverShadow();\n\t\t\treturn;\n\t\t}\n\n\t\t// Use resting page dimensions: the preview itself transforms the page bounds.\n\t\tconst page = isForward ? leaf.pages[0] : (leaf.pages[1] ?? leaf.pages[0]);\n\t\tconst width = Number.parseFloat(page.style.width);\n\t\tconst height = Number.parseFloat(page.style.height);\n\t\tconst top = Number.parseFloat(page.style.top);\n\t\tconst distanceFromEdge = isRightSide ? spineX + width - x : x - (spineX - width);\n\t\tconst edgeZone = width * EDGE_ZONE_RATIO;\n\t\tif (\n\t\t\t!Number.isFinite(distanceFromEdge) ||\n\t\t\t!Number.isFinite(y) ||\n\t\t\t!Number.isFinite(height) ||\n\t\t\t!Number.isFinite(top) ||\n\t\t\twidth <= 0 ||\n\t\t\theight <= 0 ||\n\t\t\tdistanceFromEdge < 0 ||\n\t\t\tdistanceFromEdge > edgeZone ||\n\t\t\ty < top ||\n\t\t\ty > top + height\n\t\t) {\n\t\t\tthis.clearHoverShadow();\n\t\t\treturn;\n\t\t}\n\t\tconst edgeProgress = 1 - Math.min(1, distanceFromEdge / edgeZone);\n\t\tconst hoverStrength = edgeProgress * HOVER_STRENGTH_MAX;\n\n\t\tif (this.hoveredLeaf && this.hoveredLeaf !== leaf) {\n\t\t\tthis.hoveredLeaf.setHoverShadow(0);\n\t\t}\n\t\tif (this.hoveredLeaf !== leaf) {\n\t\t\tleaf.setFoldOrigin((y - top) / height);\n\t\t}\n\t\tthis.hoveredLeaf = leaf;\n\t\tleaf.setHoverShadow(hoverStrength);\n\t});\n\n\tprivate handleMouseLeave = () => {\n\t\tthis.handleMouseMove.cancel({ upcomingOnly: true });\n\t\tthis.clearHoverShadow();\n\t};\n\n\t/**\n\t * Capture-phase click handler that suppresses clicks fired by the browser\n\t * immediately after a drag gesture ends. Without this, releasing the pointer\n\t * on an interactive element (button, link) inside a page would trigger both\n\t * the flip animation AND the element's click handler.\n\t */\n\tprivate handleClickAfterDrag = (e: Event) => {\n\t\tif (this._suppressNextClick) {\n\t\t\tthis._suppressNextClick = false;\n\t\t\te.stopPropagation();\n\t\t\te.preventDefault();\n\t\t}\n\t};\n\n\tprivate clearHoverShadow() {\n\t\tif (this.hoveredLeaf) {\n\t\t\tthis.hoveredLeaf.setHoverShadow(0);\n\t\t\tthis.hoveredLeaf = undefined;\n\t\t}\n\t}\n\tprivate onTurned(\n\t\tcurrentVisiblePageIndices: [number] | [number, number],\n\t\tprevVisibilePageIndices?: [number] | [number, number],\n\t) {\n\t\tfor (let i = 0; i < this.pageElements.length; i++) {\n\t\t\tconst pageElement = this.pageElements[i];\n\t\t\tconst inCurrent = currentVisiblePageIndices.includes(i);\n\t\t\tconst inPrev = prevVisibilePageIndices?.includes(i);\n\t\t\tconst action = inCurrent\n\t\t\t\t? pageElement.classList.add\n\t\t\t\t: !prevVisibilePageIndices || !inPrev\n\t\t\t\t\t? () => null\n\t\t\t\t\t: pageElement.classList.remove;\n\t\t\taction.bind(pageElement.classList)(\"current-page\");\n\t\t}\n\t\t// Update leaves buffer visibility after turn completes\n\t\tthis.updateLeavesBufferVisibility();\n\t\t// Notify React/consumer and sync history\n\t\tthis.syncHistoryAndNotifyFlipped(false);\n\t\t// TODO expose to outside using https://github.com/open-draft/strict-event-emitter, and just be a consumer internally.\n\t\t// TODO: set prev-page / next-page classes for prev/next pages as accordingally\n\t}\n\t/**\n\t * Get the index of the current (leftmost visible) page.\n\t * Returns 0 if no pages are visible.\n\t */\n\tget currentPageIndex(): number {\n\t\treturn this.prevVisiblePageIndices?.[0] ?? 0;\n\t}\n\n\t/**\n\t * Get the total number of pages in the flipbook.\n\t */\n\tget totalPages(): number {\n\t\treturn this.pagesCount;\n\t}\n\n\t/**\n\t * Check if the book is currently showing the first page(s).\n\t */\n\tget isFirstPage(): boolean {\n\t\treturn this.currentPageIndex === 0;\n\t}\n\n\t/**\n\t * Check if the book is currently showing the last page(s).\n\t */\n\tget isLastPage(): boolean {\n\t\tconst lastPageIndex = this.pagesCount - 1;\n\t\treturn this.prevVisiblePageIndices?.includes(lastPageIndex) ?? false;\n\t}\n\n\t/**\n\t * Check if there's an active auto-flip in the given direction.\n\t */\n\tprivate hasActiveAutoFlipInDirection(direction: FlipDirection): boolean {\n\t\tfor (const state of this.activeFlips.values()) {\n\t\t\tif (state.isDuringAutoFlip && state.direction === direction) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n\n\t/**\n\t * Animate flip to the next page.\n\t * @returns Promise that resolves when the flip animation completes\n\t */\n\tasync flipNext(): Promise<void> {\n\t\t// Block if there's an active auto-flip in the opposite direction\n\t\tif (this.hasActiveAutoFlipInDirection(FlipDirection.Backward)) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst leaf = this.getNextAvailableLeaf(FlipDirection.Forward);\n\t\tif (!leaf) return;\n\t\tthis.clearHoverShadow();\n\n\t\tconst flipState: FlipState = {\n\t\t\tleaf,\n\t\t\tdirection: FlipDirection.Forward,\n\t\t\tstartingPos: 0,\n\t\t\tdelta: 0,\n\t\t\tisDuringAutoFlip: true,\n\t\t};\n\t\tthis.activeFlips.set(leaf.index, flipState);\n\t\tconst targetPageIndex = this.currentPageIndex + 2;\n\t\tthis.lastFlipParams = this.buildPageFlipParams(targetPageIndex, \"forward\");\n\t\tif (this.onPageFlipping) this.onPageFlipping(this.lastFlipParams);\n\n\t\ttry {\n\t\t\tawait leaf.flipToPosition(1);\n\t\t} finally {\n\t\t\tthis.activeFlips.delete(leaf.index);\n\t\t}\n\t}\n\n\t/**\n\t * Animate flip to the previous page.\n\t * @returns Promise that resolves when the flip animation completes\n\t */\n\tasync flipPrev(): Promise<void> {\n\t\t// Block if there's an active auto-flip in the opposite direction\n\t\tif (this.hasActiveAutoFlipInDirection(FlipDirection.Forward)) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst leaf = this.getNextAvailableLeaf(FlipDirection.Backward);\n\t\tif (!leaf) return;\n\t\tthis.clearHoverShadow();\n\n\t\tconst flipState: FlipState = {\n\t\t\tleaf,\n\t\t\tdirection: FlipDirection.Backward,\n\t\t\tstartingPos: 0,\n\t\t\tdelta: 0,\n\t\t\tisDuringAutoFlip: true,\n\t\t};\n\t\tthis.activeFlips.set(leaf.index, flipState);\n\t\tconst targetPageIndex = this.currentPageIndex - 2;\n\t\tthis.lastFlipParams = this.buildPageFlipParams(Math.max(0, targetPageIndex), \"backward\");\n\t\tif (this.onPageFlipping) this.onPageFlipping(this.lastFlipParams);\n\n\t\ttry {\n\t\t\tawait leaf.flipToPosition(0);\n\t\t} finally {\n\t\t\tthis.activeFlips.delete(leaf.index);\n\t\t}\n\t}\n\n\t/**\n\t * Animate to a specific page index.\n\t * Flips through pages sequentially to reach the target.\n\t * @param pageIndex - The target page index (0-based)\n\t * @returns Promise that resolves when all flip animations complete\n\t */\n\tasync flipToPage(pageIndex: number): Promise<void> {\n\t\tif (pageIndex < 0 || pageIndex >= this.pagesCount) {\n\t\t\tconsole.warn(\n\t\t\t\t`flipToPage: Invalid page index ${pageIndex}. Must be between 0 and ${this.pagesCount - 1}.`,\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\t// Compute the last-turned-leaf index that makes pageIndex visible,\n\t\t// using the same formula as jumpToPage.\n\t\tconst targetLastTurned = pageIndex <= 0 ? -1 : Math.floor((pageIndex - 1) / 2);\n\t\tconst currentLastTurned =\n\t\t\tthis.currentPageIndex <= 0 ? -1 : Math.floor((this.currentPageIndex - 1) / 2);\n\n\t\tif (targetLastTurned === currentLastTurned) {\n\t\t\treturn; // Already at the target spread\n\t\t}\n\n\t\tif (targetLastTurned > currentLastTurned) {\n\t\t\tconst steps = targetLastTurned - currentLastTurned;\n\t\t\tfor (let i = 0; i < steps; i++) {\n\t\t\t\tawait this.flipNext();\n\t\t\t}\n\t\t} else {\n\t\t\tconst steps = currentLastTurned - targetLastTurned;\n\t\t\tfor (let i = 0; i < steps; i++) {\n\t\t\t\tawait this.flipPrev();\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Jump to a specific page instantly without animation.\n\t * @param pageIndex - The target page index (0-based)\n\t */\n\tjumpToPage(pageIndex: number): void {\n\t\tif (pageIndex < 0 || pageIndex >= this.pagesCount) {\n\t\t\tconsole.warn(\n\t\t\t\t`jumpToPage: Invalid page index ${pageIndex}. Must be between 0 and ${this.pagesCount - 1}.`,\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\tconst isLastPage = pageIndex === this.pagesCount - 1;\n\t\tconst isOddPage = pageIndex % 2 === 1;\n\t\t// If targeting the last page and it's odd (back of leaf), close the book reversed\n\t\tconst closeBookReversed = isLastPage && isOddPage;\n\n\t\t// Determine which leaf should be the last one turned.\n\t\t// A leaf's pages are visible as follows:\n\t\t// - At position 0 (not turned): its even page (front) is visible\n\t\t// - At position 1 (turned): its odd page (back) is visible\n\t\t// The visible spread after turning leaf k is [2k+1, 2k+2]:\n\t\t// back of leaf k (odd) + front of leaf k+1 (even).\n\t\t// For page 0, no leaves need to be turned (initial spread [0, 1]).\n\t\tlet lastTurnedLeafIndex: number;\n\t\tif (pageIndex <= 0) {\n\t\t\tlastTurnedLeafIndex = -1; // No leaves turned\n\t\t} else if (closeBookReversed) {\n\t\t\tlastTurnedLeafIndex = this.leaves.length - 1; // All leaves turned\n\t\t} else {\n\t\t\tlastTurnedLeafIndex = Math.floor((pageIndex - 1) / 2);\n\t\t}\n\n\t\t// Set all leaves up to lastTurnedLeafIndex as turned, all after as not turned\n\t\tfor (let i = 0; i < this.leaves.length; i++) {\n\t\t\tconst leaf = this.leaves[i];\n\t\t\tconst shouldBeTurned = i <= lastTurnedLeafIndex;\n\n\t\t\tif (shouldBeTurned && !leaf.isTurned) {\n\t\t\t\t// Turn this leaf instantly\n\t\t\t\tleaf.flipPosition = 1;\n\t\t\t\tthis.applyLeafTransform(leaf, 1);\n\t\t\t} else if (!shouldBeTurned && leaf.isTurned) {\n\t\t\t\t// Unturn this leaf instantly\n\t\t\t\tleaf.flipPosition = 0;\n\t\t\t\tthis.applyLeafTransform(leaf, 0);\n\t\t\t}\n\t\t}\n\n\t\t// Update visible page indices\n\t\tif (closeBookReversed) {\n\t\t\t// Closed reversed: only show the last page\n\t\t\tthis.prevVisiblePageIndices = [pageIndex];\n\t\t} else if (lastTurnedLeafIndex < 0) {\n\t\t\t// No leaves turned: initial spread\n\t\t\tthis.prevVisiblePageIndices = 1 < this.pagesCount ? [0, 1] : [0];\n\t\t} else {\n\t\t\tconst firstVisiblePageIndex = lastTurnedLeafIndex * 2 + 1;\n\t\t\tthis.prevVisiblePageIndices =\n\t\t\t\tfirstVisiblePageIndex + 1 < this.pagesCount\n\t\t\t\t\t? [firstVisiblePageIndex, firstVisiblePageIndex + 1]\n\t\t\t\t\t: [firstVisiblePageIndex];\n\t\t}\n\n\t\t// Update current-page classes\n\t\tfor (let i = 0; i < this.pageElements.length; i++) {\n\t\t\tconst pageElement = this.pageElements[i];\n\t\t\tif (this.prevVisiblePageIndices.includes(i)) {\n\t\t\t\tpageElement.classList.add(\"current-page\");\n\t\t\t} else {\n\t\t\t\tpageElement.classList.remove(\"current-page\");\n\t\t\t}\n\t\t}\n\n\t\t// Update leaves buffer visibility\n\t\tthis.updateLeavesBufferVisibility();\n\n\t\t// Notify callback and sync history\n\t\tthis.syncHistoryAndNotifyFlipped(false);\n\t}\n\n\t/**\n\t * Show a page selected by an external router or browser history entry without\n\t * adding another history entry. Page-change callbacks still run.\n\t */\n\trestorePage(pageIndex: number): void {\n\t\tconst wasRestoring = this._isRestoringFromHistory;\n\t\tthis._isRestoringFromHistory = true;\n\t\ttry {\n\t\t\tthis.jumpToPage(pageIndex);\n\t\t} finally {\n\t\t\tthis._isRestoringFromHistory = wasRestoring;\n\t\t}\n\t}\n\n\t/**\n\t * Apply transform styles to a leaf's pages for a given flip position.\n\t * Used for instant positioning (jumpToPage).\n\t */\n\tprivate applyLeafTransform(leaf: Leaf, position: number): void {\n\t\tleaf.applyTransform(position);\n\t}\n\n\t/**\n\t * Clean up event listeners and Hammer instance.\n\t * Should be called when the FlipBook is no longer needed.\n\t */\n\tdestroy() {\n\t\tif (typeof window !== \"undefined\" && this._boundPopstate) {\n\t\t\twindow.removeEventListener(\"popstate\", this._boundPopstate, true);\n\t\t\tthis._boundPopstate = undefined;\n\t\t}\n\t\tif (this.resizeObserver) {\n\t\t\tthis.resizeObserver.disconnect();\n\t\t\tthis.resizeObserver = undefined;\n\t\t}\n\t\tif (this.hammer) {\n\t\t\tthis.hammer.destroy();\n\t\t\tthis.hammer = undefined;\n\t\t}\n\t\tif (this.bookElement) {\n\t\t\tfor (const page of this.pageElements) {\n\t\t\t\tpage.removeEventListener(\"pointerdown\", this.handlePageMouseDown);\n\t\t\t\tpage.removeEventListener(\"mousedown\", this.handlePageMouseDown);\n\t\t\t\tpage.removeEventListener(\"dragstart\", this.handlePageDragStart, true);\n\t\t\t}\n\t\t\tthis.bookElement.removeEventListener(\"touchstart\", this.handleTouchStart as EventListener);\n\t\t\tthis.bookElement.removeEventListener(\"touchmove\", this.handleTouchMove as EventListener);\n\t\t\tthis.bookElement.removeEventListener(\n\t\t\t\t\"mousemove\",\n\t\t\t\tthis.handleMouseMove as unknown as EventListener,\n\t\t\t);\n\t\t\tthis.bookElement.removeEventListener(\"mouseleave\", this.handleMouseLeave as EventListener);\n\t\t\tthis.bookElement.removeEventListener(\n\t\t\t\t\"click\",\n\t\t\t\tthis.handleClickAfterDrag as EventListener,\n\t\t\t\ttrue,\n\t\t\t);\n\t\t}\n\t}\n}\n\nexport { FlipBook, type PageSemantics };\nexport type {\n\tFlipPageSemantic,\n\tHistoryMapper,\n\tMouseMode,\n\tPageFlipDirection,\n\tPageFlipParams,\n} from \"./flip-book-options\";\n"],"names":["FlipDirection","foldStrength","position","clamp","value","lower","upper","progress","dimension","clipAtCrease","polygon","normal","midpoint","side","clipped","distance","point","previous","previousDistance","current","currentDistance","previousInside","currentInside","fraction","unique","index","next","constrainDestination","width","height","target","corner","cornerY","destination","anchors","tolerance","inside","anchor","allowedError","candidates","dx","dy","scale","length","candidate","other","score","nearest","getPageFoldDestination","pointerOffset","w","h","p","strength","inward","offset","travel","computePageFoldFromPoint","deltaX","deltaY","separation","projection","reflection","sheet","computePageFold","easePageTurn","scheduleFrame","cb","SHADOW_STRENGTH_MULTIPLIER","HIGHLIGHT_STRENGTH_MULTIPLIER","Leaf","pages","isFlipped","bookProperties","onTurned","normalizedY","size","fold","previousPosition","previousPoint","clamped","page","flipPosition","velocity","resolve","currentFlipPosition","startingFoldPoint","targetFoldPoint","duration","start","step","timestamp","elapsed","easedProgress","newPosition","throttle","clampedPosition","pageShadow","previewPosition","paperSize","shadowStrength","highlightStrength","shadowFromFlip","shadowProgress","isLTR","isOdd","rotateY","translateX","scaleX","origin","edge","folded","beforeSpine","leafOrder","faceOrder","manualFold","backwardPreview","a","b","c","d","tx","ty","offsetX","front","mirror","nx","mx","span","minimum","stop","angle","band","opacity","AspectRatioImpl","aspectRatio","Size","rhsAspectRatio","rhsAspectRatioValue","DEFAULT_FAST_DELTA","EDGE_ZONE_RATIO","HOVER_STRENGTH_MAX","MOUSE_MOVE_THROTTLE_MS","historyOwnerEpoch","nextHistoryOwner","FlipBook","el","pageIndex","leftmostPageIndex","direction","rightPageIndex","pageIndices","semantics","isInitial","semantic","params","route","currentState","state","url","event","secondLeafIndex","i","leaf","options","setTocPageIndex","mode","coverPageIndicesSet","indices","pageIdx","coverSize","leafSize","containerWidth","containerHeight","pageElement","pageSize","selector","debug","bookElement","pageElements","leavesCount","leafIndex","isOddPage","isInitiallyTurned","firstVisibleLeafIndex","isCurrentPage","currentVisiblePageIndices","prevVisiblePageIndices","initialPosition","lastTurnedLeafIndex","firstVisiblePageIndex","lastLeafFirstPage","Hammer","leftLeaf","rightLeaf","currentLeafIndex","bufferStart","bufferEnd","coverLeafIndices","isWithinBuffer","visible","debugBar","activeFlipsInfo","idx","flipState","currentPos","bookWidth","startingPos","delta","verticalMove","isRightSide","originY","pageWidth","pageHeight","hasDimensions","rigid","pointerY","top","bookTop","cancelled","ppsX","flipTo","targetPageIndex","e","touch","absDx","absDy","rect","x","y","spineX","isForward","distanceFromEdge","edgeZone","hoverStrength","prevVisibilePageIndices","inCurrent","inPrev","lastPageIndex","targetLastTurned","currentLastTurned","steps","isLastPage","closeBookReversed","shouldBeTurned","wasRestoring"],"mappings":";;;AAAO,IAAKA,sBAAAA,OACXA,EAAA,UAAU,WACVA,EAAA,WAAW,YACXA,EAAA,OAAO,QAHIA,IAAAA,KAAA,CAAA,CAAA;ACeZ,SAASC,EAAaC,GAA0B;AAC/C,SAAOA,MAAa,KAAKA,MAAa,IAAI,IAAI,KAAK,IAAI,KAAK,KAAKA,CAAQ;AAC1E;AAEA,SAASC,EAAMC,GAAeC,GAAeC,GAAuB;AACnE,SAAO,KAAK,IAAID,GAAO,KAAK,IAAIC,GAAOF,CAAK,CAAC;AAC9C;AAEA,SAASG,EAASH,GAAuB;AACxC,SAAO,OAAO,MAAMA,CAAK,IAAI,IAAID,EAAMC,GAAO,GAAG,CAAC;AACnD;AAEA,SAASI,EAAUJ,GAAuB;AACzC,SAAO,OAAO,SAASA,CAAK,KAAKA,IAAQ,IAAI,KAAK,IAAIA,GAAO,OAAO,gBAAgB,IAAI;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A;AAAA;AAAA;AAAA;AAAA,EAMQ,mBAAmBlF,GAAYjK,GAAwB;AAC9D,IAAAiK,EAAK,eAAejK,CAAQ;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,UAAU;AAaT,QAZI,OAAO,SAAW,OAAe,KAAK,mBACzC,OAAO,oBAAoB,YAAY,KAAK,gBAAgB,EAAI,GAChE,KAAK,iBAAiB,SAEnB,KAAK,mBACR,KAAK,eAAe,WAAA,GACpB,KAAK,iBAAiB,SAEnB,KAAK,WACR,KAAK,OAAO,QAAA,GACZ,KAAK,SAAS,SAEX,KAAK,aAAa;AACrB,iBAAW+E,KAAQ,KAAK;AACvB,QAAAA,EAAK,oBAAoB,eAAe,KAAK,mBAAmB,GAChEA,EAAK,oBAAoB,aAAa,KAAK,mBAAmB,GAC9DA,EAAK,oBAAoB,aAAa,KAAK,qBAAqB,EAAI;AAErE,WAAK,YAAY,oBAAoB,cAAc,KAAK,gBAAiC,GACzF,KAAK,YAAY,oBAAoB,aAAa,KAAK,eAAgC,GACvF,KAAK,YAAY;AAAA,QAChB;AAAA,QACA,KAAK;AAAA,MAAA,GAEN,KAAK,YAAY,oBAAoB,cAAc,KAAK,gBAAiC,GACzF,KAAK,YAAY;AAAA,QAChB;AAAA,QACA,KAAK;AAAA,QACL;AAAA,MAAA;AAAA,IAEF;AAAA,EACD;AACD;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "html-flip-book-vanilla",
3
- "version": "0.0.0-alpha.47",
3
+ "version": "0.0.0-alpha.48",
4
4
  "description": "Flipbook component for HTML (vanilla JavaScript)",
5
5
  "author": "DoradSoft",
6
6
  "type": "module",