Skip to content

Commit 314f63a

Browse files
authored
1 parent 8a860a0 commit 314f63a

File tree

1 file changed

+20
-11
lines changed
  • src/ui/MessageInput/hooks/usePaste

1 file changed

+20
-11
lines changed

src/ui/MessageInput/hooks/usePaste/index.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
import { useCallback } from 'react';
1+
import React, { useCallback } from 'react';
22
import DOMPurify from 'dompurify';
33

44
import { inserTemplateToDOM } from './insertTemplate';
55
import { sanitizeString } from '../../utils';
66
import { DynamicProps } from './types';
7-
import {
8-
createPasteNode,
9-
hasMention,
10-
domToMessageTemplate,
11-
getUsersFromWords,
12-
extractTextFromNodes,
13-
getLeafNodes,
14-
} from './utils';
7+
import { createPasteNode, domToMessageTemplate, extractTextFromNodes, getLeafNodes, getUsersFromWords, hasMention } from './utils';
158

169
// exported, should be backward compatible
1710
// conditions to test:
@@ -30,10 +23,10 @@ export function usePaste({
3023
}: DynamicProps): (e: React.ClipboardEvent<HTMLDivElement>) => void {
3124
return useCallback((e) => {
3225
e.preventDefault();
33-
const html = e?.clipboardData.getData('text/html');
26+
const html = e.clipboardData.getData('text/html');
3427
// simple text, continue as normal
3528
if (!html) {
36-
const text = e?.clipboardData.getData('text');
29+
const text = e.clipboardData.getData('text') || getURIListText(e);
3730
document.execCommand('insertHTML', false, sanitizeString(text));
3831
setIsInput(true);
3932
setHeight();
@@ -70,5 +63,21 @@ export function usePaste({
7063
}, [ref, setIsInput, setHeight, channel, setMentionedUsers]);
7164
}
7265

66+
// https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/Recommended_drag_types#dragging_links
67+
function getURIListText(e: React.ClipboardEvent<HTMLDivElement>) {
68+
const pasteData = e.clipboardData.getData('text/uri-list');
69+
if (pasteData.length === 0) return '';
70+
71+
return pasteData
72+
.split('\n')
73+
.reduce((accumulator, line) => {
74+
const txt = line.trim();
75+
if (txt !== '' && !txt.startsWith('#')) {
76+
accumulator += txt + '\n';
77+
}
78+
return accumulator;
79+
}, '');
80+
}
81+
7382
// to do -> In the future donot export default
7483
export default usePaste;

0 commit comments

Comments
 (0)