mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: enhance paste creation with dynamic textarea resizing and improved password handling; generate random password button in paste creation; show password in paste creation;
This commit is contained in:
@@ -5,15 +5,22 @@
|
||||
import { getToken } from '$lib/utils/auth';
|
||||
import { onMount } from 'svelte';
|
||||
import { fly, fade } from 'svelte/transition';
|
||||
import { encryptAES } from '$lib/utils/encryption';
|
||||
import { formatBytes, formatNumber, isBinaryData } from '$lib/utils/misc';
|
||||
import { encryptAES, estimateEncryptedSize } from '$lib/utils/encryption';
|
||||
import {
|
||||
formatBytes,
|
||||
formatNumber,
|
||||
isBinaryData,
|
||||
tooltip,
|
||||
syntaxFromExtension
|
||||
} from '$lib/utils/misc';
|
||||
import Dropdown from '$lib/components/Dropdown.svelte';
|
||||
import { SvelteURLSearchParams } from 'svelte/reactivity';
|
||||
import { FileUp, X } from '@lucide/svelte';
|
||||
import { syntaxFromExtension } from '$lib/utils/misc';
|
||||
import { FileUp, X, Eye, EyeClosed, Dices } from '@lucide/svelte';
|
||||
|
||||
export let data: PageData;
|
||||
|
||||
let textArea: HTMLTextAreaElement;
|
||||
|
||||
let error = '';
|
||||
let title = '';
|
||||
let content = '';
|
||||
@@ -21,11 +28,37 @@
|
||||
let expiresIn: number = 0;
|
||||
let selectedVisibility = data.options?.visibilities?.[0]?.value;
|
||||
let password = '';
|
||||
let passwordVisible = false;
|
||||
let anonymousUpload = false;
|
||||
let isDragging = false;
|
||||
let dragError = '';
|
||||
|
||||
$: currentByteSize =
|
||||
selectedVisibility === 2
|
||||
? estimateEncryptedSize(content)
|
||||
: new TextEncoder().encode(content).length;
|
||||
|
||||
const MAX_HEIGHT = () => Math.floor((window.innerHeight - 120) * 0.6);
|
||||
|
||||
function resize() {
|
||||
if (!textArea) return;
|
||||
|
||||
textArea.style.height = 'auto';
|
||||
|
||||
const h = textArea.scrollHeight;
|
||||
const max = MAX_HEIGHT();
|
||||
|
||||
if (h > max) {
|
||||
textArea.style.height = max + 'px';
|
||||
textArea.style.overflowY = 'auto';
|
||||
} else {
|
||||
textArea.style.height = h + 'px';
|
||||
textArea.style.overflowY = 'hidden';
|
||||
}
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
resize();
|
||||
const render = () => {
|
||||
if (
|
||||
window.turnstile &&
|
||||
@@ -98,11 +131,30 @@
|
||||
}
|
||||
}
|
||||
|
||||
if (content.trim().length === 0) {
|
||||
error = 'Paste content cannot be empty.';
|
||||
setTimeout(() => {
|
||||
error = '';
|
||||
}, 5000);
|
||||
return;
|
||||
}
|
||||
|
||||
let body = content;
|
||||
if (selectedVisibility === 2) {
|
||||
body = await encryptAES(content, password);
|
||||
}
|
||||
|
||||
if (
|
||||
data.options?.maxPasteSize &&
|
||||
new TextEncoder().encode(body).length > data.options.maxPasteSize
|
||||
) {
|
||||
error = `Paste exceeds the maximum size limit of ${formatBytes(data.options.maxPasteSize)}. ${selectedVisibility === 2 ? 'Encryption adds some overhead.' : ''}`;
|
||||
setTimeout(() => {
|
||||
error = '';
|
||||
}, 5000);
|
||||
return;
|
||||
}
|
||||
|
||||
const url = `/api/paste/create?${params.toString()}`;
|
||||
const options: RequestInit = {
|
||||
method: 'POST',
|
||||
@@ -165,7 +217,7 @@
|
||||
</svelte:head>
|
||||
|
||||
<main
|
||||
class="flex min-h-[calc(100vh-60px)] w-full flex-col items-center justify-center pt-5 pb-5 text-white"
|
||||
class="flex min-h-[calc(100vh-120px)] w-full flex-col items-center justify-center pt-5 pb-5 text-white"
|
||||
>
|
||||
<div class="w-[95%] max-w-7xl rounded border-2 border-neutral-800 bg-neutral-900 p-6">
|
||||
<h1 class="mb-6 text-center text-4xl font-bold">Create a Paste</h1>
|
||||
@@ -183,17 +235,24 @@
|
||||
<div class="relative">
|
||||
<textarea
|
||||
placeholder="Your paste content here..."
|
||||
class="h-64 max-h-[50vh] min-h-20 w-full resize-y rounded rounded-b-none border border-neutral-700 bg-neutral-800 p-2 transition-colors duration-200"
|
||||
class="min-h-20 w-full resize-none overflow-hidden rounded rounded-b-none border border-neutral-700 bg-neutral-800 p-2 transition-colors duration-200"
|
||||
spellcheck="false"
|
||||
autocomplete="off"
|
||||
minlength="1"
|
||||
bind:this={textArea}
|
||||
bind:value={content}
|
||||
on:input={resize}
|
||||
on:change={resize}
|
||||
on:focus={resize}
|
||||
on:beforeinput={(e) => {
|
||||
if (
|
||||
data.options?.maxPasteSize &&
|
||||
(new TextEncoder().encode(content).length >= data.options.maxPasteSize ||
|
||||
(e.data &&
|
||||
new TextEncoder().encode(content + e.data).length > data.options.maxPasteSize))
|
||||
) {
|
||||
if (!data.options?.maxPasteSize) return;
|
||||
|
||||
const futureSize =
|
||||
selectedVisibility === 2
|
||||
? estimateEncryptedSize(content + (e.data ?? ''))
|
||||
: new TextEncoder().encode(content + (e.data ?? '')).length;
|
||||
|
||||
if (futureSize > data.options.maxPasteSize) {
|
||||
e.preventDefault();
|
||||
}
|
||||
}}
|
||||
@@ -210,6 +269,7 @@
|
||||
isDragging = false;
|
||||
}}
|
||||
on:drop={(e) => {
|
||||
requestAnimationFrame(resize);
|
||||
e.preventDefault();
|
||||
isDragging = false;
|
||||
|
||||
@@ -269,7 +329,7 @@
|
||||
{/if}
|
||||
</div>
|
||||
<div
|
||||
class="monospace mb-4 flex h-12 items-center justify-between rounded-b border border-neutral-700 bg-neutral-800 p-1 pr-2 pl-2 text-sm text-neutral-400 md:h-8"
|
||||
class="monospace mb-2 flex h-12 items-center justify-between rounded-b border border-neutral-700 bg-neutral-800 p-1 pr-2 pl-2 text-sm text-neutral-400 md:h-8"
|
||||
>
|
||||
<div class="flex flex-col md:flex-row md:gap-2">
|
||||
<span>
|
||||
@@ -279,8 +339,11 @@
|
||||
<span>{formatNumber(content.length)} char{content.length !== 1 ? 's' : ''}</span>
|
||||
</div>
|
||||
<div class="flex flex-col text-center md:flex-row md:gap-2">
|
||||
<span class="border-b border-neutral-600 md:border-0">
|
||||
{formatBytes(new TextEncoder().encode(content).length)}
|
||||
<span
|
||||
class="border-b border-neutral-600 md:border-0"
|
||||
class:text-red-500={currentByteSize > (data.options?.maxPasteSize ?? 0)}
|
||||
>
|
||||
{formatBytes(currentByteSize)}
|
||||
</span>
|
||||
<span class="hidden md:block">/</span>
|
||||
{formatBytes(data.options?.maxPasteSize ?? 0)}
|
||||
@@ -306,14 +369,41 @@
|
||||
/>
|
||||
</div>
|
||||
{#if selectedVisibility === 2}
|
||||
<div class="flex w-full">
|
||||
<input
|
||||
type="password"
|
||||
type={passwordVisible ? 'text' : 'password'}
|
||||
placeholder="Password for private paste"
|
||||
class="w-full rounded border border-neutral-700 bg-neutral-800 p-2"
|
||||
class="w-[calc(100%-100px)] rounded-l border border-neutral-700 bg-neutral-800 p-2"
|
||||
spellcheck="false"
|
||||
autocomplete="off"
|
||||
minlength="6"
|
||||
bind:value={password}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
class="mr-1 ml-1 flex w-12.5 items-center justify-center border border-neutral-700 bg-neutral-800 p-2 text-neutral-400 hover:bg-neutral-700"
|
||||
on:click={() => {
|
||||
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
const bytes = new Uint8Array(16);
|
||||
crypto.getRandomValues(bytes);
|
||||
password = Array.from(bytes, (b) => chars[b % chars.length]).join('');
|
||||
}}
|
||||
use:tooltip={'Generate Random Password'}
|
||||
>
|
||||
<Dices class="h-5 w-5" />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="flex w-12.5 items-center justify-center rounded-r border border-neutral-700 bg-neutral-800 p-2 text-neutral-400 hover:bg-neutral-700"
|
||||
on:click={() => (passwordVisible = !passwordVisible)}
|
||||
>
|
||||
{#if passwordVisible}
|
||||
<EyeClosed class="h-5 w-5" />
|
||||
{:else}
|
||||
<Eye class="h-5 w-5" />
|
||||
{/if}
|
||||
</button>
|
||||
</div>
|
||||
{/if}
|
||||
{#if $user}
|
||||
<label
|
||||
|
||||
Reference in New Issue
Block a user