From 29e53c2224ae2d3694fae0a5f37cae5a96866ebb Mon Sep 17 00:00:00 2001 From: Bojan Mojsilovic Date: Fri, 29 Dec 2023 14:30:32 +0100 Subject: [PATCH] Modify editor behavior when quoting a note. --- src/components/NewNote/EditBox/EditBox.tsx | 28 +++++++++++++++++----- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/components/NewNote/EditBox/EditBox.tsx b/src/components/NewNote/EditBox/EditBox.tsx index da35a37..0c7b81b 100644 --- a/src/components/NewNote/EditBox/EditBox.tsx +++ b/src/components/NewNote/EditBox/EditBox.tsx @@ -443,12 +443,28 @@ const EditBox: Component<{ }) createEffect(() => { - if (account?.quotedNote && textArea) { - setMessage((msg) => `${msg}${account.quotedNote} `); - textArea.value = message(); - onExpandableTextareaInput(new InputEvent('input')) - account.actions.quoteNote(undefined); - } + const quote = account?.quotedNote; + if (!textArea || !quote) return; + + let position = textArea.selectionStart; + + const isEmptyMessage = message().length === 0; + + setMessage((msg) => { + if (isEmptyMessage) return `\r\n\r\n${quote} `; + + return msg.slice(0, position) + quote + ' ' + msg.slice(position, msg.length); + }); + + position = isEmptyMessage ? 0 : position + quote.length + 1; + + textArea.value = message(); + account.actions.quoteNote(undefined); + + onExpandableTextareaInput(new InputEvent('input')); + + textArea.focus(); + textArea.selectionEnd = position; }) const onEscape = (e: KeyboardEvent) => {