From 54eed06c6f96fe3dcc91607056586e786a6f4f49 Mon Sep 17 00:00:00 2001 From: zhc Date: Mon, 19 Oct 2026 14:15:21 +0800 Subject: [PATCH 1/3] feat(gui): support image panning and original-file drag sharing --- apps/desktop/README.i18n.yaml | 4 +- apps/desktop/README.md | 2 + apps/desktop/README.zh.md | 2 + apps/desktop/src/image-drag-files.ts | 96 ++++++++++++++ apps/desktop/src/image-drag.ts | 74 +++++++++++ apps/desktop/src/ipc.ts | 8 ++ apps/desktop/src/main.ts | 14 ++- apps/desktop/src/preload-app.ts | 7 ++ apps/desktop/tests/image-drag-files.spec.ts | 48 +++++++ apps/desktop/tests/image-drag.spec.ts | 88 +++++++++++++ apps/desktop/tests/main-startup.spec.ts | 4 +- apps/desktop/tests/preload-app.spec.ts | 16 +++ apps/web/tests/document-preview.e2e.ts | 23 +++- apps/web/tests/markdown-images.e2e.ts | 9 ++ .../client/ui-attachment/README.i18n.yaml | 4 +- packages/client/ui-attachment/README.md | 2 + packages/client/ui-attachment/README.zh.md | 2 + .../client/ui-attachment/src/MessageImage.tsx | 8 +- .../src/client/ComposerAttachments.tsx | 7 +- .../ui-attachment/src/client/drop-events.ts | 8 +- .../client/ui-attachment/src/client/labels.ts | 1 + .../tests/message-image.client.spec.tsx | 1 + .../ui-chat/src/client/chat/ChatView.tsx | 2 +- packages/client/ui-chat/src/client/locale.ts | 2 + .../src/client/contract/slots.ts | 2 + .../ui-conversation/src/client/locales.ts | 2 + packages/client/ui-layout/package.json | 3 +- .../client/ui-layout/src/client/AppFrame.tsx | 5 +- .../client/ui-primitives/README.i18n.yaml | 4 +- packages/client/ui-primitives/README.md | 2 + packages/client/ui-primitives/README.zh.md | 2 + .../ui-primitives/src/ImageDragProvider.tsx | 60 +++++++++ .../client/ui-primitives/src/image-drag.ts | 110 ++++++++++++++++ packages/client/ui-primitives/src/index.ts | 3 + .../src/markdown/MarkdownDelegate.tsx | 2 +- .../ui-primitives/src/markdown/render.tsx | 6 +- .../tests/image-drag.client.spec.tsx | 119 ++++++++++++++++++ .../markdown-image-entries.client.spec.tsx | 2 +- .../README.i18n.yaml | 4 +- .../ui-sidebar-documentpreview/README.md | 2 +- .../ui-sidebar-documentpreview/README.zh.md | 2 +- .../src/client/image/ImageBody.module.css | 3 + .../src/client/image/ImageBody.tsx | 25 +++- .../src/client/image/locales.ts | 2 + .../src/client/image/pan.ts | 78 ++++++++++++ .../tests/image-body.client.spec.tsx | 4 +- .../tests/image-pan.client.spec.ts | 84 +++++++++++++ .../src/client/slot-catalog.ts | 54 ++++---- pnpm-lock.yaml | 3 + .../web/document-preview/document.expected.md | 2 + 50 files changed, 954 insertions(+), 63 deletions(-) create mode 100644 apps/desktop/src/image-drag-files.ts create mode 100644 apps/desktop/src/image-drag.ts create mode 100644 apps/desktop/tests/image-drag-files.spec.ts create mode 100644 apps/desktop/tests/image-drag.spec.ts create mode 100644 packages/client/ui-primitives/src/ImageDragProvider.tsx create mode 100644 packages/client/ui-primitives/src/image-drag.ts create mode 100644 packages/client/ui-primitives/tests/image-drag.client.spec.tsx create mode 100644 packages/client/ui-sidebar-documentpreview/src/client/image/pan.ts create mode 100644 packages/client/ui-sidebar-documentpreview/tests/image-pan.client.spec.ts diff --git a/apps/desktop/README.i18n.yaml b/apps/desktop/README.i18n.yaml index a46786a046..89ac4aea9f 100644 --- a/apps/desktop/README.i18n.yaml +++ b/apps/desktop/README.i18n.yaml @@ -33,8 +33,8 @@ en: 372297005b57a08b zh: 074a635ac17e4362 /deepseek-harness-desktop/installation-ownership: - en: 22153a298a6662e1 - zh: 3600803c8c9d0274 + en: fffd3892a309aeae + zh: 4127b7f511c70e75 /deepseek-harness-desktop/installation-ownership/runtime-and-plugin-activation: en: a1a47b09f4a72ba4 zh: 7b1cf27f0b72cf31 diff --git a/apps/desktop/README.md b/apps/desktop/README.md index 14bfd38f14..34f0946ec2 100644 --- a/apps/desktop/README.md +++ b/apps/desktop/README.md @@ -142,6 +142,8 @@ Electron owns `$DSH_HOME/profiles/desktop`. Its `dependencies` contains packages The application preload exposes boot readiness, fatal startup reporting, native directory selection, the `__DSH_HOST_PATHS__` bridge for composer path references, and the lease-scoped Browser bridge only to `dsh-app://app` documents. The same preload forwards the main-process machine description from `dshDesktop.deviceInfo()` as `name=value` fields separated by `; `: `platform`, `os`, `app_arch` (the architecture the application binary runs as, which differs from the hardware architecture under emulation), `cpu`, and `memory_gib` (total physical memory in GiB with one decimal). A field is omitted when its source is unavailable. The description carries no hostname, user name, or serial number. Product documents use the shared authenticated HTTP APIs and receive the Desktop marker, update presentation, and an action that opens native confirmation; they cannot choose artifacts or authorize installation. Plugin management uses the Web application's authenticated HTTP APIs. Electron serves update-dialog documents and assets locally at `dsh-app://shell/`, independently of Host readiness. Electron exposes no plugin-management IPC or separate management document. No renderer receives filesystem access, raw Electron IPC, a shell, or arbitrary pnpm arguments. +The image-drag bridge accepts original PNG, JPEG, GIF, WebP, BMP, ICO, or SVG bytes up to 64 MiB from the owned application main frame. It prepares a private temporary file and icon before `startDrag`, exposes only an opaque handle, and never accepts renderer-supplied filesystem paths. Unused preparations are released with their source image; files handed to the OS remain readable until application exit because receivers may read them after the drag returns. Shutdown removes the temporary files. Native drag errors are reported once by the application’s localized image feedback. + Only the main application window enables ``. Guest attachment must match a main-issued lease and partition; guests keep sandbox, context isolation and Web security without Node integration or guest preload. Browser IPC listeners are created only for the application document. [Sidebar Browser](../../packages/client/ui-sidebar-browser/README.md) describes storage grouping and guest limitations; Host authentication remains required independently of URL filtering. The `dsh-app://shell/` origin serves packaged update documents, scripts, and styles without contacting the Host. Static requests retain GET/HEAD, path-containment, and MIME handling; each update document keeps its isolated preload and owned-window IPC checks. diff --git a/apps/desktop/README.zh.md b/apps/desktop/README.zh.md index 64cc5f7b22..7f26bd720a 100644 --- a/apps/desktop/README.zh.md +++ b/apps/desktop/README.zh.md @@ -142,6 +142,8 @@ Electron 拥有 `$DSH_HOME/profiles/desktop`。其 `dependencies` 包含 pnpm 应用 preload 只向 `dsh-app://app` 文档暴露启动就绪、致命启动失败上报、原生目录选择、用于 composer 路径引用的 `__DSH_HOST_PATHS__` 桥接和租约范围内的 Browser 桥接。同一个 preload 通过 `dshDesktop.deviceInfo()` 转发主进程采集的机器描述,按 `name=value` 字段以 `; ` 分隔:`platform`、`os`、`app_arch`(应用二进制实际运行的架构,模拟运行时与硬件架构不同)、`cpu` 和 `memory_gib`(物理内存总量,GiB,保留一位小数)。取值不可用时省略对应字段。该描述不包含主机名、用户名或序列号。产品页面还获得 Desktop 标记、更新展示数据和打开原生确认的操作,不能选择安装产物或授权安装。插件管理使用 Web 应用经过认证的 HTTP API;Electron 在 `dsh-app://shell/` 本地提供更新弹窗文档和资源,不依赖 Host 就绪。Electron 不提供插件管理 IPC 或独立管理页面。任何渲染进程都不会获得文件系统访问、原始 Electron IPC、shell 或任意 pnpm 参数。 +图片拖放桥接只接受主应用所属主框架传入的 PNG、JPEG、GIF、WebP、BMP、ICO 或 SVG 原图字节,上限为 64 MiB。桥接在 `startDrag` 前准备私有临时文件和图标,只暴露不透明句柄,绝不接受渲染进程指定的文件系统路径。未使用的准备结果随来源图片释放;已交给操作系统的文件保留到应用退出,因为接收方可能在拖动返回后继续读取。退出时删除临时文件。原生拖放错误由应用的本地化图片反馈统一报告一次。 + 只有主应用窗口启用 ``。guest 挂载必须匹配主进程签发的租约和分区;guest 保持 sandbox、context isolation 和 Web security,不启用 Node integration 或 guest preload。Browser IPC 监听只为应用文档创建。[Sidebar Browser](../../packages/client/ui-sidebar-browser/README.zh.md) 说明存储分组和 guest 限制;Host 鉴权仍独立于 URL 过滤而必需。 `dsh-app://shell/` 无需联系 Host 即可提供打包的更新文档、脚本和样式。静态请求保留 GET/HEAD、路径范围和 MIME 处理;每个更新文档继续使用隔离 preload 和所属窗口的 IPC 校验。 diff --git a/apps/desktop/src/image-drag-files.ts b/apps/desktop/src/image-drag-files.ts new file mode 100644 index 0000000000..df01eeaad2 --- /dev/null +++ b/apps/desktop/src/image-drag-files.ts @@ -0,0 +1,96 @@ +/** Process-owned original image files staged for native copy drags. */ +import { randomUUID } from 'node:crypto' +import { mkdtemp, rm, writeFile } from 'node:fs/promises' +import { join } from 'node:path' + +const IMAGE_EXTENSIONS: Readonly> = { + 'image/png': 'png', 'image/jpeg': 'jpg', 'image/gif': 'gif', 'image/webp': 'webp', + 'image/bmp': 'bmp', 'image/x-icon': 'ico', 'image/vnd.microsoft.icon': 'ico', 'image/svg+xml': 'svg', +} +// Limits the memory and temporary storage accepted from one renderer IPC request. +const MAX_IMAGE_BYTES = 64 * 1024 * 1024 + +interface ImageDragFile { + readonly directory: string + readonly path: string + used: boolean +} + +/** Original files belong to the application; handed-off files remain readable until app shutdown. */ +export class ImageDragFiles { + private readonly files = new Map() + private readonly pending = new Set>() + private disposed = false + + /** @param temporaryDirectory - OS temporary directory in which private export directories are allocated. */ + constructor(private readonly temporaryDirectory: string) {} + + /** + * Validate renderer bytes and stage one original file without accepting renderer filesystem paths. + * @param bytes - complete image bytes. + * @param name - suggested filename; directory characters and platform-reserved characters are removed. + * @param mediaType - supported image MIME type, which owns the output suffix. + * @returns application-local opaque handle; failures retain no usable handle or partial file. + */ + prepare(bytes: unknown, name: unknown, mediaType: unknown): Promise { + const extension = typeof mediaType === 'string' && Object.hasOwn(IMAGE_EXTENSIONS, mediaType) + ? IMAGE_EXTENSIONS[mediaType] : undefined + if (this.disposed || !(bytes instanceof Uint8Array) || bytes.byteLength === 0 || bytes.byteLength > MAX_IMAGE_BYTES + || typeof name !== 'string' || typeof mediaType !== 'string' || extension === undefined) { + return Promise.reject(new Error('Invalid image transfer')) + } + const stem = name.replace(/\.[^.]*$/u, '').replace(/[<>:"/\\|?*\x00-\x1f]/gu, '_').replace(/^[. ]+|[. ]+$/gu, '').slice(0, 120) || 'image' + const operation = this.write(bytes, `${stem}.${extension}`) + this.pending.add(operation) + void operation.then(() => { this.pending.delete(operation) }, () => { this.pending.delete(operation) }) + return operation + } + + private async write(bytes: Uint8Array, name: string): Promise { + const directory = await mkdtemp(join(this.temporaryDirectory, 'dsh-image-drag-')) + try { + const path = join(directory, name) + await writeFile(path, bytes, { flag: 'wx', mode: 0o600 }) + if (this.disposed) throw new Error('Image transfer closed') + const handle = randomUUID() + this.files.set(handle, { directory, path, used: false }) + return handle + } catch (error) { + await rm(directory, { recursive: true, force: true }) + throw error + } + } + + /** + * Resolve and retain a prepared file for an OS-owned drag. + * @param handle - handle created by this application's preparation operation. + * @param retain - whether the OS is taking ownership; false only during preparation. + * @returns original file path, or undefined after release/disposal or for a foreign handle. + */ + take(handle: unknown, retain = true): string | undefined { + const file = typeof handle === 'string' ? this.files.get(handle) : undefined + if (file === undefined || this.disposed) return undefined + if (retain) file.used = true + return file.path + } + + /** + * Release a preparation that no longer has a mounted image consumer. + * @param handle - application-local handle; OS-used files stay until disposal. + */ + async release(handle: unknown): Promise { + const file = typeof handle === 'string' ? this.files.get(handle) : undefined + if (file === undefined || file.used) return + this.files.delete(handle as string) + await rm(file.directory, { recursive: true, force: true }) + } + + /** Stop preparation, await pending writes, and remove all owned files after native drags finish. */ + async dispose(): Promise { + this.disposed = true + await Promise.allSettled(this.pending) + const files = [...this.files.values()] + this.files.clear() + await Promise.all(files.map(file => rm(file.directory, { recursive: true, force: true }))) + } +} diff --git a/apps/desktop/src/image-drag.ts b/apps/desktop/src/image-drag.ts new file mode 100644 index 0000000000..da549f1108 --- /dev/null +++ b/apps/desktop/src/image-drag.ts @@ -0,0 +1,74 @@ +/** Native copy drags authorized only for the owning application window. */ +import { app, ipcMain, nativeImage, type BrowserWindow, type IpcMainInvokeEvent, type NativeImage } from 'electron' +import { DESKTOP_IPC, assertDesktopSender } from './ipc.ts' +import { ImageDragFiles } from './image-drag-files.ts' + +/** + * Register native image preparation and drag operations for the application window. + * @param getWindow - current application window; subframes and other windows are rejected. + * @returns cleanup that waits for preparation and removes all exported temporary files. + */ +export function installDesktopImageDrag(getWindow: () => BrowserWindow | undefined): () => Promise { + let disposed = false + const files = new ImageDragFiles(app.getPath('temp')) + const pending = new Set>() + const icons = new Map() + const owner = (event: IpcMainInvokeEvent): BrowserWindow => { + const window = getWindow() + if (disposed || window === undefined || window.isDestroyed() || event.sender !== window.webContents + || event.senderFrame !== window.webContents.mainFrame) throw new Error('Unowned image drag request') + assertDesktopSender(event, ['app']) + return window + } + const prepare = async (event: IpcMainInvokeEvent, bytes: unknown, name: unknown, mediaType: unknown): Promise => { + owner(event) + const handle = await files.prepare(bytes, name, mediaType) + try { + const file = files.take(handle, false) + if (file === undefined) throw new Error('Image transfer closed') + let icon = nativeImage.createFromPath(file) + if (icon.isEmpty()) icon = await app.getFileIcon(file, { size: 'normal' }) + else icon = icon.resize({ width: 64 }) + owner(event) + icons.set(handle, icon) + return handle + } catch (error) { + await files.release(handle) + throw error + } + } + ipcMain.handle(DESKTOP_IPC.imageDragPrepare, (event, bytes: unknown, name: unknown, mediaType: unknown) => { + const operation = prepare(event, bytes, name, mediaType) + pending.add(operation) + void operation.then(() => { pending.delete(operation) }, () => { pending.delete(operation) }) + return operation + }) + ipcMain.handle(DESKTOP_IPC.imageDragStart, (event, handle: unknown) => { + const window = owner(event) + const icon = typeof handle === 'string' ? icons.get(handle) : undefined + if (icon === undefined) return false + const file = files.take(handle) + if (file === undefined) return false + try { + window.webContents.startDrag({ file, icon }) + return true + } catch (_error) { + // The renderer reports one localized failure; no filesystem paths cross IPC. + return false + } + }) + ipcMain.handle(DESKTOP_IPC.imageDragRelease, async (event, handle: unknown) => { + owner(event) + if (typeof handle === 'string') icons.delete(handle) + await files.release(handle) + }) + return async () => { + disposed = true + ipcMain.removeHandler(DESKTOP_IPC.imageDragPrepare) + ipcMain.removeHandler(DESKTOP_IPC.imageDragStart) + ipcMain.removeHandler(DESKTOP_IPC.imageDragRelease) + await Promise.allSettled(pending) + icons.clear() + await files.dispose() + } +} diff --git a/apps/desktop/src/ipc.ts b/apps/desktop/src/ipc.ts index fc19021fa6..30218cc211 100644 --- a/apps/desktop/src/ipc.ts +++ b/apps/desktop/src/ipc.ts @@ -21,6 +21,9 @@ export const DESKTOP_IPC = { browserRelease: 'dsh-desktop:browser-release', browserOpenRequested: 'dsh-desktop:browser-open-requested', directoryPick: 'dsh-desktop:directory-pick', + imageDragPrepare: 'dsh-desktop:image-drag-prepare', + imageDragStart: 'dsh-desktop:image-drag-start', + imageDragRelease: 'dsh-desktop:image-drag-release', deviceInfo: 'dsh-desktop:device-info', localeBootstrap: 'dsh-desktop:locale-bootstrap', localeChanged: 'dsh-desktop:locale-changed', @@ -72,6 +75,11 @@ export interface DesktopUpdatePresentation { export interface DshDesktopProductApi { readonly protocolVersion: 1 readonly browser: DesktopBrowserBridge + readonly imageDrag: { + prepare(bytes: Uint8Array, name: string, mediaType: string): Promise + start(handle: string): Promise + release(handle: string): void + } readonly keyboard: DesktopKeyboardApi readonly shortcuts: DesktopShortcutsApi /** diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts index 2afdfd7194..cd93618175 100644 --- a/apps/desktop/src/main.ts +++ b/apps/desktop/src/main.ts @@ -26,6 +26,7 @@ import { resolveDesktopPaths } from './paths.ts' import { DesktopProjectManager } from './project-manager.ts' import { DesktopHostFatalError, DesktopHostProcess, DesktopHostUncleanExitError } from './host-process.ts' import { DesktopPlatformView, PLATFORM_IPC, platformBounds } from './platform-view.ts' +import { installDesktopImageDrag } from './image-drag.ts' import { installDesktopDirectoryPicker } from './directory-picker.ts' import { installMicrophonePermissions } from './microphone-permissions.ts' import { DesktopBackendController } from './backend-controller.ts' @@ -682,6 +683,9 @@ async function main(): Promise { }) installDesktopDirectoryPicker(() => mainWindow) + const disposeImageDrag = installDesktopImageDrag(() => mainWindow) + let installerImageCleanup: Promise | undefined + let installerImagesReleased = false installMicrophonePermissions(session.defaultSession, () => mainWindow?.webContents) const shortcuts = installDesktopShortcuts(() => mainWindow, app.getPath('userData'), process.platform === 'darwin' ? 'macos' : process.platform === 'win32' ? 'windows' : 'linux', () => { refreshApplicationMenu() }, window => updateOverlays.input(window)) @@ -1260,10 +1264,18 @@ async function main(): Promise { mandatoryUI?.dispose() void Promise.all([Promise.resolve(mandatoryPolicy?.dispose()).then(() => policyAuth?.dispose()), backend.close(), // A Platform cleanup failure is logged without cutting the remaining Host shutdown short. - platformView.dispose().catch((error: unknown) => { console.error(error) })]) + platformView.dispose().catch((error: unknown) => { console.error(error) }), disposeImageDrag()]) .catch((error: unknown) => { console.error(error) }).finally(() => { app.quit() }) } app.on('before-quit', (event) => { + if (shellInstallerOwnsQuit && !installerImagesReleased) { + event.preventDefault() + installerImageCleanup ??= disposeImageDrag().catch((error: unknown) => { console.error(error) }).then(() => { + installerImagesReleased = true + app.quit() + }) + return + } if (shellInstallerOwnsQuit) { shuttingDown = true quitConfirmation.dispose() diff --git a/apps/desktop/src/preload-app.ts b/apps/desktop/src/preload-app.ts index f590b8bc8b..0e7f4c0fc9 100644 --- a/apps/desktop/src/preload-app.ts +++ b/apps/desktop/src/preload-app.ts @@ -14,6 +14,13 @@ function createProductApi(): DshDesktopProductApi { return { protocolVersion: 1, browser: createDesktopBrowserBridge(), + imageDrag: { + prepare: (bytes, name, mediaType) => ipcRenderer.invoke(DESKTOP_IPC.imageDragPrepare, bytes, name, mediaType) as Promise, + start: handle => ipcRenderer.invoke(DESKTOP_IPC.imageDragStart, handle) as Promise, + release: (handle) => { void ipcRenderer.invoke(DESKTOP_IPC.imageDragRelease, handle).catch((_error: unknown) => { + // Main-process shutdown also releases every prepared file. + }) }, + }, deviceInfo: () => ipcRenderer.invoke(DESKTOP_IPC.deviceInfo) as Promise, keyboard: { closeWindow: revision => ipcRenderer.invoke(DESKTOP_IPC.shortcutsCloseWindow, revision) as Promise, diff --git a/apps/desktop/tests/image-drag-files.spec.ts b/apps/desktop/tests/image-drag-files.spec.ts new file mode 100644 index 0000000000..f231cea286 --- /dev/null +++ b/apps/desktop/tests/image-drag-files.spec.ts @@ -0,0 +1,48 @@ +import { mkdtemp, readFile, readdir, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { basename, join } from 'node:path' +import { expect, it, onTestFinished } from 'vitest' +import { ImageDragFiles } from '../src/image-drag-files.ts' + +async function files() { + const root = await mkdtemp(join(tmpdir(), 'dsh-drag-test-')) + const store = new ImageDragFiles(root) + onTestFinished(async () => { await store.dispose(); await rm(root, { recursive: true, force: true }) }) + return { root, store } +} + +it('exports original bytes and keeps a used file readable until disposal', async () => { + const { root, store } = await files() + const bytes = new Uint8Array([137, 80, 78, 71]) + const handle = await store.prepare(bytes, '../../original.png', 'image/png') + const path = store.take(handle)! + expect(basename(path)).toBe('_.._original.png') + expect(new Uint8Array(await readFile(path))).toEqual(bytes) + await store.release(handle) + expect(new Uint8Array(await readFile(path))).toEqual(bytes) + await store.dispose() + expect(await readdir(root)).toEqual([]) + expect(store.take(handle)).toBeUndefined() +}) + +it('rejects non-images, foreign handles and writes after disposal', async () => { + const { store } = await files() + await expect(store.prepare(new Uint8Array([1]), 'x.html', 'text/html')).rejects.toThrow('Invalid image transfer') + await expect(store.prepare(new Uint8Array(), 'x.png', 'image/png')).rejects.toThrow('Invalid image transfer') + await expect(store.prepare(new Uint8Array([1]), 'x.png', 'constructor')).rejects.toThrow('Invalid image transfer') + expect(store.take('foreign')).toBeUndefined() + await store.dispose() + await expect(store.prepare(new Uint8Array([1]), 'x.png', 'image/png')).rejects.toThrow('Invalid image transfer') +}) + +it('releases unused preparations and waits for a concurrent write before cleanup', async () => { + const { root, store } = await files() + const handle = await store.prepare(new Uint8Array([1]), 'x.png', 'image/png') + await store.release(handle) + expect(await readdir(root)).toEqual([]) + const pending = store.prepare(new Uint8Array([2]), 'y.png', 'image/png') + const rejected = expect(pending).rejects.toThrow('Image transfer closed') + await store.dispose() + await rejected + expect(await readdir(root)).toEqual([]) +}) diff --git a/apps/desktop/tests/image-drag.spec.ts b/apps/desktop/tests/image-drag.spec.ts new file mode 100644 index 0000000000..d9460fb02c --- /dev/null +++ b/apps/desktop/tests/image-drag.spec.ts @@ -0,0 +1,88 @@ +import { mkdtemp, readFile, readdir, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, expect, it, onTestFinished, vi } from 'vitest' +import type { BrowserWindow, IpcMainInvokeEvent, NativeImage, WebContents, WebFrameMain } from 'electron' +import { DESKTOP_IPC } from '../src/ipc.ts' + +const electron = vi.hoisted(() => ({ + handle: vi.fn(), removeHandler: vi.fn(), getPath: vi.fn(), getFileIcon: vi.fn(), createFromPath: vi.fn(), +})) +vi.mock('electron', () => ({ + app: electron, + ipcMain: electron, + nativeImage: { createFromPath: electron.createFromPath }, +})) +const { installDesktopImageDrag } = await import('../src/image-drag.ts') +afterEach(() => { vi.resetAllMocks() }) + +type Handler = (event: IpcMainInvokeEvent, ...args: unknown[]) => unknown +async function fixture() { + const root = await mkdtemp(join(tmpdir(), 'dsh-native-image-drag-')) + onTestFinished(async () => { await rm(root, { recursive: true, force: true }) }) + electron.getPath.mockReturnValue(root) + const icon: NativeImage = Object.assign({} as NativeImage, { isEmpty: () => false, resize: () => icon }) + electron.createFromPath.mockReturnValue(icon) + const frame = Object.assign({} as WebFrameMain, { url: 'dsh-app://app/' }) + const window = Object.assign({} as BrowserWindow, { isDestroyed: () => false, + webContents: Object.assign({} as WebContents, { mainFrame: frame, startDrag: vi.fn() }) }) + let current: BrowserWindow | undefined = window + const dispose = installDesktopImageDrag(() => current) + onTestFinished(dispose) + const handlers = new Map(electron.handle.mock.calls.map(([channel, handler]) => [channel, handler])) + const event = Object.assign({} as IpcMainInvokeEvent, { sender: window.webContents, senderFrame: frame }) + const call = (channel: string, ...args: unknown[]) => (handlers.get(channel) as Handler)(event, ...args) + return { root, window, frame, event, icon, call, dispose, detach: () => { current = undefined } } +} + +it('stages original bytes before the gesture and synchronously starts an OS copy drag', async () => { + const f = await fixture() + const bytes = new Uint8Array([137, 80, 78, 71]) + const handle = await f.call(DESKTOP_IPC.imageDragPrepare, bytes, 'original.png', 'image/png') + expect(f.window.webContents.startDrag).not.toHaveBeenCalled() + expect(f.call(DESKTOP_IPC.imageDragStart, handle)).toBe(true) + const [request] = f.window.webContents.startDrag.mock.calls[0] as [{ file: string; icon: NativeImage }] + expect(request.icon).toBe(f.icon) + expect(new Uint8Array(await readFile(request.file))).toEqual(bytes) + await f.call(DESKTOP_IPC.imageDragRelease, handle) + expect(new Uint8Array(await readFile(request.file))).toEqual(bytes) + expect(f.call(DESKTOP_IPC.imageDragStart, handle)).toBe(false) + await f.dispose() + expect(await readdir(f.root)).toEqual([]) +}) + +it('rejects foreign senders, subframes, revoked handles and native failures', async () => { + const f = await fixture() + expect(f.call(DESKTOP_IPC.imageDragStart, 'foreign')).toBe(false) + const handle = await f.call(DESKTOP_IPC.imageDragPrepare, new Uint8Array([1]), 'x.png', 'image/png') + f.window.webContents.startDrag.mockImplementationOnce(() => { throw new Error('OS rejected drag') }) + expect(f.call(DESKTOP_IPC.imageDragStart, handle)).toBe(false) + f.frame.url = 'https://other.test/' + expect(() => f.call(DESKTOP_IPC.imageDragStart, handle)).toThrow('unowned renderer') + f.frame.url = 'dsh-app://app/' + Object.assign(f.event, { senderFrame: { url: 'dsh-app://app/' } }) + expect(() => f.call(DESKTOP_IPC.imageDragStart, handle)).toThrow('Unowned image drag request') + Object.assign(f.event, { senderFrame: f.frame, sender: {} }) + expect(() => f.call(DESKTOP_IPC.imageDragStart, handle)).toThrow('Unowned image drag request') + f.detach() + expect(() => f.call(DESKTOP_IPC.imageDragStart, handle)).toThrow('Unowned image drag request') +}) + +it('prepares the fallback icon before accepting a gesture and cancels late work on shutdown', async () => { + const f = await fixture() + electron.createFromPath.mockReturnValue({ isEmpty: () => true }) + const pending = Promise.withResolvers() + electron.getFileIcon.mockReturnValue(pending.promise) + const prepare = f.call(DESKTOP_IPC.imageDragPrepare, new Uint8Array([1]), 'x.svg', 'image/svg+xml') + const rejected = expect(prepare).rejects.toThrow('Unowned image drag request') + await vi.waitFor(() => { expect(electron.getFileIcon).toHaveBeenCalledOnce() }) + let stopped = false + const stopping = f.dispose().then(() => { stopped = true }) + await Promise.resolve() + expect(stopped).toBe(false) + pending.resolve(f.icon) + await rejected + await stopping + expect(f.window.webContents.startDrag).not.toHaveBeenCalled() + expect(await readdir(f.root)).toEqual([]) +}) diff --git a/apps/desktop/tests/main-startup.spec.ts b/apps/desktop/tests/main-startup.spec.ts index d9a8f3103b..5e60f0a3e6 100644 --- a/apps/desktop/tests/main-startup.spec.ts +++ b/apps/desktop/tests/main-startup.spec.ts @@ -1870,7 +1870,7 @@ describe('desktop main startup', () => { harness.app.quit() await harness.quitCompleted.promise expect(modal.isDestroyed()).toBe(true) - expect(harness.app.quit).toHaveBeenCalledTimes(2) + expect(harness.app.quit).toHaveBeenCalledTimes(3) // The installer owns the exit, so Platform cleanup starts without holding the quit open. expect(harness.platformDispose).toHaveBeenCalledOnce() inspected.resolve({ activeTasks: true, scheduledTasks: true }) @@ -1878,7 +1878,7 @@ describe('desktop main startup', () => { await vi.advanceTimersByTimeAsync(0) expect(harness.dialog.showMessageBox).toHaveBeenCalledTimes(pendingPhase === 'dialog' ? 1 : 0) expect(harness.platformDispose).toHaveBeenCalledOnce() - expect(harness.app.quit).toHaveBeenCalledTimes(2) + expect(harness.app.quit).toHaveBeenCalledTimes(3) disposal.resolve() }) diff --git a/apps/desktop/tests/preload-app.spec.ts b/apps/desktop/tests/preload-app.spec.ts index e238f583c1..b16b6e2341 100644 --- a/apps/desktop/tests/preload-app.spec.ts +++ b/apps/desktop/tests/preload-app.spec.ts @@ -254,3 +254,19 @@ it('forwards browser guest input only for the focused live webview lease', async expect(listener).toHaveBeenCalledOnce() off() }) + +it('prepares original image bytes and starts native drags without renderer filesystem paths', async () => { + vi.stubGlobal('location', new URL('dsh-app://app/')) + await import('../src/preload-app.ts') + const api = electron.contextBridge.exposeInMainWorld.mock.calls.find(([name]) => name === 'dshDesktop')?.[1] as DshDesktopProductApi + const bytes = new Uint8Array([137, 80, 78, 71]) + electron.ipcRenderer.invoke.mockResolvedValueOnce('image-handle').mockResolvedValueOnce(true).mockResolvedValueOnce(undefined) + await expect(api.imageDrag.prepare(bytes, 'original.png', 'image/png')).resolves.toBe('image-handle') + await expect(api.imageDrag.start('image-handle')).resolves.toBe(true) + api.imageDrag.release('image-handle') + expect(electron.ipcRenderer.invoke.mock.calls).toEqual([ + [DESKTOP_IPC.imageDragPrepare, bytes, 'original.png', 'image/png'], + [DESKTOP_IPC.imageDragStart, 'image-handle'], + [DESKTOP_IPC.imageDragRelease, 'image-handle'], + ]) +}) diff --git a/apps/web/tests/document-preview.e2e.ts b/apps/web/tests/document-preview.e2e.ts index 9953e112cc..c7326999f7 100644 --- a/apps/web/tests/document-preview.e2e.ts +++ b/apps/web/tests/document-preview.e2e.ts @@ -1082,7 +1082,7 @@ else process.exit(1); width: (node as HTMLImageElement).naturalWidth, height: (node as HTMLImageElement).naturalHeight, draggable: (node as HTMLImageElement).draggable, - }))).toEqual({ width: 1, height: 1, draggable: false }) + }))).toEqual({ width: 1, height: 1, draggable: true }) const centering = await tinyImage.evaluate((node) => { const image = node.getBoundingClientRect() const scroller = node.closest('[data-textpreview-body]')?.getBoundingClientRect() @@ -1105,6 +1105,14 @@ else process.exit(1); await page.getByRole('menuitem', { name: 'Fit width', exact: true }).click() await expect.poll(() => imageZoom.innerText()).toBe('100%') + await tinyImage.dragTo(input) + const draggedPreview = page.getByRole('group', { name: 'Pending attachments', exact: true }).getByRole('img') + await draggedPreview.waitFor({ state: 'visible', timeout: 5000 }) + const draggedBytes = await draggedPreview.evaluate(async (node: HTMLImageElement) => + Array.from(new Uint8Array(await (await fetch(node.src)).arrayBuffer()))) + expect(Buffer.from(draggedBytes)).toEqual(TINY_PNG) + expect(await input.innerText()).toBe('') + await page.getByRole('button', { name: 'Remove image tiny.png', exact: true }).click() await openFile('large.svg') await expect.poll(() => viewer.innerText()).toBe('Image') await viewer.click() @@ -1143,6 +1151,17 @@ else process.exit(1); await page.getByRole('menuitem', { name: '100%', exact: true }).click() await expect.poll(async () => (await largeImage.boundingBox())!.width).toBeCloseTo(1200, 0) expect(await imageScrollport.evaluate(node => node.scrollWidth > node.clientWidth)).toBe(true) + await imageScrollport.evaluate((node) => { node.scrollLeft = 0; node.scrollTop = 0 }) + const panBox = await imageScrollport.boundingBox() + if (panBox === null) throw new Error('image viewport missing') + await expect.poll(() => largeImage.evaluate(node => getComputedStyle(node).cursor)).toBe('grab') + await page.mouse.move(panBox.x + 180, panBox.y + 180) + await page.mouse.down() + await page.mouse.move(panBox.x + 100, panBox.y + 100, { steps: 8 }) + await expect.poll(() => imageScrollport.evaluate(node => [node.scrollLeft, node.scrollTop])).toEqual([80, 80]) + await expect.poll(() => largeImage.evaluate(node => getComputedStyle(node).cursor)).toBe('grabbing') + await page.mouse.up() + await expect.poll(() => largeImage.evaluate(node => getComputedStyle(node).cursor)).toBe('grab') svgZoom = await revealDocumentZoom(page, preview) await svgZoom.click() await page.getByRole('menuitem', { name: 'Fit width', exact: true }).click() @@ -1151,9 +1170,11 @@ else process.exit(1); expect(await page.locator('html').getAttribute('data-image-preview-escape')).toBeNull() sections.push([ '## Image zoom', '', + '- Contained sidebar PNG drags into the composer as an original-byte image attachment', '- Small PNG fit width remains at intrinsic size; 200% doubles it', '- SVG fit width -> 100% -> fit width toggles horizontal overflow: false -> true -> false', '- Image and Blob identities remain stable while zoom changes', + '- Overflowing image drag pans both axes by 80px; releasing restores the grab cursor', ].join('\n')) const releaseRead = Promise.withResolvers() diff --git a/apps/web/tests/markdown-images.e2e.ts b/apps/web/tests/markdown-images.e2e.ts index db3cad441a..d521b59dce 100644 --- a/apps/web/tests/markdown-images.e2e.ts +++ b/apps/web/tests/markdown-images.e2e.ts @@ -319,6 +319,15 @@ describe('web e2e: Markdown image rendering', () => { expect(tripwire.pageErrors).toEqual([]) expect(tripwire.warnings).toEqual([]) await assertFixtureInventory(SNAPSHOT_DIR, ['ui.expected.md']) + const input = page.locator('[data-composer-input]').first() + await workspaceImage.dragTo(input) + const dragged = page.getByRole('group', { name: 'Pending attachments', exact: true }).getByRole('img') + await dragged.waitFor({ state: 'visible' }) + expect(Buffer.from(await dragged.evaluate(async (node: HTMLImageElement) => + Array.from(new Uint8Array(await (await fetch(node.src)).arrayBuffer()))))).toEqual(PNG) + expect(await input.innerText()).toBe('') + expect(await page.getByRole('dialog', { name: 'Image preview', exact: true }).count()).toBe(0) + await page.getByRole('button', { name: 'Remove image valid.png', exact: true }).click() await link.evaluate((element) => { element.scrollIntoView({ block: 'end' }) }) await page.mouse.move(0, 0) await link.hover() diff --git a/packages/client/ui-attachment/README.i18n.yaml b/packages/client/ui-attachment/README.i18n.yaml index 123b686b4e..bac5a4d946 100644 --- a/packages/client/ui-attachment/README.i18n.yaml +++ b/packages/client/ui-attachment/README.i18n.yaml @@ -21,8 +21,8 @@ en: a77c8ec48e219aec zh: 756d4fe881403006 /deepseek-ai-dsh-client-ui-attachment/use-this-package/message-images-and-the-lightbox: - en: 8b3d7729152d8060 - zh: 815f932ccc9653f5 + en: 10518a60d83df5c9 + zh: f50541b6662ab72b /deepseek-ai-dsh-client-ui-attachment/use-this-package/drop-overlay: en: 836a62c8f73366ac zh: 35da7836eaf68a67 diff --git a/packages/client/ui-attachment/README.md b/packages/client/ui-attachment/README.md index 4fee29e123..330be4a286 100644 --- a/packages/client/ui-attachment/README.md +++ b/packages/client/ui-attachment/README.md @@ -37,6 +37,8 @@ In Chat, one user message presents files and images in a right-aligned wrapping Trajectory attachment rows request 48px square thumbnails that contain the complete image without cropping. Loading and retry icons keep the same box, with localized tooltips and accessible names; the image opens the same lightbox. A slot owner may supply a presentation-only image label for the thumbnail and lightbox without changing the durable reference or the cache lookup. +Loaded conversation images can be dragged into the composer as image attachments or to another application as original files. Dragging does not open the lightbox. The shared image-transfer owner retains Web drag bytes until the gesture ends; Desktop uses the system file drag. A failed preparation or drag keeps the image visible and reports a localized retry message. + ### Drop overlay While a file drag is over the page, the full-viewport overlay announces the drop: illustration, title, and a limits line when drops are accepted. The overlay only shows state — the owner's document-level listeners decide accept or reject, and the drop handler reports which dropped members are directories through the entry API so the owner can cite or refuse them. diff --git a/packages/client/ui-attachment/README.zh.md b/packages/client/ui-attachment/README.zh.md index 706a6d97c7..3a4d79a336 100644 --- a/packages/client/ui-attachment/README.zh.md +++ b/packages/client/ui-attachment/README.zh.md @@ -37,6 +37,8 @@ Chat 中的一条用户消息把文件与图片放在同一个靠右、可换行 Trajectory 附件行使用 48px 方形缩略图,完整缩放图片而不裁剪。加载与重试图标保持相同尺寸,提供本地化的工具提示和可访问名称;图片打开同一个灯箱。插槽持有方可以为缩略图与灯箱提供仅用于展示的图片名称,而不改变持久化引用或缓存查询。 +已加载的会话图片可拖入输入框作为图片附件,也可将原文件拖到其他应用。拖动不会打开灯箱。共享图片传递组件将 Web 拖动的原图字节保留到手势结束;Desktop 使用系统文件拖放。准备或拖放失败时保留图片,并显示本地化的重试提示。 + ### 拖放遮罩 文件拖到页面上方时,全视口遮罩显示拖放提示,包括插画和标题;接受拖放时还会显示一行限制说明。遮罩只呈现状态,是否接受由持有方的文档级监听器决定,拖放处理器通过 entry API 报告放下的成员中哪些是文件夹,以便持有方引用或拒绝它们。 diff --git a/packages/client/ui-attachment/src/MessageImage.tsx b/packages/client/ui-attachment/src/MessageImage.tsx index a067ffd446..62afb5b279 100644 --- a/packages/client/ui-attachment/src/MessageImage.tsx +++ b/packages/client/ui-attachment/src/MessageImage.tsx @@ -1,6 +1,6 @@ import { useCallback, useEffect, useMemo, useState } from 'react' import type { ImageAttachmentRef } from '@deepseek-ai/dsh-attachment' -import { IconLoadingOutlineRegular, IconRefreshOutlineRegular } from '@deepseek-ai/dsh-client-ui-primitives' +import { IconLoadingOutlineRegular, IconRefreshOutlineRegular, useImageDrag } from '@deepseek-ai/dsh-client-ui-primitives' import { ImageLightbox } from '@deepseek-ai/dsh-client-ui-primitives' import type { MessageImageLabels } from '@deepseek-ai/dsh-client-ui-conversation/client' import css from './MessageImage.module.css' @@ -108,6 +108,7 @@ export function MessageImage({ image, load, variant, labels }: { }, [attachment, load, attempt]) const src = preview?.url ?? loaded + const drag = useImageDrag(src ?? '', preview?.name ?? attachment?.name ?? 'image', labels.dragFailed) const label = ('attachment' in image ? image.label : undefined) ?? preview?.name ?? attachment?.name ?? labels.image const loadingThumbnail = variant === 'thumbnail' && src === null @@ -135,6 +136,7 @@ export function MessageImage({ image, load, variant, labels }: { title={loadingThumbnail ? labels.loading : labels.open} aria-label={loadingThumbnail ? labels.loading : labels.openNamed(label)} aria-busy={loadingThumbnail || undefined} + onClickCapture={drag.onClickCapture} onClick={() => { if (src !== null) setOpen(true) }} > {src === null @@ -143,7 +145,9 @@ export function MessageImage({ image, load, variant, labels }: { {loadingThumbnail ? : labels.loading} ) - : {label}} + : {label}} {open && src !== null && } diff --git a/packages/client/ui-attachment/src/client/ComposerAttachments.tsx b/packages/client/ui-attachment/src/client/ComposerAttachments.tsx index 0065410b9d..e172778335 100644 --- a/packages/client/ui-attachment/src/client/ComposerAttachments.tsx +++ b/packages/client/ui-attachment/src/client/ComposerAttachments.tsx @@ -2,7 +2,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import type { ComposerAttachment, ComposerAttachmentsProps, ComposerImageAttachment, } from '@deepseek-ai/dsh-client-ui-conversation/client' -import { IconCloseFillRegular } from '@deepseek-ai/dsh-client-ui-primitives' +import { IconCloseFillRegular, useImageDrop } from '@deepseek-ai/dsh-client-ui-primitives' import { AttachmentRail } from '../AttachmentRail.tsx' import type { AttachmentRailItem } from '../AttachmentRail.tsx' import { DropOverlay } from '../DropOverlay.tsx' @@ -23,6 +23,7 @@ export function ComposerAttachments({ }: ComposerAttachmentsProps) { const [preview, setPreview] = useState(null) const [dragActive, setDragActive] = useState(false) + const readImageDrag = useImageDrop() const dragDepth = useRef(0) const closePreview = useCallback(() => { setPreview(null) }, []) useEffect(() => { @@ -30,8 +31,8 @@ export function ComposerAttachments({ }, [attachments, preview]) useEffect(() => { - return installDocumentDropEvents(canAcceptDrop, onAddFiles, dragDepth, setDragActive) - }, [canAcceptDrop, onAddFiles]) + return installDocumentDropEvents(canAcceptDrop, onAddFiles, dragDepth, setDragActive, readImageDrag) + }, [canAcceptDrop, onAddFiles, readImageDrag]) const railItems = useMemo(() => attachments.map(attachment => ({ id: attachment.id, diff --git a/packages/client/ui-attachment/src/client/drop-events.ts b/packages/client/ui-attachment/src/client/drop-events.ts index 2f3da69372..938cd3bada 100644 --- a/packages/client/ui-attachment/src/client/drop-events.ts +++ b/packages/client/ui-attachment/src/client/drop-events.ts @@ -1,4 +1,5 @@ /** Document drag-and-drop listeners owned by one mounted attachment view. */ +import { IMAGE_DRAG_TYPE } from '@deepseek-ai/dsh-client-ui-primitives' import type { ComposerAttachmentsProps } from '@deepseek-ai/dsh-client-ui-conversation/client' /** @@ -25,6 +26,7 @@ function droppedDirectories(dataTransfer: DataTransfer, files: readonly File[]): * @param onAddFiles - attachment intake callback. * @param dragDepth - the view's retained nested-drag counter. * @param setDragActive - publish whether a file drag is active. + * @param readImageDrag - resolve an original image retained by this application during a browser drag. * @returns cleanup for exactly these listeners. */ export function installDocumentDropEvents( @@ -32,10 +34,11 @@ export function installDocumentDropEvents( onAddFiles: ComposerAttachmentsProps['onAddFiles'], dragDepth: { current: number }, setDragActive: (active: boolean) => void, + readImageDrag: (transfer: DataTransfer) => File | undefined, ): () => void { const fileTransfer = (event: globalThis.DragEvent): DataTransfer | null => { const dataTransfer = event.dataTransfer - if (dataTransfer === null || !dataTransfer.types.includes('Files')) return null + if (dataTransfer === null || (!dataTransfer.types.includes('Files') && !dataTransfer.types.includes(IMAGE_DRAG_TYPE))) return null return dataTransfer } const reset = (): void => { @@ -68,7 +71,8 @@ export function installDocumentDropEvents( event.preventDefault() reset() if (canAcceptDrop) { - const files = [...dataTransfer.files] + const image = dataTransfer.types.includes(IMAGE_DRAG_TYPE) ? readImageDrag(dataTransfer) : undefined + const files = image === undefined ? [...dataTransfer.files] : [image] onAddFiles(files, droppedDirectories(dataTransfer, files)) } } diff --git a/packages/client/ui-attachment/src/client/labels.ts b/packages/client/ui-attachment/src/client/labels.ts index 257a362d97..6e684ed6b0 100644 --- a/packages/client/ui-attachment/src/client/labels.ts +++ b/packages/client/ui-attachment/src/client/labels.ts @@ -21,6 +21,7 @@ export function lightboxLabels(t: TranslateNS<'conversation'>): ImageLightboxLab */ export function messageImageLabels(t: TranslateNS<'conversation'>): MessageImageLabels { return { + dragFailed: t('image.dragFailed'), image: t('image.label'), open: t('image.openOriginal'), openNamed: label => t('image.openOriginalLabel', { label }), diff --git a/packages/client/ui-attachment/tests/message-image.client.spec.tsx b/packages/client/ui-attachment/tests/message-image.client.spec.tsx index d447df5fd9..b55c65ecfc 100644 --- a/packages/client/ui-attachment/tests/message-image.client.spec.tsx +++ b/packages/client/ui-attachment/tests/message-image.client.spec.tsx @@ -18,6 +18,7 @@ afterEach(cleanup) const labels: MessageImageLabels = { image: '图片', + dragFailed: '图片尚未准备好拖出,请重试', open: '查看原图', openNamed: label => `${label},点击查看原图`, loading: '图片加载中…', diff --git a/packages/client/ui-chat/src/client/chat/ChatView.tsx b/packages/client/ui-chat/src/client/chat/ChatView.tsx index 1e0741f31f..6c38f26abd 100644 --- a/packages/client/ui-chat/src/client/chat/ChatView.tsx +++ b/packages/client/ui-chat/src/client/chat/ChatView.tsx @@ -86,7 +86,7 @@ export function ChatView({ const fileImages = useMemo(() => ({ resolve: (path: string) => fileMediaUrl(document.baseURI, resolveWorkspacePath(cwd, path)), labels: { - open: t('image.open'), loading: t('image.loading'), failed: t('image.failed'), + dragFailed: t('image.dragFailed'), open: t('image.open'), loading: t('image.loading'), failed: t('image.failed'), dialog: t('image.dialog'), close: t('image.close'), }, }), [cwd, t]) diff --git a/packages/client/ui-chat/src/client/locale.ts b/packages/client/ui-chat/src/client/locale.ts index 7a950132e2..095f62dd68 100644 --- a/packages/client/ui-chat/src/client/locale.ts +++ b/packages/client/ui-chat/src/client/locale.ts @@ -67,6 +67,7 @@ export const zh = { 'view.chat': '对话', 'image.open': '查看大图', 'image.loading': '加载图片…', + 'image.dragFailed': '图片尚未准备好拖出,请重试', 'image.failed': '图片无法预览', 'image.dialog': '图片预览', 'image.close': '关闭图片预览', @@ -269,6 +270,7 @@ export const en = { 'view.chat': 'Chat', 'image.open': 'View full image', 'image.loading': 'Loading image…', + 'image.dragFailed': 'The image is not ready to drag. Try again.', 'image.failed': 'Image preview unavailable', 'image.dialog': 'Image preview', 'image.close': 'Close image preview', diff --git a/packages/client/ui-conversation/src/client/contract/slots.ts b/packages/client/ui-conversation/src/client/contract/slots.ts index a02f5aa662..a5b99922eb 100644 --- a/packages/client/ui-conversation/src/client/contract/slots.ts +++ b/packages/client/ui-conversation/src/client/contract/slots.ts @@ -131,6 +131,8 @@ export interface MessageImageLabels { loading: string /** Retry-control label shown when the load fails. */ loadFailed: string + /** Failed original-image drag, with retry guidance. */ + dragFailed: string /** Lightbox strings forwarded to the opened preview. */ lightbox: ImageLightboxLabels } diff --git a/packages/client/ui-conversation/src/client/locales.ts b/packages/client/ui-conversation/src/client/locales.ts index f1f708073b..d7e432fdd6 100644 --- a/packages/client/ui-conversation/src/client/locales.ts +++ b/packages/client/ui-conversation/src/client/locales.ts @@ -46,6 +46,7 @@ export const zh = { 'image.label': '图片', 'image.loadFailed': '图片加载失败,点击重试', 'image.loading': '图片加载中…', + 'image.dragFailed': '图片尚未准备好拖出,请重试', 'image.preview': '原图预览', 'image.closePreview': '关闭原图预览', 'image.unsupportedType': '仅支持 PNG、JPG、WebP、GIF 格式的图片', @@ -426,6 +427,7 @@ export const en = { 'image.label': 'Image', 'image.loadFailed': 'Image failed to load; click to retry', 'image.loading': 'Loading image…', + 'image.dragFailed': 'The image is not ready to drag. Try again.', 'image.preview': 'Original image preview', 'image.closePreview': 'Close original image preview', 'image.unsupportedType': 'Only PNG, JPG, WebP, and GIF images are supported', diff --git a/packages/client/ui-layout/package.json b/packages/client/ui-layout/package.json index a233d3410a..ce216ab52f 100644 --- a/packages/client/ui-layout/package.json +++ b/packages/client/ui-layout/package.json @@ -56,7 +56,8 @@ "@deepseek-ai/dsh-client-ui-theme": "workspace:*", "@deepseek-ai/cordis": "workspace:~", "react": "^18.2.0", - "@deepseek-ai/dsh-client-shortcuts": "workspace:*" + "@deepseek-ai/dsh-client-shortcuts": "workspace:*", + "@deepseek-ai/dsh-client-ui-primitives": "workspace:*" }, "files": [ "locale/*.json", diff --git a/packages/client/ui-layout/src/client/AppFrame.tsx b/packages/client/ui-layout/src/client/AppFrame.tsx index 5614947447..2a076acb14 100644 --- a/packages/client/ui-layout/src/client/AppFrame.tsx +++ b/packages/client/ui-layout/src/client/AppFrame.tsx @@ -20,6 +20,7 @@ import type { PropsLocale, PropsRenderSlots, PropsRuntime, PropsStore, } from '@deepseek-ai/dsh-client-ui-slots' import { CENTER_MIN, clampWidth, computeColumns, RIGHTBAR_DEFAULT_RATIO, RIGHTBAR_MAX_RATIO, RIGHTBAR_MIN, SIDEBAR_AUTO_COLLAPSE, SIDEBAR_COLLAPSED, SIDEBAR_DEFAULT } from './columns.ts' +import { ImageDragProvider } from '@deepseek-ai/dsh-client-ui-primitives' import { DocumentTitle } from './DocumentTitle.tsx' import type { createLayoutStore } from './stores.ts' import css from './AppFrame.module.css' @@ -256,7 +257,7 @@ export function AppFrame({ const leadingMounted = darwin && sidebarCollapsed return ( -
0 && ( )} -
+
) } diff --git a/packages/client/ui-primitives/README.i18n.yaml b/packages/client/ui-primitives/README.i18n.yaml index 2e59aaf715..4599e98497 100644 --- a/packages/client/ui-primitives/README.i18n.yaml +++ b/packages/client/ui-primitives/README.i18n.yaml @@ -24,8 +24,8 @@ en: 28ac8f0b1edbcf81 zh: 5c4c324bf84a282f /deepseek-ai-dsh-client-ui-primitives/use-this-package/rendering-agent-output: - en: 12fe9d9b1410edef - zh: f2dfdc01170d7cce + en: 40a6ad9b8840aaf2 + zh: 2997fa3df59a4ec8 /deepseek-ai-dsh-client-ui-primitives/use-this-package/localizing-copy: en: 4aef65dea11de083 zh: abc22cd7c65cdf38 diff --git a/packages/client/ui-primitives/README.md b/packages/client/ui-primitives/README.md index 5b29187be1..f65d3900c3 100644 --- a/packages/client/ui-primitives/README.md +++ b/packages/client/ui-primitives/README.md @@ -111,6 +111,8 @@ The nearest `MarkdownDelegateProvider` supplies optional `openExternalLink` and `ImageLightbox` is the shared original-image modal with focus restoration and Escape dismissal. Like `Modal`, its mask leaves the Windows caption unpainted, and its close control sits 20px below the caption. The internal thumbnail renderer receives loading/failure labels from its owner. `HoverCard.inline` keeps a file link in the text flow and uses the shared menu material, keyboard-visible focus, and placement above or below the anchor without covering it. Escape closes an open thumbnail even when focus is elsewhere; subsequent Escape presses reach the owner. `MarkdownDelegateProvider.fileImages` supplies a decoded-path resolver and complete image labels: settled image links gain hover previews, and standalone images gain lightbox activation. Image-only anchors retain their single navigation target. The parser recovers only complete, unescaped, standalone local image references with bare spaces and an unambiguous supported image suffix; code and ambiguous destinations remain literal. +`ImageDragProvider` wraps the application and retains the active Web image transfer and failure toast. `useImageDrag` prepares original bytes from an authorized source or supplied `File`, cancels stale preparation, and uses the Desktop bridge when present. Web transfers carry an application-local token plus Chromium’s `DownloadURL`; `useImageDrop` resolves only the active token to its original `File`. External browser drops depend on the receiving application and browser support. No drag places an internal URL or alt text in `text/plain`. + ### Localizing copy The atoms cannot read the application locale, so every piece of user-facing copy arrives through required label props. `HoverCard`, `TerminalBlock`, `JsonTree`, `CodeBlock`, `MarkdownText`, `JsonBlock`, `ConnectionIndicator`, `Modal`, `DiffBlock`, `ReadBlock`, `SearchBlock`, and `WebBlock` accept complete localized labels. The package owns no language fallback; omission fails typechecking, and each feature maps its typed `t` seat into the primitive's label interface. diff --git a/packages/client/ui-primitives/README.zh.md b/packages/client/ui-primitives/README.zh.md index c1593e273b..fac88aa250 100644 --- a/packages/client/ui-primitives/README.zh.md +++ b/packages/client/ui-primitives/README.zh.md @@ -111,6 +111,8 @@ kind: "package-library" `ImageLightbox` 是共享原图浮层,支持焦点恢复与 Esc 关闭。与 `Modal` 相同,其遮罩不覆盖 Windows 顶栏,关闭按钮位于顶栏下方 20px。包内缩略图渲染器由调用方提供加载及失败文案。`HoverCard.inline` 使文件链接保持在文本流内,并使用共享菜单材质、键盘可见焦点,并在锚点上方或下方定位而不遮挡锚点。即使焦点位于其他位置,Esc 也会关闭已打开的缩略图;后续 Esc 按键继续传给 owner。`MarkdownDelegateProvider.fileImages` 提供已解码路径解析器与完整图片文案:消息落定后的图片链接支持悬停预览,独立图片支持点击放大。仅包含图片的链接保留单一导航目标。解析器仅恢复完整、未转义、独立成段且带明确图片扩展名的含裸空格本地图片引用;代码与有歧义的目标保持原文。 +`ImageDragProvider` 包裹应用,持有当前 Web 图片传递状态和失败提示。`useImageDrag` 从已授权来源或传入的 `File` 准备原图字节,取消过期准备,并在 Desktop 桥接存在时使用它。Web 拖放携带应用内令牌和 Chromium 的 `DownloadURL`;`useImageDrop` 仅将当前令牌解析为原始 `File`。浏览器向外部拖放的结果取决于浏览器和接收应用的支持。拖放不会将内部 URL 或 alt 文本放入 `text/plain`。 + ### 本地化文案 这些原子组件无法读取应用 locale,因此每段面向用户的文案都必须通过 label prop 提供。`HoverCard`、`TerminalBlock`、`JsonTree`、`CodeBlock`、`MarkdownText`、`JsonBlock`、`ConnectionIndicator`、`Modal`、`DiffBlock`、`ReadBlock`、`SearchBlock` 与 `WebBlock` 接收完整的本地化 label。本包不拥有语言回退;遗漏会导致类型检查失败,各功能会把带类型的 `t` 席位映射到 primitive 的 label 接口。 diff --git a/packages/client/ui-primitives/src/ImageDragProvider.tsx b/packages/client/ui-primitives/src/ImageDragProvider.tsx new file mode 100644 index 0000000000..f6c2cece0a --- /dev/null +++ b/packages/client/ui-primitives/src/ImageDragProvider.tsx @@ -0,0 +1,60 @@ +/** Application-lifetime feedback for image drag operations. */ +import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, type ReactNode } from 'react' +import { Toast } from './Toast.tsx' + +/** Browser drag token; file bytes remain in the owning mounted application. */ +export const IMAGE_DRAG_TYPE = 'application/x-dsh-image' + +interface ImageTransfer { + report: (text: string) => void + begin: (file: File) => string + end: () => void + read: (transfer: DataTransfer) => File | undefined +} + +const missingProvider = (): never => { throw new Error('Image dragging requires ImageDragProvider') } +const ImageDragFeedback = createContext({ + report: missingProvider, begin: missingProvider, end: missingProvider, read: missingProvider, +}) + +/** + * Keep drag failures visible when their source image or conversation unmounts. + * @param props - application children. + * @returns children and the current localized failure toast. + */ +export function ImageDragProvider({ children }: { readonly children: ReactNode }): ReactNode { + const active = useRef<{ token: string; file: File }>() + const sequence = useRef(0) + const [failure, setFailure] = useState<{ text: string; sequence: number }>() + const report = useCallback((text: string) => { setFailure({ text, sequence: ++sequence.current }) }, []) + const dismiss = useCallback(() => { setFailure(undefined) }, []) + const begin = useCallback((file: File): string => { + const token = Array.from(crypto.getRandomValues(new Uint8Array(16)), byte => byte.toString(16).padStart(2, '0')).join('') + active.current = { token, file } + return token + }, []) + const end = useCallback(() => { active.current = undefined }, []) + useEffect(() => { + window.addEventListener('dragend', end) + return () => { window.removeEventListener('dragend', end) } + }, [end]) + const read = useCallback((transfer: DataTransfer): File | undefined => { + const current = active.current + return current !== undefined && current.token === transfer.getData(IMAGE_DRAG_TYPE) ? current.file : undefined + }, []) + const value = useMemo(() => ({ report, begin, end, read }), [report, begin, end, read]) + return + {children} + {failure !== undefined && } + +} + +/** @returns the mounted application's image transfer owner. */ +export function useImageDragTransfer(): ImageTransfer { + return useContext(ImageDragFeedback) +} + +/** @returns a reader that resolves only this application's active original-image drag. */ +export function useImageDrop(): (transfer: DataTransfer) => File | undefined { + return useImageDragTransfer().read +} diff --git a/packages/client/ui-primitives/src/image-drag.ts b/packages/client/ui-primitives/src/image-drag.ts new file mode 100644 index 0000000000..80db3219bf --- /dev/null +++ b/packages/client/ui-primitives/src/image-drag.ts @@ -0,0 +1,110 @@ +/** Original image files supplied to browser and native drag sessions. */ +import { useCallback, useEffect, useRef, type DragEvent, type MouseEvent, type PointerEvent } from 'react' + +import { IMAGE_DRAG_TYPE, useImageDragTransfer } from './ImageDragProvider.tsx' + +/** Application-owned native image transfer; prepared handles never expose host paths. */ +export interface DesktopImageDrag { + /** @returns an opaque handle for the original file, retained until release or application exit. */ + prepare: (bytes: Uint8Array, name: string, mediaType: string) => Promise + /** Start a system copy drag from an already prepared handle. */ + start: (handle: string) => Promise + /** Release an unused handle; files already handed to the OS survive until application exit. */ + release: (handle: string) => void +} + +interface PreparedImage { + readonly file: File + readonly handle?: string +} + +interface ImageDragHandlers { + onPointerEnter: () => void + onPointerDown: (event: PointerEvent) => void + onDragStart: (event: DragEvent) => void + onDragEnd: () => void + onClickCapture: (event: MouseEvent) => void +} + +/** + * Prepare original bytes on hover and supply them synchronously when dragging begins. + * @param src - authorized image URL; changing it retires pending preparation. + * @param name - suggested original filename, without a directory. + * @param failureText - localized failure message, including a retry action. + * @param original - already loaded original file; omitted sources are read from their image URL. + * @returns drag handlers; cancellation never starts a late drag. + */ +export function useImageDrag(src: string, name: string, failureText: string, original?: File): ImageDragHandlers { + const desktop = (globalThis as typeof globalThis & { dshDesktop?: { imageDrag?: DesktopImageDrag } }).dshDesktop?.imageDrag + const state = useRef<{ source: string; prepared?: PreparedImage; pending?: Promise | undefined; abort: AbortController }>() + const dragged = useRef(false) + const transfer = useImageDragTransfer() + const reportFailure = transfer.report + const prepare = useCallback((): void => { + const current = state.current + if (src === '' || current === undefined || current.source !== src || current.pending !== undefined || current.prepared !== undefined) return + current.pending = (async () => { + let file = original + if (file === undefined) { + const response = await fetch(src, { signal: current.abort.signal, referrerPolicy: 'no-referrer' }) + if (!response.ok) throw new Error('Image read failed') + const blob = await response.blob() + if (!blob.type.startsWith('image/')) throw new Error('Image media type unavailable') + file = new File([blob], name, { type: blob.type }) + } + if (current.abort.signal.aborted) return + const handle = desktop === undefined ? undefined + : await desktop.prepare(new Uint8Array(await file.arrayBuffer()), file.name, file.type) + if (state.current !== current) { + if (handle !== undefined) desktop?.release(handle) + return + } + current.prepared = { file, ...handle === undefined ? {} : { handle } } + })().catch((_error: unknown) => { + // Preparation is speculative; a failed user gesture reports through the owning image. + }).finally(() => { current.pending = undefined }) + }, [src, name, original, desktop]) + useEffect(() => { + const current: NonNullable = { source: src, abort: new AbortController() } + state.current = current + if (src !== '' && (original !== undefined || new URL(src, location.href).origin === location.origin)) prepare() + return () => { + current.abort.abort() + const prepared = current.prepared + if (prepared?.handle !== undefined) desktop?.release(prepared.handle) + state.current = undefined + } + }, [src, name, original, desktop, prepare]) + const onDragStart = useCallback((event: DragEvent): void => { + if (event.defaultPrevented) return + dragged.current = true + const prepared = state.current?.source === src ? state.current.prepared : undefined + if (prepared === undefined) { + event.preventDefault() + prepare() + reportFailure(failureText) + return + } + event.dataTransfer.items.clear() + event.dataTransfer.effectAllowed = 'copy' + if (desktop !== undefined && prepared.handle !== undefined) { + event.preventDefault() + void desktop.start(prepared.handle).then((started) => { if (!started) reportFailure(failureText) }, + () => { reportFailure(failureText) }) + return + } + event.dataTransfer.setData(IMAGE_DRAG_TYPE, transfer.begin(prepared.file)) + event.dataTransfer.setData('DownloadURL', `${prepared.file.type}:${prepared.file.name}:${src}`) + }, [src, desktop, prepare, reportFailure, failureText, transfer]) + const onPointerDown = useCallback((event: PointerEvent): void => { + dragged.current = false + if (event.button === 0) prepare() + }, [prepare]) + const onClickCapture = useCallback((event: MouseEvent): void => { + if (!dragged.current) return + event.preventDefault() + event.stopPropagation() + dragged.current = false + }, []) + return { onPointerEnter: prepare, onPointerDown, onDragStart, onDragEnd: transfer.end, onClickCapture } +} diff --git a/packages/client/ui-primitives/src/index.ts b/packages/client/ui-primitives/src/index.ts index 18f9544405..28a3a71fd0 100644 --- a/packages/client/ui-primitives/src/index.ts +++ b/packages/client/ui-primitives/src/index.ts @@ -67,6 +67,9 @@ export { pointerModality } from './input-modality.ts' export { Toast } from './Toast.tsx' export { fileSizeText } from './file-size.ts' export { writeClipboard } from './clipboard.ts' +export { IMAGE_DRAG_TYPE, ImageDragProvider, useImageDrop } from './ImageDragProvider.tsx' +export { useImageDrag } from './image-drag.ts' +export type { DesktopImageDrag } from './image-drag.ts' export { SettingsForm } from './settings-form/SettingsForm.tsx' export type { SettingsFormLabels, SettingsFormProps } from './settings-form/SettingsForm.tsx' export { SettingsSecretField, SettingsValueField } from './settings-form/fields.tsx' diff --git a/packages/client/ui-primitives/src/markdown/MarkdownDelegate.tsx b/packages/client/ui-primitives/src/markdown/MarkdownDelegate.tsx index 681e8f5608..d65c435fea 100644 --- a/packages/client/ui-primitives/src/markdown/MarkdownDelegate.tsx +++ b/packages/client/ui-primitives/src/markdown/MarkdownDelegate.tsx @@ -14,7 +14,7 @@ export interface MarkdownDelegate { /** Image previews for decoded local paths in this owner's workspace. */ readonly fileImages?: { resolve: (path: string) => string | undefined - labels: ImageLightboxLabels & { open: string; loading: string; failed: string } + labels: ImageLightboxLabels & { open: string; loading: string; failed: string; dragFailed: string } } | undefined /** Ordinary HTTP(S) activation; absent handlers retain native anchor behavior. */ readonly openExternalLink?: MarkdownExternalLinkHandler | undefined diff --git a/packages/client/ui-primitives/src/markdown/render.tsx b/packages/client/ui-primitives/src/markdown/render.tsx index cdb9afcbda..05362b438e 100644 --- a/packages/client/ui-primitives/src/markdown/render.tsx +++ b/packages/client/ui-primitives/src/markdown/render.tsx @@ -29,6 +29,7 @@ import { renderTexToReact } from './katex.tsx' import { LinkIconMedium, classifyLinkPath } from '../LinkIcon.tsx' import { useMarkdownDelegate } from './MarkdownDelegate.tsx' import { HoverCard } from '../HoverCard.tsx' +import { useImageDrag } from '../image-drag.ts' import { ImageLightbox } from '../ImageLightbox.tsx' import { ImagePreview } from '../ImagePreview.tsx' import type { PositionedBlock } from './incremental.ts' @@ -650,14 +651,17 @@ function LoadedMarkdownImage({ src, alt, destination, preview }: { const [failed, setFailed] = useState(false) const [open, setOpen] = useState(false) const close = useCallback(() => { setOpen(false) }, []) + const drag = useImageDrag(preview === undefined ? '' : src, destination.split('/').at(-1)?.split(/[?#]/u)[0] || 'image', preview?.labels.dragFailed ?? '') if (failed) return {preview === undefined ? '' : `${preview.labels.failed} · `}{alt || destination} const img = {alt} { setFailed(true) }} loading="lazy" decoding="async" referrerPolicy="no-referrer" /> if (preview === undefined) return img return <> + onClickCapture={drag.onClickCapture} onClick={() => { setOpen(true) }}>{img} {open && } } diff --git a/packages/client/ui-primitives/tests/image-drag.client.spec.tsx b/packages/client/ui-primitives/tests/image-drag.client.spec.tsx new file mode 100644 index 0000000000..740178bd33 --- /dev/null +++ b/packages/client/ui-primitives/tests/image-drag.client.spec.tsx @@ -0,0 +1,119 @@ +// @vitest-environment jsdom +import { File as NodeFile } from 'node:buffer' +import { act, cleanup, fireEvent, render, screen } from '@testing-library/react' +import { afterEach, expect, it, vi } from 'vitest' +import { IMAGE_DRAG_TYPE, ImageDragProvider, useImageDrop } from '../src/ImageDragProvider.tsx' +import { useImageDrag, type DesktopImageDrag } from '../src/image-drag.ts' + +afterEach(() => { cleanup(); vi.unstubAllGlobals(); vi.restoreAllMocks() }) + +function Image({ src, file, onOpen }: { src: string; file?: File; onOpen?: () => void }) { + const drag = useImageDrag(src, 'original.png', 'Image drag failed. Try again.', file) + return +} +function transfer() { + const add = vi.fn() + const values = new Map() + return { items: { add, clear: vi.fn() }, getData: (type: string) => values.get(type) ?? '', + setData: vi.fn((type: string, value: string) => { values.set(type, value) }), effectAllowed: 'none' } +} +function native() { + const bridge: DesktopImageDrag = { prepare: vi.fn(async () => 'handle'), start: vi.fn(async () => true), release: vi.fn() } + vi.stubGlobal('dshDesktop', { imageDrag: bridge }) + return bridge +} +function original() { + return new NodeFile([new Uint8Array([137, 80, 78, 71])], 'original.png', { type: 'image/png' }) as File +} + +it('hands original bytes to the browser without alt text or an internal URI payload', async () => { + const file = original() + const open = vi.fn() + render() + const dataTransfer = transfer() + await act(async () => { fireEvent.dragStart(screen.getByRole('img'), { dataTransfer }) }) + expect(dataTransfer.items.add).not.toHaveBeenCalled() + expect(dataTransfer.items.clear).toHaveBeenCalledOnce() + expect(dataTransfer.setData).toHaveBeenCalledWith(IMAGE_DRAG_TYPE, expect.any(String)) + expect(dataTransfer.effectAllowed).toBe('copy') + expect(dataTransfer.setData).toHaveBeenCalledWith('DownloadURL', 'image/png:original.png:blob:original') + fireEvent.click(screen.getByRole('button')) + expect(open).not.toHaveBeenCalled() + fireEvent.pointerDown(screen.getByRole('img'), { button: 0 }) + fireEvent.click(screen.getByRole('button')) + expect(open).toHaveBeenCalledOnce() +}) + +it('prepares before the gesture, starts native file dragging and releases its handle on unmount', async () => { + const bridge = native() + const file = original() + const view = render() + await act(async () => { await Promise.resolve() }) + expect(bridge.prepare).toHaveBeenCalledExactlyOnceWith(new Uint8Array([137, 80, 78, 71]), 'original.png', 'image/png') + const dataTransfer = transfer() + await act(async () => { expect(fireEvent.dragStart(screen.getByRole('img'), { dataTransfer })).toBe(false) }) + expect(bridge.start).toHaveBeenCalledExactlyOnceWith('handle') + expect(dataTransfer.items.add).not.toHaveBeenCalled() + view.unmount() + expect(bridge.release).toHaveBeenCalledExactlyOnceWith('handle') +}) + +it('cancels an unready gesture and releases late preparation without starting a delayed drag', async () => { + const bridge = native() + const pending = Promise.withResolvers() + vi.mocked(bridge.prepare).mockReturnValue(pending.promise) + const view = render() + await act(async () => { await Promise.resolve() }) + fireEvent.dragStart(screen.getByRole('img'), { dataTransfer: transfer() }) + expect(screen.getByRole('alert').textContent).toBe('Image drag failed. Try again.') + view.rerender(Image closed) + expect(screen.getByRole('alert').textContent).toBe('Image drag failed. Try again.') + await act(async () => { pending.resolve('late'); await pending.promise }) + expect(bridge.release).toHaveBeenCalledExactlyOnceWith('late') + expect(bridge.start).not.toHaveBeenCalled() +}) + +it('reports native failure even after its source image closes', async () => { + const bridge = native() + const pending = Promise.withResolvers() + vi.mocked(bridge.start).mockReturnValue(pending.promise) + const view = render() + await act(async () => { await Promise.resolve() }) + fireEvent.dragStart(screen.getByRole('img'), { dataTransfer: transfer() }) + view.rerender(Image closed) + await act(async () => { pending.resolve(false); await pending.promise }) + expect(screen.getByRole('alert').textContent).toBe('Image drag failed. Try again.') +}) + +it('never exports a failed read or starts a native drag after fetch cancellation', async () => { + const bridge = native() + const pending = Promise.withResolvers() + const fetchImage = vi.fn().mockReturnValue(pending.promise) + vi.stubGlobal('fetch', fetchImage) + const view = render() + fireEvent.pointerEnter(screen.getByRole('img')) + const request = fetchImage.mock.calls[0]?.[1] + view.rerender(Closed) + expect(request?.signal?.aborted).toBe(true) + await act(async () => { pending.resolve(new Response('unavailable', { status: 404 })); await pending.promise }) + expect(bridge.prepare).not.toHaveBeenCalled() + expect(bridge.start).not.toHaveBeenCalled() +}) + +function DropTarget({ receive }: { receive: (file: File | undefined) => void }) { + const read = useImageDrop() + return
{ event.preventDefault(); receive(read(event.dataTransfer)) }} /> +} + +it('resolves only the active application drag token and clears it after dragging ends', () => { + const file = original() + const receive = vi.fn() + render() + const dataTransfer = transfer() + fireEvent.dragStart(screen.getByRole('img'), { dataTransfer }) + fireEvent.drop(screen.getByTestId('drop'), { dataTransfer }) + expect(receive).toHaveBeenLastCalledWith(file) + fireEvent.dragEnd(screen.getByRole('img'), { dataTransfer }) + fireEvent.drop(screen.getByTestId('drop'), { dataTransfer }) + expect(receive).toHaveBeenLastCalledWith(undefined) +}) diff --git a/packages/client/ui-primitives/tests/markdown-image-entries.client.spec.tsx b/packages/client/ui-primitives/tests/markdown-image-entries.client.spec.tsx index 61626a483e..72cff3c14e 100644 --- a/packages/client/ui-primitives/tests/markdown-image-entries.client.spec.tsx +++ b/packages/client/ui-primitives/tests/markdown-image-entries.client.spec.tsx @@ -7,7 +7,7 @@ import { MarkdownText } from './markdown-test-components.tsx' import { parseGfm, parseGfmWithMath } from '../src/markdown/parse.ts' afterEach(() => { cleanup(); vi.useRealTimers(); vi.restoreAllMocks() }) -const labels = { open: 'View full image', dialog: 'Image preview', close: 'Close preview', loading: 'Loading image', failed: 'Image unavailable' } +const labels = { dragFailed: 'Image drag failed', open: 'View full image', dialog: 'Image preview', close: 'Close preview', loading: 'Loading image', failed: 'Image unavailable' } const fileImages = { resolve: (path: string) => `https://example.com/api/file?path=${encodeURIComponent('/workspace/' + path)}`, labels } function mount(text: string, streaming = false) { const openFile = vi.fn() diff --git a/packages/client/ui-sidebar-documentpreview/README.i18n.yaml b/packages/client/ui-sidebar-documentpreview/README.i18n.yaml index 97adbc6c21..88793e2a34 100644 --- a/packages/client/ui-sidebar-documentpreview/README.i18n.yaml +++ b/packages/client/ui-sidebar-documentpreview/README.i18n.yaml @@ -21,8 +21,8 @@ en: 57942270e23d3a8e zh: 6617124e652b3f67 /deepseek-ai-dsh-client-ui-sidebar-documentpreview/how-it-reads: - en: b056bbe0f74fe328 - zh: fd9ac88f08825afc + en: e197303166b5a86f + zh: 8d151e49321d67aa /deepseek-ai-dsh-client-ui-sidebar-documentpreview/excel-preview: en: b99b0e0b8efc2853 zh: 4327352d1334fc6b diff --git a/packages/client/ui-sidebar-documentpreview/README.md b/packages/client/ui-sidebar-documentpreview/README.md index 43ac41c129..31e5e58923 100644 --- a/packages/client/ui-sidebar-documentpreview/README.md +++ b/packages/client/ui-sidebar-documentpreview/README.md @@ -59,7 +59,7 @@ The body reads its record, navigation and lifetime through `useTabInfo().tab`. ` When Coding Tools is enabled, HTML fills the body edge to edge in a Blob iframe with exactly `sandbox="allow-scripts"`, without `allow-same-origin`; scripts cannot access the parent application's origin or file reader. The renderer loads directly declared relative `.js` classic scripts and `.css` stylesheets through its injected Remote callback, with fixed safety limits of 4 MiB per asset, 32 MiB total, and 64 distinct assets. Host code resolves the related path, and `readBytes` with `baseFile` returns native bytes. The dependency Resource is added after the read returns, using its `absolutePath` or a string `error.details.path` on failure; without a Host path, the Client does not guess one. Inside the renderer, base64 is used only to embed the iframe bootstrap payload in script text. A `` leaves dependency resolution to the browser, as do HTTPS resources. Local module imports, CSS `url()`/`@import`, and dynamic `fetch` do not use Host file access. Read failures, invalid UTF-8, or exceeded limits fail the preview rather than publishing a partial asset package. Replacing or unmounting the document releases its Blob URL. -PNG, JPEG, GIF, WebP, BMP, ICO, and SVG render through Blob URLs in an `` static-image context, rounded inside a 12px inset. Images default to fit width without enlarging content smaller than the pane; 100% uses the image's intrinsic CSS-pixel width. The shared zoom controls can produce horizontal and vertical scrolling, but do not provide drag-to-pan. Zoom changes retain the same `` and Blob URL, so animated images continue playing. Raster formats can soften above their intrinsic size, while SVG remains on the browser's vector rendering path. SVG markup never enters the application DOM or an iframe, so its scripts cannot execute or reach the parent page. Replacing or unmounting the image revokes its Blob URL. +PNG, JPEG, GIF, WebP, BMP, ICO, and SVG render through Blob URLs in an `` static-image context, rounded inside a 12px inset. Images default to fit width without enlarging content smaller than the pane; 100% uses the image's intrinsic CSS-pixel width. When either axis overflows the viewport, left-drag pans the image with grab/grabbing cursors. A fully contained image instead drags its original file into the composer or an external application; the gesture mode remains fixed until release. Ctrl+wheel and trackpad pinch retain the shared zoom behavior. Zoom changes retain the same `` and Blob URL, so animated images continue playing. Raster formats can soften above their intrinsic size, while SVG remains on the browser's vector rendering path. SVG markup never enters the application DOM or an iframe, so its scripts cannot execute or reach the parent page. Replacing or unmounting the image revokes its Blob URL. Shared copy comes from `sidebarDocumentPreview`; each builtin renderer owns its localized labels. PDF and converted Office previews use a cool light-grey background in light mode and a matte-black background in dark mode, with subtle page shadows and original document colors. PDF and image previews share a floating control with **Fit width**, 25%, 50%, 100%, 150%, and 200% choices plus 25% incremental zoom across the 25%–400% fixed range. Fit width is the default and tracks pane resizing; 100% means the PDF's 96-DPI page size or the image's intrinsic CSS-pixel size. On fine pointers, the control starts hidden, slides up when the pointer enters the bottom 72 CSS pixels, and slides down after a 420 ms leave delay; hover, keyboard focus, an open menu, and an active gesture keep it visible, while devices without hover keep it visible continuously. Chromium maps a macOS trackpad pinch to the same live, pointer-anchored path as Ctrl+wheel; the control displays the gesture percentage, and the settled value survives body remounts until the tab closes. HTML previews do not expose zoom controls. After zoom settles or fit width changes, nearby PDF pages redraw at the resolved scale multiplied by the device pixel ratio. The previous bitmap and selectable text stay visible until the replacement bitmap is ready; offscreen pages refresh when they approach the viewport. diff --git a/packages/client/ui-sidebar-documentpreview/README.zh.md b/packages/client/ui-sidebar-documentpreview/README.zh.md index daaf6288ab..c6f61e9db3 100644 --- a/packages/client/ui-sidebar-documentpreview/README.zh.md +++ b/packages/client/ui-sidebar-documentpreview/README.zh.md @@ -59,7 +59,7 @@ Web 和桌面端均通过代码工作工具选择 HTML 预览策略。渲染器 开启代码工作工具时,HTML 以贴合正文四边的 Blob iframe 运行,沙箱属性严格为 `sandbox="allow-scripts"`,不含 `allow-same-origin`;脚本无法访问父应用的源或文件读取接口。渲染器通过注入的 Remote 回调,加载直接声明的相对 `.js` 经典脚本和 `.css` 样式表;固定安全上限为单个资源 4 MiB、总计 32 MiB、64 个不同资源。Host 代码解析关联路径,带 `baseFile` 的 `readBytes` 返回原生字节。依赖 Resource 在读取返回后加入,使用返回的 `absolutePath`,失败时则使用字符串类型的 `error.details.path`;没有 Host 路径时,Client 不自行猜测。在渲染器内部,base64 仅用于把 iframe 引导载荷嵌入脚本文本。`` 将依赖解析交给浏览器,HTTPS 资源也由浏览器处理。本地模块 import、CSS `url()`/`@import` 和动态 `fetch` 不使用 Host 文件访问。读取失败、无效 UTF-8 或超出上限都使预览失败,不发布部分资源包。替换或卸载文档会释放其 Blob URL。 -PNG、JPEG、GIF、WebP、BMP、ICO 和 SVG 通过 Blob URL 在 `` 静态图片上下文中渲染,带 12px 内边距和圆角。图片默认适应宽度,但不会放大小于面板的内容;100% 使用图片的固有 CSS 像素宽度。共享缩放控件可产生横向和纵向滚动,但不提供拖拽平移。缩放不会替换 `` 或 Blob URL,因此动画图片会继续播放。位图超过固有尺寸后可能变虚,SVG 则继续使用浏览器的矢量渲染路径。SVG 标记绝不进入应用 DOM 或 iframe,因此其中的脚本无法执行,也无法访问父页面。替换或卸载图片会撤销其 Blob URL。 +PNG、JPEG、GIF、WebP、BMP、ICO 和 SVG 通过 Blob URL 在 `` 静态图片上下文中渲染,带 12px 内边距和圆角。图片默认适应宽度,但不会放大小于面板的内容;100% 使用图片的固有 CSS 像素宽度。图片在任一方向超出视口时,按住鼠标左键拖动可平移,指针显示为抓手或抓取状态。图片完全位于视口内时,拖动会将原文件交给输入框或外部应用;一次手势的模式保持到松开为止。Ctrl+滚轮和触控板捏合沿用共享缩放行为。缩放不会替换 `` 或 Blob URL,因此动画图片会继续播放。位图超过固有尺寸后可能变虚,SVG 则继续使用浏览器的矢量渲染路径。SVG 标记绝不进入应用 DOM 或 iframe,因此其中的脚本无法执行,也无法访问父页面。替换或卸载图片会撤销其 Blob URL。 共享文案来自 `sidebarDocumentPreview`;各内置渲染器拥有自己的本地化标签。PDF 与转换后的 Office 预览在浅色模式下使用冷调浅灰底色,在深色模式下使用哑黑底色,页面带有轻微阴影并保留文档原色。PDF 与图片预览共用浮动控件,提供**适应宽度**、25%、50%、100%、150% 和 200% 选项,并在 25%–400% 固定范围内按 25% 逐级缩放。适应宽度是默认模式并跟随面板尺寸;100% 表示 PDF 的 96 DPI 页面尺寸或图片的固有 CSS 像素尺寸。在精细指针设备上,控件初始隐藏;指针进入底部 72 个 CSS 像素时向上滑入,离开 420 ms 后向下退出;控件悬停、键盘焦点、菜单展开和手势进行期间保持显示,无 hover 的设备则始终显示。Chromium 将 macOS 触控板捏合映射到与 Ctrl+滚轮相同的实时指针锚定路径;控件同步显示手势比例,静止后的值在正文重新挂载后继续保留,直至 tab 关闭。HTML 预览不显示缩放控件。缩放稳定或适应宽度发生变化后,视口附近的 PDF 页面按最终缩放比例乘以设备像素比重绘。新位图完成前保留原有画面和可选文字层;屏幕外的页面在接近视口时更新。 diff --git a/packages/client/ui-sidebar-documentpreview/src/client/image/ImageBody.module.css b/packages/client/ui-sidebar-documentpreview/src/client/image/ImageBody.module.css index 414054d2aa..422f480d64 100644 --- a/packages/client/ui-sidebar-documentpreview/src/client/image/ImageBody.module.css +++ b/packages/client/ui-sidebar-documentpreview/src/client/image/ImageBody.module.css @@ -52,3 +52,6 @@ line-height: 1.5; white-space: normal; } + +.image[data-pan="available"] { cursor: grab; } +.image[data-pan="active"] { cursor: grabbing; } diff --git a/packages/client/ui-sidebar-documentpreview/src/client/image/ImageBody.tsx b/packages/client/ui-sidebar-documentpreview/src/client/image/ImageBody.tsx index 770c806bd5..8da0834551 100644 --- a/packages/client/ui-sidebar-documentpreview/src/client/image/ImageBody.tsx +++ b/packages/client/ui-sidebar-documentpreview/src/client/image/ImageBody.tsx @@ -1,5 +1,7 @@ /** Complete image bytes rendered in a shared zoom viewport. */ -import { useCallback, useEffect, useMemo, useState, type CSSProperties, type ReactNode } from 'react' +import { useCallback, useEffect, useMemo, useRef, useState, type CSSProperties, type ReactNode } from 'react' +import { useImageDrag } from '@deepseek-ai/dsh-client-ui-primitives' +import { bindImagePan } from './pan.ts' import type { PropsLocale, PropsStore } from '@deepseek-ai/dsh-client-ui-slots' import { pathPartsOf } from '@deepseek-ai/dsh-util-workspace-path' import type { DocumentPreviewProps } from '../document/contract.ts' @@ -33,6 +35,7 @@ type ImageSource = readonly data: Uint8Array readonly mediaType: ImageMediaType readonly url: string + readonly file: File } | { readonly kind: 'failed'; readonly data: Uint8Array; readonly mediaType: ImageMediaType } @@ -72,14 +75,14 @@ export function ImageBody(props: ImageBodyProps): ReactNode { let url: string | undefined try { url = URL.createObjectURL(new Blob([data], { type: mediaType })) - setSource({ kind: 'ready', data, mediaType, url }) + setSource({ kind: 'ready', data, mediaType, url, file: new File([data], pathPartsOf(path).name, { type: mediaType }) }) } catch { setSource({ kind: 'failed', data, mediaType }) } return () => { if (url !== undefined) URL.revokeObjectURL(url) } - }, [data, mediaType]) + }, [data, mediaType, path]) if (data === undefined || mediaType === undefined) { return

{t('unsupported')}

@@ -93,15 +96,16 @@ export function ImageBody(props: ImageBodyProps): ReactNode { controls: t('zoomControls'), menu: t('zoomMenu'), out: t('zoomOut'), into: t('zoomIn'), fitWidth: t('zoomFitWidth'), value: percent => t('zoomValue', { percent }), } - return } /** SVG stays in the browser's static image mode because its bytes only reach an img Blob URL. */ -function LoadedImage({ url, name, preference, onPreference, labels, signal, scrollportRef, t }: { +function LoadedImage({ url, name, file, preference, onPreference, labels, signal, scrollportRef, t }: { readonly url: string readonly name: string + readonly file: File readonly preference: ZoomPreference readonly onPreference: (preference: ZoomPreference) => void readonly labels: ZoomLabels @@ -109,6 +113,12 @@ function LoadedImage({ url, name, preference, onPreference, labels, signal, scro readonly scrollportRef: ImageBodyProps['scrollportRef'] readonly t: ImageBodyProps['t'] }): ReactNode { + const drag = useImageDrag(url, name, t('dragFailed'), file) + const releasePan = useRef<() => void>() + const attachImage = useCallback((image: HTMLImageElement | null): void => { + releasePan.current?.() + releasePan.current = image === null ? undefined : bindImagePan(image) + }, []) const [state, setState] = useState<'loading' | 'ready' | 'failed'>('loading') const [width, setWidth] = useState() return