Handle pasting in URL so user can easily remove

This commit is contained in:
Saoud Rizwan
2024-09-17 18:06:07 -04:00
parent 3f63428580
commit bb3944e8f1

View File

@@ -275,6 +275,22 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
const handlePaste = useCallback( const handlePaste = useCallback(
async (e: React.ClipboardEvent) => { async (e: React.ClipboardEvent) => {
const items = e.clipboardData.items const items = e.clipboardData.items
const pastedText = e.clipboardData.getData("text")
// Check if the pasted content is a URL, add space after so user can easily delete if they don't want it
const urlRegex = /^\S+:\/\/\S+$/
if (urlRegex.test(pastedText.trim())) {
e.preventDefault()
const trimmedUrl = pastedText.trim()
const newValue =
inputValue.slice(0, cursorPosition) + trimmedUrl + " " + inputValue.slice(cursorPosition)
setInputValue(newValue)
const newCursorPosition = cursorPosition + trimmedUrl.length + 1
setCursorPosition(newCursorPosition)
setIntendedCursorPosition(newCursorPosition)
return
}
const acceptedTypes = ["png", "jpeg", "webp"] // supported by anthropic and openrouter (jpg is just a file extension but the image will be recognized as jpeg) const acceptedTypes = ["png", "jpeg", "webp"] // supported by anthropic and openrouter (jpg is just a file extension but the image will be recognized as jpeg)
const imageItems = Array.from(items).filter((item) => { const imageItems = Array.from(items).filter((item) => {
const [type, subtype] = item.type.split("/") const [type, subtype] = item.type.split("/")
@@ -312,7 +328,7 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
} }
} }
}, },
[shouldDisableImages, setSelectedImages] [shouldDisableImages, setSelectedImages, cursorPosition, setInputValue, inputValue]
) )
const handleThumbnailsHeightChange = useCallback((height: number) => { const handleThumbnailsHeightChange = useCallback((height: number) => {