You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 9c283a1
Browse filesBrowse the repository at this point in the historyBrowse files
feat(console): preview images, PDF, text and CSV in the page from attachments and file fields (#36587)
<!-- claude-projects-attribution: {"github_login":"os-steve"} -->
_Requested by **Steve** · [project
thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi92ncfFpPUpTD8314otX5oaG)_Fixes#36442
Clause-②: yes (widening)
Metadata-protocol: none
## 维护者速读
-
控制台用户在记录的附件列表点文件名、或在文件字段点文件链接时,图片、PDF、纯文本和表格文件直接在页面弹窗里看,不再先下载;Word、Excel、PPT
仍只能下载。
- 这是附件预览决策选定的方案甲(页内预览,社区版默认)的界面半边,服务端那半边由同区另一张卡负责。
- 若出错,最坏是预览打不开、用户点弹窗里的下载或新标签页照旧拿到文件;没有下载权限的人在预览里同样被拒;可整体撤回。
- 要你做的:无。
## What changes
A shared `FilePreviewDialog` in `@object-ui/fields` (decision #34781,
option A), opened from a record's attachments and from a file field:
- **Views, no new dependency.** Image (PNG, JPEG, GIF, WebP, and SVG
through an `img` element only) via the lightbox's own image element
(`LightboxImageView`, extracted from `ImageLightbox`); PDF in an
`iframe` on the browser's own viewer, without a `sandbox` attribute;
plain text as text; CSV as a table, read with the GBK-aware
`decodeSpreadsheetText` and `parseDelimited`, which moved down from
`@object-ui/plugin-grid` (it now imports them from `@object-ui/fields`;
no copy).
- **Download only.** Word, Excel, PowerPoint, HTML and every other type:
"This file type cannot be previewed. Please download it." HTML was
measured as a choice between "unsupported" and "source as text"; this PR
takes **unsupported**: nothing is fetched, nothing is framed, nothing is
drawn. A declared HTML type wins over a harmless name, and an `.html`
name wins over a declared plain-text type.
- **The PDF frame needs a PDF signal**: the declared `application/pdf`,
or a `.pdf` name when no type is declared. A `.pdf` name with another
declared type (for example `application/octet-stream`) is unsupported,
so the frame never receives bytes the browser would read as anything
else.
- **Limits** (maintainer defaults): images and PDF 20 MB, text and CSV 2
MB, judged on the declared size before anything is fetched and on the
bytes read when no size is declared; text and CSV show their first 1000
lines (CSV: header plus 1000 rows). Past a limit: "too large, please
download" with a download button.
- **Controls**: previous / next (and the arrow keys) across a record's
attachments; download; open in a new tab.
- **Same door as the download.** In the attachments panel the preview
mints its link with the download's own call (`/files/:id/url` with the
console's token, now one `signedUrlFor` both use), and a refusal shows
the download's own copy. On a file field the preview uses the field's
own link (the stable `/files/:id` endpoint) and first asks it with
`redirect: 'manual'`: a redirect is the grant, a 401/403 is the refusal,
so no byte moves twice.
- **Phone**: below the `useIsMobile` breakpoint a PDF is offered in a
new tab (the system viewer), never framed; image, text and CSV stay in
the page.
- **Cross-origin bucket** (the contract in #36443 / #36565): text and
CSV are read with a `GET` on the signed link from the page; when the
browser refuses that read (no bucket CORS rule for the console origin),
the dialog offers "Open in new tab" instead.
- **Wiring**: `RecordAttachmentsPanel`, the row's name is now a button
that opens the preview (download stays in the row's menu);
`FileValueAffordance` (`file-affordance.tsx`), a plain click opens the
preview, a modified click keeps the link's own new tab, and a click
inside the dialog does not bubble to the grid row behind its portal.
- **Copy**: 15 new keys under `fields.filePreview` in all 12 packs
(zh-Hant regenerated from zh-Hans by its generator).
New public exports from `@object-ui/fields` (preferred names, built
under them): `FilePreviewDialog`, `FilePreviewDialogProps`,
`FilePreviewItem`, `decodeSpreadsheetText`, `parseDelimited`.
Alternatives considered: `FileViewerDialog` / `FileViewerDialogProps` /
`FileViewerItem`.
New `@object-ui/i18n` keys (every pack): `fields.filePreview.title`,
`fields.filePreview.previous`, `fields.filePreview.next`,
`fields.filePreview.download`, `fields.filePreview.openInNewTab`,
`fields.filePreview.loading`, `fields.filePreview.tooLarge`,
`fields.filePreview.unsupported`, `fields.filePreview.denied`,
`fields.filePreview.loadFailed`, `fields.filePreview.blocked`,
`fields.filePreview.pdfOutside`, `fields.filePreview.truncatedRows`,
`fields.filePreview.truncatedLines`, `fields.filePreview.empty`.
Read first, by risk: `filePreviewKind` (which bytes may reach the
`iframe`), `useResolver` in `FilePreviewDialog.tsx` (the refusal probe
and the one-resolver-per-dialog rule), then `previewSignedUrl` /
`signedUrlFor` in `RecordAttachmentsPanel.tsx`.
Two-minute check: `pnpm --dir ui exec vitest run
packages/fields/src/widgets/FilePreviewDialog-36442.test.tsx` shows 51
passing.
## Tests
- New card-named file
`ui/packages/fields/src/widgets/FilePreviewDialog-36442.test.tsx`: the
kind table (Office, HTML, SVG, PDF-signal cases); each view and its
element; SVG only as `img`, no `iframe`/`object`, bytes never fetched;
text drawn as text (a script tag in the file stays text); GBK CSV; the
1000-row cap; Office and HTML download only with nothing resolved,
fetched or framed; too large per kind, a control at exactly the limit,
and a text file judged on its bytes; the refusal from `resolveUrl`, from
the default probe (403), from the text read (403), with a redirect as
the control; the CORS-blocked read offering the new tab; previous / next
/ arrow keys / wrap; download and new tab; phone PDF in a new tab and
phone image in the page; the field link opening the preview, a modified
click not, a refused field file, a Word field file.
- Shared harness `RecordAttachmentsPanel.test.tsx`, new top-level
`describe`: the name opens the preview on the signed link, next walks to
the next row's own link, a refused reader gets the download's copy (and
the link is asked once), a Word file offers only the download and that
download is the row menu's, the row menu still carries Download.
- Shared harness `importParsers.test.ts`, new `describe`: the wizard's
`parseDelimited` / `decodeSpreadsheetText` are the very functions
`@object-ui/fields` exports.
- Tests changed because the product changed: none. Existing pins that
query `getByText('report.pdf')` keep passing on the new name button.
## Acceptance notes
- **Multi-file field values preview one file at a time.** A file field
holding several files opens each file's own preview without previous /
next across the value: the group lives at the call sites in
`FileField.tsx` and the cell renderer in the fields barrel, outside this
card's claimed file surface. The attachments panel walks its whole list.
- "Phone" is the console's viewport breakpoint (`useIsMobile`, under 768
px), not a device check.
- #36518 (open): answers stored bytes outside an inline allow-list as
attachments, keeps PDF and plain text inline. Effect here: none on the
in-page views (the text and CSV views read bytes with `fetch`, which
ignores the disposition; #36565 measured Chromium mounting its PDF
viewer under that PR's sandbox header). The "Open in new tab" for a CSV
would download it rather than display it once #36518 lands.
- On S3, the PDF frame needs #36565's `frame-src` and the text and CSV
reads need the bucket CORS rule #36443 documents; without them the
dialog offers the new tab, as that card's contract states.
- The pre-existing `FILE_DOWNLOAD_DENIED` (field-owned files) has no
friendly copy in the attachments panel's mapper; the panel only maps
`ATTACHMENT_DOWNLOAD_DENIED`. The preview shows exactly what the
download shows. Observation only.
## Verification
Screenshots: NOT MEASURED. Reason: the relay writes text bodies only and
no write door here uploads an image to a pull request; a live Console
run was not started in this container.
<details><summary>Package suites on the merged head (ab1feda, then
c131bca and 81b19aa for the census fixes)</summary>
```
pnpm --dir ui exec vitest run packages/fields/ packages/plugin-grid/ packages/i18n/ (merged head 538f09f)
Test Files 1 failed | 735 passed | 1 skipped (737) the zh-Hant pack is generated: regenerated in ab1feda
pnpm --dir ui exec vitest run packages/i18n/ FilePreviewDialog-36442 RecordAttachmentsPanel.test importParsers.test (ab1feda)
Test Files 109 passed (109) Tests 2138 passed | 13 skipped (2151)
pnpm --dir ui exec vitest run scripts/ packages/plugin-grid/src/importParsers.test.ts (c131bca)
Test Files 173 passed | 2 skipped (175) Tests 5185 passed | 2 skipped (5187)
pnpm --dir ui exec vitest run dialogViewportGutterCensus-12603 clickableNonControlCensus-13461 FilePreviewDialog-36442 fileCellDownload-9485 RecordAttachmentsPanel.test (81b19aa)
Test Files 5 passed (5) Tests 129 passed (129)
pnpm --filter @object-ui/{fields,i18n,plugin-grid,app-shell} type-check TC-OK on all four
```
</details>
<details><summary>Ablation: delete the default probe's refusal (one line
in useResolver)</summary>
```
ablation-replace: ok mutation landed: anchor 1 -> 0, blob 1697ea6 -> 6bf8a8ebcffc
× by default, the stable storage endpoint is asked without following its redirect; a 403 is refused
× a field file the reader may not download gets the refusal in the preview
Tests 2 failed | 49 passed (51)
ablation-replace: ok restored: blob == HEAD (1697ea6) and `git diff HEAD` is empty
```
</details>
<details><summary>Derived gate list on 81b19aa: 42 of 42 run, all
exit 0</summary>
`node scripts/pm/dispatch-gates.mjs --repo objectstack-ai/objectstack
--ran FILE`: "42 derived famil(ies) accounted for — 42 run, 0
NOT-MEASURED". `check:type-check-debt` and the ui `scripts/` suite ran
under the shared verify lock. `node scripts/pm/overlap-check.mjs`:
VERDICT clean exit 0.
</details>
CI on 81b19aa: every required check green (Lint & Repo Gates,
TypeScript Type Check, Test Core, Governed Surface Queue Guard, Lint UI,
Type Check UI, Test UI, Build & E2E UI, Live E2E UI, Dogfood Regression
Gate; Build Core skipped by its selector).
Two shared censuses the first CI round named are now satisfied without
touching them: the dialog's width is `sm:max-w-[92vw] xl:max-w-5xl`, a
viewport-relative cap that keeps a margin at every width (the gutter
census counts it as not wide), and the dialog stops its own clicks at
its content rather than through a clickable wrapper element (the
clickable-non-control census).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01ADHkmRB34k9WUWeGpRnVNm
<!-- signed: repo:objectstack#2 · os-steve ·
session_01ADHkmRB34k9WUWeGpRnVNm -->
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: objectstack-fleet[bot] <332303061+objectstack-fleet[bot]@users.noreply.github.com>
feat(console): preview images, PDF, text and CSV in the page from a record's attachments and from a file field
9
+
10
+
Clause-②: yes (widening)
11
+
Metadata-protocol: none
12
+
13
+
A file opens in a dialog in the page instead of leaving it. In a record's attachments, the file's name opens the preview; the download stays in the row's menu. On a file field, a click on the file's link opens the preview; a modified click (new tab, new window) still opens the link as before.
14
+
15
+
-**Four views, no new dependency.** Images (PNG, JPEG, GIF, WebP) draw in an image element, and an SVG only ever through an image element, never inlined. A PDF draws in the browser's own viewer in a frame; on a phone it opens in a new tab, which hands it to the system viewer. Plain text draws as text. A CSV draws as a table, read with the same GBK-aware decoder the grid's import wizard uses.
16
+
-**Download only.** Word, Excel, PowerPoint, HTML and every other type show "This file type cannot be previewed. Please download it." with a download button. An HTML file is never framed and never drawn as markup.
17
+
-**Limits.** Images and PDF up to 20 MB; text and CSV up to 2 MB, showing their first 1000 rows or lines. Past a limit the dialog shows "This file is too large to preview. Please download it." with a download button.
18
+
-**Controls.** Previous and next (and the arrow keys) walk a record's attachments; download; open in a new tab.
19
+
-**Same door as the download.** The preview reads the same signed link through the same permission check as the download, so a reader the download refuses sees the same refusal in the preview. When the browser cannot read a text or CSV file from the page (a bucket on another origin without the CORS rule the preview needs), the dialog offers the new tab instead.
20
+
21
+
New exports from `@object-ui/fields`: `FilePreviewDialog` and its types `FilePreviewDialogProps` and `FilePreviewItem`; `decodeSpreadsheetText` and `parseDelimited`, the delimited-text reader that moved down from `@object-ui/plugin-grid` (which now imports them from `@object-ui/fields`; its own behaviour is unchanged).
22
+
23
+
New `@object-ui/i18n` keys in every pack, under `fields.filePreview`: `title`, `previous`, `next`, `download`, `openInNewTab`, `loading`, `tooLarge`, `unsupported`, `denied`, `loadFailed`, `blocked`, `pdfOutside`, `truncatedRows`, `truncatedLines`, `empty`.
0 commit comments