3 lines
10 KiB
JavaScript
3 lines
10 KiB
JavaScript
import{getCookie,useEventListener}from"./chunk-7JONXWSO.js";import{k}from"./chunk-RG4ZUIOR.js";import{icons_exports,loader_2_default}from"./chunk-PE2ZHJ3V.js";import{IS_BROWSER}from"./chunk-P55SGJLG.js";import{w}from"./chunk-ICN4RY3D.js";import"./chunk-SMUKOLE7.js";import{v}from"./chunk-CUT5GMLI.js";import{A}from"./chunk-YC2TUGGU.js";import"./chunk-UK35T4TJ.js";var addMovieInfos={title:"Add Movie infos",meta:"",icon:"IconReportSearch",cb:async(state,context)=>{state.activeState.value="loading";let movie=context,query=movie.name,json=await(await fetch(`/api/tmdb/query?q=${encodeURIComponent(query)}`)).json(),menuID=`result/${movie.name}`;state.menus[menuID]={title:"Select",entries:json.map(m=>({title:`${m.title} released ${m.release_date}`,cb:async()=>{state.activeState.value="loading",await fetch(`/api/movies/enhance/${movie.name}/`,{method:"POST",body:JSON.stringify({tmdbId:m.id})}),state.visible.value=!1,state.activeState.value="normal",window.location.reload()}}))},state.activeMenu.value=menuID,state.commandInput.value="",state.activeState.value="normal"},visible:()=>{let loc=globalThis.location;return getCookie("session_cookie")?loc?.pathname?.includes("movie")&&!loc.pathname.endsWith("movies"):!1}};var isValidUrl=urlString=>{try{return!!new URL(urlString)}catch{return!1}};async function fetchStream(url,cb){let reader=(await fetch(url))?.body?.getReader();if(reader)for(;;){let{done,value}=await reader.read();if(done)return;new TextDecoder().decode(value).split("$").filter(d=>d&&d.length).map(d=>cb(Array.isArray(d)?d[0]:d))}}function debounce(fn,delay=300){let timer;return(...args)=>{clearTimeout(timer),timer=setTimeout(()=>fn.apply(this,args),delay)}}var createNewMovie={title:"Create new movie",meta:"",icon:"IconSquareRoundedPlus",cb:state=>{state.menus.input_link={title:"Search",entries:[]},state.menus.loading={title:"Search",entries:[{title:"Loading",icon:"IconLoader2",cb(){}}]},state.activeMenu.value="input_link",state.activeState.value="normal";let currentQuery,search=debounce(async function(query){if(currentQuery=query,query.length<2)return;let movies=await(await fetch("/api/tmdb/query?q="+query)).json();query===currentQuery&&(state.menus.input_link={title:"Search",entries:movies.map(r=>({title:`${r.title} - ${r.release_date}`,cb:async()=>{state.activeState.value="loading";let movie=await(await fetch("/api/movies/"+r.id,{method:"POST"})).json();unsub(),window.location.href="/movies/"+movie.name}}))},state.activeMenu.value="input_link")},500),unsub=state.commandInput.subscribe(value=>{state.activeMenu.value="loading",search(value)})},visible:()=>!(!getCookie("session_cookie")||!globalThis?.location?.pathname?.includes("movie")&&globalThis?.location?.pathname!=="/")};var createNewArticle={title:"Create new article",meta:"",icon:"IconSquareRoundedPlus",cb:state=>{state.menus.input_link={title:"Link:",entries:[]},state.activeMenu.value="input_link",state.activeState.value="input";let unsub=state.commandInput.subscribe(value=>{isValidUrl(value)&&(unsub(),state.activeState.value="loading",fetchStream("/api/articles/create?url="+value,chunk=>{chunk.startsWith("id:")?(state.loadingText.value="Finished",setTimeout(()=>{window.location.href="/articles/"+chunk.replace("id:","").trim()},500)):state.loadingText.value=chunk}))})},visible:()=>!(!getCookie("session_cookie")||!globalThis?.location?.pathname?.includes("article")&&globalThis?.location?.pathname!=="/")};var addSeriesInfo={title:"Add Series infos",meta:"",icon:"IconReportSearch",cb:async(state,context)=>{state.activeState.value="loading";let series=context,query=series.name,json=await(await fetch(`/api/tmdb/query?q=${encodeURIComponent(query)}&type=serie`)).json(),menuID=`result/${series.name}`;state.menus[menuID]={title:"Select",entries:json.map(m=>({title:`${m.name||m.original_name} released ${m.first_air_date}`,cb:async()=>{state.activeState.value="loading",await fetch(`/api/series/enhance/${series.name}/`,{method:"POST",body:JSON.stringify({tmdbId:m.id})}),state.visible.value=!1,state.activeState.value="normal"}}))},state.commandInput.value="",state.activeMenu.value=menuID,state.activeState.value="normal"},visible:()=>{let loc=globalThis.location;return getCookie("session_cookie")?loc?.pathname?.includes("series")&&!loc.pathname.endsWith("series"):!1}};var createNewSeries={title:"Create new series",meta:"",icon:"IconSquareRoundedPlus",cb:state=>{state.menus.input_link={title:"Search",entries:[]},state.menus.loading={title:"Search",entries:[{title:"Loading",icon:"IconLoader2",cb(){}}]},state.activeMenu.value="input_link",state.activeState.value="normal";let currentQuery,search=debounce(async function(query){if(currentQuery=query,query.length<2)return;let series=await(await fetch("/api/tmdb/query?q="+query+"&type=series")).json();query===currentQuery&&(state.menus.input_link={title:"Search",entries:series.map(r=>({title:`${r.name} - ${r.first_air_date}`,cb:async()=>{state.activeState.value="loading";let series2=await(await fetch("/api/series/"+r.id,{method:"POST"})).json();unsub(),window.location.href="/series/"+series2.name}}))},state.commandInput.value="",state.activeMenu.value="input_link")},500),unsub=state.commandInput.subscribe(value=>{state.activeMenu.value="loading",search(value)})},visible:()=>!(!getCookie("session_cookie")||!globalThis?.location?.pathname?.includes("series")&&globalThis?.location?.pathname!=="/")};var menus={main:{title:"Run",entries:[{title:"Clear Cache",icon:"IconRefresh",cb:async state=>{state.activeState.value="loading",await fetch("/api/cache",{method:"DELETE"}),state.activeState.value="normal",state.visible.value=!1},visible:()=>!!getCookie("session_cookie")},{title:"Login",icon:"IconLogin",cb:()=>{let url=new URL(window.location.href);url.pathname="/api/auth/login",url.searchParams.set("redirect",encodeURIComponent(window.location.pathname)),window.location.href=url.href},visible:()=>!getCookie("session_cookie")},{title:"Search",icon:"IconSearch",cb:()=>{window.location.href+="?q="},visible:()=>!!getCookie("session_cookie")&&window.location.search===""},{title:"Logout",icon:"IconLogout",cb:()=>{let url=new URL(window.location.href);url.pathname="/api/auth/logout",url.searchParams.set("redirect",encodeURIComponent(window.location.pathname)),window.location.href=url.href},visible:()=>!!getCookie("session_cookie")},{title:"Sync Typesense",icon:"IconStarFilled",cb:()=>{fetch("/api/query/sync",{method:"POST"})},visible:()=>!!getCookie("session_cookie")},addSeriesInfo,createNewArticle,createNewMovie,createNewSeries,addMovieInfos]}};var KMenuEntry=({entry,activeIndex,index})=>v("div",{onClick:()=>activeIndex.value=index,class:`px-4 py-2 flex items-center gap-1 ${activeIndex.value===index?"bg-gray-100 text-gray-900":"text-gray-400"}`,children:[entry?.icon&&icons_exports[entry.icon]({class:"w-4 h-4 mr-1"}),entry.title]}),KMenu=({type="main",context})=>{let activeMenuType=k(type),activeMenu=menus[activeMenuType.value||"main"],activeState=k("normal"),loadingText=k(""),activeIndex=k(-1),containerRef=w(null),input=w(null),commandInput=k(""),visible=k(!1);visible.value===!1&&setTimeout(()=>{activeMenuType.value="main",activeState.value="normal",input.current&&(input.current.value=""),commandInput.value=""},100);let entries=activeMenu?.entries?.filter(entry=>{let entryVisible=typeof entry.visible=="boolean"?entry.visible:typeof entry.visible=="function"?entry.visible?.(context):!0,search=commandInput?.value?.toLowerCase(),searchMatches=entry.title.toLowerCase().includes(search)||entry.meta?.toLowerCase()?.includes(search);return entryVisible&&searchMatches});entries.length===1&&(activeIndex.value=0);function activateEntry(menuEntry){menuEntry&&menuEntry.cb({activeMenu:activeMenuType,loadingText,menus,activeState,commandInput,visible},context)}let container=containerRef.current;if(container){let selectedItem=container.children[activeIndex.value];if(selectedItem){let itemPosition=selectedItem.getBoundingClientRect(),containerPosition=container.getBoundingClientRect();itemPosition.top<containerPosition.top?container.scrollTop-=containerPosition.top-itemPosition.top:itemPosition.bottom>containerPosition.bottom&&(container.scrollTop+=itemPosition.bottom-containerPosition.bottom)}}return useEventListener("keydown",ev=>{if(ev.key==="k"){if(ev?.target?.nodeName=="INPUT")return;visible.value||(ev.preventDefault(),ev.stopPropagation()),visible.value=!visible.value}ev.key==="ArrowDown"&&(activeIndex.value+1>=entries.length?activeIndex.value=0:activeIndex.value+=1),ev.key==="Enter"&&activateEntry(entries[activeIndex.value]),ev.key==="ArrowUp"&&(activeIndex.value-1<0?activeIndex.value=entries.length-1:activeIndex.value-=1),ev.key==="Escape"&&(visible.value=!1,input.current&&(input.current.value="")),visible.value?input.current?.focus():input.current?.blur()},IS_BROWSER?document?.body:void 0),v("div",{class:`opacity-${visible.value?100:0} pointer-events-${visible.value?"auto":"none"} transition grid place-items-center w-full h-full fixed top-0 left-0 z-50`,style:{background:"#141217ee"},children:v("div",{class:"relative w-1/2 max-h-64 max-w-[400px] rounded-2xl shadow-2xl nnoisy-gradient overflow-hidden after:opacity-10 bg-gray-700",style:{background:"#2B2930",color:"#818181"},children:[v("div",{class:`grid h-12 text-gray-400 ${activeState.value!=="loading"&&"border-b"} border-gray-500 `,style:{gridTemplateColumns:activeState.value!=="loading"?"auto 1fr":"1fr"},children:[(activeState.value==="normal"||activeState.value==="input")&&v(A,{children:[v("div",{class:"grid place-items-center border-r border-gray-500",children:v("span",{class:"text-white mx-4",children:activeMenu.title})}),v("input",{ref:input,onInput:()=>{commandInput.value=input.current?.value||""},style:{outline:"none !important"},placeholder:"Command",class:"bg-transparent color pl-4 outline outline outline-2 outline-offset-2"})]}),activeState.value==="loading"&&v("div",{class:"py-3 px-4 flex items-center gap-2",children:[v(loader_2_default,{class:"animate-spin w-4 h-4"}),loadingText.value||"Loading..."]})]}),activeState.value==="normal"&&v("div",{class:"",style:{maxHeight:"12rem",overflowY:"auto"},ref:containerRef,children:[entries?.length===0&&v("div",{class:"text-gray-400 px-4 py-2",children:"No Entries"}),entries.map((k2,index)=>v(KMenuEntry,{entry:k2,activeIndex,index}))]})]})})};export{KMenu};
|
|
//# sourceMappingURL=island-kmenu_kmenu.js.map
|