From f9b1096ded8b0b6c2c4a0062b249c21bda9ca606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=97=AD=E7=BA=A2=20=28karminski-=E7=89=99?= =?UTF-8?q?=E5=8C=BB=29?= Date: Sun, 10 Nov 2024 09:18:01 +0800 Subject: [PATCH] Modify the logic for appending the URL after uploading an image, placing the image URL after the cursor. (#2804) --- .../components/common/markdown-textarea.tsx | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/shared/components/common/markdown-textarea.tsx b/src/shared/components/common/markdown-textarea.tsx index 50f65a13..bd2e3f46 100644 --- a/src/shared/components/common/markdown-textarea.tsx +++ b/src/shared/components/common/markdown-textarea.tsx @@ -460,12 +460,29 @@ export class MarkdownTextArea extends Component< if (res.state === "success") { if (res.data.msg === "ok") { const imageMarkdown = `![](${res.data.url})`; - i.setState(({ content }) => ({ - content: content ? `${content}\n${imageMarkdown}` : imageMarkdown, - })); + const textarea: HTMLTextAreaElement = document.getElementById( + i.id, + ) as HTMLTextAreaElement; + const cursorPosition = textarea.selectionStart; + + i.setState(({ content }) => { + const currentContent = content || ""; + return { + content: + currentContent.slice(0, cursorPosition) + + imageMarkdown + + currentContent.slice(cursorPosition), + }; + }); + i.contentChange(); - const textarea: any = document.getElementById(i.id); - autosize.update(textarea); + // Update cursor position to after the inserted image link + setTimeout(() => { + textarea.selectionStart = cursorPosition + imageMarkdown.length; + textarea.selectionEnd = cursorPosition + imageMarkdown.length; + autosize.update(textarea); + }, 10); + pictrsDeleteToast(image.name, res.data.delete_url as string); } else if (res.data.msg === "too_large") { toast(I18NextService.i18n.t("upload_too_large"), "danger");