@dori-ai/dori-side-widget 1.13.193 → 1.13.194
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/assets/{apply-CRDTMIhh.js → apply-Dp8rOad5.js} +1 -1
- package/dist/assets/apply-Dp8rOad5.js.br +0 -0
- package/dist/assets/{apply-CRDTMIhh.js.gz → apply-Dp8rOad5.js.gz} +0 -0
- package/dist/widget.js +2 -2
- package/dist/widget.js.br +0 -0
- package/dist/widget.js.gz +0 -0
- package/package.json +1 -1
- package/dist/assets/apply-CRDTMIhh.js.br +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
import{A as t,c as e}from"../widget.js";import{r as n,S as i,U as s}from"./store-font-B5B6CNk6.js";import{S as r}from"./anchors-B_5p2Z47.js";const o="Cancel",a="AI search",l="Search all products";function c(t,e){e.buffer+=t.replace(/\r\n?/g,"\n");const n=[];let i=e.buffer.indexOf("\n\n");for(;-1!==i;){const t=e.buffer.slice(0,i);e.buffer=e.buffer.slice(i+2);let s="message";const r=[];for(const e of t.split("\n"))e.startsWith("event:")?s=e.slice(6).trim():e.startsWith("data:")&&r.push(e.slice(5).replace(/^ /,""));r.length&&n.push({event:s,data:r.join("\n")}),i=e.buffer.indexOf("\n\n")}return n}const d=(t=window)=>t.Shopify?.routes?.root||"/";class p extends Error{constructor(t){super(`AI search unavailable (${t})`),this.status=t}}const h=e=>`${t}/ai-search/${encodeURIComponent(e)}`;async function u(t,e,n,i,s){const r=new URLSearchParams({q:e,mode:n});i.currency&&r.set("currency",i.currency),i.country&&r.set("country",i.country);const o=await fetch(`${h(t)}/suggest?${r}`,{signal:s});if(!o.ok)throw new p(o.status);return(await o.json()).products??[]}async function f(t,e,n){try{const i=e.endsWith("/")?e:`${e}/`,s=new URLSearchParams({q:t,"resources[type]":"collection","resources[limit]":"2"}),r=await fetch(`${i}search/suggest.json?${s}`,{signal:n});if(!r.ok)return[];const o=await r.json();return(o.resources?.results?.collections??[]).filter(t=>!!t.title&&!!t.url).map(t=>({title:t.title,url:t.url}))}catch{return[]}}const g=new Set(["results","answer","not_found","fallback"]),m="dori-ai-search",x='header, [role="banner"], [id*="header" i], [class*="header" i]';function y(t,e){if(null==t)return!1;try{const n=new URL(t||"/",e);return n.origin===e&&/\/search\/?$/.test(n.pathname)}catch{return!1}}const v=t=>String(t??"").replace(/[&<>"']/g,t=>({"&":"&","<":"<",">":">",'"':""","'":"'"}[t])),w=(t=!1,e=18)=>`<svg class="spark${t?" live":""}" width="${e}" height="${e}" viewBox="0 0 24 24" aria-hidden="true"><path class="s1" d="M10 4Q11.76 10.24 18 12Q11.76 13.76 10 20Q8.24 13.76 2 12Q8.24 10.24 10 4Z" fill="currentColor"/><path class="s2" d="M19 1.5Q19.77 4.23 22.5 5Q19.77 5.77 19 8.5Q18.23 5.77 15.5 5Q18.23 4.23 19 1.5Z" fill="currentColor"/></svg>`;function b(t,e,n){if(null==t||!Number.isFinite(t))return"";const i=(e||n.currency||"").split(" ")[0].toUpperCase(),s=Number.isInteger(t)?0:2;try{if(i)return new Intl.NumberFormat(n.locale||void 0,{style:"currency",currency:i,minimumFractionDigits:s,maximumFractionDigits:2}).format(t)}catch{}return`${t.toFixed(s)}${i?` ${i}`:""}`}function k(t,e){if(!t)return"#";if(/^https?:\/\//.test(t))return t;if(t.startsWith("//"))return"#";const n=t.startsWith("/")?t:`/${t}`,i=e.root.endsWith("/")?e.root:`${e.root}/`;return"/"===i?n:`${i.slice(0,-1)}${n}`}const $=(t,e)=>k(`/search?q=${encodeURIComponent(t)}&options%5Bprefix%5D=last`,e);function S(t,e){return t?/cdn\.shopify\.com|\/cdn\/shop\//.test(t)?`${t}${t.includes("?")?"&":"?"}width=${e}`:t:""}function q(t,e){const n=b(t.price,t.currency,e),i=t.price_unit?` / ${v(t.price_unit)}`:"",s=t.compare_at?` <s>${v(b(t.compare_at,t.currency,e))}</s>`:"";return`${v(n)}${i}${s}`}const A=(t,e)=>`<div class="grid">${t.map(t=>function(t,e){const n=t.image?`<img src="${v(S(t.image,400))}" alt="" loading="lazy">`:"";return`<a class="card" href="${v(k(t.url,e))}" data-id="${v(t.id)}"><div class="ph">${n}</div>`+(t.brand?`<div class="br">${v(t.brand)}</div>`:"")+`<div class="nm">${v(t.name)}</div><div class="pr">${q(t,e)}${t.sold_out?'<span class="so">Sold out</span>':""}</div></a>`}(t,e)).join("")}</div>`;function M(t){return`<a class="colrow" href="${v((e=t.url,e&&(/^https?:\/\//.test(e)||e.startsWith("/")&&!e.startsWith("//"))?e:"#"))}">${v(t.title)}</a>`;var e}const z=(t,e)=>`<div class="stp${e?" done":""}"><span class="ic">${e?'<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m5 12.5 4.5 4.5L19 7.5"/></svg>':""}</span><span class="tx">${v(t)}</span></div>`;function E(t,e,n){return null!=t.cursor?`<div class="loader">${e.loadingMore?'<span class="spin"></span>':""}</div>`:`<div class="end"><a class="more" href="${v($(e.asked,n))}">${l}</a></div>`}function L(t,e,n,i){return`<div class="empty"><h2>${v(t)}</h2><a class="more" href="${v($(e.asked,n))}">${l}</a></div>`+(i.length?`<div class="popular"><div class="sech">Popular right now</div>${A(i,n)}</div>`:"")}function j(t,e){return"typing"===t.phase?function(t,e){const n=t.q.trim();if(!n)return"";const i=t=>t===n||n.startsWith(t),s=i(t.collectionsFor)?t.collections.slice(0,2):[],r=i(t.suggestedFor)?t.suggestions.slice(0,4):[];return function(t){return`<button class="airow" data-act="enter">${w()}<span class="lab">${a}</span><span class="q">“${v(t)}”</span><span class="key"><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 5v7a3 3 0 0 1-3 3H6"/><path d="m10 11-4 4 4 4"/></svg></span></button>`}(n)+(s.length?`<div class="sech">Collections</div><div class="rows">${s.map(M).join("")}</div>`:"")+(r.length?`<div class="sech">Products</div><div class="rows">${r.map((t,n)=>function(t,e,n){const i=t.image?`<img src="${v(S(t.image,120))}" alt="" loading="lazy">`:'<span class="noimg"></span>';return`<a class="row" href="${v(k(t.url,e))}" data-id="${v(t.id)}" data-i="${n}">${i}<span class="meta"><span class="nm">${v(t.name)}</span><span class="pr">${q(t,e)}</span></span></a>`}(t,e,n)).join("")}</div>`:"")}(t,e):"working"!==t.phase&&t.result?function(t,e){const n=t.result;switch(n.type){case"results":{const r=n.chips.length?`<div class="chips">${n.chips.map(t=>`<button class="chip" data-drop="${v(t.key)}" aria-label="Remove">${v(function(t,e){if("price"===t.kind){const n=t.min?b(t.min,t.currency,e):"",i=t.max?b(t.max,t.currency,e):"";return n&&i?`${n} to ${i}`:i?`Under ${i}`:`From ${n}`}return t.value??""}(t,e))}<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.6" stroke-linecap="round" aria-hidden="true"><path d="M6 6l12 12M18 6 6 18"/></svg></button>`).join("")}</div>`:"",o=n.note?.currency||e.currency;return r+(n.note?`<div class="note">${v((i=b(n.note.under,o,e),s=null!=n.note.from?b(n.note.from,o,e):null,s?`Nothing under ${i}. Closest from ${s}`:`Nothing under ${i}`))}</div>`:"")+A(n.products,e)+E(n,t,e)}case"fallback":return n.products.length?A(n.products,e)+E(n,t,e):L(`Nothing found for “${t.asked}”`,t,e,[]);case"not_found":return L(`No ${n.noun} found`,t,e,n.popular);case"answer":{if("none"===n.kind)return L("No answer in the store's help pages",t,e,[]);const i=n.text&&"entry"!==n.kind?`<div class="ansh">${w(!1,13)}Answer</div><p class="anst">${v(n.text)}</p>`:"";return`<div class="ans">${i}<div class="src${i?"":" alone"}"><div class="sech">${"page"===n.entry.source?"From the store's pages":"From the store's FAQ"}</div><div class="fq">${v(n.entry.question)}</div><div class="fa">${v(n.entry.answer)}</div></div></div>`}}var i,s}(t,e):function(t){const e=`<div class="aih">${w(!0)}<span class="shim">${a}</span></div>`,n=t.plan?[z(t.plan.label,!0),z("faq"===t.plan.kind?"Checking the store's help pages":(r=t.plan.products,r?`Searching ${r.toLocaleString()} products`:"Searching the products"),!1)]:[z((s=t.asked,`Reading “${s}”`),!1)],i="faq"===t.plan?.kind?'<div class="ans"><span class="skl sk" style="width:92%"></span><span class="skl sk" style="width:70%"></span></div>':`<div class="grid">${Array.from({length:4},()=>'<div class="card skel"><div class="ph"></div><span class="skl sk" style="width:80%"></span><span class="skl sk" style="width:40%"></span></div>').join("")}</div>`;var s,r;return`<div class="aib">${e}<div class="stps">${n.join("")}</div></div>${i}`}(t)}const C="dori-ai-search-last";let F=null;class P{constructor(t){this.config=t,this.state=((t="")=>({q:t,phase:"typing",suggestions:[],collections:[],suggestedFor:"",collectionsFor:"",asked:"",rid:null,drop:[],plan:null,result:null,loadingMore:!1}))(),this.shopper=function(t=window){const e=t.Shopify;return{currency:e?.currency?.active||void 0,country:e?.country||void 0,language:e?.locale||document.documentElement.lang||void 0}}(),this.ctx={locale:navigator.language||"en",currency:this.shopper.currency||"",root:d()},this.host=document.createElement("div"),this.host.id=m,this.host.style.setProperty("display","block","important");const e=this.host.attachShadow({mode:"open"}),r=getComputedStyle(document.body).color||"#111",l=n(document.body);l&&this.host.style.setProperty(i,l),this.host.style.fontFamily=s,e.innerHTML=`<style>\n:host{all:initial}\n*{box-sizing:border-box}\n.wrap{position:fixed;inset:0;z-index:2147483647;color:var(--ink,#111);font-size:14px;line-height:1.35;-webkit-font-smoothing:antialiased}\n.scrim{position:absolute;inset:0;background:rgba(0,0,0,.32);animation:fadein .16s ease both}\n.panel{position:absolute;inset:0;background:#fff;display:flex;flex-direction:column;animation:fadein .16s ease both}\na{color:inherit;text-decoration:none}\nbutton{font:inherit;color:inherit}\n.sbar{display:flex;align-items:center;gap:10px;padding:10px 14px;border-bottom:1px solid var(--line)}\n.field{flex:1;display:flex;align-items:center;gap:8px;height:44px;border-radius:8px;background:#f5f5f5;padding:0 12px;border:1px solid #e6e6e6;min-width:0}\n.field svg{flex:none;color:#777}\n.field input{flex:1;min-width:0;border:0;outline:0;background:none;font:inherit;font-size:16px;color:var(--ink);-webkit-appearance:none;appearance:none}\n.field input::-webkit-search-cancel-button{display:none}\n.cancel{border:0;background:none;font-size:15px;cursor:pointer;padding:6px 2px}\n.cancel .x{display:none}\n.body{flex:1;overflow-y:auto;overscroll-behavior:contain;min-height:120px}\n.body:empty{min-height:0}\n.spark{flex:none;display:block}\n.spark.live .s1{transform-origin:10px 12px;animation:pulse 1.3s ease-in-out infinite}\n.spark.live .s2{transform-origin:19px 5px;animation:twinkle 1.3s ease-in-out infinite}\n@keyframes pulse{50%{transform:scale(.76)}}\n@keyframes twinkle{0%,100%{opacity:.2;transform:scale(.55)}50%{opacity:1;transform:scale(1)}}\n.airow{display:flex;align-items:center;gap:10px;width:calc(100% - 24px);margin:10px 12px 2px;padding:12px;border:0;border-radius:12px;background:var(--ai);cursor:pointer;text-align:left;transition:transform .12s ease}\n.airow:active{transform:scale(.975)}\n.airow .lab{font-weight:600;white-space:nowrap;font-size:15px}\n.airow .q{min-width:0;flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;color:#5c5c5c;font-size:15px}\n.airow .key{flex:none;display:inline-flex;align-items:center;justify-content:center;width:28px;height:24px;border-radius:7px;background:#fff;border:1px solid var(--line);color:#555}\n.sech{font-weight:600;font-size:11px;line-height:1;letter-spacing:.12em;text-transform:uppercase;color:#777;margin:16px 14px 6px}\n.rows{display:flex;flex-direction:column}\n.row{display:flex;align-items:center;gap:12px;padding:7px 14px}\n.row img,.row .noimg{width:46px;height:46px;object-fit:contain;background:#f6f6f6;border-radius:6px;flex:none}\n.row .meta{min-width:0;flex:1;display:flex;flex-direction:column}\n.row .nm{font-size:14px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}\n.row .pr{font-size:13px;color:#555;margin-top:2px}\n.colrow{display:block;padding:8px 14px;font-size:14px}\n.aib{padding:16px 16px 0}\n.aih{display:flex;align-items:center;gap:8px;font-weight:600;font-size:15.5px}\n.shim{background:linear-gradient(90deg,var(--ink) 0%,var(--ink) 38%,#c4c4c4 50%,var(--ink) 62%,var(--ink) 100%);background-size:260% 100%;-webkit-background-clip:text;background-clip:text;color:transparent;animation:shim 1.7s linear infinite}\n@keyframes shim{from{background-position:100% 0}to{background-position:-160% 0}}\n.stps{margin:10px 0 0 1px;display:flex;flex-direction:column;gap:8px}\n.stp{display:flex;align-items:center;gap:9px;font-size:14px;color:#6b6b6b;animation:fadein .24s ease both}\n.stp .ic{width:15px;height:15px;flex:none;border-radius:50%;border:1.7px solid #d2d2d2;border-top-color:var(--ink);animation:spin .8s linear infinite;display:flex;align-items:center;justify-content:center}\n.stp.done{color:var(--ink)}\n.stp.done .ic{border:0;animation:none}\n.stp .tx{min-width:0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}\n@keyframes spin{to{transform:rotate(360deg)}}\n.chips{display:flex;flex-wrap:wrap;gap:6px;padding:10px 14px 0}\n.chip{display:inline-flex;align-items:center;gap:7px;font-size:13px;border:1px solid var(--ink);border-radius:999px;padding:5px 11px;cursor:pointer;background:#fff}\n.note{margin:10px 14px 0;font-size:13px;color:#6b6b6b}\n.grid{display:grid;grid-template-columns:1fr 1fr;gap:18px 10px;padding:14px 14px 20px}\n.card{min-width:0;display:block}\n.card .ph{aspect-ratio:1/1;background:#f6f6f6;border-radius:6px;overflow:hidden}\n.card .ph img{width:100%;height:100%;object-fit:contain;display:block}\n.card .br{font-size:10.5px;font-weight:500;line-height:1.2;letter-spacing:.12em;text-transform:uppercase;color:#777;margin-top:8px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}\n.card .nm{font-size:13.5px;line-height:1.3;margin-top:3px;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden}\n.card .pr{font-size:13.5px;margin-top:4px}\n.card .pr s,.row .pr s{color:#8a8a8a;margin-left:4px}\n.card .so{font-size:11px;color:#a33;margin-left:6px}\n.card.skel .ph,.sk{background:linear-gradient(90deg,#f3f3f3,#e9e9e9,#f3f3f3);background-size:200% 100%;animation:sh 1.2s linear infinite}\n.skl{display:block;height:10px;border-radius:5px;margin-top:9px}\n@keyframes sh{to{background-position:-200% 0}}\n.ans{padding:18px 16px 24px}\n.ansh{display:flex;align-items:center;gap:6px;font-weight:600;font-size:11px;line-height:1;letter-spacing:.12em;text-transform:uppercase;color:#777}\n.anst{margin:10px 0 0;font-size:16.5px;line-height:1.5}\n.src{margin-top:20px;border-top:1px solid var(--line);padding-top:16px}\n.src.alone{margin-top:0;border-top:0;padding-top:0}\n.src .sech{margin:0 0 9px}\n.src .fq{font-weight:500;font-size:15px;line-height:1.3}\n.src .fa{margin-top:6px;font-size:13.5px;line-height:1.5;color:#555;white-space:pre-line}\n.empty{padding:36px 20px 20px;text-align:center}\n.empty h2{margin:0;font-weight:500;font-size:21px;line-height:1.25}\n.more{display:inline-block;margin-top:12px;font-size:13.5px;color:#6b6b6b;text-decoration:underline}\n.end{text-align:center;padding:0 14px 28px}\n.loader{display:flex;justify-content:center;padding:4px 0 28px;min-height:40px}\n.spin{width:18px;height:18px;border-radius:50%;border:2px solid #ddd;border-top-color:var(--ink);animation:spin .8s linear infinite}\n.popular{border-top:1px solid var(--line);margin:6px 14px 0;padding-top:14px}\n.popular .sech{margin:0 0 2px}\n.popular .grid{padding:12px 0 28px}\n@keyframes fadein{from{opacity:0}to{opacity:1}}\n@media (min-width:750px){\n .scrim{background:rgba(0,0,0,.18)}\n .panel{inset:auto;top:var(--top,16px);left:var(--left,16px);width:var(--w,min(760px,100vw - 32px));max-height:calc(100vh - var(--top,16px) - 16px);border-radius:12px;overflow:hidden;box-shadow:0 0 0 1px rgba(0,0,0,.06),0 14px 40px rgba(0,0,0,.16);animation:drop .14s ease-out both}\n .sbar{height:56px;padding:0 8px 0 16px;gap:6px;border-bottom:0}\n .field{height:56px;padding:0;gap:10px;border:0;border-radius:0;background:none}\n .body:not(:empty){border-top:1px solid var(--line)}\n .cancel{display:flex;align-items:center;justify-content:center;width:36px;height:36px;padding:0;border-radius:8px;color:#6b6b6b}\n .cancel:hover{background:#f2f2f2;color:var(--ink)}\n .cancel .t{display:none}\n .cancel .x{display:block}\n .grid{grid-template-columns:repeat(4,minmax(0,1fr))}\n .narrow .grid{grid-template-columns:repeat(3,minmax(0,1fr))}\n}\n@keyframes drop{from{opacity:0;transform:translateY(-4px)}to{opacity:1;transform:none}}\n@media (prefers-reduced-motion:reduce){*{animation:none!important;transition:none!important}}\n</style><div class="wrap" style="--ink:${r};--ai:color-mix(in srgb, ${r} 6%, #fff);--line:#eaeaea" hidden><div class="scrim"></div><div class="panel" role="dialog" aria-modal="true" aria-label="${a}"><form class="sbar" role="search"><label class="field"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><circle cx="11" cy="11" r="7"/><path d="m20 20-4-4"/></svg><input type="search" enterkeyhint="search" autocomplete="off" autocapitalize="off" spellcheck="false" placeholder="What are you looking for?"></label><button type="button" class="cancel" aria-label="${o}"><span class="t">${o}</span><svg class="x" width="18" height="18" viewBox="0 0 18 18" aria-hidden="true"><path d="M4.5 4.5l9 9M13.5 4.5l-9 9" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/></svg></button></form><div class="body"></div></div></div>`,this.input=e.querySelector("input"),this.body=e.querySelector(".body"),e.querySelector("form").addEventListener("submit",t=>{t.preventDefault(),this.search()}),this.input.addEventListener("input",()=>this.typed()),e.querySelector(".cancel").addEventListener("click",()=>this.close()),e.querySelector(".scrim").addEventListener("click",()=>this.close()),this.body.addEventListener("click",t=>this.clicked(t)),this.body.addEventListener("scroll",()=>this.scrolled(),{passive:!0}),window.addEventListener("popstate",this.onPop),window.addEventListener("resize",this.onResize,{passive:!0}),document.addEventListener("keydown",this.onKey,!0),document.documentElement.appendChild(this.host)}host;input;body;state;ctx;shopper;timers=[];suggestAbort=null;searchAbort=null;moreAbort=null;pausedFor="";isOpen=!1;pushed=!1;overflow="";started=0;anchor=null;get wrap(){return this.host.shadowRoot.querySelector(".wrap")}place(){const t=this.host.shadowRoot.querySelector(".panel");if(!window.matchMedia("(min-width: 750px)").matches)return t.removeAttribute("style"),void t.classList.remove("narrow");let e=this.anchor&&this.anchor.isConnected?this.anchor:null,n=e?.getBoundingClientRect();(!n||n.width<=0)&&(e=function(){for(const t of document.querySelectorAll(r)){if(t.closest(`#${m}`))continue;const e=t.getBoundingClientRect();if(e.width>0&&e.height>0&&e.top<window.innerHeight/3)return t}return null}(),n=e?.getBoundingClientRect());const i=function(t,e,n){const i=e-32;if(!t||t.width<=0||t.height<=0){const t=Math.min(760,i);return{top:16,left:Math.round((e-t)/2),width:t}}const s=t.width>=240,r=Math.round(Math.min(i,s?Math.max(t.width+16,560):760)),o=s?t.left-8:(e-r)/2,a=t.top+t.height/2-28;return{top:Math.round(Math.min(Math.max(a,8),Math.max(8,n-240))),left:Math.round(Math.min(Math.max(o,16),e-16-r)),width:r}}(n??null,window.innerWidth,window.innerHeight);t.style.setProperty("--top",`${i.top}px`),t.style.setProperty("--left",`${i.left}px`),t.style.setProperty("--w",`${i.width}px`),t.classList.toggle("narrow",i.width<860)}onResize=()=>{this.isOpen&&this.place()};open(t,n,i=null){if(!this.isOpen){this.isOpen=!0,this.anchor=i,this.wrap.hidden=!1,this.overflow=document.documentElement.style.overflow,document.documentElement.style.overflow="hidden",this.place();try{history.pushState({...history.state||{},doriAiSearch:!0},""),this.pushed=!0}catch{this.pushed=!1}e("ai_search_open",{run:n})}t&&t!==this.input.value&&(this.input.value=t,this.typed()),n||this.input.focus(),n&&t.trim()&&this.search()}restore(t){this.isOpen=!0;try{history.replaceState({...history.state||{},doriAiSearch:!1},""),history.pushState({...history.state||{},doriAiSearch:!0},""),this.pushed=!0}catch{this.pushed=!1}this.wrap.hidden=!1,this.overflow=document.documentElement.style.overflow,document.documentElement.style.overflow="hidden",this.anchor=null,this.place(),this.input.value=t.q,this.dispatch({type:"restore",q:t.q,rid:t.rid,result:t.result,drop:t.drop})}close(t=!1){this.isOpen&&(this.isOpen=!1,this.cancelAll(),this.wrap.hidden=!0,document.documentElement.style.overflow=this.overflow,this.pushed&&!t&&history.state?.doriAiSearch&&(this.pushed=!1,history.back()),this.pushed=!1)}destroy(){this.close(),window.removeEventListener("popstate",this.onPop),window.removeEventListener("resize",this.onResize),document.removeEventListener("keydown",this.onKey,!0),this.host.remove()}onKey=t=>{this.isOpen&&"Escape"===t.key&&(t.stopPropagation(),this.close())};onPop=()=>{this.isOpen&&!history.state?.doriAiSearch&&this.close(!0)};dispatch(t){this.state=function(t,e){switch(e.type){case"input":return{...t,q:e.q,phase:"typing",rid:null,plan:null,result:null,loadingMore:!1};case"suggested":return e.q!==t.q.trim()||"typing"!==t.phase?t:e.collections?{...t,suggestions:e.products,suggestedFor:e.q,collections:e.collections,collectionsFor:e.q}:{...t,suggestions:e.products,suggestedFor:e.q};case"enter":return{...t,phase:"working",asked:t.q.trim(),rid:e.rid,drop:e.drop??[],plan:null,result:null,loadingMore:!1};case"server":return e.rid!==t.rid||"working"!==t.phase?t:"plan"===e.event.type?{...t,plan:e.event}:g.has(e.event.type)?{...t,phase:"done",result:e.event}:t;case"failed":return e.rid!==t.rid||"working"!==t.phase?t:{...t,phase:"done",result:{type:"fallback",products:[],cursor:null}};case"loading_more":return e.rid!==t.rid?t:{...t,loadingMore:!0};case"more":{const n=t.result;if(e.rid!==t.rid||!n||"results"!==n.type&&"fallback"!==n.type)return t;const i=new Set(n.products.map(t=>t.id)),s=[...n.products,...e.products.filter(t=>!i.has(t.id))];return{...t,loadingMore:!1,result:{...n,products:s,cursor:e.cursor}}}case"restore":return{...t,q:e.q,asked:e.q,rid:e.rid,drop:e.drop,phase:"done",result:e.result,plan:null,loadingMore:!1}}}(this.state,t);const e=this.body.scrollTop;this.body.innerHTML=j(this.state,this.ctx),"done"===this.state.phase&&(this.body.scrollTop=e)}cancelAll(){this.timers.forEach(t=>clearTimeout(t)),this.timers=[];for(const t of[this.suggestAbort,this.searchAbort,this.moreAbort])t?.abort();this.suggestAbort=this.searchAbort=this.moreAbort=null}typed(){this.cancelAll(),this.body.scrollTop=0,this.dispatch({type:"input",q:this.input.value});const t=this.input.value.trim();if(!t)return;this.pausedFor="";const e=new AbortController;this.suggestAbort=e,this.timers.push(window.setTimeout(()=>{u(this.config.sharingId,t,"key",this.shopper,e.signal).then(e=>{this.pausedFor!==t&&this.dispatch({type:"suggested",q:t,products:e})}).catch(()=>{})},80)),this.timers.push(window.setTimeout(()=>{Promise.all([u(this.config.sharingId,t,"pause",this.shopper,e.signal),f(t,this.ctx.root,e.signal)]).then(([e,n])=>{this.pausedFor=t,this.dispatch({type:"suggested",q:t,products:e,collections:n})}).catch(()=>{})},350))}search(t=[]){const n=this.input.value.trim();if(!n)return;this.cancelAll();const i=function(){const t=globalThis.crypto;if(t?.randomUUID)return t.randomUUID();const e=Array.from({length:16},()=>Math.floor(256*Math.random()));e[6]=15&e[6]|64,e[8]=63&e[8]|128;const n=e.map(t=>t.toString(16).padStart(2,"0")).join("");return`${n.slice(0,8)}-${n.slice(8,12)}-${n.slice(12,16)}-${n.slice(16,20)}-${n.slice(20)}`}();this.started=performance.now(),this.dispatch({type:"enter",rid:i,drop:t}),this.input.blur(),this.body.scrollTop=0;const s=new AbortController;this.searchAbort=s,async function(t,e,n,i){const s=await fetch(`${h(t)}/search`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e),signal:i});if(!s.ok||!s.body)throw new p(s.status);const r=s.body.getReader(),o=new TextDecoder,a={buffer:""};for(;;){const{done:t,value:e}=await r.read(),i=t?o.decode()+"\n\n":o.decode(e,{stream:!0});for(const s of c(i,a))try{n(JSON.parse(s.data))}catch{}if(t)return}}(this.config.sharingId,{q:n,rid:i,drop:t,...this.shopper},t=>{this.dispatch({type:"server",rid:i,event:t}),"plan"!==t.type&&e("ai_search_enter",{outcome:t.type,ms:Math.round(performance.now()-this.started),count:"products"in t&&Array.isArray(t.products)?t.products.length:void 0})},s.signal).then(()=>this.dispatch({type:"failed",rid:i})).catch(t=>{if(!s.signal.aborted)return t instanceof p?(this.close(),void location.assign($(n,this.ctx))):void this.dispatch({type:"failed",rid:i})})}loadMore(){const t=this.state.result,e=this.state.rid;if(!e||this.state.loadingMore||!t||"results"!==t.type&&"fallback"!==t.type||null==t.cursor)return;this.dispatch({type:"loading_more",rid:e});const n=new AbortController;this.moreAbort=n,async function(t,e,n,i){const s=await fetch(`${h(t)}/more?${new URLSearchParams({rid:e,cursor:String(n)})}`,{signal:i});if(!s.ok)throw new p(s.status);return await s.json()}(this.config.sharingId,e,t.cursor,n.signal).then(t=>this.dispatch({type:"more",rid:e,products:t.products,cursor:t.cursor})).catch(()=>{n.signal.aborted||this.dispatch({type:"more",rid:e,products:[],cursor:null})})}scrolled(){const t=this.body;t.scrollTop+t.clientHeight>t.scrollHeight-500&&this.loadMore()}clicked(t){const n=t.target;if(n.closest("[data-act='enter']"))return t.preventDefault(),void this.search();const i=n.closest("[data-drop]");if(i)return t.preventDefault(),void this.search([...this.state.drop,i.dataset.drop]);const s=n.closest("a.card, a.row");s&&(e("ai_search_click",{phase:this.state.phase,outcome:this.state.result?.type,id:s.dataset.id}),this.save())}save(){const t=this.state;if("done"!==t.phase||!t.result||!t.rid)return;const e={q:t.asked,rid:t.rid,drop:t.drop,result:t.result,url:location.href,at:Date.now()};try{sessionStorage.setItem(C,JSON.stringify(e))}catch{}}}let N=null,R=null;function I(t,e){return!!t&&!0===t.feature_flags?.ai_search&&!e}function O(t,e){const n=window;if(!I(t,!0===n.Shopify?.designMode))return N?.(),N=null,R=null,F?.destroy(),void(F=null);R={sharingId:e},N||(N=function(t,e,n){const i=t=>{const i=t.target;if(!i||"FORM"!==i.tagName||!y(i.getAttribute("action"),n))return;t.preventDefault(),t.stopImmediatePropagation();const s=i.querySelector('input[name="q"]');e(s?.value??"",!0,s??i)},s=t=>{const i=t.target;i&&"INPUT"===i.tagName&&i.matches('input[type="search"], input[name="q"]')&&(i.form?y(i.form.getAttribute("action"),n):i.closest?.(x))&&(i.blur?.(),e(i.value??"",!1,i))},o=t=>{const i=t;if(i.defaultPrevented||0!==(i.button??0)||i.metaKey||i.ctrlKey||i.shiftKey||i.altKey)return;const s=t.target;if(!s?.closest)return;const o=s.closest(r);if(!o||"INPUT"===o.tagName)return;if(o.closest(`#${m}`))return;if("FORM"===o.tagName||s.closest("form"))return;if(!("A"===o.tagName?y(o.getAttribute("href"),n):o.closest(x)))return;t.preventDefault(),t.stopImmediatePropagation();const a="A"===o.tagName?function(t,e){if(!t)return"";try{return new URL(t,e).searchParams.get("q")??""}catch{return""}}(o.getAttribute("href"),n):"";e(a,!!a,o)};return t.addEventListener("submit",i,!0),t.addEventListener("focusin",s,!0),t.addEventListener("click",o,!0),()=>{t.removeEventListener("submit",i,!0),t.removeEventListener("focusin",s,!0),t.removeEventListener("click",o,!0)}}(document,(t,e,n)=>R&&function(t,e,n,i=null){F||(F=new P(R)),F.open(e,n,i)}(0,t,e,n),location.origin),function(t){let e=null;try{e=JSON.parse(sessionStorage.getItem(C)||"null")}catch{e=null}const n=history.state,i=n?.doriAiSearch?function(t,e,n){return!t||t.url!==e||n-t.at>18e5?null:t}(e,location.href,Date.now()):null;i&&(F||(F=new P(t)),F.restore(i))}(R))}export{O as applyAiSearch,I as wantsAiSearch};
|
|
1
|
+
import{A as t,c as e}from"../widget.js";import{r as n,S as i,U as s}from"./store-font-B5B6CNk6.js";import{S as r}from"./anchors-B_5p2Z47.js";const o="Cancel",a="AI search",l="Search all products";function c(t,e){e.buffer+=t.replace(/\r\n?/g,"\n");const n=[];let i=e.buffer.indexOf("\n\n");for(;-1!==i;){const t=e.buffer.slice(0,i);e.buffer=e.buffer.slice(i+2);let s="message";const r=[];for(const e of t.split("\n"))e.startsWith("event:")?s=e.slice(6).trim():e.startsWith("data:")&&r.push(e.slice(5).replace(/^ /,""));r.length&&n.push({event:s,data:r.join("\n")}),i=e.buffer.indexOf("\n\n")}return n}const d=(t=window)=>t.Shopify?.routes?.root||"/";class p extends Error{constructor(t){super(`AI search unavailable (${t})`),this.status=t}}const h=e=>`${t}/ai-search/${encodeURIComponent(e)}`;async function u(t,e,n,i,s){const r=new URLSearchParams({q:e,mode:n});i.currency&&r.set("currency",i.currency),i.country&&r.set("country",i.country);const o=await fetch(`${h(t)}/suggest?${r}`,{signal:s});if(!o.ok)throw new p(o.status);return(await o.json()).products??[]}async function f(t,e,n){try{const i=e.endsWith("/")?e:`${e}/`,s=new URLSearchParams({q:t,"resources[type]":"collection","resources[limit]":"2"}),r=await fetch(`${i}search/suggest.json?${s}`,{signal:n});if(!r.ok)return[];const o=await r.json();return(o.resources?.results?.collections??[]).filter(t=>!!t.title&&!!t.url).map(t=>({title:t.title,url:t.url}))}catch{return[]}}const g=new Set(["results","answer","not_found","fallback"]),m="dori-ai-search",x='header, [role="banner"], [id*="header" i], [class*="header" i]';function y(t,e){if(null==t)return!1;try{const n=new URL(t||"/",e);return n.origin===e&&/\/search\/?$/.test(n.pathname)}catch{return!1}}const v=t=>String(t??"").replace(/[&<>"']/g,t=>({"&":"&","<":"<",">":">",'"':""","'":"'"}[t])),w=(t=!1,e=18)=>`<svg class="spark${t?" live":""}" width="${e}" height="${e}" viewBox="0 0 24 24" aria-hidden="true"><path class="s1" d="M10 4Q11.76 10.24 18 12Q11.76 13.76 10 20Q8.24 13.76 2 12Q8.24 10.24 10 4Z" fill="currentColor"/><path class="s2" d="M19 1.5Q19.77 4.23 22.5 5Q19.77 5.77 19 8.5Q18.23 5.77 15.5 5Q18.23 4.23 19 1.5Z" fill="currentColor"/></svg>`;function b(t,e,n){if(null==t||!Number.isFinite(t))return"";const i=(e||n.currency||"").split(" ")[0].toUpperCase(),s=Number.isInteger(t)?0:2;try{if(i)return new Intl.NumberFormat(n.locale||void 0,{style:"currency",currency:i,minimumFractionDigits:s,maximumFractionDigits:2}).format(t)}catch{}return`${t.toFixed(s)}${i?` ${i}`:""}`}function k(t,e){if(!t)return"#";if(/^https?:\/\//.test(t))return t;if(t.startsWith("//"))return"#";const n=t.startsWith("/")?t:`/${t}`,i=e.root.endsWith("/")?e.root:`${e.root}/`;return"/"===i?n:`${i.slice(0,-1)}${n}`}const $=(t,e)=>k(`/search?q=${encodeURIComponent(t)}&options%5Bprefix%5D=last`,e);function S(t,e){return t?/cdn\.shopify\.com|\/cdn\/shop\//.test(t)?`${t}${t.includes("?")?"&":"?"}width=${e}`:t:""}function q(t,e){const n=b(t.price,t.currency,e),i=t.price_unit?` / ${v(t.price_unit)}`:"",s=t.compare_at?` <s>${v(b(t.compare_at,t.currency,e))}</s>`:"";return`${v(n)}${i}${s}`}const A=(t,e)=>`<div class="grid">${t.map(t=>function(t,e){const n=t.image?`<img src="${v(S(t.image,400))}" alt="" loading="lazy">`:"";return`<a class="card" href="${v(k(t.url,e))}" data-id="${v(t.id)}"><div class="ph">${n}</div>`+(t.brand?`<div class="br">${v(t.brand)}</div>`:"")+`<div class="nm">${v(t.name)}</div><div class="pr">${q(t,e)}${t.sold_out?'<span class="so">Sold out</span>':""}</div></a>`}(t,e)).join("")}</div>`;function M(t){return`<a class="colrow" href="${v((e=t.url,e&&(/^https?:\/\//.test(e)||e.startsWith("/")&&!e.startsWith("//"))?e:"#"))}">${v(t.title)}</a>`;var e}const z=(t,e)=>`<div class="stp${e?" done":""}"><span class="ic">${e?'<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m5 12.5 4.5 4.5L19 7.5"/></svg>':""}</span><span class="tx">${v(t)}</span></div>`;function E(t,e,n){return null!=t.cursor?`<div class="loader">${e.loadingMore?'<span class="spin"></span>':""}</div>`:`<div class="end"><a class="more" href="${v($(e.asked,n))}">${l}</a></div>`}function L(t,e,n,i){return`<div class="empty"><h2>${v(t)}</h2><a class="more" href="${v($(e.asked,n))}">${l}</a></div>`+(i.length?`<div class="popular"><div class="sech">Popular right now</div>${A(i,n)}</div>`:"")}function j(t,e){return"typing"===t.phase?function(t,e){const n=t.q.trim();if(!n)return"";const i=t=>t===n||n.startsWith(t),s=i(t.collectionsFor)?t.collections.slice(0,2):[],r=i(t.suggestedFor)?t.suggestions.slice(0,4):[];return function(t){return`<button class="airow" data-act="enter">${w()}<span class="lab">${a}</span><span class="q">“${v(t)}”</span><span class="key"><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 5v7a3 3 0 0 1-3 3H6"/><path d="m10 11-4 4 4 4"/></svg></span></button>`}(n)+(s.length?`<div class="sech">Collections</div><div class="rows">${s.map(M).join("")}</div>`:"")+(r.length?`<div class="sech">Products</div><div class="rows">${r.map((t,n)=>function(t,e,n){const i=t.image?`<img src="${v(S(t.image,120))}" alt="" loading="lazy">`:'<span class="noimg"></span>';return`<a class="row" href="${v(k(t.url,e))}" data-id="${v(t.id)}" data-i="${n}">${i}<span class="meta"><span class="nm">${v(t.name)}</span><span class="pr">${q(t,e)}</span></span></a>`}(t,e,n)).join("")}</div>`:"")}(t,e):"working"!==t.phase&&t.result?function(t,e){const n=t.result;switch(n.type){case"results":{const r=n.chips.length?`<div class="chips">${n.chips.map(t=>`<button class="chip" data-drop="${v(t.key)}" aria-label="Remove">${v(function(t,e){if("price"===t.kind){const n=t.min?b(t.min,t.currency,e):"",i=t.max?b(t.max,t.currency,e):"";return n&&i?`${n} to ${i}`:i?`Under ${i}`:`From ${n}`}return t.value??""}(t,e))}<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.6" stroke-linecap="round" aria-hidden="true"><path d="M6 6l12 12M18 6 6 18"/></svg></button>`).join("")}</div>`:"",o=n.note?.currency||e.currency;return r+(n.note?`<div class="note">${v((i=b(n.note.under,o,e),s=null!=n.note.from?b(n.note.from,o,e):null,s?`Nothing under ${i}. Closest from ${s}`:`Nothing under ${i}`))}</div>`:"")+A(n.products,e)+E(n,t,e)}case"fallback":return n.products.length?A(n.products,e)+E(n,t,e):L(`Nothing found for “${t.asked}”`,t,e,[]);case"not_found":return L(`No ${n.noun} found`,t,e,n.popular);case"answer":{if("none"===n.kind)return L("No answer in the store's help pages",t,e,[]);const i=n.text&&"entry"!==n.kind?`<div class="ansh">${w(!1,13)}Answer</div><p class="anst">${v(n.text)}</p>`:"";return`<div class="ans">${i}<div class="src${i?"":" alone"}"><div class="sech">${"page"===n.entry.source?"From the store's pages":"From the store's FAQ"}</div><div class="fq">${v(n.entry.question)}</div><div class="fa">${v(n.entry.answer)}</div></div></div>`}}var i,s}(t,e):function(t){const e=`<div class="aih">${w(!0)}<span class="shim">${a}</span></div>`,n=t.plan?[z(t.plan.label,!0),z("faq"===t.plan.kind?"Checking the store's help pages":(r=t.plan.products,r?`Searching ${r.toLocaleString()} products`:"Searching the products"),!1)]:[z((s=t.asked,`Reading “${s}”`),!1)],i="faq"===t.plan?.kind?'<div class="ans"><span class="skl sk" style="width:92%"></span><span class="skl sk" style="width:70%"></span></div>':`<div class="grid">${Array.from({length:4},()=>'<div class="card skel"><div class="ph"></div><span class="skl sk" style="width:80%"></span><span class="skl sk" style="width:40%"></span></div>').join("")}</div>`;var s,r;return`<div class="aib">${e}<div class="stps">${n.join("")}</div></div>${i}`}(t)}const C="dori-ai-search-last";let F=null;class P{constructor(t){this.config=t,this.state=((t="")=>({q:t,phase:"typing",suggestions:[],collections:[],suggestedFor:"",collectionsFor:"",asked:"",rid:null,drop:[],plan:null,result:null,loadingMore:!1}))(),this.shopper=function(t=window){const e=t.Shopify;return{currency:e?.currency?.active||void 0,country:e?.country||void 0,language:e?.locale||document.documentElement.lang||void 0}}(),this.ctx={locale:navigator.language||"en",currency:this.shopper.currency||"",root:d()},this.host=document.createElement("div"),this.host.id=m,this.host.style.setProperty("display","block","important");const e=this.host.attachShadow({mode:"open"}),r=getComputedStyle(document.body).color||"#111",l=n(document.body);l&&this.host.style.setProperty(i,l),this.host.style.fontFamily=s,e.innerHTML=`<style>\n:host{all:initial}\n*{box-sizing:border-box}\n.wrap{position:fixed;inset:0;z-index:2147483647;color:var(--ink,#111);font-size:14px;line-height:1.35;-webkit-font-smoothing:antialiased}\n.scrim{position:absolute;inset:0;background:rgba(0,0,0,.32);animation:fadein .16s ease both}\n.panel{position:absolute;inset:0;background:#fff;display:flex;flex-direction:column;animation:fadein .16s ease both}\na{color:inherit;text-decoration:none}\nbutton{font:inherit;color:inherit}\n.sbar{display:flex;align-items:center;gap:10px;padding:10px 14px;border-bottom:1px solid var(--line)}\n.field{flex:1;display:flex;align-items:center;gap:8px;height:44px;border-radius:8px;background:#f5f5f5;padding:0 12px;border:1px solid #e6e6e6;min-width:0}\n.field svg{flex:none;color:#777}\n.field input{flex:1;min-width:0;border:0;outline:0;background:none;font:inherit;font-size:16px;color:var(--ink);-webkit-appearance:none;appearance:none}\n.field input::-webkit-search-cancel-button{display:none}\n.cancel{border:0;background:none;font-size:15px;cursor:pointer;padding:6px 2px}\n.cancel .x{display:none}\n.body{flex:1;overflow-y:auto;overscroll-behavior:contain;min-height:120px}\n.body:empty{min-height:0}\n.spark{flex:none;display:block}\n.spark.live .s1{transform-origin:10px 12px;animation:pulse 1.3s ease-in-out infinite}\n.spark.live .s2{transform-origin:19px 5px;animation:twinkle 1.3s ease-in-out infinite}\n@keyframes pulse{50%{transform:scale(.76)}}\n@keyframes twinkle{0%,100%{opacity:.2;transform:scale(.55)}50%{opacity:1;transform:scale(1)}}\n.airow{display:flex;align-items:center;gap:10px;width:calc(100% - 24px);margin:10px 12px 2px;padding:12px;border:0;border-radius:12px;background:var(--ai);cursor:pointer;text-align:left;transition:transform .12s ease}\n.airow:active{transform:scale(.975)}\n.airow .lab{font-weight:600;white-space:nowrap;font-size:15px}\n.airow .q{min-width:0;flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;color:#5c5c5c;font-size:15px}\n.airow .key{flex:none;display:inline-flex;align-items:center;justify-content:center;width:28px;height:24px;border-radius:7px;background:#fff;border:1px solid var(--line);color:#555}\n.sech{font-weight:600;font-size:11px;line-height:1;letter-spacing:.12em;text-transform:uppercase;color:#777;margin:16px 14px 6px}\n.rows{display:flex;flex-direction:column}\n.row{display:flex;align-items:center;gap:12px;padding:7px 14px}\n.row img,.row .noimg{width:46px;height:46px;object-fit:contain;background:#f6f6f6;border-radius:6px;flex:none}\n.row .meta{min-width:0;flex:1;display:flex;flex-direction:column}\n.row .nm{font-size:14px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}\n.row .pr{font-size:13px;color:#555;margin-top:2px}\n.colrow{display:block;padding:8px 14px;font-size:14px}\n.aib{padding:16px 16px 0}\n.aih{display:flex;align-items:center;gap:8px;font-weight:600;font-size:15.5px}\n.shim{background:linear-gradient(90deg,var(--ink) 0%,var(--ink) 38%,#c4c4c4 50%,var(--ink) 62%,var(--ink) 100%);background-size:260% 100%;-webkit-background-clip:text;background-clip:text;color:transparent;animation:shim 1.7s linear infinite}\n@keyframes shim{from{background-position:100% 0}to{background-position:-160% 0}}\n.stps{margin:10px 0 0 1px;display:flex;flex-direction:column;gap:8px}\n.stp{display:flex;align-items:center;gap:9px;font-size:14px;color:#6b6b6b;animation:fadein .24s ease both}\n.stp .ic{width:15px;height:15px;flex:none;border-radius:50%;border:1.7px solid #d2d2d2;border-top-color:var(--ink);animation:spin .8s linear infinite;display:flex;align-items:center;justify-content:center}\n.stp.done{color:var(--ink)}\n.stp.done .ic{border:0;animation:none}\n.stp .tx{min-width:0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}\n@keyframes spin{to{transform:rotate(360deg)}}\n.chips{display:flex;flex-wrap:wrap;gap:6px;padding:10px 14px 0}\n.chip{display:inline-flex;align-items:center;gap:7px;font-size:13px;border:1px solid var(--ink);border-radius:999px;padding:5px 11px;cursor:pointer;background:#fff}\n.note{margin:10px 14px 0;font-size:13px;color:#6b6b6b}\n.grid{display:grid;grid-template-columns:1fr 1fr;gap:18px 10px;padding:14px 14px 20px}\n.card{min-width:0;display:block}\n.card .ph{aspect-ratio:1/1;background:#f6f6f6;border-radius:6px;overflow:hidden}\n.card .ph img{width:100%;height:100%;object-fit:contain;display:block}\n.card .br{font-size:10.5px;font-weight:500;line-height:1.2;letter-spacing:.12em;text-transform:uppercase;color:#777;margin-top:8px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}\n.card .nm{font-size:13.5px;line-height:1.3;margin-top:3px;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden}\n.card .pr{font-size:13.5px;margin-top:4px}\n.card .pr s,.row .pr s{color:#8a8a8a;margin-left:4px}\n.card .so{font-size:11px;color:#a33;margin-left:6px}\n.card.skel .ph,.sk{background:linear-gradient(90deg,#f3f3f3,#e9e9e9,#f3f3f3);background-size:200% 100%;animation:sh 1.2s linear infinite}\n.skl{display:block;height:10px;border-radius:5px;margin-top:9px}\n@keyframes sh{to{background-position:-200% 0}}\n.ans{padding:18px 16px 24px}\n.ansh{display:flex;align-items:center;gap:6px;font-weight:600;font-size:11px;line-height:1;letter-spacing:.12em;text-transform:uppercase;color:#777}\n.anst{margin:10px 0 0;font-size:16.5px;line-height:1.5}\n.src{margin-top:20px;border-top:1px solid var(--line);padding-top:16px}\n.src.alone{margin-top:0;border-top:0;padding-top:0}\n.src .sech{margin:0 0 9px}\n.src .fq{font-weight:500;font-size:15px;line-height:1.3}\n.src .fa{margin-top:6px;font-size:13.5px;line-height:1.5;color:#555;white-space:pre-line}\n.empty{padding:36px 20px 20px;text-align:center}\n.empty h2{margin:0;font-weight:500;font-size:21px;line-height:1.25}\n.more{display:inline-block;margin-top:12px;font-size:13.5px;color:#6b6b6b;text-decoration:underline}\n.end{text-align:center;padding:0 14px 28px}\n.loader{display:flex;justify-content:center;padding:4px 0 28px;min-height:40px}\n.spin{width:18px;height:18px;border-radius:50%;border:2px solid #ddd;border-top-color:var(--ink);animation:spin .8s linear infinite}\n.popular{border-top:1px solid var(--line);margin:6px 14px 0;padding-top:14px}\n.popular .sech{margin:0 0 2px}\n.popular .grid{padding:12px 0 28px}\n@keyframes fadein{from{opacity:0}to{opacity:1}}\n@media (min-width:750px){\n .scrim{background:rgba(0,0,0,.18)}\n .panel{inset:auto;top:var(--top,16px);left:var(--left,16px);width:var(--w,min(760px,100vw - 32px));max-height:calc(100vh - var(--top,16px) - 16px);border-radius:12px;overflow:hidden;box-shadow:0 0 0 1px rgba(0,0,0,.06),0 14px 40px rgba(0,0,0,.16);animation:drop .14s ease-out both}\n .sbar{height:56px;padding:0 8px 0 16px;gap:6px;border-bottom:0}\n .field{height:56px;padding:0;gap:10px;border:0;border-radius:0;background:none}\n .body:not(:empty){border-top:1px solid var(--line)}\n .cancel{display:flex;align-items:center;justify-content:center;width:36px;height:36px;padding:0;border-radius:8px;color:#6b6b6b}\n .cancel:hover{background:#f2f2f2;color:var(--ink)}\n .cancel .t{display:none}\n .cancel .x{display:block}\n .grid{grid-template-columns:repeat(4,minmax(0,1fr))}\n .narrow .grid{grid-template-columns:repeat(3,minmax(0,1fr))}\n}\n@keyframes drop{from{opacity:0;transform:translateY(-4px)}to{opacity:1;transform:none}}\n@media (prefers-reduced-motion:reduce){*{animation:none!important;transition:none!important}}\n</style><div class="wrap" style="--ink:${r};--ai:color-mix(in srgb, ${r} 6%, #fff);--line:#eaeaea" hidden><div class="scrim"></div><div class="panel" role="dialog" aria-modal="true" aria-label="${a}"><form class="sbar" role="search"><label class="field"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><circle cx="11" cy="11" r="7"/><path d="m20 20-4-4"/></svg><input type="search" enterkeyhint="search" autocomplete="off" autocapitalize="off" spellcheck="false" placeholder="What are you looking for?"></label><button type="button" class="cancel" aria-label="${o}"><span class="t">${o}</span><svg class="x" width="18" height="18" viewBox="0 0 18 18" aria-hidden="true"><path d="M4.5 4.5l9 9M13.5 4.5l-9 9" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/></svg></button></form><div class="body"></div></div></div>`,this.input=e.querySelector("input"),this.body=e.querySelector(".body"),e.querySelector("form").addEventListener("submit",t=>{t.preventDefault(),this.search()}),this.input.addEventListener("input",()=>this.typed()),e.querySelector(".cancel").addEventListener("click",()=>this.close()),e.querySelector(".scrim").addEventListener("click",()=>this.close()),this.body.addEventListener("click",t=>this.clicked(t)),this.body.addEventListener("scroll",()=>this.scrolled(),{passive:!0}),window.addEventListener("popstate",this.onPop),window.addEventListener("resize",this.onResize,{passive:!0}),document.addEventListener("keydown",this.onKey,!0),document.documentElement.appendChild(this.host)}host;input;body;state;ctx;shopper;timers=[];suggestAbort=null;searchAbort=null;moreAbort=null;pausedFor="";isOpen=!1;pushed=!1;overflow="";started=0;anchor=null;get wrap(){return this.host.shadowRoot.querySelector(".wrap")}place(){const t=this.host.shadowRoot.querySelector(".panel");if(!window.matchMedia("(min-width: 750px)").matches)return t.removeAttribute("style"),void t.classList.remove("narrow");let e=this.anchor&&this.anchor.isConnected?this.anchor:null,n=e?.getBoundingClientRect();(!n||n.width<=0)&&(e=function(){for(const t of document.querySelectorAll(r)){if(t.closest(`#${m}`))continue;const e=t.getBoundingClientRect();if(e.width>0&&e.height>0&&e.top<window.innerHeight/3)return t}return null}(),n=e?.getBoundingClientRect());const i=function(t,e,n){const i=e-32;if(!t||t.width<=0||t.height<=0){const t=Math.min(760,i);return{top:16,left:Math.round((e-t)/2),width:t}}const s=t.width>=240,r=Math.round(Math.min(i,s?Math.max(t.width+16,560):760)),o=s?t.left-8:(e-r)/2,a=t.top+t.height/2-28;return{top:Math.round(Math.min(Math.max(a,8),Math.max(8,n-240))),left:Math.round(Math.min(Math.max(o,16),e-16-r)),width:r}}(n??null,window.innerWidth,window.innerHeight);t.style.setProperty("--top",`${i.top}px`),t.style.setProperty("--left",`${i.left}px`),t.style.setProperty("--w",`${i.width}px`),t.classList.toggle("narrow",i.width<860)}onResize=()=>{this.isOpen&&this.place()};open(t,n,i=null){if(!this.isOpen){this.isOpen=!0,this.anchor=i,this.wrap.hidden=!1,this.overflow=document.documentElement.style.overflow,document.documentElement.style.overflow="hidden",this.place();try{history.pushState({...history.state||{},doriAiSearch:!0},""),this.pushed=!0}catch{this.pushed=!1}e("ai_search_open",{run:n})}t&&t!==this.input.value&&(this.input.value=t,this.typed()),n||this.input.focus(),n&&t.trim()&&this.search()}restore(t){this.isOpen=!0;try{history.replaceState({...history.state||{},doriAiSearch:!1},""),history.pushState({...history.state||{},doriAiSearch:!0},""),this.pushed=!0}catch{this.pushed=!1}this.wrap.hidden=!1,this.overflow=document.documentElement.style.overflow,document.documentElement.style.overflow="hidden",this.anchor=null,this.place(),this.input.value=t.q,this.dispatch({type:"restore",q:t.q,rid:t.rid,result:t.result,drop:t.drop})}close(t=!1){this.isOpen&&(this.isOpen=!1,this.cancelAll(),this.wrap.hidden=!0,document.documentElement.style.overflow=this.overflow,this.pushed&&!t&&history.state?.doriAiSearch&&(this.pushed=!1,history.back()),this.pushed=!1)}destroy(){this.close(),window.removeEventListener("popstate",this.onPop),window.removeEventListener("resize",this.onResize),document.removeEventListener("keydown",this.onKey,!0),this.host.remove()}onKey=t=>{this.isOpen&&"Escape"===t.key&&(t.stopPropagation(),this.close())};onPop=()=>{this.isOpen&&!history.state?.doriAiSearch&&this.close(!0)};dispatch(t){this.state=function(t,e){switch(e.type){case"input":return{...t,q:e.q,phase:"typing",rid:null,plan:null,result:null,loadingMore:!1};case"suggested":return e.q!==t.q.trim()||"typing"!==t.phase?t:e.collections?{...t,suggestions:e.products,suggestedFor:e.q,collections:e.collections,collectionsFor:e.q}:{...t,suggestions:e.products,suggestedFor:e.q};case"enter":return{...t,phase:"working",asked:t.q.trim(),rid:e.rid,drop:e.drop??[],plan:null,result:null,loadingMore:!1};case"server":return e.rid!==t.rid||"working"!==t.phase?t:"plan"===e.event.type?{...t,plan:e.event}:g.has(e.event.type)?{...t,phase:"done",result:e.event}:t;case"failed":return e.rid!==t.rid||"working"!==t.phase?t:{...t,phase:"done",result:{type:"fallback",products:[],cursor:null}};case"loading_more":return e.rid!==t.rid?t:{...t,loadingMore:!0};case"more":{const n=t.result;if(e.rid!==t.rid||!n||"results"!==n.type&&"fallback"!==n.type)return t;const i=new Set(n.products.map(t=>t.id)),s=[...n.products,...e.products.filter(t=>!i.has(t.id))];return{...t,loadingMore:!1,result:{...n,products:s,cursor:e.cursor}}}case"restore":return{...t,q:e.q,asked:e.q,rid:e.rid,drop:e.drop,phase:"done",result:e.result,plan:null,loadingMore:!1}}}(this.state,t);const e=this.body.scrollTop;this.body.innerHTML=j(this.state,this.ctx),"done"===this.state.phase&&(this.body.scrollTop=e)}cancelAll(){this.timers.forEach(t=>clearTimeout(t)),this.timers=[];for(const t of[this.suggestAbort,this.searchAbort,this.moreAbort])t?.abort();this.suggestAbort=this.searchAbort=this.moreAbort=null}typed(){this.cancelAll(),this.body.scrollTop=0,this.dispatch({type:"input",q:this.input.value});const t=this.input.value.trim();if(!t)return;this.pausedFor="";const e=new AbortController;this.suggestAbort=e,this.timers.push(window.setTimeout(()=>{u(this.config.sharingId,t,"key",this.shopper,e.signal).then(e=>{this.pausedFor!==t&&this.dispatch({type:"suggested",q:t,products:e})}).catch(()=>{})},80)),this.timers.push(window.setTimeout(()=>{Promise.all([u(this.config.sharingId,t,"pause",this.shopper,e.signal),f(t,this.ctx.root,e.signal)]).then(([e,n])=>{this.pausedFor=t,this.dispatch({type:"suggested",q:t,products:e,collections:n})}).catch(()=>{})},350))}search(t=[]){const n=this.input.value.trim();if(!n)return;this.cancelAll();const i=function(){const t=globalThis.crypto;if(t?.randomUUID)return t.randomUUID();const e=Array.from({length:16},()=>Math.floor(256*Math.random()));e[6]=15&e[6]|64,e[8]=63&e[8]|128;const n=e.map(t=>t.toString(16).padStart(2,"0")).join("");return`${n.slice(0,8)}-${n.slice(8,12)}-${n.slice(12,16)}-${n.slice(16,20)}-${n.slice(20)}`}();this.started=performance.now(),this.dispatch({type:"enter",rid:i,drop:t}),this.input.blur(),this.body.scrollTop=0;const s=new AbortController;this.searchAbort=s,async function(t,e,n,i){const s=await fetch(`${h(t)}/search`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e),signal:i});if(!s.ok||!s.body)throw new p(s.status);const r=s.body.getReader(),o=new TextDecoder,a={buffer:""};for(;;){const{done:t,value:e}=await r.read(),i=t?o.decode()+"\n\n":o.decode(e,{stream:!0});for(const s of c(i,a))try{n(JSON.parse(s.data))}catch{}if(t)return}}(this.config.sharingId,{q:n,rid:i,drop:t,...this.shopper},t=>{this.dispatch({type:"server",rid:i,event:t}),"plan"!==t.type&&e("ai_search_enter",{outcome:t.type,ms:Math.round(performance.now()-this.started),count:"products"in t&&Array.isArray(t.products)?t.products.length:void 0})},s.signal).then(()=>this.dispatch({type:"failed",rid:i})).catch(t=>{if(!s.signal.aborted)return t instanceof p?(this.close(),void location.assign($(n,this.ctx))):void this.dispatch({type:"failed",rid:i})})}loadMore(){const t=this.state.result,e=this.state.rid;if(!e||this.state.loadingMore||!t||"results"!==t.type&&"fallback"!==t.type||null==t.cursor)return;this.dispatch({type:"loading_more",rid:e});const n=new AbortController;this.moreAbort=n,async function(t,e,n,i){const s=await fetch(`${h(t)}/more?${new URLSearchParams({rid:e,cursor:String(n)})}`,{signal:i});if(!s.ok)throw new p(s.status);return await s.json()}(this.config.sharingId,e,t.cursor,n.signal).then(t=>this.dispatch({type:"more",rid:e,products:t.products,cursor:t.cursor})).catch(()=>{n.signal.aborted||this.dispatch({type:"more",rid:e,products:[],cursor:null})})}scrolled(){const t=this.body;t.scrollTop+t.clientHeight>t.scrollHeight-500&&this.loadMore()}clicked(t){const n=t.target;if(n.closest("[data-act='enter']"))return t.preventDefault(),void this.search();const i=n.closest("[data-drop]");if(i)return t.preventDefault(),void this.search([...this.state.drop,i.dataset.drop]);const s=n.closest("a.card, a.row");s&&(e("ai_search_click",{phase:this.state.phase,outcome:this.state.result?.type,id:s.dataset.id}),this.save())}save(){const t=this.state;if("done"!==t.phase||!t.result||!t.rid)return;const e={q:t.asked,rid:t.rid,drop:t.drop,result:t.result,url:location.href,at:Date.now()};try{sessionStorage.setItem(C,JSON.stringify(e))}catch{}}}let N=null,R=null;function I(t,e){return!!t&&!0===t.feature_flags?.ai_search&&!e}function O(t,e){const n=window;if(!I(t,!0===n.Shopify?.designMode))return N?.(),N=null,R=null,F?.destroy(),void(F=null);R={sharingId:e},N||(N=function(t,e,n){const i=t=>{const i=t.target;if(!i||"FORM"!==i.tagName||!y(i.getAttribute("action"),n))return;t.preventDefault(),t.stopImmediatePropagation();const s=i.querySelector('input[name="q"]');e(s?.value??"",!0,s??i)},s=t=>{const i=t.target;i&&"INPUT"===i.tagName&&i.matches('input[type="search"], input[name="q"]')&&(i.form?y(i.form.getAttribute("action"),n):i.closest?.(x))&&(i.blur?.(),e(i.value??"",!1,i))},o=t=>{const i=t;if(i.defaultPrevented||0!==(i.button??0)||i.metaKey||i.ctrlKey||i.shiftKey||i.altKey)return;const s=t.target;if(!s?.closest)return;const o=s.closest(r);if(!o||"INPUT"===o.tagName)return;if(o.closest(`#${m}`))return;if("FORM"===o.tagName||s.closest("form"))return;if(!("A"===o.tagName?y(o.getAttribute("href"),n):o.closest(x)))return;t.preventDefault(),t.stopImmediatePropagation();const a="A"===o.tagName?function(t,e){if(!t)return"";try{return new URL(t,e).searchParams.get("q")??""}catch{return""}}(o.getAttribute("href"),n):"";e(a,!!a,o)};return t.addEventListener("submit",i,!0),t.addEventListener("focusin",s,!0),t.addEventListener("click",o,!0),()=>{t.removeEventListener("submit",i,!0),t.removeEventListener("focusin",s,!0),t.removeEventListener("click",o,!0)}}(window,(t,e,n)=>R&&function(t,e,n,i=null){F||(F=new P(R)),F.open(e,n,i)}(0,t,e,n),location.origin),function(t){let e=null;try{e=JSON.parse(sessionStorage.getItem(C)||"null")}catch{e=null}const n=history.state,i=n?.doriAiSearch?function(t,e,n){return!t||t.url!==e||n-t.at>18e5?null:t}(e,location.href,Date.now()):null;i&&(F||(F=new P(t)),F.restore(i))}(R))}export{O as applyAiSearch,I as wantsAiSearch};
|
|
Binary file
|
|
Binary file
|