@mojsica/vue-mention-input 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -2
- package/dist/index.cjs +3 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +410 -0
- package/dist/models.d.ts +22 -0
- package/dist/styles.css +1 -0
- package/package.json +51 -4
package/README.md
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mojsica/vue-mention-input
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Contenteditable `@` mention input for Vue 3 with chips and structured `MentionContentSection[]` value.
|
|
4
|
+
|
|
5
|
+
Shares behavior with the Angular/React packages via private `@mojsica/mention-core`.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i @mojsica/vue-mention-input
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { MentionInput } from '@mojsica/vue-mention-input';
|
|
15
|
+
import '@mojsica/vue-mention-input/styles.css';
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```vue
|
|
19
|
+
<MentionInput
|
|
20
|
+
v-model="sections"
|
|
21
|
+
:users="users"
|
|
22
|
+
:loading="loading"
|
|
23
|
+
@mention-query-change="onQuery"
|
|
24
|
+
/>
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Local demo
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pnpm --filter demo-vue dev
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## License
|
|
34
|
+
|
|
35
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("vue"),x="mi-mention",k="mi-mention-draft";function F(o){const a=window.getSelection();if(!(a!=null&&a.rangeCount))return;const t=a.getRangeAt(0);t.collapsed||t.deleteContents();let l=null;o.forEach(f=>{t.insertNode(f),l=f,t.setStartAfter(f)}),l&&(t.collapse(!0),a.removeAllRanges(),a.addRange(t))}function b(o){const a=[];return o.forEach(t=>{if(t.type==="text"){const l=t.message.split(`
|
|
2
|
+
`);l.forEach((f,r)=>{f&&a.push(document.createTextNode(f)),r<l.length-1&&a.push(document.createElement("br"))})}if(t.type==="mention"){const l=document.createElement("span");l.classList.add(x),l.setAttribute("contenteditable","false"),l.setAttribute("userId",t.userId||""),l.textContent=` @${t.userName} `,a.push(l)}}),a}function D(o){var l;const a=z(o),t=[];return a.childNodes.length===1&&((l=o.childNodes[0])==null?void 0:l.tagName)==="BR"||a.childNodes.forEach(f=>{var r,u;if(f.nodeType===Node.TEXT_NODE){const s=(r=f.textContent)==null?void 0:r.replace(/\u00A0/g," ");s!=null&&s.trim()&&t.push({type:"text",message:s})}if(f.nodeType===Node.ELEMENT_NODE){const s=f;if(s.classList.contains(x)||s.classList.contains("tag-wrap")){const c=s.getAttribute("userId")||"",m=((u=s.textContent)==null?void 0:u.replace(/^\s*@/,"").trim())||"";t.push({type:"mention",userId:c,userName:m})}else s.tagName==="BR"&&t.push({type:"text",message:`
|
|
3
|
+
`})}}),t}function z(o){const a=o.cloneNode(!0),t=l=>{const f=Array.from(l.childNodes);for(const r of f){if(r.nodeType!==Node.ELEMENT_NODE)continue;const u=r,s=u.parentElement;if(!s)continue;const c=u.tagName.toUpperCase();if(c==="DIV"){const m=Array.from(u.childNodes);if(m.length===1&&m[0].nodeType===Node.ELEMENT_NODE&&m[0].tagName==="BR")s.insertBefore(m[0],r),s.removeChild(r),t(s);else{const N=document.createElement("br");for(s.insertBefore(N,r);r.firstChild;)s.insertBefore(r.firstChild,r);s.removeChild(r),t(s)}}else if(c==="SPAN"&&!u.hasAttribute("userId")||c==="FONT"){for(;r.firstChild;)s.insertBefore(r.firstChild,r);s.removeChild(r),t(s)}else if(c==="BR"||c==="SPAN"&&u.hasAttribute("userId")||c==="LIB-MENTION-PICKER")t(r);else{for(;r.firstChild;)s.insertBefore(r.firstChild,r);s.removeChild(r),t(s)}}};return t(a),a}function U(o){const a=window.getSelection();if(!(a!=null&&a.rangeCount))return;const t=a.getRangeAt(0);let l=t.startContainer.childNodes[t.startOffset-1];(l==null?void 0:l.nodeName)==="BR"&&(l=t.startContainer.childNodes[t.startOffset-2]),(l==null?void 0:l.contentEditable)==="false"&&(o.preventDefault(),l.remove())}function q(o){const a=window.getSelection();if(!(!o||!a))try{const t=document.createRange();t.selectNodeContents(o),t.collapse(!1),a.removeAllRanges(),a.addRange(t)}catch{}}function H(o){const a=window.getSelection();if(!(a!=null&&a.rangeCount))return;const t=a.getRangeAt(0);t.collapse(!0),t.insertNode(o),t.setStartAfter(o),t.collapse(!0),a.removeAllRanges(),a.addRange(t)}function K(o){if(!o)return;const a=window.getSelection();a&&requestAnimationFrame(()=>{const t=document.createRange();t.setStartAfter(o),t.collapse(!0),a.removeAllRanges(),a.addRange(t)})}function L(o){var f,r;const a=o.parentNode;if(!a)return;const t=((r=(f=o.childNodes[0])==null?void 0:f.textContent)==null?void 0:r.trim())||"",l=document.createTextNode(t);a.insertBefore(l,o),a.removeChild(o)}function A(o){const a=window.getSelection(),t=(a==null?void 0:a.anchorNode)??null;return t?o===t||o.contains(t):!1}const Q={class:"mi-picker"},W={key:0,class:"mi-picker__status"},j={key:1,class:"mi-picker__status"},J=["aria-selected","onMousedown"],X={class:"mi-user-avatar","aria-hidden":"true"},G=["src"],Y={class:"mi-user-meta"},Z={class:"mi-user-name"},ee={key:0,class:"mi-user-email"},M=e.defineComponent({__name:"MentionPicker",props:{users:{},search:{},loading:{type:Boolean,default:!1},clientFilter:{type:Boolean,default:!0},emptyText:{default:"No users found"},left:{},top:{}},emits:["select","close"],setup(o,{emit:a}){const t=o,l=a;function f(c){return c.split(/\s+/).filter(Boolean).slice(0,2).map(m=>{var p;return((p=m[0])==null?void 0:p.toUpperCase())??""}).join("")}const r=e.computed(()=>{if(!t.clientFilter)return t.users;const c=t.search.trim().toLowerCase();return c?t.users.filter(m=>{var p;return m.name.toLowerCase().includes(c)||(((p=m.email)==null?void 0:p.toLowerCase().includes(c))??!1)}):t.users}),u=e.ref(0);e.watch(()=>[t.search,r.value.length],()=>{u.value=0});function s(c){if(c.key==="Escape"){c.preventDefault(),l("close");return}r.value.length&&(c.key==="ArrowDown"?(c.preventDefault(),u.value=(u.value+1)%r.value.length):c.key==="ArrowUp"?(c.preventDefault(),u.value=(u.value-1+r.value.length)%r.value.length):c.key==="Enter"&&(c.preventDefault(),l("select",r.value[u.value])))}return e.onMounted(()=>document.addEventListener("keydown",s)),e.onUnmounted(()=>document.removeEventListener("keydown",s)),(c,m)=>(e.openBlock(),e.createElementBlock("div",{class:"mi-picker-host",style:e.normalizeStyle({left:`${o.left}px`,top:`${o.top}px`}),role:"listbox"},[e.createElementVNode("div",Q,[o.loading?(e.openBlock(),e.createElementBlock("div",W,"Loading…")):r.value.length===0?(e.openBlock(),e.createElementBlock("div",j,e.toDisplayString(o.emptyText),1)):(e.openBlock(!0),e.createElementBlock(e.Fragment,{key:2},e.renderList(r.value,(p,N)=>(e.openBlock(),e.createElementBlock("button",{key:p.id,type:"button",class:e.normalizeClass(["mi-user",{"mi-user--active":N===u.value}]),role:"option","aria-selected":N===u.value,onMousedown:e.withModifiers(C=>l("select",p),["prevent"])},[e.createElementVNode("span",X,[p.avatarUrl?(e.openBlock(),e.createElementBlock("img",{key:0,src:p.avatarUrl,alt:""},null,8,G)):(e.openBlock(),e.createElementBlock(e.Fragment,{key:1},[e.createTextVNode(e.toDisplayString(f(p.name)),1)],64))]),e.createElementVNode("span",Y,[e.createElementVNode("span",Z,e.toDisplayString(p.name),1),p.email?(e.openBlock(),e.createElementBlock("span",ee,e.toDisplayString(p.email),1)):e.createCommentVNode("",!0)])],42,J))),128))])],4))}}),te={key:0,class:"mi-placeholder"},ne=["contenteditable"],oe={key:1,class:"mi-message mi-message--error"},ae={key:2,class:"mi-message"},se=e.defineComponent({__name:"MentionInput",props:{modelValue:{default:()=>[]},users:{default:()=>[]},loading:{type:Boolean,default:!1},clientFilter:{type:Boolean,default:!0},emptyText:{default:"No users found"},placeholder:{default:"Write something… use @ to mention"},disabled:{type:Boolean,default:!1},hasError:{type:Boolean,default:!1},errorMessage:{default:""},helperText:{default:""},className:{},style:{}},emits:["update:modelValue","mentionQueryChange","mentionSelected","mentionOpenChange"],setup(o,{emit:a}){const t=o,l=a;function f(i,n){return JSON.stringify(i)===JSON.stringify(n)}const r=e.ref(null),u=e.ref(!1),s=e.ref(null),c=e.ref(!0),m=e.ref([]),p=e.computed(()=>`mi-editor-wrap${t.disabled?" mi-editor-wrap--disabled":""}${t.className?` ${t.className}`:""}`);function N(i,n){var y;const d=!!n.querySelector(`span.${k}`),g=!!((y=n.textContent)!=null&&y.replace(/\u00A0/g," ").trim());c.value=i.length===0&&!d&&!g}function C(){const i=r.value;if(!i)return;const n=D(i);N(n,i),!f(n,m.value)&&(m.value=n,l("update:modelValue",n))}function R(i){const n=r.value;if(!n)return;const d=b(i);n.innerHTML="",d.forEach(g=>n.appendChild(g)),m.value=i,N(i,n)}e.watch(()=>t.modelValue,i=>{f(i??[],m.value)||R(i??[])},{deep:!0,immediate:!0});function B(i){const n=i.getBoundingClientRect(),d=Math.min(Math.max(8,n.left),window.innerWidth-260),g=Math.min(n.bottom+4,window.innerHeight-16);return{left:d,top:Math.max(8,g)}}function w(i){var d;if(!u.value)return;u.value=!1;const n=(d=s.value)==null?void 0:d.span;s.value=null,l("mentionOpenChange",!1),(i==null?void 0:i.unwrap)!==!1&&(n!=null&&n.isConnected)&&n.classList.contains(k)&&L(n),C(),E()}function I(){if(!r.value||t.disabled)return;const n=document.createElement("span");n.className=k,n.contentEditable="true",n.style.position="relative",n.style.display="inline-block",n.appendChild(document.createTextNode("@")),H(n),q(n),u.value=!0;const d=B(n);s.value={span:n,search:"",...d},c.value=!1,l("mentionOpenChange",!0),l("mentionQueryChange",""),e.nextTick(()=>$())}function O(i){var d;const n=(d=s.value)==null?void 0:d.span;n&&(u.value=!1,n.innerHTML="",n.classList.remove(k),n.classList.add(x),n.contentEditable="false",n.setAttribute("userId",i.id),n.style.position="",n.style.display="",n.appendChild(document.createTextNode(`@${i.name}`)),K(n),s.value=null,l("mentionSelected",i),l("mentionOpenChange",!1),C(),E())}let E=()=>{};function $(){if(E(),!s.value)return;const n=()=>{if(!u.value||!s.value)return;if(!A(s.value.span)){w();return}const h=r.value,v=h==null?void 0:h.querySelector(`span.${k}[contenteditable="true"]`);if(!v){w({unwrap:!1});return}const S=v.innerText??"";if(!S.startsWith("@")){w();return}const T=S.slice(1),P=B(v);s.value={span:v,search:T,...P},l("mentionQueryChange",T)},d=()=>{var v;if(!u.value||!((v=s.value)!=null&&v.span.isConnected))return;const h=window.getSelection();!(h!=null&&h.rangeCount)||!h.anchorNode||A(s.value.span)||w()},g=()=>{var v;if(!((v=s.value)!=null&&v.span.isConnected))return;const h=B(s.value.span);s.value={...s.value,...h}},y=r.value;y==null||y.addEventListener("input",n),document.addEventListener("selectionchange",d),window.addEventListener("resize",g),window.addEventListener("scroll",g,!0),E=()=>{y==null||y.removeEventListener("input",n),document.removeEventListener("selectionchange",d),window.removeEventListener("resize",g),window.removeEventListener("scroll",g,!0),E=()=>{}}}function V(i){var n;if(i.key==="@"){i.preventDefault(),I();return}if(i.key==="Backspace"){const d=(n=r.value)==null?void 0:n.querySelector(`span.${k}[contenteditable="true"]`);if(d&&d.innerText==="@"){i.preventDefault(),u.value=!1,s.value=null,l("mentionOpenChange",!1),L(d),C(),E();return}U(i)}}function _(i){var h,v;i.preventDefault();const n=((h=i.clipboardData)==null?void 0:h.getData("text/html"))||((v=i.clipboardData)==null?void 0:v.getData("text/plain"));if(!n)return;const d=document.createElement("div");d.innerHTML=n;const g=D(d),y=b(g);F(y),C()}return e.onBeforeUnmount(()=>{E()}),(i,n)=>(e.openBlock(),e.createElementBlock("div",{class:e.normalizeClass(p.value),style:e.normalizeStyle(o.style)},[o.placeholder&&c.value?(e.openBlock(),e.createElementBlock("span",te,e.toDisplayString(o.placeholder),1)):e.createCommentVNode("",!0),e.createElementVNode("p",{ref_key:"editorRef",ref:r,class:e.normalizeClass(["mi-editor",{"mi-editor--error":o.hasError}]),contenteditable:!o.disabled,onInput:C,onKeydown:V,onPaste:_},null,42,ne),o.hasError&&o.errorMessage?(e.openBlock(),e.createElementBlock("span",oe,e.toDisplayString(o.errorMessage),1)):o.helperText?(e.openBlock(),e.createElementBlock("span",ae,e.toDisplayString(o.helperText),1)):e.createCommentVNode("",!0),(e.openBlock(),e.createBlock(e.Teleport,{to:"body"},[s.value?(e.openBlock(),e.createBlock(M,{key:0,users:o.users,search:s.value.search,loading:o.loading,"client-filter":o.clientFilter,"empty-text":o.emptyText,left:s.value.left,top:s.value.top,onSelect:O,onClose:n[0]||(n[0]=d=>w())},null,8,["users","search","loading","client-filter","empty-text","left","top"])):e.createCommentVNode("",!0)]))],6))}});exports.MentionInput=se;exports.MentionPicker=M;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { DefineComponent } from 'vue';
|
|
2
|
+
import type {
|
|
3
|
+
MentionContentSection,
|
|
4
|
+
MentionSection,
|
|
5
|
+
MentionSectionType,
|
|
6
|
+
MentionUser,
|
|
7
|
+
TextSection,
|
|
8
|
+
} from './models';
|
|
9
|
+
|
|
10
|
+
export type {
|
|
11
|
+
MentionContentSection,
|
|
12
|
+
MentionSection,
|
|
13
|
+
MentionSectionType,
|
|
14
|
+
MentionUser,
|
|
15
|
+
TextSection,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export declare const MentionInput: DefineComponent<
|
|
19
|
+
{
|
|
20
|
+
modelValue?: MentionContentSection[];
|
|
21
|
+
users?: MentionUser[];
|
|
22
|
+
loading?: boolean;
|
|
23
|
+
clientFilter?: boolean;
|
|
24
|
+
emptyText?: string;
|
|
25
|
+
placeholder?: string;
|
|
26
|
+
disabled?: boolean;
|
|
27
|
+
hasError?: boolean;
|
|
28
|
+
errorMessage?: string | null;
|
|
29
|
+
helperText?: string;
|
|
30
|
+
className?: string;
|
|
31
|
+
style?: Record<string, string>;
|
|
32
|
+
},
|
|
33
|
+
{},
|
|
34
|
+
unknown
|
|
35
|
+
>;
|
|
36
|
+
|
|
37
|
+
export declare const MentionPicker: DefineComponent<object, object, unknown>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
import { defineComponent as U, computed as V, ref as b, watch as q, onMounted as G, onUnmounted as Y, openBlock as p, createElementBlock as y, normalizeStyle as z, createElementVNode as L, toDisplayString as E, Fragment as I, renderList as Z, withModifiers as ee, normalizeClass as D, createTextVNode as te, createCommentVNode as A, onBeforeUnmount as ne, createBlock as O, Teleport as se, nextTick as ae } from "vue";
|
|
2
|
+
const R = "mi-mention", T = "mi-mention-draft";
|
|
3
|
+
function oe(n) {
|
|
4
|
+
const s = window.getSelection();
|
|
5
|
+
if (!(s != null && s.rangeCount))
|
|
6
|
+
return;
|
|
7
|
+
const e = s.getRangeAt(0);
|
|
8
|
+
e.collapsed || e.deleteContents();
|
|
9
|
+
let r = null;
|
|
10
|
+
n.forEach((d) => {
|
|
11
|
+
e.insertNode(d), r = d, e.setStartAfter(d);
|
|
12
|
+
}), r && (e.collapse(!0), s.removeAllRanges(), s.addRange(e));
|
|
13
|
+
}
|
|
14
|
+
function $(n) {
|
|
15
|
+
const s = [];
|
|
16
|
+
return n.forEach((e) => {
|
|
17
|
+
if (e.type === "text") {
|
|
18
|
+
const r = e.message.split(`
|
|
19
|
+
`);
|
|
20
|
+
r.forEach((d, o) => {
|
|
21
|
+
d && s.push(document.createTextNode(d)), o < r.length - 1 && s.push(document.createElement("br"));
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
if (e.type === "mention") {
|
|
25
|
+
const r = document.createElement("span");
|
|
26
|
+
r.classList.add(R), r.setAttribute("contenteditable", "false"), r.setAttribute("userId", e.userId || ""), r.textContent = ` @${e.userName} `, s.push(r);
|
|
27
|
+
}
|
|
28
|
+
}), s;
|
|
29
|
+
}
|
|
30
|
+
function _(n) {
|
|
31
|
+
var r;
|
|
32
|
+
const s = re(n), e = [];
|
|
33
|
+
return s.childNodes.length === 1 && ((r = n.childNodes[0]) == null ? void 0 : r.tagName) === "BR" || s.childNodes.forEach((d) => {
|
|
34
|
+
var o, c;
|
|
35
|
+
if (d.nodeType === Node.TEXT_NODE) {
|
|
36
|
+
const a = (o = d.textContent) == null ? void 0 : o.replace(/\u00A0/g, " ");
|
|
37
|
+
a != null && a.trim() && e.push({ type: "text", message: a });
|
|
38
|
+
}
|
|
39
|
+
if (d.nodeType === Node.ELEMENT_NODE) {
|
|
40
|
+
const a = d;
|
|
41
|
+
if (a.classList.contains(R) || a.classList.contains("tag-wrap")) {
|
|
42
|
+
const l = a.getAttribute("userId") || "", f = ((c = a.textContent) == null ? void 0 : c.replace(/^\s*@/, "").trim()) || "";
|
|
43
|
+
e.push({ type: "mention", userId: l, userName: f });
|
|
44
|
+
} else a.tagName === "BR" && e.push({ type: "text", message: `
|
|
45
|
+
` });
|
|
46
|
+
}
|
|
47
|
+
}), e;
|
|
48
|
+
}
|
|
49
|
+
function re(n) {
|
|
50
|
+
const s = n.cloneNode(!0), e = (r) => {
|
|
51
|
+
const d = Array.from(r.childNodes);
|
|
52
|
+
for (const o of d) {
|
|
53
|
+
if (o.nodeType !== Node.ELEMENT_NODE)
|
|
54
|
+
continue;
|
|
55
|
+
const c = o, a = c.parentElement;
|
|
56
|
+
if (!a)
|
|
57
|
+
continue;
|
|
58
|
+
const l = c.tagName.toUpperCase();
|
|
59
|
+
if (l === "DIV") {
|
|
60
|
+
const f = Array.from(c.childNodes);
|
|
61
|
+
if (f.length === 1 && f[0].nodeType === Node.ELEMENT_NODE && f[0].tagName === "BR")
|
|
62
|
+
a.insertBefore(f[0], o), a.removeChild(o), e(a);
|
|
63
|
+
else {
|
|
64
|
+
const C = document.createElement("br");
|
|
65
|
+
for (a.insertBefore(C, o); o.firstChild; )
|
|
66
|
+
a.insertBefore(o.firstChild, o);
|
|
67
|
+
a.removeChild(o), e(a);
|
|
68
|
+
}
|
|
69
|
+
} else if (l === "SPAN" && !c.hasAttribute("userId") || l === "FONT") {
|
|
70
|
+
for (; o.firstChild; )
|
|
71
|
+
a.insertBefore(o.firstChild, o);
|
|
72
|
+
a.removeChild(o), e(a);
|
|
73
|
+
} else if (l === "BR" || l === "SPAN" && c.hasAttribute("userId") || l === "LIB-MENTION-PICKER")
|
|
74
|
+
e(o);
|
|
75
|
+
else {
|
|
76
|
+
for (; o.firstChild; )
|
|
77
|
+
a.insertBefore(o.firstChild, o);
|
|
78
|
+
a.removeChild(o), e(a);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
return e(s), s;
|
|
83
|
+
}
|
|
84
|
+
function ie(n) {
|
|
85
|
+
const s = window.getSelection();
|
|
86
|
+
if (!(s != null && s.rangeCount))
|
|
87
|
+
return;
|
|
88
|
+
const e = s.getRangeAt(0);
|
|
89
|
+
let r = e.startContainer.childNodes[e.startOffset - 1];
|
|
90
|
+
(r == null ? void 0 : r.nodeName) === "BR" && (r = e.startContainer.childNodes[e.startOffset - 2]), (r == null ? void 0 : r.contentEditable) === "false" && (n.preventDefault(), r.remove());
|
|
91
|
+
}
|
|
92
|
+
function le(n) {
|
|
93
|
+
const s = window.getSelection();
|
|
94
|
+
if (!(!n || !s))
|
|
95
|
+
try {
|
|
96
|
+
const e = document.createRange();
|
|
97
|
+
e.selectNodeContents(n), e.collapse(!1), s.removeAllRanges(), s.addRange(e);
|
|
98
|
+
} catch {
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function ce(n) {
|
|
102
|
+
const s = window.getSelection();
|
|
103
|
+
if (!(s != null && s.rangeCount))
|
|
104
|
+
return;
|
|
105
|
+
const e = s.getRangeAt(0);
|
|
106
|
+
e.collapse(!0), e.insertNode(n), e.setStartAfter(n), e.collapse(!0), s.removeAllRanges(), s.addRange(e);
|
|
107
|
+
}
|
|
108
|
+
function ue(n) {
|
|
109
|
+
if (!n)
|
|
110
|
+
return;
|
|
111
|
+
const s = window.getSelection();
|
|
112
|
+
s && requestAnimationFrame(() => {
|
|
113
|
+
const e = document.createRange();
|
|
114
|
+
e.setStartAfter(n), e.collapse(!0), s.removeAllRanges(), s.addRange(e);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
function P(n) {
|
|
118
|
+
var d, o;
|
|
119
|
+
const s = n.parentNode;
|
|
120
|
+
if (!s)
|
|
121
|
+
return;
|
|
122
|
+
const e = ((o = (d = n.childNodes[0]) == null ? void 0 : d.textContent) == null ? void 0 : o.trim()) || "", r = document.createTextNode(e);
|
|
123
|
+
s.insertBefore(r, n), s.removeChild(n);
|
|
124
|
+
}
|
|
125
|
+
function F(n) {
|
|
126
|
+
const s = window.getSelection(), e = (s == null ? void 0 : s.anchorNode) ?? null;
|
|
127
|
+
return e ? n === e || n.contains(e) : !1;
|
|
128
|
+
}
|
|
129
|
+
const de = { class: "mi-picker" }, fe = {
|
|
130
|
+
key: 0,
|
|
131
|
+
class: "mi-picker__status"
|
|
132
|
+
}, me = {
|
|
133
|
+
key: 1,
|
|
134
|
+
class: "mi-picker__status"
|
|
135
|
+
}, pe = ["aria-selected", "onMousedown"], he = {
|
|
136
|
+
class: "mi-user-avatar",
|
|
137
|
+
"aria-hidden": "true"
|
|
138
|
+
}, ve = ["src"], ge = { class: "mi-user-meta" }, ye = { class: "mi-user-name" }, Ne = {
|
|
139
|
+
key: 0,
|
|
140
|
+
class: "mi-user-email"
|
|
141
|
+
}, Ce = /* @__PURE__ */ U({
|
|
142
|
+
__name: "MentionPicker",
|
|
143
|
+
props: {
|
|
144
|
+
users: {},
|
|
145
|
+
search: {},
|
|
146
|
+
loading: { type: Boolean, default: !1 },
|
|
147
|
+
clientFilter: { type: Boolean, default: !0 },
|
|
148
|
+
emptyText: { default: "No users found" },
|
|
149
|
+
left: {},
|
|
150
|
+
top: {}
|
|
151
|
+
},
|
|
152
|
+
emits: ["select", "close"],
|
|
153
|
+
setup(n, { emit: s }) {
|
|
154
|
+
const e = n, r = s;
|
|
155
|
+
function d(l) {
|
|
156
|
+
return l.split(/\s+/).filter(Boolean).slice(0, 2).map((f) => {
|
|
157
|
+
var m;
|
|
158
|
+
return ((m = f[0]) == null ? void 0 : m.toUpperCase()) ?? "";
|
|
159
|
+
}).join("");
|
|
160
|
+
}
|
|
161
|
+
const o = V(() => {
|
|
162
|
+
if (!e.clientFilter)
|
|
163
|
+
return e.users;
|
|
164
|
+
const l = e.search.trim().toLowerCase();
|
|
165
|
+
return l ? e.users.filter(
|
|
166
|
+
(f) => {
|
|
167
|
+
var m;
|
|
168
|
+
return f.name.toLowerCase().includes(l) || (((m = f.email) == null ? void 0 : m.toLowerCase().includes(l)) ?? !1);
|
|
169
|
+
}
|
|
170
|
+
) : e.users;
|
|
171
|
+
}), c = b(0);
|
|
172
|
+
q(
|
|
173
|
+
() => [e.search, o.value.length],
|
|
174
|
+
() => {
|
|
175
|
+
c.value = 0;
|
|
176
|
+
}
|
|
177
|
+
);
|
|
178
|
+
function a(l) {
|
|
179
|
+
if (l.key === "Escape") {
|
|
180
|
+
l.preventDefault(), r("close");
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
o.value.length && (l.key === "ArrowDown" ? (l.preventDefault(), c.value = (c.value + 1) % o.value.length) : l.key === "ArrowUp" ? (l.preventDefault(), c.value = (c.value - 1 + o.value.length) % o.value.length) : l.key === "Enter" && (l.preventDefault(), r("select", o.value[c.value])));
|
|
184
|
+
}
|
|
185
|
+
return G(() => document.addEventListener("keydown", a)), Y(() => document.removeEventListener("keydown", a)), (l, f) => (p(), y("div", {
|
|
186
|
+
class: "mi-picker-host",
|
|
187
|
+
style: z({ left: `${n.left}px`, top: `${n.top}px` }),
|
|
188
|
+
role: "listbox"
|
|
189
|
+
}, [
|
|
190
|
+
L("div", de, [
|
|
191
|
+
n.loading ? (p(), y("div", fe, "Loading…")) : o.value.length === 0 ? (p(), y("div", me, E(n.emptyText), 1)) : (p(!0), y(I, { key: 2 }, Z(o.value, (m, C) => (p(), y("button", {
|
|
192
|
+
key: m.id,
|
|
193
|
+
type: "button",
|
|
194
|
+
class: D(["mi-user", { "mi-user--active": C === c.value }]),
|
|
195
|
+
role: "option",
|
|
196
|
+
"aria-selected": C === c.value,
|
|
197
|
+
onMousedown: ee((x) => r("select", m), ["prevent"])
|
|
198
|
+
}, [
|
|
199
|
+
L("span", he, [
|
|
200
|
+
m.avatarUrl ? (p(), y("img", {
|
|
201
|
+
key: 0,
|
|
202
|
+
src: m.avatarUrl,
|
|
203
|
+
alt: ""
|
|
204
|
+
}, null, 8, ve)) : (p(), y(I, { key: 1 }, [
|
|
205
|
+
te(E(d(m.name)), 1)
|
|
206
|
+
], 64))
|
|
207
|
+
]),
|
|
208
|
+
L("span", ge, [
|
|
209
|
+
L("span", ye, E(m.name), 1),
|
|
210
|
+
m.email ? (p(), y("span", Ne, E(m.email), 1)) : A("", !0)
|
|
211
|
+
])
|
|
212
|
+
], 42, pe))), 128))
|
|
213
|
+
])
|
|
214
|
+
], 4));
|
|
215
|
+
}
|
|
216
|
+
}), we = {
|
|
217
|
+
key: 0,
|
|
218
|
+
class: "mi-placeholder"
|
|
219
|
+
}, Ee = ["contenteditable"], xe = {
|
|
220
|
+
key: 1,
|
|
221
|
+
class: "mi-message mi-message--error"
|
|
222
|
+
}, Te = {
|
|
223
|
+
key: 2,
|
|
224
|
+
class: "mi-message"
|
|
225
|
+
}, ke = /* @__PURE__ */ U({
|
|
226
|
+
__name: "MentionInput",
|
|
227
|
+
props: {
|
|
228
|
+
modelValue: { default: () => [] },
|
|
229
|
+
users: { default: () => [] },
|
|
230
|
+
loading: { type: Boolean, default: !1 },
|
|
231
|
+
clientFilter: { type: Boolean, default: !0 },
|
|
232
|
+
emptyText: { default: "No users found" },
|
|
233
|
+
placeholder: { default: "Write something… use @ to mention" },
|
|
234
|
+
disabled: { type: Boolean, default: !1 },
|
|
235
|
+
hasError: { type: Boolean, default: !1 },
|
|
236
|
+
errorMessage: { default: "" },
|
|
237
|
+
helperText: { default: "" },
|
|
238
|
+
className: {},
|
|
239
|
+
style: {}
|
|
240
|
+
},
|
|
241
|
+
emits: ["update:modelValue", "mentionQueryChange", "mentionSelected", "mentionOpenChange"],
|
|
242
|
+
setup(n, { emit: s }) {
|
|
243
|
+
const e = n, r = s;
|
|
244
|
+
function d(i, t) {
|
|
245
|
+
return JSON.stringify(i) === JSON.stringify(t);
|
|
246
|
+
}
|
|
247
|
+
const o = b(null), c = b(!1), a = b(null), l = b(!0), f = b([]), m = V(
|
|
248
|
+
() => `mi-editor-wrap${e.disabled ? " mi-editor-wrap--disabled" : ""}${e.className ? ` ${e.className}` : ""}`
|
|
249
|
+
);
|
|
250
|
+
function C(i, t) {
|
|
251
|
+
var N;
|
|
252
|
+
const u = !!t.querySelector(`span.${T}`), g = !!((N = t.textContent) != null && N.replace(/\u00A0/g, " ").trim());
|
|
253
|
+
l.value = i.length === 0 && !u && !g;
|
|
254
|
+
}
|
|
255
|
+
function x() {
|
|
256
|
+
const i = o.value;
|
|
257
|
+
if (!i)
|
|
258
|
+
return;
|
|
259
|
+
const t = _(i);
|
|
260
|
+
C(t, i), !d(t, f.value) && (f.value = t, r("update:modelValue", t));
|
|
261
|
+
}
|
|
262
|
+
function H(i) {
|
|
263
|
+
const t = o.value;
|
|
264
|
+
if (!t)
|
|
265
|
+
return;
|
|
266
|
+
const u = $(i);
|
|
267
|
+
t.innerHTML = "", u.forEach((g) => t.appendChild(g)), f.value = i, C(i, t);
|
|
268
|
+
}
|
|
269
|
+
q(
|
|
270
|
+
() => e.modelValue,
|
|
271
|
+
(i) => {
|
|
272
|
+
d(i ?? [], f.value) || H(i ?? []);
|
|
273
|
+
},
|
|
274
|
+
{ deep: !0, immediate: !0 }
|
|
275
|
+
);
|
|
276
|
+
function S(i) {
|
|
277
|
+
const t = i.getBoundingClientRect(), u = Math.min(Math.max(8, t.left), window.innerWidth - 260), g = Math.min(t.bottom + 4, window.innerHeight - 16);
|
|
278
|
+
return { left: u, top: Math.max(8, g) };
|
|
279
|
+
}
|
|
280
|
+
function k(i) {
|
|
281
|
+
var u;
|
|
282
|
+
if (!c.value)
|
|
283
|
+
return;
|
|
284
|
+
c.value = !1;
|
|
285
|
+
const t = (u = a.value) == null ? void 0 : u.span;
|
|
286
|
+
a.value = null, r("mentionOpenChange", !1), (i == null ? void 0 : i.unwrap) !== !1 && (t != null && t.isConnected) && t.classList.contains(T) && P(t), x(), w();
|
|
287
|
+
}
|
|
288
|
+
function K() {
|
|
289
|
+
if (!o.value || e.disabled)
|
|
290
|
+
return;
|
|
291
|
+
const t = document.createElement("span");
|
|
292
|
+
t.className = T, t.contentEditable = "true", t.style.position = "relative", t.style.display = "inline-block", t.appendChild(document.createTextNode("@")), ce(t), le(t), c.value = !0;
|
|
293
|
+
const u = S(t);
|
|
294
|
+
a.value = { span: t, search: "", ...u }, l.value = !1, r("mentionOpenChange", !0), r("mentionQueryChange", ""), ae(() => W());
|
|
295
|
+
}
|
|
296
|
+
function Q(i) {
|
|
297
|
+
var u;
|
|
298
|
+
const t = (u = a.value) == null ? void 0 : u.span;
|
|
299
|
+
t && (c.value = !1, t.innerHTML = "", t.classList.remove(T), t.classList.add(R), t.contentEditable = "false", t.setAttribute("userId", i.id), t.style.position = "", t.style.display = "", t.appendChild(document.createTextNode(`@${i.name}`)), ue(t), a.value = null, r("mentionSelected", i), r("mentionOpenChange", !1), x(), w());
|
|
300
|
+
}
|
|
301
|
+
let w = () => {
|
|
302
|
+
};
|
|
303
|
+
function W() {
|
|
304
|
+
if (w(), !a.value)
|
|
305
|
+
return;
|
|
306
|
+
const t = () => {
|
|
307
|
+
if (!c.value || !a.value)
|
|
308
|
+
return;
|
|
309
|
+
if (!F(a.value.span)) {
|
|
310
|
+
k();
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
const h = o.value, v = h == null ? void 0 : h.querySelector(
|
|
314
|
+
`span.${T}[contenteditable="true"]`
|
|
315
|
+
);
|
|
316
|
+
if (!v) {
|
|
317
|
+
k({ unwrap: !1 });
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
const B = v.innerText ?? "";
|
|
321
|
+
if (!B.startsWith("@")) {
|
|
322
|
+
k();
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
const M = B.slice(1), X = S(v);
|
|
326
|
+
a.value = { span: v, search: M, ...X }, r("mentionQueryChange", M);
|
|
327
|
+
}, u = () => {
|
|
328
|
+
var v;
|
|
329
|
+
if (!c.value || !((v = a.value) != null && v.span.isConnected))
|
|
330
|
+
return;
|
|
331
|
+
const h = window.getSelection();
|
|
332
|
+
!(h != null && h.rangeCount) || !h.anchorNode || F(a.value.span) || k();
|
|
333
|
+
}, g = () => {
|
|
334
|
+
var v;
|
|
335
|
+
if (!((v = a.value) != null && v.span.isConnected))
|
|
336
|
+
return;
|
|
337
|
+
const h = S(a.value.span);
|
|
338
|
+
a.value = { ...a.value, ...h };
|
|
339
|
+
}, N = o.value;
|
|
340
|
+
N == null || N.addEventListener("input", t), document.addEventListener("selectionchange", u), window.addEventListener("resize", g), window.addEventListener("scroll", g, !0), w = () => {
|
|
341
|
+
N == null || N.removeEventListener("input", t), document.removeEventListener("selectionchange", u), window.removeEventListener("resize", g), window.removeEventListener("scroll", g, !0), w = () => {
|
|
342
|
+
};
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
function J(i) {
|
|
346
|
+
var t;
|
|
347
|
+
if (i.key === "@") {
|
|
348
|
+
i.preventDefault(), K();
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
if (i.key === "Backspace") {
|
|
352
|
+
const u = (t = o.value) == null ? void 0 : t.querySelector(
|
|
353
|
+
`span.${T}[contenteditable="true"]`
|
|
354
|
+
);
|
|
355
|
+
if (u && u.innerText === "@") {
|
|
356
|
+
i.preventDefault(), c.value = !1, a.value = null, r("mentionOpenChange", !1), P(u), x(), w();
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
ie(i);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
function j(i) {
|
|
363
|
+
var h, v;
|
|
364
|
+
i.preventDefault();
|
|
365
|
+
const t = ((h = i.clipboardData) == null ? void 0 : h.getData("text/html")) || ((v = i.clipboardData) == null ? void 0 : v.getData("text/plain"));
|
|
366
|
+
if (!t)
|
|
367
|
+
return;
|
|
368
|
+
const u = document.createElement("div");
|
|
369
|
+
u.innerHTML = t;
|
|
370
|
+
const g = _(u), N = $(g);
|
|
371
|
+
oe(N), x();
|
|
372
|
+
}
|
|
373
|
+
return ne(() => {
|
|
374
|
+
w();
|
|
375
|
+
}), (i, t) => (p(), y("div", {
|
|
376
|
+
class: D(m.value),
|
|
377
|
+
style: z(n.style)
|
|
378
|
+
}, [
|
|
379
|
+
n.placeholder && l.value ? (p(), y("span", we, E(n.placeholder), 1)) : A("", !0),
|
|
380
|
+
L("p", {
|
|
381
|
+
ref_key: "editorRef",
|
|
382
|
+
ref: o,
|
|
383
|
+
class: D(["mi-editor", { "mi-editor--error": n.hasError }]),
|
|
384
|
+
contenteditable: !n.disabled,
|
|
385
|
+
onInput: x,
|
|
386
|
+
onKeydown: J,
|
|
387
|
+
onPaste: j
|
|
388
|
+
}, null, 42, Ee),
|
|
389
|
+
n.hasError && n.errorMessage ? (p(), y("span", xe, E(n.errorMessage), 1)) : n.helperText ? (p(), y("span", Te, E(n.helperText), 1)) : A("", !0),
|
|
390
|
+
(p(), O(se, { to: "body" }, [
|
|
391
|
+
a.value ? (p(), O(Ce, {
|
|
392
|
+
key: 0,
|
|
393
|
+
users: n.users,
|
|
394
|
+
search: a.value.search,
|
|
395
|
+
loading: n.loading,
|
|
396
|
+
"client-filter": n.clientFilter,
|
|
397
|
+
"empty-text": n.emptyText,
|
|
398
|
+
left: a.value.left,
|
|
399
|
+
top: a.value.top,
|
|
400
|
+
onSelect: Q,
|
|
401
|
+
onClose: t[0] || (t[0] = (u) => k())
|
|
402
|
+
}, null, 8, ["users", "search", "loading", "client-filter", "empty-text", "left", "top"])) : A("", !0)
|
|
403
|
+
]))
|
|
404
|
+
], 6));
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
export {
|
|
408
|
+
ke as MentionInput,
|
|
409
|
+
Ce as MentionPicker
|
|
410
|
+
};
|
package/dist/models.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type MentionSectionType = 'text' | 'mention';
|
|
2
|
+
|
|
3
|
+
export interface TextSection {
|
|
4
|
+
type: 'text';
|
|
5
|
+
message: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface MentionSection {
|
|
9
|
+
type: 'mention';
|
|
10
|
+
userId: string;
|
|
11
|
+
userName: string;
|
|
12
|
+
email?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type MentionContentSection = TextSection | MentionSection;
|
|
16
|
+
|
|
17
|
+
export interface MentionUser {
|
|
18
|
+
id: string;
|
|
19
|
+
name: string;
|
|
20
|
+
email?: string;
|
|
21
|
+
avatarUrl?: string;
|
|
22
|
+
}
|
package/dist/styles.css
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
.mi-editor-wrap{position:relative;display:block}.mi-editor-wrap--disabled{opacity:.65;pointer-events:none}.mi-placeholder{position:absolute;top:12px;left:14px;color:var(--mi-placeholder-color, #9ca3af);pointer-events:none;-webkit-user-select:none;user-select:none;z-index:1}.mi-editor{min-height:var(--mi-editor-min-height, 88px);max-height:180px;overflow:auto;margin:0;padding:12px 14px;border:1px solid var(--mi-editor-border, #d1d5db);border-radius:var(--mi-editor-radius, 8px);background:var(--mi-editor-bg, #fff);outline:none;line-height:1.5;white-space:pre-wrap;word-break:break-word}.mi-editor:focus{border-color:#6366f1;box-shadow:0 0 0 3px #6366f126}.mi-editor--error{border-color:#ef4444}.mi-editor>*{pointer-events:none}.mi-editor .mi-mention{pointer-events:none;background:var(--mi-mention-bg, #e0e7ff);color:var(--mi-mention-color, #3730a3);border-radius:4px;padding:0 2px;font-weight:500}.mi-editor .mi-mention-draft{pointer-events:auto;color:var(--mi-mention-draft-color, #4f46e5);font-weight:500}.mi-message{display:block;margin-top:4px;font-size:12px;color:#6b7280}.mi-message--error{color:#ef4444}.mi-picker-host{position:fixed;z-index:10000;min-width:240px;max-width:320px}.mi-picker{max-height:240px;overflow:auto;border:1px solid var(--mi-editor-border, #d1d5db);border-radius:var(--mi-editor-radius, 8px);background:var(--mi-picker-bg, #fff);box-shadow:var(--mi-picker-shadow, 0 8px 24px rgba(0, 0, 0, .12));padding:4px}.mi-picker__status{padding:10px 12px;color:#6b7280;font-size:13px}.mi-user{display:flex;align-items:center;gap:10px;width:100%;border:0;background:transparent;text-align:left;padding:8px 10px;border-radius:6px;cursor:pointer}.mi-user:hover,.mi-user--active{background:var(--mi-user-active-bg, #eef2ff)}.mi-user-avatar{width:var(--mi-avatar-size, 28px);height:var(--mi-avatar-size, 28px);border-radius:50%;background:#e5e7eb;color:#374151;display:inline-flex;align-items:center;justify-content:center;font-size:11px;font-weight:600;overflow:hidden;flex-shrink:0}.mi-user-avatar img{width:100%;height:100%;object-fit:cover}.mi-user-meta{display:flex;flex-direction:column;min-width:0}.mi-user-name{font-size:13px;color:#111827;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.mi-user-email{font-size:12px;color:#6b7280;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mojsica/vue-mention-input",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Contenteditable @mention input for Vue with chips and structured sections.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/mojsica/mention-input.git",
|
|
9
|
+
"directory": "packages/vue-mention-input"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"vue",
|
|
13
|
+
"mention",
|
|
14
|
+
"contenteditable",
|
|
15
|
+
"chips"
|
|
16
|
+
],
|
|
17
|
+
"type": "module",
|
|
18
|
+
"main": "./dist/index.cjs",
|
|
19
|
+
"module": "./dist/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"import": "./dist/index.js",
|
|
25
|
+
"require": "./dist/index.cjs"
|
|
26
|
+
},
|
|
27
|
+
"./styles.css": "./dist/styles.css"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"sideEffects": [
|
|
33
|
+
"**/*.css"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "vite build && cp src/index.d.ts dist/index.d.ts && cp src/models.ts dist/models.d.ts",
|
|
37
|
+
"dev": "vite build --watch"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"vue": "^3.4.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@mojsica/mention-core": "workspace:*",
|
|
44
|
+
"@vitejs/plugin-vue": "^5.2.1",
|
|
45
|
+
"typescript": "~5.9.2",
|
|
46
|
+
"vite": "^6.0.0",
|
|
47
|
+
"vue": "^3.5.13",
|
|
48
|
+
"vue-tsc": "^2.1.10"
|
|
49
|
+
},
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
}
|
|
53
|
+
}
|