Skip to content

Commit dd8bc7f

Browse files
committed
feat: add allowSpace prop to allow space in search
feat: add allowSpace prop to allow space in search fix: lint errors v2.0.0-alpha.4 fix: undo accidental revert
1 parent 35fb54c commit dd8bc7f

File tree

7 files changed

+86
-6
lines changed

7 files changed

+86
-6
lines changed

lerna.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"npmClient": "yarn",
33
"useWorkspaces": true,
4-
"version": "2.0.0-alpha.3",
4+
"version": "2.0.0-alpha.4",
55
"packages": [
66
"packages/*"
77
]

packages/docs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-mention-docs",
3-
"version": "0.1.7",
3+
"version": "2.0.0-alpha.4",
44
"private": true,
55
"scripts": {
66
"dev": "vuepress dev src",

packages/test-e2e/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "test-e2e",
3-
"version": "2.0.0-alpha.3",
3+
"version": "2.0.0-alpha.4",
44
"private": true,
55
"scripts": {
66
"serve": "vue-cli-service serve",
@@ -13,7 +13,7 @@
1313
"core-js": "^3.6.4",
1414
"floating-vue": "^2.0.0-beta.1",
1515
"vue": "^3.2.26",
16-
"vue-mention": "^2.0.0-alpha.1",
16+
"vue-mention": "^2.0.0-alpha.4",
1717
"vue-router": "^4.0.12"
1818
},
1919
"devDependencies": {

packages/test-e2e/src/router.js

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ const routes = [
4545
path: '/content-editable',
4646
component: () => import('./views/ContentEditable.vue'),
4747
},
48+
{
49+
path: '/allow-space',
50+
component: () => import('./views/AllowSpace.vue'),
51+
},
4852
]
4953

5054
const router = createRouter({
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<script>
2+
export default {
3+
data () {
4+
return {
5+
text: '',
6+
items: [
7+
{
8+
value: 'akryum',
9+
firstName: 'Guillaume',
10+
},
11+
{
12+
value: 'posva',
13+
firstName: 'Eduardo',
14+
},
15+
{
16+
value: 'space invader',
17+
firstName: 'Space Invader',
18+
},
19+
],
20+
}
21+
},
22+
}
23+
</script>
24+
25+
<template>
26+
<div
27+
class="demo"
28+
>
29+
<Mentionable
30+
:keys="['@']"
31+
:items="items"
32+
allow-space
33+
popper-class="defaults-mention"
34+
>
35+
<textarea
36+
v-model="text"
37+
rows="6"
38+
class="input"
39+
/>
40+
</Mentionable>
41+
42+
<div class="preview">{{ text }}</div>
43+
</div>
44+
</template>
45+
46+
<style>
47+
.defaults-mention .mention-selected {
48+
background: #aaffc7;
49+
}
50+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
describe('with allow-space prop', () => {
2+
it('shows suggestions after typing mention containing space', () => {
3+
cy.visit('/allow-space')
4+
cy.get('.input').type('abc @space in')
5+
cy.get('.v-popper__popper').should('be.visible')
6+
.should('contain', 'space invader')
7+
cy.get('.input').type('{enter}')
8+
cy.get('.preview').should('contain', '@space invader')
9+
})
10+
})

packages/vue-mention/src/Mentionable.vue

+18-2
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)