fix: improve formatBytes function and display byte size in textarea

This commit is contained in:
2026-01-07 20:18:42 -05:00
parent 2773813c26
commit 2a9a4b9eef
3 changed files with 47 additions and 18 deletions
+12 -6
View File
@@ -1,10 +1,16 @@
export function formatBytes(bytes: number, decimals = 2): string { export function formatBytes(bytes: number, decimals = 2): string {
if (bytes === 0) return '0 Bytes'; if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals; const unit = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const units = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; const exponent = Math.floor(Math.log(bytes) / Math.log(unit));
const value = bytes / Math.pow(unit, exponent);
const unitLabel = value === 1 && exponent === 0 ? 'Byte' : units[exponent];
const formattedValue = Number.isInteger(value) ? value : value.toFixed(decimals);
return `${formattedValue} ${unitLabel}`;
} }
export function formatNumber(num: number): string { export function formatNumber(num: number): string {
+31 -10
View File
@@ -5,6 +5,7 @@
import { getToken } from '$lib/utils/auth'; import { getToken } from '$lib/utils/auth';
import { fly } from 'svelte/transition'; import { fly } from 'svelte/transition';
import { encryptAES } from '$lib/utils/encryption'; import { encryptAES } from '$lib/utils/encryption';
import { formatBytes } from '$lib/utils/misc';
import Dropdown from '$lib/components/Dropdown.svelte'; import Dropdown from '$lib/components/Dropdown.svelte';
export let data: PageData; export let data: PageData;
@@ -118,16 +119,36 @@
bind:value={title} bind:value={title}
maxlength={data.options?.maxTitleLength ?? 500} maxlength={data.options?.maxTitleLength ?? 500}
/> />
<textarea <div class="relative">
placeholder="Your paste content here..." <textarea
class="mb-4 h-64 max-h-[50vh] min-h-10 w-full resize-y rounded border border-neutral-700 bg-neutral-800 p-2" placeholder="Your paste content here..."
spellcheck="false" class="mb-4 h-64 max-h-[50vh] min-h-10 w-full resize-y rounded border border-neutral-700 bg-neutral-800 p-2"
autocomplete="off" spellcheck="false"
bind:value={content} autocomplete="off"
maxlength={data.options?.maxContentSize bind:value={content}
? Math.floor(data.options.maxContentSize / 4) on:beforeinput={(e) => {
: undefined} if (
></textarea> data.options?.maxPasteSize &&
(new TextEncoder().encode(content).length >= data.options.maxPasteSize ||
(e.data &&
new TextEncoder().encode(content + e.data).length > data.options.maxPasteSize))
) {
e.preventDefault();
}
}}
></textarea>
<div
class="monospace absolute right-4 bottom-6 rounded bg-neutral-900/50 p-1 text-sm text-neutral-400 backdrop-blur-sm"
>
{#if data.options?.maxPasteSize}
{formatBytes(new TextEncoder().encode(content).length)} / {formatBytes(
data.options.maxPasteSize
)}
{:else}
{formatBytes(new TextEncoder().encode(content).length)}
{/if}
</div>
</div>
<Dropdown <Dropdown
options={syntaxOptions} options={syntaxOptions}
bind:value={selectedSyntax} bind:value={selectedSyntax}
+4 -2
View File
@@ -13,7 +13,8 @@ body,
h1, h1,
h2, h2,
h3, h3,
p { p,
.sans-serif {
font-family: 'IBM Plex Sans', sans-serif; font-family: 'IBM Plex Sans', sans-serif;
} }
@@ -21,7 +22,8 @@ pre,
code, code,
textarea, textarea,
option, option,
select { select,
.monospace {
font-family: 'Fira Code', monospace; font-family: 'Fira Code', monospace;
} }