open-webui/src/lib/components/common/Textarea.svelte

71 lines
1.7 KiB
Svelte
Raw Normal View History

2024-10-20 06:17:47 +00:00
<script lang="ts">
import { onMount, tick } from 'svelte';
export let value = '';
export let placeholder = '';
export let className =
'w-full rounded-lg px-3 py-2 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none resize-none h-full';
2024-12-23 08:06:59 +00:00
export let onKeydown: Function = () => {};
2024-10-20 06:17:47 +00:00
let textareaElement;
2024-11-27 19:25:54 +00:00
$: if (textareaElement) {
2024-11-28 03:38:57 +00:00
if (textareaElement.innerText !== value && value !== '') {
2024-12-01 08:15:43 +00:00
textareaElement.innerText = value ?? '';
2024-11-27 19:25:54 +00:00
}
}
2024-11-24 07:10:12 +00:00
// Adjust height on mount and after setting the element.
2024-10-20 06:17:47 +00:00
onMount(async () => {
await tick();
});
2024-11-27 19:25:54 +00:00
// Handle paste event to ensure only plaintext is pasted
function handlePaste(event: ClipboardEvent) {
event.preventDefault(); // Prevent the default paste action
const clipboardData = event.clipboardData?.getData('text/plain'); // Get plaintext from clipboard
2024-11-28 03:24:20 +00:00
2024-11-28 03:26:05 +00:00
// Insert plaintext into the textarea
document.execCommand('insertText', false, clipboardData);
2024-11-27 19:25:54 +00:00
}
2024-10-20 06:17:47 +00:00
</script>
2024-11-27 19:25:54 +00:00
<div
contenteditable="true"
2024-10-20 06:17:47 +00:00
bind:this={textareaElement}
2024-11-29 21:24:37 +00:00
class="{className} whitespace-pre-wrap relative {value
? !value.trim()
? 'placeholder'
: ''
: 'placeholder'}"
2024-11-27 19:25:54 +00:00
style="field-sizing: content; -moz-user-select: text !important;"
on:input={() => {
2024-11-28 03:24:20 +00:00
const text = textareaElement.innerText;
2024-11-28 03:38:57 +00:00
if (text === '\n') {
2024-11-28 03:26:05 +00:00
value = '';
return;
}
2024-11-28 03:38:57 +00:00
value = text;
2024-11-27 19:25:54 +00:00
}}
on:paste={handlePaste}
2024-12-23 08:06:59 +00:00
on:keydown={onKeydown}
2024-11-27 19:25:54 +00:00
data-placeholder={placeholder}
2024-10-20 06:17:47 +00:00
/>
2024-11-27 19:25:54 +00:00
<style>
.placeholder::before {
/* absolute */
2024-11-27 19:25:54 +00:00
position: absolute;
content: attr(data-placeholder);
color: #adb5bd;
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 1;
pointer-events: none;
touch-action: none;
}
</style>