owncast/web/components/chat/ChatTextField/ChatTextField.tsx

190 lines
4.9 KiB
TypeScript
Raw Normal View History

import { SendOutlined, SmileOutlined } from '@ant-design/icons';
2022-05-06 00:43:40 +03:00
import { Button, Popover } from 'antd';
2022-05-12 09:31:31 +03:00
import React, { useState } from 'react';
import { useRecoilValue } from 'recoil';
2022-05-12 09:31:31 +03:00
import { Transforms, createEditor, BaseEditor, Text } from 'slate';
import { Slate, Editable, withReact, ReactEditor } from 'slate-react';
2022-05-06 00:43:40 +03:00
import EmojiPicker from './EmojiPicker';
import WebsocketService from '../../../services/websocket-service';
import { websocketServiceAtom } from '../../stores/ClientConfigStore';
import { MessageType } from '../../../interfaces/socket-events';
2022-05-17 17:36:07 +03:00
import s from './ChatTextField.module.scss';
2022-05-04 00:55:13 +03:00
2022-05-17 17:36:07 +03:00
type CustomElement = { type: 'paragraph' | 'span'; children: CustomText[] };
type CustomText = { text: string };
declare module 'slate' {
interface CustomTypes {
Editor: BaseEditor & ReactEditor;
Element: CustomElement;
Text: CustomText;
}
}
interface Props {
value?: string;
}
2022-05-04 00:55:13 +03:00
2022-05-12 09:31:31 +03:00
// eslint-disable-next-line react/prop-types
const Image = ({ element }) => (
<img
2022-05-12 09:31:31 +03:00
// eslint-disable-next-line no-undef
// eslint-disable-next-line react/prop-types
src={element.url}
alt="emoji"
style={{ display: 'inline', position: 'relative', width: '30px', bottom: '10px' }}
/>
);
2022-05-12 09:31:31 +03:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const insertImage = (editor, url) => {
2022-05-12 09:31:31 +03:00
// const text = { text: '' };
// const image: ImageElement = { type: 'image', url, children: [text] };
// Transforms.insertNodes(editor, image);
};
const withImages = editor => {
const { isVoid } = editor;
2022-05-12 09:31:31 +03:00
// eslint-disable-next-line no-param-reassign
editor.isVoid = element => (element.type === 'image' ? true : isVoid(element));
2022-05-12 09:31:31 +03:00
// eslint-disable-next-line no-param-reassign
editor.isInline = element => element.type === 'image';
return editor;
};
export type EmptyText = {
text: string;
};
2022-05-12 09:31:31 +03:00
// type ImageElement = {
// type: 'image';
// url: string;
// children: EmptyText[];
// };
2022-05-12 09:31:31 +03:00
const Element = (props: any) => {
const { attributes, children, element } = props;
switch (element.type) {
case 'image':
return <Image {...props} />;
default:
return <p {...attributes}>{children}</p>;
}
};
const serialize = node => {
if (Text.isText(node)) {
2022-05-12 09:31:31 +03:00
const string = node.text;
// if (node.bold) {
// string = `<strong>${string}</strong>`;
// }
return string;
}
const children = node.children.map(n => serialize(n)).join('');
switch (node.type) {
case 'paragraph':
return `<p>${children}</p>`;
case 'image':
return `<img src="${node.url}" alt="emoji" />`;
default:
return children;
}
};
2022-05-12 09:31:31 +03:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2022-05-04 00:55:13 +03:00
export default function ChatTextField(props: Props) {
2022-05-12 09:31:31 +03:00
// const { value: originalValue } = props;
2022-05-04 00:55:13 +03:00
const [showEmojis, setShowEmojis] = useState(false);
const websocketService = useRecoilValue<WebsocketService>(websocketServiceAtom);
const [editor] = useState(() => withImages(withReact(createEditor())));
const sendMessage = () => {
if (!websocketService) {
console.log('websocketService is not defined');
return;
}
const message = serialize(editor);
websocketService.send({ type: MessageType.CHAT, body: message });
// Clear the editor.
Transforms.select(editor, [0, editor.children.length - 1]);
Transforms.delete(editor);
};
2022-05-12 09:31:31 +03:00
const handleChange = () => {};
2022-07-08 23:20:22 +03:00
const handleEmojiSelect = (e: any) => {
2022-05-27 04:59:16 +03:00
ReactEditor.focus(editor);
if (e.url) {
2022-05-06 00:43:40 +03:00
// Custom emoji
2022-05-27 04:59:16 +03:00
const { url } = e;
insertImage(editor, url);
2022-05-06 00:43:40 +03:00
} else {
// Native emoji
2022-05-27 04:59:16 +03:00
const { emoji } = e;
Transforms.insertText(editor, emoji);
2022-05-06 00:43:40 +03:00
}
};
2022-07-08 23:20:22 +03:00
const onKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter') {
e.preventDefault();
sendMessage();
}
};
2022-05-04 00:55:13 +03:00
return (
<div>
<div className={s.root}>
2022-05-22 09:37:23 +03:00
<Slate
editor={editor}
value={[{ type: 'paragraph', children: [{ text: '' }] }]}
onChange={handleChange}
>
<Editable
onKeyDown={onKeyDown}
renderElement={p => <Element {...p} />}
placeholder="Chat message goes here..."
style={{ width: '100%' }}
2022-05-27 04:59:16 +03:00
autoFocus
2022-05-22 09:37:23 +03:00
/>
<Popover
content={<EmojiPicker onEmojiSelect={handleEmojiSelect} />}
trigger="click"
onVisibleChange={visible => setShowEmojis(visible)}
visible={showEmojis}
/>
2022-05-22 09:37:23 +03:00
</Slate>
<button
type="button"
2022-05-22 09:37:23 +03:00
className={s.emojiButton}
title="Emoji picker button"
2022-05-22 09:37:23 +03:00
onClick={() => setShowEmojis(!showEmojis)}
>
<SmileOutlined />
</button>
<Button
className={s.sendButton}
size="large"
type="ghost"
icon={<SendOutlined />}
onClick={sendMessage}
/>
2022-05-22 09:37:23 +03:00
</div>
2022-05-04 00:55:13 +03:00
</div>
);
}
ChatTextField.defaultProps = {
value: '',
};