From 11e78eac914c89cfbdbf2964aa1d0f8c90943a3a Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 18 Nov 2024 20:50:12 -0800 Subject: [PATCH] enh: large pasted text as file Co-Authored-By: Taylor Wilsdon <6508528+taylorwilsdon@users.noreply.github.com> --- src/lib/components/chat/MessageInput.svelte | 26 ++++++++++++++++++- .../components/common/RichTextInput.svelte | 8 ++++++ src/lib/constants.ts | 3 +++ 3 files changed, 36 insertions(+), 1 deletion(-) 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_).