Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add enhanced screenprotection
Marc Seitz1f5ae5c45a0f
diff --git a/components/NotionPage.tsx b/components/NotionPage.tsxindex 5e1ea3e1..38031a97 100644--- a/components/NotionPage.tsx+++ b/components/NotionPage.tsx@@ -12,6 +12,7 @@ import { NotionRenderer } from "react-notion-x";import "react-notion-x/src/styles.css";import { NotionTheme } from "@/lib/types";+import { cn } from "@/lib/utils";import { determineTextColor } from "@/lib/utils/determine-text-color";// custom styles for notion@@ -26,6 +27,7 @@ import {BreadcrumbSeparator,} from "./ui/breadcrumb";import { Portal } from "./ui/portal";+import { ScreenProtector } from "./view/ScreenProtection";import { TDocumentData } from "./view/dataroom/dataroom-view";import Nav from "./view/nav";@@ -48,6 +50,7 @@ export const NotionPage = ({setDocumentData,isPreview,theme,+ screenshotProtectionEnabled,}: {recordMap: ExtendedRecordMap;rootPageId?: string;@@ -61,6 +64,7 @@ export const NotionPage = ({setDocumentData?: React.Dispatch<React.SetStateAction<TDocumentData | null>>;isPreview?: boolean;theme?: NotionTheme | null;+ screenshotProtectionEnabled: boolean;}) => {const [pageNumber, setPageNumber] = useState<number>(1); // start on first pageconst [maxScrollPercentage, setMaxScrollPercentage] = useState<number>(0);@@ -72,6 +76,8 @@ export const NotionPage = ({const [subTitle, setSubTitle] = useState<string>("");const [title, setTitle] = useState<string>("");+ const [isWindowFocused, setIsWindowFocused] = useState(true);+const [recordMapState, setRecordMapState] =useState<ExtendedRecordMap>(recordMap);@@ -107,6 +113,22 @@ export const NotionPage = ({};}, []);+ // Add this effect near your other useEffect hooks+ useEffect(() => {+ if (!screenshotProtectionEnabled) return;++ const handleFocus = () => setIsWindowFocused(true);+ const handleBlur = () => setIsWindowFocused(false);++ window.addEventListener("focus", handleFocus);+ window.addEventListener("blur", handleBlur);++ return () => {+ window.removeEventListener("focus", handleFocus);+ window.removeEventListener("blur", handleBlur);+ };+ }, [screenshotProtectionEnabled]);+// Memoize the fetchSubPage functionconst fetchSubPage = useCallback(async (pageId: string | null) => {@@ -314,7 +336,13 @@ export const NotionPage = ({{loading && <div>Loading...</div>}- <div>+ <div+ className={cn(+ !isWindowFocused &&+ screenshotProtectionEnabled &&+ "blur-xl transition-all duration-300",+ )}+ ><NotionRendererrecordMap={recordMapState}fullPage={true}@@ -350,6 +378,7 @@ export const NotionPage = ({}}/></div>+ {screenshotProtectionEnabled ? <ScreenProtector /> : null}</div>);};diff --git a/components/view/PagesViewerNew.tsx b/components/view/PagesViewerNew.tsxindex a93b7498..9a764bcb 100644--- a/components/view/PagesViewerNew.tsx+++ b/components/view/PagesViewerNew.tsx@@ -164,6 +164,8 @@ export default function PagesViewer({const [viewedPages, setViewedPages] =useState<{ pageNumber: number; duration: number }[]>(initialViewedPages);+ const [isWindowFocused, setIsWindowFocused] = useState(true);+const startTimeRef = useRef(Date.now());const pageNumberRef = useRef<number>(pageNumber);const visibilityRef = useRef<boolean>(true);@@ -308,6 +310,22 @@ export default function PagesViewer({};}, [pageNumber, numPages]);+ // Add this effect near your other useEffect hooks+ useEffect(() => {+ if (!screenshotProtectionEnabled) return;++ const handleFocus = () => setIsWindowFocused(true);+ const handleBlur = () => setIsWindowFocused(false);++ window.addEventListener("focus", handleFocus);+ window.addEventListener("blur", handleBlur);++ return () => {+ window.removeEventListener("focus", handleFocus);+ window.removeEventListener("blur", handleBlur);+ };+ }, [screenshotProtectionEnabled]);+useEffect(() => {setLoadedImages((prev) =>prev.map((loaded, index) =>@@ -690,190 +708,198 @@ export default function PagesViewer({className={cn("relative flex items-center", isVertical && "h-dvh")}><div- className={`relative flex h-full w-full ${isVertical ? "flex-col overflow-y-auto" : "flex-row"- }`}+ className={cn(+ "relative flex h-full w-full",+ isVertical ? "flex-col overflow-y-auto" : "flex-row",+ !isWindowFocused &&+ screenshotProtectionEnabled &&+ "blur-xl transition-all duration-300",+ )}ref={containerRef}><div- className={`flex ${isVertical ? "flex-col items-center" : "flex-row justify-center"} w-full`}+ className={cn(+ "flex w-full",+ isVertical ? "flex-col items-center" : "flex-row justify-center",+ )}onContextMenu={handleContextMenu}>{pageNumber <= numPagesWithAccountCreation &&- pages &&- loadedImages[pageNumber - 1]+ pages &&+ loadedImages[pageNumber - 1]? pages.map((page, index) => (- <TransformWrapper- key={index}- initialScale={scale}- initialPositionX={0}- initialPositionY={0}- disabled={isVertical && isMobile}- panning={{- lockAxisY: isVertical,- velocityDisabled: true,- wheelPanning: false,- }}- wheel={{ disabled: true }}- pinch={{ disabled: true }}- doubleClick={{ disabled: true }}- onZoom={(ref) => {- setScale(ref.state.scale);- }}- ref={(ref) => {- pinchRefs.current[index] = ref;- }}- customTransform={(x: number, y: number, scale: number) => {- // Keep the translateY value constant- if (isVertical) {- const transform = `translate(${x}px, ${index * y * -2}px) scale(${scale})`;+ <TransformWrapper+ key={index}+ initialScale={scale}+ initialPositionX={0}+ initialPositionY={0}+ disabled={isVertical && isMobile}+ panning={{+ lockAxisY: isVertical,+ velocityDisabled: true,+ wheelPanning: false,+ }}+ wheel={{ disabled: true }}+ pinch={{ disabled: true }}+ doubleClick={{ disabled: true }}+ onZoom={(ref) => {+ setScale(ref.state.scale);+ }}+ ref={(ref) => {+ pinchRefs.current[index] = ref;+ }}+ customTransform={(x: number, y: number, scale: number) => {+ // Keep the translateY value constant+ if (isVertical) {+ const transform = `translate(${x}px, ${index * y * -2}px) scale(${scale})`;+ return transform;+ }+ const transform = `translate(${x}px, ${y}px) scale(${scale})`;return transform;- }- const transform = `translate(${x}px, ${y}px) scale(${scale})`;- return transform;- }}- >- <TransformComponent- wrapperClass={cn(- !isVertical && "!h-full",- isVertical- ? "!overflow-x-clip !overflow-y-visible"- : isMobile- ? "!overflow-x-clip !overflow-y-clip"- : "!overflow-x-visible !overflow-y-clip",- )}- contentClass={cn(- !isVertical && "!h-full",- isVertical && "!w-dvw !h-[calc(100dvh-64px)]",- )}+ }}>- <div- key={index}- className={cn(- "relative my-auto w-full",- pageNumber - 1 === index && !isVertical- ? "block"- : "hidden",- isVertical && "flex justify-center",+ <TransformComponent+ wrapperClass={cn(+ !isVertical && "!h-full",+ isVertical+ ? "!overflow-x-clip !overflow-y-visible"+ : isMobile+ ? "!overflow-x-clip !overflow-y-clip"+ : "!overflow-x-visible !overflow-y-clip",+ )}+ contentClass={cn(+ !isVertical && "!h-full",+ isVertical && "!w-dvw !h-[calc(100dvh-64px)]",)}>- <img+ <div+ key={index}className={cn(- "!pointer-events-auto object-contain",- isVertical && "h-auto",+ "relative my-auto w-full",+ pageNumber - 1 === index && !isVertical+ ? "block"+ : "hidden",+ isVertical && "flex justify-center",)}- style={{- maxHeight: "calc(100dvh - 64px)",- }}- // ref={(ref) => {- // imageRefs.current[index] = ref;- // }}- ref={(ref) => {- imageRefs.current[index] = ref;- if (ref) {- ref.onload = () =>- setImageDimensions((prev) => ({- ...prev,- [index]: {- width: ref.clientWidth,- height: ref.clientHeight,- },- }));- }- }}- useMap={`#page-map-${index + 1}`}- src={- loadedImages[index]- ? page.file- : "https://www.papermark.io/_static/blank.gif"- }- alt={`Page ${index + 1}`}- />-- {/* Add Watermark Component */}- {watermarkConfig ? (- <SVGWatermark- config={watermarkConfig}- viewerData={{- email: viewerEmail,- date: new Date().toLocaleDateString(),- time: new Date().toLocaleTimeString(),- link: linkName,- ipAddress: ipAddress,+ >+ <img+ className={cn(+ "!pointer-events-auto object-contain",+ isVertical && "h-auto",+ )}+ style={{+ maxHeight: "calc(100dvh - 64px)",+ }}+ // ref={(ref) => {+ // imageRefs.current[index] = ref;+ // }}+ ref={(ref) => {+ imageRefs.current[index] = ref;+ if (ref) {+ ref.onload = () =>+ setImageDimensions((prev) => ({+ ...prev,+ [index]: {+ width: ref.clientWidth,+ height: ref.clientHeight,+ },+ }));+ }}}- documentDimensions={- imageDimensions[index] || { width: 0, height: 0 }+ useMap={`#page-map-${index + 1}`}+ src={+ loadedImages[index]+ ? page.file+ : "https://www.papermark.io/_static/blank.gif"}- pageIndex={index}+ alt={`Page ${index + 1}`}/>- ) : null}-- {page.pageLinks ? (- <map name={`page-map-${index + 1}`}>- {page.pageLinks- .filter((link) => !link.href.includes(".gif"))- .map((link, index) => (- <area- key={index}- shape="rect"- coords={scaleCoordinates(++ {/* Add Watermark Component */}+ {watermarkConfig ? (+ <SVGWatermark+ config={watermarkConfig}+ viewerData={{+ email: viewerEmail,+ date: new Date().toLocaleDateString(),+ time: new Date().toLocaleTimeString(),+ link: linkName,+ ipAddress: ipAddress,+ }}+ documentDimensions={+ imageDimensions[index] || { width: 0, height: 0 }+ }+ pageIndex={index}+ />+ ) : null}++ {page.pageLinks ? (+ <map name={`page-map-${index + 1}`}>+ {page.pageLinks+ .filter((link) => !link.href.includes(".gif"))+ .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}++ {/** Automatically Render Overlays **/}+ {page.pageLinks+ ? page.pageLinks+ .filter((link) => link.href.includes(".gif"))+ .map((link, linkIndex) => {+ const [x1, y1, x2, y2] = scaleCoordinates(link.coords,getScaleFactor({naturalHeight: page.metadata.height,scaleFactor: page.metadata.scaleFactor,}),- )}- href={link.href}- target="_blank"- rel="noopener noreferrer"- />- ))}- </map>- ) : null}-- {/** Automatically Render Overlays **/}- {page.pageLinks- ? page.pageLinks- .filter((link) => link.href.includes(".gif"))- .map((link, linkIndex) => {- const [x1, y1, x2, y2] = scaleCoordinates(- link.coords,- getScaleFactor({- naturalHeight: page.metadata.height,- scaleFactor: page.metadata.scaleFactor,- }),- )- .split(",")- .map(Number);-- const overlayWidth = x2 - x1;- const overlayHeight = y2 - y1;-- return (- <img- key={`overlay-${index}-${linkIndex}`}- src={link.href} // Assuming the href points to a GIF or overlay image- alt={`Overlay ${index + 1}`}- style={{- position: "absolute",- top: y1,- left: x1,- width: `${overlayWidth}px`,- height: `${overlayHeight}px`,- pointerEvents: "none", // To ensure the overlay doesn't interfere with interaction- }}- />- );- })- : null}- </div>- </TransformComponent>- </TransformWrapper>- ))+ )+ .split(",")+ .map(Number);++ const overlayWidth = x2 - x1;+ const overlayHeight = y2 - y1;++ return (+ <img+ key={`overlay-${index}-${linkIndex}`}+ src={link.href} // Assuming the href points to a GIF or overlay image+ alt={`Overlay ${index + 1}`}+ style={{+ position: "absolute",+ top: y1,+ left: x1,+ width: `${overlayWidth}px`,+ height: `${overlayHeight}px`,+ pointerEvents: "none", // To ensure the overlay doesn't interfere with interaction+ }}+ />+ );+ })+ : null}+ </div>+ </TransformComponent>+ </TransformWrapper>+ )): null}{enableQuestion &&- feedback &&- (isVertical || pageNumber === numPagesWithFeedback) ? (+ feedback &&+ (isVertical || pageNumber === numPagesWithFeedback) ? (<divclassName={cn("relative block h-dvh w-full")}style={{ height: "calc(100dvh - 64px)" }}@@ -889,8 +915,8 @@ export default function PagesViewer({) : null}{showStatsSlideWithAccountCreation &&- !isVertical &&- pageNumber === numPagesWithAccountCreation ? (+ !isVertical &&+ pageNumber === numPagesWithAccountCreation ? (<divclassName={cn("relative block h-dvh w-full")}style={{ height: "calc(100dvh - 64px)" }}diff --git a/components/view/dataroom/dataroom-view.tsx b/components/view/dataroom/dataroom-view.tsxindex e67ab27b..53c151c5 100644--- a/components/view/dataroom/dataroom-view.tsx+++ b/components/view/dataroom/dataroom-view.tsx@@ -345,6 +345,7 @@ export default function DataroomView({dataroomId={dataroom.id}theme={viewData.notionData.theme}setDocumentData={setDocumentData}+ screenshotProtectionEnabled={link.enableScreenshotProtection!}/></div>) : viewData.fileType === "sheet" && viewData.sheetData ? (@@ -361,6 +362,7 @@ export default function DataroomView({dataroomId={dataroom.id}setDocumentData={setDocumentData}allowDownload={viewData.canDownload ?? link.allowDownload!}+ screenshotProtectionEnabled={link.enableScreenshotProtection!}/></div>) : viewData.fileType === "sheet" && viewData.useAdvancedExcelViewer ? (diff --git a/components/view/view-data.tsx b/components/view/view-data.tsxindex a289b305..7ac33e5f 100644--- a/components/view/view-data.tsx+++ b/components/view/view-data.tsx@@ -54,6 +54,7 @@ export default function ViewData({versionNumber={document.versions[0].versionNumber}brand={brand}theme={notionData.theme}+ screenshotProtectionEnabled={link.enableScreenshotProtection!}/>) : viewData.fileType === "sheet" && viewData.sheetData ? (<ExcelViewer@@ -66,6 +67,7 @@ export default function ViewData({sheetData={viewData.sheetData}brand={brand}allowDownload={link.allowDownload!}+ screenshotProtectionEnabled={link.enableScreenshotProtection!}/>) : viewData.fileType === "sheet" && useAdvancedExcelViewer ? (<AdvancedExcelViewerdiff --git a/components/view/viewer/excel-viewer.tsx b/components/view/viewer/excel-viewer.tsxindex addf26d1..6b88f01f 100644--- a/components/view/viewer/excel-viewer.tsx+++ b/components/view/viewer/excel-viewer.tsx@@ -8,6 +8,7 @@ import { Button } from "@/components/ui/button";import { cn } from "@/lib/utils";+import { ScreenProtector } from "../ScreenProtection";import { TDocumentData } from "../dataroom/dataroom-view";import Nav from "../nav";@@ -49,6 +50,7 @@ export default function ExcelViewer({versionNumber,sheetData,allowDownload,+ screenshotProtectionEnabled,brand,dataroomId,setDocumentData,@@ -61,6 +63,7 @@ export default function ExcelViewer({versionNumber: number;sheetData: SheetData[];allowDownload: boolean;+ screenshotProtectionEnabled: boolean;brand?: Partial<Brand> | Partial<DataroomBrand> | null;dataroomId?: string;setDocumentData?: React.Dispatch<React.SetStateAction<TDocumentData | null>>;@@ -71,6 +74,8 @@ export default function ExcelViewer({const [handsontableLoaded, setHandsontableLoaded] = useState<boolean>(false);const [selectedSheetIndex, setSelectedSheetIndex] = useState<number>(0);+ const [isWindowFocused, setIsWindowFocused] = useState(true);+useEffect(() => {const script = document.createElement("script");script.src =@@ -116,6 +121,22 @@ export default function ExcelViewer({return () => window.removeEventListener("resize", handleResize);}, []);+ // Add this effect near your other useEffect hooks+ useEffect(() => {+ if (!screenshotProtectionEnabled) return;++ const handleFocus = () => setIsWindowFocused(true);+ const handleBlur = () => setIsWindowFocused(false);++ window.addEventListener("focus", handleFocus);+ window.addEventListener("blur", handleBlur);++ return () => {+ window.removeEventListener("focus", handleFocus);+ window.removeEventListener("blur", handleBlur);+ };+ }, [screenshotProtectionEnabled]);+useEffect(() => {const handleVisibilityChange = () => {if (document.visibilityState === "visible") {@@ -235,7 +256,12 @@ export default function ExcelViewer({/><divstyle={{ height: "calc(100dvh - 64px)" }}- className="mx-2 flex h-dvh flex-col sm:mx-6 lg:mx-8"+ className={cn(+ "mx-2 flex h-dvh flex-col sm:mx-6 lg:mx-8",+ !isWindowFocused &&+ screenshotProtectionEnabled &&+ "blur-xl transition-all duration-300",+ )}ref={containerRef}><div className="" ref={hotRef}></div>@@ -247,7 +273,7 @@ export default function ExcelViewer({className={cn("mb-1 rounded-none rounded-b-sm bg-[#f0f0f0] font-normal text-gray-950 hover:bg-gray-50",index === selectedSheetIndex &&- "bg-white font-medium text-black ring-1 ring-gray-500 hover:bg-white",+ "bg-white font-medium text-black ring-1 ring-gray-500 hover:bg-white",)}>{sheet.sheetName}@@ -255,6 +281,7 @@ export default function ExcelViewer({</div>))}</div>+ {screenshotProtectionEnabled ? <ScreenProtector /> : null}</div></>);diff --git a/components/view/viewer/image-viewer.tsx b/components/view/viewer/image-viewer.tsxindex b3d212c4..5534c104 100644--- a/components/view/viewer/image-viewer.tsx+++ b/components/view/viewer/image-viewer.tsx@@ -102,6 +102,7 @@ export default function ImageViewer({const [pageNumber, setPageNumber] = useState<number>(1); // start on first pageconst [scale, setScale] = useState<number>(1);+ const [isWindowFocused, setIsWindowFocused] = useState(true);const startTimeRef = useRef(Date.now());const visibilityRef = useRef<boolean>(true);@@ -211,6 +212,22 @@ export default function ImageViewer({};}, [pageNumber, numPages]);+ // Add this effect near your other useEffect hooks+ useEffect(() => {+ if (!screenshotProtectionEnabled) return;++ const handleFocus = () => setIsWindowFocused(true);+ const handleBlur = () => setIsWindowFocused(false);++ window.addEventListener("focus", handleFocus);+ window.addEventListener("blur", handleBlur);++ return () => {+ window.removeEventListener("focus", handleFocus);+ window.removeEventListener("blur", handleBlur);+ };+ }, [screenshotProtectionEnabled]);+useEffect(() => {// Remove token and email query parameters on component mountconst removeQueryParams = (queries: string[]) => {@@ -272,7 +289,12 @@ export default function ImageViewer({className="relative flex items-center"><div- className="relative flex h-full w-full flex-row"+ className={cn(+ "relative flex h-full w-full flex-row",+ !isWindowFocused &&+ screenshotProtectionEnabled &&+ "blur-xl transition-all duration-300",+ )}ref={containerRef}><div