mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
529 lines
16 KiB
Svelte
529 lines
16 KiB
Svelte
<script lang="ts">
|
|
import type { PageData } from './$types';
|
|
import { syntaxes, expiresOptions } from '$lib/consts';
|
|
import { user } from '$lib/stores/user';
|
|
import { getToken } from '$lib/utils/auth';
|
|
import { onMount } from 'svelte';
|
|
import { fly, fade } from 'svelte/transition';
|
|
import { encryptAES, estimateEncryptedSize } from '$lib/utils/encryption';
|
|
import {
|
|
formatBytes,
|
|
formatNumber,
|
|
isBinaryData,
|
|
tooltip,
|
|
syntaxFromExtension
|
|
} from '$lib/utils/misc';
|
|
import { parseMarkdown } from '$lib/utils/markdown';
|
|
import Dropdown from '$lib/components/Dropdown.svelte';
|
|
import { SvelteURLSearchParams } from 'svelte/reactivity';
|
|
import { FileUp, X, Eye, EyeClosed, Dices } from '@lucide/svelte';
|
|
|
|
interface Props {
|
|
data: PageData;
|
|
}
|
|
|
|
let { data }: Props = $props();
|
|
|
|
let textArea = $state<HTMLTextAreaElement | undefined>(undefined);
|
|
let markdownConatiner = $state<HTMLDivElement | undefined>(undefined);
|
|
|
|
const getInitialSyntax = () => data.options?.syntaxes?.[0] ?? 'plaintext';
|
|
const getInitialVisibility = () => data.options?.visibilities?.[0]?.value ?? 0;
|
|
|
|
let error = $state('');
|
|
let title = $state('');
|
|
let content = $state('');
|
|
let selectedSyntax = $state(getInitialSyntax());
|
|
let expiresIn: number = $state(0);
|
|
let selectedVisibility = $state(getInitialVisibility());
|
|
let password = $state('');
|
|
let passwordVisible = $state(false);
|
|
let anonymousUpload = $state(false);
|
|
let isDragging = $state(false);
|
|
let dragError = $state('');
|
|
let previewMode = $state(false);
|
|
let renderedMarkdown = $state('');
|
|
|
|
let currentByteSize = $derived(
|
|
selectedVisibility === 2
|
|
? estimateEncryptedSize(content)
|
|
: new TextEncoder().encode(content).length
|
|
);
|
|
|
|
$effect(() => {
|
|
if (selectedSyntax !== 'markdown') {
|
|
previewMode = false;
|
|
}
|
|
});
|
|
|
|
const MAX_HEIGHT = () => Math.floor((window.innerHeight - 120) * 0.6);
|
|
|
|
let previewHeight = $state('auto');
|
|
async function togglePreview(toPreview: boolean) {
|
|
if (toPreview && !previewMode) {
|
|
previewHeight = textArea?.style.height || 'auto';
|
|
renderedMarkdown = await parseMarkdown(content, false);
|
|
}
|
|
previewMode = toPreview;
|
|
requestAnimationFrame(() => resize(previewMode));
|
|
}
|
|
|
|
function resize(markdown: boolean = false) {
|
|
const element = markdown ? markdownConatiner : textArea;
|
|
if (!element) return;
|
|
|
|
let h: number;
|
|
let max: number;
|
|
|
|
element.style.height = 'auto';
|
|
h = element.scrollHeight;
|
|
max = MAX_HEIGHT();
|
|
|
|
if (h > max) {
|
|
element.style.height = max + 'px';
|
|
element.style.overflowY = 'auto';
|
|
} else {
|
|
element.style.height = h + 'px';
|
|
element.style.overflowY = 'hidden';
|
|
}
|
|
}
|
|
|
|
onMount(() => {
|
|
resize();
|
|
const render = () => {
|
|
if (
|
|
window.turnstile &&
|
|
data.options?.requiresVerification &&
|
|
data.auth?.cf_turnstile_site_key
|
|
) {
|
|
window.turnstile.render('.cf-turnstile', {
|
|
sitekey: data.auth.cf_turnstile_site_key,
|
|
theme: 'dark',
|
|
size: 'flexible',
|
|
appearence: 'interaction-only'
|
|
});
|
|
}
|
|
};
|
|
|
|
if (window.turnstile) {
|
|
render();
|
|
} else {
|
|
window.addEventListener('turnstile:loaded', render, { once: true });
|
|
}
|
|
|
|
return () => {
|
|
window.removeEventListener('turnstile:loaded', render);
|
|
};
|
|
});
|
|
|
|
let syntaxOptions = $derived.by(
|
|
() =>
|
|
data.options?.syntaxes?.map((lang: string) => ({
|
|
value: lang,
|
|
label: syntaxes[lang]?.name ?? lang.charAt(0).toUpperCase() + lang.slice(1)
|
|
})) ?? []
|
|
);
|
|
|
|
let visibilityOptions = $derived.by(
|
|
() =>
|
|
data.options?.visibilities?.map((visibility: { value: number; displayName: string }) => ({
|
|
value: visibility.value,
|
|
label: visibility.displayName
|
|
})) ?? []
|
|
);
|
|
|
|
async function handleSubmit(event: Event) {
|
|
event.preventDefault();
|
|
const params = new SvelteURLSearchParams({
|
|
title: title.trim(),
|
|
syntax: selectedSyntax,
|
|
visibility: selectedVisibility.toString(),
|
|
expiresAt: expiresIn > 0 ? (Date.now() + Number(expiresIn)).toString() : '0'
|
|
});
|
|
|
|
if (
|
|
data.options?.requiresVerification &&
|
|
data.auth?.cf_turnstile_site_key &&
|
|
window.turnstile
|
|
) {
|
|
const verificationToken = window.turnstile.getResponse();
|
|
if (verificationToken) {
|
|
params.append('verificationToken', verificationToken);
|
|
}
|
|
}
|
|
|
|
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',
|
|
headers: { 'Content-Type': 'text/plain' },
|
|
body
|
|
};
|
|
|
|
if ($user && !anonymousUpload) {
|
|
const token = getToken();
|
|
if (token) {
|
|
options.headers = {
|
|
...options.headers,
|
|
Authorization: `Bearer ${token}`
|
|
};
|
|
}
|
|
}
|
|
const response = await fetch(url, options);
|
|
if (response.ok) {
|
|
const result = await response.json();
|
|
if (selectedVisibility === 2) {
|
|
const passwordBase64 = btoa(password);
|
|
window.location.href = `/${result.id}#${passwordBase64}`;
|
|
return;
|
|
}
|
|
window.location.href = `/${result.id}`;
|
|
} else {
|
|
if (window.turnstile) {
|
|
window.turnstile.reset();
|
|
}
|
|
if (response.status === 403) {
|
|
error = 'You are banned from creating new pastes.';
|
|
} else {
|
|
const resData = await response.json();
|
|
if (resData.errors) {
|
|
const messages = Object.values(resData.errors).flat();
|
|
error = messages.join('\n');
|
|
} else {
|
|
if (resData.message === 'Verification failed.') {
|
|
if (window.turnstile) {
|
|
window.turnstile.reset();
|
|
}
|
|
}
|
|
error = resData.message || 'Registration failed. Please try again.';
|
|
}
|
|
}
|
|
setTimeout(() => {
|
|
error = '';
|
|
}, 5000);
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<svelte:head>
|
|
<title>Create a Paste - Sharpbin</title>
|
|
<meta
|
|
name="description"
|
|
content="Create and share code snippets, and text instantly with Sharpbin. Supports syntax highlighting, encryption, expiry, and more."
|
|
/>
|
|
<meta name="robots" content="index, follow" />
|
|
<link rel="canonical" href={data.url.split('/').slice(0, 3).join('/') + '/'} />
|
|
<!-- normalized to prevent duplicate content issues -google search console -->
|
|
<meta property="og:title" content="Create a Paste - Sharpbin" />
|
|
<meta
|
|
property="og:description"
|
|
content="Create and share code snippets, and text instantly with Sharpbin. Supports syntax highlighting, encryption, expiry, and more."
|
|
/>
|
|
<meta property="og:type" content="website" />
|
|
<meta property="og:url" content={data.url} />
|
|
<meta property="og:site_name" content="Sharpbin" />
|
|
<meta name="twitter:card" content="summary" />
|
|
<meta name="twitter:title" content="Create a Paste - Sharpbin" />
|
|
<meta
|
|
name="twitter:description"
|
|
content="Create and share code snippets, and text instantly with Sharpbin."
|
|
/>
|
|
</svelte:head>
|
|
|
|
<main
|
|
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>
|
|
<form class="mt-4" onsubmit={handleSubmit}>
|
|
<input
|
|
type="text"
|
|
placeholder="Title (optional)"
|
|
class="mb-2 w-full rounded border border-neutral-700 bg-neutral-800 p-2"
|
|
spellcheck="false"
|
|
autocomplete="off"
|
|
bind:value={title}
|
|
maxlength={data.options?.maxTitleLength ?? 500}
|
|
/>
|
|
<div>
|
|
<div class="relative">
|
|
{#if previewMode}
|
|
<div
|
|
class="markdown min-h-20 w-full overflow-y-auto rounded rounded-b-none border border-neutral-700 bg-neutral-800 p-2 transition-colors duration-200"
|
|
style="height: {previewHeight}"
|
|
bind:this={markdownConatiner}
|
|
>
|
|
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
|
|
{@html renderedMarkdown}
|
|
</div>
|
|
{:else}
|
|
<textarea
|
|
placeholder="Your paste content here..."
|
|
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}
|
|
oninput={() => resize()}
|
|
onchange={() => resize()}
|
|
onfocus={() => resize()}
|
|
onbeforeinput={(e) => {
|
|
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.data?.length ?? Number.MAX_SAFE_INTEGER < content.length)
|
|
) {
|
|
e.preventDefault();
|
|
}
|
|
}}
|
|
ondragenter={(e) => {
|
|
e.preventDefault();
|
|
isDragging = true;
|
|
}}
|
|
ondragover={(e) => {
|
|
e.preventDefault();
|
|
isDragging = true;
|
|
}}
|
|
ondragleave={(e) => {
|
|
e.preventDefault();
|
|
isDragging = false;
|
|
}}
|
|
ondrop={(e) => {
|
|
requestAnimationFrame(() => resize());
|
|
e.preventDefault();
|
|
isDragging = false;
|
|
|
|
const file = e.dataTransfer?.files[0];
|
|
if (!file) return;
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = (e) => {
|
|
const arrayBuffer = e.target?.result as ArrayBuffer;
|
|
if (isBinaryData(new Uint8Array(arrayBuffer))) {
|
|
content = '';
|
|
dragError =
|
|
'The dropped file appears to be binary and cannot be pasted as text.';
|
|
setTimeout(() => {
|
|
dragError = '';
|
|
}, 5000);
|
|
return;
|
|
}
|
|
const syntax = syntaxFromExtension(file.name.split('.').pop() || '');
|
|
if (syntax && selectedSyntax !== syntax) {
|
|
selectedSyntax = syntax;
|
|
} else if (!syntax && selectedSyntax !== 'plaintext') {
|
|
selectedSyntax = 'plaintext';
|
|
}
|
|
title = file.name;
|
|
const decoder = new TextDecoder();
|
|
content = decoder.decode(arrayBuffer);
|
|
};
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
}}
|
|
></textarea>
|
|
{/if}
|
|
{#if isDragging || (dragError && dragError !== '')}
|
|
<div
|
|
class="pointer-events-none absolute inset-0 z-10 flex items-center justify-center rounded-md rounded-b-none border-2 border-dashed border-neutral-500 bg-neutral-800/70 text-center text-neutral-300"
|
|
transition:fade={{ duration: 150 }}
|
|
>
|
|
<div class="relative flex h-full w-full items-center justify-center">
|
|
{#if dragError && dragError !== ''}
|
|
<div
|
|
class="absolute flex flex-col items-center gap-4"
|
|
transition:fly={{ y: -20, duration: 200 }}
|
|
>
|
|
<X class="h-12 w-12 text-red-500" />
|
|
<p>{dragError}</p>
|
|
</div>
|
|
{:else}
|
|
<div
|
|
class="absolute flex flex-col items-center gap-4"
|
|
transition:fly={{ y: -20, duration: 200 }}
|
|
>
|
|
<FileUp class="h-12 w-12" />
|
|
</div>
|
|
{/if}
|
|
</div>
|
|
</div>
|
|
{/if}
|
|
</div>
|
|
<div class="mt-1 flex">
|
|
{#if selectedSyntax === 'markdown'}
|
|
<div
|
|
class="monospace mb-2 flex min-h-full w-36 items-center justify-between gap-1 rounded-bl border border-neutral-700 bg-neutral-800 p-1 text-sm text-neutral-400 md:h-8"
|
|
>
|
|
<button
|
|
type="button"
|
|
class="h-full w-1/2 rounded-l transition-colors {previewMode
|
|
? 'hover:bg-neutral-700'
|
|
: 'bg-neutral-700 text-white'}"
|
|
onclick={() => togglePreview(false)}
|
|
>
|
|
Code
|
|
</button>
|
|
<button
|
|
type="button"
|
|
class="h-full w-1/2 rounded-r transition-colors {!previewMode
|
|
? 'hover:bg-neutral-700'
|
|
: 'bg-neutral-700 text-white'}"
|
|
onclick={() => togglePreview(true)}
|
|
>
|
|
Preview
|
|
</button>
|
|
</div>
|
|
{/if}
|
|
<div
|
|
class="monospace mb-2 flex h-fit min-h-12 items-center justify-between {selectedSyntax ===
|
|
'markdown'
|
|
? 'ml-auto w-[calc(100%-150px)] rounded-br'
|
|
: 'w-full rounded-b'} border border-neutral-700 bg-neutral-800 p-1 pr-2 pl-2 text-sm text-neutral-400 md:h-8 md:min-h-8"
|
|
>
|
|
<div class="flex flex-col md:flex-row md:gap-2">
|
|
<span>
|
|
{content.split('\n').length} line{content.split('\n').length !== 1 ? 's' : ''}
|
|
</span>
|
|
<span class="hidden text-neutral-600 md:block">•</span>
|
|
<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"
|
|
class:text-red-500={currentByteSize > (data.options?.maxPasteSize ?? 0)}
|
|
>
|
|
{formatBytes(currentByteSize)}
|
|
</span>
|
|
<span class="hidden md:block">/</span>
|
|
{formatBytes(data.options?.maxPasteSize ?? 0)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="grid grid-cols-1 md:grid-cols-3 md:gap-2">
|
|
<Dropdown
|
|
options={syntaxOptions}
|
|
bind:value={selectedSyntax}
|
|
placeholder="Select syntax..."
|
|
searchable={true}
|
|
/>
|
|
<Dropdown
|
|
options={expiresOptions}
|
|
bind:value={expiresIn}
|
|
placeholder="Select expiration..."
|
|
/>
|
|
<Dropdown
|
|
options={visibilityOptions}
|
|
bind:value={selectedVisibility}
|
|
placeholder="Select visibility..."
|
|
/>
|
|
</div>
|
|
{#if selectedVisibility === 2}
|
|
<div class="mb-2 flex w-full">
|
|
<input
|
|
type={passwordVisible ? 'text' : 'password'}
|
|
placeholder="Password for private paste"
|
|
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 active:bg-neutral-600"
|
|
onclick={() => {
|
|
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', true]}
|
|
>
|
|
<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 active:bg-neutral-600"
|
|
onclick={() => (passwordVisible = !passwordVisible)}
|
|
>
|
|
{#if passwordVisible}
|
|
<EyeClosed class="h-5 w-5" />
|
|
{:else}
|
|
<Eye class="h-5 w-5" />
|
|
{/if}
|
|
</button>
|
|
</div>
|
|
{/if}
|
|
{#if $user}
|
|
<label
|
|
class="mb-2 flex w-full items-center gap-2 rounded border border-neutral-700 bg-neutral-800 p-2"
|
|
>
|
|
<input
|
|
type="checkbox"
|
|
bind:checked={anonymousUpload}
|
|
class="h-4 w-4 rounded border-neutral-600 bg-neutral-700 text-neutral-500"
|
|
/>
|
|
<span>Upload anonymously</span>
|
|
</label>
|
|
{/if}
|
|
{#if data.options?.requiresVerification && data.auth?.cf_turnstile_site_key}
|
|
<div class="cf-turnstile mb-1 w-full" data-sitekey={data.auth.cf_turnstile_site_key}></div>
|
|
{/if}
|
|
<button
|
|
type="submit"
|
|
disabled={$user?.isBanned}
|
|
class="mb-2 w-full cursor-pointer rounded bg-neutral-700 px-4 py-2 font-semibold text-white transition-colors duration-200 hover:bg-neutral-800 active:bg-neutral-900 disabled:cursor-not-allowed disabled:bg-neutral-950/50 disabled:text-neutral-400"
|
|
>Create Paste</button
|
|
>
|
|
{#if error}
|
|
<div
|
|
transition:fly={{ y: 40, duration: 300 }}
|
|
class="mb-2 rounded border border-red-900 bg-red-950 p-2 text-sm text-red-200"
|
|
>
|
|
{error}
|
|
</div>
|
|
{/if}
|
|
{#if $user?.isBanned}
|
|
<div
|
|
transition:fly={{ y: 40, duration: 300 }}
|
|
class="mb-2 rounded border border-red-900 bg-red-950 p-2 text-sm text-red-200"
|
|
>
|
|
You are banned from creating new pastes.
|
|
</div>
|
|
{/if}
|
|
</form>
|
|
</div>
|
|
</main>
|