GeezerVison is showing github.com — fix(web): PDFs in the desktop file panel can be saved again (#38749) · HNRow/r3code@e48d6f3
File treeExpand file treeCollapse file tree Open diff view settings apps/web/src/components/files
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/files/BrowserDocumentFrame.tsx +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,6 +28,7 @@ import { | 28 | 28 | } from "@t3tools/shared/keybindings"; | 29 | 29 | import { | 30 | 30 | lazy, | | 31 | + type ReactNode, | 31 | 32 | Suspense, | 32 | 33 | useEffect, | 33 | 34 | useEffectEvent, | @@ -79,7 +80,12 @@ export const isPdfPreviewFile = (path: string): boolean => | 79 | 80 | const PdfViewer = lazy(() => import("./PdfViewer")); | 80 | 81 |
| 81 | 82 | // The document stays mounted while each revision gets a fresh PDF.js document. | 82 | | -function PdfDocument(props: { src: string; title: string; documentKey: string }) { | | 83 | +function PdfDocument(props: { | | 84 | + src: string; | | 85 | + title: string; | | 86 | + documentKey: string; | | 87 | + actions?: ReactNode; | | 88 | +}) { | 83 | 89 | const [view, setView] = useState<PdfViewState>({ page: 0, offset: 0, zoom: null, left: 0 }); | 84 | 90 | return ( | 85 | 91 | <PdfDocumentRevision | @@ -89,6 +95,7 @@ function PdfDocument(props: { src: string; title: string; documentKey: string }) | 89 | 95 | positionKey={props.documentKey} | 90 | 96 | initialView={view} | 91 | 97 | onViewChange={setView} | | 98 | + actions={props.actions} | 92 | 99 | /> | 93 | 100 | ); | 94 | 101 | } | @@ -100,6 +107,7 @@ function PdfDocumentRevision(props: { | 100 | 107 | positionKey: string; | 101 | 108 | initialView: PdfViewState; | 102 | 109 | onViewChange: (view: PdfViewState) => void; | | 110 | + actions?: ReactNode; | 103 | 111 | }) { | 104 | 112 | const [src] = useState(props.src); | 105 | 113 | // The Electron shell does not enable PDF plugins. WebKit embeds on iOS | @@ -124,6 +132,7 @@ function PdfDocumentRevision(props: { | 124 | 132 | positionKey={props.positionKey} | 125 | 133 | initialView={props.initialView} | 126 | 134 | onViewChange={props.onViewChange} | | 135 | + actions={props.actions} | 127 | 136 | /> | 128 | 137 | </Suspense> | 129 | 138 | ); | @@ -143,6 +152,8 @@ export function BrowserDocumentFrame(props: { | 143 | 152 | readonly documentKey?: string | undefined; | 144 | 153 | /** Resource identity without its revision, so PDF reloads can retain the reading position. */ | 145 | 154 | readonly documentId?: string | undefined; | | 155 | + /** Toolbar actions for the in-app PDF viewer; Chromium's own viewer brings its own. */ | | 156 | + readonly pdfActions?: ReactNode; | 146 | 157 | }) { | 147 | 158 | const key = props.documentKey ?? props.src; | 148 | 159 | return props.pdf ? ( | @@ -151,6 +162,7 @@ export function BrowserDocumentFrame(props: { | 151 | 162 | documentKey={key} | 152 | 163 | src={props.src} | 153 | 164 | title={props.title} | | 165 | + actions={props.pdfActions} | 154 | 166 | /> | 155 | 167 | ) : props.htmlRender ? ( | 156 | 168 | <HtmlRenderDocument | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/files/FilePreviewPanel.tsx +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,7 @@ import { m, type MessageResolver } from "~/i18n/messages"; | 5 | 5 | import { namedClipboardTarget, useCopyToClipboard } from "~/hooks/useCopyToClipboard"; | 6 | 6 | import { Spinner } from "~/components/ui/spinner"; | 7 | 7 | import { | | 8 | + type AssetResource, | 8 | 9 | AuthPreviewOperateScope, | 9 | 10 | type EditorId, | 10 | 11 | type EnvironmentId, | @@ -296,10 +297,59 @@ export function WorkspaceBrowserPreview(props: { | 296 | 297 | documentId={`${props.environmentId}:${resource._tag}:${props.threadRef.threadId}:${props.absolutePath}`} | 297 | 298 | title={props.title} | 298 | 299 | pdf={pdf} | | 300 | + pdfActions={ | | 301 | + <WorkspacePdfSaveAction | | 302 | + environmentId={props.environmentId} | | 303 | + resource={resource} | | 304 | + name={props.title} | | 305 | + /> | | 306 | + } | 299 | 307 | /> | 300 | 308 | ); | 301 | 309 | } | 302 | 310 |
| | 311 | +function WorkspacePdfSaveAction(props: { | | 312 | + readonly environmentId: EnvironmentId; | | 313 | + readonly resource: AssetResource; | | 314 | + readonly name: string; | | 315 | +}) { | | 316 | + const gt = useGT(); | | 317 | + const [saving, setSaving] = useState(false); | | 318 | + const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, props.resource); | | 319 | + return ( | | 320 | + <FileSurfaceAction | | 321 | + label={saving ? gt("Preparing file…") : gt("Save file")} | | 322 | + disabled={saving} | | 323 | + onPress={() => { | | 324 | + setSaving(true); | | 325 | + void (async () => { | | 326 | + const url = await refreshAssetUrl(); | | 327 | + if (!url) throw new Error(gt("Reconnect to the environment and try again.")); | | 328 | + const response = await fetch(url); | | 329 | + if (!response.ok) throw new Error(gt("The file could not be loaded. Try again.")); | | 330 | + // Blob downloads keep desktop's custom app scheme from navigating to an external browser. | | 331 | + const downloadUrl = URL.createObjectURL(await response.blob()); | | 332 | + setTimeout(() => URL.revokeObjectURL(downloadUrl), 30_000); | | 333 | + const anchor = document.createElement("a"); | | 334 | + anchor.href = downloadUrl; | | 335 | + anchor.download = props.name.split(/[\\/]/).at(-1) || props.name; | | 336 | + anchor.click(); | | 337 | + })() | | 338 | + .catch((cause: unknown) => | | 339 | + toastManager.add({ | | 340 | + type: "error", | | 341 | + title: gt("Could not save file"), | | 342 | + description: thrownErrorMessage(cause, m) ?? gt("Please try again."), | | 343 | + }), | | 344 | + ) | | 345 | + .finally(() => setSaving(false)); | | 346 | + }} | | 347 | + > | | 348 | + <DownloadIcon className="size-3.5" /> | | 349 | + </FileSurfaceAction> | | 350 | + ); | | 351 | +} | | 352 | + | 303 | 353 | function WorkspaceVideoPreview(props: { | 304 | 354 | readonly environmentId: EnvironmentId; | 305 | 355 | readonly threadRef: ScopedThreadRef; | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/files/PdfViewer.tsx +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | useRef, | 7 | 7 | useState, | 8 | 8 | type CSSProperties, | | 9 | + type ReactNode, | 9 | 10 | } from "react"; | 10 | 11 | import { TextLayer, type PDFDocumentProxy } from "pdfjs-dist"; | 11 | 12 | import { msg, useGT } from "gt-react"; | @@ -144,6 +145,7 @@ export default function PdfViewer(props: { | 144 | 145 | positionKey?: string; | 145 | 146 | initialView?: PdfViewState; | 146 | 147 | onViewChange?: (view: PdfViewState) => void; | | 148 | + actions?: ReactNode; | 147 | 149 | }) { | 148 | 150 | const gt = useGT(); | 149 | 151 | const [savedPosition] = useState(() => | @@ -448,6 +450,7 @@ export default function PdfViewer(props: { | 448 | 450 | </FileSurfaceAction> | 449 | 451 | </> | 450 | 452 | ) : null} | | 453 | + {props.actions} | 451 | 454 | </div> | 452 | 455 | <div | 453 | 456 | ref={scroll} | |
|