浏览代码

enh: large pasted text as file

Co-Authored-By: Taylor Wilsdon <6508528+taylorwilsdon@users.noreply.github.com>
Timothy Jaeryang Baek 5 月之前
父节点
当前提交
11e78eac91
共有 3 个文件被更改,包括 36 次插入1 次删除
  1. 25 1
      src/lib/components/chat/MessageInput.svelte
  2. 8 0
      src/lib/components/common/RichTextInput.svelte
  3. 3 0
      src/lib/constants.ts

+ 25 - 1
src/lib/components/chat/MessageInput.svelte

@@ -23,7 +23,7 @@
 	import { uploadFile } from '$lib/apis/files';
 	import { getTools } from '$lib/apis/tools';
 
-	import { WEBUI_BASE_URL, WEBUI_API_BASE_URL } from '$lib/constants';
+	import { WEBUI_BASE_URL, WEBUI_API_BASE_URL, PASTED_TEXT_CHARACTER_LIMIT } from '$lib/constants';
 
 	import Tooltip from '../common/Tooltip.svelte';
 	import InputMenu from './MessageInput/InputMenu.svelte';
@@ -772,6 +772,18 @@
 																};
 
 																reader.readAsDataURL(blob);
+															} else if (item.type === 'text/plain') {
+																e.preventDefault();
+																const text = await clipboardData.getData('text/plain');
+
+																if (text.length > PASTED_TEXT_CHARACTER_LIMIT) {
+																	const blob = new Blob([text], { type: 'text/plain' });
+																	const file = new File([blob], `Pasted_Text_${Date.now()}.txt`, {
+																		type: 'text/plain'
+																	});
+
+																	await uploadFileHandler(file);
+																}
 															}
 														}
 													}
@@ -948,6 +960,18 @@
 															};
 
 															reader.readAsDataURL(blob);
+														} else if (item.type === 'text/plain') {
+															e.preventDefault();
+															const text = await clipboardData.getData('text/plain');
+
+															if (text.length > PASTED_TEXT_CHARACTER_LIMIT) {
+																const blob = new Blob([text], { type: 'text/plain' });
+																const file = new File([blob], `Pasted_Text_${Date.now()}.txt`, {
+																	type: 'text/plain'
+																});
+
+																await uploadFileHandler(file);
+															}
 														}
 													}
 												}

+ 8 - 0
src/lib/components/common/RichTextInput.svelte

@@ -23,6 +23,7 @@
 	import { keymap } from 'prosemirror-keymap';
 	import { baseKeymap, chainCommands } from 'prosemirror-commands';
 	import { DOMParser, DOMSerializer, Schema, Fragment } from 'prosemirror-model';
+	import { PASTED_TEXT_CHARACTER_LIMIT } from '$lib/constants';
 
 	export let className = 'input-prose';
 	export let shiftEnter = false;
@@ -412,6 +413,13 @@
 						// Extract plain text from clipboard and paste it without formatting
 						const plainText = event.clipboardData.getData('text/plain');
 						if (plainText) {
+							if (plainText.length > PASTED_TEXT_CHARACTER_LIMIT) {
+								// Dispatch paste event to parent component
+								eventDispatch('paste', { event });
+								event.preventDefault();
+								return true;
+							}
+
 							const modifiedText = handleTabIndentation(plainText);
 							console.log(modifiedText);
 

+ 3 - 0
src/lib/constants.ts

@@ -95,6 +95,9 @@ export const SUPPORTED_FILE_EXTENSIONS = [
 	'msg'
 ];
 
+
+export const PASTED_TEXT_CHARACTER_LIMIT = 1000;
+
 // Source: https://kit.svelte.dev/docs/modules#$env-static-public
 // This feature, akin to $env/static/private, exclusively incorporates environment variables
 // that are prefixed with config.kit.env.publicPrefix (usually set to PUBLIC_).