Limit chat input to max characters. Closes #2109

This commit is contained in:
Gabe Kangas 2022-10-08 22:33:43 -07:00
parent 2c7be0db10
commit cd3722d9f1
No known key found for this signature in database
GPG key ID: 9A56337728BC81EA
4 changed files with 80 additions and 31 deletions

View file

@ -24,6 +24,12 @@
}
}
.maxCharacters {
border-style: solid;
border-width: 1px;
border-color: red;
}
div[role='textbox'] {
font-size: 0.9rem;
padding: 0.3rem;

View file

@ -50,9 +50,9 @@ export default {
},
} as ComponentMeta<typeof ChatTextField>;
const Template: ComponentStory<typeof ChatTextField> = () => (
const Template: ComponentStory<typeof ChatTextField> = args => (
<RecoilRoot>
<ChatTextField />
<ChatTextField {...args} />
</RecoilRoot>
);
@ -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.',
};

View file

@ -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<ChatTextFieldProps> = () => {
let count = 0;
node.children.forEach(child => {
count += getCharacterCount(child);
});
return count;
};
export type ChatTextFieldProps = {
defaultText?: string;
};
const characterLimit = 300;
export const ChatTextField: FC<ChatTextFieldProps> = ({ defaultText }) => {
const [showEmojis, setShowEmojis] = useState(false);
const [characterCount, setCharacterCount] = useState(defaultText?.length);
const websocketService = useRecoilValue<WebsocketService>(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<ChatTextFieldProps> = () => {
focus: Editor.end(editor, []),
},
});
setCharacterCount(0);
};
const createImageNode = (alt, src, name): ImageNode => ({
@ -164,30 +187,47 @@ export const ChatTextField: FC<ChatTextFieldProps> = () => {
}
};
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<ChatTextFieldProps> = () => {
return (
<div className={styles.root}>
<div className={styles.inputWrap}>
<div
className={classNames(
styles.inputWrap,
characterCount >= characterLimit && styles.maxCharacters,
)}
>
<Slate editor={editor} value={defaultEditorValue}>
<Editable
onKeyDown={onKeyDown}
onPaste={onPaste}
renderElement={renderElement}
placeholder="Chat message goes here..."
style={{ width: '100%' }}

View file

@ -1,17 +1,14 @@
import React, { useEffect, useRef, useState } from 'react';
import React, { FC, useEffect, useRef, useState } from 'react';
import { createPicker } from 'picmo';
const CUSTOM_EMOJI_URL = '/api/emoji';
interface Props {
// eslint-disable-next-line react/no-unused-prop-types
export type EmojiPickerProps = {
onEmojiSelect: (emoji: string) => 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<EmojiPickerProps> = ({ 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]);