2022-05-04 16:55:54 -07:00
|
|
|
import { SmileOutlined } from '@ant-design/icons';
|
|
|
|
import { Button, Input } from 'antd';
|
2022-05-04 23:06:35 -07:00
|
|
|
import React, { useRef, useState } from 'react';
|
2022-05-04 16:55:54 -07:00
|
|
|
import { useRecoilValue } from 'recoil';
|
|
|
|
import ContentEditable from 'react-contenteditable';
|
|
|
|
import WebsocketService from '../../../services/websocket-service';
|
|
|
|
import { websocketServiceAtom } from '../../stores/ClientConfigStore';
|
2022-05-04 23:06:35 -07:00
|
|
|
import { getCaretPosition, convertToText, convertOnPaste } from '../chat';
|
|
|
|
import { MessageType } from '../../../interfaces/socket-events';
|
2022-05-03 23:55:13 +02:00
|
|
|
import s from './ChatTextField.module.scss';
|
|
|
|
|
2022-05-04 16:55:54 -07:00
|
|
|
interface Props {
|
|
|
|
value?: string;
|
|
|
|
}
|
2022-05-03 23:55:13 +02:00
|
|
|
|
|
|
|
export default function ChatTextField(props: Props) {
|
2022-05-04 16:55:54 -07:00
|
|
|
const { value: originalValue } = props;
|
|
|
|
const [value, setValue] = useState(originalValue);
|
2022-05-03 23:55:13 +02:00
|
|
|
const [showEmojis, setShowEmojis] = useState(false);
|
2022-05-04 16:55:54 -07:00
|
|
|
const websocketService = useRecoilValue<WebsocketService>(websocketServiceAtom);
|
|
|
|
|
|
|
|
const text = useRef(value);
|
|
|
|
|
2022-05-03 23:55:13 +02:00
|
|
|
// large is 40px
|
2022-05-04 16:55:54 -07:00
|
|
|
const size = 'small';
|
2022-05-03 23:55:13 +02:00
|
|
|
|
2022-05-04 23:06:35 -07:00
|
|
|
const sendMessage = () => {
|
|
|
|
if (!websocketService) {
|
|
|
|
console.log('websocketService is not defined');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = convertToText(value);
|
|
|
|
websocketService.send({ type: MessageType.CHAT, body: message });
|
|
|
|
setValue('');
|
|
|
|
};
|
|
|
|
|
2022-05-04 16:55:54 -07:00
|
|
|
const handleChange = evt => {
|
|
|
|
text.current = evt.target.value;
|
2022-05-04 23:06:35 -07:00
|
|
|
setValue(evt.target.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleKeyDown = event => {
|
|
|
|
const key = event && event.key;
|
|
|
|
|
|
|
|
if (key === 'Enter') {
|
|
|
|
}
|
2022-05-04 16:55:54 -07:00
|
|
|
};
|
2022-05-03 23:55:13 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
2022-05-04 16:55:54 -07:00
|
|
|
<Input.Group compact style={{ display: 'flex', width: '100%', position: 'absolute' }}>
|
|
|
|
<ContentEditable
|
|
|
|
style={{ width: '60%', maxHeight: '50px', padding: '5px' }}
|
|
|
|
html={text.current}
|
|
|
|
onChange={handleChange}
|
2022-05-04 23:06:35 -07:00
|
|
|
onKeyDown={e => {
|
|
|
|
handleKeyDown(e);
|
|
|
|
}}
|
2022-05-03 23:55:13 +02:00
|
|
|
/>
|
2022-05-04 16:55:54 -07:00
|
|
|
<Button type="default" ghost title="Emoji" onClick={() => setShowEmojis(!showEmojis)}>
|
|
|
|
<SmileOutlined style={{ color: 'rgba(0,0,0,.45)' }} />
|
|
|
|
</Button>
|
2022-05-04 23:06:35 -07:00
|
|
|
<Button size={size} type="primary" onClick={sendMessage}>
|
2022-05-03 23:55:13 +02:00
|
|
|
Submit
|
|
|
|
</Button>
|
|
|
|
</Input.Group>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2022-05-04 16:55:54 -07:00
|
|
|
|
|
|
|
ChatTextField.defaultProps = {
|
|
|
|
value: '',
|
|
|
|
};
|