Skip to content

Build your personal knowledge base with Trilium Notes

feat(pdfjs): enable editing features only if in main editor

Elian Doran23f7dc63b88a
diff --git a/apps/client/src/widgets/type_widgets/file/Pdf.tsx b/apps/client/src/widgets/type_widgets/file/Pdf.tsx
index 60d6f20594..d7a09b35a4 100644
--- a/apps/client/src/widgets/type_widgets/file/Pdf.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Pdf.tsx
@@ -192,6 +192,7 @@ export default function PdfPreview({ note, blob, componentId, noteContext }: {
});
}
}}
+ editable
/>
);
}
diff --git a/apps/client/src/widgets/type_widgets/file/PdfViewer.tsx b/apps/client/src/widgets/type_widgets/file/PdfViewer.tsx
index 82bc4a2b9e..7e6870dd42 100644
--- a/apps/client/src/widgets/type_widgets/file/PdfViewer.tsx
+++ b/apps/client/src/widgets/type_widgets/file/PdfViewer.tsx
@@ -15,12 +15,16 @@ interface PdfViewerProps extends Pick<HTMLAttributes<HTMLIFrameElement>, "tabInd
/** Note: URLs are relative to /pdfjs/web. */
pdfUrl: string;
onLoad?(): void;
+ /**
+ * 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;
}
/**
* Reusable component displaying a PDF. The PDF needs to be provided via a URL.
*/
-export default function PdfViewer({ iframeRef: externalIframeRef, pdfUrl, onLoad }: PdfViewerProps) {
+export default function PdfViewer({ iframeRef: externalIframeRef, pdfUrl, onLoad, editable }: PdfViewerProps) {
const iframeRef = useSyncedRef(externalIframeRef, null);
const [ locale ] = useTriliumOption("locale");
const [ newLayout ] = useTriliumOptionBool("newLayout");
@@ -30,7 +34,7 @@ export default function PdfViewer({ iframeRef: externalIframeRef, pdfUrl, onLoad
<iframe
ref={iframeRef}
class="pdf-preview"
- src={`pdfjs/web/viewer.html?file=${pdfUrl}&lang=${locale}&sidebar=${newLayout ? "0" : "1"}`}
+ src={`pdfjs/web/viewer.html?file=${pdfUrl}&lang=${locale}&sidebar=${newLayout ? "0" : "1"}&editable=${editable ? "1" : "0"}`}
onLoad={() => {
injectStyles();
onLoad?.();
diff --git a/packages/pdfjs-viewer/src/custom.ts b/packages/pdfjs-viewer/src/custom.ts
index 8ec01752c0..2532c8d767 100644
--- a/packages/pdfjs-viewer/src/custom.ts
+++ b/packages/pdfjs-viewer/src/custom.ts
@@ -6,11 +6,14 @@ import { setupPdfLayers } from "./layers";
async function main() {
const urlParams = new URLSearchParams(window.location.search);
+ const isEditable = urlParams.get("editable") === "1";
if (urlParams.get("sidebar") === "0") {
hideSidebar();
}
- interceptPersistence(getCustomAppOptions(urlParams));
+ if (isEditable) {
+ interceptPersistence(getCustomAppOptions(urlParams));
+ }
// Wait for the PDF viewer application to be available.
while (!window.PDFViewerApplication) {
@@ -18,16 +21,18 @@ async function main() {
}
const app = window.PDFViewerApplication;
- app.eventBus.on("documentloaded", () => {
- manageSave();
- manageDownload();
- extractAndSendToc();
- setupScrollToHeading();
- setupActiveHeadingTracking();
- setupPdfPages();
- setupPdfAttachments();
- setupPdfLayers();
- });
+ if (isEditable) {
+ app.eventBus.on("documentloaded", () => {
+ manageSave();
+ manageDownload();
+ extractAndSendToc();
+ setupScrollToHeading();
+ setupActiveHeadingTracking();
+ setupPdfPages();
+ setupPdfAttachments();
+ setupPdfLayers();
+ });
+ }
await app.initializedPromise;
};