cosmic-ai-input 1.0.13 → 1.0.15
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/dist/index.cjs.js +6 -6
- package/dist/index.es.js +380 -363
- package/dist/style.css +1 -1
- package/package.json +2 -2
package/dist/index.cjs.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// Generated at: 2026-
|
|
2
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const u=require("react/jsx-runtime"),c=require("react"),
|
|
3
|
-
`&&(
|
|
4
|
-
`)return[{type:"text",content:"",id:`text-${f++}-${d}`}];const P=new Set((p||[]).map(T=>T.name));for(;(a=i.exec(
|
|
5
|
-
`);M.forEach((
|
|
6
|
-
`);t.forEach((M,J)=>{M&&s.push({type:"text",content:M,id:`text-${f++}`}),J<t.length-1&&s.push({type:"newline",id:`newline-${f++}`})})}return s},ie=()=>{var n;(n=m.current)==null||n.setAttribute("contenteditable","true"),S.current&&(O(S.current)&&S.current.children[0].setAttribute("contenteditable","false"),S.current=null),F(m.current)},je=n=>{b==null||b(n)},Me=n=>{ie()},K=n=>{Y(V()),de(xe()),x==null||x(V()),Qe(n)},Be=n=>{var s;if(!re.current&&n.code==="Enter"&&!n.shiftKey){n.preventDefault();const e=V();if(e.length===0||(s=m.current)!=null&&s.innerHTML.match(/^<span>[\s\u200B]*(<br\s*\/?>[\s\u200B]*)+<\/span>$/))return;C?e.length<=C?y==null||y(e):Z==null||Z(e,C):y==null||y(e)}},He=n=>{var e;if(_&&(w==null?void 0:w.type)===g.LABEL&&(n.code==="ArrowUp"||n.code==="ArrowDown"||n.code==="Enter")){n.preventDefault();return}B(!1),R==null||R(n);const s=st();if(s&&ot(s)){if(n.code==="ArrowLeft"){const i=s==null?void 0:s.previousElementSibling,a=ae(s);i&&a===0&&O(i)&&i.children[0]&&(S.current=i,H(i),requestAnimationFrame(()=>{F(i.children[0])}))}if(n.code==="ArrowRight"){const i=s==null?void 0:s.nextElementSibling,a=ae(s);i&&a===((e=s.textContent)==null?void 0:e.length)&&O(i)&&i.children[0]&&(S.current=i,H(i),requestAnimationFrame(()=>{Q(i.children[0])}))}n.code==="Backspace"&&(s.innerHTML===""&&n.preventDefault(),s.innerHTML==="<br>"&&(n.preventDefault(),s.innerHTML=""))}if(s&&s.getAttribute("data-set-type")==="container"){if(n.code==="ArrowLeft"){const i=lt();O(i)&&i.children[0]&&(S.current=i,H(i),requestAnimationFrame(()=>{F(i.children[0])}))}if(n.code==="ArrowRight"){const i=ct();O(i)&&i.children[0]&&(S.current=i,H(i),requestAnimationFrame(()=>{Q(i.children[0])}))}}Be(n)},Fe=n=>{if(_&&(w==null?void 0:w.type)===g.LABEL&&(n.code==="ArrowUp"||n.code==="ArrowDown"||n.code==="Enter")){n.preventDefault();return}q==null||q(n)},Oe=n=>{n.preventDefault();const e=window.getSelection().toString();if(!e)return;const i=e,a=e;n.clipboardData.setData("text/plain",i),n.clipboardData.setData("text/html",a)},qe=n=>{A==null||A(n)},Ve=()=>{re.current=!0},_e=()=>{re.current=!1},ve=(n,s)=>{s.stopPropagation()},Ke=(n,s)=>{var i;s.stopPropagation(),S.current&&me(S.current),(i=m.current)==null||i.setAttribute("contenteditable","false");const e=document.getElementById(n.id);S.current=e,requestAnimationFrame(()=>{H(e)})},Ue=(n,s)=>{var a,d,f,P,T;R==null||R(s),s.stopPropagation();const e=s.target,i=ae(e);if(s.code==="Backspace"&&i===0){const t=(a=e.parentElement)==null?void 0:a.previousElementSibling;t&&(O(t)&&t.children[0]&&((d=t.children[0].textContent)!=null&&d.length)?(S.current=t,H(t),F(t.children[0]),requestAnimationFrame(()=>{oe(t.children[0])})):(ie(),F(t)))}if(s.code==="ArrowRight"&&i===((f=e.textContent)==null?void 0:f.length)){const t=(P=e.parentElement)==null?void 0:P.nextElementSibling;t&&(O(t)&&t.children[0]&&((T=t.children[0].textContent)!=null&&T.length)?(S.current=t,H(t),Q(t.children[0]),requestAnimationFrame(()=>{oe(t.children[0])})):(ie(),Q(t)))}s.code==="Enter"&&s.shiftKey&&s.preventDefault()},We=(n,s)=>{q==null||q(s);const e=s.target;oe(e)},ze=(n,s)=>{var i;s.stopPropagation(),S.current&&me(S.current),(i=m.current)==null||i.setAttribute("contenteditable","false");const e=document.getElementById(n.id);S.current=e,requestAnimationFrame(()=>{H(e)})},Xe=(n,s)=>{Y(V()),de(xe()),x==null||x(V())},Ge=(n,s)=>{};c.useEffect(()=>{if(!_)return;const n=i=>{j.current&&!j.current.contains(i.target)&&B(!1)},s=i=>{j.current&&!j.current.contains(i.target)&&B(!1)},e=()=>{B(!1)};return document.addEventListener("mousedown",n),document.addEventListener("scroll",s,!0),window.addEventListener("resize",e),()=>{document.removeEventListener("mousedown",n),document.removeEventListener("scroll",s,!0),window.removeEventListener("resize",e)}},[_]);const X=(n,s)=>{if(w&&s!==g.LABEL&&nt(w.id,n),s===g.SELECT&&(B(!1),K({})),s===g.LABEL&&(ce(),B(!1),requestAnimationFrame(()=>{const e=window.getSelection();e&&G.current&&(e.removeAllRanges(),e.addRange(G.current),setTimeout(()=>{var f;const i=document.createElement("span");i.contentEditable="false",i.setAttribute("data-set-type","label"),i.id=w.id;const a=document.createElement("span");a.contentEditable="false",a.textContent=n,i.appendChild(a);const d=(f=m.current)==null?void 0:f.children;if((d==null?void 0:d.length)===1&&d[0].tagName==="SPAN"&&d[0].innerHTML==="[")m.current.innerHTML="",m.current.appendChild(i),setTimeout(()=>{K({}),ce()},0);else{document.execCommand("delete",!1,void 0);const P=G.current;P.insertNode(i),P.setStartAfter(i),P.setEndAfter(i),e.removeAllRanges(),e.addRange(P),setTimeout(()=>{K({})},0)}},0))})),s===g.MULTIPLE_SELECT||s===g.DATE_PICKER){const e=document.getElementById(w.id);e&&e.children[0]&&e.children[1]&&(n.length>0?(e.children[0].setAttribute("style","display: inline"),e.children[0].setAttribute("contenteditable","false"),e.children[1].setAttribute("style","display: none"),e.children[1].setAttribute("contenteditable","true")):(e.children[0].setAttribute("style","display: none"),e.children[0].setAttribute("contenteditable","true"),e.children[1].setAttribute("style","display: inline"),e.children[1].setAttribute("contenteditable","true"))),K({})}},G=c.useRef(null),Je=(D==null?void 0:D.map(n=>n.prefix))||[],Qe=n=>{const s=n==null?void 0:n.nativeEvent,e=s==null?void 0:s.data;Je.includes(e)&&it().then(i=>{var d;const a=window.getSelection();a&&a.rangeCount>0&&(G.current=a.getRangeAt(0)),Ze({options:((d=D.find(f=>f.prefix===e))==null?void 0:d.options)||[],value:"",type:g.LABEL,id:"label-"+Se()},i)})},Ze=(n,s)=>{te(n.options),v({top:-999999,left:-999999999}),ne(n),z(ge),B(!0),requestAnimationFrame(()=>{if(j.current){const e=j.current.getBoundingClientRect();let i=s.left,a=s.top+s.height;window.innerWidth-s.right<he&&s.width<e.width&&(i=s.right-e.width),s.top<e.height&&(a=s.top+s.height),v({top:a,left:i}),z(e.height)}})},be=(n,s)=>{const i=s.target.getBoundingClientRect();te(n.options),ue(n.value||Le(n.id)),v({top:-999999,left:-999999999}),ne(n),z(ge),B(!0),requestAnimationFrame(()=>{if(j.current){const a=j.current.getBoundingClientRect();let d=i.left,f=i.top+i.height;window.innerWidth-i.right<he&&i.width<a.width&&(d=i.right-a.width),i.top<a.height&&(f=i.top+i.height),v({top:f,left:d}),z(a.height)}})},Ie=(n,s)=>{const i=s.target.getBoundingClientRect();let a=i.left,d=i.top-ye;window.innerWidth-i.right<Ee&&(a=a-Ee+i.width),i.top<ye&&(d=i.top+i.height),te(n.options),ue(n.value||Le(n.id)),v({top:d,left:a}),ne(n),B(!0)},[Ye,we]=c.useState([]),[le,Ce]=c.useState(!0),Ae=c.useCallback(()=>{Ce(!1);const n=$e(l),s=[];return n.forEach(e=>{var i,a,d,f,P,T;if(e.type==="text")s.push(u.jsx("span",{onKeyDown:t=>Ge(),children:e.content},e.id));else if(e.type==="newline")s.push(u.jsx("br",{},e.id));else if(e.type==="label")s.push(u.jsx("span",{id:e.id,"data-set-type":"label",contentEditable:!1,children:u.jsx("span",{contentEditable:!1,children:e.name})},e.id));else if(e.type==="input")s.push(u.jsxs("span",{id:e.id,"data-set-type":"input",contentEditable:!1,suppressContentEditableWarning:!0,onInput:t=>Xe(),onClick:t=>ve(e,t),onMouseDown:t=>Ke(e,t),"data-cosmic-ai-input-placeholder":e.placeholder,children:[u.jsx("span",{className:"cosmic-ai-input-inputContent",style:{padding:(i=e.content)!=null&&i.length?"2px 4px":"2px 0 2px 4px"},contentEditable:!1,onKeyDown:t=>Ue(e,t),onKeyUp:t=>We(e,t),children:e.content}),u.jsx("span",{contentEditable:!1,style:{display:(a=e.content)!=null&&a.length?"none":"inline"},className:"cosmic-ai-input-placeholder",onMouseDown:t=>ze(e,t),children:e.placeholder})]},e.id));else if(e.type===g.SELECT){const t=e.content||((d=e.options)==null?void 0:d[0])||e.placeholder;s.push(u.jsx("span",{id:e.id,"data-set-type":"select",contentEditable:!1,suppressContentEditableWarning:!0,onClick:M=>be(e,M),"data-cosmic-ai-input-placeholder":e.placeholder,children:u.jsx("span",{contentEditable:!1,children:t})},e.id))}else if(e.type===g.MULTIPLE_SELECT){const t=e.content||((f=e.options)==null?void 0:f[0]);s.push(u.jsxs("span",{id:e.id,"data-set-type":"multiple-select",contentEditable:!1,suppressContentEditableWarning:!0,onClick:M=>be(e,M),"data-cosmic-ai-input-placeholder":e.placeholder,children:[u.jsx("span",{style:{display:t!=null&&t.length?"inline":"none"},contentEditable:!1,children:t}),u.jsx("span",{style:{display:t!=null&&t.length?"none":"inline"},className:"cosmic-ai-input-selectPlaceholder",contentEditable:!1,children:e.placeholder})]},e.id))}else e.type===g.DATE_PICKER&&s.push(u.jsxs("span",{id:e.id,"data-set-type":"date-picker",contentEditable:!1,suppressContentEditableWarning:!0,onClick:t=>Ie(e,t),"data-cosmic-ai-input-placeholder":e.placeholder,children:[u.jsx("span",{style:{display:(P=e.content)!=null&&P.length?"inline":"none"},contentEditable:!1,children:e.content}),u.jsx("span",{style:{display:(T=e.content)!=null&&T.length?"none":"inline"},className:"cosmic-ai-input-datePickerPlaceholder",contentEditable:!1,children:e.placeholder})]},e.id))}),setTimeout(()=>{Ce(!0)},0),s},[l]);c.useEffect(()=>{le&&setTimeout(()=>{F(m.current)},0)},[le]),c.useEffect(()=>{l!==I?(we(Ae()),Y(l),pe(!0)):(l===""||!l)&&we(Ae())},[l,I]),c.useEffect(()=>{if(!N&&!fe)return;const n=new MutationObserver(()=>{m.current&&document.contains(m.current)&&(requestAnimationFrame(()=>{F(m.current),pe(!1)}),n.disconnect())});return m.current&&n.observe(document.body,{childList:!0,subtree:!0}),()=>n.disconnect()},[N,fe]);const ce=()=>{m.current&&requestAnimationFrame(()=>{F(m.current)})};return c.useImperativeHandle(o,()=>({getCurrentValue:V,focus:ce})),u.jsxs("div",{className:Re(`cosmic-ai-input-rows${E}`),style:{textIndent:L+"px"},children:[le&&u.jsx("div",{"data-set-type":"container","data-cosmic-ai-input-placeholder":k,className:Re("cosmic-ai-input",`cosmic-ai-input-rows${E}`,{"is-disabled":$},{"is-empty":I.length===0}),ref:m,contentEditable:!0,suppressContentEditableWarning:!0,onClick:n=>je(n),onFocus:n=>{qe(n)},onBlur:n=>{h==null||h(n)},onMouseDown:n=>Me(),onKeyDown:n=>He(n),onKeyUp:n=>Fe(n),onInput:n=>K(n),onCopy:n=>Oe(n),onCompositionStart:Ve,onCompositionEnd:_e,children:Ye}),_&&tt.createPortal((w==null?void 0:w.type)===g.MULTIPLE_SELECT?u.jsx(De,{ref:j,options:ee,value:W,position:U,height:se,onChange:X},g.MULTIPLE_SELECT):(w==null?void 0:w.type)===g.DATE_PICKER?u.jsx(Te,{ref:j,value:W,position:U,onChange:X},g.DATE_PICKER):(w==null?void 0:w.type)===g.LABEL?u.jsx(ke,{ref:j,options:ee,value:W,position:U,height:se,onChange:X},g.LABEL):u.jsx(Ne,{ref:j,options:ee,value:W,position:U,height:se,onChange:X},g.SELECT),document.body)]})});Pe.displayName="AiInput";exports.default=Pe;
|
|
1
|
+
// Generated at: 2026-04-03T02:03:21.601Z
|
|
2
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const u=require("react/jsx-runtime"),c=require("react"),me=require("classnames"),nt=require("react-dom");var g=(r=>(r.INPUT="input",r.SELECT="select",r.MULTIPLE_SELECT="multiple-select",r.DATE_PICKER="date-picker",r.LABEL="label",r))(g||{});const Te=c.forwardRef(({value:r,position:o,id:l,onChange:p},k)=>{const[C,y]=c.useState(!0),$=c.useRef(null),N=c.useRef(null);c.useEffect(()=>{typeof k=="function"?k($.current):k&&(k.current=$.current)},[k]);const L=h=>{const E=h.target.value;p==null||p(E,g.DATE_PICKER)};c.useEffect(()=>{if(N.current)try{setTimeout(()=>{D()},100)}catch{}},[]);const D=()=>{if(N.current)try{N.current.showPicker()}catch{}},A=h=>{h.stopPropagation()};return C?u.jsx("div",{ref:$,id:l,style:{top:(o==null?void 0:o.top)||0,left:(o==null?void 0:o.left)||0},className:"cosmic-ai-input-datePicker",suppressHydrationWarning:!0,children:u.jsx("input",{ref:N,type:"date",value:r||"",onChange:L,onClick:A})}):null});Te.displayName="DatePicker";function de(r){const o=r.textContent||"",l=r.nextSibling;o.length?(l==null||l.setAttribute("style","display: none"),r.style.padding="2px 4px",r.style.borderTopLeftRadius="2px",r.style.borderBottomLeftRadius="2px",r.style.borderTopRightRadius="2px",r.style.borderBottomRightRadius="2px"):(l==null||l.setAttribute("style","display: inline"),r.style.padding="2px 0 2px 4px",r.style.borderTopLeftRadius="2px",r.style.borderBottomLeftRadius="2px",r.style.borderTopRightRadius="0",r.style.borderBottomRightRadius="0")}function O(r){if(r&&r.getAttribute("data-set-type")==="input"){const o=r.children[0];o.setAttribute("contenteditable","true"),requestAnimationFrame(()=>{o.focus()})}}function Re(r){r&&r.getAttribute("data-set-type")==="input"&&r.children[0].setAttribute("contenteditable","false")}function q(r){return r&&(r!=null&&r.getAttribute)?r.getAttribute("data-set-type")==="input":!1}const De=c.forwardRef(({options:r,value:o,position:l,id:p,height:k=37,onChange:C},y)=>{const[$,N]=c.useState(o||""),L=c.useRef(null);c.useEffect(()=>{typeof y=="function"?y(L.current):y&&(y.current=L.current)},[y]);const D=h=>{const E=$.split(",").filter(x=>x)||[];let b="";E.includes(h)?b=E.filter(x=>x!==h).join(","):E.length?b=`${E.join(",")},${h}`:b=h,N(b),C==null||C(b,g.MULTIPLE_SELECT)},A=$.split(",")||[];return u.jsx("div",{ref:L,className:"cosmic-ai-input-selectFloatElement",id:p,style:{position:"absolute",top:(l==null?void 0:l.top)||0,left:(l==null?void 0:l.left)||0,zIndex:5e3,maxHeight:`${k}px`},children:r.map((h,E)=>u.jsx("div",{className:`selectOption ${A.includes(h)?"selected":""}`,onClick:()=>D(h),children:h},E))})});De.displayName="MultipleSelect";const Ne=c.forwardRef(({options:r,value:o,position:l,id:p,height:k=120,onChange:C},y)=>{const[$,N]=c.useState(o||""),L=c.useRef(null);c.useEffect(()=>{typeof y=="function"?y(L.current):y&&(y.current=L.current)},[y]);const D=A=>{N(A),C==null||C(A,g.SELECT)};return u.jsx("div",{ref:L,className:"cosmic-ai-input-selectFloatElement",id:p,style:{position:"absolute",top:(l==null?void 0:l.top)||0,left:(l==null?void 0:l.left)||0,zIndex:5e3,maxHeight:`${k}px`},children:r.length>0&&r.map((A,h)=>u.jsx("div",{className:`selectOption ${$===A?"selected":""}`,onClick:()=>D(A),children:A},h))})});Ne.displayName="Select";function Le(r){const o=r instanceof Element?r:document.getElementById(r);return o&&o.children[0].textContent||""}function st(r,o){const l=r instanceof Element?r:document.getElementById(r);l&&(l.children[0].textContent=o)}function I(r){const o=document.createRange(),l=window.getSelection();l&&(!r||!(r instanceof Node)||(o.selectNodeContents(r),o.collapse(!0),l.removeAllRanges(),l.addRange(o)))}function F(r){const o=document.createRange(),l=window.getSelection();l&&(!r||!(r instanceof Node)||(o.selectNodeContents(r),o.collapse(!1),l.removeAllRanges(),l.addRange(o)))}function ue(r){const o=window.getSelection();if(!o||o.rangeCount===0)return 0;if(!r||!(r instanceof Node))return;const l=o.getRangeAt(0),p=l.cloneRange();return p.selectNodeContents(r),p.setEnd(l.startContainer,l.startOffset),p.toString().length}function rt(){const r=window.getSelection();if(!r||r.rangeCount===0)return null;const l=r.getRangeAt(0).startContainer;return l.nodeType===Node.TEXT_NODE?l.parentElement:l.nodeType===Node.ELEMENT_NODE?l:null}function it(){const r=window.getSelection();return!r||r.rangeCount===0?null:r.getRangeAt(0).getBoundingClientRect()}function lt(){return new Promise(r=>{requestAnimationFrame(()=>{r(it())})})}function ct(){const r=window.getSelection();if(!r||r.rangeCount===0)return null;const o=r.getRangeAt(0),l=o.startContainer,p=o.startOffset;return l.nodeType===Node.TEXT_NODE?p>0?l:l.previousSibling:l.nodeType===Node.ELEMENT_NODE?p>0?l.childNodes[p-1]:l.previousSibling:null}function ot(){const r=window.getSelection();if(!r||r.rangeCount===0)return null;const o=r.getRangeAt(0),l=o.startContainer,p=o.startOffset;return l.nodeType===Node.TEXT_NODE?p<l.length?l:l.nextSibling:l.nodeType===Node.ELEMENT_NODE?p<l.childNodes.length?l.childNodes[p]:l.nextSibling:null}function at(r){return r&&!r.getAttribute("data-set-type")}function Se(){return`${Date.now()}-${Math.random().toString(36).slice(2,9)}`}const ke=c.forwardRef(({options:r,value:o,position:l,id:p,height:k=120,onChange:C},y)=>{const[$,N]=c.useState(o||""),[L,D]=c.useState(0),A=c.useRef(null),h=c.useRef(L),E=c.useCallback(b=>{N(b),C==null||C(b,g.LABEL)},[C]);return c.useEffect(()=>{typeof y=="function"?y(A.current):y&&(y.current=A.current)},[y]),c.useEffect(()=>{h.current=L},[L]),c.useEffect(()=>{D(0)},[r]),c.useEffect(()=>{const b=x=>{r.length!==0&&(x.key==="ArrowDown"?(x.preventDefault(),D(m=>m<r.length-1?m+1:m)):x.key==="ArrowUp"?(x.preventDefault(),D(m=>m>0?m-1:m)):x.key==="Enter"&&(x.preventDefault(),setTimeout(()=>{const m=h.current;m>=0&&r[m]&&E(r[m])},0)))};return document.addEventListener("keydown",b),()=>{document.removeEventListener("keydown",b)}},[r,E]),u.jsx("div",{ref:A,className:"cosmic-ai-input-selectFloatElement",id:p,style:{position:"absolute",top:(l==null?void 0:l.top)||0,left:(l==null?void 0:l.left)||0,zIndex:5e3,maxHeight:`${k}px`},children:r.length>0&&r.map((b,x)=>u.jsx("div",{className:`selectOption ${$===b?"selected":""} ${L===x?"highlighted":""}`,onClick:()=>E(b),onMouseEnter:()=>D(x),children:b},x))})});ke.displayName="Mention";const Pe=c.forwardRef((r,o)=>{const{value:l="",varList:p,placeholder:k="",maxLength:C,defaultRows:y=3,disabled:$=!1,defaultFocus:N=!1,textIndex:L=0,mentions:D=[],onFocus:A,onBlur:h,onChange:E,onClick:b,onSend:x,onKeyDown:m,onKeyUp:v,onMaxLengthExceeded:Y}=r,[dt,ut]=c.useState(l||""),[ee,U]=c.useState(""),[ft,te]=c.useState(""),[V,B]=c.useState(!1),[W,_]=c.useState({top:0,left:0}),[ne,se]=c.useState([]),[z,fe]=c.useState(""),[w,re]=c.useState(null),[ie,X]=c.useState(120),[pe,he]=c.useState(N),ge=240,Ee=240,ye=240,xe=276,R=c.useRef(null),S=c.useRef(null),j=c.useRef(null),G=c.useRef(!1),H=()=>{var s,t;let e=((t=(s=R.current)==null?void 0:s.innerText)==null?void 0:t.replace(/\u200B/g,""))||"";return e===`
|
|
3
|
+
`&&(e=""),e},le=()=>{var s;let e=((s=R.current)==null?void 0:s.innerHTML)||"";return e==="<br>"&&(e=""),e},$e=e=>{const s=[];let t=0;const i=/\[([^\]]+)\]/g;let a;const d=Se();let f=0;if(e===""||!e||e===`
|
|
4
|
+
`)return[{type:"text",content:"",id:`text-${f++}-${d}`}];const P=new Set((p||[]).map(T=>T.name));for(;(a=i.exec(e))!==null;){if(a.index>t){const M=e.substring(t,a.index).split(`
|
|
5
|
+
`);M.forEach((Z,tt)=>{Z&&s.push({type:"text",content:Z,id:`text-${f++}-${d}`}),tt<M.length-1&&s.push({type:"newline",id:`newline-${f++}-${d}`})})}const T=a[1];if(P.has(T)){const n=p.find(M=>M.name===T);(n==null?void 0:n.type)==="input"?s.push({type:"input",name:n==null?void 0:n.name,content:(n==null?void 0:n.content)||"",placeholder:(n==null?void 0:n.placeholder)||T,id:`input-${f++}-${d}`}):(n==null?void 0:n.type)==="label"?s.push({type:"label",name:n==null?void 0:n.name,content:(n==null?void 0:n.content)||"",id:`label-${f++}-${d}`}):(n==null?void 0:n.type)==="select"?s.push({type:"select",name:n==null?void 0:n.name,content:(n==null?void 0:n.content)||"",placeholder:(n==null?void 0:n.placeholder)||T,options:(n==null?void 0:n.options)||[],id:`select-${f++}-${d}`}):(n==null?void 0:n.type)==="multiple-select"?s.push({type:"multiple-select",name:n==null?void 0:n.name,content:(n==null?void 0:n.content)||"",placeholder:(n==null?void 0:n.placeholder)||T,options:(n==null?void 0:n.options)||[],id:`multiple-select-${f++}-${d}`}):(n==null?void 0:n.type)==="date-picker"&&s.push({type:"date-picker",name:n==null?void 0:n.name,content:(n==null?void 0:n.content)||"",placeholder:(n==null?void 0:n.placeholder)||T,id:`date-picker-${f++}-${d}`})}else s.push({type:"text",content:`[${T}]`,id:`text-${f++}-${d}`});t=i.lastIndex}if(t<e.length){const n=e.substring(t).split(`
|
|
6
|
+
`);n.forEach((M,Z)=>{M&&s.push({type:"text",content:M,id:`text-${f++}`}),Z<n.length-1&&s.push({type:"newline",id:`newline-${f++}`})})}return s},ce=()=>{var e;(e=R.current)==null||e.setAttribute("contenteditable","true"),S.current&&(q(S.current)&&S.current.children[0].setAttribute("contenteditable","false"),S.current=null),F(R.current)},je=e=>{b==null||b(e)},Me=e=>{ce()},K=e=>{G.current||(U(H()),te(le()),E==null||E(H()),Ze(e))},Be=e=>{var s;if(!G.current&&e.code==="Enter"&&!e.shiftKey){e.preventDefault();const t=H();if(t.length===0||(s=R.current)!=null&&s.innerHTML.match(/^<span>[\s\u200B]*(<br\s*\/?>[\s\u200B]*)+<\/span>$/))return;C?t.length<=C?x==null||x(t):Y==null||Y(t,C):x==null||x(t)}},He=e=>{var t;if(V&&(w==null?void 0:w.type)===g.LABEL&&(e.code==="ArrowUp"||e.code==="ArrowDown"||e.code==="Enter")){e.preventDefault();return}B(!1),m==null||m(e);const s=rt();if(s&&at(s)){if(e.code==="ArrowLeft"){const i=s==null?void 0:s.previousElementSibling,a=ue(s);i&&a===0&&q(i)&&i.children[0]&&(S.current=i,O(i),requestAnimationFrame(()=>{F(i.children[0])}))}if(e.code==="ArrowRight"){const i=s==null?void 0:s.nextElementSibling,a=ue(s);i&&a===((t=s.textContent)==null?void 0:t.length)&&q(i)&&i.children[0]&&(S.current=i,O(i),requestAnimationFrame(()=>{I(i.children[0])}))}e.code==="Backspace"&&(s.innerHTML===""&&e.preventDefault(),s.innerHTML==="<br>"&&(e.preventDefault(),s.innerHTML=""))}if(e.code==="Backspace"&&e.target){const i=e.target;if(i.childElementCount===1&&i.children[0].tagName==="SPAN"){const a=i.children[0];i.childNodes.length===1&&(a.innerHTML===""&&e.preventDefault(),a.innerHTML==="<br>"&&(e.preventDefault(),a.innerHTML=""))}}if(s&&s.getAttribute("data-set-type")==="container"){if(e.code==="ArrowLeft"){const i=ct();q(i)&&i.children[0]&&(S.current=i,O(i),requestAnimationFrame(()=>{F(i.children[0])}))}if(e.code==="ArrowRight"){const i=ot();q(i)&&i.children[0]&&(S.current=i,O(i),requestAnimationFrame(()=>{I(i.children[0])}))}}Be(e)},Oe=e=>{if(V&&(w==null?void 0:w.type)===g.LABEL&&(e.code==="ArrowUp"||e.code==="ArrowDown"||e.code==="Enter")){e.preventDefault();return}v==null||v(e)},Fe=e=>{const t=e.clipboardData.items;for(let i=0;i<t.length;i++)if(t[i].type.indexOf("image")!==-1){e.preventDefault();return}},qe=e=>{e.preventDefault();const t=window.getSelection().toString();if(!t)return;const i=t.replace(/\u200B/g,"")||"",a=t.replace(/\u200B/g,"")||"";e.clipboardData.setData("text/plain",i),e.clipboardData.setData("text/html",a)},ve=e=>{A==null||A(e)},Ve=()=>{G.current=!0},_e=()=>{setTimeout(()=>{G.current=!1,U(H()),te(le()),E==null||E(H())},0)},Ke=(e,s)=>{s.stopPropagation()},Ue=(e,s)=>{var i;s.stopPropagation(),S.current&&Re(S.current),(i=R.current)==null||i.setAttribute("contenteditable","false");const t=document.getElementById(e.id);S.current=t,requestAnimationFrame(()=>{O(t)})},We=(e,s)=>{var a,d,f,P,T;m==null||m(s),s.stopPropagation();const t=s.target,i=ue(t);if(s.code==="Backspace"&&i===0){const n=(a=t.parentElement)==null?void 0:a.previousElementSibling;n&&(q(n)&&n.children[0]&&((d=n.children[0].textContent)!=null&&d.length)?(S.current=n,O(n),F(n.children[0]),requestAnimationFrame(()=>{de(n.children[0])})):(ce(),F(n)))}if(s.code==="ArrowRight"&&i===((f=t.textContent)==null?void 0:f.length)){const n=(P=t.parentElement)==null?void 0:P.nextElementSibling;n&&(q(n)&&n.children[0]&&((T=n.children[0].textContent)!=null&&T.length)?(S.current=n,O(n),I(n.children[0]),requestAnimationFrame(()=>{de(n.children[0])})):(ce(),I(n)))}s.code==="Enter"&&s.shiftKey&&s.preventDefault()},ze=(e,s)=>{v==null||v(s);const t=s.target;de(t)},Xe=(e,s)=>{var i;s.stopPropagation(),S.current&&Re(S.current),(i=R.current)==null||i.setAttribute("contenteditable","false");const t=document.getElementById(e.id);S.current=t,requestAnimationFrame(()=>{O(t)})},Ge=(e,s)=>{U(H()),te(le()),E==null||E(H())},Je=(e,s)=>{};c.useEffect(()=>{if(!V)return;const e=i=>{j.current&&!j.current.contains(i.target)&&B(!1)},s=i=>{j.current&&!j.current.contains(i.target)&&B(!1)},t=()=>{B(!1)};return document.addEventListener("mousedown",e),document.addEventListener("scroll",s,!0),window.addEventListener("resize",t),()=>{document.removeEventListener("mousedown",e),document.removeEventListener("scroll",s,!0),window.removeEventListener("resize",t)}},[V]);const J=(e,s)=>{if(w&&s!==g.LABEL&&st(w.id,e),s===g.SELECT&&(B(!1),K({})),s===g.LABEL&&(ae(),B(!1),requestAnimationFrame(()=>{const t=window.getSelection();t&&Q.current&&(t.removeAllRanges(),t.addRange(Q.current),setTimeout(()=>{var f;const i=document.createElement("span");i.contentEditable="false",i.setAttribute("data-set-type","label"),i.id=w.id;const a=document.createElement("span");a.contentEditable="false",a.textContent=e,i.appendChild(a);const d=(f=R.current)==null?void 0:f.children;if((d==null?void 0:d.length)===1&&d[0].tagName==="SPAN"&&d[0].innerHTML==="[")R.current.innerHTML="",R.current.appendChild(i),setTimeout(()=>{K({}),ae()},0);else{document.execCommand("delete",!1,void 0);const P=Q.current;P.insertNode(i),P.setStartAfter(i),P.setEndAfter(i),t.removeAllRanges(),t.addRange(P),setTimeout(()=>{K({})},0)}},0))})),s===g.MULTIPLE_SELECT||s===g.DATE_PICKER){const t=document.getElementById(w.id);t&&t.children[0]&&t.children[1]&&(e.length>0?(t.children[0].setAttribute("style","display: inline"),t.children[0].setAttribute("contenteditable","false"),t.children[1].setAttribute("style","display: none"),t.children[1].setAttribute("contenteditable","true")):(t.children[0].setAttribute("style","display: none"),t.children[0].setAttribute("contenteditable","true"),t.children[1].setAttribute("style","display: inline"),t.children[1].setAttribute("contenteditable","true"))),K({})}},Q=c.useRef(null),Qe=(D==null?void 0:D.map(e=>e.prefix))||[],Ze=e=>{const s=e==null?void 0:e.nativeEvent,t=s==null?void 0:s.data;Qe.includes(t)&<().then(i=>{var d;const a=window.getSelection();a&&a.rangeCount>0&&(Q.current=a.getRangeAt(0)),Ie({options:((d=D.find(f=>f.prefix===t))==null?void 0:d.options)||[],value:"",type:g.LABEL,id:"label-"+Se()},i)})},Ie=(e,s)=>{se(e.options),_({top:-999999,left:-999999999}),re(e),X(Ee),B(!0),requestAnimationFrame(()=>{if(j.current){const t=j.current.getBoundingClientRect();let i=s.left,a=s.top+s.height;window.innerWidth-s.right<ge&&s.width<t.width&&(i=s.right-t.width),s.top<t.height&&(a=s.top+s.height),_({top:a,left:i}),X(t.height)}})},be=(e,s)=>{const i=s.target.getBoundingClientRect();se(e.options),fe(e.value||Le(e.id)),_({top:-999999,left:-999999999}),re(e),X(Ee),B(!0),requestAnimationFrame(()=>{if(j.current){const a=j.current.getBoundingClientRect();let d=i.left,f=i.top+i.height;window.innerWidth-i.right<ge&&i.width<a.width&&(d=i.right-a.width),i.top<a.height&&(f=i.top+i.height),_({top:f,left:d}),X(a.height)}})},Ye=(e,s)=>{const i=s.target.getBoundingClientRect();let a=i.left,d=i.top-xe;window.innerWidth-i.right<ye&&(a=a-ye+i.width),i.top<xe&&(d=i.top+i.height),se(e.options),fe(e.value||Le(e.id)),_({top:d,left:a}),re(e),B(!0)},[et,we]=c.useState([]),[oe,Ce]=c.useState(!0),Ae=c.useCallback(()=>{Ce(!1);const e=$e(l),s=[];return e.forEach(t=>{var i,a,d,f,P,T;if(t.type==="text")s.push(u.jsx("span",{onKeyDown:n=>Je(),children:t.content},t.id));else if(t.type==="newline")s.push(u.jsx("br",{},t.id));else if(t.type==="label")s.push(u.jsx("span",{id:t.id,"data-set-type":"label",contentEditable:!1,children:u.jsx("span",{contentEditable:!1,children:t.name})},t.id));else if(t.type==="input")s.push(u.jsxs("span",{id:t.id,"data-set-type":"input",contentEditable:!1,suppressContentEditableWarning:!0,onInput:n=>Ge(),onClick:n=>Ke(t,n),onMouseDown:n=>Ue(t,n),"data-cosmic-ai-input-placeholder":t.placeholder,children:[u.jsx("span",{className:"cosmic-ai-input-inputContent",style:{padding:(i=t.content)!=null&&i.length?"2px 4px":"2px 0 2px 4px"},contentEditable:!1,onKeyDown:n=>We(t,n),onKeyUp:n=>ze(t,n),children:t.content}),u.jsx("span",{contentEditable:!1,style:{display:(a=t.content)!=null&&a.length?"none":"inline"},className:"cosmic-ai-input-placeholder",onMouseDown:n=>Xe(t,n),children:t.placeholder})]},t.id));else if(t.type===g.SELECT){const n=t.content||((d=t.options)==null?void 0:d[0])||t.placeholder;s.push(u.jsx("span",{id:t.id,"data-set-type":"select",contentEditable:!1,suppressContentEditableWarning:!0,onClick:M=>be(t,M),"data-cosmic-ai-input-placeholder":t.placeholder,children:u.jsx("span",{contentEditable:!1,children:n})},t.id))}else if(t.type===g.MULTIPLE_SELECT){const n=t.content||((f=t.options)==null?void 0:f[0]);s.push(u.jsxs("span",{id:t.id,"data-set-type":"multiple-select",contentEditable:!1,suppressContentEditableWarning:!0,onClick:M=>be(t,M),"data-cosmic-ai-input-placeholder":t.placeholder,children:[u.jsx("span",{style:{display:n!=null&&n.length?"inline":"none"},contentEditable:!1,children:n}),u.jsx("span",{style:{display:n!=null&&n.length?"none":"inline"},className:"cosmic-ai-input-selectPlaceholder",contentEditable:!1,children:t.placeholder})]},t.id))}else t.type===g.DATE_PICKER&&s.push(u.jsxs("span",{id:t.id,"data-set-type":"date-picker",contentEditable:!1,suppressContentEditableWarning:!0,onClick:n=>Ye(t,n),"data-cosmic-ai-input-placeholder":t.placeholder,children:[u.jsx("span",{style:{display:(P=t.content)!=null&&P.length?"inline":"none"},contentEditable:!1,children:t.content}),u.jsx("span",{style:{display:(T=t.content)!=null&&T.length?"none":"inline"},className:"cosmic-ai-input-datePickerPlaceholder",contentEditable:!1,children:t.placeholder})]},t.id))}),setTimeout(()=>{Ce(!0)},0),s},[l]);c.useEffect(()=>{oe&&setTimeout(()=>{F(R.current)},0)},[oe]),c.useEffect(()=>{l!==ee?(we(Ae()),U(l),he(!0)):(l===""||!l)&&we(Ae())},[l,ee]),c.useEffect(()=>{if(!N&&!pe)return;const e=new MutationObserver(()=>{R.current&&document.contains(R.current)&&(requestAnimationFrame(()=>{F(R.current),he(!1)}),e.disconnect())});return R.current&&e.observe(document.body,{childList:!0,subtree:!0}),()=>e.disconnect()},[N,pe]);const ae=()=>{R.current&&requestAnimationFrame(()=>{F(R.current)})};return c.useImperativeHandle(o,()=>({getCurrentValue:H,focus:ae})),u.jsxs("div",{className:me(`cosmic-ai-input-rows${y}`),style:{textIndent:L+"px"},children:[oe&&u.jsx("div",{"data-set-type":"container","data-cosmic-ai-input-placeholder":k,className:me("cosmic-ai-input",`cosmic-ai-input-rows${y}`,{"is-disabled":$},{"is-empty":ee.length===0}),ref:R,contentEditable:!0,suppressContentEditableWarning:!0,onClick:e=>je(e),onFocus:e=>{ve(e)},onBlur:e=>{h==null||h(e)},onMouseDown:e=>Me(),onKeyDown:e=>He(e),onKeyUp:e=>Oe(e),onInput:e=>K(e),onCopy:e=>qe(e),onPaste:e=>Fe(e),onCompositionStart:Ve,onCompositionEnd:_e,children:et}),V&&nt.createPortal((w==null?void 0:w.type)===g.MULTIPLE_SELECT?u.jsx(De,{ref:j,options:ne,value:z,position:W,height:ie,onChange:J},g.MULTIPLE_SELECT):(w==null?void 0:w.type)===g.DATE_PICKER?u.jsx(Te,{ref:j,value:z,position:W,onChange:J},g.DATE_PICKER):(w==null?void 0:w.type)===g.LABEL?u.jsx(ke,{ref:j,options:ne,value:z,position:W,height:ie,onChange:J},g.LABEL):u.jsx(Ne,{ref:j,options:ne,value:z,position:W,height:ie,onChange:J},g.SELECT),document.body)]})});Pe.displayName="AiInput";exports.default=Pe;
|