Files
aurora-admin/frameworks/Mention.html
T

97 lines
4.0 KiB
HTML
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.
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Aurora Admin · Mention</title>
<link rel="stylesheet" href="../.design_library/aurora-admin/colors_and_type.css" />
<link rel="stylesheet" href="./Mention.css" />
</head>
<body>
<div class="aa-page">
<h2 class="aa-h2">Mention @ 提及</h2>
<p class="aa-desc">输入框输入 @ 触发用户面板,选择后以标签形式插入,可删除。</p>
<div class="aa-demo">
<div class="aa-mention" id="mention">
<div class="aa-mention-area">
<textarea class="aa-mention-input" id="input" rows="3" placeholder="输入 @ 提及同事…"></textarea>
<div class="aa-mention-panel" id="panel" style="display:none"></div>
</div>
<div class="aa-mention-chips" id="chips"></div>
</div>
</div>
</div>
<script>
var users = [
{ name: '王伟', dept: '华东大区' },
{ name: '李娜', dept: '华南大区' },
{ name: '张强', dept: '华北大区' },
{ name: '陈静', dept: '研发中心' },
{ name: '刘洋', dept: '市场部' }
];
var input = document.getElementById('input');
var panel = document.getElementById('panel');
var chips = document.getElementById('chips');
var active = -1, mentioned = [];
function renderPanel() {
panel.innerHTML = users.map(function (u, i) {
return '<div class="aa-mention-item' + (i === active ? ' is-active' : '') + '" data-i="' + i + '">' +
'<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>';
}).join('');
}
function openPanel() { active = -1; renderPanel(); panel.style.display = 'block'; }
function closePanel() { panel.style.display = 'none'; }
function insert(name) {
var pos = input.selectionStart;
var v = input.value;
// 找到触发 @ 的位置(当前光标前一个 @ 且其后无空格)
var at = v.lastIndexOf('@', pos - 1);
if (at >= 0 && (at === 0 || /\s/.test(v[at - 1]))) {
input.value = v.slice(0, at) + '@' + name + ' ' + v.slice(pos);
} else {
input.value = v.slice(0, pos) + '@' + name + ' ' + v.slice(pos);
}
if (mentioned.indexOf(name) < 0) mentioned.push(name);
renderChips();
closePanel();
input.focus();
}
function renderChips() {
chips.innerHTML = mentioned.map(function (n) {
return '<span class="aa-mention-chip">@' + n + ' <b data-n="' + n + '">×</b></span>';
}).join('');
}
input.addEventListener('input', function () {
var pos = input.selectionStart;
var v = input.value;
var at = v.lastIndexOf('@', pos - 1);
if (at >= 0 && (at === 0 || /\s/.test(v[at - 1])) && !/\s/.test(v.slice(at + 1, pos))) openPanel();
else closePanel();
});
input.addEventListener('keydown', function (e) {
if (panel.style.display === 'none') return;
if (e.key === 'ArrowDown') { e.preventDefault(); active = Math.min(users.length - 1, active + 1); renderPanel(); }
else if (e.key === 'ArrowUp') { e.preventDefault(); active = Math.max(0, active - 1); renderPanel(); }
else if (e.key === 'Enter' && active >= 0) { e.preventDefault(); insert(users[active].name); }
else if (e.key === 'Escape') closePanel();
});
panel.addEventListener('click', function (e) {
var it = e.target.closest('.aa-mention-item');
if (it) insert(users[parseInt(it.dataset.i, 10)].name);
});
chips.addEventListener('click', function (e) {
var b = e.target.closest('[data-n]');
if (b) { mentioned = mentioned.filter(function (n) { return n !== b.dataset.n; }); renderChips(); }
});
document.addEventListener('click', function (e) {
if (!document.getElementById('mention').contains(e.target)) closePanel();
});
</script>
</body>
</html>