diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 84bdd6b20..673ba609c 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/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); + } } } } diff --git a/src/lib/components/common/RichTextInput.svelte b/src/lib/components/common/RichTextInput.svelte index d6c4d7e40..fd0de2f44 100644 --- a/src/lib/components/common/RichTextInput.svelte +++ b/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); diff --git a/src/lib/constants.ts b/src/lib/constants.ts index fc756fce3..0499564a9 100644 --- a/src/lib/constants.ts +++ b/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_).