@magicx-eng/ai-autocomplete-react 0.5.2 → 0.5.3
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 +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -994,5 +994,5 @@ import{forwardRef as zt,useCallback as Te,useEffect as at,useImperativeHandle as
|
|
|
994
994
|
opacity: 0.4;
|
|
995
995
|
cursor: default;
|
|
996
996
|
}
|
|
997
|
-
`,document.head.appendChild(e)}var Xe={submitButton:"SubmitButton-module_submitButton_otz7H"};import{jsx as Ee}from"react/jsx-runtime";function Je({disabled:e,onClick:d}){return Ee("button",{type:"button","data-aia-submit":"",className:Xe.submitButton,disabled:e,onClick:n=>{n.stopPropagation(),d()},"aria-label":"Submit",children:Ee("svg",{width:"18",height:"18",viewBox:"0 0 18 18",fill:"none",role:"img","aria-label":"Submit",children:Ee("path",{d:"M9 14V4M9 4L4 9M9 4L14 9",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})})})}import{AIAutocomplete as Et}from"@magicx-eng/ai-autocomplete-vanilla";import{useCallback as Ze,useEffect as ye,useRef as N,useState as Dt}from"react";var Rt={text:"",completedParams:[],suggestions:[],activeDropdownIndex:-1,newParamId:null,isLoading:!0,isReady:!1,error:null,segments:[],actionableSuggestions:[],filteredOptions:[],placeholderText:"",isDropdownOpen:!1,isActivePillSelected:!1,filterBase:0,filterInProgress:!1,pillTapped:!1,skipNextFetch:!1,lastRawQuery:"",isFocused:!1,editingParam:null,editingAnchor:null,editingTail:null,caretOffset:null,inSelectionAnimation:!1};function De({onSubmit:e,onError:d,optionOverrides:n,maskCompletedText:p,apiConfig:i,columns:t=2,dropdownTrigger:s,optionsPosition:l,closeDropdownOnBlur:f,showNonTappableOptions:x,onFocus:R,onBlur:I,value:k,completedParams:S,onChange:v,onParamsChange:C,source:z,setCursor:V}){let r=N(null),[w,T]=Dt(null),W=N(e);W.current=e;let m=N(d);m.current=d;let X=N(v);X.current=v;let B=N(C);B.current=C;let E=N(R);E.current=R;let G=N(I);G.current=I;let L=N(V);L.current=V,ye(()=>{if(typeof document>"u")return;let a=new Et(document.createElement("div"),{renderMode:"headless",apiConfig:i,optionOverrides:n,maskCompletedText:p,columns:t,dropdownTrigger:s,optionsPosition:l,closeDropdownOnBlur:f,showNonTappableOptions:x,source:z,value:k,completedParams:S,onSubmit:(...A)=>W.current?.(...A),onError:(...A)=>m.current?.(...A),onChange:(...A)=>X.current?.(...A),onParamsChange:(...A)=>B.current?.(...A),onFocus:()=>E.current?.(),onBlur:()=>G.current?.(),setCursor:A=>L.current?.(A)});r.current=a,T(a.getState());let P=a.subscribe(A=>T(A));return()=>{P(),a.destroy(),r.current===a&&(r.current=null)}},[]),ye(()=>{k!==void 0&&r.current?.setValue(k)},[k]),ye(()=>{S!==void 0&&r.current?.setCompletedParams(S)},[S]);let J=JSON.stringify(i??null),q=N(n),F=N(0);if(n!==q.current){let a=q.current,P=n,A=Object.keys(a??{}),ae=Object.keys(P??{});(A.length!==ae.length||ae.some(se=>!a?.[se]||P[se]!==a[se]))&&F.current++,q.current=n}ye(()=>{r.current?.update({apiConfig:i,optionOverrides:n,dropdownTrigger:s,optionsPosition:l,closeDropdownOnBlur:f,showNonTappableOptions:x})},[J,F.current,s,l,f,x]);let O=N(null);O.current===null&&(O.current={handleTextChange:a=>r.current?.handleTextChange(a),handleKeyDown:a=>{let P="nativeEvent"in a?a.nativeEvent:a;r.current?.handleKeyDown(P)},setFocused:a=>r.current?.setFocused(a),startEditingParam:a=>r.current?.startEditingParam(a),exitEditMode:()=>r.current?.exitEditMode(),handleCaretAfterInput:a=>r.current?.handleCaretAfterInput(a),handleCaretMove:a=>r.current?.handleCaretMove(a),replaceEditingRange:a=>r.current?.replaceEditingRange(a)??!1,setActivePill:a=>r.current?.setActivePill(a),removeLastParam:()=>r.current?.removeLastParam(),clearNewParamId:()=>r.current?.clearNewParamId(),reset:()=>r.current?.reset(),selectOption:a=>r.current?.selectOption(a),setActiveDropdownIndex:a=>r.current?.setActiveDropdownIndex(a),handleFocus:()=>r.current?.setFocused(!0),handleBlur:()=>r.current?.setFocused(!1)});let g=O.current,Y=Ze(a=>{let P=a.target.value,ae=P.length>0&&!a.nativeEvent?.isComposing&&P[0]!==P[0].toUpperCase()?P[0].toUpperCase()+P.slice(1):P;r.current?.handleTextChange(ae)},[]),ee=Ze(a=>{r.current?.handleKeyDown(a.nativeEvent)},[]),Z=r.current,c=w??Rt,o=k!==void 0?k:c.text,u=S!==void 0?S:c.completedParams,h=c.actionableSuggestions,_=h[0],b=Z?.listboxId??"",D=c.activeDropdownIndex>=0&&Z?`${b}-option-${c.activeDropdownIndex}`:void 0,M=c.editingParam,$=M?{type:M.suggestionType,text:M.suggestionPlaceholder,required:!0,options:M.options}:null,pe=$??_,ke=$?[$]:h,ue=!Z||c.isLoading&&!c.editingParam&&!c.inSelectionAnimation;return{completedParams:u,suggestionPills:h,setActivePill:g.setActivePill,removeLastParam:g.removeLastParam,segments:c.segments,newParamId:c.newParamId,clearNewParamId:g.clearNewParamId,suggestions:c.suggestions,activeIndex:c.activeDropdownIndex,isReady:c.isReady,isLoading:ue,isFocused:c.isFocused,isDropdownOpen:c.isDropdownOpen,isActivePillSelected:c.isActivePillSelected,placeholderText:c.placeholderText,listboxId:b,error:c.error,handleTextChange:g.handleTextChange,handleKeyDown:g.handleKeyDown,setFocused:g.setFocused,editingParam:M,editingAnchor:c.editingAnchor,caretOffset:c.caretOffset,startEditingParam:g.startEditingParam,exitEditMode:g.exitEditMode,handleCaretAfterInput:g.handleCaretAfterInput,handleCaretMove:g.handleCaretMove,replaceEditingRange:g.replaceEditingRange,inputProps:{value:o,placeholder:c.placeholderText||void 0,onChange:Y,onKeyDown:ee,onFocus:g.handleFocus,onBlur:g.handleBlur,role:"combobox","aria-expanded":c.isDropdownOpen,"aria-activedescendant":D,"aria-autocomplete":"list","aria-controls":b},reset:g.reset,dropdownProps:{suggestions:pe?[{...pe,options:c.filteredOptions}]:[],activeIndex:c.activeDropdownIndex,onSelect:g.selectOption,onHighlight:g.setActiveDropdownIndex,isOpen:c.isDropdownOpen,id:b,pills:ke,activeSelected:c.isActivePillSelected,onPillClick:g.setActivePill,isLoading:ue,isInputEmpty:o.trim().length===0,optionsPosition:l??"below"}}}import{extractPlainText as Ye,getCursorOffset as et,plainTextLength as Lt,renderEditableContent as Tt,setCursorOffset as tt}from"@magicx-eng/ai-autocomplete-vanilla";import{useCallback as K,useEffect as Re,useLayoutEffect as Le,useRef as ie}from"react";var _e;function Bt(){if(_e!==void 0)return _e;if(typeof document>"u")return!1;let e=document.createElement("div");return e.setAttribute("contenteditable","plaintext-only"),_e=e.contentEditable==="plaintext-only",_e}function ot(e){let{segments:d,newParamId:n,editingParam:p,editingAnchor:i,caretOffset:t,placeholderText:s,isFocused:l,isDropdownOpen:f,listboxId:x,activeDescendantId:R,autoFocus:I,handleTextChange:k,handleKeyDown:S,handleCaretAfterInput:v,handleCaretMove:C,startEditingParam:z,replaceEditingRange:V,setFocused:r}=e,w=ie(null),T=ie(!1),W=ie(""),m=ie(""),X=ie(null),B=ie(0);X.current=t,Re(()=>{if(!I)return;let o=w.current;o&&(document.activeElement===o?r(!0):o.focus())},[I,r]),Re(()=>{let o=w.current;if(!o)return;let u=o.ownerDocument??document,h=()=>{let _=u.getSelection();if(!_||_.rangeCount===0||!_.anchorNode||!o.contains(_.anchorNode))return;let b=_.anchorNode,$=(b.nodeType===Node.ELEMENT_NODE?b:b.parentElement)?.closest('strong[data-seg="completed"][data-param-id]')?.dataset.paramId??null;if($&&$!==p?.id){z($);return}performance.now()-B.current<50||C(et(o))};return u.addEventListener("selectionchange",h),()=>u.removeEventListener("selectionchange",h)},[p,z,C]),Le(()=>{let o=w.current;o&&Tt({input:o,segments:d,newParamId:n,editingParamId:p?.id??null,placeholderText:s??"",isFocused:l})},[d,n,p,s,l]),Le(()=>{let o=W.current,u=n??"";if(W.current=u,!u||u===o)return;let h=w.current;if(!h)return;h.focus();let _=X.current??Lt(h);tt(h,_)},[n]),Le(()=>{let o=m.current,u=p?.id??"";if(m.current=u,!u||u===o||i==null)return;let h=w.current;h&&tt(h,i)},[p,i]);let E=K(()=>{if(T.current)return;let o=w.current;if(!o)return;let u=Ye(o),_=u.length>0&&u[0]!==u[0].toUpperCase()?u[0].toUpperCase()+u.slice(1):u;k(_)},[k]),G=K(()=>{B.current=performance.now(),E();let o=w.current;o&&v(et(o))},[E,v]);Re(()=>{let o=w.current;if(!o)return;let u=h=>{let _=h,b=_.inputType;if(b==="insertParagraph"||b==="insertLineBreak"||b==="insertFromDrop"){h.preventDefault();return}if(b.startsWith("insert")||b.startsWith("delete")){let D=b.startsWith("delete")?"":_.data??"";V(D)&&h.preventDefault()}};return o.addEventListener("beforeinput",u),()=>o.removeEventListener("beforeinput",u)},[V]);let L=K(()=>{T.current=!0},[]),J=K(()=>{T.current=!1,E()},[E]),q=K(o=>{o.preventDefault();let u=w.current;if(!u)return;let h=(o.clipboardData.getData("text/plain")??"").replace(/\r?\n/g," ");if(!h)return;let _=u.ownerDocument??document,b=_.getSelection();if(!b||b.rangeCount===0)return;let D=b.getRangeAt(0);if(!u.contains(D.startContainer))return;D.deleteContents();let M=_.createTextNode(h);D.insertNode(M),D.setStartAfter(M),D.collapse(!0),b.removeAllRanges(),b.addRange(D),E()},[E]),F=K(o=>S(o),[S]),O=K(()=>r(!0),[r]),g=K(()=>r(!1),[r]),Y=K(()=>w.current?.focus(),[]),ee=K(()=>w.current?.blur(),[]),Z=K(()=>{let o=w.current;return o?Ye(o):""},[]),c=Bt()?"plaintext-only":"true";return{inputRef:w,editorProps:{ref:w,contentEditable:c,suppressContentEditableWarning:!0,tabIndex:0,role:"combobox","aria-autocomplete":"list","aria-haspopup":"listbox","aria-controls":x,"aria-expanded":f,"aria-activedescendant":R,spellCheck:!0,enterKeyHint:"send",onInput:G,onKeyDown:F,onCompositionStart:L,onCompositionEnd:J,onPaste:q,onFocus:O,onBlur:g},getPlainText:Z,focus:Y,blur:ee}}import{jsx as re,jsxs as Be}from"react/jsx-runtime";function Kt(e){return e!=="auto"?e:typeof window>"u"||window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"}var Wt=zt(function({onSubmit:d,onError:n,optionOverrides:p,maskCompletedText:i,className:t,apiConfig:s,columns:l,pillPlacement:f="inline",mode:x="auto",optionsPosition:R="below",animations:I=!0,dropdownTrigger:k,closeDropdownOnBlur:S,showNonTappableOptions:v,autoFocus:C=!0,onFocus:z,onBlur:V,value:r,completedParams:w,onChange:T,onParamsChange:W,submitButton:m},X){let B=ce(null),E=ce(null),G=ce(()=>{}),L=ce(null),J=ce(null);at(()=>{let y=B.current;if(y)return L.current?L.current.setMode(x):L.current=new Ht(y,x),()=>{L.current?.destroy(),L.current=null}},[x]);let q=Te(y=>{let j=J.current?.current;j&&(j.focus(),Nt(j,y))},[]),{completedParams:F,suggestionPills:O,setActivePill:g,segments:Y,newParamId:ee,clearNewParamId:Z,placeholderText:c,isFocused:o,isDropdownOpen:u,isActivePillSelected:h,isLoading:_,activeIndex:b,listboxId:D,handleTextChange:M,handleKeyDown:$,setFocused:pe,editingParam:ke,editingAnchor:ue,caretOffset:a,startEditingParam:P,handleCaretAfterInput:A,handleCaretMove:ae,replaceEditingRange:se,dropdownProps:nt,reset:me}=De({onSubmit:y=>G.current(y),onError:n,optionOverrides:p,maskCompletedText:i,apiConfig:s,columns:l,dropdownTrigger:k,optionsPosition:R,closeDropdownOnBlur:S,showNonTappableOptions:v,onFocus:z,onBlur:V,value:r,completedParams:w,onChange:T,onParamsChange:W,source:"full-sdk",setCursor:q});at(()=>{if(!ee)return;let y=window.setTimeout(()=>Z(),650);return()=>window.clearTimeout(y)},[ee,Z]);let it=b>=0?`${D}-option-${b}`:void 0,{inputRef:Ie,editorProps:rt,focus:ge,blur:ze,getPlainText:Fe}=ot({segments:Y,newParamId:ee,editingParam:ke,editingAnchor:ue,caretOffset:a,placeholderText:c,isFocused:o,isDropdownOpen:u,listboxId:D,activeDescendantId:it,autoFocus:C,handleTextChange:M,handleKeyDown:$,handleCaretAfterInput:A,handleCaretMove:ae,startEditingParam:P,replaceEditingRange:se,setFocused:pe});J.current=Ie,Ot(()=>{let y=E.current,j=Ie.current;if(!y||!j)return;let he=()=>{let Me=y.firstElementChild;if(!Me)return;let ct=Me.getBoundingClientRect(),pt=j.getBoundingClientRect();ct.top>=pt.bottom-2?y.setAttribute("data-aia-pill-wrapped",""):y.removeAttribute("data-aia-pill-wrapped")};he();let Oe=new ResizeObserver(he);return Oe.observe(j),()=>Oe.disconnect()},[Y,O.length,_,Ie]),Ft(X,()=>({focus:ge,blur:ze,reset:me,setMode:y=>L.current?.setMode(y)}),[ge,ze,me]);let fe=!!Y.length||F.length>0,Se=Te(()=>{if(!fe)return;let y=Fe(),{rawQuery:j,completedParams:he}=Mt(y,F);d({query:y.trim(),raw_query:j,completed_params:he}),me()},[fe,F,d,me,Fe]);G.current=Se;let st=Te(y=>{y.target?.closest("[data-aia-pill]")||ge()},[ge]),lt=f==="inline",dt=f==="dropdown";return Be("div",{ref:B,className:`magicx-aia ${te.container} ${t??""}`,"data-pill-placement":f,"data-options-position":R,"data-animations":I?"on":"off","data-mode":Kt(x),children:[re(Ce,{...nt,showPills:dt}),Be("div",{className:te.inputWrapper,onClick:st,children:[Be("div",{className:te.editorArea,"data-aia-editor":"",children:[re("div",{...rt,className:te.input,"data-aia-input":""}),lt&&(_||O.length>0)&&re("span",{ref:E,className:te.pillListContainer,"data-aia-pill-list-container":"",children:re(ve,{pills:O,activePillIndex:0,activeSelected:h,onSelectPill:g,loading:_})})]}),m===null?null:m===void 0?re(Je,{disabled:!fe,onClick:Se}):re("span",{"data-aia-submit":"",className:te.submitSlot,onClick:y=>{fe&&(y.stopPropagation(),Se())},children:m})]})]})});export{Wt as AIAutocomplete,Ce as AIAutocompleteDropdown,De as useAIAutocomplete};
|
|
997
|
+
`,document.head.appendChild(e)}var Xe={submitButton:"SubmitButton-module_submitButton_otz7H"};import{jsx as Ee}from"react/jsx-runtime";function Je({disabled:e,onClick:d}){return Ee("button",{type:"button","data-aia-submit":"",className:Xe.submitButton,disabled:e,onClick:n=>{n.stopPropagation(),d()},"aria-label":"Submit",children:Ee("svg",{width:"18",height:"18",viewBox:"0 0 18 18",fill:"none",role:"img","aria-label":"Submit",children:Ee("path",{d:"M9 14V4M9 4L4 9M9 4L14 9",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})})})}import{AIAutocomplete as Et}from"@magicx-eng/ai-autocomplete-vanilla";import{useCallback as Ze,useEffect as ye,useRef as N,useState as Dt}from"react";var Rt={text:"",completedParams:[],suggestions:[],activeDropdownIndex:-1,newParamId:null,isLoading:!0,isReady:!1,error:null,segments:[],actionableSuggestions:[],filteredOptions:[],placeholderText:"",isDropdownOpen:!1,isActivePillSelected:!1,filterBase:0,filterInProgress:!1,pillTapped:!1,skipNextFetch:!1,lastRawQuery:"",isFocused:!1,editingParam:null,editingAnchor:null,editingTail:null,caretOffset:null,inSelectionAnimation:!1};function De({onSubmit:e,onError:d,optionOverrides:n,maskCompletedText:p,apiConfig:i,columns:t=2,dropdownTrigger:s,optionsPosition:l,closeDropdownOnBlur:f,showNonTappableOptions:x,onFocus:R,onBlur:I,value:k,completedParams:S,onChange:v,onParamsChange:C,source:z,setCursor:V}){let r=N(null),[w,T]=Dt(null),W=N(e);W.current=e;let m=N(d);m.current=d;let X=N(v);X.current=v;let B=N(C);B.current=C;let E=N(R);E.current=R;let G=N(I);G.current=I;let L=N(V);L.current=V,ye(()=>{if(typeof document>"u")return;let a=new Et(document.createElement("div"),{renderMode:"headless",apiConfig:i,optionOverrides:n,maskCompletedText:p,columns:t,dropdownTrigger:s,optionsPosition:l,closeDropdownOnBlur:f,showNonTappableOptions:x,source:z,value:k,completedParams:S,onSubmit:(...A)=>W.current?.(...A),onError:(...A)=>m.current?.(...A),onChange:(...A)=>X.current?.(...A),onParamsChange:(...A)=>B.current?.(...A),onFocus:()=>E.current?.(),onBlur:()=>G.current?.(),setCursor:A=>L.current?.(A)});r.current=a,T(a.getState());let P=a.subscribe(A=>T(A));return()=>{P(),a.destroy(),r.current===a&&(r.current=null)}},[]),ye(()=>{k!==void 0&&r.current?.setValue(k)},[k]),ye(()=>{S!==void 0&&r.current?.setCompletedParams(S)},[S]);let J=JSON.stringify(i??null),q=N(n),F=N(0);if(n!==q.current){let a=q.current,P=n,A=Object.keys(a??{}),ae=Object.keys(P??{});(A.length!==ae.length||ae.some(se=>!a?.[se]||P[se]!==a[se]))&&F.current++,q.current=n}ye(()=>{r.current?.update({apiConfig:i,optionOverrides:n,dropdownTrigger:s,optionsPosition:l,closeDropdownOnBlur:f,showNonTappableOptions:x})},[J,F.current,s,l,f,x]);let O=N(null);O.current===null&&(O.current={handleTextChange:a=>r.current?.handleTextChange(a),handleKeyDown:a=>{let P="nativeEvent"in a?a.nativeEvent:a;r.current?.handleKeyDown(P)},setFocused:a=>r.current?.setFocused(a),startEditingParam:a=>r.current?.startEditingParam(a),exitEditMode:()=>r.current?.exitEditMode(),handleCaretAfterInput:a=>r.current?.handleCaretAfterInput(a),handleCaretMove:a=>r.current?.handleCaretMove(a),replaceEditingRange:a=>r.current?.replaceEditingRange(a)??!1,setActivePill:a=>r.current?.setActivePill(a),removeLastParam:()=>r.current?.removeLastParam(),clearNewParamId:()=>r.current?.clearNewParamId(),reset:()=>r.current?.reset(),selectOption:a=>r.current?.selectOption(a),setActiveDropdownIndex:a=>r.current?.setActiveDropdownIndex(a),handleFocus:()=>r.current?.setFocused(!0),handleBlur:()=>r.current?.setFocused(!1)});let g=O.current,Y=Ze(a=>{let P=a.target.value,ae=P.length>0&&!a.nativeEvent?.isComposing&&P[0]!==P[0].toUpperCase()?P[0].toUpperCase()+P.slice(1):P;r.current?.handleTextChange(ae)},[]),ee=Ze(a=>{r.current?.handleKeyDown(a.nativeEvent)},[]),Z=r.current,c=w??Rt,o=k!==void 0?k:c.text,u=S!==void 0?S:c.completedParams,h=c.actionableSuggestions,_=h[0],b=Z?.listboxId??"",D=c.activeDropdownIndex>=0&&Z?`${b}-option-${c.activeDropdownIndex}`:void 0,M=c.editingParam,$=M?{type:M.suggestionType,text:M.suggestionPlaceholder,required:!0,options:M.options}:null,pe=$??_,ke=$?[$]:h,ue=!Z||c.isLoading&&!c.editingParam&&!c.inSelectionAnimation;return{completedParams:u,suggestionPills:h,setActivePill:g.setActivePill,removeLastParam:g.removeLastParam,segments:c.segments,newParamId:c.newParamId,clearNewParamId:g.clearNewParamId,suggestions:c.suggestions,activeIndex:c.activeDropdownIndex,isReady:c.isReady,isLoading:ue,isFocused:c.isFocused,isDropdownOpen:c.isDropdownOpen,isActivePillSelected:c.isActivePillSelected,placeholderText:c.placeholderText,listboxId:b,error:c.error,handleTextChange:g.handleTextChange,handleKeyDown:g.handleKeyDown,setFocused:g.setFocused,editingParam:M,editingAnchor:c.editingAnchor,caretOffset:c.caretOffset,startEditingParam:g.startEditingParam,exitEditMode:g.exitEditMode,handleCaretAfterInput:g.handleCaretAfterInput,handleCaretMove:g.handleCaretMove,replaceEditingRange:g.replaceEditingRange,inputProps:{value:o,placeholder:c.placeholderText||void 0,onChange:Y,onKeyDown:ee,onFocus:g.handleFocus,onBlur:g.handleBlur,role:"combobox","aria-expanded":c.isDropdownOpen,"aria-activedescendant":D,"aria-autocomplete":"list","aria-controls":b},reset:g.reset,dropdownProps:{suggestions:pe?[{...pe,options:c.filteredOptions}]:[],activeIndex:c.activeDropdownIndex,onSelect:g.selectOption,onHighlight:g.setActiveDropdownIndex,isOpen:c.isDropdownOpen,id:b,pills:ke,activeSelected:c.isActivePillSelected,onPillClick:g.setActivePill,isLoading:ue,isInputEmpty:o.trim().length===0,optionsPosition:l??"below"}}}import{extractPlainText as Ye,getCursorOffset as et,plainTextLength as Lt,renderEditableContent as Tt,setCursorOffset as tt}from"@magicx-eng/ai-autocomplete-vanilla";import{useCallback as K,useEffect as Re,useLayoutEffect as Le,useRef as ie}from"react";var _e;function Bt(){if(_e!==void 0)return _e;if(typeof document>"u")return!1;let e=document.createElement("div");return e.setAttribute("contenteditable","plaintext-only"),_e=e.contentEditable==="plaintext-only",_e}function ot(e){let{segments:d,newParamId:n,editingParam:p,editingAnchor:i,caretOffset:t,placeholderText:s,isFocused:l,isDropdownOpen:f,listboxId:x,activeDescendantId:R,autoFocus:I,handleTextChange:k,handleKeyDown:S,handleCaretAfterInput:v,handleCaretMove:C,startEditingParam:z,replaceEditingRange:V,setFocused:r}=e,w=ie(null),T=ie(!1),W=ie(""),m=ie(""),X=ie(null),B=ie(0);X.current=t,Re(()=>{if(!I)return;let o=w.current;o&&(document.activeElement===o?r(!0):o.focus())},[I,r]),Re(()=>{let o=w.current;if(!o)return;let u=o.ownerDocument??document,h=()=>{let _=u.getSelection();if(!_||_.rangeCount===0||!_.anchorNode||!o.contains(_.anchorNode))return;let b=_.anchorNode,$=(b.nodeType===Node.ELEMENT_NODE?b:b.parentElement)?.closest('strong[data-seg="completed"][data-param-id]')?.dataset.paramId??null;if($&&$!==p?.id){z($);return}performance.now()-B.current<50||C(et(o))};return u.addEventListener("selectionchange",h),()=>u.removeEventListener("selectionchange",h)},[p,z,C]),Le(()=>{let o=w.current;o&&Tt({input:o,segments:d,newParamId:n,editingParamId:p?.id??null,placeholderText:s??"",isFocused:l})},[d,n,p,s,l]),Le(()=>{let o=W.current,u=n??"";if(W.current=u,!u||u===o)return;let h=w.current;if(!h)return;h.focus();let _=X.current??Lt(h);tt(h,_)},[n]),Le(()=>{let o=m.current,u=p?.id??"";if(m.current=u,!u||u===o||i==null)return;let h=w.current;h&&tt(h,i)},[p,i]);let E=K(()=>{if(T.current)return;let o=w.current;if(!o)return;let u=Ye(o),_=u.length>0&&u[0]!==u[0].toUpperCase()?u[0].toUpperCase()+u.slice(1):u;k(_)},[k]),G=K(()=>{B.current=performance.now(),E();let o=w.current;o&&v(et(o))},[E,v]);Re(()=>{let o=w.current;if(!o)return;let u=h=>{let _=h,b=_.inputType;if(b==="insertParagraph"||b==="insertLineBreak"||b==="insertFromDrop"){h.preventDefault();return}if(b.startsWith("insert")||b.startsWith("delete")){let D=b.startsWith("delete")?"":_.data??"";V(D)&&h.preventDefault()}};return o.addEventListener("beforeinput",u),()=>o.removeEventListener("beforeinput",u)},[V]);let L=K(()=>{T.current=!0},[]),J=K(()=>{T.current=!1,E()},[E]),q=K(o=>{o.preventDefault();let u=w.current;if(!u)return;let h=(o.clipboardData.getData("text/plain")??"").replace(/\r?\n/g," ");if(!h)return;let _=u.ownerDocument??document,b=_.getSelection();if(!b||b.rangeCount===0)return;let D=b.getRangeAt(0);if(!u.contains(D.startContainer))return;D.deleteContents();let M=_.createTextNode(h);D.insertNode(M),D.setStartAfter(M),D.collapse(!0),b.removeAllRanges(),b.addRange(D),E()},[E]),F=K(o=>S(o),[S]),O=K(()=>r(!0),[r]),g=K(()=>r(!1),[r]),Y=K(()=>w.current?.focus(),[]),ee=K(()=>w.current?.blur(),[]),Z=K(()=>{let o=w.current;return o?Ye(o):""},[]),c=Bt()?"plaintext-only":"true";return{inputRef:w,editorProps:{ref:w,contentEditable:c,suppressContentEditableWarning:!0,tabIndex:0,role:"combobox","aria-autocomplete":"list","aria-haspopup":"listbox","aria-controls":x,"aria-expanded":f,"aria-activedescendant":R,spellCheck:!0,enterKeyHint:"send",onInput:G,onKeyDown:F,onCompositionStart:L,onCompositionEnd:J,onPaste:q,onFocus:O,onBlur:g},getPlainText:Z,focus:Y,blur:ee}}import{jsx as re,jsxs as Be}from"react/jsx-runtime";function Kt(e){return e!=="auto"?e:typeof window>"u"||window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"}var Wt=zt(function({onSubmit:d,onError:n,optionOverrides:p,maskCompletedText:i,className:t,apiConfig:s,columns:l,pillPlacement:f="dropdown",mode:x="auto",optionsPosition:R="below",animations:I=!0,dropdownTrigger:k,closeDropdownOnBlur:S,showNonTappableOptions:v,autoFocus:C=!0,onFocus:z,onBlur:V,value:r,completedParams:w,onChange:T,onParamsChange:W,submitButton:m},X){let B=ce(null),E=ce(null),G=ce(()=>{}),L=ce(null),J=ce(null);at(()=>{let y=B.current;if(y)return L.current?L.current.setMode(x):L.current=new Ht(y,x),()=>{L.current?.destroy(),L.current=null}},[x]);let q=Te(y=>{let j=J.current?.current;j&&(j.focus(),Nt(j,y))},[]),{completedParams:F,suggestionPills:O,setActivePill:g,segments:Y,newParamId:ee,clearNewParamId:Z,placeholderText:c,isFocused:o,isDropdownOpen:u,isActivePillSelected:h,isLoading:_,activeIndex:b,listboxId:D,handleTextChange:M,handleKeyDown:$,setFocused:pe,editingParam:ke,editingAnchor:ue,caretOffset:a,startEditingParam:P,handleCaretAfterInput:A,handleCaretMove:ae,replaceEditingRange:se,dropdownProps:nt,reset:me}=De({onSubmit:y=>G.current(y),onError:n,optionOverrides:p,maskCompletedText:i,apiConfig:s,columns:l,dropdownTrigger:k,optionsPosition:R,closeDropdownOnBlur:S,showNonTappableOptions:v,onFocus:z,onBlur:V,value:r,completedParams:w,onChange:T,onParamsChange:W,source:"full-sdk",setCursor:q});at(()=>{if(!ee)return;let y=window.setTimeout(()=>Z(),650);return()=>window.clearTimeout(y)},[ee,Z]);let it=b>=0?`${D}-option-${b}`:void 0,{inputRef:Ie,editorProps:rt,focus:ge,blur:ze,getPlainText:Fe}=ot({segments:Y,newParamId:ee,editingParam:ke,editingAnchor:ue,caretOffset:a,placeholderText:c,isFocused:o,isDropdownOpen:u,listboxId:D,activeDescendantId:it,autoFocus:C,handleTextChange:M,handleKeyDown:$,handleCaretAfterInput:A,handleCaretMove:ae,startEditingParam:P,replaceEditingRange:se,setFocused:pe});J.current=Ie,Ot(()=>{let y=E.current,j=Ie.current;if(!y||!j)return;let he=()=>{let Me=y.firstElementChild;if(!Me)return;let ct=Me.getBoundingClientRect(),pt=j.getBoundingClientRect();ct.top>=pt.bottom-2?y.setAttribute("data-aia-pill-wrapped",""):y.removeAttribute("data-aia-pill-wrapped")};he();let Oe=new ResizeObserver(he);return Oe.observe(j),()=>Oe.disconnect()},[Y,O.length,_,Ie]),Ft(X,()=>({focus:ge,blur:ze,reset:me,setMode:y=>L.current?.setMode(y)}),[ge,ze,me]);let fe=!!Y.length||F.length>0,Se=Te(()=>{if(!fe)return;let y=Fe(),{rawQuery:j,completedParams:he}=Mt(y,F);d({query:y.trim(),raw_query:j,completed_params:he}),me()},[fe,F,d,me,Fe]);G.current=Se;let st=Te(y=>{y.target?.closest("[data-aia-pill]")||ge()},[ge]),lt=f==="inline",dt=f==="dropdown";return Be("div",{ref:B,className:`magicx-aia ${te.container} ${t??""}`,"data-pill-placement":f,"data-options-position":R,"data-animations":I?"on":"off","data-mode":Kt(x),children:[re(Ce,{...nt,showPills:dt}),Be("div",{className:te.inputWrapper,onClick:st,children:[Be("div",{className:te.editorArea,"data-aia-editor":"",children:[re("div",{...rt,className:te.input,"data-aia-input":""}),lt&&(_||O.length>0)&&re("span",{ref:E,className:te.pillListContainer,"data-aia-pill-list-container":"",children:re(ve,{pills:O,activePillIndex:0,activeSelected:h,onSelectPill:g,loading:_})})]}),m===null?null:m===void 0?re(Je,{disabled:!fe,onClick:Se}):re("span",{"data-aia-submit":"",className:te.submitSlot,onClick:y=>{fe&&(y.stopPropagation(),Se())},children:m})]})]})});export{Wt as AIAutocomplete,Ce as AIAutocompleteDropdown,De as useAIAutocomplete};
|
|
998
998
|
//# sourceMappingURL=index.mjs.map
|