@@ -49,6 +49,11 @@ export default defineComponent({
49
49
default: false ,
50
50
},
51
51
52
+ allowSpace: {
53
+ type: Boolean ,
54
+ default: false ,
55
+ },
56
+
52
57
mapInsert: {
53
58
type: Function as PropType <(item : MentionItem , key : string ) => string >,
54
59
default: null ,
@@ -76,7 +81,7 @@ export default defineComponent({
76
81
const currentKey = ref <string >(null )
77
82
let currentKeyIndex: number
78
83
const oldKey = ref <string >(null )
79
-
84
+ const isMentioning = ref < boolean >( false )
80
85
// Items
81
86
82
87
const searchText = ref <string >(null )
@@ -256,10 +261,15 @@ export default defineComponent({
256
261
if (index >= 0 ) {
257
262
const { key, keyIndex } = getLastKeyBeforeCaret (index )
258
263
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
259
267
if (! (keyIndex < 1 || / \s / .test (getValue ()[keyIndex - 1 ]))) {
260
268
return false
261
269
}
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 ) {
263
273
openMenu (key , keyIndex )
264
274
searchText .value = text
265
275
return true
@@ -280,6 +290,9 @@ export default defineComponent({
280
290
function getLastSearchText (caretIndex : number , keyIndex : number ) {
281
291
if (keyIndex !== - 1 ) {
282
292
const text = getValue ().substring (keyIndex + 1 , caretIndex )
293
+ if (props .allowSpace ) {
294
+ return text .trim ()
295
+ }
283
296
// If there is a space we close the menu
284
297
if (! / \s / .test (text )) {
285
298
return text
@@ -323,13 +336,15 @@ export default defineComponent({
323
336
updateCaretPosition ()
324
337
selectedIndex .value = 0
325
338
emit (' open' , currentKey .value )
339
+ isMentioning .value = true
326
340
}
327
341
}
328
342
329
343
function closeMenu () {
330
344
if (currentKey .value != null ) {
331
345
oldKey .value = currentKey .value
332
346
currentKey .value = null
347
+ isMentioning .value = false
333
348
emit (' close' , oldKey .value )
334
349
}
335
350
}
@@ -362,6 +377,7 @@ export default defineComponent({
362
377
el ,
363
378
currentKey ,
364
379
oldKey ,
380
+ isMentioning ,
365
381
caretPosition ,
366
382
displayedItems ,
367
383
selectedIndex ,
0 commit comments