@@ -20,30 +20,54 @@ export default defineComponent({
2020 const showSuggestions = ref < boolean > ( false ) ;
2121 const currentIndex = ref < number > ( 0 ) ;
2222 const suggestionsTop = ref < number > ( ) ;
23+ const suggestionsLeft = ref < number > ( ) ;
2324 const suggestions = ref < IMentionSuggestionItem [ ] > ( [ ] ) ;
2425 const filteredSuggestions = ref < IMentionSuggestionItem [ ] > ( [ ] ) ;
2526 const suggestionsDom = ref < HTMLDivElement > ( ) ;
2627 const loading = computed ( ( ) => props . loading ) ;
2728 const instance = getCurrentInstance ( ) ;
2829
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+
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+
2947 const handleUpdate = debounce ( ( val : string ) => {
30- if ( props . trigger . includes ( val [ 0 ] ) ) {
48+ const wordsWithoutSpace = val . split ( ' ' ) ;
49+ const lastWordIndex = wordsWithoutSpace . length - 1 ;
50+ const lastWord = wordsWithoutSpace [ lastWordIndex ] ;
51+ const shouldBeActive = checkShouldShowSuggestions ( lastWord ) ;
52+ if ( shouldBeActive ) {
3153 showSuggestions . value = true ;
3254 if ( props . position === 'top' ) {
3355 setTimeout ( ( ) => {
34- const height = window . getComputedStyle ( suggestionsDom . value as Element , null ) . height ;
56+ const element = window . getComputedStyle ( suggestionsDom . value as Element , null ) ;
57+ const { height, width } = element ;
3558 suggestionsTop . value = - Number ( height . replace ( 'px' , '' ) ) ;
59+ suggestionsLeft . value = Number ( width . replace ( 'px' , '' ) ) ;
3660 } , 0 ) ;
3761 }
3862 filteredSuggestions . value = ( suggestions . value as IMentionSuggestionItem [ ] ) . filter ( ( item : IMentionSuggestionItem ) =>
3963 String ( item [ props . dmValueParse . value as keyof IMentionSuggestionItem ] )
4064 . toLocaleLowerCase ( )
41- . includes ( val . slice ( 1 ) . toLocaleLowerCase ( ) )
65+ . includes ( lastWord . slice ( 1 ) . toLocaleLowerCase ( ) )
4266 ) ;
4367 } else {
4468 showSuggestions . value = false ;
4569 }
46- emit ( 'change' , val . slice ( 1 ) ) ;
70+ emit ( 'change' , lastWord . slice ( 1 ) ) ;
4771 } , 300 ) ;
4872
4973 const handleBlur = ( e : Event ) => {
@@ -67,7 +91,8 @@ export default defineComponent({
6791 e . stopPropagation ( ) ;
6892 e . preventDefault ( ) ;
6993 showSuggestions . value = false ;
70- textContext . value = textContext . value . substring ( 0 , 1 ) + item [ props . dmValueParse . value as keyof IMentionSuggestionItem ] ;
94+ const suggestion = item [ props . dmValueParse . value as keyof IMentionSuggestionItem ] ;
95+ updateTextContentWithSuggestion ( suggestion ) ;
7196 } ;
7297
7398 const arrowKeyDown = ( e : KeyboardEvent ) => {
@@ -102,9 +127,8 @@ export default defineComponent({
102127 e . stopPropagation ( ) ;
103128 e . preventDefault ( ) ;
104129 showSuggestions . value = false ;
105- textContext . value =
106- textContext . value . substring ( 0 , 1 ) +
107- filteredSuggestions . value [ currentIndex . value ] [ props . dmValueParse . value as keyof IMentionSuggestionItem ] ;
130+ const suggestion = filteredSuggestions . value [ currentIndex . value ] [ props . dmValueParse . value as keyof IMentionSuggestionItem ] ;
131+ updateTextContentWithSuggestion ( suggestion ) ;
108132 emit ( 'select' , filteredSuggestions . value [ currentIndex . value ] ) ;
109133 }
110134 }
@@ -149,6 +173,7 @@ export default defineComponent({
149173 style = { {
150174 marginTop : props . position === 'top' ? '0px' : '-16px' ,
151175 top : suggestionsTop . value ? suggestionsTop . value + 'px' : 'inherit' ,
176+ left : suggestionsLeft . value ? suggestionsLeft . value + 'px' : 'inherit' ,
152177 } } >
153178 { filteredSuggestions . value . length > 0 ? (
154179 filteredSuggestions . value ?. map ( ( item , index ) => {
0 commit comments