Skip to content

Commit

Permalink
fix: mention (#1095)
Browse files Browse the repository at this point in the history
- Fix the issue where the mention feature does not work properly in the
input component
- Fix the issue where unnecessary spaces are added between mention texts
when editing an already mentioned message
  • Loading branch information
bang9 committed May 20, 2024
1 parent 15976c5 commit 479c819
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/ui/MessageInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,15 @@ const MessageInput = React.forwardRef<HTMLInputElement, MessageInputProps>((prop
.map((token) => {
if (token.type === TOKEN_TYPES.mention) {
const mentionedUser = mentionedUsers.find((user) => user.userId === token.userId);
const nickname = `${USER_MENTION_PREFIX}${
mentionedUser?.nickname || token.value || stringSet.MENTION_NAME__NO_NAME
}`;
const nickname = `${USER_MENTION_PREFIX}${mentionedUser?.nickname || token.value || stringSet.MENTION_NAME__NO_NAME}`;
return renderMentionLabelToString({
userId: token.userId,
nickname,
});
}
return sanitizeString(token.value);
})
.join(' ');
.join('');
}
} else {
/* mention disabled */
Expand Down Expand Up @@ -267,15 +265,15 @@ const MessageInput = React.forwardRef<HTMLInputElement, MessageInputProps>((prop
if (isMentionEnabled && mentionSelectedUser) {
const { targetString, startNodeIndex, startOffsetIndex, endNodeIndex, endOffsetIndex } = targetStringInfo;
const textField = internalRef?.current;
if (targetString && startNodeIndex && startOffsetIndex && textField && endNodeIndex) {
if (targetString && startNodeIndex !== null && startOffsetIndex !== null && endOffsetIndex !== null && endNodeIndex !== null && textField) {
// const textField = document.getElementById(textFieldId);
const childNodes = nodeListToArray(textField?.childNodes);
const startNodeTextContent: string = childNodes[startNodeIndex]?.textContent ?? '';
const frontTextNode = document?.createTextNode(
const frontTextNode = document.createTextNode(
startNodeTextContent.slice(0, startOffsetIndex),
);
const endNodeTextContent: string = childNodes[endNodeIndex]?.textContent ?? '';
const backTextNode = endOffsetIndex && document?.createTextNode(
const backTextNode = endOffsetIndex && document.createTextNode(
`\u00A0${endNodeTextContent.slice(endOffsetIndex)}`,
);
const mentionLabel = renderMentionLabelToString({
Expand Down Expand Up @@ -374,7 +372,7 @@ const MessageInput = React.forwardRef<HTMLInputElement, MessageInputProps>((prop
* targetString could be ''
* startNodeIndex and startOffsetIndex could be null
*/
const targetString = textStack && startOffsetIndex ? textStack.slice(startOffsetIndex) : ''; // include template character
const targetString = textStack && startOffsetIndex !== null ? textStack.slice(startOffsetIndex) : ''; // include template character
setTargetStringInfo({
targetString,
startNodeIndex,
Expand Down

0 comments on commit 479c819

Please sign in to comment.