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
+11 -5
View File
@@ -1,10 +1,16 @@
export function formatBytes(bytes: number, decimals = 2): string {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['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 unit = 1024;
const units = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
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 {
+24 -3
View File
@@ -5,6 +5,7 @@
import { getToken } from '$lib/utils/auth';
import { fly } from 'svelte/transition';
import { encryptAES } from '$lib/utils/encryption';
import { formatBytes } from '$lib/utils/misc';
import Dropdown from '$lib/components/Dropdown.svelte';
export let data: PageData;
@@ -118,16 +119,36 @@
bind:value={title}
maxlength={data.options?.maxTitleLength ?? 500}
/>
<div class="relative">
<textarea
placeholder="Your paste content here..."
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"
spellcheck="false"
autocomplete="off"
bind:value={content}
maxlength={data.options?.maxContentSize
? Math.floor(data.options.maxContentSize / 4)
: undefined}
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))
) {
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
options={syntaxOptions}
bind:value={selectedSyntax}
+4 -2
View File
@@ -13,7 +13,8 @@ body,
h1,
h2,
h3,
p {
p,
.sans-serif {
font-family: 'IBM Plex Sans', sans-serif;
}
@@ -21,7 +22,8 @@ pre,
code,
textarea,
option,
select {
select,
.monospace {
font-family: 'Fira Code', monospace;
}