Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: make links clickable inside the page
Marc Seitz7af9a742131d
diff --git a/components/view/PagesViewerNew.tsx b/components/view/PagesViewerNew.tsxindex c69965ed..c2c7c668 100644--- a/components/view/PagesViewerNew.tsx+++ b/components/view/PagesViewerNew.tsx@@ -88,7 +88,13 @@ export default function PagesViewer({viewerEmail,isPreview,}: {- pages: { file: string; pageNumber: string; embeddedLinks: string[] }[];+ pages: {+ file: string;+ pageNumber: string;+ embeddedLinks: string[];+ pageLinks: { href: string; coords: string }[];+ metadata: { width: number; height: number; scaleFactor: number };+ }[];linkId: string;documentId: string;viewId?: string;@@ -161,6 +167,26 @@ export default function PagesViewer({const { isMobile } = useMediaQuery();+ const scaleCoordinates = (coords: string, scaleFactor: number) => {+ return coords+ .split(",")+ .map((coord) => parseFloat(coord) * scaleFactor)+ .join(",");+ };++ const getScaleFactor = ({+ naturalHeight,+ scaleFactor,+ }: {+ naturalHeight: number;+ scaleFactor: number;+ }) => {+ const containerHeight = containerRef.current+ ? containerRef.current.clientHeight+ : window.innerHeight - 64;+ return (scaleFactor * containerHeight) / naturalHeight;+ };+// Update the previous page number after the effect hook has runuseEffect(() => {pageNumberRef.current = pageNumber;@@ -682,15 +708,20 @@ export default function PagesViewer({style={{ height: "calc(100dvh - 64px)" }}className={cn("relative w-full",+ isVertical && "flex justify-center",pageNumber - 1 === index- ? "block"+ ? "flex": !isVertical? "hidden"- : "block",+ : "flex",)}><img- className={cn("h-full w-full object-contain")}+ className={cn(+ "!pointer-events-auto h-full object-contain",+ !isVertical && "w-full",+ )}+ useMap={`#page-map-${index + 1}`}src={loadedImages[index]? page.file@@ -698,6 +729,26 @@ export default function PagesViewer({}alt={`Page ${index + 1}`}/>+ {page.pageLinks ? (+ <map name={`page-map-${index + 1}`}>+ {page.pageLinks.map((link, index) => (+ <area+ key={index}+ shape="rect"+ coords={scaleCoordinates(+ link.coords,+ getScaleFactor({+ naturalHeight: page.metadata.height,+ scaleFactor: page.metadata.scaleFactor,+ }),+ )}+ href={link.href}+ target="_blank"+ rel="noopener noreferrer"+ />+ ))}+ </map>+ ) : null}</div></TransformComponent></TransformWrapper>diff --git a/pages/api/mupdf/convert-page.ts b/pages/api/mupdf/convert-page.tsindex 9acdc2ef..3c908d86 100644--- a/pages/api/mupdf/convert-page.ts+++ b/pages/api/mupdf/convert-page.ts@@ -61,10 +61,10 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {const bounds = page.getBounds();const [ulx, uly, lrx, lry] = bounds;const widthInPoints = Math.abs(lrx - ulx);+ const heightInPoints = Math.abs(lry - uly);if (pageNumber === 1) {// get the orientation of the document and update document version- const heightInPoints = Math.abs(lry - uly);const isVertical = heightInPoints > widthInPoints;await prisma.documentVersion.update({@@ -81,7 +81,17 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {// get linksconst links = page.getLinks();- const embeddedLinks = links.map((link: any) => link.getURI());+ const embeddedLinks = links.map((link) => {+ return { href: link.getURI(), coords: link.getBounds().join(",") };+ });++ const metadata = {+ originalWidth: widthInPoints,+ originalHeight: heightInPoints,+ width: widthInPoints * scaleFactor,+ height: heightInPoints * scaleFactor,+ scaleFactor: scaleFactor,+ };console.time("toPixmap");let scaledPixmap = page.toPixmap(@@ -163,7 +173,8 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {pageNumber: pageNumber,file: data,storageType: type,- embeddedLinks: embeddedLinks,+ pageLinks: embeddedLinks,+ metadata: metadata,},});} else {diff --git a/pages/api/views-dataroom.ts b/pages/api/views-dataroom.tsindex 42936e59..74124dd8 100644--- a/pages/api/views-dataroom.ts+++ b/pages/api/views-dataroom.ts@@ -432,6 +432,8 @@ export default async function handle(storageType: true,pageNumber: true,embeddedLinks: true,+ pageLinks: true,+ metadata: true,},});diff --git a/pages/api/views.ts b/pages/api/views.tsindex 5cc7f252..1ad99044 100644--- a/pages/api/views.ts+++ b/pages/api/views.ts@@ -321,6 +321,8 @@ export default async function handle(storageType: true,pageNumber: true,embeddedLinks: true,+ pageLinks: true,+ metadata: true,},});diff --git a/prisma/migrations/20240712000000_add_page_metadata/migration.sql b/prisma/migrations/20240712000000_add_page_metadata/migration.sqlnew file mode 100644index 00000000..6580b7b3--- /dev/null+++ b/prisma/migrations/20240712000000_add_page_metadata/migration.sql@@ -0,0 +1,4 @@+-- AlterTable+ALTER TABLE "DocumentPage" ADD COLUMN "metadata" JSONB,+ADD COLUMN "pageLinks" JSONB;+diff --git a/prisma/schema.prisma b/prisma/schema.prismaindex 553c33c6..8a266bfa 100644--- a/prisma/schema.prisma+++ b/prisma/schema.prisma@@ -177,6 +177,8 @@ model DocumentPage {versionId StringpageNumber Int // e.g., 1, 2, 3 forembeddedLinks String[]+ pageLinks Json? // This will store the page links data: [{href: "https://example.com", coords: "0,0,100,100"}]+ metadata Json? // This will store the page metadata: {originalWidth: 100, origianlHeight: 100, scaledWidth: 50, scaledHeight: 50, scaleFactor: 2}file String // This should be a reference to where the file / page is stored (S3, Google Cloud Storage, etc.)storageType DocumentStorageType @default(VERCEL_BLOB)createdAt DateTime @default(now())