From 3b3a9fc783f85c0f3f6c079e200179a277e384e0 Mon Sep 17 00:00:00 2001 From: Drew Lytle Date: Tue, 7 Feb 2023 14:51:17 -0500 Subject: [PATCH 1/7] Refactor onEnter handler to handle case of putting lyrics on a new line --- .../forms/song/LineFields/LineFields.tsx | 36 ++++++++++++------- .../forms/song/StanzaFields/StanzaFields.tsx | 10 +++--- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/app/components/forms/song/LineFields/LineFields.tsx b/app/components/forms/song/LineFields/LineFields.tsx index f3b2819..911b796 100644 --- a/app/components/forms/song/LineFields/LineFields.tsx +++ b/app/components/forms/song/LineFields/LineFields.tsx @@ -1,6 +1,6 @@ import type { ILine, IMarking } from "@/types/song"; import { Menu } from "@headlessui/react"; -import type { KeyboardEvent } from "react"; +import type { ClipboardEvent, KeyboardEvent } from "react"; import { useState } from "react"; import { usePopper } from "react-popper"; import { Input } from "~/components/Input"; @@ -12,7 +12,7 @@ import uniqid from "uniqid"; type Props = { line: ILine; - insertLine?: () => void; + insertLine?: (lyric?: string) => void; deleteLine?: () => void; }; @@ -44,28 +44,40 @@ export const LineFields: React.FC = ({ placement: "left", }); - const insertMark = (index: number) => { + function insertMark(index: number) { return () => { const length = markingsFieldArray.length; push({ indent: index, id: uniqid(), mark: "" } as IMarking); focus(`input[name="${namespace}markings[${length}].mark"]`); }; - }; + } - const removeMark = (index: number) => { + function removeMark(index: number) { return () => { remove(index); }; - }; + } - const newLineOnEnterKey = (e: KeyboardEvent) => { + function newLineOnEnterKey(e: KeyboardEvent) { if (e.key === "Enter") { e.preventDefault(); - if (insertLine) insertLine(); + const target = e.target as HTMLInputElement; + // Get text after the current cursor position + const afterCursorText = target.value.slice( + target.selectionStart ?? undefined + ); + // Remove the text after the cursor from current lyrics input + target.value = target.value.slice(0, target.selectionStart ?? undefined); + // Insert line with text after the cursor + if (insertLine) insertLine(afterCursorText); } - }; + } - const confirmAndDelete = () => { + function onPasteLyrics(e: ClipboardEvent) { + // Trigger handlePaste from StanzaContext + } + + function confirmAndDelete() { if ( confirm( "Are you sure you want to delete this line?\nYou can't undo this action." @@ -73,12 +85,11 @@ export const LineFields: React.FC = ({ ) { if (deleteLine) deleteLine(); } - }; + } return (
-
= ({ className="font-mono" defaultValue={line.lyrics} onKeyDown={newLineOnEnterKey} + onPaste={onPasteLyrics} /> { - return () => { - insert(index + 1, createMockStanza({}).lines[0]); + return (lyrics?: string) => { + const lineToInsert = createMockStanza({}).lines[0]; + lineToInsert.lyrics = lyrics || ""; + insert(index + 1, lineToInsert); focus(`input[name="${namespace}lines[${index + 1}].lyrics"]`); }; }; @@ -83,7 +85,7 @@ export const StanzaFields: React.FC<{
- + {({ active }) => (