Skip to content

Commit 78b1f87

Browse files
committed
fix: prevent pasting or dropping images into issue commands
1 parent 6a8dceb commit 78b1f87

3 files changed

Lines changed: 63 additions & 10 deletions

File tree

‎resources/js/components/commit-composer.tsx‎

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Form, Link, usePage } from '@inertiajs/react';
22
import { ChartBar, Clock3 } from 'lucide-react';
3-
import type { KeyboardEvent } from 'react';
3+
import type { DragEvent, KeyboardEvent } from 'react';
44
import { useEffect, useRef, useState } from 'react';
55
import CommitController from '@/actions/App/Http/Controllers/CommitController';
66
import ScheduledCommitController from '@/actions/App/Http/Controllers/ScheduledCommitController';
@@ -205,12 +205,20 @@ function CommitComposerBody({
205205
maxLength: isIssue ? maxBodyLength : maxRawBodyLength,
206206
});
207207

208+
const cancelFileDrop = (event: DragEvent<HTMLElement>) => {
209+
if (event.dataTransfer.types.includes('Files')) {
210+
event.preventDefault();
211+
}
212+
};
213+
208214
return (
209215
<div
210216
className={cn('relative flex', { 'min-h-0': isInDialog })}
211-
{...(poll === null ? attachments.dropzone : {})}
217+
{...(poll === null && !isIssue
218+
? attachments.dropzone
219+
: { onDragOver: cancelFileDrop, onDrop: cancelFileDrop })}
212220
>
213-
{attachments.isDraggingFiles && (
221+
{!isIssue && attachments.isDraggingFiles && (
214222
<ImageDropOverlay className="inset-2" />
215223
)}
216224
<GraphRail
@@ -284,7 +292,9 @@ function CommitComposerBody({
284292
value={body}
285293
onValueChange={issue.change}
286294
onPaste={
287-
poll === null ? attachments.onPaste : undefined
295+
poll === null && !isIssue
296+
? attachments.onPaste
297+
: undefined
288298
}
289299
onKeyDown={(event) =>
290300
preview.handleShortcut(event) ||
@@ -364,18 +374,22 @@ function CommitComposerBody({
364374
/>
365375
<InputError message={error} />
366376

367-
{!preview.isPreviewing && (
377+
{!isIssue && !preview.isPreviewing && (
368378
<ImagePickerPreviews
369379
images={attachments.images}
370380
descriptions={attachments.descriptions}
371381
onDescribe={attachments.describe}
372382
onRemove={attachments.remove}
373383
/>
374384
)}
375-
<InputError
376-
message={attachments.error ?? imageErrorFrom(errors)}
377-
className="mt-1"
378-
/>
385+
{!isIssue && (
386+
<InputError
387+
message={
388+
attachments.error ?? imageErrorFrom(errors)
389+
}
390+
className="mt-1"
391+
/>
392+
)}
379393

380394
{poll && (
381395
<div className="mt-2 grid">
@@ -645,15 +659,21 @@ export function CommitComposer({
645659
poll === null &&
646660
!isScheduling,
647661
initialKind: initialIssue,
662+
onStart: () => {
663+
attachments.clear();
664+
},
648665
onCreated: () => {
649666
drop();
667+
attachments.clear();
650668
focusBody(id);
651669
},
652670
});
653671

654672
const isCommittable =
655673
!attachments.reading &&
656-
(hasImages || characterCount(body.trim()) >= minBodyLength) &&
674+
(issue.command !== null
675+
? characterCount(body.trim()) >= minBodyLength
676+
: hasImages || characterCount(body.trim()) >= minBodyLength) &&
657677
(issue.command !== null
658678
? characterCount(body)
659679
: bodyCharacterCount(body)) <= maxBodyLength &&

‎resources/js/components/issue-command.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ export function useIssueCommand({
3636
updateBody,
3737
enabled,
3838
initialKind = null,
39+
onStart,
3940
onCreated,
4041
}: {
4142
body: string;
4243
updateBody: (body: string) => void;
4344
enabled: boolean;
4445
initialKind?: IssueKind | null;
46+
onStart?: () => void;
4547
onCreated: () => void;
4648
}) {
4749
const [kind, setKind] = useState<IssueKind | null>(
@@ -69,6 +71,7 @@ export function useIssueCommand({
6971
setKind(next);
7072
setSelected(0);
7173
updateBody(rest);
74+
onStart?.();
7275
};
7376

7477
const change = (value: string) => {

‎tests/Unit/ComposerDraft.test.tsx‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,36 @@ test('typing a command and a space turns the draft into an issue, and backspace
537537
);
538538
assert.equal(find('data-test', 'add-poll-button'), undefined);
539539
assert.equal(view.form.props.onBefore(), false);
540+
const issueTextarea = view.fields.find(
541+
(element) => element.type === MentionTextarea,
542+
)!;
543+
assert.equal(issueTextarea.props.onPaste, undefined);
544+
assert.equal(
545+
view.fields.some(
546+
(element) => element.props.onDragEnter !== undefined,
547+
),
548+
false,
549+
);
550+
const dropzone = view.fields.find(
551+
(element) => element.props.onDragOver !== undefined,
552+
)!;
553+
const onDragOver = dropzone.props.onDragOver as (event: unknown) => void;
554+
let prevented = false;
555+
onDragOver({
556+
dataTransfer: { types: ['Files'] },
557+
preventDefault: () => {
558+
prevented = true;
559+
},
560+
});
561+
assert.equal(prevented, true);
562+
prevented = false;
563+
onDragOver({
564+
dataTransfer: { types: ['text/plain'] },
565+
preventDefault: () => {
566+
prevented = true;
567+
},
568+
});
569+
assert.equal(prevented, false);
540570

541571
keyDown(view, 'Backspace');
542572
view = render();

0 commit comments

Comments
 (0)