feat: better tag suggestion (#2421)

Better Tag Suggestion
This commit is contained in:
MotH 2023-10-22 03:15:25 +02:00 committed by GitHub
parent c31f306b5b
commit 9aed80a4fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,9 +33,28 @@ const TagSuggestions = ({ editorRef, editorActions }: Props) => {
const suggestionsRef = useRef<string[]>([]); const suggestionsRef = useRef<string[]>([]);
suggestionsRef.current = (() => { suggestionsRef.current = (() => {
const partial = getCurrentWord()[0].slice(1).toLowerCase(); const input = 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 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); const isVisibleRef = useRef(false);