cosmic-ai-input 1.0.4 → 1.0.7

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 CHANGED
@@ -1,6 +1,6 @@
1
- // Generated at: 2025-12-30T11:50:42.545Z
2
- "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const u=require("react/jsx-runtime"),i=require("react"),xe=require("classnames"),_e=require("react-dom"),we=i.forwardRef(({value:c,position:r,id:s,onChange:p},S)=>{const[b,w]=i.useState(!0),k=i.useRef(null),R=i.useRef(null);i.useEffect(()=>{typeof S=="function"?S(k.current):S&&(S.current=k.current)},[S]);const m=d=>{console.log("DatePicker handleChange",d);const y=d.target.value;p==null||p(y,"date-picker")};i.useEffect(()=>{if(R.current)try{setTimeout(()=>{N()},100)}catch(d){console.log("无法自动打开日期选择器,浏览器安全策略限制",d)}},[]);const N=()=>{if(R.current)try{R.current.showPicker()}catch(d){console.log("点击后无法打开日期选择器",d)}},g=d=>{d.stopPropagation(),console.log("DatePicker handleClick",d)};return b?u.jsx("div",{ref:k,id:s,style:{top:(r==null?void 0:r.top)||0,left:(r==null?void 0:r.left)||0},className:"cosmic-ai-input-datePicker",suppressHydrationWarning:!0,children:u.jsx("input",{ref:R,type:"date",value:c||"",onChange:m,onClick:g})}):null});we.displayName="DatePicker";function Y(c){const r=c.textContent||"",s=c.nextSibling;r.length?(s==null||s.setAttribute("style","display: none"),c.style.padding="2px 4px",c.style.borderTopLeftRadius="2px",c.style.borderBottomLeftRadius="2px",c.style.borderTopRightRadius="2px",c.style.borderBottomRightRadius="2px"):(s==null||s.setAttribute("style","display: inline"),c.style.padding="2px 0 2px 4px",c.style.borderTopLeftRadius="2px",c.style.borderBottomLeftRadius="2px",c.style.borderTopRightRadius="0",c.style.borderBottomRightRadius="0")}function j(c){if(c&&c.getAttribute("data-set-type")==="input"){const r=c.children[0];r.setAttribute("contenteditable","true"),requestAnimationFrame(()=>{r.focus()})}}function Ee(c){c&&c.getAttribute("data-set-type")==="input"&&c.children[0].setAttribute("contenteditable","false")}function P(c){return c&&c.getAttribute("data-set-type")==="input"}const Ce=i.forwardRef(({options:c,value:r,position:s,id:p,height:S=37,onChange:b},w)=>{const[k,R]=i.useState(r||""),m=i.useRef(null);i.useEffect(()=>{typeof w=="function"?w(m.current):w&&(w.current=m.current)},[w]);const N=d=>{const y=k.split(",").filter(T=>T)||[];let F="";y.includes(d)?F=y.filter(T=>T!==d).join(","):(console.log("handleOptionClick",y),y.length?F=`${y.join(",")},${d}`:F=d),R(F),b==null||b(F,"multiple-select")},g=k.split(",")||[];return u.jsx("div",{ref:m,className:"cosmic-ai-input-selectFloatElement",id:p,style:{position:"absolute",top:(s==null?void 0:s.top)||0,left:(s==null?void 0:s.left)||0,zIndex:5e3,maxHeight:`${S}px`},children:c.map((d,y)=>u.jsx("div",{className:`selectOption ${g.includes(d)?"selected":""}`,onClick:()=>N(d),children:d},y))})});Ce.displayName="MultipleSelect";const me=i.forwardRef(({options:c,value:r,position:s,id:p,height:S=120,onChange:b},w)=>{const[k,R]=i.useState(r||""),m=i.useRef(null);i.useEffect(()=>{typeof w=="function"?w(m.current):w&&(w.current=m.current)},[w]);const N=g=>{R(g),b==null||b(g,"select")};return u.jsx("div",{ref:m,className:"cosmic-ai-input-selectFloatElement",id:p,style:{position:"absolute",top:(s==null?void 0:s.top)||0,left:(s==null?void 0:s.left)||0,zIndex:5e3,maxHeight:`${S}px`},children:c.length>0&&c.map((g,d)=>u.jsx("div",{className:`selectOption ${k===g?"selected":""}`,onClick:()=>N(g),children:g},d))})});me.displayName="Select";function be(c){const r=c instanceof Element?c:document.getElementById(c);return r&&r.children[0].textContent||""}function ze(c,r){const s=c instanceof Element?c:document.getElementById(c);s&&(s.children[0].textContent=r)}function V(c){const r=document.createRange(),s=window.getSelection();if(!s){console.error("无法获取selection对象");return}r.selectNodeContents(c),r.collapse(!0),s.removeAllRanges(),s.addRange(r)}function M(c){const r=document.createRange(),s=window.getSelection();if(!s){console.error("无法获取selection对象");return}r.selectNodeContents(c),r.collapse(!1),s.removeAllRanges(),s.addRange(r)}function Z(c){const r=window.getSelection();if(!r||r.rangeCount===0)return 0;const s=r.getRangeAt(0),p=s.cloneRange();return p.selectNodeContents(c),p.setEnd(s.startContainer,s.startOffset),p.toString().length}function ve(){const c=window.getSelection();if(!c||c.rangeCount===0)return null;const s=c.getRangeAt(0).startContainer;return s.nodeType===Node.TEXT_NODE?s.parentElement:s.nodeType===Node.ELEMENT_NODE?s:null}function Ue(){const c=window.getSelection();if(!c||c.rangeCount===0)return null;const r=c.getRangeAt(0),s=r.startContainer,p=r.startOffset;return s.nodeType===Node.TEXT_NODE?p>0?s:s.previousSibling:s.nodeType===Node.ELEMENT_NODE?p>0?s.childNodes[p-1]:s.previousSibling:null}function Xe(){const c=window.getSelection();if(!c||c.rangeCount===0)return null;const r=c.getRangeAt(0),s=r.startContainer,p=r.startOffset;return s.nodeType===Node.TEXT_NODE?p<s.length?s:s.nextSibling:s.nodeType===Node.ELEMENT_NODE?p<s.childNodes.length?s.childNodes[p]:s.nextSibling:null}function Ge(c){return c&&!c.getAttribute("data-set-type")}const Re=i.forwardRef((c,r)=>{const{value:s="",varList:p,placeholder:S="",maxLength:b,defaultRows:w=3,disabled:k=!1,defaultFocus:R=!1,onFocus:m,onBlur:N,onChange:g,onClick:d,onSend:y,onParse:F,onKeyDown:T,onKeyUp:O,onMaxLengthExceeded:K}=c,[Je,Qe]=i.useState(s||""),[W,_]=i.useState(""),[Ye,I]=i.useState(""),[z,H]=i.useState(!1),[v,U]=i.useState({top:0,left:0}),[ee,te]=i.useState([]),[X,ne]=i.useState(""),[L,le]=i.useState(null),[oe,se]=i.useState(120),[ce,re]=i.useState(R),Se=240,Ae=240,ie=240,ae=276,C=i.useRef(null),x=i.useRef(null),D=i.useRef(null),B=()=>{var l,n;let e=((n=(l=C.current)==null?void 0:l.innerText)==null?void 0:n.replace(/\u200B/g,""))||"";return e===`
1
+ // Generated at: 2026-01-16T02:56:25.442Z
2
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const u=require("react/jsx-runtime"),i=require("react"),xe=require("classnames"),_e=require("react-dom"),we=i.forwardRef(({value:c,position:r,id:s,onChange:p},S)=>{const[b,w]=i.useState(!0),k=i.useRef(null),R=i.useRef(null);i.useEffect(()=>{typeof S=="function"?S(k.current):S&&(S.current=k.current)},[S]);const m=d=>{console.log("DatePicker handleChange",d);const y=d.target.value;p==null||p(y,"date-picker")};i.useEffect(()=>{if(R.current)try{setTimeout(()=>{T()},100)}catch(d){console.log("无法自动打开日期选择器,浏览器安全策略限制",d)}},[]);const T=()=>{if(R.current)try{R.current.showPicker()}catch(d){console.log("点击后无法打开日期选择器",d)}},g=d=>{d.stopPropagation(),console.log("DatePicker handleClick",d)};return b?u.jsx("div",{ref:k,id:s,style:{top:(r==null?void 0:r.top)||0,left:(r==null?void 0:r.left)||0},className:"cosmic-ai-input-datePicker",suppressHydrationWarning:!0,children:u.jsx("input",{ref:R,type:"date",value:c||"",onChange:m,onClick:g})}):null});we.displayName="DatePicker";function Y(c){const r=c.textContent||"",s=c.nextSibling;r.length?(s==null||s.setAttribute("style","display: none"),c.style.padding="2px 4px",c.style.borderTopLeftRadius="2px",c.style.borderBottomLeftRadius="2px",c.style.borderTopRightRadius="2px",c.style.borderBottomRightRadius="2px"):(s==null||s.setAttribute("style","display: inline"),c.style.padding="2px 0 2px 4px",c.style.borderTopLeftRadius="2px",c.style.borderBottomLeftRadius="2px",c.style.borderTopRightRadius="0",c.style.borderBottomRightRadius="0")}function M(c){if(c&&c.getAttribute("data-set-type")==="input"){const r=c.children[0];r.setAttribute("contenteditable","true"),requestAnimationFrame(()=>{r.focus()})}}function Ee(c){c&&c.getAttribute("data-set-type")==="input"&&c.children[0].setAttribute("contenteditable","false")}function j(c){return c&&c.getAttribute("data-set-type")==="input"}const Ce=i.forwardRef(({options:c,value:r,position:s,id:p,height:S=37,onChange:b},w)=>{const[k,R]=i.useState(r||""),m=i.useRef(null);i.useEffect(()=>{typeof w=="function"?w(m.current):w&&(w.current=m.current)},[w]);const T=d=>{const y=k.split(",").filter(D=>D)||[];let F="";y.includes(d)?F=y.filter(D=>D!==d).join(","):(console.log("handleOptionClick",y),y.length?F=`${y.join(",")},${d}`:F=d),R(F),b==null||b(F,"multiple-select")},g=k.split(",")||[];return u.jsx("div",{ref:m,className:"cosmic-ai-input-selectFloatElement",id:p,style:{position:"absolute",top:(s==null?void 0:s.top)||0,left:(s==null?void 0:s.left)||0,zIndex:5e3,maxHeight:`${S}px`},children:c.map((d,y)=>u.jsx("div",{className:`selectOption ${g.includes(d)?"selected":""}`,onClick:()=>T(d),children:d},y))})});Ce.displayName="MultipleSelect";const me=i.forwardRef(({options:c,value:r,position:s,id:p,height:S=120,onChange:b},w)=>{const[k,R]=i.useState(r||""),m=i.useRef(null);i.useEffect(()=>{typeof w=="function"?w(m.current):w&&(w.current=m.current)},[w]);const T=g=>{R(g),b==null||b(g,"select")};return u.jsx("div",{ref:m,className:"cosmic-ai-input-selectFloatElement",id:p,style:{position:"absolute",top:(s==null?void 0:s.top)||0,left:(s==null?void 0:s.left)||0,zIndex:5e3,maxHeight:`${S}px`},children:c.length>0&&c.map((g,d)=>u.jsx("div",{className:`selectOption ${k===g?"selected":""}`,onClick:()=>T(g),children:g},d))})});me.displayName="Select";function be(c){const r=c instanceof Element?c:document.getElementById(c);return r&&r.children[0].textContent||""}function ze(c,r){const s=c instanceof Element?c:document.getElementById(c);s&&(s.children[0].textContent=r)}function V(c){const r=document.createRange(),s=window.getSelection();if(!s){console.error("无法获取selection对象");return}if(!c||!(c instanceof Node)){console.error("element 不是有效的 DOM 节点");return}r.selectNodeContents(c),r.collapse(!0),s.removeAllRanges(),s.addRange(r)}function P(c){const r=document.createRange(),s=window.getSelection();if(!s){console.error("无法获取selection对象");return}if(!c||!(c instanceof Node)){console.error("element 不是有效的 DOM 节点");return}r.selectNodeContents(c),r.collapse(!1),s.removeAllRanges(),s.addRange(r)}function Z(c){const r=window.getSelection();if(!r||r.rangeCount===0)return 0;if(!c||!(c instanceof Node)){console.error("element 不是有效的 DOM 节点");return}const s=r.getRangeAt(0),p=s.cloneRange();return p.selectNodeContents(c),p.setEnd(s.startContainer,s.startOffset),p.toString().length}function ve(){const c=window.getSelection();if(!c||c.rangeCount===0)return null;const s=c.getRangeAt(0).startContainer;return s.nodeType===Node.TEXT_NODE?s.parentElement:s.nodeType===Node.ELEMENT_NODE?s:null}function Ue(){const c=window.getSelection();if(!c||c.rangeCount===0)return null;const r=c.getRangeAt(0),s=r.startContainer,p=r.startOffset;return s.nodeType===Node.TEXT_NODE?p>0?s:s.previousSibling:s.nodeType===Node.ELEMENT_NODE?p>0?s.childNodes[p-1]:s.previousSibling:null}function Xe(){const c=window.getSelection();if(!c||c.rangeCount===0)return null;const r=c.getRangeAt(0),s=r.startContainer,p=r.startOffset;return s.nodeType===Node.TEXT_NODE?p<s.length?s:s.nextSibling:s.nodeType===Node.ELEMENT_NODE?p<s.childNodes.length?s.childNodes[p]:s.nextSibling:null}function Ge(c){return c&&!c.getAttribute("data-set-type")}const Re=i.forwardRef((c,r)=>{const{value:s="",varList:p,placeholder:S="",maxLength:b,defaultRows:w=3,disabled:k=!1,defaultFocus:R=!1,onFocus:m,onBlur:T,onChange:g,onClick:d,onSend:y,onParse:F,onKeyDown:D,onKeyUp:O,onMaxLengthExceeded:K}=c,[Je,Qe]=i.useState(s||""),[W,_]=i.useState(""),[Ye,I]=i.useState(""),[z,H]=i.useState(!1),[v,U]=i.useState({top:0,left:0}),[ee,te]=i.useState([]),[X,ne]=i.useState(""),[L,le]=i.useState(null),[oe,se]=i.useState(120),[ce,re]=i.useState(R),Se=240,Ae=240,ie=240,ae=276,C=i.useRef(null),x=i.useRef(null),N=i.useRef(null),B=()=>{var l,n;let e=((n=(l=C.current)==null?void 0:l.innerText)==null?void 0:n.replace(/\u200B/g,""))||"";return e===`
3
3
  `&&(e=""),e},de=()=>{var l;let e=((l=C.current)==null?void 0:l.innerHTML)||"";return e==="<br>"&&(e=""),e},ke=e=>{const l=[];let n=0;const o=/\[([^\]]+)\]/g;let a;const f=`${Date.now()}-${Math.random().toString(36).slice(2,9)}`;let h=0;if(e===""||!e||e===`
4
4
  `)return[{type:"text",content:"​",id:`text-${h++}-${f}`}];const $=new Set((p||[]).map(E=>E.name));for(;(a=o.exec(e))!==null;){if(a.index>n){const A=e.substring(n,a.index).split(`
5
5
  `);A.forEach((q,We)=>{q&&l.push({type:"text",content:q,id:`text-${h++}-${f}`}),We<A.length-1&&l.push({type:"newline",id:`newline-${h++}-${f}`})})}const E=a[1];if($.has(E)){const t=p.find(A=>A.name===E);(t==null?void 0:t.type)==="input"?l.push({type:"input",name:t==null?void 0:t.name,content:(t==null?void 0:t.content)||"",placeholder:(t==null?void 0:t.placeholder)||E,id:`input-${h++}-${f}`}):(t==null?void 0:t.type)==="select"?l.push({type:"select",name:t==null?void 0:t.name,content:(t==null?void 0:t.content)||"",placeholder:(t==null?void 0:t.placeholder)||E,options:(t==null?void 0:t.options)||[],id:`select-${h++}-${f}`}):(t==null?void 0:t.type)==="multiple-select"?l.push({type:"multiple-select",name:t==null?void 0:t.name,content:(t==null?void 0:t.content)||"",placeholder:(t==null?void 0:t.placeholder)||E,options:(t==null?void 0:t.options)||[],id:`multiple-select-${h++}-${f}`}):(t==null?void 0:t.type)==="date-picker"&&l.push({type:"date-picker",name:t==null?void 0:t.name,content:(t==null?void 0:t.content)||"",placeholder:(t==null?void 0:t.placeholder)||E,id:`date-picker-${h++}-${f}`})}else l.push({type:"text",content:`[${E}]`,id:`text-${h++}-${f}`});n=o.lastIndex}if(n<e.length){const t=e.substring(n).split(`
6
- `);t.forEach((A,q)=>{A&&l.push({type:"text",content:A,id:`text-${h++}`}),q<t.length-1&&l.push({type:"newline",id:`newline-${h++}`})})}return l},G=()=>{var e;(e=C.current)==null||e.setAttribute("contenteditable","true"),x.current&&(P(x.current)&&x.current.children[0].setAttribute("contenteditable","false"),x.current=null),M(C.current)},Te=e=>{console.log("handleEditorClick",e),d==null||d(e)},De=e=>{console.log("handleEditorMouseDown",e),G()},ue=e=>{console.log("handleEditorInput",e),_(B()),I(de()),g==null||g(B())},Ne=e=>{var l,n;if(e.code==="Enter"&&!e.shiftKey){e.preventDefault();const o=B();if(console.log("onSend",o,"maxLength:",b,"length:",o.length),o.length===0||(console.log("containerRef.current?.innerHTML",(l=C.current)==null?void 0:l.innerHTML),(n=C.current)!=null&&n.innerHTML.match(/^<span>[\s\u200B]*(<br\s*\/?>[\s\u200B]*)+<\/span>$/)))return;b?o.length<=b?y==null||y(o):(console.log("maxLength exceeded",o,b),K==null||K(o,b)):y==null||y(o)}},Le=e=>{var n;console.log("handleEditorKeyDown",e),T==null||T(e);const l=ve();if(l&&Ge(l)){if(e.code==="ArrowLeft"){const o=l==null?void 0:l.previousElementSibling,a=Z(l);o&&a===0&&P(o)&&o.children[0]&&(x.current=o,j(o),requestAnimationFrame(()=>{M(o.children[0])}))}if(e.code==="ArrowRight"){const o=l==null?void 0:l.nextElementSibling,a=Z(l);o&&a===((n=l.textContent)==null?void 0:n.length)&&P(o)&&o.children[0]&&(x.current=o,j(o),requestAnimationFrame(()=>{V(o.children[0])}))}e.code==="Backspace"&&(l.innerHTML==="​"&&e.preventDefault(),l.innerHTML==="<br>"&&(e.preventDefault(),l.innerHTML="​"))}if(l&&l.getAttribute("data-set-type")==="container"){if(e.code==="ArrowLeft"){const o=Ue();P(o)&&o.children[0]&&(x.current=o,j(o),requestAnimationFrame(()=>{M(o.children[0])}))}if(e.code==="ArrowRight"){const o=Xe();P(o)&&o.children[0]&&(x.current=o,j(o),requestAnimationFrame(()=>{V(o.children[0])}))}}Ne(e)},$e=e=>{console.log("handleEditorKeyUp",e),O==null||O(e)},je=e=>{e.preventDefault();const n=window.getSelection().toString();if(!n)return;const o=n,a=n;e.clipboardData.setData("text/plain",o),e.clipboardData.setData("text/html",a)},Pe=e=>{m==null||m(e)},Me=(e,l)=>{l.stopPropagation(),console.log("handleInputClick",e)},Fe=(e,l)=>{var o;l.stopPropagation(),console.log("handleInputMouseDown",e,l),x.current&&Ee(x.current),(o=C.current)==null||o.setAttribute("contenteditable","false");const n=document.getElementById(e.id);x.current=n,requestAnimationFrame(()=>{j(n)})},Oe=(e,l)=>{var a,f,h,$,E;console.log("handleInputKeyDown",e,l),T==null||T(l),l.stopPropagation();const n=l.target,o=Z(n);if(l.code==="Backspace"&&o===0){const t=(a=n.parentElement)==null?void 0:a.previousElementSibling;console.log("previousElement",t),t&&(P(t)&&t.children[0]&&((f=t.children[0].textContent)!=null&&f.length)?(x.current=t,j(t),M(t.children[0]),requestAnimationFrame(()=>{Y(t.children[0])})):(G(),M(t)))}if(l.code==="ArrowRight"&&o===((h=n.textContent)==null?void 0:h.length)){const t=($=n.parentElement)==null?void 0:$.nextElementSibling;console.log("nextElement",t),t&&(P(t)&&t.children[0]&&((E=t.children[0].textContent)!=null&&E.length)?(x.current=t,j(t),V(t.children[0]),requestAnimationFrame(()=>{Y(t.children[0])})):(G(),V(t)))}},He=(e,l)=>{console.log("handleInputKeyUp",e,l),O==null||O(l);const n=l.target;Y(n)},Be=(e,l)=>{var o;l.stopPropagation(),console.log("handlePlaceholderMouseDown",e),x.current&&Ee(x.current),(o=C.current)==null||o.setAttribute("contenteditable","false");const n=document.getElementById(e.id);x.current=n,requestAnimationFrame(()=>{j(n)})},qe=(e,l)=>{console.log("handleInputChange",e,l),_(B()),I(de()),g==null||g(B())},Ve=(e,l)=>{console.log("handleTextSpanKeyDown 不生效",e,l)};i.useEffect(()=>{if(!z)return;const e=o=>{D.current&&!D.current.contains(o.target)&&H(!1)},l=o=>{D.current&&!D.current.contains(o.target)&&H(!1)},n=()=>{H(!1)};return document.addEventListener("mousedown",e),document.addEventListener("scroll",l,!0),window.addEventListener("resize",n),()=>{document.removeEventListener("mousedown",e),document.removeEventListener("scroll",l,!0),window.removeEventListener("resize",n)}},[z]);const J=(e,l)=>{if(console.log("handleFloatChange",e,l),L&&ze(L.id,e),l==="select"&&H(!1),l==="multiple-select"||l==="date-picker"){const n=document.getElementById(L.id);n&&n.children[0]&&n.children[1]&&(e.length>0?(n.children[0].setAttribute("style","display: inline"),n.children[0].setAttribute("contenteditable","false"),n.children[1].setAttribute("style","display: none"),n.children[1].setAttribute("contenteditable","true")):(n.children[0].setAttribute("style","display: none"),n.children[0].setAttribute("contenteditable","true"),n.children[1].setAttribute("style","display: inline"),n.children[1].setAttribute("contenteditable","true")))}ue({})},pe=(e,l)=>{console.log("handleSelectClick",e,l);const o=l.target.getBoundingClientRect();te(e.options),ne(e.value||be(e.id)),U({top:-999999,left:-999999999}),le(e),se(Ae),H(!0),requestAnimationFrame(()=>{if(D.current){console.log("currentFloatElementRef.current",D.current);const a=D.current.getBoundingClientRect();let f=o.left,h=o.top+o.height;window.innerWidth-o.right<Se&&o.width<a.width&&(f=o.right-a.width),o.top<a.height&&(h=o.top+o.height),U({top:h,left:f}),se(a.height)}})},Ke=(e,l)=>{console.log("handleDatePickerClick",e,l);const o=l.target.getBoundingClientRect();let a=o.left,f=o.top-ae;window.innerWidth-o.right<ie&&(a=a-ie+o.width),o.top<ae&&(f=o.top+o.height),te(e.options),ne(e.value||be(e.id)),U({top:f,left:a}),le(e),H(!0)},[fe,he]=i.useState([]),[Q,ge]=i.useState(!0),ye=i.useCallback(()=>{ge(!1);const e=ke(s),l=[];return e.forEach(n=>{var o,a,f,h,$,E;if(n.type==="text")l.push(u.jsx("span",{onKeyDown:t=>Ve(n,t),children:n.content},n.id));else if(n.type==="newline")l.push(u.jsx("br",{},n.id));else if(n.type==="input")l.push(u.jsxs("span",{id:n.id,"data-set-type":"input",contentEditable:!1,suppressContentEditableWarning:!0,onInput:t=>qe(n,t),onClick:t=>Me(n,t),onMouseDown:t=>Fe(n,t),"data-cosmic-ai-input-placeholder":n.placeholder,children:[u.jsx("span",{className:"cosmic-ai-input-inputContent",style:{padding:(o=n.content)!=null&&o.length?"2px 4px":"2px 0 2px 4px"},contentEditable:!1,onKeyDown:t=>Oe(n,t),onKeyUp:t=>He(n,t),children:n.content}),u.jsx("span",{contentEditable:!1,style:{display:(a=n.content)!=null&&a.length?"none":"inline"},className:"cosmic-ai-input-placeholder",onMouseDown:t=>Be(n,t),children:n.placeholder})]},n.id));else if(n.type==="select"){const t=n.content||((f=n.options)==null?void 0:f[0])||n.placeholder;l.push(u.jsx("span",{id:n.id,"data-set-type":"select",contentEditable:!1,suppressContentEditableWarning:!0,onClick:A=>pe(n,A),"data-cosmic-ai-input-placeholder":n.placeholder,children:u.jsx("span",{contentEditable:!1,children:t})},n.id))}else if(n.type==="multiple-select"){const t=n.content||((h=n.options)==null?void 0:h[0]);l.push(u.jsxs("span",{id:n.id,"data-set-type":"multiple-select",contentEditable:!1,suppressContentEditableWarning:!0,onClick:A=>pe(n,A),"data-cosmic-ai-input-placeholder":n.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:n.placeholder})]},n.id))}else n.type==="date-picker"&&l.push(u.jsxs("span",{id:n.id,"data-set-type":"date-picker",contentEditable:!1,suppressContentEditableWarning:!0,onClick:t=>Ke(n,t),"data-cosmic-ai-input-placeholder":n.placeholder,children:[u.jsx("span",{style:{display:($=n.content)!=null&&$.length?"inline":"none"},contentEditable:!1,children:n.content}),u.jsx("span",{style:{display:(E=n.content)!=null&&E.length?"none":"inline"},className:"cosmic-ai-input-datePickerPlaceholder",contentEditable:!1,children:n.placeholder})]},n.id))}),setTimeout(()=>{ge(!0)},0),console.log("重新渲染 ============================>","elements",l,"parsed",e,s),l},[s]);return i.useEffect(()=>{Q&&setTimeout(()=>{M(C.current)},0)},[Q]),i.useEffect(()=>{s!==W?(he(ye()),_(s),re(!0)):(s===""||!s)&&he(ye())},[s,W]),i.useEffect(()=>{if(!R&&!ce)return;const e=new MutationObserver(()=>{C.current&&document.contains(C.current)&&(requestAnimationFrame(()=>{M(C.current),re(!1)}),e.disconnect())});return C.current&&e.observe(document.body,{childList:!0,subtree:!0}),()=>e.disconnect()},[R,ce]),i.useImperativeHandle(r,()=>({getCurrentValue:B})),console.log("renderElements",fe),u.jsxs("div",{className:xe(`cosmic-ai-input-rows${w}`),children:[Q&&u.jsx("div",{"data-set-type":"container","data-cosmic-ai-input-placeholder":S,className:xe("cosmic-ai-input",`cosmic-ai-input-rows${w}`,{"is-disabled":k},{"is-empty":W.length===0}),ref:C,contentEditable:!0,suppressContentEditableWarning:!0,onClick:e=>Te(e),onFocus:e=>{Pe(e)},onBlur:e=>{N==null||N(e)},onMouseDown:e=>De(e),onKeyDown:e=>Le(e),onKeyUp:e=>$e(e),onInput:e=>ue(e),onCopy:e=>je(e),children:fe}),z&&_e.createPortal((L==null?void 0:L.type)==="multiple-select"?u.jsx(Ce,{ref:D,options:ee,value:X,position:v,height:oe,onChange:J},"multiple-select"):(L==null?void 0:L.type)==="date-picker"?u.jsx(we,{ref:D,value:X,position:v,onChange:J},"date-picker"):u.jsx(me,{ref:D,options:ee,value:X,position:v,height:oe,onChange:J},"select"),document.body)]})});Re.displayName="AiInput";exports.default=Re;
6
+ `);t.forEach((A,q)=>{A&&l.push({type:"text",content:A,id:`text-${h++}`}),q<t.length-1&&l.push({type:"newline",id:`newline-${h++}`})})}return l},G=()=>{var e;(e=C.current)==null||e.setAttribute("contenteditable","true"),x.current&&(j(x.current)&&x.current.children[0].setAttribute("contenteditable","false"),x.current=null),P(C.current)},De=e=>{console.log("handleEditorClick",e),d==null||d(e)},Ne=e=>{console.log("handleEditorMouseDown",e),G()},ue=e=>{console.log("handleEditorInput",e),_(B()),I(de()),g==null||g(B())},Te=e=>{var l,n;if(e.code==="Enter"&&!e.shiftKey){e.preventDefault();const o=B();if(console.log("onSend",o,"maxLength:",b,"length:",o.length),o.length===0||(console.log("containerRef.current?.innerHTML",(l=C.current)==null?void 0:l.innerHTML),(n=C.current)!=null&&n.innerHTML.match(/^<span>[\s\u200B]*(<br\s*\/?>[\s\u200B]*)+<\/span>$/)))return;b?o.length<=b?y==null||y(o):(console.log("maxLength exceeded",o,b),K==null||K(o,b)):y==null||y(o)}},Le=e=>{var n;console.log("handleEditorKeyDown",e),D==null||D(e);const l=ve();if(l&&Ge(l)){if(e.code==="ArrowLeft"){const o=l==null?void 0:l.previousElementSibling,a=Z(l);o&&a===0&&j(o)&&o.children[0]&&(x.current=o,M(o),requestAnimationFrame(()=>{P(o.children[0])}))}if(e.code==="ArrowRight"){const o=l==null?void 0:l.nextElementSibling,a=Z(l);o&&a===((n=l.textContent)==null?void 0:n.length)&&j(o)&&o.children[0]&&(x.current=o,M(o),requestAnimationFrame(()=>{V(o.children[0])}))}e.code==="Backspace"&&(l.innerHTML==="​"&&e.preventDefault(),l.innerHTML==="<br>"&&(e.preventDefault(),l.innerHTML="​"))}if(l&&l.getAttribute("data-set-type")==="container"){if(e.code==="ArrowLeft"){const o=Ue();j(o)&&o.children[0]&&(x.current=o,M(o),requestAnimationFrame(()=>{P(o.children[0])}))}if(e.code==="ArrowRight"){const o=Xe();j(o)&&o.children[0]&&(x.current=o,M(o),requestAnimationFrame(()=>{V(o.children[0])}))}}Te(e)},$e=e=>{console.log("handleEditorKeyUp",e),O==null||O(e)},Me=e=>{e.preventDefault();const n=window.getSelection().toString();if(!n)return;const o=n,a=n;e.clipboardData.setData("text/plain",o),e.clipboardData.setData("text/html",a)},je=e=>{m==null||m(e)},Pe=(e,l)=>{l.stopPropagation(),console.log("handleInputClick",e)},Fe=(e,l)=>{var o;l.stopPropagation(),console.log("handleInputMouseDown",e,l),x.current&&Ee(x.current),(o=C.current)==null||o.setAttribute("contenteditable","false");const n=document.getElementById(e.id);x.current=n,requestAnimationFrame(()=>{M(n)})},Oe=(e,l)=>{var a,f,h,$,E;console.log("handleInputKeyDown",e,l),D==null||D(l),l.stopPropagation();const n=l.target,o=Z(n);if(l.code==="Backspace"&&o===0){const t=(a=n.parentElement)==null?void 0:a.previousElementSibling;console.log("previousElement",t),t&&(j(t)&&t.children[0]&&((f=t.children[0].textContent)!=null&&f.length)?(x.current=t,M(t),P(t.children[0]),requestAnimationFrame(()=>{Y(t.children[0])})):(G(),P(t)))}if(l.code==="ArrowRight"&&o===((h=n.textContent)==null?void 0:h.length)){const t=($=n.parentElement)==null?void 0:$.nextElementSibling;console.log("nextElement",t),t&&(j(t)&&t.children[0]&&((E=t.children[0].textContent)!=null&&E.length)?(x.current=t,M(t),V(t.children[0]),requestAnimationFrame(()=>{Y(t.children[0])})):(G(),V(t)))}},He=(e,l)=>{console.log("handleInputKeyUp",e,l),O==null||O(l);const n=l.target;Y(n)},Be=(e,l)=>{var o;l.stopPropagation(),console.log("handlePlaceholderMouseDown",e),x.current&&Ee(x.current),(o=C.current)==null||o.setAttribute("contenteditable","false");const n=document.getElementById(e.id);x.current=n,requestAnimationFrame(()=>{M(n)})},qe=(e,l)=>{console.log("handleInputChange",e,l),_(B()),I(de()),g==null||g(B())},Ve=(e,l)=>{console.log("handleTextSpanKeyDown 不生效",e,l)};i.useEffect(()=>{if(!z)return;const e=o=>{N.current&&!N.current.contains(o.target)&&H(!1)},l=o=>{N.current&&!N.current.contains(o.target)&&H(!1)},n=()=>{H(!1)};return document.addEventListener("mousedown",e),document.addEventListener("scroll",l,!0),window.addEventListener("resize",n),()=>{document.removeEventListener("mousedown",e),document.removeEventListener("scroll",l,!0),window.removeEventListener("resize",n)}},[z]);const J=(e,l)=>{if(console.log("handleFloatChange",e,l),L&&ze(L.id,e),l==="select"&&H(!1),l==="multiple-select"||l==="date-picker"){const n=document.getElementById(L.id);n&&n.children[0]&&n.children[1]&&(e.length>0?(n.children[0].setAttribute("style","display: inline"),n.children[0].setAttribute("contenteditable","false"),n.children[1].setAttribute("style","display: none"),n.children[1].setAttribute("contenteditable","true")):(n.children[0].setAttribute("style","display: none"),n.children[0].setAttribute("contenteditable","true"),n.children[1].setAttribute("style","display: inline"),n.children[1].setAttribute("contenteditable","true")))}ue({})},pe=(e,l)=>{console.log("handleSelectClick",e,l);const o=l.target.getBoundingClientRect();te(e.options),ne(e.value||be(e.id)),U({top:-999999,left:-999999999}),le(e),se(Ae),H(!0),requestAnimationFrame(()=>{if(N.current){console.log("currentFloatElementRef.current",N.current);const a=N.current.getBoundingClientRect();let f=o.left,h=o.top+o.height;window.innerWidth-o.right<Se&&o.width<a.width&&(f=o.right-a.width),o.top<a.height&&(h=o.top+o.height),U({top:h,left:f}),se(a.height)}})},Ke=(e,l)=>{console.log("handleDatePickerClick",e,l);const o=l.target.getBoundingClientRect();let a=o.left,f=o.top-ae;window.innerWidth-o.right<ie&&(a=a-ie+o.width),o.top<ae&&(f=o.top+o.height),te(e.options),ne(e.value||be(e.id)),U({top:f,left:a}),le(e),H(!0)},[fe,he]=i.useState([]),[Q,ge]=i.useState(!0),ye=i.useCallback(()=>{ge(!1);const e=ke(s),l=[];return e.forEach(n=>{var o,a,f,h,$,E;if(n.type==="text")l.push(u.jsx("span",{onKeyDown:t=>Ve(n,t),children:n.content},n.id));else if(n.type==="newline")l.push(u.jsx("br",{},n.id));else if(n.type==="input")l.push(u.jsxs("span",{id:n.id,"data-set-type":"input",contentEditable:!1,suppressContentEditableWarning:!0,onInput:t=>qe(n,t),onClick:t=>Pe(n,t),onMouseDown:t=>Fe(n,t),"data-cosmic-ai-input-placeholder":n.placeholder,children:[u.jsx("span",{className:"cosmic-ai-input-inputContent",style:{padding:(o=n.content)!=null&&o.length?"2px 4px":"2px 0 2px 4px"},contentEditable:!1,onKeyDown:t=>Oe(n,t),onKeyUp:t=>He(n,t),children:n.content}),u.jsx("span",{contentEditable:!1,style:{display:(a=n.content)!=null&&a.length?"none":"inline"},className:"cosmic-ai-input-placeholder",onMouseDown:t=>Be(n,t),children:n.placeholder})]},n.id));else if(n.type==="select"){const t=n.content||((f=n.options)==null?void 0:f[0])||n.placeholder;l.push(u.jsx("span",{id:n.id,"data-set-type":"select",contentEditable:!1,suppressContentEditableWarning:!0,onClick:A=>pe(n,A),"data-cosmic-ai-input-placeholder":n.placeholder,children:u.jsx("span",{contentEditable:!1,children:t})},n.id))}else if(n.type==="multiple-select"){const t=n.content||((h=n.options)==null?void 0:h[0]);l.push(u.jsxs("span",{id:n.id,"data-set-type":"multiple-select",contentEditable:!1,suppressContentEditableWarning:!0,onClick:A=>pe(n,A),"data-cosmic-ai-input-placeholder":n.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:n.placeholder})]},n.id))}else n.type==="date-picker"&&l.push(u.jsxs("span",{id:n.id,"data-set-type":"date-picker",contentEditable:!1,suppressContentEditableWarning:!0,onClick:t=>Ke(n,t),"data-cosmic-ai-input-placeholder":n.placeholder,children:[u.jsx("span",{style:{display:($=n.content)!=null&&$.length?"inline":"none"},contentEditable:!1,children:n.content}),u.jsx("span",{style:{display:(E=n.content)!=null&&E.length?"none":"inline"},className:"cosmic-ai-input-datePickerPlaceholder",contentEditable:!1,children:n.placeholder})]},n.id))}),setTimeout(()=>{ge(!0)},0),console.log("重新渲染 ============================>","elements",l,"parsed",e,s),l},[s]);return i.useEffect(()=>{Q&&setTimeout(()=>{P(C.current)},0)},[Q]),i.useEffect(()=>{s!==W?(he(ye()),_(s),re(!0)):(s===""||!s)&&he(ye())},[s,W]),i.useEffect(()=>{if(!R&&!ce)return;const e=new MutationObserver(()=>{C.current&&document.contains(C.current)&&(requestAnimationFrame(()=>{P(C.current),re(!1)}),e.disconnect())});return C.current&&e.observe(document.body,{childList:!0,subtree:!0}),()=>e.disconnect()},[R,ce]),i.useImperativeHandle(r,()=>({getCurrentValue:B})),console.log("renderElements",fe),u.jsxs("div",{className:xe(`cosmic-ai-input-rows${w}`),children:[Q&&u.jsx("div",{"data-set-type":"container","data-cosmic-ai-input-placeholder":S,className:xe("cosmic-ai-input",`cosmic-ai-input-rows${w}`,{"is-disabled":k},{"is-empty":W.length===0}),ref:C,contentEditable:!0,suppressContentEditableWarning:!0,onClick:e=>De(e),onFocus:e=>{je(e)},onBlur:e=>{T==null||T(e)},onMouseDown:e=>Ne(e),onKeyDown:e=>Le(e),onKeyUp:e=>$e(e),onInput:e=>ue(e),onCopy:e=>Me(e),children:fe}),z&&_e.createPortal((L==null?void 0:L.type)==="multiple-select"?u.jsx(Ce,{ref:N,options:ee,value:X,position:v,height:oe,onChange:J},"multiple-select"):(L==null?void 0:L.type)==="date-picker"?u.jsx(we,{ref:N,value:X,position:v,onChange:J},"date-picker"):u.jsx(me,{ref:N,options:ee,value:X,position:v,height:oe,onChange:J},"select"),document.body)]})});Re.displayName="AiInput";exports.default=Re;
package/dist/index.es.js CHANGED
@@ -1,20 +1,20 @@
1
- // Generated at: 2025-12-30T11:50:42.162Z
2
- import { jsx as h, jsxs as _ } from "react/jsx-runtime";
3
- import { forwardRef as j, useState as w, useRef as O, useEffect as F, useCallback as Xe, useImperativeHandle as ve } from "react";
1
+ // Generated at: 2026-01-16T02:56:24.968Z
2
+ import { jsx as f, jsxs as _ } from "react/jsx-runtime";
3
+ import { forwardRef as j, useState as w, useRef as H, useEffect as P, useCallback as Xe, useImperativeHandle as ve } from "react";
4
4
  import Ce from "classnames";
5
5
  import { createPortal as Ge } from "react-dom";
6
6
  const ke = j(
7
7
  ({ value: s, position: r, id: c, onChange: d }, k) => {
8
- const [b, x] = w(!0), T = O(null), A = O(null);
9
- F(() => {
10
- typeof k == "function" ? k(T.current) : k && (k.current = T.current);
8
+ const [b, x] = w(!0), D = H(null), A = H(null);
9
+ P(() => {
10
+ typeof k == "function" ? k(D.current) : k && (k.current = D.current);
11
11
  }, [k]);
12
12
  const m = (a) => {
13
13
  console.log("DatePicker handleChange", a);
14
14
  const g = a.target.value;
15
15
  d == null || d(g, "date-picker");
16
16
  };
17
- F(() => {
17
+ P(() => {
18
18
  if (A.current)
19
19
  try {
20
20
  setTimeout(() => {
@@ -31,13 +31,13 @@ const ke = j(
31
31
  } catch (a) {
32
32
  console.log("点击后无法打开日期选择器", a);
33
33
  }
34
- }, f = (a) => {
34
+ }, h = (a) => {
35
35
  a.stopPropagation(), console.log("DatePicker handleClick", a);
36
36
  };
37
- return b ? /* @__PURE__ */ h(
37
+ return b ? /* @__PURE__ */ f(
38
38
  "div",
39
39
  {
40
- ref: T,
40
+ ref: D,
41
41
  id: c,
42
42
  style: {
43
43
  top: (r == null ? void 0 : r.top) || 0,
@@ -45,14 +45,14 @@ const ke = j(
45
45
  },
46
46
  className: "cosmic-ai-input-datePicker",
47
47
  suppressHydrationWarning: !0,
48
- children: /* @__PURE__ */ h(
48
+ children: /* @__PURE__ */ f(
49
49
  "input",
50
50
  {
51
51
  ref: A,
52
52
  type: "date",
53
53
  value: s || "",
54
54
  onChange: m,
55
- onClick: f
55
+ onClick: h
56
56
  }
57
57
  )
58
58
  }
@@ -64,7 +64,7 @@ function te(s) {
64
64
  const r = s.textContent || "", c = s.nextSibling;
65
65
  r.length ? (c == null || c.setAttribute("style", "display: none"), s.style.padding = "2px 4px", s.style.borderTopLeftRadius = "2px", s.style.borderBottomLeftRadius = "2px", s.style.borderTopRightRadius = "2px", s.style.borderBottomRightRadius = "2px") : (c == null || c.setAttribute("style", "display: inline"), s.style.padding = "2px 0 2px 4px", s.style.borderTopLeftRadius = "2px", s.style.borderBottomLeftRadius = "2px", s.style.borderTopRightRadius = "0", s.style.borderBottomRightRadius = "0");
66
66
  }
67
- function P(s) {
67
+ function M(s) {
68
68
  if (s && s.getAttribute("data-set-type") === "input") {
69
69
  const r = s.children[0];
70
70
  r.setAttribute("contenteditable", "true"), requestAnimationFrame(() => {
@@ -75,7 +75,7 @@ function P(s) {
75
75
  function me(s) {
76
76
  s && s.getAttribute("data-set-type") === "input" && s.children[0].setAttribute("contenteditable", "false");
77
77
  }
78
- function M(s) {
78
+ function F(s) {
79
79
  return s && s.getAttribute("data-set-type") === "input";
80
80
  }
81
81
  const Re = j(({
@@ -86,16 +86,16 @@ const Re = j(({
86
86
  height: k = 37,
87
87
  onChange: b
88
88
  }, x) => {
89
- const [T, A] = w(r || ""), m = O(null);
90
- F(() => {
89
+ const [D, A] = w(r || ""), m = H(null);
90
+ P(() => {
91
91
  typeof x == "function" ? x(m.current) : x && (x.current = m.current);
92
92
  }, [x]);
93
93
  const S = (a) => {
94
- const g = T.split(",").filter((D) => D) || [];
94
+ const g = D.split(",").filter((N) => N) || [];
95
95
  let B = "";
96
- g.includes(a) ? B = g.filter((D) => D !== a).join(",") : (console.log("handleOptionClick", g), g.length ? B = `${g.join(",")},${a}` : B = a), A(B), b == null || b(B, "multiple-select");
97
- }, f = T.split(",") || [];
98
- return /* @__PURE__ */ h(
96
+ g.includes(a) ? B = g.filter((N) => N !== a).join(",") : (console.log("handleOptionClick", g), g.length ? B = `${g.join(",")},${a}` : B = a), A(B), b == null || b(B, "multiple-select");
97
+ }, h = D.split(",") || [];
98
+ return /* @__PURE__ */ f(
99
99
  "div",
100
100
  {
101
101
  ref: m,
@@ -108,10 +108,10 @@ const Re = j(({
108
108
  zIndex: 5e3,
109
109
  maxHeight: `${k}px`
110
110
  },
111
- children: s.map((a, g) => /* @__PURE__ */ h(
111
+ children: s.map((a, g) => /* @__PURE__ */ f(
112
112
  "div",
113
113
  {
114
- className: `selectOption ${f.includes(a) ? "selected" : ""}`,
114
+ className: `selectOption ${h.includes(a) ? "selected" : ""}`,
115
115
  onClick: () => S(a),
116
116
  children: a
117
117
  },
@@ -121,7 +121,7 @@ const Re = j(({
121
121
  );
122
122
  });
123
123
  Re.displayName = "MultipleSelect";
124
- const Te = j(({
124
+ const De = j(({
125
125
  options: s,
126
126
  value: r,
127
127
  position: c,
@@ -129,14 +129,14 @@ const Te = j(({
129
129
  height: k = 120,
130
130
  onChange: b
131
131
  }, x) => {
132
- const [T, A] = w(r || ""), m = O(null);
133
- F(() => {
132
+ const [D, A] = w(r || ""), m = H(null);
133
+ P(() => {
134
134
  typeof x == "function" ? x(m.current) : x && (x.current = m.current);
135
135
  }, [x]);
136
- const S = (f) => {
137
- A(f), b == null || b(f, "select");
136
+ const S = (h) => {
137
+ A(h), b == null || b(h, "select");
138
138
  };
139
- return /* @__PURE__ */ h(
139
+ return /* @__PURE__ */ f(
140
140
  "div",
141
141
  {
142
142
  ref: m,
@@ -149,19 +149,19 @@ const Te = j(({
149
149
  zIndex: 5e3,
150
150
  maxHeight: `${k}px`
151
151
  },
152
- children: s.length > 0 && s.map((f, a) => /* @__PURE__ */ h(
152
+ children: s.length > 0 && s.map((h, a) => /* @__PURE__ */ f(
153
153
  "div",
154
154
  {
155
- className: `selectOption ${T === f ? "selected" : ""}`,
156
- onClick: () => S(f),
157
- children: f
155
+ className: `selectOption ${D === h ? "selected" : ""}`,
156
+ onClick: () => S(h),
157
+ children: h
158
158
  },
159
159
  a
160
160
  ))
161
161
  }
162
162
  );
163
163
  });
164
- Te.displayName = "Select";
164
+ De.displayName = "Select";
165
165
  function Ae(s) {
166
166
  const r = s instanceof Element ? s : document.getElementById(s);
167
167
  return r && r.children[0].textContent || "";
@@ -176,20 +176,32 @@ function z(s) {
176
176
  console.error("无法获取selection对象");
177
177
  return;
178
178
  }
179
+ if (!s || !(s instanceof Node)) {
180
+ console.error("element 不是有效的 DOM 节点");
181
+ return;
182
+ }
179
183
  r.selectNodeContents(s), r.collapse(!0), c.removeAllRanges(), c.addRange(r);
180
184
  }
181
- function H(s) {
185
+ function O(s) {
182
186
  const r = document.createRange(), c = window.getSelection();
183
187
  if (!c) {
184
188
  console.error("无法获取selection对象");
185
189
  return;
186
190
  }
191
+ if (!s || !(s instanceof Node)) {
192
+ console.error("element 不是有效的 DOM 节点");
193
+ return;
194
+ }
187
195
  r.selectNodeContents(s), r.collapse(!1), c.removeAllRanges(), c.addRange(r);
188
196
  }
189
197
  function ne(s) {
190
198
  const r = window.getSelection();
191
199
  if (!r || r.rangeCount === 0)
192
200
  return 0;
201
+ if (!s || !(s instanceof Node)) {
202
+ console.error("element 不是有效的 DOM 节点");
203
+ return;
204
+ }
193
205
  const c = r.getRangeAt(0), d = c.cloneRange();
194
206
  return d.selectNodeContents(s), d.setEnd(c.startContainer, c.startOffset), d.toString().length;
195
207
  }
@@ -202,15 +214,13 @@ function Qe() {
202
214
  }
203
215
  function Ye() {
204
216
  const s = window.getSelection();
205
- if (!s || s.rangeCount === 0)
206
- return null;
217
+ if (!s || s.rangeCount === 0) return null;
207
218
  const r = s.getRangeAt(0), c = r.startContainer, d = r.startOffset;
208
219
  return c.nodeType === Node.TEXT_NODE ? d > 0 ? c : c.previousSibling : c.nodeType === Node.ELEMENT_NODE ? d > 0 ? c.childNodes[d - 1] : c.previousSibling : null;
209
220
  }
210
221
  function Ze() {
211
222
  const s = window.getSelection();
212
- if (!s || s.rangeCount === 0)
213
- return null;
223
+ if (!s || s.rangeCount === 0) return null;
214
224
  const r = s.getRangeAt(0), c = r.startContainer, d = r.startOffset;
215
225
  return c.nodeType === Node.TEXT_NODE ? d < c.length ? c : c.nextSibling : c.nodeType === Node.ELEMENT_NODE ? d < c.childNodes.length ? c.childNodes[d] : c.nextSibling : null;
216
226
  }
@@ -218,12 +228,12 @@ function Ie(s) {
218
228
  return s && !s.getAttribute("data-set-type");
219
229
  }
220
230
  const et = j((s, r) => {
221
- const { value: c = "", varList: d, placeholder: k = "", maxLength: b, defaultRows: x = 3, disabled: T = !1, defaultFocus: A = !1, onFocus: m, onBlur: S, onChange: f, onClick: a, onSend: g, onParse: B, onKeyDown: D, onKeyUp: V, onMaxLengthExceeded: U } = s, [tt, nt] = w(c || ""), [X, v] = w(""), [lt, le] = w(""), [G, q] = w(!1), [J, Q] = w({ top: 0, left: 0 }), [oe, ce] = w([]), [Y, se] = w(""), [L, re] = w(null), [ie, ae] = w(120), [de, ue] = w(A), De = 240, Ne = 240, pe = 240, he = 276, C = O(null), y = O(null), N = O(null), K = () => {
231
+ const { value: c = "", varList: d, placeholder: k = "", maxLength: b, defaultRows: x = 3, disabled: D = !1, defaultFocus: A = !1, onFocus: m, onBlur: S, onChange: h, onClick: a, onSend: g, onParse: B, onKeyDown: N, onKeyUp: V, onMaxLengthExceeded: U } = s, [tt, nt] = w(c || ""), [X, v] = w(""), [lt, le] = w(""), [G, q] = w(!1), [J, Q] = w({ top: 0, left: 0 }), [oe, ce] = w([]), [Y, se] = w(""), [L, re] = w(null), [ie, ae] = w(120), [de, ue] = w(A), Ne = 240, Te = 240, pe = 240, fe = 276, C = H(null), y = H(null), T = H(null), K = () => {
222
232
  var l, n;
223
233
  let e = ((n = (l = C.current) == null ? void 0 : l.innerText) == null ? void 0 : n.replace(/\u200B/g, "")) || "";
224
234
  return e === `
225
235
  ` && (e = ""), e;
226
- }, fe = () => {
236
+ }, he = () => {
227
237
  var l;
228
238
  let e = ((l = C.current) == null ? void 0 : l.innerHTML) || "";
229
239
  return e === "<br>" && (e = ""), e;
@@ -312,14 +322,14 @@ const et = j((s, r) => {
312
322
  return l;
313
323
  }, Z = () => {
314
324
  var e;
315
- (e = C.current) == null || e.setAttribute("contenteditable", "true"), y.current && (M(y.current) && y.current.children[0].setAttribute("contenteditable", "false"), y.current = null), H(C.current);
325
+ (e = C.current) == null || e.setAttribute("contenteditable", "true"), y.current && (F(y.current) && y.current.children[0].setAttribute("contenteditable", "false"), y.current = null), O(C.current);
316
326
  }, Le = (e) => {
317
327
  console.log("handleEditorClick", e), a == null || a(e);
318
328
  }, $e = (e) => {
319
329
  console.log("handleEditorMouseDown", e), Z();
320
330
  }, ge = (e) => {
321
- console.log("handleEditorInput", e), v(K()), le(fe()), f == null || f(K());
322
- }, Pe = (e) => {
331
+ console.log("handleEditorInput", e), v(K()), le(he()), h == null || h(K());
332
+ }, Me = (e) => {
323
333
  var l, n;
324
334
  if (e.code === "Enter" && !e.shiftKey) {
325
335
  e.preventDefault();
@@ -328,20 +338,20 @@ const et = j((s, r) => {
328
338
  return;
329
339
  b ? o.length <= b ? g == null || g(o) : (console.log("maxLength exceeded", o, b), U == null || U(o, b)) : g == null || g(o);
330
340
  }
331
- }, Fe = (e) => {
341
+ }, Pe = (e) => {
332
342
  var n;
333
- console.log("handleEditorKeyDown", e), D == null || D(e);
343
+ console.log("handleEditorKeyDown", e), N == null || N(e);
334
344
  const l = Qe();
335
345
  if (l && Ie(l)) {
336
346
  if (e.code === "ArrowLeft") {
337
347
  const o = l == null ? void 0 : l.previousElementSibling, i = ne(l);
338
- o && i === 0 && M(o) && o.children[0] && (y.current = o, P(o), requestAnimationFrame(() => {
339
- H(o.children[0]);
348
+ o && i === 0 && F(o) && o.children[0] && (y.current = o, M(o), requestAnimationFrame(() => {
349
+ O(o.children[0]);
340
350
  }));
341
351
  }
342
352
  if (e.code === "ArrowRight") {
343
353
  const o = l == null ? void 0 : l.nextElementSibling, i = ne(l);
344
- o && i === ((n = l.textContent) == null ? void 0 : n.length) && M(o) && o.children[0] && (y.current = o, P(o), requestAnimationFrame(() => {
354
+ o && i === ((n = l.textContent) == null ? void 0 : n.length) && F(o) && o.children[0] && (y.current = o, M(o), requestAnimationFrame(() => {
345
355
  z(o.children[0]);
346
356
  }));
347
357
  }
@@ -350,27 +360,27 @@ const et = j((s, r) => {
350
360
  if (l && l.getAttribute("data-set-type") === "container") {
351
361
  if (e.code === "ArrowLeft") {
352
362
  const o = Ye();
353
- M(o) && o.children[0] && (y.current = o, P(o), requestAnimationFrame(() => {
354
- H(o.children[0]);
363
+ F(o) && o.children[0] && (y.current = o, M(o), requestAnimationFrame(() => {
364
+ O(o.children[0]);
355
365
  }));
356
366
  }
357
367
  if (e.code === "ArrowRight") {
358
368
  const o = Ze();
359
- M(o) && o.children[0] && (y.current = o, P(o), requestAnimationFrame(() => {
369
+ F(o) && o.children[0] && (y.current = o, M(o), requestAnimationFrame(() => {
360
370
  z(o.children[0]);
361
371
  }));
362
372
  }
363
373
  }
364
- Pe(e);
365
- }, Me = (e) => {
374
+ Me(e);
375
+ }, Fe = (e) => {
366
376
  console.log("handleEditorKeyUp", e), V == null || V(e);
367
- }, He = (e) => {
377
+ }, Oe = (e) => {
368
378
  e.preventDefault();
369
379
  const n = window.getSelection().toString();
370
380
  if (!n) return;
371
381
  const o = n, i = n;
372
382
  e.clipboardData.setData("text/plain", o), e.clipboardData.setData("text/html", i);
373
- }, Oe = (e) => {
383
+ }, He = (e) => {
374
384
  m == null || m(e);
375
385
  }, Be = (e, l) => {
376
386
  l.stopPropagation(), console.log("handleInputClick", e);
@@ -379,21 +389,21 @@ const et = j((s, r) => {
379
389
  l.stopPropagation(), console.log("handleInputMouseDown", e, l), y.current && me(y.current), (o = C.current) == null || o.setAttribute("contenteditable", "false");
380
390
  const n = document.getElementById(e.id);
381
391
  y.current = n, requestAnimationFrame(() => {
382
- P(n);
392
+ M(n);
383
393
  });
384
394
  }, qe = (e, l) => {
385
395
  var i, u, p, $, E;
386
- console.log("handleInputKeyDown", e, l), D == null || D(l), l.stopPropagation();
396
+ console.log("handleInputKeyDown", e, l), N == null || N(l), l.stopPropagation();
387
397
  const n = l.target, o = ne(n);
388
398
  if (l.code === "Backspace" && o === 0) {
389
399
  const t = (i = n.parentElement) == null ? void 0 : i.previousElementSibling;
390
- console.log("previousElement", t), t && (M(t) && t.children[0] && ((u = t.children[0].textContent) != null && u.length) ? (y.current = t, P(t), H(t.children[0]), requestAnimationFrame(() => {
400
+ console.log("previousElement", t), t && (F(t) && t.children[0] && ((u = t.children[0].textContent) != null && u.length) ? (y.current = t, M(t), O(t.children[0]), requestAnimationFrame(() => {
391
401
  te(t.children[0]);
392
- })) : (Z(), H(t)));
402
+ })) : (Z(), O(t)));
393
403
  }
394
404
  if (l.code === "ArrowRight" && o === ((p = n.textContent) == null ? void 0 : p.length)) {
395
405
  const t = ($ = n.parentElement) == null ? void 0 : $.nextElementSibling;
396
- console.log("nextElement", t), t && (M(t) && t.children[0] && ((E = t.children[0].textContent) != null && E.length) ? (y.current = t, P(t), z(t.children[0]), requestAnimationFrame(() => {
406
+ console.log("nextElement", t), t && (F(t) && t.children[0] && ((E = t.children[0].textContent) != null && E.length) ? (y.current = t, M(t), z(t.children[0]), requestAnimationFrame(() => {
397
407
  te(t.children[0]);
398
408
  })) : (Z(), z(t)));
399
409
  }
@@ -406,20 +416,20 @@ const et = j((s, r) => {
406
416
  l.stopPropagation(), console.log("handlePlaceholderMouseDown", e), y.current && me(y.current), (o = C.current) == null || o.setAttribute("contenteditable", "false");
407
417
  const n = document.getElementById(e.id);
408
418
  y.current = n, requestAnimationFrame(() => {
409
- P(n);
419
+ M(n);
410
420
  });
411
421
  }, _e = (e, l) => {
412
- console.log("handleInputChange", e, l), v(K()), le(fe()), f == null || f(K());
422
+ console.log("handleInputChange", e, l), v(K()), le(he()), h == null || h(K());
413
423
  }, ze = (e, l) => {
414
424
  console.log("handleTextSpanKeyDown 不生效", e, l);
415
425
  };
416
- F(() => {
426
+ P(() => {
417
427
  if (!G)
418
428
  return;
419
429
  const e = (o) => {
420
- N.current && !N.current.contains(o.target) && q(!1);
430
+ T.current && !T.current.contains(o.target) && q(!1);
421
431
  }, l = (o) => {
422
- N.current && !N.current.contains(o.target) && q(!1);
432
+ T.current && !T.current.contains(o.target) && q(!1);
423
433
  }, n = () => {
424
434
  q(!1);
425
435
  };
@@ -436,19 +446,19 @@ const et = j((s, r) => {
436
446
  }, ye = (e, l) => {
437
447
  console.log("handleSelectClick", e, l);
438
448
  const o = l.target.getBoundingClientRect();
439
- ce(e.options), se(e.value || Ae(e.id)), Q({ top: -999999, left: -999999999 }), re(e), ae(Ne), q(!0), requestAnimationFrame(() => {
440
- if (N.current) {
441
- console.log("currentFloatElementRef.current", N.current);
442
- const i = N.current.getBoundingClientRect();
449
+ ce(e.options), se(e.value || Ae(e.id)), Q({ top: -999999, left: -999999999 }), re(e), ae(Te), q(!0), requestAnimationFrame(() => {
450
+ if (T.current) {
451
+ console.log("currentFloatElementRef.current", T.current);
452
+ const i = T.current.getBoundingClientRect();
443
453
  let u = o.left, p = o.top + o.height;
444
- window.innerWidth - o.right < De && o.width < i.width && (u = o.right - i.width), o.top < i.height && (p = o.top + o.height), Q({ top: p, left: u }), ae(i.height);
454
+ window.innerWidth - o.right < Ne && o.width < i.width && (u = o.right - i.width), o.top < i.height && (p = o.top + o.height), Q({ top: p, left: u }), ae(i.height);
445
455
  }
446
456
  });
447
457
  }, je = (e, l) => {
448
458
  console.log("handleDatePickerClick", e, l);
449
459
  const o = l.target.getBoundingClientRect();
450
- let i = o.left, u = o.top - he;
451
- window.innerWidth - o.right < pe && (i = i - pe + o.width), o.top < he && (u = o.top + o.height), ce(e.options), se(e.value || Ae(e.id)), Q({ top: u, left: i }), re(e), q(!0);
460
+ let i = o.left, u = o.top - fe;
461
+ window.innerWidth - o.right < pe && (i = i - pe + o.width), o.top < fe && (u = o.top + o.height), ce(e.options), se(e.value || Ae(e.id)), Q({ top: u, left: i }), re(e), q(!0);
452
462
  }, [Ee, be] = w([]), [ee, xe] = w(!0), we = Xe(() => {
453
463
  xe(!1);
454
464
  const e = Se(c), l = [];
@@ -456,10 +466,10 @@ const et = j((s, r) => {
456
466
  var o, i, u, p, $, E;
457
467
  if (n.type === "text")
458
468
  l.push(
459
- /* @__PURE__ */ h("span", { onKeyDown: (t) => ze(n, t), children: n.content }, n.id)
469
+ /* @__PURE__ */ f("span", { onKeyDown: (t) => ze(n, t), children: n.content }, n.id)
460
470
  );
461
471
  else if (n.type === "newline")
462
- l.push(/* @__PURE__ */ h("br", {}, n.id));
472
+ l.push(/* @__PURE__ */ f("br", {}, n.id));
463
473
  else if (n.type === "input")
464
474
  l.push(
465
475
  /* @__PURE__ */ _(
@@ -474,8 +484,8 @@ const et = j((s, r) => {
474
484
  onMouseDown: (t) => Ve(n, t),
475
485
  "data-cosmic-ai-input-placeholder": n.placeholder,
476
486
  children: [
477
- /* @__PURE__ */ h("span", { className: "cosmic-ai-input-inputContent", style: { padding: (o = n.content) != null && o.length ? "2px 4px" : "2px 0 2px 4px" }, contentEditable: !1, onKeyDown: (t) => qe(n, t), onKeyUp: (t) => Ke(n, t), children: n.content }),
478
- /* @__PURE__ */ h("span", { contentEditable: !1, style: { display: (i = n.content) != null && i.length ? "none" : "inline" }, className: "cosmic-ai-input-placeholder", onMouseDown: (t) => We(n, t), children: n.placeholder })
487
+ /* @__PURE__ */ f("span", { className: "cosmic-ai-input-inputContent", style: { padding: (o = n.content) != null && o.length ? "2px 4px" : "2px 0 2px 4px" }, contentEditable: !1, onKeyDown: (t) => qe(n, t), onKeyUp: (t) => Ke(n, t), children: n.content }),
488
+ /* @__PURE__ */ f("span", { contentEditable: !1, style: { display: (i = n.content) != null && i.length ? "none" : "inline" }, className: "cosmic-ai-input-placeholder", onMouseDown: (t) => We(n, t), children: n.placeholder })
479
489
  ]
480
490
  },
481
491
  n.id
@@ -484,7 +494,7 @@ const et = j((s, r) => {
484
494
  else if (n.type === "select") {
485
495
  const t = n.content || ((u = n.options) == null ? void 0 : u[0]) || n.placeholder;
486
496
  l.push(
487
- /* @__PURE__ */ h(
497
+ /* @__PURE__ */ f(
488
498
  "span",
489
499
  {
490
500
  id: n.id,
@@ -493,7 +503,7 @@ const et = j((s, r) => {
493
503
  suppressContentEditableWarning: !0,
494
504
  onClick: (R) => ye(n, R),
495
505
  "data-cosmic-ai-input-placeholder": n.placeholder,
496
- children: /* @__PURE__ */ h("span", { contentEditable: !1, children: t })
506
+ children: /* @__PURE__ */ f("span", { contentEditable: !1, children: t })
497
507
  },
498
508
  n.id
499
509
  )
@@ -511,8 +521,8 @@ const et = j((s, r) => {
511
521
  onClick: (R) => ye(n, R),
512
522
  "data-cosmic-ai-input-placeholder": n.placeholder,
513
523
  children: [
514
- /* @__PURE__ */ h("span", { style: { display: t != null && t.length ? "inline" : "none" }, contentEditable: !1, children: t }),
515
- /* @__PURE__ */ h("span", { style: { display: t != null && t.length ? "none" : "inline" }, className: "cosmic-ai-input-selectPlaceholder", contentEditable: !1, children: n.placeholder })
524
+ /* @__PURE__ */ f("span", { style: { display: t != null && t.length ? "inline" : "none" }, contentEditable: !1, children: t }),
525
+ /* @__PURE__ */ f("span", { style: { display: t != null && t.length ? "none" : "inline" }, className: "cosmic-ai-input-selectPlaceholder", contentEditable: !1, children: n.placeholder })
516
526
  ]
517
527
  },
518
528
  n.id
@@ -529,8 +539,8 @@ const et = j((s, r) => {
529
539
  onClick: (t) => je(n, t),
530
540
  "data-cosmic-ai-input-placeholder": n.placeholder,
531
541
  children: [
532
- /* @__PURE__ */ h("span", { style: { display: ($ = n.content) != null && $.length ? "inline" : "none" }, contentEditable: !1, children: n.content }),
533
- /* @__PURE__ */ h("span", { style: { display: (E = n.content) != null && E.length ? "none" : "inline" }, className: "cosmic-ai-input-datePickerPlaceholder", contentEditable: !1, children: n.placeholder })
542
+ /* @__PURE__ */ f("span", { style: { display: ($ = n.content) != null && $.length ? "inline" : "none" }, contentEditable: !1, children: n.content }),
543
+ /* @__PURE__ */ f("span", { style: { display: (E = n.content) != null && E.length ? "none" : "inline" }, className: "cosmic-ai-input-datePickerPlaceholder", contentEditable: !1, children: n.placeholder })
534
544
  ]
535
545
  },
536
546
  n.id
@@ -540,17 +550,17 @@ const et = j((s, r) => {
540
550
  xe(!0);
541
551
  }, 0), console.log("重新渲染 ============================>", "elements", l, "parsed", e, c), l;
542
552
  }, [c]);
543
- return F(() => {
553
+ return P(() => {
544
554
  ee && setTimeout(() => {
545
- H(C.current);
555
+ O(C.current);
546
556
  }, 0);
547
- }, [ee]), F(() => {
557
+ }, [ee]), P(() => {
548
558
  c !== X ? (be(we()), v(c), ue(!0)) : (c === "" || !c) && be(we());
549
- }, [c, X]), F(() => {
559
+ }, [c, X]), P(() => {
550
560
  if (!A && !de) return;
551
561
  const e = new MutationObserver(() => {
552
562
  C.current && document.contains(C.current) && (requestAnimationFrame(() => {
553
- H(C.current), ue(!1);
563
+ O(C.current), ue(!1);
554
564
  }), e.disconnect());
555
565
  });
556
566
  return C.current && e.observe(document.body, { childList: !0, subtree: !0 }), () => e.disconnect();
@@ -558,35 +568,35 @@ const et = j((s, r) => {
558
568
  getCurrentValue: K
559
569
  })), console.log("renderElements", Ee), // 最外层的防止renderContainer为false的时候样式坍塌
560
570
  /* @__PURE__ */ _("div", { className: Ce(`cosmic-ai-input-rows${x}`), children: [
561
- ee && /* @__PURE__ */ h(
571
+ ee && /* @__PURE__ */ f(
562
572
  "div",
563
573
  {
564
574
  "data-set-type": "container",
565
575
  "data-cosmic-ai-input-placeholder": k,
566
- className: Ce("cosmic-ai-input", `cosmic-ai-input-rows${x}`, { "is-disabled": T }, { "is-empty": X.length === 0 }),
576
+ className: Ce("cosmic-ai-input", `cosmic-ai-input-rows${x}`, { "is-disabled": D }, { "is-empty": X.length === 0 }),
567
577
  ref: C,
568
578
  contentEditable: !0,
569
579
  suppressContentEditableWarning: !0,
570
580
  onClick: (e) => Le(e),
571
581
  onFocus: (e) => {
572
- Oe(e);
582
+ He(e);
573
583
  },
574
584
  onBlur: (e) => {
575
585
  S == null || S(e);
576
586
  },
577
587
  onMouseDown: (e) => $e(e),
578
- onKeyDown: (e) => Fe(e),
579
- onKeyUp: (e) => Me(e),
588
+ onKeyDown: (e) => Pe(e),
589
+ onKeyUp: (e) => Fe(e),
580
590
  onInput: (e) => ge(e),
581
- onCopy: (e) => He(e),
591
+ onCopy: (e) => Oe(e),
582
592
  children: Ee
583
593
  }
584
594
  ),
585
595
  G && Ge(
586
- (L == null ? void 0 : L.type) === "multiple-select" ? /* @__PURE__ */ h(
596
+ (L == null ? void 0 : L.type) === "multiple-select" ? /* @__PURE__ */ f(
587
597
  Re,
588
598
  {
589
- ref: N,
599
+ ref: T,
590
600
  options: oe,
591
601
  value: Y,
592
602
  position: J,
@@ -594,19 +604,19 @@ const et = j((s, r) => {
594
604
  onChange: I
595
605
  },
596
606
  "multiple-select"
597
- ) : (L == null ? void 0 : L.type) === "date-picker" ? /* @__PURE__ */ h(
607
+ ) : (L == null ? void 0 : L.type) === "date-picker" ? /* @__PURE__ */ f(
598
608
  ke,
599
609
  {
600
- ref: N,
610
+ ref: T,
601
611
  value: Y,
602
612
  position: J,
603
613
  onChange: I
604
614
  },
605
615
  "date-picker"
606
- ) : /* @__PURE__ */ h(
607
- Te,
616
+ ) : /* @__PURE__ */ f(
617
+ De,
608
618
  {
609
- ref: N,
619
+ ref: T,
610
620
  options: oe,
611
621
  value: Y,
612
622
  position: J,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cosmic-ai-input",
3
- "version": "1.0.4",
3
+ "version": "1.0.7",
4
4
  "description": "A powerful AI conversation input component for React with TypeScript support",
5
5
  "main": "./dist/index.cjs.js",
6
6
  "module": "./dist/index.es.js",