2022-10-01 11:34:59 +03:00
|
|
|
import { Popover } from 'antd';
|
2023-06-21 03:05:24 +03:00
|
|
|
import React, { FC, useReducer, useRef, useState } from 'react';
|
2022-05-05 02:55:54 +03:00
|
|
|
import { useRecoilValue } from 'recoil';
|
2023-06-21 03:05:24 +03:00
|
|
|
import ContentEditable from 'react-contenteditable';
|
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,
|
|
|
|
});
|
|
|
|
|
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;
|
2022-09-07 03:52:02 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
function getCaretPosition(node) {
|
|
|
|
const selection = window.getSelection();
|
|
|
|
|
|
|
|
if (selection.rangeCount === 0) {
|
|
|
|
return 0;
|
2022-05-05 23:52:10 +03:00
|
|
|
}
|
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
const range = selection.getRangeAt(0);
|
|
|
|
const preCaretRange = range.cloneRange();
|
|
|
|
const tempElement = document.createElement('div');
|
2022-05-04 00:55:13 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
preCaretRange.selectNodeContents(node);
|
|
|
|
preCaretRange.setEnd(range.endContainer, range.endOffset);
|
|
|
|
tempElement.appendChild(preCaretRange.cloneContents());
|
2022-05-05 23:52:10 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
return tempElement.innerHTML.length;
|
|
|
|
}
|
2022-05-05 23:52:10 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
function setCaretPosition(editableDiv, position) {
|
|
|
|
try {
|
|
|
|
const range = document.createRange();
|
|
|
|
const sel = window.getSelection();
|
|
|
|
range.selectNode(editableDiv);
|
|
|
|
range.setStart(editableDiv.childNodes[0], position);
|
|
|
|
range.collapse(true);
|
|
|
|
|
|
|
|
sel.removeAllRanges();
|
|
|
|
sel.addRange(range);
|
|
|
|
} catch (e) {
|
|
|
|
console.debug(e);
|
|
|
|
}
|
|
|
|
}
|
2022-05-05 23:52:10 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
function convertToText(str = '') {
|
|
|
|
// Ensure string.
|
|
|
|
let value = String(str);
|
2022-05-05 23:52:10 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
// Convert encoding.
|
|
|
|
value = value.replace(/ /gi, ' ');
|
|
|
|
value = value.replace(/&/gi, '&');
|
2022-05-05 23:52:10 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
// Replace `<br>`.
|
|
|
|
value = value.replace(/<br>/gi, '\n');
|
2022-05-05 23:52:10 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
// Replace `<div>` (from Chrome).
|
|
|
|
value = value.replace(/<div>/gi, '\n');
|
2022-05-05 23:52:10 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
// Replace `<p>` (from IE).
|
|
|
|
value = value.replace(/<p>/gi, '\n');
|
2022-10-09 08:33:43 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
// Cleanup the emoji titles.
|
|
|
|
value = value.replace(/\u200C{2}/gi, '');
|
2022-10-09 08:33:43 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
// Trim each line.
|
|
|
|
value = value
|
|
|
|
.split('\n')
|
|
|
|
.map((line = '') => line.trim())
|
|
|
|
.join('\n');
|
2022-09-07 10:00:28 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
// No more than 2x newline, per "paragraph".
|
|
|
|
value = value.replace(/\n\n+/g, '\n\n');
|
2022-10-09 08:33:43 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
// Clean up spaces.
|
|
|
|
value = value.replace(/[ ]+/g, ' ');
|
|
|
|
value = value.trim();
|
|
|
|
|
|
|
|
// Expose string.
|
|
|
|
return value;
|
|
|
|
}
|
2022-10-09 08:33:43 +03:00
|
|
|
|
2023-05-23 04:56:44 +03:00
|
|
|
export const ChatTextField: FC<ChatTextFieldProps> = ({ defaultText, enabled, focusInput }) => {
|
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);
|
2023-06-21 03:05:24 +03:00
|
|
|
const text = useRef(defaultText || '');
|
|
|
|
const [savedCursorLocation, setSavedCursorLocation] = useState(0);
|
2022-09-07 03:52:02 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
// This is a bit of a hack to force the component to re-render when the text changes.
|
|
|
|
// By default when updating a ref the component doesn't re-render.
|
|
|
|
const [, forceUpdate] = useReducer(x => x + 1, 0);
|
|
|
|
|
|
|
|
const getCharacterCount = () => text.current.length;
|
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-06-21 03:05:24 +03:00
|
|
|
let message = text.current;
|
2023-05-07 02:00:43 +03:00
|
|
|
// Strip the opening and closing <p> tags.
|
|
|
|
message = message.replace(/^<p>|<\/p>$/g, '');
|
2022-05-05 09:06:35 +03:00
|
|
|
websocketService.send({ type: MessageType.CHAT, body: message });
|
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
// Clear the input.
|
|
|
|
text.current = '';
|
2022-10-09 08:33:43 +03:00
|
|
|
setCharacterCount(0);
|
2023-06-21 03:05:24 +03:00
|
|
|
forceUpdate();
|
|
|
|
};
|
|
|
|
|
|
|
|
const insertTextAtCursor = (textToInsert: string) => {
|
|
|
|
const output = [
|
|
|
|
text.current.slice(0, savedCursorLocation),
|
|
|
|
textToInsert,
|
|
|
|
text.current.slice(savedCursorLocation),
|
|
|
|
].join('');
|
|
|
|
text.current = output;
|
|
|
|
forceUpdate();
|
2022-05-05 09:06:35 +03:00
|
|
|
};
|
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
const convertOnPaste = (event: React.ClipboardEvent) => {
|
|
|
|
// Prevent paste.
|
|
|
|
event.preventDefault();
|
2022-09-07 03:52:02 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
// Set later.
|
|
|
|
let value = '';
|
2022-09-07 03:52:02 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
// Does method exist?
|
|
|
|
const hasEventClipboard = !!(
|
|
|
|
event.clipboardData &&
|
|
|
|
typeof event.clipboardData === 'object' &&
|
|
|
|
typeof event.clipboardData.getData === 'function'
|
|
|
|
);
|
2022-09-07 03:52:02 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
// Get clipboard data?
|
|
|
|
if (hasEventClipboard) {
|
|
|
|
value = event.clipboardData.getData('text/plain');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert into temp `<textarea>`, read back out.
|
|
|
|
const textarea = document.createElement('textarea');
|
|
|
|
textarea.innerHTML = value;
|
|
|
|
value = textarea.innerText;
|
|
|
|
|
|
|
|
// Clean up text.
|
|
|
|
value = convertToText(value);
|
|
|
|
|
|
|
|
// Insert text.
|
|
|
|
insertTextAtCursor(value);
|
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) => {
|
2023-06-21 03:05:24 +03:00
|
|
|
insertTextAtCursor(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-06-21 03:05:24 +03:00
|
|
|
const html = `<img src="${emoji}" alt="${name}" title=${name} class="emoji" />`;
|
|
|
|
insertTextAtCursor(html);
|
2022-09-07 03:52:02 +03:00
|
|
|
};
|
|
|
|
|
2022-07-08 23:20:22 +03:00
|
|
|
const onKeyDown = (e: React.KeyboardEvent) => {
|
2023-06-21 03:05:24 +03:00
|
|
|
const charCount = getCharacterCount() + 1;
|
2022-10-09 08:33:43 +03:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
// Always allow delete.
|
|
|
|
if (e.key === 'Delete') {
|
|
|
|
setCharacterCount(charCount - 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always allow ctrl + a.
|
|
|
|
if (e.key === 'a' && e.ctrlKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-09 08:33:43 +03:00
|
|
|
// Limit the number of characters.
|
|
|
|
if (charCount + 1 > characterLimit) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
setCharacterCount(charCount + 1);
|
|
|
|
};
|
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
const handleChange = evt => {
|
|
|
|
text.current = evt.target.value;
|
|
|
|
};
|
2022-10-09 08:33:43 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
const handleBlur = () => {
|
|
|
|
// Save the cursor location.
|
|
|
|
setSavedCursorLocation(
|
|
|
|
getCaretPosition(document.getElementById('chat-input-content-editable')),
|
|
|
|
);
|
2022-05-05 02:55:54 +03:00
|
|
|
};
|
2022-05-04 00:55:13 +03:00
|
|
|
|
2023-06-21 03:05:24 +03:00
|
|
|
const handleFocus = () => {
|
|
|
|
if (!savedCursorLocation) {
|
|
|
|
return;
|
2022-09-07 03:52:02 +03:00
|
|
|
}
|
2023-06-21 03:05:24 +03:00
|
|
|
|
|
|
|
// Restore the cursor location.
|
|
|
|
setCaretPosition(document.getElementById('chat-input-content-editable'), savedCursorLocation);
|
|
|
|
setSavedCursorLocation(0);
|
2022-09-07 03:52:02 +03:00
|
|
|
};
|
|
|
|
|
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,
|
|
|
|
characterCount >= characterLimit && styles.maxCharacters,
|
|
|
|
)}
|
|
|
|
>
|
2023-06-21 03:05:24 +03:00
|
|
|
<Popover
|
|
|
|
content={
|
|
|
|
<EmojiPicker onEmojiSelect={onEmojiSelect} onCustomEmojiSelect={onCustomEmojiSelect} />
|
|
|
|
}
|
|
|
|
trigger="click"
|
|
|
|
placement="topRight"
|
|
|
|
onOpenChange={open => setShowEmojis(open)}
|
|
|
|
open={showEmojis}
|
|
|
|
/>
|
|
|
|
<ContentEditable
|
|
|
|
id="chat-input-content-editable"
|
|
|
|
html={text.current}
|
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}
|
|
|
|
onPaste={convertOnPaste}
|
|
|
|
onChange={handleChange}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
onFocus={handleFocus}
|
|
|
|
autoFocus={focusInput}
|
|
|
|
style={{ width: '100%' }}
|
|
|
|
role="textbox"
|
|
|
|
aria-label="Chat text input"
|
|
|
|
/>
|
2023-03-02 03:19:02 +03:00
|
|
|
{enabled && (
|
|
|
|
<div style={{ display: 'flex', paddingLeft: '5px' }}>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className={styles.emojiButton}
|
|
|
|
title="Emoji picker button"
|
|
|
|
onClick={() => setShowEmojis(!showEmojis)}
|
|
|
|
>
|
|
|
|
<SmileOutlined />
|
|
|
|
</button>
|
|
|
|
<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
|
|
|
};
|