@@ -49,6 +49,11 @@ export default defineComponent({
4949 default: false ,
5050 },
5151
52+ allowSpace: {
53+ type: Boolean ,
54+ default: false ,
55+ },
56+
5257 mapInsert: {
5358 type: Function as PropType <(item : MentionItem , key : string ) => string >,
5459 default: null ,
@@ -76,7 +81,7 @@ export default defineComponent({
7681 const currentKey = ref <string >(null )
7782 let currentKeyIndex: number
7883 const oldKey = ref <string >(null )
79-
84+ const isMentioning = ref < boolean >( false )
8085 // Items
8186
8287 const searchText = ref <string >(null )
@@ -256,10 +261,15 @@ export default defineComponent({
256261 if (index >= 0 ) {
257262 const { key, keyIndex } = getLastKeyBeforeCaret (index )
258263 const text = lastSearchText = getLastSearchText (index , keyIndex )
264+ // Makes sure that key is not first character in editable element and
265+ // that there is a space before the key. Returns false if these conditions are
266+ // not met
259267 if (! (keyIndex < 1 || / \s / .test (getValue ()[keyIndex - 1 ]))) {
260268 return false
261269 }
262- if (text != null ) {
270+ const keyIsBeforeCaret = getValue ()[index - 1 ] === key
271+ const shouldOpen = props .allowSpace ? isMentioning .value || keyIsBeforeCaret : true
272+ if (text != null && shouldOpen ) {
263273 openMenu (key , keyIndex )
264274 searchText .value = text
265275 return true
@@ -280,6 +290,9 @@ export default defineComponent({
280290 function getLastSearchText (caretIndex : number , keyIndex : number ) {
281291 if (keyIndex !== - 1 ) {
282292 const text = getValue ().substring (keyIndex + 1 , caretIndex )
293+ if (props .allowSpace ) {
294+ return text .trim ()
295+ }
283296 // If there is a space we close the menu
284297 if (! / \s / .test (text )) {
285298 return text
@@ -323,13 +336,15 @@ export default defineComponent({
323336 updateCaretPosition ()
324337 selectedIndex .value = 0
325338 emit (' open' , currentKey .value )
339+ isMentioning .value = true
326340 }
327341 }
328342
329343 function closeMenu () {
330344 if (currentKey .value != null ) {
331345 oldKey .value = currentKey .value
332346 currentKey .value = null
347+ isMentioning .value = false
333348 emit (' close' , oldKey .value )
334349 }
335350 }
@@ -362,6 +377,7 @@ export default defineComponent({
362377 el ,
363378 currentKey ,
364379 oldKey ,
380+ isMentioning ,
365381 caretPosition ,
366382 displayedItems ,
367383 selectedIndex ,
0 commit comments