Skip to content

Build your personal knowledge base with Trilium Notes

feat(print/pdf): disable selection in preview

Elian Doranf68a481edcfb
diff --git a/apps/client/src/widgets/dialogs/print_preview.tsx b/apps/client/src/widgets/dialogs/print_preview.tsx
index bcc329b9d7..5862802204 100644
--- a/apps/client/src/widgets/dialogs/print_preview.tsx
+++ b/apps/client/src/widgets/dialogs/print_preview.tsx
@@ -310,7 +310,7 @@ export default function PrintPreviewDialog() {
<span class="bx bx-loader-circle bx-spin" style={{ fontSize: "2rem" }} />
</div>
)}
- {pdfUrl && <PdfViewer pdfUrl={pdfUrl} />}
+ {pdfUrl && <PdfViewer pdfUrl={pdfUrl} disableSelection />}
</div>
</Modal>
);
diff --git a/apps/client/src/widgets/type_widgets/file/PdfViewer.tsx b/apps/client/src/widgets/type_widgets/file/PdfViewer.tsx
index 0780a75d72..c072cb328c 100644
--- a/apps/client/src/widgets/type_widgets/file/PdfViewer.tsx
+++ b/apps/client/src/widgets/type_widgets/file/PdfViewer.tsx
@@ -22,16 +22,18 @@ interface PdfViewerProps extends Pick<HTMLAttributes<HTMLIFrameElement>, "tabInd
* If set, enables editable mode which includes persistence of user settings, annotations as well as specific features such as sending table of contents data for the sidebar.
*/
editable?: boolean;
+ /** If set, disables text selection in the rendered PDF. */
+ disableSelection?: boolean;
}
/**
* Reusable component displaying a PDF. The PDF needs to be provided via a URL.
*/
-export default function PdfViewer({ iframeRef: externalIframeRef, pdfUrl, onLoad, editable }: PdfViewerProps) {
+export default function PdfViewer({ iframeRef: externalIframeRef, pdfUrl, onLoad, editable, disableSelection }: PdfViewerProps) {
const iframeRef = useSyncedRef(externalIframeRef, null);
const [ locale ] = useTriliumOption("locale");
const [ newLayout ] = useTriliumOptionBool("newLayout");
- const injectStyles = useStyleInjection(iframeRef);
+ const injectStyles = useStyleInjection(iframeRef, disableSelection);
return (
<iframe
@@ -47,7 +49,7 @@ export default function PdfViewer({ iframeRef: externalIframeRef, pdfUrl, onLoad
);
}
-function useStyleInjection(iframeRef: RefObject<HTMLIFrameElement>) {
+function useStyleInjection(iframeRef: RefObject<HTMLIFrameElement>, disableSelection?: boolean) {
const styleRef = useRef<HTMLStyleElement | null>(null);
// First load.
@@ -65,7 +67,13 @@ function useStyleInjection(iframeRef: RefObject<HTMLIFrameElement>) {
fontStyles.textContent = FONTS.map(injectFont).join("\n");
doc.head.appendChild(fontStyles);
- }, [ iframeRef ]);
+ if (disableSelection) {
+ const selectionStyles = doc.createElement("style");
+ selectionStyles.textContent = `.textLayer, .textLayer * { user-select: none !important; cursor: default !important; }`;
+ doc.head.appendChild(selectionStyles);
+ }
+
+ }, [ iframeRef, disableSelection ]);
// React to changes.
useEffect(() => {