Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | 21x 21x 128x 9x 2x 2x 9x 21x 60x 60x 60x 60x 60x 7x 60x 60x 60x 60x 1x 1x 60x 1x 1x 60x 68x 60x 68x 68x 5x 68x | import { Box, Stack, styled } from "@mui/material";
import { Markup } from "interweave";
import { useCallback, useEffect, useMemo } from "react";
import { Controller, SubmitHandler, useFormContext } from "react-hook-form";
import { MentionProps } from "react-mentions";
import { ConversationFormInput } from "../hooks/useCreateMessage";
import { useMentions } from "../hooks/useMentions";
import { useUndeletableWord } from "../hooks/useUndeletableWord";
import { TMentionedUser } from "../utils/functions";
import ConversationsMentionsInput from "./MentionInput";
const HASHTAG_DETECTION_REGEX = /#[A-Za-z0-9.,%_()-/]+/g;
const replacer = (
value: string | undefined,
shouldHideMentionIcon?: boolean,
) => {
if (!value) return "";
const result = value.replace(
/@\[([^[\]]{1,100})\]\(user:([^()]{1,50})\)/g,
(match: any, display: string) => {
const computedDisplay = display.replace(/^__|__$/g, "");
return shouldHideMentionIcon ? computedDisplay : `@${computedDisplay}`;
},
);
return result;
};
const ChatEditor: React.FC<
Partial<MentionProps> & {
mentionedUser?: TMentionedUser;
name: string;
onSubmit: SubmitHandler<ConversationFormInput>;
defaultMessage?: string;
shouldHideMentionIcon?: boolean;
disabled?: boolean;
}
> = ({
mentionedUser,
name,
onSubmit,
defaultMessage,
shouldHideMentionIcon,
disabled,
}) => {
const { control, setValue, watch } = useFormContext();
const el = document.getElementById("mention-input") as HTMLTextAreaElement;
const isHashtag = defaultMessage?.startsWith("#");
const { keyDownExtender, autoFocus } = useUndeletableWord(
el,
replacer(defaultMessage),
isHashtag,
);
useEffect(() => {
setValue(name, defaultMessage);
}, [defaultMessage]);
const builder = mentionedUser
? `@[__${mentionedUser.user}__](user:__${mentionedUser.authorAccID}__)`
: "";
const tagBuilder: undefined | string = builder;
const { onSourceAdd } = useMentions(tagBuilder);
const handleKeyUp = (event: any) => {
const conversationInput = watch(name);
Iif (event.key === "Enter" && !event.shiftKey && conversationInput) {
event.preventDefault();
!disabled && onSubmit({ conversationInput });
}
};
const handleKeyDown = (event: any) => {
Iif (event.key === "Enter" && !event.shiftKey) {
event.preventDefault();
}
Iif (defaultMessage) keyDownExtender(event);
};
const marksFormatter = useCallback((value: string) => {
return replacer(value, shouldHideMentionIcon).replace(
HASHTAG_DETECTION_REGEX,
"<mark>$&</mark>",
);
}, []);
return (
<Controller
name={name}
control={control}
render={({ field: { onChange, ref: inputRef, ...rest } }) => {
const result = marksFormatter(rest.value);
return (
<Stack direction="row" position={"relative"} flex={1}>
<BackdropContainer>
<Markup className="conversations-mark" content={result} />
</BackdropContainer>
<ConversationsMentionsInput
onChange={(e) => {
return onChange(e);
}}
inputRef={inputRef}
shouldAutoFocus={autoFocus}
onAdd={onSourceAdd}
handleKeyUp={handleKeyUp}
handleKeyDown={handleKeyDown}
{...rest}
/>
</Stack>
);
}}
/>
);
};
const BackdropContainer = styled(Box)(() => ({
position: "absolute",
width: "100%",
top: 0,
bottom: 0,
zIndex: 1,
overflow: "visible",
pointerEvents: "none",
transition: "transform 1s",
fontSize: "14px",
whiteSpace: "pre-wrap",
color: "transparent",
padding: "8px",
fontWeight: 350,
"& > *": {
width: "100%",
display: "block",
wordBreak: "break-word",
zIndex: 1,
fontWeight: 350,
},
"& > span > mark": {
backgroundColor: "transparent",
zIndex: 1,
fontWeight: 350,
color: "#FF8124",
textShadow: `0 0 1px ${"#FF8124"}`,
},
}));
export default ChatEditor;
|