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

83 lines
3.0 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" ref="root">
<div class="aa-mention-area">
<textarea
ref="input"
class="aa-mention-input"
:value="modelValue"
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 setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';
const props = defineProps({
modelValue: { type: String, default: '' },
users: {
type: Array,
default: () => [
{ name: '王伟', dept: '华东大区' }, { name: '李娜', dept: '华南大区' }, { name: '张强', dept: '华北大区' },
{ name: '陈静', dept: '研发中心' }, { name: '刘洋', dept: '市场部' }
]
}
});
const emit = defineEmits(['update:modelValue']);
const root = ref(null);
const input = ref(null);
const open = ref(false);
const active = ref(-1);
const mentioned = ref([]);
function onInput(e) {
const pos = e.target.selectionStart, v = e.target.value;
emit('update:modelValue', v);
const at = v.lastIndexOf('@', pos - 1);
if (at >= 0 && (at === 0 || /\s/.test(v[at - 1])) && !/\s/.test(v.slice(at + 1, pos))) { active.value = -1; open.value = true; }
else open.value = false;
}
function move(d) { active.value = Math.max(-1, Math.min(props.users.length - 1, active.value + d)); }
function enter() { if (active.value >= 0) insert(props.users[active.value].name); }
function insert(name) {
const el = input.value, pos = el.selectionStart, v = el.value;
const 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 (mentioned.value.indexOf(name) < 0) mentioned.value.push(name);
emit('update:modelValue', el.value);
open.value = false;
el.focus();
}
function remove(n) { mentioned.value = mentioned.value.filter((x) => x !== n); }
function close() { open.value = false; }
function onDoc(e) { if (root.value && !root.value.contains(e.target)) close(); }
onMounted(() => document.addEventListener('click', onDoc));
onBeforeUnmount(() => document.removeEventListener('click', onDoc));
</script>
<style src="./Mention.css"></style>