Files
aurora-admin/frameworks/Mention.vue2.vue
T

82 lines
2.9 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="aa-mention" v-click-outside="close">
<div class="aa-mention-area">
<textarea
ref="input"
class="aa-mention-input"
:value="value"
rows="3"
placeholder="输入 @ 提及同事…"
@input="onInput"
@keydown.down.prevent="move(1)"
@keydown.up.prevent="move(-1)"
@keydown.enter.prevent="enter"
@keydown.esc="close"
></textarea>
<div v-show="open" class="aa-mention-panel">
<div
v-for="(u, i) in users"
:key="u.name"
class="aa-mention-item"
:class="{ 'is-active': i === active }"
@click="insert(u.name)"
>
<span class="aa-mention-avatar">{{ u.name[0] }}</span>
<span class="aa-mention-name">{{ u.name }}</span>
<span class="aa-mention-dept">{{ u.dept }}</span>
</div>
</div>
</div>
<div class="aa-mention-chips">
<span v-for="n in mentioned" :key="n" class="aa-mention-chip">@{{ n }} <b @click="remove(n)">×</b></span>
</div>
</div>
</template>
<script>
export default {
name: 'AaMention',
directives: {
clickOutside: { bind(el, b) { el.__d = (e) => { if (!el.contains(e.target)) b.value(); }; document.addEventListener('click', el.__d); }, unbind(el) { document.removeEventListener('click', el.__d); } }
},
props: {
value: { type: String, default: '' },
users: {
type: Array,
default: () => [
{ name: '王伟', dept: '华东大区' }, { name: '李娜', dept: '华南大区' }, { name: '张强', dept: '华北大区' },
{ name: '陈静', dept: '研发中心' }, { name: '刘洋', dept: '市场部' }
]
}
},
data() {
return { open: false, active: -1, mentioned: [] };
},
methods: {
onInput(e) {
var pos = e.target.selectionStart, v = e.target.value;
this.$emit('input', v);
var at = v.lastIndexOf('@', pos - 1);
if (at >= 0 && (at === 0 || /\s/.test(v[at - 1])) && !/\s/.test(v.slice(at + 1, pos))) { this.active = -1; this.open = true; }
else this.open = false;
},
move(d) { this.active = Math.max(-1, Math.min(this.users.length - 1, this.active + d)); },
enter() { if (this.active >= 0) this.insert(this.users[this.active].name); },
insert(name) {
var el = this.$refs.input, pos = el.selectionStart, v = el.value;
var at = v.lastIndexOf('@', pos - 1);
if (at >= 0 && (at === 0 || /\s/.test(v[at - 1]))) el.value = v.slice(0, at) + '@' + name + ' ' + v.slice(pos);
else el.value = v.slice(0, pos) + '@' + name + ' ' + v.slice(pos);
if (this.mentioned.indexOf(name) < 0) this.mentioned.push(name);
this.$emit('input', el.value);
this.close();
el.focus();
},
remove(n) { this.mentioned = this.mentioned.filter((x) => x !== n); },
close() { this.open = false; }
}
};
</script>
<style src="./Mention.css"></style>