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

59 lines
1.5 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 = '';
2024-10-21 02:04:30 +00:00
export let rows = 1;
export let required = false;
2024-10-20 06:17:47 +00:00
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';
let textareaElement;
2024-11-27 19:25:54 +00:00
$: if (textareaElement) {
if (textareaElement.innerText !== value) {
textareaElement.innerText = value;
}
}
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
document.execCommand('insertText', false, clipboardData); // Insert plaintext into contenteditable
}
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-27 19:25:54 +00:00
class="{className} whitespace-pre-wrap relative {!value.trim() ? 'placeholder' : ''}"
style="field-sizing: content; -moz-user-select: text !important;"
on:input={() => {
value = textareaElement.innerText;
console.log(value);
}}
on:paste={handlePaste}
data-placeholder={placeholder}
2024-10-20 06:17:47 +00:00
/>
2024-11-27 19:25:54 +00:00
<style>
.placeholder::before {
/* abolute */
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>