Skip to content

Commit

Permalink
Split caption input into parts automatically (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethWussmann authored Nov 7, 2024
1 parent c76299d commit 51b6b2f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/components/caption/components/caption-input.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Input } from "@/components/ui/input";
import { useCaptionEditor } from "@/hooks/provider/caption-editor-provider";
import { settings } from "@/lib/settings";
import clsx from "clsx";
import { useAtom } from "jotai/react";
import { ArrowUp, Pencil, X } from "lucide-react";
import { KeyboardEvent, useEffect, useRef, useState } from "react";

Expand Down Expand Up @@ -28,6 +30,7 @@ export const EditBanner = ({ onCancel }: { onCancel?: VoidFunction }) => {
};

export const CaptionInput = () => {
const [separator] = useAtom(settings.caption.separator);
const [value, setValue] = useState("");
const {
isEditing,
Expand All @@ -40,10 +43,15 @@ export const CaptionInput = () => {
} = useCaptionEditor();
const inputFieldRef = useRef<HTMLInputElement>(null);

const sanitizeValue = (value: string) => value.trim().replace(".", "");
const sanitizeValue = (value: string) => value.trim();
const splitIntoParts = (value: string) =>
value.split(separator.trim())
.map((text) => text.trim())
.filter((text) => text !== "");

const onSubmit = () => {
const sanitizedValue = sanitizeValue(value);
const parts = splitIntoParts(sanitizedValue);
if (isEditing) {
if (sanitizedValue === "") {
cancelEditMode();
Expand All @@ -60,7 +68,7 @@ export const CaptionInput = () => {
if (sanitizedValue === "") {
return;
}
addPart(sanitizedValue);
parts.forEach((part) => addPart(part));
}
setValue("");
};
Expand Down

0 comments on commit 51b6b2f

Please sign in to comment.