Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions packages/app/src/components/reader/FoliateViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,22 @@ function getTTSSegmentIdentity(cfi?: string | null, text?: string | null) {
return `${cfi || ""}::${normalizeTTSSegmentText(text)}`;
}

function getIframePointInContainerFraction(
doc: Document,
container: HTMLElement | null,
clientX: number,
) {
const iframe = doc.defaultView?.frameElement as HTMLIFrameElement | null;
if (!iframe || !container) return null;

const iframeRect = iframe.getBoundingClientRect();
const containerRect = container.getBoundingClientRect();
const scaleX = iframe.clientWidth > 0 ? iframeRect.width / iframe.clientWidth : 1;
const x = iframeRect.left + clientX * scaleX - containerRect.left;
if (containerRect.width <= 0) return null;
return Math.max(0, Math.min(1, x / containerRect.width));
}

// Polyfills required by foliate-js
// biome-ignore lint: polyfill for foliate-js
(Object as any).groupBy ??= (
Expand Down Expand Up @@ -1824,7 +1840,10 @@ export const FoliateViewer = forwardRef<FoliateViewerHandle, FoliateViewerProps>
// Visible range in document coords: [scrollStart - pageWidth, scrollStart]
// Position within visible page: clientX - (scrollStart - pageWidth)
const visibleX = pageWidth > 0 ? clientX - (scrollStart - pageWidth) : clientX;
const xFraction = pageWidth > 0 ? visibleX / pageWidth : 0;
const xFraction =
pageWidth > 0
? visibleX / pageWidth
: getIframePointInContainerFraction(doc, containerRef.current, clientX);

setTimeout(() => {
// If show-annotation handler already handled this click, skip
Expand Down Expand Up @@ -1869,7 +1888,7 @@ export const FoliateViewer = forwardRef<FoliateViewerHandle, FoliateViewerProps>
clientY,
screenX,
screenY,
xFraction,
...(typeof xFraction === "number" ? { xFraction } : {}),
},
"*",
);
Expand Down
1 change: 1 addition & 0 deletions packages/foliate-js/fixed-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export class FixedLayout extends HTMLElement {
}
get index() {
const spread = this.#spreads[this.#index];
if (!spread) return -1;
const section =
spread?.center ??
(this.#side === "left" ? (spread.left ?? spread.right) : (spread.right ?? spread.left));
Expand Down