Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add Excel advanced mode preview support in preview API and viewer
Cursor Agent6a9828894721
diff --git a/components/documents/preview-viewers/index.ts b/components/documents/preview-viewers/index.tsindex 3b0782ea..85449c05 100644--- a/components/documents/preview-viewers/index.ts+++ b/components/documents/preview-viewers/index.ts@@ -1,3 +1,4 @@+export { PreviewExcelViewer } from "./preview-excel-viewer";export { PreviewImageViewer } from "./preview-image-viewer";export { PreviewPagesViewer } from "./preview-pages-viewer";export { PreviewViewer } from "./preview-viewer";diff --git a/components/documents/preview-viewers/preview-excel-viewer.tsx b/components/documents/preview-viewers/preview-excel-viewer.tsxnew file mode 100644index 00000000..e8ff592f--- /dev/null+++ b/components/documents/preview-viewers/preview-excel-viewer.tsx@@ -0,0 +1,58 @@+import { useState } from "react";++import { DocumentPreviewData } from "@/lib/types/document-preview";+import { cn } from "@/lib/utils";++interface PreviewExcelViewerProps {+ documentData: DocumentPreviewData;+ onClose: () => void;+}++export function PreviewExcelViewer({+ documentData,+ onClose,+}: PreviewExcelViewerProps) {+ const [iframeLoaded, setIframeLoaded] = useState(false);++ const { file, documentName } = documentData;++ if (!file) {+ return (+ <div className="flex h-full w-full items-center justify-center">+ <p className="text-gray-400">Excel preview not available</p>+ </div>+ );+ }++ const embedUrl = `https://view.officeapps.live.com/op/embed.aspx?src=${encodeURIComponent(file)}&wdPrint=0&action=embedview&wdAllowInteractivity=False`;++ return (+ <div className="relative h-full w-full overflow-hidden">+ {/* Document Title */}+ <div className="absolute left-1/2 top-4 z-50 -translate-x-1/2">+ <div className="rounded-lg bg-black/20 px-3 py-2 text-white">+ <span className="text-sm font-medium">{documentName}</span>+ </div>+ </div>++ {/* Loading indicator */}+ {!iframeLoaded && (+ <div className="absolute inset-0 flex items-center justify-center">+ <div className="h-8 w-8 animate-spin rounded-full border-2 border-white border-t-transparent" />+ </div>+ )}++ {/* Office Online iframe */}+ <div className="h-full w-full pt-14 pb-2 px-2">+ <iframe+ className={cn(+ "h-full w-full rounded-md transition-opacity duration-200",+ iframeLoaded ? "opacity-100" : "opacity-0",+ )}+ src={embedUrl}+ onLoad={() => setIframeLoaded(true)}+ />+ </div>+ </div>+ );+}diff --git a/components/documents/preview-viewers/preview-viewer.tsx b/components/documents/preview-viewers/preview-viewer.tsxindex 491393c8..8ed9aba6 100644--- a/components/documents/preview-viewers/preview-viewer.tsx+++ b/components/documents/preview-viewers/preview-viewer.tsx@@ -1,5 +1,6 @@import { DocumentPreviewData } from "@/lib/types/document-preview";+import { PreviewExcelViewer } from "./preview-excel-viewer";import { PreviewImageViewer } from "./preview-image-viewer";import { PreviewPagesViewer } from "./preview-pages-viewer";@@ -24,13 +25,24 @@ export function PreviewViewer({ documentData, onClose }: PreviewViewerProps) {);}- // Excel/CSV files- if (documentData.fileType === "sheet" && documentData.sheetData) {+ // Excel/CSV files with advanced mode+ if (+ documentData.fileType === "sheet" &&+ documentData.advancedExcelEnabled &&+ documentData.file+ ) {+ return (+ <PreviewExcelViewer documentData={documentData} onClose={onClose} />+ );+ }++ // Excel/CSV files without advanced mode+ if (documentData.fileType === "sheet") {return (<div className="flex h-full w-full items-center justify-center"><div className="text-center"><p className="text-gray-400">- Sheet preview coming soon. Please preview via a shared link.+ Enable advanced Excel mode to preview this document.</p></div></div>diff --git a/lib/types/document-preview.ts b/lib/types/document-preview.tsindex b21d374f..c5b06132 100644--- a/lib/types/document-preview.ts+++ b/lib/types/document-preview.ts@@ -5,6 +5,7 @@ export interface DocumentPreviewData {fileType: string;isVertical: boolean;numPages: number;+ advancedExcelEnabled?: boolean;pages?: {file: string;pageNumber: string;diff --git a/pages/api/teams/[teamId]/documents/[id]/preview-data.ts b/pages/api/teams/[teamId]/documents/[id]/preview-data.tsindex e645b359..b5c38f07 100644--- a/pages/api/teams/[teamId]/documents/[id]/preview-data.ts+++ b/pages/api/teams/[teamId]/documents/[id]/preview-data.ts@@ -2,6 +2,8 @@ import { NextApiRequest, NextApiResponse } from "next";import { getServerSession } from "next-auth/next";+import { getTeamStorageConfigById } from "@/ee/features/storage/config";+import { getFile } from "@/lib/files/get-file";import prisma from "@/lib/prisma";import { CustomUser } from "@/lib/types";@@ -99,6 +101,7 @@ export default async function handle(fileType: primaryVersion.type,isVertical: primaryVersion.isVertical,numPages: primaryVersion.numPages,+ advancedExcelEnabled: document.advancedExcelEnabled,pages: undefined as any,file: undefined as string | undefined,sheetData: undefined as any,@@ -124,11 +127,21 @@ export default async function handle(});returnData.numPages = 1;} else if (primaryVersion.type === "sheet") {- // Excel/CSV files - preview not supported- return res.status(400).json({- message:- "Sheet preview not available. Please preview via a shared link.",- });+ if (document.advancedExcelEnabled) {+ // Advanced Excel mode: use Office Online viewer URL+ if (!primaryVersion.file.includes("https://")) {+ const storageConfig = await getTeamStorageConfigById(document.teamId);+ returnData.file = `https://${storageConfig.advancedDistributionHost}/${primaryVersion.file}`;+ } else {+ returnData.file = primaryVersion.file;+ }+ returnData.numPages = 1;+ } else {+ return res.status(400).json({+ message:+ "Sheet preview requires advanced Excel mode. Enable it from the document settings.",+ });+ }} else if (primaryVersion.type === "notion") {// Notion documents - preview not supportedreturn res.status(400).json({