Skip to content

Build your personal knowledge base with Trilium Notes

refactor(client/pdf): split into own component

Elian Doranf09a3e06f4dc
diff --git a/apps/client/src/widgets/type_widgets/File.tsx b/apps/client/src/widgets/type_widgets/File.tsx
index f1d0de6fae..1d69e3b71d 100644
--- a/apps/client/src/widgets/type_widgets/File.tsx
+++ b/apps/client/src/widgets/type_widgets/File.tsx
@@ -1,13 +1,11 @@
import "./File.css";
-import { useEffect } from "preact/hooks";
-
import FNote from "../../entities/fnote";
import { t } from "../../services/i18n";
import { getUrlForDownload } from "../../services/open";
-import server from "../../services/server";
import Alert from "../react/Alert";
import { useNoteBlob } from "../react/hooks";
+import PdfPreview from "./file/Pdf";
import { TypeWidgetProps } from "./type_widget";
const TEXT_MAX_NUM_CHARS = 5000;
@@ -44,28 +42,6 @@ function TextPreview({ content }: { content: string }) {
);
}
-function PdfPreview({ note }: { note: FNote }) {
- useEffect(() => {
- function handleMessage(event: MessageEvent) {
- if (event.data?.type === "pdfjs-viewer-document-modified" && event.data?.data) {
- const blob = new Blob([event.data.data], { type: note.mime });
- server.upload(`notes/${note.noteId}/file`, new File([blob], note.title, { type: note.mime }));
- }
- }
-
- window.addEventListener("message", handleMessage);
- return () => {
- window.removeEventListener("message", handleMessage);
- };
- }, [ note ]);
-
- return (
- <iframe
- class="pdf-preview"
- src={`pdfjs/web/viewer.html?file=../../api/notes/${note.noteId}/open`} />
- );
-}
-
function VideoPreview({ note }: { note: FNote }) {
return (
<video
diff --git a/apps/client/src/widgets/type_widgets/file/Pdf.tsx b/apps/client/src/widgets/type_widgets/file/Pdf.tsx
new file mode 100644
index 0000000000..8c503a2b00
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/file/Pdf.tsx
@@ -0,0 +1,26 @@
+import { useEffect } from "preact/hooks";
+
+import FNote from "../../../entities/fnote";
+import server from "../../../services/server";
+
+export default function PdfPreview({ note }: { note: FNote }) {
+ useEffect(() => {
+ function handleMessage(event: MessageEvent) {
+ if (event.data?.type === "pdfjs-viewer-document-modified" && event.data?.data) {
+ const blob = new Blob([event.data.data], { type: note.mime });
+ server.upload(`notes/${note.noteId}/file`, new File([blob], note.title, { type: note.mime }));
+ }
+ }
+
+ window.addEventListener("message", handleMessage);
+ return () => {
+ window.removeEventListener("message", handleMessage);
+ };
+ }, [ note ]);
+
+ return (
+ <iframe
+ class="pdf-preview"
+ src={`pdfjs/web/viewer.html?file=../../api/notes/${note.noteId}/open`} />
+ );
+}