diff --git a/resources/js/components/commit-composer.tsx b/resources/js/components/commit-composer.tsx index 115756896e..b35ac594b6 100644 --- a/resources/js/components/commit-composer.tsx +++ b/resources/js/components/commit-composer.tsx @@ -1,6 +1,6 @@ import { Form, Link, usePage } from '@inertiajs/react'; import { ChartBar, Clock3 } from 'lucide-react'; -import type { KeyboardEvent } from 'react'; +import type { DragEvent, KeyboardEvent } from 'react'; import { useEffect, useRef, useState } from 'react'; import CommitController from '@/actions/App/Http/Controllers/CommitController'; import ScheduledCommitController from '@/actions/App/Http/Controllers/ScheduledCommitController'; @@ -213,12 +213,20 @@ function CommitComposerBody({ maxLength: isIssue ? maxBodyLength : maxRawBodyLength, }); + const cancelFileDrop = (event: DragEvent) => { + if (event.dataTransfer.types.includes('Files')) { + event.preventDefault(); + } + }; + return (
- {attachments.isDraggingFiles && ( + {!isIssue && attachments.isDraggingFiles && ( )} preview.handleShortcut(event) || @@ -390,7 +400,15 @@ function CommitComposerBody({ /> )} 0 + ? 'Issues carry text only: images will not be attached.' + : !isIssue + ? (attachments.error ?? + imageErrorFrom(errors) ?? + undefined) + : undefined + } className="mt-1" /> @@ -671,15 +689,21 @@ export function CommitComposer({ poll === null && !isScheduling, initialKind: initialIssue, + onStart: () => { + attachments.clear(); + }, onCreated: () => { drop(); + attachments.clear(); focusBody(id); }, }); const isCommittable = !attachments.reading && - (hasImages || characterCount(body.trim()) >= minBodyLength) && + (issue.command !== null + ? characterCount(body.trim()) >= minBodyLength + : hasImages || characterCount(body.trim()) >= minBodyLength) && (issue.command !== null ? characterCount(body) : bodyCharacterCount(body)) <= maxBodyLength && diff --git a/resources/js/components/issue-command.tsx b/resources/js/components/issue-command.tsx index 56b0513c75..343cc2881f 100644 --- a/resources/js/components/issue-command.tsx +++ b/resources/js/components/issue-command.tsx @@ -36,12 +36,14 @@ export function useIssueCommand({ updateBody, enabled, initialKind = null, + onStart, onCreated, }: { body: string; updateBody: (body: string) => void; enabled: boolean; initialKind?: IssueKind | null; + onStart?: () => void; onCreated: () => void; }) { const [kind, setKind] = useState( @@ -69,6 +71,7 @@ export function useIssueCommand({ setKind(next); setSelected(0); updateBody(rest); + onStart?.(); }; const change = (value: string) => { diff --git a/tests/Unit/ComposerDraft.test.tsx b/tests/Unit/ComposerDraft.test.tsx index 6b361215c5..8f030b963b 100644 --- a/tests/Unit/ComposerDraft.test.tsx +++ b/tests/Unit/ComposerDraft.test.tsx @@ -99,6 +99,8 @@ const { LinkCardPicker } = const { PollEditor } = await import('../../resources/js/components/poll-editor'); const { GraphRail } = await import('../../resources/js/components/graph-rail'); +const InputError = (await import('../../resources/js/components/input-error')) + .default; type Element = ReactElement>; @@ -617,6 +619,36 @@ test('typing a command and a space turns the draft into an issue, and backspace ); assert.equal(find('data-test', 'add-poll-button'), undefined); assert.equal(view.form.props.onBefore(), false); + const issueTextarea = view.fields.find( + (element) => element.type === MentionTextarea, + )!; + assert.equal(issueTextarea.props.onPaste, undefined); + assert.equal( + view.fields.some( + (element) => element.props.onDragEnter !== undefined, + ), + false, + ); + const dropzone = view.fields.find( + (element) => element.props.onDragOver !== undefined, + )!; + const onDragOver = dropzone.props.onDragOver as (event: unknown) => void; + let prevented = false; + onDragOver({ + dataTransfer: { types: ['Files'] }, + preventDefault: () => { + prevented = true; + }, + }); + assert.equal(prevented, true); + prevented = false; + onDragOver({ + dataTransfer: { types: ['text/plain'] }, + preventDefault: () => { + prevented = true; + }, + }); + assert.equal(prevented, false); keyDown(view, 'Backspace'); view = render(); @@ -912,3 +944,31 @@ test('ignores further submits while a commit is being posted', () => { form.props.onFinish(); assert.equal(form.props.onBefore(), true); }); + +test('notifies the user and preserves previews if images are restored while in issue mode', () => { + let view = render({ issue: 'bug' }); + assert.ok( + view.fields.some((element) => element.type === IssueCommandBadge), + ); + const initialPreview = view.fields.find( + (element) => element.type === ImagePickerPreviews, + )!; + assert.deepEqual(initialPreview.props.images, []); + + draft.images = [image]; + draft.descriptions = ['A restored screenshot']; + view = render({ issue: 'bug' }); + + const preview = view.fields.find( + (element) => element.type === ImagePickerPreviews, + )!; + assert.deepEqual(preview.props.images, [image]); + + const error = view.fields.find( + (element) => + element.type === InputError && + element.props.message === + 'Issues carry text only: images will not be attached.', + ); + assert.ok(error); +});