Skip to content

Commit afc7e72

Browse files
author
Dan Carbonell
committed
move paste truncate to insertText
1 parent 4fc9d3a commit afc7e72

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

src/components/MessageInput/hooks/messageInput.js

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-check
21
import {
32
useReducer,
43
useEffect,
@@ -62,7 +61,7 @@ function initState(message) {
6261
};
6362
}
6463

65-
// if message prop is defined, get imageuploads, fileuploads, text, etc. from it
64+
// if message prop is defined, get image uploads, file uploads, text, etc. from it
6665
const imageUploads =
6766
message.attachments
6867
?.filter(({ type }) => type === 'image')
@@ -225,12 +224,16 @@ export default function useMessageInput(props) {
225224
publishTypingEvent,
226225
} = props;
227226

227+
const {
228+
channel,
229+
editMessage,
230+
maxNumberOfFiles,
231+
multipleUploads,
232+
sendMessage,
233+
} = useContext(ChannelContext);
234+
228235
const [state, dispatch] = useReducer(messageInputReducer, message, initState);
229-
const textareaRef = useRef(
230-
/** @type {HTMLTextAreaElement | undefined} */ (undefined),
231-
);
232-
const emojiPickerRef = useRef(/** @type {HTMLDivElement | null} */ (null));
233-
const channelContext = useContext(ChannelContext);
236+
234237
const {
235238
text,
236239
imageOrder,
@@ -241,10 +244,13 @@ export default function useMessageInput(props) {
241244
numberOfUploads,
242245
mentioned_users,
243246
} = state;
244-
const { channel, editMessage, sendMessage } = channelContext;
245247

246-
// Focus
248+
const textareaRef = useRef(
249+
/** @type {HTMLTextAreaElement | undefined} */ (undefined),
250+
);
251+
const emojiPickerRef = useRef(/** @type {HTMLDivElement | null} */ (null));
247252

253+
// Focus
248254
useEffect(() => {
249255
if (focus && textareaRef.current) {
250256
textareaRef.current.focus();
@@ -253,6 +259,7 @@ export default function useMessageInput(props) {
253259

254260
// Text + cursor position
255261
const newCursorPosition = useRef(/** @type {number | null} */ (null));
262+
256263
const insertText = useCallback(
257264
(textToInsert) => {
258265
if (!textareaRef.current) {
@@ -263,18 +270,27 @@ export default function useMessageInput(props) {
263270
return;
264271
}
265272

266-
const textareaElement = textareaRef.current;
267-
const { selectionStart, selectionEnd } = textareaElement;
273+
const { maxLength } = additionalTextareaProps;
274+
const { selectionStart, selectionEnd } = textareaRef.current;
268275
newCursorPosition.current = selectionStart + textToInsert.length;
276+
269277
dispatch({
270278
type: 'setText',
271-
getNewText: (prevText) =>
272-
prevText.slice(0, selectionStart) +
273-
textToInsert +
274-
prevText.slice(selectionEnd),
279+
getNewText: (prevText) => {
280+
const updatedText =
281+
prevText.slice(0, selectionStart) +
282+
textToInsert +
283+
prevText.slice(selectionEnd);
284+
285+
if (updatedText.length > maxLength) {
286+
return updatedText.slice(0, maxLength);
287+
}
288+
289+
return updatedText;
290+
},
275291
});
276292
},
277-
[textareaRef, newCursorPosition],
293+
[additionalTextareaProps, newCursorPosition, textareaRef],
278294
);
279295

280296
useEffect(() => {
@@ -525,7 +541,7 @@ export default function useMessageInput(props) {
525541
dispatch({ type: 'setFileUpload', id, state: 'failed' });
526542
}
527543
if (!alreadyRemoved && errorHandler) {
528-
// TODO: verify if the paramaters passed to the error handler actually make sense
544+
// TODO: verify if the parameters passed to the error handler actually make sense
529545
errorHandler(e, 'upload-file', file);
530546
}
531547
return;
@@ -580,7 +596,7 @@ export default function useMessageInput(props) {
580596
dispatch({ type: 'setImageUpload', id, state: 'failed' });
581597
}
582598
if (!alreadyRemoved && errorHandler) {
583-
// TODO: verify if the paramaters passed to the error handler actually make sense
599+
// TODO: verify if the parameters passed to the error handler actually make sense
584600
errorHandler(e, 'upload-image', {
585601
id,
586602
file,
@@ -640,12 +656,12 @@ export default function useMessageInput(props) {
640656
// Number of files that the user can still add. Should never be more than the amount allowed by the API.
641657
// If multipleUploads is false, we only want to allow a single upload.
642658
const maxFilesAllowed = useMemo(() => {
643-
if (!channelContext.multipleUploads) return 1;
644-
if (channelContext.maxNumberOfFiles === undefined) {
659+
if (!multipleUploads) return 1;
660+
if (maxNumberOfFiles === undefined) {
645661
return apiMaxNumberOfFiles;
646662
}
647-
return channelContext.maxNumberOfFiles;
648-
}, [channelContext.maxNumberOfFiles, channelContext.multipleUploads]);
663+
return maxNumberOfFiles;
664+
}, [maxNumberOfFiles, multipleUploads]);
649665

650666
const maxFilesLeft = maxFilesAllowed - numberOfUploads;
651667

@@ -702,21 +718,12 @@ export default function useMessageInput(props) {
702718

703719
// fallback to regular text paste
704720
if (plainTextPromise) {
705-
const { maxLength } = additionalTextareaProps;
706721
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-
}
722+
insertText(pastedText);
716723
}
717724
})(e);
718725
},
719-
[additionalTextareaProps, insertText, uploadNewFiles],
726+
[insertText, uploadNewFiles],
720727
);
721728

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

0 commit comments

Comments
 (0)