Skip to content

Commit 881efad

Browse files
committed
chore: 将重复逻辑封装为函数
1 parent 482beff commit 881efad

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

packages/devui-vue/devui/mention/src/mention.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ export default defineComponent({
3636
}
3737
};
3838

39+
const updateTextContentWithSuggestion = (suggestion: string | number) => {
40+
const wordsWithoutSpace = textContext.value.split(' ');
41+
const lastWordIndex = wordsWithoutSpace.length - 1;
42+
const lastWord = wordsWithoutSpace[lastWordIndex];
43+
wordsWithoutSpace[lastWordIndex] = `${lastWord[0]}${suggestion}`;
44+
textContext.value = wordsWithoutSpace.join(' ');
45+
};
46+
3947
const handleUpdate = debounce((val: string) => {
4048
const wordsWithoutSpace = val.split(' ');
4149
const lastWordIndex = wordsWithoutSpace.length - 1;
@@ -83,12 +91,8 @@ export default defineComponent({
8391
e.stopPropagation();
8492
e.preventDefault();
8593
showSuggestions.value = false;
86-
const wordsWithoutSpace = textContext.value.split(' ');
87-
const lastWordIndex = wordsWithoutSpace.length - 1;
88-
const lastWord = wordsWithoutSpace[lastWordIndex];
89-
const selectedWord = item[props.dmValueParse.value as keyof IMentionSuggestionItem];
90-
wordsWithoutSpace[lastWordIndex] = `${lastWord[0]}${selectedWord}`;
91-
textContext.value = wordsWithoutSpace.join(' ');
94+
const suggestion = item[props.dmValueParse.value as keyof IMentionSuggestionItem];
95+
updateTextContentWithSuggestion(suggestion);
9296
};
9397

9498
const arrowKeyDown = (e: KeyboardEvent) => {
@@ -123,12 +127,8 @@ export default defineComponent({
123127
e.stopPropagation();
124128
e.preventDefault();
125129
showSuggestions.value = false;
126-
const wordsWithoutSpace = textContext.value.split(' ');
127-
const lastWordIndex = wordsWithoutSpace.length - 1;
128-
const lastWord = wordsWithoutSpace[lastWordIndex];
129-
const selectedWord = filteredSuggestions.value[currentIndex.value][props.dmValueParse.value as keyof IMentionSuggestionItem];
130-
wordsWithoutSpace[lastWordIndex] = `${lastWord[0]}${selectedWord}`;
131-
textContext.value = wordsWithoutSpace.join(' ');
130+
const suggestion = filteredSuggestions.value[currentIndex.value][props.dmValueParse.value as keyof IMentionSuggestionItem];
131+
updateTextContentWithSuggestion(suggestion);
132132
emit('select', filteredSuggestions.value[currentIndex.value]);
133133
}
134134
}

0 commit comments

Comments
 (0)