Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add improved table of content loading

Marc Seitz6ea9bb0708b6
diff --git a/components/view/dataroom/dataroom-document-view.tsx b/components/view/dataroom/dataroom-document-view.tsx
index 47b0a1bb..3a0943fe 100644
--- a/components/view/dataroom/dataroom-document-view.tsx
+++ b/components/view/dataroom/dataroom-document-view.tsx
@@ -40,7 +40,12 @@ export type DEFAULT_DATAROOM_DOCUMENT_VIEW_TYPE = {
file: string;
pageNumber: string;
embeddedLinks: string[];
- pageLinks: { href: string; coords: string }[];
+ pageLinks: {
+ href: string;
+ coords: string;
+ isInternal?: boolean;
+ targetPage?: number;
+ }[];
metadata: { width: number; height: number; scaleFactor: number };
}[]
| null;
diff --git a/components/view/document-view.tsx b/components/view/document-view.tsx
index c67d4d21..498ad873 100644
--- a/components/view/document-view.tsx
+++ b/components/view/document-view.tsx
@@ -35,7 +35,12 @@ export type DEFAULT_DOCUMENT_VIEW_TYPE = {
file: string;
pageNumber: string;
embeddedLinks: string[];
- pageLinks: { href: string; coords: string }[];
+ pageLinks: {
+ href: string;
+ coords: string;
+ isInternal?: boolean;
+ targetPage?: number;
+ }[];
metadata: { width: number; height: number; scaleFactor: number };
}[]
| null;
diff --git a/components/view/viewer/pages-horizontal-viewer.tsx b/components/view/viewer/pages-horizontal-viewer.tsx
index 2eccc462..723cbe7e 100644
--- a/components/view/viewer/pages-horizontal-viewer.tsx
+++ b/components/view/viewer/pages-horizontal-viewer.tsx
@@ -11,10 +11,7 @@ import { getTrackingOptions } from "@/lib/tracking/tracking-config";
import { WatermarkConfig } from "@/lib/types";
import { cn } from "@/lib/utils";
-import {
- ResizablePanel,
- ResizablePanelGroup,
-} from "@/components/ui/resizable";
+import { ResizablePanel, ResizablePanelGroup } from "@/components/ui/resizable";
import { ScreenProtector } from "../ScreenProtection";
import Nav, { TNavData } from "../nav";
@@ -48,7 +45,12 @@ export default function PagesHorizontalViewer({
file: string;
pageNumber: string;
embeddedLinks: string[];
- pageLinks: { href: string; coords: string }[];
+ pageLinks: {
+ href: string;
+ coords: string;
+ isInternal?: boolean;
+ targetPage?: number;
+ }[];
metadata: { width: number; height: number; scaleFactor: number };
}[];
feedbackEnabled: boolean;
@@ -901,7 +903,6 @@ export default function PagesHorizontalViewer({
/>
</div>
</ResizablePanel>
-
</ResizablePanelGroup>
</div>
</>
diff --git a/components/view/viewer/pages-vertical-viewer.tsx b/components/view/viewer/pages-vertical-viewer.tsx
index d6d7fc09..407f60a2 100644
--- a/components/view/viewer/pages-vertical-viewer.tsx
+++ b/components/view/viewer/pages-vertical-viewer.tsx
@@ -11,10 +11,7 @@ import { WatermarkConfig } from "@/lib/types";
import { cn } from "@/lib/utils";
import { useMediaQuery } from "@/lib/utils/use-media-query";
-import {
- ResizablePanel,
- ResizablePanelGroup,
-} from "@/components/ui/resizable";
+import { ResizablePanel, ResizablePanelGroup } from "@/components/ui/resizable";
import { ScreenProtector } from "../ScreenProtection";
import Nav, { TNavData } from "../nav";
@@ -73,7 +70,12 @@ export default function PagesVerticalViewer({
file: string;
pageNumber: string;
embeddedLinks: string[];
- pageLinks: { href: string; coords: string }[];
+ pageLinks: {
+ href: string;
+ coords: string;
+ isInternal?: boolean;
+ targetPage?: number;
+ }[];
metadata: { width: number; height: number; scaleFactor: number };
}[];
feedbackEnabled: boolean;
@@ -621,13 +623,14 @@ export default function PagesVerticalViewer({
isPreview,
});
- // Preload target page and 2 pages on either side
- const startPage = Math.max(0, targetPage - 2 - 1);
+ // Load all pages from start to target + buffer to ensure proper scroll height
+ // This is necessary because unloaded pages don't take up space in the DOM
const endPage = Math.min(numPages - 1, targetPage + 2 - 1);
setLoadedImages((prev) => {
const newLoadedImages = [...prev];
- for (let i = startPage; i <= endPage; i++) {
+ // Load all pages from 0 to endPage to create proper scroll height
+ for (let i = 0; i <= endPage; i++) {
newLoadedImages[i] = true;
}
return newLoadedImages;
@@ -635,16 +638,49 @@ export default function PagesVerticalViewer({
setPageNumber(targetPage);
pageNumberRef.current = targetPage;
- if (containerRef.current) {
- scrollActionRef.current = true;
- const newScrollPosition =
- ((targetPage - 1) * containerRef.current.scrollHeight) /
- numPagesWithAccountCreation;
- containerRef.current.scrollTo({
- top: newScrollPosition,
- behavior: "smooth",
- });
- }
+
+ // Wait for images to load before scrolling
+ const waitForImageAndScroll = () => {
+ const targetImg = imageRefs.current[targetPage - 1];
+
+ // Check if target image exists and is loaded
+ if (targetImg && targetImg.complete && targetImg.naturalHeight > 0) {
+ scrollActionRef.current = true;
+ targetImg.scrollIntoView({ behavior: "smooth", block: "start" });
+ return;
+ }
+
+ // If image element exists but not loaded, wait for it
+ if (targetImg) {
+ const handleLoad = () => {
+ scrollActionRef.current = true;
+ targetImg.scrollIntoView({ behavior: "smooth", block: "start" });
+ targetImg.removeEventListener("load", handleLoad);
+ };
+ targetImg.addEventListener("load", handleLoad);
+
+ // Timeout fallback in case image is already cached but complete wasn't set
+ setTimeout(() => {
+ targetImg.removeEventListener("load", handleLoad);
+ if (targetImg) {
+ scrollActionRef.current = true;
+ targetImg.scrollIntoView({
+ behavior: "smooth",
+ block: "start",
+ });
+ }
+ }, 500);
+ return;
+ }
+
+ // Image ref not available yet, wait for React to render
+ requestAnimationFrame(waitForImageAndScroll);
+ };
+
+ // Start checking after React processes the state update
+ requestAnimationFrame(() => {
+ requestAnimationFrame(waitForImageAndScroll);
+ });
// Reset the start time for the new page
startTimeRef.current = Date.now();
@@ -1060,7 +1096,6 @@ export default function PagesVerticalViewer({
{/* </div> */}
{/* </div> */}
</ResizablePanel>
-
</ResizablePanelGroup>
</div>
</>
diff --git a/lib/types/document-preview.ts b/lib/types/document-preview.ts
index e66dc1da..b21d374f 100644
--- a/lib/types/document-preview.ts
+++ b/lib/types/document-preview.ts
@@ -9,7 +9,12 @@ export interface DocumentPreviewData {
file: string;
pageNumber: string;
embeddedLinks: string[];
- pageLinks: { href: string; coords: string }[];
+ pageLinks: {
+ href: string;
+ coords: string;
+ isInternal?: boolean;
+ targetPage?: number;
+ }[];
metadata: { width: number; height: number; scaleFactor: number };
}[];
file?: string;
diff --git a/pages/api/mupdf/convert-page.ts b/pages/api/mupdf/convert-page.ts
index c1743845..c3ac07ef 100644
--- a/pages/api/mupdf/convert-page.ts
+++ b/pages/api/mupdf/convert-page.ts
@@ -142,7 +142,30 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
// get links
const links = page.getLinks();
const embeddedLinks = links.map((link) => {
- return { href: link.getURI(), coords: link.getBounds().join(",") };
+ const coords = link.getBounds().join(",");
+
+ // Check if this is an internal link (GoTo action for TOC, etc.)
+ if (!link.isExternal()) {
+ try {
+ // Resolve internal link to page number (0-indexed from mupdf)
+ const targetPage = doc.resolveLink(link);
+ if (targetPage >= 0) {
+ return {
+ href: `#page=${targetPage + 1}`, // Convert to 1-indexed for frontend
+ coords,
+ isInternal: true,
+ targetPage: targetPage + 1,
+ };
+ }
+ } catch (e) {
+ console.log("Failed to resolve internal link:", e);
+ }
+ // Fallback for unresolvable internal links
+ return { href: "", coords, isInternal: true };
+ }
+
+ // External URI link
+ return { href: link.getURI(), coords, isInternal: false };
});
// Check embedded links for blocked keywords