diff --git a/web/components/chat/ChatTextField/ChatTextField.module.scss b/web/components/chat/ChatTextField/ChatTextField.module.scss index 04f352d70..187da238a 100644 --- a/web/components/chat/ChatTextField/ChatTextField.module.scss +++ b/web/components/chat/ChatTextField/ChatTextField.module.scss @@ -24,6 +24,12 @@ } } + .maxCharacters { + border-style: solid; + border-width: 1px; + border-color: red; + } + div[role='textbox'] { font-size: 0.9rem; padding: 0.3rem; diff --git a/web/components/chat/ChatTextField/ChatTextField.stories.tsx b/web/components/chat/ChatTextField/ChatTextField.stories.tsx index ea3da2df4..3db664ee0 100644 --- a/web/components/chat/ChatTextField/ChatTextField.stories.tsx +++ b/web/components/chat/ChatTextField/ChatTextField.stories.tsx @@ -50,9 +50,9 @@ export default { }, } as ComponentMeta; -const Template: ComponentStory = () => ( +const Template: ComponentStory = args => ( - + ); @@ -60,7 +60,7 @@ export const Example = Template.bind({}); export const LongerMessage = Template.bind({}); LongerMessage.args = { - value: + defaultText: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', }; diff --git a/web/components/chat/ChatTextField/ChatTextField.tsx b/web/components/chat/ChatTextField/ChatTextField.tsx index 596d620da..9452c4160 100644 --- a/web/components/chat/ChatTextField/ChatTextField.tsx +++ b/web/components/chat/ChatTextField/ChatTextField.tsx @@ -5,6 +5,7 @@ import { useRecoilValue } from 'recoil'; import { Transforms, createEditor, BaseEditor, Text, Descendant, Editor, Node, Path } from 'slate'; import { Slate, Editable, withReact, ReactEditor, useSelected, useFocused } from 'slate-react'; import dynamic from 'next/dynamic'; +import classNames from 'classnames'; import WebsocketService from '../../../services/websocket-service'; import { websocketServiceAtom } from '../../stores/ClientConfigStore'; import { MessageType } from '../../../interfaces/socket-events'; @@ -94,17 +95,38 @@ const serialize = node => { } }; -export type ChatTextFieldProps = {}; +const getCharacterCount = node => { + if (Text.isText(node)) { + return node.text.length; + } + if (node.type === 'image') { + return 5; + } -export const ChatTextField: FC = () => { + let count = 0; + node.children.forEach(child => { + count += getCharacterCount(child); + }); + + return count; +}; + +export type ChatTextFieldProps = { + defaultText?: string; +}; + +const characterLimit = 300; + +export const ChatTextField: FC = ({ defaultText }) => { const [showEmojis, setShowEmojis] = useState(false); + const [characterCount, setCharacterCount] = useState(defaultText?.length); const websocketService = useRecoilValue(websocketServiceAtom); const editor = useMemo(() => withReact(withImages(createEditor())), []); const defaultEditorValue: Descendant[] = [ { type: 'paragraph', - children: [{ text: '' }], + children: [{ text: defaultText || '' }], }, ]; @@ -124,6 +146,7 @@ export const ChatTextField: FC = () => { focus: Editor.end(editor, []), }, }); + setCharacterCount(0); }; const createImageNode = (alt, src, name): ImageNode => ({ @@ -164,30 +187,47 @@ export const ChatTextField: FC = () => { } }; - const onEmojiSelect = (e: any) => { + // Native emoji + const onEmojiSelect = (emoji: string) => { ReactEditor.focus(editor); - - if (e.url) { - // Custom emoji - const { url } = e; - insertImage(url, url); - } else { - // Native emoji - const { emoji } = e; - Transforms.insertText(editor, emoji); - } + Transforms.insertText(editor, emoji); }; - const onCustomEmojiSelect = (e: any) => { + const onCustomEmojiSelect = (name: string, emoji: string) => { ReactEditor.focus(editor); - const { url } = e; - insertImage(url, url); + insertImage(emoji, name); }; const onKeyDown = (e: React.KeyboardEvent) => { + const charCount = getCharacterCount(editor) + 1; + + // Send the message when hitting enter. if (e.key === 'Enter') { e.preventDefault(); sendMessage(); + return; + } + + // Always allow backspace. + if (e.key === 'Backspace') { + setCharacterCount(charCount - 1); + return; + } + + // Limit the number of characters. + if (charCount + 1 > characterLimit) { + e.preventDefault(); + } + + setCharacterCount(charCount + 1); + }; + + const onPaste = (e: React.ClipboardEvent) => { + const text = e.clipboardData.getData('text/plain'); + + const { length } = text; + if (characterCount + length > characterLimit) { + e.preventDefault(); } }; @@ -202,10 +242,16 @@ export const ChatTextField: FC = () => { return (
-
+
= characterLimit && styles.maxCharacters, + )} + > void; - onCustomEmojiSelect: (emoji: string) => void; -} + onCustomEmojiSelect: (name: string, url: string) => void; +}; -// eslint-disable-next-line @typescript-eslint/no-unused-vars -export const EmojiPicker = (props: Props) => { +export const EmojiPicker: FC = ({ onEmojiSelect, onCustomEmojiSelect }) => { const [customEmoji, setCustomEmoji] = useState([]); - const { onEmojiSelect, onCustomEmojiSelect } = props; const ref = useRef(); const getCustomEmoji = async () => { @@ -46,9 +43,9 @@ export const EmojiPicker = (props: Props) => { }); picker.addEventListener('emoji:select', event => { if (event.url) { - onCustomEmojiSelect(event); + onCustomEmojiSelect(event.name, event.url); } else { - onEmojiSelect(event); + onEmojiSelect(event.emoji); } }); }, [customEmoji]);