From d45fc838de5b74b65b8804b232838f7b92405e97 Mon Sep 17 00:00:00 2001 From: ChaoLiu Date: Mon, 18 Aug 2025 18:22:39 +0800 Subject: [PATCH] feat: improve tag insertion logic for better content flow - Add smart tag insertion based on content ending patterns - Handle tag-at-end scenarios with appropriate spacing - Improve user experience when adding recommended tags Signed-off-by: ChaoLiu --- web/src/components/MemoEditor/index.tsx | 42 ++++++++++++++++--------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/web/src/components/MemoEditor/index.tsx b/web/src/components/MemoEditor/index.tsx index 0dca58f3b..efb284f52 100644 --- a/web/src/components/MemoEditor/index.tsx +++ b/web/src/components/MemoEditor/index.tsx @@ -476,17 +476,24 @@ const MemoEditor = observer((props: Props) => { if (!editorRef.current) return; const content = editorRef.current.getContent(); - const lastChar = content.slice(-1); // Move cursor to end of content editorRef.current.setCursorPosition(content.length); - if (content !== "" && lastChar !== "\n") { - editorRef.current.insertText("\n"); - } + // Check if content ends with a tag + const tagAtEndRegex = /#[a-zA-Z0-9_\-\u4e00-\u9fa5]+\s*$/; - editorRef.current.insertText("\n"); - editorRef.current.insertText(`#${tag} `); + if (tagAtEndRegex.test(content)) { + // Content ends with a tag, just add a space and the new tag + editorRef.current.insertText(` #${tag} `); + } else { + // Content ends with regular text, add newline then tag + const lastChar = content.slice(-1); + if (content !== "" && lastChar !== "\n") { + editorRef.current.insertText("\n"); + } + editorRef.current.insertText(`#${tag} `); + } // Scroll to the bottom to show the newly added tag editorRef.current.scrollToCursor(); @@ -502,20 +509,27 @@ const MemoEditor = observer((props: Props) => { if (!editorRef.current || state.recommendedTags.length === 0) return; const content = editorRef.current.getContent(); - const lastChar = content.slice(-1); // Move cursor to end of content editorRef.current.setCursorPosition(content.length); - if (content !== "" && lastChar !== "\n") { - editorRef.current.insertText("\n"); - } - - editorRef.current.insertText("\n"); + // Check if content ends with a tag + const tagAtEndRegex = /#[a-zA-Z0-9_\-\u4e00-\u9fa5]+\s*$/; // Add all tags - const tagsText = state.recommendedTags.map((tagSuggestion) => `#${tagSuggestion.tag}`).join(" ") + " "; - editorRef.current.insertText(tagsText); + const tagsText = state.recommendedTags.map((tagSuggestion) => `#${tagSuggestion.tag}`).join(" "); + + if (tagAtEndRegex.test(content)) { + // Content ends with a tag, just add a space and the new tags + editorRef.current.insertText(` ${tagsText} `); + } else { + // Content ends with regular text, add newline then tags + const lastChar = content.slice(-1); + if (content !== "" && lastChar !== "\n") { + editorRef.current.insertText("\n"); + } + editorRef.current.insertText(`${tagsText} `); + } // Scroll to the bottom to show the newly added tags editorRef.current.scrollToCursor();