From 9aed80a4fd0fcfc757bb647b4f96573b4671a730 Mon Sep 17 00:00:00 2001 From: MotH <45972355+moth-boi@users.noreply.github.com> Date: Sun, 22 Oct 2023 03:15:25 +0200 Subject: [PATCH] feat: better tag suggestion (#2421) Better Tag Suggestion --- .../MemoEditor/Editor/TagSuggestions.tsx | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/web/src/components/MemoEditor/Editor/TagSuggestions.tsx b/web/src/components/MemoEditor/Editor/TagSuggestions.tsx index fa9de672..6383926b 100644 --- a/web/src/components/MemoEditor/Editor/TagSuggestions.tsx +++ b/web/src/components/MemoEditor/Editor/TagSuggestions.tsx @@ -33,9 +33,28 @@ const TagSuggestions = ({ editorRef, editorActions }: Props) => { const suggestionsRef = useRef([]); suggestionsRef.current = (() => { - const partial = getCurrentWord()[0].slice(1).toLowerCase(); - const matches = (str: string) => str.startsWith(partial) && partial.length < str.length; - return tagsRef.current.filter((tag) => matches(tag.toLowerCase())).slice(0, 5); + const input = getCurrentWord()[0].slice(1).toLowerCase(); + + const customMatches = (tag: string, input: string) => { + const tagLowerCase = tag.toLowerCase(); + const inputLowerCase = input.toLowerCase(); + let inputIndex = 0; + + for (let i = 0; i < tagLowerCase.length; i++) { + if (tagLowerCase[i] === inputLowerCase[inputIndex]) { + inputIndex++; + if (inputIndex === inputLowerCase.length) { + return true; + } + } + } + + return false; + }; + + const matchedTags = tagsRef.current.filter((tag) => customMatches(tag, input)); + + return matchedTags.slice(0, 5); })(); const isVisibleRef = useRef(false);