Skip to content

Commit 83af213

Browse files
committed
add copy paste image
1 parent 93e70be commit 83af213

4 files changed

Lines changed: 69 additions & 1 deletion

File tree

dist/components/inputs/textInput/components/TextInput.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,6 +2440,28 @@ const TextInput = props => {
24402440
const handleFileUploadClick = () => {
24412441
if (fileUploadRef) fileUploadRef.click();
24422442
};
2443+
const handlePaste = e => {
2444+
const items = e.clipboardData?.items;
2445+
if (!items) return;
2446+
const imageFiles = [];
2447+
for (const item of items) {
2448+
if (item.kind === 'file' && item.type.startsWith('image/')) {
2449+
const file = item.getAsFile();
2450+
if (file) imageFiles.push(file);
2451+
}
2452+
}
2453+
if (imageFiles.length > 0 && (props.uploadsConfig?.isImageUploadAllowed || props.uploadsConfig?.isRAGFileUploadAllowed || props.isFullFileUpload)) {
2454+
e.preventDefault();
2455+
const dataTransfer = new DataTransfer();
2456+
imageFiles.forEach(file => dataTransfer.items.add(file));
2457+
const syntheticEvent = {
2458+
target: {
2459+
files: dataTransfer.files
2460+
}
2461+
};
2462+
props.handleFileChange(syntheticEvent);
2463+
}
2464+
};
24432465
const handleKeyDown = e => {
24442466
if (e.key === 'Enter' && !e.shiftKey) {
24452467
const isIMEComposition = e.isComposing || e.keyCode === 229;
@@ -2489,6 +2511,7 @@ const TextInput = props => {
24892511
return (() => {
24902512
const _el$ = _tmpl$3$d(),
24912513
_el$3 = _el$.firstChild;
2514+
_el$.addEventListener("paste", handlePaste);
24922515
_el$.$$keydown = handleKeyDown;
24932516
_el$.style.setProperty("margin", "auto");
24942517
insert(_el$, createComponent(Show, {

dist/web.umd.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,28 @@
24462446
const handleFileUploadClick = () => {
24472447
if (fileUploadRef) fileUploadRef.click();
24482448
};
2449+
const handlePaste = e => {
2450+
const items = e.clipboardData?.items;
2451+
if (!items) return;
2452+
const imageFiles = [];
2453+
for (const item of items) {
2454+
if (item.kind === 'file' && item.type.startsWith('image/')) {
2455+
const file = item.getAsFile();
2456+
if (file) imageFiles.push(file);
2457+
}
2458+
}
2459+
if (imageFiles.length > 0 && (props.uploadsConfig?.isImageUploadAllowed || props.uploadsConfig?.isRAGFileUploadAllowed || props.isFullFileUpload)) {
2460+
e.preventDefault();
2461+
const dataTransfer = new DataTransfer();
2462+
imageFiles.forEach(file => dataTransfer.items.add(file));
2463+
const syntheticEvent = {
2464+
target: {
2465+
files: dataTransfer.files
2466+
}
2467+
};
2468+
props.handleFileChange(syntheticEvent);
2469+
}
2470+
};
24492471
const handleKeyDown = e => {
24502472
if (e.key === 'Enter' && !e.shiftKey) {
24512473
const isIMEComposition = e.isComposing || e.keyCode === 229;
@@ -2495,6 +2517,7 @@
24952517
return (() => {
24962518
const _el$ = _tmpl$3$d(),
24972519
_el$3 = _el$.firstChild;
2520+
_el$.addEventListener("paste", handlePaste);
24982521
_el$.$$keydown = handleKeyDown;
24992522
_el$.style.setProperty("margin", "auto");
25002523
insert(_el$, createComponent(Show, {

src/components/inputs/textInput/components/TextInput.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,27 @@ export const TextInput = (props: TextInputProps) => {
9090
if (fileUploadRef) fileUploadRef.click();
9191
};
9292

93+
const handlePaste = (e: ClipboardEvent) => {
94+
const items = e.clipboardData?.items;
95+
if (!items) return;
96+
97+
const imageFiles: File[] = [];
98+
for (const item of items) {
99+
if (item.kind === 'file' && item.type.startsWith('image/')) {
100+
const file = item.getAsFile();
101+
if (file) imageFiles.push(file);
102+
}
103+
}
104+
105+
if (imageFiles.length > 0 && (props.uploadsConfig?.isImageUploadAllowed || props.uploadsConfig?.isRAGFileUploadAllowed || props.isFullFileUpload)) {
106+
e.preventDefault();
107+
const dataTransfer = new DataTransfer();
108+
imageFiles.forEach((file) => dataTransfer.items.add(file));
109+
const syntheticEvent = { target: { files: dataTransfer.files } } as FileEvent<HTMLInputElement>;
110+
props.handleFileChange(syntheticEvent);
111+
}
112+
};
113+
93114
const handleKeyDown = (e: KeyboardEvent) => {
94115
if (e.key === 'Enter' && !e.shiftKey) {
95116
const isIMEComposition = e.isComposing || e.keyCode === 229;
@@ -155,6 +176,7 @@ export const TextInput = (props: TextInputProps) => {
155176
color: props.textColor ?? defaultTextColor,
156177
}}
157178
onKeyDown={handleKeyDown}
179+
onPaste={handlePaste}
158180
>
159181
<Show when={warningMessage() !== ''}>
160182
<div class="w-full px-4 pt-4 pb-1 text-red-500 text-sm" data-testid="warning-message">

0 commit comments

Comments
 (0)