From 548ba48c324dbb6daf909aefcf7c52da0f819a4a Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Fri, 6 Nov 2026 16:03:20 -0800 Subject: [PATCH] Upload images before publishing GitHub drafts (#38928) * Upload GitHub images when pasted or dropped Keep image bytes out of issue and review bodies by uploading from the owning composer and inserting hosted attachment URLs. Co-authored-by: lydiassssz <60514233+lydiassssz@users.noreply.github.com> * Preserve GitHub image intake across editor and thread states * Keep image placeholder boundaries and ordinary HTML paste intact * Match the native replace overload in the image projection fixture * Use typed element queries in the image intake fixture * Narrow textarea elements at the fixture boundary * Show failed GitHub draft images as filename links * Keep GitHub draft attachment links current across updates * Supply the repository address in the attachment preview fixture --------- Co-authored-by: lydiassssz <60514233+lydiassssz@users.noreply.github.com> --- .../agent-browser-bridge-test-harness.ts | 9 +- .../exec-file-capture-stdin.test.ts | 16 + .../git/command-runner/exec-file-capture.ts | 2 +- src/main/git/command-runner/gh-exec-file.ts | 3 +- .../github/image-attachment-upload.test.ts | 67 +++++ src/main/github/image-attachment-upload.ts | 94 ++++++ src/main/github/issue-create.ts | 2 +- src/main/github/issues.test.ts | 9 +- .../runtime-file-command-state.ts | 1 + ...time-github-issue-comment-commands.test.ts | 52 ++++ .../runtime-github-issue-comment-commands.ts | 60 ++++ .../rpc/methods/clipboard/clipboard.ts | 13 + .../github/github-issue-methods.ts | 13 + .../runtime-rpc-mobile-method-routing.ts | 1 + .../runtime/runtime-review-command-surface.ts | 2 + src/packages/process-host/src/process-spec.ts | 2 +- .../diff-comments/DiffCommentDraftCard.tsx | 47 ++- .../diff-comments/diff-comment-draft-zone.ts | 11 +- .../diff-comments/diff-comment-zone-card.tsx | 4 + .../diff-comments/useDiffCommentDecorator.tsx | 4 + .../editor/diff/sections/DiffSectionItem.tsx | 2 + .../diff/sections/diff-section-item-props.ts | 2 + .../images/rich-markdown-image-error-link.ts | 45 +++ .../schema/rich-markdown-extensions.ts | 11 +- .../discuss-item/comment-reply-form.tsx | 16 +- .../conversation-tab-activity.tsx | 2 + .../conversation-tab-comment-card.tsx | 10 + .../conversation-tab-description.tsx | 19 +- .../discuss-item/conversation-tab.tsx | 21 ++ .../discuss-item/gh-comment-composer.tsx | 16 +- .../pr-files-combined-diff-body.tsx | 4 + .../pr-files-combined-diff-viewer.tsx | 4 + .../components/github/GitHubImageTextarea.tsx | 46 +++ .../github/GitHubMarkdownComposer.test.tsx | 277 ++++++++++++++++++ .../github/GitHubMarkdownComposer.tsx | 83 ++---- .../github-comment-image-upload-address.ts | 26 ++ .../github-creation-image-upload-address.ts | 33 +++ .../github/github-image-upload.test.ts | 114 +++++++ .../components/github/github-image-upload.ts | 85 ++++++ .../github/github-markdown-image-input.tsx | 69 +++++ .../github/use-github-image-paste.ts | 144 +++++++++ .../github/use-github-image-uploads.ts | 118 ++++++++ .../use-github-textarea-images.test.tsx | 199 +++++++++++++ .../github/use-github-textarea-images.ts | 201 +++++++++++++ .../pull-request-page/comments/composer.tsx | 20 +- .../pull-request-page/comments/reply-form.tsx | 20 +- .../conversation/comment-card.tsx | 10 + .../conversation/comment-group.tsx | 3 + .../conversation/comments-list.tsx | 1 + .../conversation/description.tsx | 21 +- .../pull-request-page/conversation/tab.tsx | 21 ++ .../files/combined-diff-viewer.tsx | 10 + .../pull-request-page/mentions/textarea.tsx | 20 +- .../checks-panel/comments/comment-group.tsx | 13 + .../checks-panel/comments/comment-row.tsx | 25 +- .../checks-panel/comments/comments-list.tsx | 2 + .../pr-comments-list-selection.test.tsx | 41 +++ .../comments/right-panel-comment-composer.tsx | 60 +++- .../comments/use-comments-list-state.tsx | 8 + .../presentation/active-content.tsx | 35 +++ .../presentation/checks-panel-empty-state.ts | 22 ++ .../presentation/empty-content-props.ts | 4 + .../presentation/empty-content.tsx | 21 +- .../use-checks-panel-create-review.tsx | 13 +- .../use-checks-panel-composer-state.tsx | 6 + .../creation/CreateHostedReviewComposer.tsx | 7 + .../CreateHostedReviewComposerFields.tsx | 9 +- .../useCreatePullRequestDialogFields.ts | 5 + .../hosted-review-create-failure-copy.ts | 10 + .../source-control/panel/commit-surface.tsx | 8 + .../review/create-pr-intent-flow.ts | 28 ++ .../review/use-create-pr-intent-flows.ts | 1 + .../use-create-pr-intent-jira-title.test.tsx | 14 +- .../review/use-create-pr-intent-review.ts | 64 ++-- .../review/use-create-review-composer.ts | 6 + .../review/use-hosted-review-creation.ts | 40 +-- .../source-control/review/use-review-flows.ts | 1 + .../task-page/github/IssueDialog.tsx | 40 ++- .../src/hooks/use-os-file-drop-owner.test.tsx | 28 ++ .../src/hooks/use-os-file-drop-owner.ts | 66 +++-- src/renderer/src/i18n/locales/en.json | 6 +- src/shared/github/image-attachment.ts | 24 ++ .../rpc-contract/github-attachment-params.ts | 18 ++ .../rpc-params-catalog.generated.ts | 2 + .../task-source-runtime-capabilities.ts | 2 + 85 files changed, 2498 insertions(+), 216 deletions(-) create mode 100644 src/main/github/image-attachment-upload.test.ts create mode 100644 src/main/github/image-attachment-upload.ts create mode 100644 src/main/runtime/integrations/github/runtime-github-issue-comment-commands.test.ts create mode 100644 src/renderer/src/components/editor/rich-markdown/images/rich-markdown-image-error-link.ts create mode 100644 src/renderer/src/components/github/GitHubImageTextarea.tsx create mode 100644 src/renderer/src/components/github/github-comment-image-upload-address.ts create mode 100644 src/renderer/src/components/github/github-creation-image-upload-address.ts create mode 100644 src/renderer/src/components/github/github-image-upload.test.ts create mode 100644 src/renderer/src/components/github/github-image-upload.ts create mode 100644 src/renderer/src/components/github/github-markdown-image-input.tsx create mode 100644 src/renderer/src/components/github/use-github-image-paste.ts create mode 100644 src/renderer/src/components/github/use-github-image-uploads.ts create mode 100644 src/renderer/src/components/github/use-github-textarea-images.test.tsx create mode 100644 src/renderer/src/components/github/use-github-textarea-images.ts create mode 100644 src/shared/github/image-attachment.ts create mode 100644 src/shared/rpc-contract/github-attachment-params.ts diff --git a/src/main/browser/agent-browser/agent-browser-bridge-test-harness.ts b/src/main/browser/agent-browser/agent-browser-bridge-test-harness.ts index 7869d3b27ec..79a2463fe79 100644 --- a/src/main/browser/agent-browser/agent-browser-bridge-test-harness.ts +++ b/src/main/browser/agent-browser/agent-browser-bridge-test-harness.ts @@ -140,9 +140,14 @@ export function createFakeAgentBrowserChild(fields: T): EventE } /** The agent-browser command a spawn ran: batch commands arrive on stdin, raw CLI tokens in argv. */ -export function agentBrowserCommandOf(args: readonly string[], stdin?: string): string[] { +export function agentBrowserCommandOf( + args: readonly string[], + stdin?: string | Uint8Array +): string[] { if (stdin !== undefined && args.includes('batch')) { - const [command]: string[][] = JSON.parse(stdin) + const [command]: string[][] = JSON.parse( + typeof stdin === 'string' ? stdin : Buffer.from(stdin).toString('utf8') + ) return command } let start = 0 diff --git a/src/main/git/command-runner/exec-file-capture-stdin.test.ts b/src/main/git/command-runner/exec-file-capture-stdin.test.ts index ec891f1be9e..22e3e2794b8 100644 --- a/src/main/git/command-runner/exec-file-capture-stdin.test.ts +++ b/src/main/git/command-runner/exec-file-capture-stdin.test.ts @@ -32,3 +32,19 @@ describe.each([ } ) }) + +it('delivers non-UTF8 image bytes unchanged through the termination barrier', async () => { + const result = await execFileCaptureToTermination( + process.execPath, + [ + '-e', + 'const chunks = []; process.stdin.on("data", c => chunks.push(c)); process.stdin.on("end", () => process.stdout.write(Buffer.concat(chunks).toString("hex")))' + ], + { + encoding: 'utf8', + timeout: 3_000, + stdin: new Uint8Array([0, 128, 255]) + } + ) + expect(result).toEqual({ stdout: '0080ff', stderr: '' }) +}) diff --git a/src/main/git/command-runner/exec-file-capture.ts b/src/main/git/command-runner/exec-file-capture.ts index f03e69737f4..89edb391bd3 100644 --- a/src/main/git/command-runner/exec-file-capture.ts +++ b/src/main/git/command-runner/exec-file-capture.ts @@ -30,7 +30,7 @@ export async function execFileCaptureToTermination( command: string, args: string[], // Why: runProcess owns this child's streams; git-exec-file refuses onStderr with a barrier. - options: Omit, + options: Omit & { stdin?: string | Uint8Array }, termination?: WslProcessGroupTermination ): Promise<{ stdout: string | Buffer; stderr: string | Buffer }> { // runProcess already reports spawn cost through spawnProcess. diff --git a/src/main/git/command-runner/gh-exec-file.ts b/src/main/git/command-runner/gh-exec-file.ts index 7de2babf9b3..dcc7a3c67d0 100644 --- a/src/main/git/command-runner/gh-exec-file.ts +++ b/src/main/git/command-runner/gh-exec-file.ts @@ -38,7 +38,8 @@ import { // cwd is optional; wslDistro routes global calls when Windows has no host gh binary. // Retry only idempotent calls: a failed response can follow a completed write. -export type GhExecOptions = Omit & { +export type GhExecOptions = Omit & { + stdin?: string | Uint8Array cwd?: string wslDistro?: string idempotent?: boolean diff --git a/src/main/github/image-attachment-upload.test.ts b/src/main/github/image-attachment-upload.test.ts new file mode 100644 index 00000000000..ac06daecb61 --- /dev/null +++ b/src/main/github/image-attachment-upload.test.ts @@ -0,0 +1,67 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { uploadGitHubImage } from './image-attachment-upload' + +const { gh, acquire, release } = vi.hoisted(() => ({ + gh: vi.fn(), + acquire: vi.fn(), + release: vi.fn() +})) +vi.mock('./gh-utils', () => ({ + ghExecFileAsync: gh, + acquire, + release, + extractExecError: (error: unknown) => ({ stdout: '', stderr: String(error) }) +})) +const repository = { + ownerRepo: { owner: 'octo', repo: 'images', host: 'github.com' }, + ghOptions: { host: 'github.com' } +} +beforeEach(() => { + vi.resetAllMocks() +}) +describe('GitHub image attachment upload', () => { + it('posts unchanged binary bytes with their exact content length in the resolved gh context', async () => { + gh.mockResolvedValueOnce({ stdout: '{"id":123}' }).mockResolvedValueOnce({ + stdout: '{"url":"https://github.com/user-attachments/assets/a"}' + }) + await expect( + uploadGitHubImage({ + fileName: 'screen.png', + contentType: 'image/png', + bytes: Buffer.from([0, 128, 255]), + resolve: async () => repository + }) + ).resolves.toEqual({ url: 'https://github.com/user-attachments/assets/a' }) + expect(gh.mock.calls[0][0]).toEqual(['api', '--cache', '60s', 'repos/octo/images']) + expect(gh.mock.calls[1][0]).toEqual([ + 'api', + '-X', + 'POST', + 'https://uploads.github.com/user-attachments/assets?name=screen.png&content_type=image%2Fpng&repository_id=123', + '-H', + 'Content-Type: application/octet-stream', + '-H', + 'Content-Length: 3', + '--input', + '-' + ]) + expect(gh.mock.calls[1][1]).toMatchObject({ + host: 'github.com', + idempotent: false, + stdin: Buffer.from([0, 128, 255]) + }) + }) + it('reports a rejected upload without retrying the POST and releases admission', async () => { + gh.mockResolvedValueOnce({ stdout: '{"id":123}' }).mockRejectedValueOnce(new Error('HTTP 403')) + await expect( + uploadGitHubImage({ + fileName: 'screen.png', + contentType: 'image/png', + bytes: Buffer.from([1]), + resolve: async () => repository + }) + ).rejects.toThrow('selected account') + expect(gh.mock.calls.map((call) => call[0][0])).toEqual(['api', 'api']) + expect(release).toHaveBeenCalledOnce() + }) +}) diff --git a/src/main/github/image-attachment-upload.ts b/src/main/github/image-attachment-upload.ts new file mode 100644 index 00000000000..7ee97203d9c --- /dev/null +++ b/src/main/github/image-attachment-upload.ts @@ -0,0 +1,94 @@ +import { acquire, extractExecError, ghExecFileAsync, release } from './gh-utils' +import type { GitHubRepoExecution } from './github-api-repository' +import { assertGitHubImage, GitHubImageValidationError } from '../../shared/github/image-attachment' + +export async function uploadGitHubImage(args: { + fileName: string + contentType: string + bytes: Buffer + signal?: AbortSignal + resolve: (signal: AbortSignal) => Promise +}): Promise<{ url: string }> { + assertGitHubImage(args.contentType, args.bytes.byteLength) + const signal = AbortSignal.any([ + AbortSignal.timeout(120_000), + ...(args.signal ? [args.signal] : []) + ]) + let admitted = false + try { + const { ownerRepo, ghOptions } = await args.resolve(signal) + signal.throwIfAborted() + if (!ownerRepo) { + throw new GitHubImageValidationError( + 'GitHub could not find this repository. Reopen it and try again.' + ) + } + const host = ownerRepo.host?.trim().toLowerCase() || 'github.com' + if (host !== 'github.com' && !/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.ghe\.com$/.test(host)) { + throw new GitHubImageValidationError( + 'Image uploads are unavailable on this GitHub server. Attach the image in GitHub or use an image URL.' + ) + } + await acquire(signal) + admitted = true + const options = { ...ghOptions, signal, idempotent: false, timeout: 120_000 } + const { stdout } = await ghExecFileAsync( + ['api', '--cache', '60s', `repos/${ownerRepo.owner}/${ownerRepo.repo}`], + options + ) + const repository: { id?: number } = JSON.parse(stdout) + if (!Number.isSafeInteger(repository.id) || (repository.id ?? 0) <= 0) { + throw new GitHubImageValidationError( + 'GitHub could not find this repository. Reopen it and try again.' + ) + } + const endpoint = new URL(`https://uploads.${host}/user-attachments/assets`) + endpoint.search = new URLSearchParams({ + name: args.fileName, + content_type: args.contentType, + repository_id: String(repository.id) + }).toString() + const result = await ghExecFileAsync( + [ + 'api', + '-X', + 'POST', + endpoint.href, + '-H', + 'Content-Type: application/octet-stream', + '-H', + `Content-Length: ${args.bytes.byteLength}`, + '--input', + '-' + ], + { ...options, stdin: args.bytes } + ) + const asset: { url?: string } = JSON.parse(result.stdout) + if (!asset.url?.startsWith(`https://${host}/user-attachments/`)) { + throw new GitHubImageValidationError( + 'GitHub did not return an image URL. Paste the image again to retry.' + ) + } + return { url: asset.url } + } catch (error) { + if (error instanceof GitHubImageValidationError) { + throw error + } + const { stdout, stderr } = extractExecError(error) + const message = `${stdout}\n${stderr}` + if (/\b(?:401|403|404)\b/.test(message)) { + throw new Error( + 'GitHub could not upload this image with the selected account. Attach it in GitHub or use an image URL.' + ) + } + throw new Error( + signal.aborted + ? 'The GitHub image upload timed out or was interrupted. Paste the image again to retry.' + : 'GitHub could not upload this image. Check your connection and paste it again to retry.' + ) + } finally { + if (admitted) { + release() + } + } +} diff --git a/src/main/github/issue-create.ts b/src/main/github/issue-create.ts index 4579e04365f..54f5bb74ac3 100644 --- a/src/main/github/issue-create.ts +++ b/src/main/github/issue-create.ts @@ -13,7 +13,7 @@ import { codeHostFailure } from '../../shared/code-host-failure-copy' function githubIssueErrorMessage(error: unknown): string { const { stderr, stdout } = extractExecError(error) - return stderr.trim() || stdout.trim() + return [stderr.trim(), stdout.trim()].filter(Boolean).join('\n') } /** Explicit owner/repo preserves the Tasks-selected source independently of cwd remotes. */ diff --git a/src/main/github/issues.test.ts b/src/main/github/issues.test.ts index c57e06a235c..55862e1f0a1 100644 --- a/src/main/github/issues.test.ts +++ b/src/main/github/issues.test.ts @@ -89,7 +89,7 @@ describe('issue source operations', () => { })) }) - it('recovers issue 7704 oversized inline-image creation', async () => { + it.each(['stderr', 'stdout'])('recovers a long issue body reported on %s', async (stream) => { const imagePrefix = 'data:image/png;base64,' const body = imagePrefix + 'x'.repeat(133596 - imagePrefix.length) expect(body).toContain('data:image') @@ -97,7 +97,12 @@ describe('issue source operations', () => { getIssueOwnerRepoMock.mockResolvedValueOnce({ owner: 'stablyai', repo: 'orca' }) ghExecFileAsyncMock - .mockRejectedValueOnce(new Error('HTTP 422: body is too long (maximum is 65536 characters)')) + .mockRejectedValueOnce( + Object.assign(new Error('gh failed'), { + stderr: 'gh: Validation Failed (HTTP 422)', + [stream]: 'body is too long (maximum is 65536 characters)' + }) + ) .mockResolvedValueOnce({ stdout: JSON.stringify({ number: 926, diff --git a/src/main/runtime/construction/runtime-file-command-state.ts b/src/main/runtime/construction/runtime-file-command-state.ts index 83c154dd604..ed17aa5176d 100644 --- a/src/main/runtime/construction/runtime-file-command-state.ts +++ b/src/main/runtime/construction/runtime-file-command-state.ts @@ -295,6 +295,7 @@ export abstract class OrcaRuntimeWithFileCommands extends OrcaRuntimeWithPreserv }) protected readonly gitHubIssueComments = new RuntimeGitHubIssueCommentCommands({ + resolveTarget: (args) => this.resolveHostedReviewTarget(args), resolveRepo: (selector) => this.resolveRepoSelector(selector), getLocalGitArgs: (repo) => this.getLocalGitExecutionOptionArgs(repo) }) diff --git a/src/main/runtime/integrations/github/runtime-github-issue-comment-commands.test.ts b/src/main/runtime/integrations/github/runtime-github-issue-comment-commands.test.ts new file mode 100644 index 00000000000..03943814ab8 --- /dev/null +++ b/src/main/runtime/integrations/github/runtime-github-issue-comment-commands.test.ts @@ -0,0 +1,52 @@ +import { expect, it, vi } from 'vitest' +import type * as GitHubApiRepository from '../../../github/github-api-repository' +import { RuntimeGitHubIssueCommentCommands } from './runtime-github-issue-comment-commands' +const { resolve, upload } = vi.hoisted(() => ({ resolve: vi.fn(), upload: vi.fn() })) +vi.mock('../../../github/image-attachment-upload', () => ({ uploadGitHubImage: upload })) +vi.mock('../../../github/github-api-repository', async (original) => ({ + ...(await original()), + resolveGitHubRepoExecution: resolve +})) +it('resolves the addressed worktree with the existing SSH/WSL gh execution options at upload time', async () => { + const repo = { + id: 'repo', + path: '/root', + connectionId: 'ssh-1', + executionHostId: 'ssh:ssh-1', + displayName: '', + badgeColor: '', + addedAt: 1 + } + const resolveTarget = vi.fn().mockResolvedValue({ repo, repoPath: '/linked/worktree' }) + resolve.mockResolvedValue({ + ownerRepo: { owner: 'octo', repo: 'images' }, + ghOptions: { connectionId: 'ssh-1' } + }) + upload.mockImplementation(async (args) => { + await args.resolve(new AbortController().signal) + return { url: 'https://github.com/user-attachments/assets/a' } + }) + const runtime = new RuntimeGitHubIssueCommentCommands({ + resolveRepo: vi.fn(), + resolveTarget, + getLocalGitArgs: () => [{ wslDistro: 'Ubuntu' }] + }) + await runtime.uploadGitHubAttachment( + { + target: { kind: 'repo', repo: 'repo', worktree: 'path:/linked/worktree', source: 'origin' }, + fileName: 'a.png', + contentType: 'image/png' + }, + Buffer.from([1]) + ) + expect(resolveTarget).toHaveBeenCalledExactlyOnceWith({ + repoSelector: 'repo', + worktreeSelector: 'path:/linked/worktree' + }) + expect(resolve).toHaveBeenCalledWith( + '/linked/worktree', + undefined, + 'ssh-1', + expect.objectContaining({ wslDistro: 'Ubuntu' }) + ) +}) diff --git a/src/main/runtime/integrations/github/runtime-github-issue-comment-commands.ts b/src/main/runtime/integrations/github/runtime-github-issue-comment-commands.ts index 553180ef57b..3735a5c2c6e 100644 --- a/src/main/runtime/integrations/github/runtime-github-issue-comment-commands.ts +++ b/src/main/runtime/integrations/github/runtime-github-issue-comment-commands.ts @@ -15,17 +15,77 @@ import { import { createIssue } from '../../../github/issue-create' import { updateIssue } from '../../../github/issue-update' import { getRepoRoutingConnectionId } from '../../../../shared/execution-host' +import type { GitHubAttachmentTarget } from '../../../../shared/rpc-contract/github-attachment-params' +import { uploadGitHubImage } from '../../../github/image-attachment-upload' +import { + resolveGitHubRepoExecution, + resolveIssueGitHubApiRepositorySource +} from '../../../github/github-api-repository' +import { projectGhExecOptions } from '../../../github/project-view/internals' +import { runAsDefaultWslCliUserAct } from '../../../git/command-runner/default-wsl-cli-fallback' type LocalGitArgs = [] | [LocalProjectGhExecOptions] type RuntimeGitHubIssueCommentCommandsDeps = { resolveRepo: (selector: string) => Promise + resolveTarget: (args: { + repoSelector: string + worktreeSelector?: string + }) => Promise<{ repo: Repo; repoPath: string }> getLocalGitArgs: (repo: Repo) => LocalGitArgs } export class RuntimeGitHubIssueCommentCommands { constructor(private readonly deps: RuntimeGitHubIssueCommentCommandsDeps) {} + async uploadGitHubAttachment( + args: { target: GitHubAttachmentTarget; fileName: string; contentType: string }, + bytes: Buffer, + signal?: AbortSignal + ): Promise<{ url: string }> { + const target = args.target + const upload = () => + uploadGitHubImage({ + ...args, + bytes, + signal, + resolve: async (uploadSignal) => { + if (target.kind === 'slug') { + return { + ownerRepo: target, + ghOptions: { ...projectGhExecOptions(target.host), signal: uploadSignal } + } + } + const { repo, repoPath } = await this.deps.resolveTarget({ + repoSelector: target.repo, + worktreeSelector: target.worktree + }) + const connectionId = getRepoRoutingConnectionId(repo) + const options = { ...this.deps.getLocalGitArgs(repo)[0], signal: uploadSignal } + return resolveGitHubRepoExecution( + repoPath, + target.ownerRepo ?? + (target.source === 'issue' + ? async () => + ( + await resolveIssueGitHubApiRepositorySource( + repoPath, + repo.issueSourcePreference, + connectionId, + options + ) + ).source + : undefined), + connectionId, + options + ) + } + }) + return target.kind === 'slug' && target.wakeWslDistro + ? runAsDefaultWslCliUserAct(upload) + : upload() + } + async createRepoIssue( repoSelector: string, title: string, diff --git a/src/main/runtime/rpc/methods/clipboard/clipboard.ts b/src/main/runtime/rpc/methods/clipboard/clipboard.ts index 47cbfd0e8c8..0b13863a3ab 100644 --- a/src/main/runtime/rpc/methods/clipboard/clipboard.ts +++ b/src/main/runtime/rpc/methods/clipboard/clipboard.ts @@ -89,6 +89,19 @@ function assertMobileUploadOwner( return clientId } +export function claimClipboardImageUpload(uploadId: string, ctx: RpcContext): Buffer { + const upload = getUpload(uploadId) + assertMobileUploadOwner(upload, ctx) + try { + if (upload.receivedBase64Length !== upload.expectedBase64Length) { + throw new Error('Clipboard image upload is incomplete') + } + return decodeClipboardImageUpload(upload.chunks) + } finally { + deleteUpload(uploadId) + } +} + export const CLIPBOARD_METHODS = [ defineMethod({ name: 'clipboard.saveImageAsTempFile', diff --git a/src/main/runtime/rpc/methods/source-control/github/github-issue-methods.ts b/src/main/runtime/rpc/methods/source-control/github/github-issue-methods.ts index 4c59cc36a91..421e643d0a6 100644 --- a/src/main/runtime/rpc/methods/source-control/github/github-issue-methods.ts +++ b/src/main/runtime/rpc/methods/source-control/github/github-issue-methods.ts @@ -1,4 +1,6 @@ import { defineMethod } from '../../../core' +import { UploadGitHubAttachment } from '../../../../../../shared/rpc-contract/github-attachment-params' +import { claimClipboardImageUpload } from '../../clipboard/clipboard' import { CreateIssue, Issue, @@ -7,6 +9,17 @@ import { } from '../../../../../../shared/rpc-contract/github-issue-params' export const GITHUB_ISSUE_METHODS = [ + defineMethod({ + name: 'github.uploadAttachment', + permission: 'workspace', + params: UploadGitHubAttachment, + handler: (params, ctx) => + ctx.runtime.uploadGitHubAttachment( + params, + claimClipboardImageUpload(params.uploadId, ctx), + ctx.signal + ) + }), defineMethod({ name: 'github.issue', permission: 'workspace', diff --git a/src/main/runtime/rpc/server/runtime-rpc-mobile-method-routing.ts b/src/main/runtime/rpc/server/runtime-rpc-mobile-method-routing.ts index f419583f6e7..854efdbe03b 100644 --- a/src/main/runtime/rpc/server/runtime-rpc-mobile-method-routing.ts +++ b/src/main/runtime/rpc/server/runtime-rpc-mobile-method-routing.ts @@ -102,6 +102,7 @@ export const EXECUTION_HOST_METHODS = [ 'github.addPRReviewCommentReply', 'github.countWorkItems', 'github.createIssue', + 'github.uploadAttachment', 'github.listAssignableUsers', 'github.listLabels', 'github.listWorkItems', diff --git a/src/main/runtime/runtime-review-command-surface.ts b/src/main/runtime/runtime-review-command-surface.ts index 11b4b1bc6a8..4b721f10b71 100644 --- a/src/main/runtime/runtime-review-command-surface.ts +++ b/src/main/runtime/runtime-review-command-surface.ts @@ -30,6 +30,7 @@ type GitHubReviewMutationName = | 'removeRepoPRReviewers' type GitHubIssueCommentName = | 'createRepoIssue' + | 'uploadGitHubAttachment' | 'updateRepoIssue' | 'addRepoIssueComment' | 'addRepoPRReviewComment' @@ -103,6 +104,7 @@ export function installRuntimeReviewCommandSurface( requestRepoPRReviewers: ghm.requestRepoPRReviewers.bind(ghm), removeRepoPRReviewers: ghm.removeRepoPRReviewers.bind(ghm), createRepoIssue: comments.createRepoIssue.bind(comments), + uploadGitHubAttachment: comments.uploadGitHubAttachment.bind(comments), updateRepoIssue: comments.updateRepoIssue.bind(comments), addRepoIssueComment: comments.addRepoIssueComment.bind(comments), addRepoPRReviewComment: comments.addRepoPRReviewComment.bind(comments), diff --git a/src/packages/process-host/src/process-spec.ts b/src/packages/process-host/src/process-spec.ts index 0014b86718e..1137d5f3a55 100644 --- a/src/packages/process-host/src/process-spec.ts +++ b/src/packages/process-host/src/process-spec.ts @@ -17,7 +17,7 @@ export type ProcessSpec = { env?: NodeJS.ProcessEnv timeoutMs?: number | null /** Written to stdin then closed. Omit to leave stdin empty and closed. */ - input?: string + input?: string | Uint8Array /** Cap on captured stdout/stderr; output past it is discarded. */ maxOutputBytes?: number /** Capture stdout as bytes without decoding; stdout stays empty in this mode. */ diff --git a/src/renderer/src/components/diff-comments/DiffCommentDraftCard.tsx b/src/renderer/src/components/diff-comments/DiffCommentDraftCard.tsx index a4b1fe2f555..885ffd134d7 100644 --- a/src/renderer/src/components/diff-comments/DiffCommentDraftCard.tsx +++ b/src/renderer/src/components/diff-comments/DiffCommentDraftCard.tsx @@ -1,3 +1,6 @@ +import { useGitHubTextareaImages } from '@/components/github/use-github-textarea-images' +import { useGitHubImagePending } from '@/components/github/use-github-image-uploads' +import type { GitHubImageIntake } from '@/components/github/use-github-image-uploads' import { ImeTextarea } from '@/lib/ime-text-field' import { useCallback, useEffect, useId, useLayoutEffect, useRef, useState } from 'react' import { CornerDownLeft } from 'lucide-react' @@ -14,6 +17,7 @@ import { installOpenDraftAddReviewNoteGuard } from '../editor/editor-shortcuts' export type DiffCommentDraftCardProps = { lineNumber: number startLine?: number + imageUploadAddress?: GitHubImageIntake['address'] placeholder?: string submitLabel?: string submittingLabel?: string @@ -34,6 +38,7 @@ function resizeDraftTextarea(textarea: HTMLTextAreaElement): boolean { export function DiffCommentDraftCard({ lineNumber, startLine, + imageUploadAddress, placeholder, submitLabel, submittingLabel, @@ -46,8 +51,22 @@ export function DiffCommentDraftCard({ const [body, setBody] = useState(initialBody) const bodyRef = useRef(body) const [submitting, setSubmitting] = useState(false) + const imagePending = useGitHubImagePending() const mountedRef = useMountedRef() const textareaRef = useRef(null) + const images = useGitHubTextareaImages( + textareaRef, + body, + (next) => { + setBody(next) + onBodyChange?.(next) + }, + { + address: imageUploadAddress, + onPending: imagePending.onPending, + enabled: !!imageUploadAddress && !submitting + } + ) const cardRef = useRef(null) const onContentResizeRef = useRef(onContentResize) @@ -73,10 +92,19 @@ export function DiffCommentDraftCard({ { value0: lineNumber } ) - const focusTextareaRef = useCallback((textarea: HTMLTextAreaElement | null): void => { - textareaRef.current = textarea - textarea?.focus() - }, []) + const { attachDrop } = images + const hasImageUpload = !!imageUploadAddress + const focusTextareaRef = useCallback( + (textarea: HTMLTextAreaElement | null): void => { + if (hasImageUpload) { + attachDrop(textarea) + } else { + textareaRef.current = textarea + } + textarea?.focus() + }, + [attachDrop, hasImageUpload] + ) useEffect(() => { const focusTextarea = (): void => textareaRef.current?.focus() @@ -134,7 +162,7 @@ export function DiffCommentDraftCard({ }, [onCancel]) const handleSubmit = async (): Promise => { - if (submitting) { + if (submitting || imagePending.pendingRef.current) { return } const bodyState = getCommentBodySubmitState(body) @@ -194,7 +222,7 @@ export function DiffCommentDraftCard({ } } - const canSubmit = !submitting && hasBoundedCommentBodyText(body) + const canSubmit = !imagePending.pending && !submitting && hasBoundedCommentBodyText(body) return (
{ - setBody(e.target.value) - onBodyChange?.(e.target.value) + images.change(e.target.value) if (resizeDraftTextarea(e.currentTarget)) { onContentResizeRef.current?.() } diff --git a/src/renderer/src/components/diff-comments/diff-comment-draft-zone.ts b/src/renderer/src/components/diff-comments/diff-comment-draft-zone.ts index a642ba8ea89..04b4cbdefa4 100644 --- a/src/renderer/src/components/diff-comments/diff-comment-draft-zone.ts +++ b/src/renderer/src/components/diff-comments/diff-comment-draft-zone.ts @@ -1,3 +1,4 @@ +import type { GitHubImageIntake } from '@/components/github/use-github-image-uploads' import { useCallback, useEffect, useRef } from 'react' import type { editor as monacoEditor } from 'monaco-editor' import { createRoot } from 'react-dom/client' @@ -28,6 +29,7 @@ export type UseDiffCommentDraftZoneArgs = { startLine?: number body: string }) => Promise + imageUploadAddress?: GitHubImageIntake['address'] draftPlaceholder?: string draftSubmitLabel?: string onAddCommentClick?: (args: { lineNumber: number; startLine?: number; top: number }) => void @@ -47,6 +49,7 @@ export function useDiffCommentDraftZone({ canOpenDraft = true, captureCreateComment = false, onCreateComment, + imageUploadAddress, draftPlaceholder, draftSubmitLabel, onAddCommentClick @@ -172,6 +175,7 @@ export function useDiffCommentDraftZone({ draftZoneRef.current = entry renderDiffCommentDraftCard(root, draft, { + imageUploadAddress, placeholder: draftPlaceholder, submitLabel: draftSubmitLabel, initialBody, @@ -224,6 +228,7 @@ export function useDiffCommentDraftZone({ canOpenDraft, captureCreateComment, disposeDraftZone, + imageUploadAddress, draftPlaceholder, draftSubmitLabel, editor, @@ -326,9 +331,5 @@ export function useDiffCommentDraftZone({ } }, [cancelReanchorFrame, disposeDraftZone, editor]) - return { - disposeDraftZone, - isDraftOpen, - onAddCommentClickRef - } + return { disposeDraftZone, isDraftOpen, onAddCommentClickRef } } diff --git a/src/renderer/src/components/diff-comments/diff-comment-zone-card.tsx b/src/renderer/src/components/diff-comments/diff-comment-zone-card.tsx index 547f5e6b62e..498e0b581c9 100644 --- a/src/renderer/src/components/diff-comments/diff-comment-zone-card.tsx +++ b/src/renderer/src/components/diff-comments/diff-comment-zone-card.tsx @@ -1,3 +1,4 @@ +import type { GitHubImageIntake } from '@/components/github/use-github-image-uploads' import type { DiffCommentOwner } from '@/store/diff-comment-owner' import type { RefObject } from 'react' import type { Root } from 'react-dom/client' @@ -133,6 +134,7 @@ export function renderDiffCommentZoneCard( } export type DiffCommentDraftCardContext = { + imageUploadAddress?: GitHubImageIntake['address'] placeholder?: string submitLabel?: string submittingLabel?: string @@ -147,6 +149,7 @@ export function renderDiffCommentDraftCard( root: Root, draft: { lineNumber: number; startLine?: number }, { + imageUploadAddress, placeholder, submitLabel, submittingLabel, @@ -160,6 +163,7 @@ export function renderDiffCommentDraftCard( root.render( Promise + imageUploadAddress?: GitHubImageIntake['address'] draftPlaceholder?: string draftSubmitLabel?: string canOpenDraft?: boolean @@ -73,6 +75,7 @@ export function useDiffCommentDecorator({ pendingCommentTarget = null, addNoteShortcutEnabled = false, onCreateComment, + imageUploadAddress, draftPlaceholder, draftSubmitLabel, canOpenDraft, @@ -112,6 +115,7 @@ export function useDiffCommentDecorator({ onPendingScrollConsumedRef.current = onPendingScrollConsumed const { onAddCommentClickRef, isDraftOpen } = useDiffCommentDraftZone({ + imageUploadAddress, captureCreateComment, editor, monacoModelIdentity, diff --git a/src/renderer/src/components/editor/diff/sections/DiffSectionItem.tsx b/src/renderer/src/components/editor/diff/sections/DiffSectionItem.tsx index a179285caf1..7bd87e72b5c 100644 --- a/src/renderer/src/components/editor/diff/sections/DiffSectionItem.tsx +++ b/src/renderer/src/components/editor/diff/sections/DiffSectionItem.tsx @@ -38,6 +38,7 @@ export function DiffSectionItem({ openSectionTitle, onOpenPreview, renderHeaderTrailingContent, + imageUploadAddress, onAddLineComment, addLineCommentLabel, addLineCommentPlaceholder, @@ -121,6 +122,7 @@ export function DiffSectionItem({ ) useDiffCommentDecorator({ + imageUploadAddress, captureCreateComment: !onAddLineComment, commentOwner, editor: hasLineCommentAction ? modifiedEditor : null, diff --git a/src/renderer/src/components/editor/diff/sections/diff-section-item-props.ts b/src/renderer/src/components/editor/diff/sections/diff-section-item-props.ts index 252ac9be1ae..10a35b52707 100644 --- a/src/renderer/src/components/editor/diff/sections/diff-section-item-props.ts +++ b/src/renderer/src/components/editor/diff/sections/diff-section-item-props.ts @@ -1,3 +1,4 @@ +import type { GitHubImageIntake } from '@/components/github/use-github-image-uploads' import type { WorkspaceResource } from '@/lib/workspace-copy-owner' import type { Dispatch, MutableRefObject, ReactNode, SetStateAction } from 'react' import type { editor as monacoEditor } from 'monaco-editor' @@ -22,6 +23,7 @@ export type DiffSectionItemProps = { openSectionTitle: string onOpenPreview?: (section: DiffSection, index: number) => void renderHeaderTrailingContent?: (section: DiffSection, index: number) => ReactNode + imageUploadAddress?: GitHubImageIntake['address'] onAddLineComment?: ( section: DiffSection, args: { lineNumber: number; startLine?: number; body: string } diff --git a/src/renderer/src/components/editor/rich-markdown/images/rich-markdown-image-error-link.ts b/src/renderer/src/components/editor/rich-markdown/images/rich-markdown-image-error-link.ts new file mode 100644 index 00000000000..6686596f380 --- /dev/null +++ b/src/renderer/src/components/editor/rich-markdown/images/rich-markdown-image-error-link.ts @@ -0,0 +1,45 @@ +import { openAppHttpLink } from '@/lib/browser/navigation/app-http-link-open' + +export function attachRichMarkdownImageErrorLink( + container: HTMLElement, + image: HTMLImageElement, + getSource: () => string | undefined +): { update: (sourceChanged: boolean) => void } { + const link = document.createElement('a') + link.hidden = true + link.style.lineHeight = 'normal' + link.contentEditable = 'false' + link.addEventListener('click', (event) => { + event.preventDefault() + event.stopPropagation() + const source = getSource() + if (source) { + openAppHttpLink(source, { event }) + } + }) + image.addEventListener('error', () => { + const source = getSource() + if (!source) { + return + } + link.href = source + link.textContent = image.alt || source + image.hidden = true + link.hidden = false + }) + const reset = (): void => { + image.hidden = false + link.hidden = true + } + image.addEventListener('load', reset) + container.appendChild(link) + return { + update: (sourceChanged) => { + if (sourceChanged) { + reset() + } else if (!link.hidden) { + link.textContent = image.alt || getSource() || '' + } + } + } +} diff --git a/src/renderer/src/components/editor/rich-markdown/schema/rich-markdown-extensions.ts b/src/renderer/src/components/editor/rich-markdown/schema/rich-markdown-extensions.ts index 4334ce4142b..36657850aab 100644 --- a/src/renderer/src/components/editor/rich-markdown/schema/rich-markdown-extensions.ts +++ b/src/renderer/src/components/editor/rich-markdown/schema/rich-markdown-extensions.ts @@ -48,6 +48,7 @@ import { RichMarkdownCodeBlockLowlight } from '../code-blocks/rich-markdown-lowl import { RichMarkdownTaskList } from '../lists/rich-markdown-task-list' import { createCachedLowlight } from '../code-blocks/rich-markdown-lowlight-cache' import { documentResourceAccess } from '@/lib/local-file-access' +import { attachRichMarkdownImageErrorLink } from '../images/rich-markdown-image-error-link' import { readRichMarkdownImageRuntimeContext, resolveRichMarkdownImageUrl @@ -63,11 +64,15 @@ const RichMarkdownCode = Code.extend({ export function createRichMarkdownExtensions({ codec, includePlaceholder = false, + allowBase64Images = true, + imageErrorLink = false, htmlSuperscriptLinks = false, htmlSuperscriptLinkContext }: { codec: RichMarkdownEditorCodec includePlaceholder?: boolean + allowBase64Images?: boolean + imageErrorLink?: boolean htmlSuperscriptLinks?: boolean htmlSuperscriptLinkContext?: RichMarkdownHtmlSuperscriptLinkContext }): AnyExtension[] { @@ -151,6 +156,9 @@ export function createRichMarkdownExtensions({ dom.appendChild(img) let currentSrc = node.attrs.src as string | undefined + const errorLink = imageErrorLink + ? attachRichMarkdownImageErrorLink(dom, img, () => currentSrc) + : undefined let currentContextVersion = getImageContextVersion(this.storage) let releaseImageLease: (() => void) | undefined @@ -222,6 +230,7 @@ export function createRichMarkdownExtensions({ ) ) const newSrc = updatedNode.attrs.src as string | undefined + errorLink?.update(newSrc !== currentSrc) const nextContextVersion = getImageContextVersion(this.storage) if (newSrc !== currentSrc || nextContextVersion !== currentContextVersion) { currentSrc = newSrc @@ -241,7 +250,7 @@ export function createRichMarkdownExtensions({ } } }).configure({ - allowBase64: true, + allowBase64: allowBase64Images, // Markdown nests images in paragraphs; block image nodes would become schema-invalid and crash on the first edit. inline: true }), diff --git a/src/renderer/src/components/github-item-dialog/discuss-item/comment-reply-form.tsx b/src/renderer/src/components/github-item-dialog/discuss-item/comment-reply-form.tsx index 3b3b24e8971..92c7d28d6d0 100644 --- a/src/renderer/src/components/github-item-dialog/discuss-item/comment-reply-form.tsx +++ b/src/renderer/src/components/github-item-dialog/discuss-item/comment-reply-form.tsx @@ -4,6 +4,7 @@ import { Button } from '@/components/ui/button' import { useMountedRef } from '@/hooks/useMountedRef' import { cn } from '@/lib/utils' import { GitHubMarkdownComposer } from '@/components/github/GitHubMarkdownComposer' +import type { GitHubImageUploadAddress } from '@/components/github/github-image-upload' import { getCommentBodySubmitState, hasBoundedCommentBodyText @@ -11,11 +12,13 @@ import { import { translate } from '@/i18n/i18n' export function CommentReplyForm({ + imageUploadAddress, className, placeholder, onCancel, onSubmit }: { + imageUploadAddress?: GitHubImageUploadAddress className?: string placeholder: string onCancel: () => void @@ -23,11 +26,12 @@ export function CommentReplyForm({ }): React.JSX.Element { const [body, setBody] = useState('') const [submitting, setSubmitting] = useState(false) + const [uploading, setUploading] = useState(false) const mountedRef = useMountedRef() const submit = useCallback(async () => { const bodyState = getCommentBodySubmitState(body) - if (bodyState.status === 'empty' || submitting) { + if (bodyState.status === 'empty' || submitting || uploading) { return } if (bodyState.status === 'too-large-leading-whitespace') { @@ -53,12 +57,14 @@ export function CommentReplyForm({ setSubmitting(false) } } - }, [body, mountedRef, onSubmit, submitting]) + }, [body, mountedRef, onSubmit, submitting, uploading]) const canSubmitReply = hasBoundedCommentBodyText(body) return (
{translate('auto.components.GitHubItemDialog.675bc0d638', 'Cancel')} - + {open && ( + + )} + + ) + } + const view = render() + await waitFor(() => expect(view.container.querySelector('.tiptap')).not.toBeNull()) + fireEvent.paste(getComposerEditor(view.container), { + clipboardData: { + files: [new File(['a'], 'orphan.png', { type: 'image/png' })], + getData: () => '' + } + }) + await waitFor(() => expect(imageUploads.upload).toHaveBeenCalledOnce()) + fireEvent.click(view.getByRole('button', { name: 'Toggle' })) + fireEvent.click(view.getByRole('button', { name: 'Toggle' })) + await act(async () => held.resolve('https://github.com/user-attachments/assets/orphan')) + await waitFor(() => expect(getComposerEditor(view.container).textContent).toBe('Keep this text')) + expect(getComposerEditor(view.container).querySelector('img')).toBeNull() +}) + +it('gives image Files precedence over the same image in clipboard HTML', async () => { + imageUploads.upload + .mockReset() + .mockResolvedValue('https://github.com/user-attachments/assets/mixed') + const view = render() + await waitFor(() => expect(view.container.querySelector('.tiptap')).not.toBeNull()) + fireEvent.paste(getComposerEditor(view.container), { + clipboardData: { + files: [new File(['png'], 'mixed.png', { type: 'image/png' })], + getData: (type: string) => + type === 'text/html' ? '

' : '' + } + }) + await waitFor(() => + expect(getComposerEditor(view.container).querySelector('img')?.getAttribute('src')).toBe( + 'https://github.com/user-attachments/assets/mixed' + ) + ) + expect(imageUploads.upload).toHaveBeenCalledOnce() + expect(toast.error).not.toHaveBeenCalled() +}) + +it('refuses normalized data-image HTML without dropping surrounding text', async () => { + imageUploads.upload.mockReset() + const onChange = vi.fn() + const view = render( + + ) + await waitFor(() => expect(view.container.querySelector('.tiptap')).not.toBeNull()) + fireEvent.paste(getComposerEditor(view.container), { + clipboardData: { + files: [], + getData: (type: string) => + type === 'text/html' + ? '

beforescreenafter

' + : '' + } + }) + await waitFor(() => expect(onChange).toHaveBeenCalled()) + expect(onChange.mock.lastCall?.[0]).toBe('beforeafter') + expect(getComposerEditor(view.container).querySelector('img')).toBeNull() + expect(toast.error).toHaveBeenCalledOnce() + expect(imageUploads.upload).not.toHaveBeenCalled() +}) + +it('leaves ordinary HTML paste on the editor parser path', async () => { + const view = render( {}} placeholder="HTML" />) + await waitFor(() => expect(view.container.querySelector('.tiptap')).not.toBeNull()) + const parse = vi.spyOn(DOMParser.prototype, 'parseFromString') + try { + fireEvent.paste(getComposerEditor(view.container), { + clipboardData: { + files: [], + getData: (type: string) => + type === 'text/html' ? '

ordinary formatted text

' : '' + } + }) + expect(getComposerEditor(view.container).textContent).toBe('ordinary formatted text') + expect(parse).not.toHaveBeenCalled() + } finally { + parse.mockRestore() + } +}) + +it('shows a failed hosted image as its filename link without changing the uploaded Markdown', async () => { + imageUploads.upload + .mockReset() + .mockResolvedValue('https://github.com/user-attachments/assets/private') + const onChange = vi.fn() + const imageUploadAddress: GitHubImageUploadAddress = { + runtime: { kind: 'local' }, + target: { kind: 'repo', repo: 'repo', source: 'issue' } + } + const view = render( + + ) + await waitFor(() => expect(view.container.querySelector('.tiptap')).not.toBeNull()) + const editor = getComposerEditor(view.container) + fireEvent.paste(editor, { + clipboardData: { + files: [new File(['png'], 'screen.png', { type: 'image/png' })], + getData: () => '' + } + }) + await waitFor(() => + expect(editor.querySelector('img')?.getAttribute('src')).toBe( + 'https://github.com/user-attachments/assets/private' + ) + ) + const image = editor.querySelector('img') + if (!image) { + throw new Error('Uploaded image missing') + } + fireEvent.error(image) + const link = view.getByRole('link', { name: 'screen.png' }) + expect(link.getAttribute('href')).toBe('https://github.com/user-attachments/assets/private') + expect(image.hidden).toBe(true) + expect(onChange.mock.lastCall?.[0]).toBe( + '![screen.png](https://github.com/user-attachments/assets/private)' + ) + fireEvent.focus(window) + expect(image.hidden).toBe(true) + expect(view.getByRole('link', { name: 'screen.png' })).toBe(link) + view.rerender( + + ) + await waitFor(() => expect(view.getByRole('link', { name: 'renamed.png' })).toBe(link)) + expect(image.alt).toBe('renamed.png') + expect(image.hidden).toBe(true) + fireEvent.load(image) + expect(image.hidden).toBe(false) + expect(view.queryByRole('link', { name: 'renamed.png' })).toBeNull() +}) diff --git a/src/renderer/src/components/github/GitHubMarkdownComposer.tsx b/src/renderer/src/components/github/GitHubMarkdownComposer.tsx index 1f9c224fde5..155750ea489 100644 --- a/src/renderer/src/components/github/GitHubMarkdownComposer.tsx +++ b/src/renderer/src/components/github/GitHubMarkdownComposer.tsx @@ -2,9 +2,7 @@ import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { useEditor } from '@tiptap/react' import type { Editor } from '@tiptap/react' import Placeholder from '@tiptap/extension-placeholder' -import { ImageIcon, Paperclip } from 'lucide-react' -import { Button } from '@/components/ui/button' -import { Input } from '@/components/ui/input' +import { Paperclip } from 'lucide-react' import { cn } from '@/lib/utils' import { isScreenSubmitShortcut } from '@/lib/screen-submit-shortcut' import { createRichMarkdownExtensions } from '@/components/editor/rich-markdown/schema/rich-markdown-extensions' @@ -25,7 +23,7 @@ import { GitHubMarkdownComposerTabbar, type ComposerTab } from '@/components/github/github-markdown-composer-tabbar' -import { hasBoundedGitHubMarkdownImageUrlText } from '@/components/github/github-markdown-image-url' +import { GitHubMarkdownImageInput } from './github-markdown-image-input' import { useImageInput } from '@/components/github/use-image-input' import type { GitHubOwnerRepo } from '../../../../shared/github/pull-request-types' import { translate } from '@/i18n/i18n' @@ -35,8 +33,12 @@ import { useRichMarkdownSpellcheckAttribute } from '@/components/editor/rich-markdown/instance/rich-markdown-spellcheck' import { openAppHttpLink } from '@/lib/browser/navigation/app-http-link-open' +import { useGitHubImagePaste } from './use-github-image-paste' +import type { GitHubImageUploadAddress } from './github-image-upload' type GitHubMarkdownComposerProps = { + imageUploadAddress?: GitHubImageUploadAddress | (() => GitHubImageUploadAddress) + onImageUploadPendingChange?: (pending: boolean) => void value: string onChange: (value: string) => void placeholder: string @@ -50,6 +52,8 @@ type GitHubMarkdownComposerProps = { } export function GitHubMarkdownComposer({ + imageUploadAddress, + onImageUploadPendingChange, value, onChange, placeholder, @@ -77,6 +81,13 @@ export function GitHubMarkdownComposer({ const [isEditingLink, setIsEditingLink] = useState(false) const isTabbed = layout === 'tabbed' const codec = useMemo(() => createRichMarkdownEditorCodec(), []) + const images = useGitHubImagePaste( + rootRef, + editorRef, + imageUploadAddress, + onImageUploadPendingChange, + !disabled && (!isTabbed || activeTab === 'write') + ) const { imageUrl, @@ -95,13 +106,14 @@ export function GitHubMarkdownComposer({ const extensions = useMemo( () => [ - ...createRichMarkdownExtensions({ codec }), + ...createRichMarkdownExtensions({ codec, allowBase64Images: false, imageErrorLink: true }), + images.extension, Placeholder.configure({ includeChildren: true, placeholder }) ], - [codec, placeholder] + [codec, placeholder, images.extension] ) const openLinkEditor = useCallback(() => { @@ -126,6 +138,8 @@ export function GitHubMarkdownComposer({ content: encodeRawMarkdownHtmlForRichEditor(value, codec), contentType: 'markdown', editorProps: { + handleDOMEvents: { paste: images.handlePaste }, + transformPastedHTML: images.transformPastedHTML, attributes: { class: cn('rich-markdown-editor github-markdown-composer-editor', minHeightClassName), spellcheck: getRichMarkdownSpellcheckAttribute(richMarkdownSpellcheckEnabled) @@ -136,7 +150,9 @@ export function GitHubMarkdownComposer({ if (submit) { event.preventDefault() event.stopPropagation() - submit() + if (!images.pending.current) { + submit() + } return true } } @@ -294,49 +310,14 @@ export function GitHubMarkdownComposer({ ) const imageInputRow = imageInputOpen ? ( -
{ - event.preventDefault() - insertImageUrl() - }} - > - - setImageUrl(event.target.value)} - onKeyDown={(event) => { - if (isScreenSubmitShortcut(event)) { - event.preventDefault() - event.stopPropagation() - insertImageUrl() - return - } - if (event.key === 'Escape') { - event.preventDefault() - event.stopPropagation() - setImageInputOpen(false) - } - }} - placeholder={translate( - 'auto.components.github.GitHubMarkdownComposer.f24783f470', - 'https://...' - )} - disabled={disabled} - className="h-8 min-w-0 text-xs" - /> - - - + ) : null const editorPane = const previewPane = ( @@ -364,7 +345,7 @@ export function GitHubMarkdownComposer({ ) : null return (
[0]> & { id: string }) + | null + | undefined, + worktreePath: string | null | undefined, + provider: string, + onPending: (pending: boolean) => void +): GitHubImageIntake | undefined { + if (provider !== 'github') { + return undefined + } + return { + onPending, + address: () => { + if (!repo) { + throw new Error('Open this repository before attaching an image.') + } + return { + runtime: hostedReviewRepoOwnerTarget(repo), + target: { + kind: 'repo', + repo: repo.id, + source: 'origin', + ...(worktreePath ? { worktree: `path:${worktreePath}` } : {}) + } + } + } + } +} diff --git a/src/renderer/src/components/github/github-image-upload.test.ts b/src/renderer/src/components/github/github-image-upload.test.ts new file mode 100644 index 00000000000..66d65646d9f --- /dev/null +++ b/src/renderer/src/components/github/github-image-upload.test.ts @@ -0,0 +1,114 @@ +// @vitest-environment happy-dom +import { beforeEach, expect, it, vi } from 'vitest' +import { captureGitHubImageUpload } from './github-image-upload' +const { rpc, revision } = vi.hoisted(() => ({ rpc: vi.fn(), revision: vi.fn() })) +vi.mock('@/runtime/runtime-rpc-client', () => ({ callRuntimeRpc: rpc })) +vi.mock('@/runtime/runtime-environment-revision', () => ({ + getRuntimeEnvironmentRevision: revision +})) +vi.mock('@/lib/web-client-location', () => ({ isWebClientLocation: () => false })) +beforeEach(() => { + vi.resetAllMocks() + revision.mockReturnValue(4) +}) +it('keeps every staged-byte request on the destination captured before the first await', async () => { + const held = Promise.withResolvers<{ capabilities: string[] }>() + rpc.mockImplementation(async (_runtime, method) => { + if (method === 'status.get') { + return held.promise + } + if (method === 'clipboard.startImageUpload') { + return { uploadId: 'image-1' } + } + if (method === 'github.uploadAttachment') { + return { url: 'https://github.com/user-attachments/assets/a' } + } + return {} + }) + const address = { + runtime: { kind: 'environment' as const, environmentId: 'owner' }, + target: { kind: 'repo' as const, repo: 'repo-1', source: 'origin' as const } + } + const upload = captureGitHubImageUpload(address) + const result = upload(new File([new Uint8Array([0, 1, 255])], 'a.png', { type: 'image/png' })) + address.runtime.environmentId = 'other' + address.target.repo = 'repo-2' + revision.mockReturnValue(9) + held.resolve({ capabilities: ['github.image-attachments.v1'] }) + await expect(result).resolves.toBe('https://github.com/user-attachments/assets/a') + for (const [runtime, , , options] of rpc.mock.calls) { + expect(runtime).toEqual({ kind: 'environment', environmentId: 'owner' }) + expect(options.expectedEnvironmentPairingRevision).toBe(4) + } + expect( + rpc.mock.calls.find((call) => call[1] === 'clipboard.appendImageUploadChunk')?.[2] + ).toEqual({ uploadId: 'image-1', offset: 0, contentBase64: 'AAH/' }) + expect(rpc.mock.calls.find((call) => call[1] === 'github.uploadAttachment')?.[2]).toEqual({ + uploadId: 'image-1', + target: { kind: 'repo', repo: 'repo-1', source: 'origin' }, + fileName: 'a.png', + contentType: 'image/png' + }) +}) +it('refuses image intake on an older runtime before sending any bytes', async () => { + rpc.mockResolvedValue({ capabilities: [] }) + await expect( + captureGitHubImageUpload({ + runtime: { kind: 'local' }, + target: { + kind: 'repo', + repo: 'repo-1', + source: 'issue' + } + })(new File(['a'], 'a.png', { type: 'image/png' })) + ).rejects.toThrow('cannot upload images yet') + expect(rpc.mock.calls.map((call) => call[1])).toEqual(['status.get']) +}) + +it.each([2, 20])('checks capability once and uploads each of %i accepted images', async (count) => { + rpc.mockImplementation(async (_runtime, method) => { + if (method === 'status.get') { + return { capabilities: ['github.image-attachments.v1'] } + } + if (method === 'clipboard.startImageUpload') { + return { uploadId: 'image' } + } + if (method === 'github.uploadAttachment') { + return { url: 'https://github.com/user-attachments/assets/image' } + } + return {} + }) + const upload = captureGitHubImageUpload({ + runtime: { kind: 'local' }, + target: { kind: 'repo', repo: 'repo', source: 'origin' } + }) + for (let index = 0; index < count; index++) { + await upload(new File(['a'], `${index}.png`, { type: 'image/png' })) + } + expect(rpc.mock.calls.filter((call) => call[1] === 'status.get')).toHaveLength(1) + expect(rpc.mock.calls.filter((call) => call[1] === 'github.uploadAttachment')).toHaveLength(count) +}) + +it('stages a 5 MiB image in fourteen bounded chunks without putting bytes in Markdown', async () => { + rpc.mockImplementation(async (_runtime, method) => { + if (method === 'status.get') { + return { capabilities: ['github.image-attachments.v1'] } + } + if (method === 'clipboard.startImageUpload') { + return { uploadId: 'large' } + } + if (method === 'github.uploadAttachment') { + return { url: 'https://github.com/user-attachments/assets/large' } + } + return {} + }) + const url = await captureGitHubImageUpload({ + runtime: { kind: 'local' }, + target: { kind: 'repo', repo: 'repo', source: 'issue' } + })(new File([new Uint8Array(5 * 1024 * 1024)], 'large.png', { type: 'image/png' })) + expect(url).toBe('https://github.com/user-attachments/assets/large') + expect( + rpc.mock.calls.filter((call) => call[1] === 'clipboard.appendImageUploadChunk') + ).toHaveLength(14) + expect(rpc.mock.calls.filter((call) => call[1] === 'status.get')).toHaveLength(1) +}) diff --git a/src/renderer/src/components/github/github-image-upload.ts b/src/renderer/src/components/github/github-image-upload.ts new file mode 100644 index 00000000000..65b9703b473 --- /dev/null +++ b/src/renderer/src/components/github/github-image-upload.ts @@ -0,0 +1,85 @@ +import type { GitHubAttachmentTarget } from '../../../../shared/rpc-contract/github-attachment-params' +import type { RuntimeClientTarget } from '../../../../shared/runtime-client-target' +import type { RuntimeStatus } from '../../../../shared/runtime-types' +import { + assertGitHubImage, + GITHUB_IMAGE_ATTACHMENT_CAPABILITY +} from '../../../../shared/github/image-attachment' +import { CLIPBOARD_IMAGE_UPLOAD_CHUNK_BASE64_CHARS } from '../../../../shared/rpc-contract/clipboard-params' +import { callRuntimeRpc } from '@/runtime/runtime-rpc-client' +import { getRuntimeEnvironmentRevision } from '@/runtime/runtime-environment-revision' +import { isWebClientLocation } from '@/lib/web-client-location' +import { readStoredWebRuntimeEnvironment } from '@/web/web-runtime-environment' +import { blobToBase64 } from '@/lib/image-blob-png' + +export type GitHubImageUploadAddress = { + runtime: RuntimeClientTarget + target: GitHubAttachmentTarget +} + +export function captureGitHubImageUpload( + address: GitHubImageUploadAddress +): (file: File) => Promise { + const target = structuredClone(address.target) + const webEnvironment = + isWebClientLocation() && address.runtime.kind === 'local' + ? readStoredWebRuntimeEnvironment() + : null + if (isWebClientLocation() && address.runtime.kind === 'local' && !webEnvironment) { + throw new Error('Pair this web client with an Orca server first.') + } + const runtime = webEnvironment + ? { kind: 'environment' as const, environmentId: webEnvironment.id } + : { ...address.runtime } + const options = { + expectedEnvironmentPairingRevision: + runtime.kind === 'environment' + ? getRuntimeEnvironmentRevision(runtime.environmentId) + : undefined + } + const call = (method: string, params?: unknown) => + callRuntimeRpc(runtime, method, params, { ...options, timeoutMs: 120_000 }) + let supported: Promise | undefined + return async (file) => { + assertGitHubImage(file.type, file.size) + supported ??= call('status.get').then((status) => { + if (!status.capabilities?.includes(GITHUB_IMAGE_ATTACHMENT_CAPABILITY)) { + throw new Error( + 'This Orca server cannot upload images yet. Update it, or use an image URL.' + ) + } + }) + await supported + const contentBase64 = await blobToBase64(file) + const { uploadId } = await call<{ uploadId: string }>('clipboard.startImageUpload', { + expectedBase64Length: contentBase64.length, + fileName: file.name + }) + try { + for ( + let offset = 0; + offset < contentBase64.length; + offset += CLIPBOARD_IMAGE_UPLOAD_CHUNK_BASE64_CHARS + ) { + await call('clipboard.appendImageUploadChunk', { + uploadId, + offset, + contentBase64: contentBase64.slice( + offset, + offset + CLIPBOARD_IMAGE_UPLOAD_CHUNK_BASE64_CHARS + ) + }) + } + const result = await call<{ url: string }>('github.uploadAttachment', { + uploadId, + target, + fileName: file.name || 'image.png', + contentType: file.type + }) + return result.url + } catch (error) { + await call('clipboard.abortImageUpload', { uploadId }).catch(() => {}) + throw error + } + } +} diff --git a/src/renderer/src/components/github/github-markdown-image-input.tsx b/src/renderer/src/components/github/github-markdown-image-input.tsx new file mode 100644 index 00000000000..d48d88b5e2d --- /dev/null +++ b/src/renderer/src/components/github/github-markdown-image-input.tsx @@ -0,0 +1,69 @@ +import type { RefObject } from 'react' +import { ImageIcon } from 'lucide-react' +import { Button } from '@/components/ui/button' +import { Input } from '@/components/ui/input' +import { isScreenSubmitShortcut } from '@/lib/screen-submit-shortcut' +import { translate } from '@/i18n/i18n' +import { hasBoundedGitHubMarkdownImageUrlText } from './github-markdown-image-url' + +export function GitHubMarkdownImageInput({ + imageUrl, + imageInputRef, + setImageUrl, + setImageInputOpen, + insertImageUrl, + disabled +}: { + imageUrl: string + imageInputRef: RefObject + setImageUrl: (value: string) => void + setImageInputOpen: (open: boolean) => void + insertImageUrl: () => void + disabled: boolean +}) { + return ( +
{ + event.preventDefault() + insertImageUrl() + }} + > + + setImageUrl(event.target.value)} + onKeyDown={(event) => { + if (isScreenSubmitShortcut(event)) { + event.preventDefault() + event.stopPropagation() + insertImageUrl() + return + } + if (event.key === 'Escape') { + event.preventDefault() + event.stopPropagation() + setImageInputOpen(false) + } + }} + placeholder={translate( + 'auto.components.github.GitHubMarkdownComposer.f24783f470', + 'https://...' + )} + disabled={disabled} + className="h-8 min-w-0 text-xs" + /> + + + + ) +} diff --git a/src/renderer/src/components/github/use-github-image-paste.ts b/src/renderer/src/components/github/use-github-image-paste.ts new file mode 100644 index 00000000000..ec7ae68c056 --- /dev/null +++ b/src/renderer/src/components/github/use-github-image-paste.ts @@ -0,0 +1,144 @@ +import { useLayoutEffect, useMemo, useRef, type RefObject } from 'react' +import { Node as TiptapNode } from '@tiptap/core' +import type { Editor } from '@tiptap/react' +import { toast } from 'sonner' +import { createNonSecureContextUuid as createBrowserUuid } from '../../../../shared/non-secure-context-uuid' +import { translate } from '@/i18n/i18n' +import type { GitHubImageUploadAddress } from './github-image-upload' +import { useGitHubImageEvents, useGitHubImageUploads } from './use-github-image-uploads' + +export function useGitHubImagePaste( + root: RefObject, + editor: RefObject, + address: GitHubImageUploadAddress | (() => GitHubImageUploadAddress) | undefined, + onPending: ((pending: boolean) => void) | undefined, + enabled: boolean +) { + const options = useRef({ address, onPending, enabled }) + const uploads = useGitHubImageUploads({ address, onPending, enabled }) + const live = useRef(new Set()) + useLayoutEffect(() => { + options.current = { address, onPending, enabled } + }) + useLayoutEffect( + () => () => { + live.current.clear() + }, + [] + ) + const extension = useMemo( + () => + TiptapNode.create({ + name: 'githubImageUpload', + inline: true, + group: 'inline', + atom: true, + addAttributes: () => ({ id: { default: null }, name: { default: '' } }), + renderHTML: () => ['span'], + renderMarkdown: () => '', + addNodeView: + () => + ({ node }) => { + const dom = document.createElement('span') + dom.className = 'text-muted-foreground' + dom.textContent = live.current.has(node.attrs.id) ? `Uploading ${node.attrs.name}…` : '' + return { dom } + } + }), + [] + ) + + const receive = (files: File[], at?: number): boolean => { + const current = editor.current + const images = files.filter((file) => file.type.startsWith('image/')) + if (!images.length || !options.current.enabled || !current || current.isDestroyed) { + return false + } + if (at !== undefined) { + current.commands.setTextSelection(at) + } + return uploads.receive(images, (file) => { + const id = createBrowserUuid() + live.current.add(id) + current.commands.insertContent({ + type: 'githubImageUpload', + attrs: { id, name: file.name || 'image.png' } + }) + return (url?: string) => { + live.current.delete(id) + if (current.isDestroyed) { + return + } + let position: number | undefined + current.state.doc.descendants((node, pos) => { + if (node.type.name === 'githubImageUpload' && node.attrs.id === id) { + position = pos + } + }) + if (position === undefined) { + return + } + const tr = current.state.tr + if (url) { + tr.replaceWith( + position, + position + 1, + current.schema.nodes.image.create({ src: url, alt: file.name }) + ) + } else { + tr.delete(position, position + 1) + } + current.view.dispatch(tr.setMeta('addToHistory', false)) + } + }) + } + + const events = useGitHubImageEvents( + root, + receive, + (event) => editor.current?.view.posAtCoords({ left: event.clientX, top: event.clientY })?.pos, + (event) => + !!( + options.current.enabled && + editor.current?.isEditable && + event.target instanceof globalThis.Node && + editor.current.view.dom.contains(event.target) + ) + ) + return { + extension, + pending: uploads.pending, + attachDrop: events.attachDrop, + handlePaste: (_view: unknown, event: ClipboardEvent) => { + if (!events.paste(event)) { + return false + } + event.preventDefault() + return true + }, + transformPastedHTML: (html: string) => { + if (!/ GitHubImageUploadAddress) + onPending?: (pending: boolean) => void + enabled?: boolean +} + +export function useGitHubImageUploads(options: GitHubImageIntake) { + const latest = useRef(options) + const pending = useRef(0) + const tail = useRef(Promise.resolve()) + const mounted = useRef(true) + useLayoutEffect(() => { + latest.current = options + }) + useLayoutEffect(() => { + mounted.current = true + return () => { + mounted.current = false + latest.current.onPending?.(false) + } + }, []) + + const receive = (files: File[], insert: (file: File) => (url?: string) => void): boolean => { + const images = files.filter((file) => file.type.startsWith('image/')) + if (!images.length || latest.current.enabled === false) { + return false + } + try { + const address = latest.current.address + if (!address) { + throw new Error('Open this GitHub repository before attaching an image.') + } + const upload = captureGitHubImageUpload(typeof address === 'function' ? address() : address) + for (const file of images) { + const replace = insert(file) + pending.current++ + latest.current.onPending?.(true) + tail.current = tail.current + .then(async () => { + try { + if (!mounted.current) { + return + } + const url = await upload(file) + if (mounted.current) { + replace(url) + } + } catch (error) { + if (mounted.current) { + replace() + toast.error( + translate('github.imageUploadFailed', 'Could not attach image: {{message}}', { + message: + error instanceof Error ? error.message : 'Paste the image again to retry.' + }) + ) + } + } finally { + pending.current-- + if (mounted.current) { + latest.current.onPending?.(pending.current > 0) + } + } + }) + .catch(() => {}) + } + } catch (error) { + toast.error( + error instanceof Error + ? error.message + : translate('github.imageUploadError', 'GitHub could not upload this image.') + ) + } + return true + } + return { receive, pending } +} + +export function useGitHubImagePending() { + const [pending, setPending] = useState(false) + const pendingRef = useRef(false) + const onPending = useCallback((value: boolean) => { + pendingRef.current = value + setPending(value) + }, []) + return { pending, pendingRef, onPending } +} + +export function useGitHubImageEvents( + root: RefObject, + receive: (files: File[], destination?: Destination) => boolean, + captureDestination: (event: DragEvent) => Destination, + canAccept: (event: DragEvent) => boolean +) { + const latest = useRef(receive) + useLayoutEffect(() => { + latest.current = receive + }) + const attachDrop = useOsFileDropOwner(root, { + kind: 'files', + captureDestination, + canAccept, + onFiles: (files, { destination }) => { + latest.current(files, destination) + } + }) + return { + attachDrop, + paste: (event: ClipboardEvent | React.ClipboardEvent) => + latest.current(Array.from(event.clipboardData?.files ?? [])) + } +} diff --git a/src/renderer/src/components/github/use-github-textarea-images.test.tsx b/src/renderer/src/components/github/use-github-textarea-images.test.tsx new file mode 100644 index 00000000000..ba4c945e38e --- /dev/null +++ b/src/renderer/src/components/github/use-github-textarea-images.test.tsx @@ -0,0 +1,199 @@ +// @vitest-environment happy-dom +import { useRef, useState } from 'react' +import { act, cleanup, fireEvent, render, waitFor } from '@testing-library/react' +import { afterEach, expect, it, vi } from 'vitest' +import { MentionTextarea } from '@/components/pull-request-page/mentions/textarea' +import { GitHubImageTextarea } from './GitHubImageTextarea' +import { useGitHubImagePending } from './use-github-image-uploads' + +const { upload } = vi.hoisted(() => ({ upload: vi.fn() })) +vi.mock('./github-image-upload', () => ({ captureGitHubImageUpload: () => upload })) +vi.mock('sonner', () => ({ toast: { error: vi.fn() } })) +afterEach(cleanup) +function Draft({ + mentions = false, + initialBody = 'hello ', + onChange +}: { + mentions?: boolean + initialBody?: string + onChange?: (value: string) => void +}) { + const [body, setBody] = useState(initialBody) + const emit = (value: string) => { + onChange?.(value) + setBody(value) + } + const images = useGitHubImagePending() + const ref = useRef(null) + const imageUpload = { + address: { + runtime: { kind: 'local' as const }, + target: { + kind: 'repo' as const, + repo: 'repo', + source: 'origin' as const + } + }, + onPending: images.onPending + } + return ( + <> + {mentions ? ( + + ) : ( + + )} + {body} + + + + ) +} +function textareaField(element: HTMLElement): HTMLTextAreaElement { + if (!(element instanceof HTMLTextAreaElement)) { + throw new Error('Draft field must be a textarea') + } + return element +} +function paste(textarea: HTMLTextAreaElement) { + textarea.setSelectionRange(textarea.value.length, textarea.value.length) + fireEvent.paste(textarea, { + clipboardData: { files: [new File(['png'], 'screen.png', { type: 'image/png' })] } + }) +} +it('keeps mentions aligned with display text and emits only a completed URL', async () => { + const held = Promise.withResolvers() + upload.mockReset().mockReturnValue(held.promise) + const view = render() + const field = textareaField(view.getByRole('combobox')) + paste(field) + expect(field.value).toContain('Uploading screen.png') + expect(view.container.querySelector('output')?.textContent).toBe('hello ') + expect(view.getByRole('button', { name: 'Submit' }).hasAttribute('disabled')).toBe(true) + fireEvent.change(field, { target: { value: `${field.value} @oct` } }) + fireEvent.keyUp(field, { key: 't' }) + fireEvent.keyDown(field, { key: 'Enter' }) + await act(async () => held.resolve('https://github.com/user-attachments/assets/screen')) + await waitFor(() => expect(field.value).not.toContain('Uploading')) + expect(view.container.querySelector('output')?.textContent).toBe( + 'hello ![screen.png]() @octocat' + ) +}) +it('removes an entire partly deleted marker and never restores it on completion', async () => { + const held = Promise.withResolvers() + upload.mockReset().mockReturnValue(held.promise) + const view = render() + const field = textareaField(view.getByRole('textbox')) + paste(field) + fireEvent.change(field, { target: { value: field.value.slice(0, -1) } }) + expect(field.value).toBe('hello ') + expect(view.container.querySelector('output')?.textContent).toBe('hello ') + await act(async () => held.resolve('https://github.com/user-attachments/assets/deleted')) + expect(field.value).toBe('hello ') +}) +it('delivers a File drop through the registered owner and detaches an externally replaced draft', async () => { + const held = Promise.withResolvers() + upload.mockReset().mockReturnValue(held.promise) + const view = render() + const field = textareaField(view.getByRole('textbox')) + const drop = new Event('drop', { bubbles: true, cancelable: true, composed: true }) + Object.defineProperties(drop, { + isTrusted: { value: true }, + dataTransfer: { + value: { + types: ['Files'], + files: [new File(['png'], 'drop.png', { type: 'image/png' })] + } + } + }) + act(() => { + field.dispatchEvent(drop) + }) + expect(field.value).toContain('Uploading drop.png') + fireEvent.click(view.getByRole('button', { name: 'Replace' })) + await act(async () => held.resolve('https://github.com/user-attachments/assets/orphan')) + expect(field.value).toBe('Replacement') + expect(view.container.querySelector('output')?.textContent).toBe('Replacement') +}) + +it.each([2, 20])( + 'keeps typing independent of %i historical labels and retires them with the draft', + async (count) => { + upload.mockReset().mockResolvedValue('https://github.com/user-attachments/assets/completed') + const changed = vi.fn() + const view = render() + const field = textareaField(view.getByRole('textbox')) + field.setSelectionRange(field.value.length, field.value.length) + fireEvent.paste(field, { + clipboardData: { + files: Array.from( + { length: count }, + (_, index) => new File(['png'], `history-${index}.png`, { type: 'image/png' }) + ) + } + }) + await waitFor(() => expect(upload).toHaveBeenCalledTimes(count)) + await waitFor(() => expect(field.value).not.toContain('Uploading')) + let projectionPasses = 0 + const originalReplace = String.prototype.replace + const replace = vi.spyOn(String.prototype, 'replace').mockImplementation(function ( + this: string, + pattern: Parameters[0], + replacement: Parameters[1] + ) { + if (pattern instanceof RegExp && pattern.source.startsWith('!\\[Uploading ')) { + projectionPasses++ + } + return originalReplace.call(this, pattern, replacement) + }) + changed.mockClear() + try { + fireEvent.change(field, { target: { value: `${field.value} typed` } }) + expect(projectionPasses).toBe(1) + expect(changed).toHaveBeenCalledOnce() + expect(field.value.endsWith(' typed')).toBe(true) + } finally { + replace.mockRestore() + } + fireEvent.change(field, { target: { value: `${field.value}![Uploading history-0.png…]()` } }) + expect(field.value).not.toContain('Uploading') + fireEvent.click(view.getByRole('button', { name: 'Replace' })) + fireEvent.change(field, { target: { value: 'Replacement ![Uploading history-0.png…]()' } }) + expect(view.container.querySelector('output')?.textContent).toBe( + 'Replacement ![Uploading history-0.png…]()' + ) + } +) + +it('removes an undo-restored owned label after an incomplete user label prefix', async () => { + const held = Promise.withResolvers() + upload.mockReset().mockReturnValue(held.promise) + const prefix = '![Uploading note ' + const view = render() + const field = textareaField(view.getByRole('textbox')) + paste(field) + const restored = field.value + fireEvent.change(field, { target: { value: field.value.slice(0, -1) } }) + expect(field.value).toBe(prefix) + fireEvent.change(field, { target: { value: restored } }) + expect(field.value).toBe(prefix) + expect(view.container.querySelector('output')?.textContent).toBe(prefix) + await act(async () => held.resolve('https://github.com/user-attachments/assets/deleted')) + expect(view.container.querySelector('output')?.textContent).toBe(prefix) +}) diff --git a/src/renderer/src/components/github/use-github-textarea-images.ts b/src/renderer/src/components/github/use-github-textarea-images.ts new file mode 100644 index 00000000000..107403ece65 --- /dev/null +++ b/src/renderer/src/components/github/use-github-textarea-images.ts @@ -0,0 +1,201 @@ +import { useLayoutEffect, useRef, useState, type RefObject } from 'react' +import { createNonSecureContextUuid } from '../../../../shared/non-secure-context-uuid' +import { + useGitHubImageEvents, + useGitHubImageUploads, + type GitHubImageIntake +} from './use-github-image-uploads' + +type Marker = { start: number; end: number; id: string } +type Shadow = { body: string; display: string; markers: Marker[] } + +export function useGitHubTextareaImages( + textarea: RefObject, + value: string, + onChange: (value: string) => void, + options: GitHubImageIntake +) { + const [shadow, setShadow] = useState({ body: value, display: value, markers: [] }) + const current = useRef(shadow) + const changeRef = useRef(onChange) + const before = useRef<{ start: number; end: number } | null>(null) + const known = useRef(new Set()) + const uploads = useGitHubImageUploads(options) + const displayed = shadow.body === value ? shadow : { body: value, display: value, markers: [] } + useLayoutEffect(() => { + if (current.current.body !== value) { + known.current.clear() + } + current.current = displayed + changeRef.current = onChange + }) + + const publish = (display: string, markers: Marker[]) => { + const parts: string[] = [] + let cursor = 0 + for (const marker of markers) { + parts.push(display.slice(cursor, marker.start)) + cursor = marker.end + } + parts.push(display.slice(cursor)) + const body = parts.join('') + const next = { body, display, markers } + current.current = next + setShadow(next) + changeRef.current(body) + } + const change = (next: string) => { + const previous = current.current + if (next === previous.display) { + const caret = before.current?.start ?? textarea.current?.selectionStart ?? next.length + before.current = null + return caret + } + if (!previous.markers.length && !known.current.size) { + before.current = null + publish(next, []) + return textarea.current?.selectionStart ?? next.length + } + let start = 0 + while (start < previous.display.length && previous.display[start] === next[start]) { + start++ + } + if (before.current) { + start = Math.min(start, before.current.start) + } + let suffix = 0 + while ( + suffix < previous.display.length - start && + suffix < next.length - start && + previous.display[previous.display.length - suffix - 1] === next[next.length - suffix - 1] + ) { + suffix++ + } + if (before.current) { + suffix = Math.min(suffix, previous.display.length - before.current.end) + } + before.current = null + const end = previous.display.length - suffix + const inserted = next.slice(start, next.length - suffix) + const touched = previous.markers.filter( + (marker) => + (start < marker.end && end > marker.start) || + (start === end && start > marker.start && start < marker.end) + ) + const from = Math.min(start, ...touched.map((marker) => marker.start)) + const to = Math.max(end, ...touched.map((marker) => marker.end)) + const delta = inserted.length - (to - from) + let display = previous.display.slice(0, from) + inserted + previous.display.slice(to) + let markers = previous.markers + .filter((marker) => !touched.includes(marker)) + .map((marker) => + marker.start >= to + ? { ...marker, start: marker.start + delta, end: marker.end + delta } + : marker + ) + // Undo can restore labels after their range disappeared; only this draft's generated labels retire. + const active = new Set(markers.map((marker) => marker.start)) + const removed: { start: number; length: number }[] = [] + display = display.replace( + /!\[Uploading (?:\\.|[^\\[\]])*…\]\(\)/g, + (token: string, offset: number) => { + if (active.has(offset) || !known.current.has(token)) { + return token + } + removed.push({ start: offset, length: token.length }) + return '' + } + ) + let shift = 0 + let index = 0 + markers = markers.map((marker) => { + while (index < removed.length && removed[index].start < marker.start) { + shift += removed[index++].length + } + return { ...marker, start: marker.start - shift, end: marker.end - shift } + }) + publish(display, markers) + return from + inserted.length + } + const receive = (files: File[], selection?: { start: number; end: number }) => { + let caret = + selection?.start ?? textarea.current?.selectionStart ?? current.current.display.length + let end = selection?.end ?? textarea.current?.selectionEnd ?? caret + return uploads.receive(files, (file) => { + const id = createNonSecureContextUuid() + const label = (file.name || 'image.png').replace(/[\\[\]]/g, '\\$&').replace(/[\r\n]/g, ' ') + const token = `![Uploading ${label}…]()` + known.current.add(token) + const previous = current.current + before.current = { start: caret, end } + caret = change(previous.display.slice(0, caret) + previous.display.slice(end)) + const clean = current.current + const delta = token.length + const marker = { start: caret, end: caret + delta, id } + publish( + clean.display.slice(0, caret) + token + clean.display.slice(caret), + [ + ...clean.markers.map((entry) => + entry.start >= caret + ? { ...entry, start: entry.start + delta, end: entry.end + delta } + : entry + ), + marker + ].sort((a, b) => a.start - b.start) + ) + caret = marker.end + end = caret + const nextCaret = caret + requestAnimationFrame(() => textarea.current?.setSelectionRange(nextCaret, nextCaret)) + return (url?: string) => { + const latest = current.current + const owned = latest.markers.find((entry) => entry.id === id) + if (!owned) { + return + } + const markdown = url + ? `![${(file.name || 'image').replace(/[\\[\]]/g, '\\$&')}](<${url}>)` + : '' + const shift = markdown.length - (owned.end - owned.start) + publish( + latest.display.slice(0, owned.start) + markdown + latest.display.slice(owned.end), + latest.markers + .filter((entry) => entry !== owned) + .map((entry) => + entry.start >= owned.end + ? { ...entry, start: entry.start + shift, end: entry.end + shift } + : entry + ) + ) + } + }) + } + const events = useGitHubImageEvents( + textarea, + receive, + () => { + const node = textarea.current + return node && document.activeElement === node + ? { start: node.selectionStart, end: node.selectionEnd } + : { start: current.current.display.length, end: current.current.display.length } + }, + () => options.enabled !== false && !textarea.current?.disabled + ) + return { + value: displayed.display, + change, + attachDrop: events.attachDrop, + pending: uploads.pending, + onBeforeInput: (event: React.FormEvent) => { + before.current = { + start: event.currentTarget.selectionStart, + end: event.currentTarget.selectionEnd + } + }, + onPaste: (event: React.ClipboardEvent) => { + if (events.paste(event)) { + event.preventDefault() + } + } + } +} diff --git a/src/renderer/src/components/pull-request-page/comments/composer.tsx b/src/renderer/src/components/pull-request-page/comments/composer.tsx index 567bec045d6..36cd9e80fa9 100644 --- a/src/renderer/src/components/pull-request-page/comments/composer.tsx +++ b/src/renderer/src/components/pull-request-page/comments/composer.tsx @@ -1,3 +1,5 @@ +import { useGitHubImagePending } from '@/components/github/use-github-image-uploads' +import { githubCommentImageUploadAddress } from '@/components/github/github-comment-image-upload-address' import React, { useCallback, useRef, useState } from 'react' import { LoaderCircle, Send } from 'lucide-react' import { toast } from 'sonner' @@ -42,6 +44,7 @@ export function GHCommentComposer({ mentionOptions: MentionOption[] onCommentAdded: (comment: PRComment) => void }): React.JSX.Element { + const images = useGitHubImagePending() const [body, setBody] = useState('') const [submitting, setSubmitting] = useState(false) const textareaRef = useRef(null) @@ -57,6 +60,9 @@ export function GHCommentComposer({ }, []) const handleSubmit = useCallback(async () => { + if (images.pendingRef.current) { + return + } const bodyState = getCommentBodySubmitState(body) if (bodyState.status === 'empty') { return @@ -112,6 +118,7 @@ export function GHCommentComposer({ } }, [ autoGrow, + images.pendingRef, body, mountedRef, repoPath, @@ -137,6 +144,17 @@ export function GHCommentComposer({ return (
{ @@ -157,7 +175,7 @@ export function GHCommentComposer({ type="button" size="icon-sm" onClick={handleSubmit} - disabled={!canSubmitComment || submitting} + disabled={!canSubmitComment || submitting || images.pending} className="absolute bottom-3 right-3 shadow-sm" aria-label={translate('auto.components.PullRequestPage.161d91ef02', 'Send comment')} > diff --git a/src/renderer/src/components/pull-request-page/comments/reply-form.tsx b/src/renderer/src/components/pull-request-page/comments/reply-form.tsx index 18879f02665..f534de288af 100644 --- a/src/renderer/src/components/pull-request-page/comments/reply-form.tsx +++ b/src/renderer/src/components/pull-request-page/comments/reply-form.tsx @@ -1,3 +1,5 @@ +import { useGitHubImagePending } from '@/components/github/use-github-image-uploads' +import type { GitHubImageUploadAddress } from '@/components/github/github-image-upload' import React, { useCallback, useEffect, useRef, useState } from 'react' import { toast } from 'sonner' import { Button } from '@/components/ui/button' @@ -14,17 +16,20 @@ import { MentionTextarea } from '../mentions/textarea' export function CommentReplyForm({ className, + imageUploadAddress, placeholder, mentionOptions, onCancel, onSubmit }: { + imageUploadAddress?: GitHubImageUploadAddress className?: string placeholder: string mentionOptions: MentionOption[] onCancel: () => void onSubmit: (body: string) => Promise }): React.JSX.Element { + const images = useGitHubImagePending() const [body, setBody] = useState('') const [submitting, setSubmitting] = useState(false) const textareaRef = useRef(null) @@ -36,7 +41,7 @@ export function CommentReplyForm({ const submit = useCallback(async () => { const bodyState = getCommentBodySubmitState(body) - if (bodyState.status === 'empty' || submitting) { + if (bodyState.status === 'empty' || submitting || images.pendingRef.current) { return } if (bodyState.status === 'too-large-leading-whitespace') { @@ -70,12 +75,17 @@ export function CommentReplyForm({ setSubmitting(false) } } - }, [body, mountedRef, onSubmit, submitting]) + }, [body, images.pendingRef, mountedRef, onSubmit, submitting]) const canSubmitReply = hasBoundedCommentBodyText(body) return (
{translate('auto.components.PullRequestPage.6591b1fa82', 'Cancel')} -