Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent pasting newlines into description input #136

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions src/routes/new/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { browser } from '$app/environment';
import { page } from '$app/stores';
import LoaderIcon from '~icons/ri/loader-4-line';
import type { FormEventHandler } from 'svelte/elements';

export let data: PageData;

Expand All @@ -23,23 +24,18 @@

$form.contributor = browser ? window.localStorage.getItem('contributor') ?? '' : '';

const handleDescriptionKeyDown = (event: KeyboardEvent) => {
// Prevent newlines
if (event.key === 'Enter') {
event.preventDefault();
}
const handleDescriptionInput: FormEventHandler<HTMLTextAreaElement> = (e) => {
$form.description = e.currentTarget.value;
// Replace any newlines with spaces and trim
$form.description = $form.description.replace(/\r?\n|\r/g, ' ').trim();

// Prevent leading spaces
if ($form.description.length === 0 && event.key === ' ') {
event.preventDefault();
}
$form.description = $form.description;
};

const handleContributorKeyDown = (event: KeyboardEvent) => {
// Prevent leading spaces
if ($form.contributor.length === 0 && event.key === ' ') {
event.preventDefault();
}
const handleContributorInput: FormEventHandler<HTMLInputElement> = (e) => {
$form.contributor = e.currentTarget.value;
// Trim spaces
$form.contributor = $form.contributor.trim();
};

const handleSubmit = () => {
Expand Down Expand Up @@ -79,7 +75,7 @@
Description <span class="optional">optional</span>
</div>
<textarea
on:keydown={handleDescriptionKeyDown}
on:input={handleDescriptionInput}
bind:value={$form.description}
use:autosize
name="description"
Expand All @@ -100,7 +96,7 @@
Your first name <span class="optional">optional</span>
</div>
<input
on:keydown={handleContributorKeyDown}
on:input={handleContributorInput}
bind:value={$form.contributor}
name="contributor"
type="text"
Expand Down