Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: give preference to pasting text over files #2552

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/components/MessageInput/__tests__/MessageInput.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,64 @@ function axeNoViolations(container) {
expect(results).toHaveNoViolations();
});

it('gives preference to pasting text over files', async () => {
const doImageUploadRequest = mockUploadApi();
const doFileUploadRequest = mockUploadApi();
const pastedString = 'pasted string';
const { container } = await renderComponent({
messageInputProps: {
doFileUploadRequest,
doImageUploadRequest,
},
});

const file = getFile();
const image = getImage();

const clipboardEvent = new Event('paste', {
bubbles: true,
});
// set `clipboardData`. Mock DataTransfer object
clipboardEvent.clipboardData = {
items: [
{
getAsFile: () => file,
kind: 'file',
},
{
getAsFile: () => image,
kind: 'file',
},
{
getAsString: (cb) => cb(pastedString),
kind: 'string',
type: 'text/plain',
},
],
};
const formElement = screen.getByPlaceholderText(inputPlaceholder);
await act(async () => {
await formElement.dispatchEvent(clipboardEvent);
});

await waitFor(() => {
expect(doFileUploadRequest).not.toHaveBeenCalled();
expect(doImageUploadRequest).not.toHaveBeenCalled();
expect(screen.queryByTestId(IMAGE_PREVIEW_TEST_ID)).not.toBeInTheDocument();
expect(screen.queryByTestId(FILE_PREVIEW_TEST_ID)).not.toBeInTheDocument();
expect(screen.queryByText(filename)).not.toBeInTheDocument();
expect(screen.queryByTestId(ATTACHMENT_PREVIEW_LIST_TEST_ID)).not.toBeInTheDocument();
if (componentName === 'EditMessageForm') {
expect(formElement.value.startsWith(pastedString)).toBeTruthy();
} else {
expect(formElement).toHaveValue(pastedString);
}
});

const results = await axe(container);
expect(results).toHaveNoViolations();
});

it('Should upload an image when it is dropped on the dropzone', async () => {
const doImageUploadRequest = mockUploadApi();
const { container } = await renderComponent({
Expand Down
14 changes: 4 additions & 10 deletions src/components/MessageInput/hooks/usePasteHandler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { useCallback } from 'react';
import {
// dataTransferItemsHaveFiles,
dataTransferItemsToFiles,
FileLike,
} from '../../ReactFileUtilities';
import { dataTransferItemsToFiles, FileLike } from '../../ReactFileUtilities';
import type { EnrichURLsController } from './useLinkPreviews';
import { SetLinkPreviewMode } from '../types';

Expand Down Expand Up @@ -35,17 +31,15 @@ export const usePasteHandler = (
}

const fileLikes = await dataTransferItemsToFiles(Array.from(items));
if (fileLikes.length && isUploadEnabled) {
uploadNewFiles(fileLikes);
return;
}

// fallback to regular text paste
if (plainTextPromise) {
const pastedText = await plainTextPromise;
insertText(pastedText);
findAndEnqueueURLsToEnrich?.(pastedText, SetLinkPreviewMode.UPSERT);
findAndEnqueueURLsToEnrich?.flush();
} else if (fileLikes.length && isUploadEnabled) {
uploadNewFiles(fileLikes);
return;
}
})(clipboardEvent);
},
Expand Down
Loading