mirror of https://github.com/msgbyte/tailchat
feat: 增加代码允许输入表情
parent
a323911e1e
commit
628148fb6f
@ -0,0 +1,11 @@
|
||||
import { Emoji } from '@capital/component';
|
||||
import React from 'react';
|
||||
import type { TagProps } from '../bbcode/type';
|
||||
|
||||
export const EmojiTag: React.FC<TagProps> = React.memo((props) => {
|
||||
const { node } = props;
|
||||
const code = node.content.join('');
|
||||
|
||||
return <Emoji emoji={code} />;
|
||||
});
|
||||
EmojiTag.displayName = 'EmojiTag';
|
@ -0,0 +1,38 @@
|
||||
import { Icon } from '@/components/Icon';
|
||||
import { Dropdown } from 'antd';
|
||||
import React, { useCallback, useState } from 'react';
|
||||
import { useChatInputActionContext } from './context';
|
||||
import { EmojiPanel } from '@/components/Emoji';
|
||||
|
||||
export const ChatInputEmotion: React.FC = React.memo(() => {
|
||||
const actionContext = useChatInputActionContext();
|
||||
const { appendMsg } = actionContext;
|
||||
|
||||
const [visible, setVisible] = useState(false);
|
||||
|
||||
const handleSelect = useCallback(
|
||||
async (code: string) => {
|
||||
appendMsg(code);
|
||||
setVisible(false);
|
||||
},
|
||||
[appendMsg]
|
||||
);
|
||||
|
||||
const menu = <EmojiPanel onSelect={handleSelect} />;
|
||||
|
||||
return (
|
||||
<Dropdown
|
||||
visible={visible}
|
||||
onVisibleChange={setVisible}
|
||||
overlay={menu}
|
||||
placement="topRight"
|
||||
trigger={['click']}
|
||||
>
|
||||
<Icon
|
||||
className="text-2xl cursor-pointer"
|
||||
icon="mdi:emoticon-happy-outline"
|
||||
/>
|
||||
</Dropdown>
|
||||
);
|
||||
});
|
||||
ChatInputEmotion.displayName = 'ChatInputEmotion';
|
@ -0,0 +1,12 @@
|
||||
import { regMessageTextDecorators } from '@/plugin/common';
|
||||
import { preprocessMessage } from '../preprocessMessage';
|
||||
|
||||
regMessageTextDecorators(() => ({
|
||||
emoji: (code) => `[emoji]${code.substring(1, code.length - 1)}[/emoji]`,
|
||||
}));
|
||||
|
||||
test('preprocessMessage', () => {
|
||||
expect(preprocessMessage('anystring :face: anystring :heart:')).toBe(
|
||||
'anystring [emoji]face[/emoji] anystring [emoji]heart[/emoji]'
|
||||
);
|
||||
});
|
@ -0,0 +1,15 @@
|
||||
import { getMessageTextDecorators } from '@/plugin/common';
|
||||
|
||||
const emojiNameRegex = /:([a-zA-Z0-9_\-\+]+):/g;
|
||||
|
||||
/**
|
||||
* 预加工待发送的消息
|
||||
*/
|
||||
export function preprocessMessage(message: string) {
|
||||
/**
|
||||
* 预加工emoji
|
||||
*/
|
||||
return message.replaceAll(emojiNameRegex, (code) =>
|
||||
getMessageTextDecorators().emoji(code)
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue