From ddd4a98fd7ede704699861ba32e2dbb643e87319 Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 <28871516+SleeplessOne1917@users.noreply.github.com> Date: Thu, 11 Apr 2024 12:56:56 +0000 Subject: [PATCH] Block urls (#2409) * Add markup * Make textarea reactive * Fix bug mixing up urls and instances * Tweak url enforcement logic * Extract url list textarea to component * Add translations * Add pnpm lock to prettier ignore --- .prettierignore | 1 + lemmy-translations | 2 +- .../components/common/url-list-textarea.tsx | 77 +++++++++++++++++++ src/shared/components/home/site-form.tsx | 18 +++++ 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 src/shared/components/common/url-list-textarea.tsx diff --git a/.prettierignore b/.prettierignore index 1640ee12..83721165 100644 --- a/.prettierignore +++ b/.prettierignore @@ -4,3 +4,4 @@ src/assets/css/themes/*.css src/assets/css/code-themes/*.css stats.json dist +pnpm-lock.yaml \ No newline at end of file diff --git a/lemmy-translations b/lemmy-translations index 9a8a86ea..b0dab329 160000 --- a/lemmy-translations +++ b/lemmy-translations @@ -1 +1 @@ -Subproject commit 9a8a86ea2edbd0ebed98f649805e69431b692dab +Subproject commit b0dab329ce23cfaec4a3a034ea2fee210888922e diff --git a/src/shared/components/common/url-list-textarea.tsx b/src/shared/components/common/url-list-textarea.tsx new file mode 100644 index 00000000..7e6972f9 --- /dev/null +++ b/src/shared/components/common/url-list-textarea.tsx @@ -0,0 +1,77 @@ +import { linkEvent, Component } from "inferno"; +import { I18NextService } from "../../services/I18NextService"; + +interface UrlListTextareaProps { + urls: string[]; + onUpdate(urls: string[]): void; +} + +interface UrlListTextareaState { + text: string; +} + +function handleTextChange(i: UrlListTextarea, event: any) { + i.setState({ text: event.target.value }); +} + +function handleTextBlur(i: UrlListTextarea, event: any) { + const inputValue: string = event.currentTarget?.value ?? ""; + + const intermediateText = inputValue.replace(/\s+/g, "\n"); + const newUrls: string[] = []; + + for (const str of intermediateText.split("\n")) { + let url: string; + + try { + url = new URL(str).toString(); + } catch { + try { + url = new URL("https://" + str).toString(); + } catch { + continue; + } + } + + if (newUrls.every(u => u !== url)) { + newUrls.push(url); + } + } + + i.setState({ text: newUrls.join("\n") }); + i.props.onUpdate(newUrls); +} + +export default class UrlListTextarea extends Component< + UrlListTextareaProps, + UrlListTextareaState +> { + state: UrlListTextareaState = { + text: "", + }; + + render() { + return ( +
+ + +
+