2022-10-01 11:34:59 +03:00
|
|
|
import { Popover } from 'antd';
|
2023-07-09 23:42:03 +03:00
|
|
|
import React, { FC, useEffect, useState } from 'react';
|
2022-05-05 02:55:54 +03:00
|
|
|
import { useRecoilValue } from 'recoil';
|
2023-06-28 00:45:45 +03:00
|
|
|
import sanitizeHtml from 'sanitize-html';
|
2023-07-06 03:25:47 +03:00
|
|
|
import GraphemeSplitter from 'grapheme-splitter';
|
2023-06-28 00:45:45 +03:00
|
|
|
|
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';
|
2023-07-09 23:42:03 +03:00
|
|
|
import ContentEditable from './ContentEditable';
|
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,
|
|
|
|
});
|
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
export type ChatTextFieldProps = {
|
|
|
|
defaultText?: string;
|
|
|
|
enabled: boolean;
|
|
|
|
focusInput: boolean;
|
2022-09-07 03:52:02 +03:00
|
|
|
};
|
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
const characterLimit = 300;
|
2023-07-06 03:25:47 +03:00
|
|
|
const maxNodeDepth = 10;
|
|
|
|
const graphemeSplitter = new GraphemeSplitter();
|
|
|
|
|
|
|
|
const getNodeTextContent = (node, depth) => {
|
|
|
|
let text = '';
|
|
|
|
|
|
|
|
if (depth > maxNodeDepth) return text;
|
|
|
|
if (node === null) return text;
|
|
|
|
|
|
|
|
switch (node.nodeType) {
|
|
|
|
case Node.CDATA_SECTION_NODE: // unlikely
|
|
|
|
case Node.TEXT_NODE: {
|
|
|
|
text = node.nodeValue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Node.ELEMENT_NODE: {
|
|
|
|
switch (node.tagName.toLowerCase()) {
|
|
|
|
case 'img': {
|
|
|
|
text = node.getAttribute('alt') || '';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'br': {
|
|
|
|
text = '\n';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'strong':
|
|
|
|
case 'b': {
|
|
|
|
/* markdown representation of bold/strong */
|
|
|
|
text = '**';
|
|
|
|
for (let i = 0; i < node.childNodes.length; i += 1) {
|
|
|
|
text += getNodeTextContent(node.childNodes[i], depth + 1);
|
|
|
|
}
|
|
|
|
text += '**';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'em':
|
|
|
|
case 'i': {
|
|
|
|
/* markdown representation of italic/emphasis */
|
|
|
|
text = '*';
|
|
|
|
for (let i = 0; i < node.childNodes.length; i += 1) {
|
|
|
|
text += getNodeTextContent(node.childNodes[i], depth + 1);
|
|
|
|
}
|
|
|
|
text += '*';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'p': {
|
|
|
|
text = '\n';
|
|
|
|
for (let i = 0; i < node.childNodes.length; i += 1) {
|
|
|
|
text += getNodeTextContent(node.childNodes[i], depth + 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'a':
|
|
|
|
case 'span':
|
|
|
|
case 'div': {
|
|
|
|
for (let i = 0; i < node.childNodes.length; i += 1) {
|
|
|
|
text += getNodeTextContent(node.childNodes[i], depth + 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* nodes which should specifically not be parsed */
|
|
|
|
case 'script':
|
|
|
|
case 'style': {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
text = node.textContent;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getTextContent = node => {
|
|
|
|
const text = getNodeTextContent(node, 0)
|
|
|
|
.replace(/^\s+/, '') /* remove leading whitespace */
|
|
|
|
.replace(/\s+$/, '') /* remove trailing whitespace */
|
|
|
|
.replace(/\n([^\n])/g, ' \n$1'); /* single line break to markdown break */
|
|
|
|
return text;
|
|
|
|
};
|
2022-09-07 03:52:02 +03:00
|
|
|
|
2023-05-23 04:56:44 +03:00
|
|
|
export const ChatTextField: FC<ChatTextFieldProps> = ({ defaultText, enabled, focusInput }) => {
|
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);
|
2023-07-09 23:42:03 +03:00
|
|
|
const [contentEditable, setContentEditable] = useState(null);
|
2023-06-28 01:48:16 +03:00
|
|
|
const [customEmoji, setCustomEmoji] = useState([]);
|
2022-09-07 03:52:02 +03:00
|
|
|
|
2023-07-09 23:42:03 +03:00
|
|
|
const onRootRef = el => {
|
|
|
|
setContentEditable(el);
|
|
|
|
};
|
2023-06-21 03:05:24 +03:00
|
|
|
|
2023-07-06 03:25:47 +03:00
|
|
|
const getCharacterCount = () => {
|
2023-07-09 23:42:03 +03:00
|
|
|
const message = getTextContent(contentEditable);
|
2023-07-06 03:25:47 +03:00
|
|
|
return graphemeSplitter.countGraphemes(message);
|
|
|
|
};
|
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;
|
|
|
|
}
|
|
|
|
|
2023-07-09 23:42:03 +03:00
|
|
|
const message = getTextContent(contentEditable);
|
2023-07-06 03:25:47 +03:00
|
|
|
const count = graphemeSplitter.countGraphemes(message);
|
2023-06-30 22:22:01 +03:00
|
|
|
if (count === 0 || count > characterLimit) return;
|
|
|
|
|
2022-05-05 09:06:35 +03:00
|
|
|
websocketService.send({ type: MessageType.CHAT, body: message });
|
2023-07-09 23:42:03 +03:00
|
|
|
contentEditable.innerHTML = '';
|
2023-06-21 03:05:24 +03:00
|
|
|
};
|
|
|
|
|
2023-06-30 06:34:06 +03:00
|
|
|
const insertTextAtEnd = (textToInsert: string) => {
|
2023-07-09 23:42:03 +03:00
|
|
|
contentEditable.innerHTML += textToInsert;
|
2022-05-05 09:06:35 +03:00
|
|
|
};
|
|
|
|
|
2022-10-09 08:33:43 +03:00
|
|
|
// Native emoji
|
|
|
|
const onEmojiSelect = (emoji: string) => {
|
2023-06-30 06:34:06 +03:00
|
|
|
insertTextAtEnd(emoji);
|
2022-05-06 00:43:40 +03:00
|
|
|
};
|
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
// Custom emoji images
|
2022-10-09 08:33:43 +03:00
|
|
|
const onCustomEmojiSelect = (name: string, emoji: string) => {
|
2023-07-06 03:25:47 +03:00
|
|
|
const html = `<img src="${emoji}" alt=":${name}:" title=":${name}:" class="emoji" />`;
|
2023-06-30 06:34:06 +03:00
|
|
|
insertTextAtEnd(html);
|
2022-09-07 03:52:02 +03:00
|
|
|
};
|
|
|
|
|
2022-07-08 23:20:22 +03:00
|
|
|
const onKeyDown = (e: React.KeyboardEvent) => {
|
2023-06-30 22:22:01 +03:00
|
|
|
if (e.key === 'Enter' && !(e.shiftKey || e.metaKey || e.ctrlKey || e.altKey)) {
|
2023-06-28 00:45:45 +03:00
|
|
|
e.preventDefault();
|
|
|
|
sendMessage();
|
|
|
|
}
|
2022-10-09 08:33:43 +03:00
|
|
|
};
|
|
|
|
|
2023-07-09 23:42:03 +03:00
|
|
|
const onPaste = evt => {
|
|
|
|
evt.preventDefault();
|
|
|
|
|
|
|
|
const clip = evt.clipboardData;
|
|
|
|
const { types } = clip;
|
|
|
|
const contentTypes = ['text/html', 'text/plain'];
|
|
|
|
|
|
|
|
let content;
|
|
|
|
|
|
|
|
for (let i = 0; i < contentTypes.length; i += 1) {
|
|
|
|
const contentType = contentTypes[i];
|
|
|
|
|
|
|
|
if (types.includes(contentType)) {
|
|
|
|
content = clip.getData(contentType);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!content) return;
|
|
|
|
|
|
|
|
const sanitized = sanitizeHtml(content, {
|
2023-06-28 00:45:45 +03:00
|
|
|
allowedTags: ['b', 'i', 'em', 'strong', 'a', 'br', 'p', 'img'],
|
|
|
|
allowedAttributes: {
|
|
|
|
img: ['class', 'alt', 'title', 'src'],
|
|
|
|
},
|
|
|
|
allowedClasses: {
|
|
|
|
img: ['emoji'],
|
|
|
|
},
|
|
|
|
transformTags: {
|
|
|
|
h1: 'p',
|
|
|
|
h2: 'p',
|
|
|
|
h3: 'p',
|
|
|
|
},
|
|
|
|
});
|
2022-10-09 08:33:43 +03:00
|
|
|
|
2023-07-09 23:42:03 +03:00
|
|
|
// MDN lists this as deprecated, but it's the only way to save this paste
|
|
|
|
// into the browser's Undo buffer. Plus it handles all the selection
|
|
|
|
// deletion, caret positioning, etc automaticaly.
|
|
|
|
if (sanitized) document.execCommand('insertHTML', false, sanitized);
|
|
|
|
};
|
2023-06-21 03:05:24 +03:00
|
|
|
|
2023-07-09 23:42:03 +03:00
|
|
|
const handleChange = () => {
|
|
|
|
const count = getCharacterCount();
|
|
|
|
setCharacterCount(count);
|
|
|
|
|
|
|
|
if (count === 0 && contentEditable.children.length === 1) {
|
|
|
|
/* if we have a single <br> element added by the browser, remove. */
|
|
|
|
if (contentEditable.children[0].tagName.toLowerCase() === 'br') {
|
|
|
|
contentEditable.removeChild(contentEditable.children[0]);
|
|
|
|
}
|
|
|
|
}
|
2022-09-07 03:52:02 +03:00
|
|
|
};
|
|
|
|
|
2023-06-28 00:45:45 +03:00
|
|
|
// Focus the input when the component mounts.
|
|
|
|
useEffect(() => {
|
|
|
|
if (!focusInput) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
document.getElementById('chat-input-content-editable').focus();
|
|
|
|
}, []);
|
|
|
|
|
2023-06-28 01:48:16 +03:00
|
|
|
const getCustomEmoji = async () => {
|
|
|
|
try {
|
|
|
|
const response = await fetch(`/api/emoji`);
|
|
|
|
const emoji = await response.json();
|
|
|
|
setCustomEmoji(emoji);
|
|
|
|
|
|
|
|
emoji.forEach(e => {
|
|
|
|
const preImg = document.createElement('link');
|
|
|
|
preImg.href = e.url;
|
|
|
|
preImg.rel = 'preload';
|
|
|
|
preImg.as = 'image';
|
|
|
|
document.head.appendChild(preImg);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error('cannot fetch custom emoji', e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
getCustomEmoji();
|
|
|
|
}, []);
|
|
|
|
|
2022-05-04 00:55:13 +03:00
|
|
|
return (
|
2023-05-23 04:56:44 +03:00
|
|
|
<div id="chat-input" className={styles.root}>
|
2022-10-09 08:33:43 +03:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
styles.inputWrap,
|
2023-06-30 22:22:01 +03:00
|
|
|
characterCount > characterLimit && styles.maxCharacters,
|
2022-10-09 08:33:43 +03:00
|
|
|
)}
|
|
|
|
>
|
2023-06-21 03:05:24 +03:00
|
|
|
<ContentEditable
|
|
|
|
id="chat-input-content-editable"
|
2023-07-09 23:42:03 +03:00
|
|
|
html={defaultText || ''}
|
2023-06-22 19:48:26 +03:00
|
|
|
placeholder={enabled ? 'Send a message to chat' : 'Chat is disabled'}
|
2023-06-21 03:05:24 +03:00
|
|
|
disabled={!enabled}
|
|
|
|
onKeyDown={onKeyDown}
|
2023-07-09 23:42:03 +03:00
|
|
|
onContentChange={handleChange}
|
|
|
|
onPaste={onPaste}
|
|
|
|
onRootRef={onRootRef}
|
|
|
|
style={{ whiteSpace: 'pre-wrap', width: '100%' }}
|
2023-06-21 03:05:24 +03:00
|
|
|
role="textbox"
|
|
|
|
aria-label="Chat text input"
|
|
|
|
/>
|
2023-03-02 03:19:02 +03:00
|
|
|
{enabled && (
|
|
|
|
<div style={{ display: 'flex', paddingLeft: '5px' }}>
|
2023-06-30 06:34:06 +03:00
|
|
|
<Popover
|
|
|
|
content={
|
|
|
|
<EmojiPicker
|
|
|
|
customEmoji={customEmoji}
|
|
|
|
onEmojiSelect={onEmojiSelect}
|
|
|
|
onCustomEmojiSelect={onCustomEmojiSelect}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
trigger="click"
|
|
|
|
placement="topRight"
|
2023-03-02 03:19:02 +03:00
|
|
|
>
|
2023-06-30 06:34:06 +03:00
|
|
|
<button type="button" className={styles.emojiButton} title="Emoji picker button">
|
|
|
|
<SmileOutlined />
|
|
|
|
</button>
|
|
|
|
</Popover>
|
2023-03-02 03:19:02 +03:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className={styles.sendButton}
|
|
|
|
title="Send message Button"
|
|
|
|
onClick={sendMessage}
|
|
|
|
>
|
|
|
|
<SendOutlined />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-10-01 11:34:59 +03:00
|
|
|
</div>
|
2022-05-04 00:55:13 +03:00
|
|
|
</div>
|
|
|
|
);
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|