@magicx-eng/ai-autocomplete-react 0.6.7 → 0.6.8

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