Skip to content

feat(chat): copy and download images from the image itself - #1918

Closed
zszz3 wants to merge 3 commits into
vastsa:mainfrom
zszz3:fix/copy-chat-images
Closed

zszz3 wants to merge 3 commits into
vastsa:mainfrom
zszz3:fix/copy-chat-images

Conversation

@zszz3

@zszz3 zszz3 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

问题

修复 #1917:回复里的图片没法直接复制。点击 Markdown 本地图片只会打开文件管理器,右键也只有复制路径;生成图片的卡片和查看器同样没有复制操作。

改动

  • Markdown 本地图片:图片加载出来后,右键菜单最上面加「复制图片」「下载图片」(下载时用原文件名),下面用分隔线隔开,接着是原来的文件操作。点击行为不变。
  • 生成图片卡片:加复制按钮,右键菜单里有「复制图片 / 下载图片」。
  • 生成图片查看器:加复制按钮,支持右键菜单和 Cmd/Ctrl+C(没有选中文字时才生效)。查看器是模态的 <dialog>,挂在 body 上的菜单和 toast 会被它挡住,所以菜单渲染进 dialog 内部(ContextMenu 新增可选的 container 参数),复制结果直接在查看器里提示。
  • 复制实现:apps/desktop/src/lib/image-clipboard.ts,PNG 原样写入;JPEG/WebP 先转成 PNG,再通过 navigator.clipboard.write + ClipboardItem 写入剪贴板。主进程的权限处理本来就允许 clipboard 写入,没有改 IPC。
  • i18n:新增 settings.copyGeneratedImage、settings.downloadImage,9 种语言都有。
  • 文档:21-image-generation.md、08-component-spec.md、E2E-IMAGES-result-download、E2E-187。

截图

右键菜单

验证

  • pnpm --filter @pi-desktop/desktop typecheck 通过
  • 桌面端全量 node --test test/*.test.mjs:4434 个测试,0 失败(新增 image-clipboard.test.mjs、generated-image-context-menu.test.mjs,并在 chat-file-open-with-menu.test.mjs 中补了断言)
  • i18n 测试 31/31,check-architecture、style tokens、git diff --check 均通过
  • 在 macOS 开发版里手动验证了 Markdown 图片的右键复制和下载(见截图)
  • 剪贴板的 E2E 没有跑:写入系统剪贴板这一步只做了手动验证

fixes #1917

Images in a chat answer could not be copied. A local markdown image
only opened the file manager, and generated results offered download
and reveal but no copy, so getting a picture into another app meant
saving it and finding the file first.

Loaded images now carry Copy image and Download image, reached from a
right-click menu on markdown images and on generated result cards and
the viewer, plus a copy button and Cmd/Ctrl+C in the viewer. Copy
writes PNG bytes (JPEG/WebP are re-encoded) through the async
clipboard API. The viewer is a modal dialog, so its menu portals into
the dialog and reports copy inline instead of through a hidden toast.

@muzimu217 muzimu217 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified the two claims in the description that could have failed silently.

No IPC change is needed, and the reason is documented. installLiveMicrophonePermissionHandlers installs the request/check handlers on session.defaultSession (electron/main/index.ts:1027-1033), which is the session the main renderer uses. Its non-media branch grants everything (microphone-permissions.ts:50-58), and the comment there says why: installing a handler replaces the platform default for every permission on that session, and denying the rest "silently broke the app's own copy buttons, which need clipboard-sanitized-write". So navigator.clipboard.write works today, and this PR would break the moment that policy narrowed — worth remembering, because the failure mode is a silent no-op rather than an exception at the call site.

The portal target is handled correctly. portalToContainer wraps the node in visiblePortalContent before createPortal (components/ContextMenu.tsx), matching what portalToBody does (lib/portal-visibility.tsx:20-28), so the visibility context survives when the menu renders inside the <dialog> instead of body. A bare createPortal would have lost it.

PNG passthrough before any conversion is the right call — Chromium's async clipboard only accepts image/png, and re-encoding a PNG would be a lossy-looking step for no reason. The test pins it byte-for-byte, and the JPEG/WebP re-encode path is covered separately.

Two small things, neither blocking:

  • SUPPORTED_IMAGE_DATA_URL accepts only png|jpeg|webp. image/jpg is a common alias and would fail the match, so a copy from such a source reports "copy failed" instead of working. Normalizing the subtype before the match (or accepting jpe?g) costs nothing.
  • downloadMarkdownImage derives the file name with reference.split(/[\\/]/).pop(), which keeps a query string or fragment when the reference is a URL (photo.png?w=800). decodeURIComponent plus stripping [?#] before the split would give the same name the browser would.

zszz3 added 2 commits October 28, 2026 01:00
Sources often label JPEG data as image/jpg, which the clipboard helper
rejected and then reported as a copy failure. Downloading an image shown
from a URL also kept its query or fragment in the saved file name
(photo.png?w=800). Local paths keep their last segment verbatim, since
`#` and `%` are legal there.
@zszz3

zszz3 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Both points are fixed in fae60ff:

  • image/jpg is now accepted and normalized to image/jpeg before the Blob is built, so the copy path treats it as an ordinary JPEG and re-encodes it to PNG. Covered by a new case in test/image-clipboard.test.mjs.
  • The download name now comes from a small lib/image-download-name.ts. For a URL it uses new URL(...).pathname, takes the last segment, and decodes it, so photo.png?w=800 saves as photo.png and %20 becomes a space. A local path keeps its last segment verbatim, because # and % are legal in file names there (shot #1.png, 100%.png). Tests are in test/image-download-name.test.mjs.

Noted on the clipboard-sanitized-write dependency. If that permission handler is ever narrowed, this copy path fails silently, which is worth keeping in mind.

I also merged the latest main (non-destructively, no force-push) so the base check stays green. On the merged result: desktop typecheck ok, desktop suite 4444 tests / 0 fail, architecture check ok.

@zszz3

zszz3 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

After the merge with the latest main, everything passes except Trusted extensions Electron E2E. It fails at the same place #1921 hit: scripts/e2e-provider-client-certificate.mjs hangs and is killed by SIGKILL after 65s. That job passed on the previous run of this PR, and #1921 passes it on this round, so this looks like the same infrastructure flake rather than something in this change. I don't have permission to re-run jobs; could a maintainer re-run it? Thanks.

vastsa commented Oct 28, 2026

Copy link
Copy Markdown
Owner

这个问题描述的是新增能力:当前点击打开本地图片、右键复制文件路径,与现有交互一致;PR 想新增把图片内容写入剪贴板的操作。我没有找到既有产品约定或回归证据能证明这是代码缺陷。谢谢你补齐复制、下载和 MIME/文件名边界;但按当前仓库的外部 PR 范围,这项新能力属于 feat,不能作为 fix 合入。后续限制解除后可按 feature 重新提交。

@vastsa vastsa closed this Oct 28, 2026
@zszz3 zszz3 changed the title fix(chat): copy and download images from the image itself feat(chat): copy and download images from the image itself Oct 28, 2026
zszz3 added a commit to zszz3/PI-Desktop that referenced this pull request Nov 6, 2026
Images in a reply could only be opened or have their path copied, so
getting the picture itself into another app meant a detour through the
file manager. Generated images and Markdown images now offer Copy image
and Download image on right-click; the generated-image viewer also has a
copy button and Cmd/Ctrl+C.

Copy writes PNG to the clipboard (JPEG, JPG and WebP are re-encoded).
Download names come from the URL path without its query or fragment;
local paths keep their file name verbatim.

Refs vastsa#1917, supersedes vastsa#1918.
Tioit-Wang pushed a commit that referenced this pull request Dec 1, 2026
Images in a reply could only be opened or have their path copied, so
getting the picture itself into another app meant a detour through the
file manager. Generated images and Markdown images now offer Copy image
and Download image on right-click; the generated-image viewer also has a
copy button and Cmd/Ctrl+C.

Copy writes PNG to the clipboard (JPEG, JPG and WebP are re-encoded).
Download names come from the URL path without its query or fragment;
local paths keep their file name verbatim.

Refs #1917, supersedes #1918.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] 支持复制/下载聊天中展示的图片

3 participants