From 23cf3d2923c06b3565573b70693395bf288d6081 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Fri, 2 Jan 2026 11:40:32 +0200 Subject: [PATCH] feat(client/pdfjs): rewrite download button --- apps/client/src/components/app_context.ts | 3 ++- apps/client/src/services/open.ts | 21 ++++++++++++------- .../src/widgets/ribbon/FilePropertiesTab.tsx | 9 ++++++-- .../src/widgets/ribbon/ImagePropertiesTab.tsx | 2 +- .../src/widgets/ribbon/NoteActionsCustom.tsx | 4 ++-- .../src/widgets/type_widgets/file/Pdf.tsx | 9 +++++++- packages/pdfjs-viewer/src/custom.ts | 12 +++++++++++ 7 files changed, 46 insertions(+), 14 deletions(-) diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts index 004d1ef66..560a00438 100644 --- a/apps/client/src/components/app_context.ts +++ b/apps/client/src/components/app_context.ts @@ -382,7 +382,8 @@ export type CommandMappings = { reloadTextEditor: CommandData; chooseNoteType: CommandData & { callback: ChooseNoteTypeCallback - } + }; + customDownload: CommandData; }; type EventMappings = { diff --git a/apps/client/src/services/open.ts b/apps/client/src/services/open.ts index 1e5513c86..05f8a33a0 100644 --- a/apps/client/src/services/open.ts +++ b/apps/client/src/services/open.ts @@ -1,6 +1,8 @@ -import utils from "./utils.js"; +import Component from "../components/component.js"; +import FNote from "../entities/fnote.js"; import options from "./options.js"; import server from "./server.js"; +import utils from "./utils.js"; type ExecFunction = (command: string, cb: (err: string, stdout: string, stderror: string) => void) => void; @@ -36,9 +38,14 @@ function download(url: string) { } } -export function downloadFileNote(noteId: string) { - const url = `${getFileUrl("notes", noteId)}?${Date.now()}`; // don't use cache +export function downloadFileNote(note: FNote, parentComponent: Component | null, ntxId: string | null | undefined) { + if (note.type === "file" && note.mime === "application/pdf" && parentComponent) { + // Special handling, manages its own downloading process. + parentComponent.triggerEvent("customDownload", { ntxId }); + return; + } + const url = `${getFileUrl("notes", note.noteId)}?${Date.now()}`; // don't use cache download(url); } @@ -97,7 +104,7 @@ async function openCustom(type: string, entityId: string, mime: string) { // Note that the path separator must be \ instead of / filePath = filePath.replace(/\//g, "\\"); } - const command = `rundll32.exe shell32.dll,OpenAs_RunDLL ` + filePath; + const command = `rundll32.exe shell32.dll,OpenAs_RunDLL ${ filePath}`; exec(command, (err, stdout, stderr) => { if (err) { console.error("Open Note custom: ", err); @@ -131,10 +138,10 @@ export function getUrlForDownload(url: string) { if (utils.isElectron()) { // electron needs absolute URL, so we extract current host, port, protocol return `${getHost()}/${url}`; - } else { - // web server can be deployed on subdomain, so we need to use a relative path - return url; } + // web server can be deployed on subdomain, so we need to use a relative path + return url; + } function canOpenInBrowser(mime: string) { diff --git a/apps/client/src/widgets/ribbon/FilePropertiesTab.tsx b/apps/client/src/widgets/ribbon/FilePropertiesTab.tsx index d59b1c547..445c1740f 100644 --- a/apps/client/src/widgets/ribbon/FilePropertiesTab.tsx +++ b/apps/client/src/widgets/ribbon/FilePropertiesTab.tsx @@ -1,3 +1,5 @@ +import { useContext } from "preact/hooks"; + import FNote from "../../entities/fnote"; import { t } from "../../services/i18n"; import { downloadFileNote, openNoteExternally } from "../../services/open"; @@ -8,11 +10,14 @@ import { formatSize } from "../../services/utils"; import Button from "../react/Button"; import { FormFileUploadButton } from "../react/FormFileUpload"; import { useNoteBlob, useNoteLabel } from "../react/hooks"; +import { ParentComponent } from "../react/react_utils"; +import { TabContext } from "./ribbon-interface"; -export default function FilePropertiesTab({ note }: { note?: FNote | null }) { +export default function FilePropertiesTab({ note, ntxId }: TabContext) { const [ originalFileName ] = useNoteLabel(note, "originalFileName"); const canAccessProtectedNote = !note?.isProtected || protected_session_holder.isProtectedSessionAvailable(); const blob = useNoteBlob(note); + const parentComponent = useContext(ParentComponent); return (
@@ -40,7 +45,7 @@ export default function FilePropertiesTab({ note }: { note?: FNote | null }) { text={t("file_properties.download")} primary disabled={!canAccessProtectedNote} - onClick={() => downloadFileNote(note.noteId)} + onClick={() => downloadFileNote(note, parentComponent, ntxId)} />