@@ -27,30 +27,30 @@ export default defineComponent({
2727 const loading = computed ( ( ) => props . loading ) ;
2828 const instance = getCurrentInstance ( ) ;
2929
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 ;
30+ function getLastTriggerIndex ( val : string ) {
31+ let lastTriggerIndex = - 1 ;
32+ for ( const trigger of props . trigger ) {
33+ lastTriggerIndex = Math . max ( lastTriggerIndex , val . lastIndexOf ( trigger ) ) ;
3634 }
37- } ;
35+ return lastTriggerIndex ;
36+ }
3837
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- } ;
38+ function handleCompleteText ( val : string | number ) {
39+ const lastTriggerIndex = getLastTriggerIndex ( textContext . value ) ;
40+ textContext . value = textContext . value . substring ( 0 , lastTriggerIndex + 1 ) + val . toLocaleString ( ) + ' ' ;
41+ showSuggestions . value = false ;
42+ }
4643
4744 const handleUpdate = debounce ( ( val : string ) => {
48- const wordsWithoutSpace = val . split ( ' ' ) ;
49- const lastWordIndex = wordsWithoutSpace . length - 1 ;
50- const lastWord = wordsWithoutSpace [ lastWordIndex ] ;
51- const shouldBeActive = checkShouldShowSuggestions ( lastWord ) ;
52- if ( shouldBeActive ) {
45+ const lastChar = val . charAt ( val . length - 1 ) ;
46+ if ( props . trigger . includes ( lastChar ) ) {
5347 showSuggestions . value = true ;
48+ }
49+ if ( lastChar === ' ' ) {
50+ showSuggestions . value = false ;
51+ }
52+ if ( showSuggestions . value ) {
53+ const prefix = val . slice ( getLastTriggerIndex ( val ) + 1 , val . length - 1 ) ;
5454 if ( props . position === 'top' ) {
5555 setTimeout ( ( ) => {
5656 const element = window . getComputedStyle ( suggestionsDom . value as Element , null ) ;
@@ -59,15 +59,12 @@ export default defineComponent({
5959 suggestionsLeft . value = Number ( width . replace ( 'px' , '' ) ) ;
6060 } , 0 ) ;
6161 }
62- filteredSuggestions . value = ( suggestions . value as IMentionSuggestionItem [ ] ) . filter ( ( item : IMentionSuggestionItem ) =>
63- String ( item [ props . dmValueParse . value as keyof IMentionSuggestionItem ] )
64- . toLocaleLowerCase ( )
65- . includes ( lastWord . slice ( 1 ) . toLocaleLowerCase ( ) )
66- ) ;
67- } else {
68- showSuggestions . value = false ;
62+ filteredSuggestions . value = ( suggestions . value as IMentionSuggestionItem [ ] ) . filter ( ( item : IMentionSuggestionItem ) => {
63+ const string = String ( item [ props . dmValueParse . value as keyof IMentionSuggestionItem ] ) . toLocaleLowerCase ( ) ;
64+ return string . includes ( prefix ) ;
65+ } ) ;
6966 }
70- emit ( 'change' , lastWord . slice ( 1 ) ) ;
67+ emit ( 'change' , lastChar ) ;
7168 } , 300 ) ;
7269
7370 const handleBlur = ( e : Event ) => {
@@ -91,8 +88,7 @@ export default defineComponent({
9188 e . stopPropagation ( ) ;
9289 e . preventDefault ( ) ;
9390 showSuggestions . value = false ;
94- const suggestion = item [ props . dmValueParse . value as keyof IMentionSuggestionItem ] ;
95- updateTextContentWithSuggestion ( suggestion ) ;
91+ handleCompleteText ( item [ props . dmValueParse . value as keyof IMentionSuggestionItem ] ) ;
9692 } ;
9793
9894 const arrowKeyDown = ( e : KeyboardEvent ) => {
@@ -127,8 +123,7 @@ export default defineComponent({
127123 e . stopPropagation ( ) ;
128124 e . preventDefault ( ) ;
129125 showSuggestions . value = false ;
130- const suggestion = filteredSuggestions . value [ currentIndex . value ] [ props . dmValueParse . value as keyof IMentionSuggestionItem ] ;
131- updateTextContentWithSuggestion ( suggestion ) ;
126+ handleCompleteText ( filteredSuggestions . value [ currentIndex . value ] [ props . dmValueParse . value as keyof IMentionSuggestionItem ] ) ;
132127 emit ( 'select' , filteredSuggestions . value [ currentIndex . value ] ) ;
133128 }
134129 }
0 commit comments