refac: textarea

This commit is contained in:
Timothy Jaeryang Baek 2024-11-27 11:25:54 -08:00
parent c5ef53a09f
commit dbd6ac8080

View File

@ -10,18 +10,49 @@
let textareaElement; let textareaElement;
$: if (textareaElement) {
if (textareaElement.innerText !== value) {
textareaElement.innerText = value;
}
}
// Adjust height on mount and after setting the element. // Adjust height on mount and after setting the element.
onMount(async () => { onMount(async () => {
await tick(); await tick();
}); });
// 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
}
</script> </script>
<textarea <div
contenteditable="true"
bind:this={textareaElement} bind:this={textareaElement}
bind:value class="{className} whitespace-pre-wrap relative {!value.trim() ? 'placeholder' : ''}"
{placeholder} style="field-sizing: content; -moz-user-select: text !important;"
class={className} on:input={() => {
style="field-sizing: content;" value = textareaElement.innerText;
{rows} console.log(value);
{required} }}
on:paste={handlePaste}
data-placeholder={placeholder}
/> />
<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>