2022-10-01 11:34:59 +03:00
|
|
|
import { Popover } from 'antd';
|
2022-09-07 10:00:28 +03:00
|
|
|
import React, { FC, useMemo, useState } from 'react';
|
2022-05-05 02:55:54 +03:00
|
|
|
import { useRecoilValue } from 'recoil';
|
2023-02-27 05:12:44 +03:00
|
|
|
import { Transforms, createEditor, BaseEditor, Text, Descendant, Editor } from 'slate';
|
2022-09-07 03:52:02 +03:00
|
|
|
import { Slate, Editable, withReact, ReactEditor, useSelected, useFocused } from 'slate-react';
|
2022-10-04 07:06:46 +03:00
|
|
|
import dynamic from 'next/dynamic';
|
2022-10-09 08:33:43 +03:00
|
|
|
import classNames from 'classnames';
|
2022-05-05 02:55:54 +03:00
|
|
|
import WebsocketService from '../../../services/websocket-service';
|
2022-08-22 04:47:08 +03:00
|
|
|
import { websocketServiceAtom } from '../../stores/ClientConfigStore';
|
2022-05-05 09:06:35 +03:00
|
|
|
import { MessageType } from '../../../interfaces/socket-events';
|
2022-09-07 10:00:28 +03:00
|
|
|
import styles from './ChatTextField.module.scss';
|
2022-05-04 00:55:13 +03:00
|
|
|
|
2022-10-04 07:06:46 +03:00
|
|
|
// Lazy loaded components
|
|
|
|
|
2023-01-11 03:39:12 +03:00
|
|
|
const EmojiPicker = dynamic(() => import('./EmojiPicker').then(mod => mod.EmojiPicker), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
2022-10-04 07:06:46 +03:00
|
|
|
|
2023-01-16 09:31:36 +03:00
|
|
|
const SendOutlined = dynamic(() => import('@ant-design/icons/SendOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
const SmileOutlined = dynamic(() => import('@ant-design/icons/SmileOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
2022-09-07 03:52:02 +03:00
|
|
|
type CustomElement = { type: 'paragraph' | 'span'; children: CustomText[] } | ImageNode;
|
2022-05-05 23:52:10 +03:00
|
|
|
type CustomText = { text: string };
|
|
|
|
|
2022-09-07 03:52:02 +03:00
|
|
|
type EmptyText = {
|
|
|
|
text: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ImageNode = {
|
|
|
|
type: 'image';
|
|
|
|
alt: string;
|
|
|
|
src: string;
|
|
|
|
name: string;
|
|
|
|
children: EmptyText[];
|
|
|
|
};
|
|
|
|
|
2022-05-05 23:52:10 +03:00
|
|
|
declare module 'slate' {
|
|
|
|
interface CustomTypes {
|
|
|
|
Editor: BaseEditor & ReactEditor;
|
|
|
|
Element: CustomElement;
|
|
|
|
Text: CustomText;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 03:52:02 +03:00
|
|
|
const Image = p => {
|
|
|
|
const { attributes, element, children } = p;
|
2022-05-04 00:55:13 +03:00
|
|
|
|
2022-09-07 03:52:02 +03:00
|
|
|
const selected = useSelected();
|
|
|
|
const focused = useFocused();
|
|
|
|
return (
|
|
|
|
<span {...attributes} contentEditable={false}>
|
|
|
|
<img
|
|
|
|
alt={element.alt}
|
|
|
|
src={element.src}
|
|
|
|
title={element.name}
|
|
|
|
style={{
|
|
|
|
display: 'inline',
|
|
|
|
maxWidth: '50px',
|
|
|
|
maxHeight: '20px',
|
|
|
|
boxShadow: `${selected && focused ? '0 0 0 3px #B4D5FF' : 'none'}`,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
);
|
2022-05-05 23:52:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const withImages = editor => {
|
|
|
|
const { isVoid } = editor;
|
|
|
|
|
2022-05-12 09:31:31 +03:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2022-05-05 23:52:10 +03:00
|
|
|
editor.isVoid = element => (element.type === 'image' ? true : isVoid(element));
|
2022-05-12 09:31:31 +03:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2022-05-05 23:52:10 +03:00
|
|
|
editor.isInline = element => element.type === 'image';
|
|
|
|
|
|
|
|
return editor;
|
|
|
|
};
|
|
|
|
|
|
|
|
const serialize = node => {
|
|
|
|
if (Text.isText(node)) {
|
2022-05-12 09:31:31 +03:00
|
|
|
const string = node.text;
|
2022-05-05 23:52:10 +03:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2022-09-07 03:52:02 +03:00
|
|
|
let children;
|
|
|
|
if (node.children.length === 0) {
|
|
|
|
children = [{ text: '' }];
|
|
|
|
} else {
|
|
|
|
children = node.children?.map(n => serialize(n)).join('');
|
|
|
|
}
|
2022-05-05 23:52:10 +03:00
|
|
|
|
|
|
|
switch (node.type) {
|
|
|
|
case 'paragraph':
|
|
|
|
return `<p>${children}</p>`;
|
|
|
|
case 'image':
|
2022-09-07 03:52:02 +03:00
|
|
|
return `<img src="${node.src}" alt="${node.alt}" title="${node.name}" class="emoji"/>`;
|
2022-05-05 23:52:10 +03:00
|
|
|
default:
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-09 08:33:43 +03:00
|
|
|
const getCharacterCount = node => {
|
|
|
|
if (Text.isText(node)) {
|
|
|
|
return node.text.length;
|
|
|
|
}
|
|
|
|
if (node.type === 'image') {
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
let count = 0;
|
|
|
|
node.children.forEach(child => {
|
|
|
|
count += getCharacterCount(child);
|
|
|
|
});
|
|
|
|
|
|
|
|
return count;
|
|
|
|
};
|
2022-09-07 10:00:28 +03:00
|
|
|
|
2022-10-09 08:33:43 +03:00
|
|
|
export type ChatTextFieldProps = {
|
|
|
|
defaultText?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const characterLimit = 300;
|
|
|
|
|
|
|
|
export const ChatTextField: FC<ChatTextFieldProps> = ({ defaultText }) => {
|
2022-05-04 00:55:13 +03:00
|
|
|
const [showEmojis, setShowEmojis] = useState(false);
|
2022-10-09 08:33:43 +03:00
|
|
|
const [characterCount, setCharacterCount] = useState(defaultText?.length);
|
2022-05-05 02:55:54 +03:00
|
|
|
const websocketService = useRecoilValue<WebsocketService>(websocketServiceAtom);
|
2022-09-07 03:52:02 +03:00
|
|
|
const editor = useMemo(() => withReact(withImages(createEditor())), []);
|
|
|
|
|
|
|
|
const defaultEditorValue: Descendant[] = [
|
|
|
|
{
|
|
|
|
type: 'paragraph',
|
2022-10-09 08:33:43 +03:00
|
|
|
children: [{ text: defaultText || '' }],
|
2022-09-07 03:52:02 +03:00
|
|
|
},
|
|
|
|
];
|
2022-05-05 02:55:54 +03:00
|
|
|
|
2022-05-05 09:06:35 +03:00
|
|
|
const sendMessage = () => {
|
|
|
|
if (!websocketService) {
|
|
|
|
console.log('websocketService is not defined');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-05 23:52:10 +03:00
|
|
|
const message = serialize(editor);
|
2022-05-05 09:06:35 +03:00
|
|
|
websocketService.send({ type: MessageType.CHAT, body: message });
|
|
|
|
|
2022-05-05 23:52:10 +03:00
|
|
|
// Clear the editor.
|
2022-09-07 03:52:02 +03:00
|
|
|
Transforms.delete(editor, {
|
|
|
|
at: {
|
|
|
|
anchor: Editor.start(editor, []),
|
|
|
|
focus: Editor.end(editor, []),
|
|
|
|
},
|
|
|
|
});
|
2022-10-09 08:33:43 +03:00
|
|
|
setCharacterCount(0);
|
2022-05-05 09:06:35 +03:00
|
|
|
};
|
|
|
|
|
2022-09-07 03:52:02 +03:00
|
|
|
const createImageNode = (alt, src, name): ImageNode => ({
|
|
|
|
type: 'image',
|
|
|
|
alt,
|
|
|
|
src,
|
|
|
|
name,
|
|
|
|
children: [{ text: '' }],
|
|
|
|
});
|
|
|
|
|
|
|
|
const insertImage = (url, name) => {
|
|
|
|
if (!url) return;
|
|
|
|
|
|
|
|
const image = createImageNode(name, url, name);
|
|
|
|
|
2023-02-27 05:09:24 +03:00
|
|
|
Transforms.insertNodes(editor, image);
|
|
|
|
Editor.normalize(editor, { force: true });
|
2022-09-07 03:52:02 +03:00
|
|
|
};
|
2022-05-05 09:06:35 +03:00
|
|
|
|
2022-10-09 08:33:43 +03:00
|
|
|
// Native emoji
|
|
|
|
const onEmojiSelect = (emoji: string) => {
|
2022-05-27 04:59:16 +03:00
|
|
|
ReactEditor.focus(editor);
|
2022-10-09 08:33:43 +03:00
|
|
|
Transforms.insertText(editor, emoji);
|
2022-05-06 00:43:40 +03:00
|
|
|
};
|
|
|
|
|
2022-10-09 08:33:43 +03:00
|
|
|
const onCustomEmojiSelect = (name: string, emoji: string) => {
|
2022-09-07 03:52:02 +03:00
|
|
|
ReactEditor.focus(editor);
|
2022-10-09 08:33:43 +03:00
|
|
|
insertImage(emoji, name);
|
2022-09-07 03:52:02 +03:00
|
|
|
};
|
|
|
|
|
2022-07-08 23:20:22 +03:00
|
|
|
const onKeyDown = (e: React.KeyboardEvent) => {
|
2022-10-09 08:33:43 +03:00
|
|
|
const charCount = getCharacterCount(editor) + 1;
|
|
|
|
|
|
|
|
// Send the message when hitting enter.
|
2022-05-05 23:52:10 +03:00
|
|
|
if (e.key === 'Enter') {
|
|
|
|
e.preventDefault();
|
|
|
|
sendMessage();
|
2022-10-09 08:33:43 +03:00
|
|
|
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();
|
2022-05-05 09:06:35 +03:00
|
|
|
}
|
2022-05-05 02:55:54 +03:00
|
|
|
};
|
2022-05-04 00:55:13 +03:00
|
|
|
|
2022-09-07 03:52:02 +03:00
|
|
|
const renderElement = p => {
|
|
|
|
switch (p.element.type) {
|
|
|
|
case 'image':
|
|
|
|
return <Image {...p} />;
|
|
|
|
default:
|
|
|
|
return <p {...p} />;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-04 00:55:13 +03:00
|
|
|
return (
|
2022-09-30 14:17:22 +03:00
|
|
|
<div className={styles.root}>
|
2022-10-09 08:33:43 +03:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
styles.inputWrap,
|
|
|
|
characterCount >= characterLimit && styles.maxCharacters,
|
|
|
|
)}
|
|
|
|
>
|
2022-10-01 11:34:59 +03:00
|
|
|
<Slate editor={editor} value={defaultEditorValue}>
|
|
|
|
<Editable
|
2022-11-05 06:04:13 +03:00
|
|
|
className="chat-text-input"
|
2022-10-01 11:34:59 +03:00
|
|
|
onKeyDown={onKeyDown}
|
2022-10-09 08:33:43 +03:00
|
|
|
onPaste={onPaste}
|
2022-10-01 11:34:59 +03:00
|
|
|
renderElement={renderElement}
|
2022-11-02 22:34:57 +03:00
|
|
|
placeholder="Send a message to chat"
|
2022-10-01 11:34:59 +03:00
|
|
|
style={{ width: '100%' }}
|
2022-12-20 08:48:57 +03:00
|
|
|
role="textbox"
|
|
|
|
aria-label="Chat text input"
|
2022-10-01 11:34:59 +03:00
|
|
|
autoFocus
|
|
|
|
/>
|
|
|
|
<Popover
|
|
|
|
content={
|
|
|
|
<EmojiPicker
|
|
|
|
onEmojiSelect={onEmojiSelect}
|
|
|
|
onCustomEmojiSelect={onCustomEmojiSelect}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
trigger="click"
|
2022-12-25 02:08:46 +03:00
|
|
|
placement="topRight"
|
2022-10-11 03:11:29 +03:00
|
|
|
onOpenChange={open => setShowEmojis(open)}
|
|
|
|
open={showEmojis}
|
2022-10-01 11:34:59 +03:00
|
|
|
/>
|
|
|
|
</Slate>
|
|
|
|
|
|
|
|
<div style={{ display: 'flex', paddingLeft: '5px' }}>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className={styles.emojiButton}
|
|
|
|
title="Emoji picker button"
|
|
|
|
onClick={() => setShowEmojis(!showEmojis)}
|
|
|
|
>
|
|
|
|
<SmileOutlined />
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
2022-10-05 05:04:23 +03:00
|
|
|
className={styles.sendButton}
|
2022-10-01 11:34:59 +03:00
|
|
|
title="Send message Button"
|
|
|
|
onClick={sendMessage}
|
|
|
|
>
|
|
|
|
<SendOutlined />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-05-04 00:55:13 +03:00
|
|
|
</div>
|
|
|
|
);
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|