@@ -20,30 +20,46 @@ export default defineComponent({
20
20
const showSuggestions = ref < boolean > ( false ) ;
21
21
const currentIndex = ref < number > ( 0 ) ;
22
22
const suggestionsTop = ref < number > ( ) ;
23
+ const suggestionsLeft = ref < number > ( ) ;
23
24
const suggestions = ref < IMentionSuggestionItem [ ] > ( [ ] ) ;
24
25
const filteredSuggestions = ref < IMentionSuggestionItem [ ] > ( [ ] ) ;
25
26
const suggestionsDom = ref < HTMLDivElement > ( ) ;
26
27
const loading = computed ( ( ) => props . loading ) ;
27
28
const instance = getCurrentInstance ( ) ;
28
29
30
+ const checkShouldShowSuggestions = ( word : string ) => {
31
+ if ( word && props . trigger . includes ( word [ 0 ] ) ) {
32
+ // 需要以空格作为分割,单词尾部的 trigger 符号不生效
33
+ return word . length === 1 || ! props . trigger . includes ( word [ word . length - 1 ] ) ;
34
+ } else {
35
+ return false ;
36
+ }
37
+ } ;
38
+
29
39
const handleUpdate = debounce ( ( val : string ) => {
30
- if ( props . trigger . includes ( val [ 0 ] ) ) {
40
+ const wordsWithoutSpace = val . split ( ' ' ) ;
41
+ const lastWordIndex = wordsWithoutSpace . length - 1 ;
42
+ const lastWord = wordsWithoutSpace [ lastWordIndex ] ;
43
+ const shouldBeActive = checkShouldShowSuggestions ( lastWord ) ;
44
+ if ( shouldBeActive ) {
31
45
showSuggestions . value = true ;
32
46
if ( props . position === 'top' ) {
33
47
setTimeout ( ( ) => {
34
- const height = window . getComputedStyle ( suggestionsDom . value as Element , null ) . height ;
48
+ const element = window . getComputedStyle ( suggestionsDom . value as Element , null ) ;
49
+ const { height, width } = element ;
35
50
suggestionsTop . value = - Number ( height . replace ( 'px' , '' ) ) ;
51
+ suggestionsLeft . value = Number ( width . replace ( 'px' , '' ) ) ;
36
52
} , 0 ) ;
37
53
}
38
54
filteredSuggestions . value = ( suggestions . value as IMentionSuggestionItem [ ] ) . filter ( ( item : IMentionSuggestionItem ) =>
39
55
String ( item [ props . dmValueParse . value as keyof IMentionSuggestionItem ] )
40
56
. toLocaleLowerCase ( )
41
- . includes ( val . slice ( 1 ) . toLocaleLowerCase ( ) )
57
+ . includes ( lastWord . slice ( 1 ) . toLocaleLowerCase ( ) )
42
58
) ;
43
59
} else {
44
60
showSuggestions . value = false ;
45
61
}
46
- emit ( 'change' , val . slice ( 1 ) ) ;
62
+ emit ( 'change' , lastWord . slice ( 1 ) ) ;
47
63
} , 300 ) ;
48
64
49
65
const handleBlur = ( e : Event ) => {
@@ -67,7 +83,12 @@ export default defineComponent({
67
83
e . stopPropagation ( ) ;
68
84
e . preventDefault ( ) ;
69
85
showSuggestions . value = false ;
70
- textContext . value = textContext . value . substring ( 0 , 1 ) + item [ props . dmValueParse . value as keyof IMentionSuggestionItem ] ;
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 ( ' ' ) ;
71
92
} ;
72
93
73
94
const arrowKeyDown = ( e : KeyboardEvent ) => {
@@ -102,9 +123,12 @@ export default defineComponent({
102
123
e . stopPropagation ( ) ;
103
124
e . preventDefault ( ) ;
104
125
showSuggestions . value = false ;
105
- textContext . value =
106
- textContext . value . substring ( 0 , 1 ) +
107
- filteredSuggestions . value [ currentIndex . value ] [ props . dmValueParse . value as keyof IMentionSuggestionItem ] ;
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 ( ' ' ) ;
108
132
emit ( 'select' , filteredSuggestions . value [ currentIndex . value ] ) ;
109
133
}
110
134
}
@@ -149,6 +173,7 @@ export default defineComponent({
149
173
style = { {
150
174
marginTop : props . position === 'top' ? '0px' : '-16px' ,
151
175
top : suggestionsTop . value ? suggestionsTop . value + 'px' : 'inherit' ,
176
+ left : suggestionsLeft . value ? suggestionsLeft . value + 'px' : 'inherit' ,
152
177
} } >
153
178
{ filteredSuggestions . value . length > 0 ? (
154
179
filteredSuggestions . value ?. map ( ( item , index ) => {
0 commit comments