Skip to content

Commit 49c2e74

Browse files
author
Dan Carbonell
committed
limit textarea paste
1 parent f8622f6 commit 49c2e74

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/components/MessageInput/hooks/messageInput.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,16 @@ function messageInputReducer(state, action) {
212212
*/
213213
export default function useMessageInput(props) {
214214
const {
215+
additionalTextareaProps,
216+
clearEditingState,
215217
doImageUploadRequest,
216218
doFileUploadRequest,
219+
errorHandler,
217220
focus,
218221
message,
219-
clearEditingState,
222+
noFiles,
220223
overrideSubmitHandler,
221224
parent,
222-
noFiles,
223-
errorHandler,
224225
publishTypingEvent,
225226
} = props;
226227

@@ -673,9 +674,7 @@ export default function useMessageInput(props) {
673674
(async (event) => {
674675
// TODO: Move this handler to package with ImageDropzone
675676
const { items } = event.clipboardData;
676-
if (!dataTransferItemsHaveFiles(items)) {
677-
return;
678-
}
677+
if (!dataTransferItemsHaveFiles(items)) return;
679678

680679
event.preventDefault();
681680
// Get a promise for the plain text in case no files are
@@ -686,6 +685,7 @@ export default function useMessageInput(props) {
686685
const plainTextItem = [...items].find(
687686
({ kind, type }) => kind === 'string' && type === 'text/plain',
688687
);
688+
689689
if (plainTextItem) {
690690
plainTextPromise = new Promise((resolve) => {
691691
plainTextItem.getAsString((s) => {
@@ -699,14 +699,24 @@ export default function useMessageInput(props) {
699699
uploadNewFiles(fileLikes);
700700
return;
701701
}
702+
702703
// fallback to regular text paste
703704
if (plainTextPromise) {
704-
const s = await plainTextPromise;
705-
insertText(s);
705+
const { maxLength } = additionalTextareaProps;
706+
const pastedText = await plainTextPromise;
707+
708+
if (pastedText.length > maxLength) {
709+
dispatch({
710+
type: 'setText',
711+
getNewText: () => pastedText.slice(0, maxLength),
712+
});
713+
} else {
714+
insertText(pastedText);
715+
}
706716
}
707717
})(e);
708718
},
709-
[uploadNewFiles, insertText],
719+
[additionalTextareaProps, insertText, uploadNewFiles],
710720
);
711721

712722
const isUploadEnabled = channel?.getConfig?.()?.uploads !== false;

0 commit comments

Comments
 (0)