Skip to content

Commit

Permalink
feat(web): 聊天框粘贴图片发送与bbcode解析
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Oct 7, 2020
1 parent 73feede commit de3782f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/web/components/chatBox/useInputMsgEditorMsgSend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import React from 'react';
import { MsgInputEditor } from '../editor/MsgInputEditor';
import { useConverseDetail } from '@redux/hooks/chat';
import { serializeToBBCode } from '../editor/utils/serialize/bbcode';
import * as pasteUtils from '@shared/utils/paste-utils';
import { showToasts } from '@shared/manager/ui';
import { insertImage } from '../editor/changes/insertImage';
import _isNil from 'lodash/isNil';
import _isString from 'lodash/isString';

export function useInputMsgEditorMsgSend(converseUUID: string) {
const editorRef = useRef<Editor>();
Expand All @@ -29,6 +34,28 @@ export function useInputMsgEditorMsgSend(converseUUID: string) {
[handleSendMsg]
);

const handlePaste = useCallback(
async (e: React.ClipboardEvent<HTMLDivElement>) => {
if (e.clipboardData && e.clipboardData.items) {
const image = pasteUtils.isPasteImage(e.clipboardData.items);
if (image) {
// 上传图片
e.preventDefault();
try {
const file = image.getAsFile();
const chatimgUrl = await pasteUtils.upload(file!);
if (!_isNil(editorRef.current) && _isString(chatimgUrl)) {
insertImage(editorRef.current, chatimgUrl);
}
} catch (err) {
showToasts(err, 'error');
}
}
}
},
[]
);

useEffect(() => {
if (message === '') {
// 清空输入框信息
Expand Down Expand Up @@ -56,6 +83,7 @@ export function useInputMsgEditorMsgSend(converseUUID: string) {
value={messageData}
onChange={handleMessageDataChange}
onKeyDown={handleKeyDown}
onPaste={handlePaste}
onEditor={(editor) => (editorRef.current = editor)}
/>
);
Expand Down
2 changes: 2 additions & 0 deletions src/web/components/editor/utils/serialize/bbcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export function serializeToBBCode(node: TRPGEditorNode): string {
'data.text',
''
)}[/at]`;
case 'image':
return `[img]${_get(node, 'url', '')}[/img]`;
default:
return children;
}
Expand Down

0 comments on commit de3782f

Please sign in to comment.