mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
fix: improve formatBytes function and display byte size in textarea
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user