@nitrogenbuilder/editor
Version:
Nitrogen Builder Editor Assets
1 lines • 7.61 kB
JavaScript
import{a as assertOk,b as authErrorFromParseFailure,l as logger,j as jsxRuntimeExports,I as Input,e as entry_default}from"./index.js";import{p as projectPath}from"./projectPath-DAlZ7Vbb.js";const wordpress=({apiUrl:e,collection:t,appUser:s,appPassword:a})=>{const i={keys:["title"]};let n=[],l=null;async function r(){return l||(n.length||await async function(){const e=await fetch(`${d}/${p}/`,{credentials:"include",headers:o()});n=await e.json()}(),l=new entry_default(n,i),l)}function o(){let e={};return window.nitrogenNonce&&(e["X-WP-Nonce"]=window.nitrogenNonce),s&&a&&(e.Authorization="Basic "+btoa(`${s}:${a}`)),e}const d=e,c=e.split("/wp-json")[0],p=t,u=(e,t,s)=>{const a=projectPath(t,s);if(!a)return logger.warn("Missing slug or relativeUrl in pageData for Provider's projectUrl()"),"";return e.split("/?")[0]+a};return{savePage:async({title:e,settings:t,page:s,setPageId:a,pageId:i})=>{if(null!=i&&""!==i.trim()){const a=new URLSearchParams(window.location.search).get("authorId"),n=await fetch(`${d}/collection/${p}/${i}`,{method:"PATCH",credentials:"include",headers:{"Content-Type":"application/json",...o()},body:JSON.stringify({data:{title:e,settings:t,author:a,data:JSON.stringify(s)}})});return void await assertOk(n,"Failed to save page")}const n=await fetch(`${d}/${p}`,{method:"POST",credentials:"include",headers:{"Content-Type":"application/json",...o()},body:JSON.stringify({data:{title:e,data:s}})});await assertOk(n,"Failed to save page");a(`${(await n.json()).data.id}`)},loadPage:async(e,t)=>{const s=await fetch(`${d}/collection/${p}/${e}`,{credentials:"include",headers:o(),...t?{signal:t.signal}:{}});let a;await assertOk(s,"Failed to load page");try{a=await s.json()}catch{throw authErrorFromParseFailure("Failed to load page")}return{title:a.title,data:a.content,slug:a.slug,relative_permalink:a.relative_permalink,dynamic_data:a.dynamic_data,template:a.template||null,headerTemplate:a.headerTemplate||null,footerTemplate:a.footerTemplate||null,settings:a.settings??{},nitrogenSettings:a.nitrogen_settings,projectSettings:a.project_settings,templateConditions:a.templateConditions??void 0}},saveProjectSettings:async e=>{const t=await fetch(`${d}/project-settings`,{method:"PUT",credentials:"include",headers:{"Content-Type":"application/json",...o()},body:JSON.stringify(e)});await assertOk(t,"Failed to save project settings");return await t.json()},saveNitrogenSettings:async e=>{const t=await fetch(`${d}/nitrogen-settings`,{method:"PUT",credentials:"include",headers:{"Content-Type":"application/json",...o()},body:JSON.stringify(e)});await assertOk(t,"Failed to save settings");return await t.json()},saveTemplateConditions:async(e,t)=>{const s=await fetch(`${d}/templates/${e}`,{method:"PATCH",credentials:"include",headers:{"Content-Type":"application/json",...o()},body:JSON.stringify({data:{conditions:t}})});await assertOk(s,"Failed to save template conditions")},searchUrls:async(e,t,s)=>{if(t.length>0){return(await r()).search(t).map(t=>({id:t.item.id,label:t.item.title,slug:t.item.slug,url:u(e,{slug:t.item.slug,relative_permalink:t.item.relative_permalink},s)}))}return[]},searchInternalLinks:async(e,t,s)=>{if(t.length>0){return(await r()).search(t).map(e=>({id:e.item.id,label:e.item.title,slug:e.item.slug,path:projectPath({slug:e.item.slug,relative_permalink:e.item.relative_permalink},s),internalRef:{entryId:e.item.id,slug:e.item.slug,relativePermalink:e.item.relative_permalink}}))}return[]},editorUrl:e=>e?`${c}/wp-admin/post.php?post=${e}&action=edit`:`${c}/wp-admin/`,projectUrl:u,gallery:{getFileUrl:e=>e.url,getFiles:async(e,t,s,a,i)=>{let n=[];t&&(n=[`filters[ext][]=${t.join("&filters[ext][]=")}`]),s.length>0&&n.push(`filters[search]=${s}`),a>0&&n.push(`paged=${a}`),fetch(`${d}/media?${n.join("&")}`,{credentials:"include",headers:o()}).then(e=>e.json()).then(t=>{t.images.sort((e,t)=>new Date(t.post_date).getTime()-new Date(e.post_date).getTime()),e(t.images),i(t.pages)})},updateFile:(e,t)=>{fetch(`${d}/media?id=${e}`,{method:"PATCH",credentials:"include",headers:{"Content-Type":"application/json",...o()},body:JSON.stringify({alt:t.alt})})},uploadFiles:async e=>{const t=new FormData;e.forEach(e=>t.append("files[]",e,e.name));const s=await fetch(`${d}/media`,{method:"POST",credentials:"include",headers:o(),body:t});return await s.json()},renderFiles:(e,t,s)=>(logger.log("Rendering files",{context:{selectedFile:t}}),e.map(e=>jsxRuntimeExports.jsx("div",{className:`h-24 w-24 cursor-pointer overflow-hidden rounded border-2 transition-all duration-150 ${[".png",".svg",".eps"].includes(e.ext),""} ${t?.id===e.id?"border-input-accent-lighter bg-input-accent-lighter text-input-accent-darker":"bg-input-accent-darker hover:bg-input-accent-dark border-input-accent-darker hover:border-input-accent-dark"}`,children:e.mime.includes("image")?jsxRuntimeExports.jsx("img",{draggable:!1,className:"h-full w-full "+([".png",".svg",".eps"].includes(e.ext)?"object-contain":"object-cover"),alt:e.alt,src:e.formats.thumbnail.url,onClick:()=>s(e)},e.id):jsxRuntimeExports.jsx("div",{className:"flex h-full w-full items-center justify-center",title:e.name,onClick:()=>s(e),children:jsxRuntimeExports.jsxs("div",{className:"flex flex-col items-center justify-center text-center",children:[jsxRuntimeExports.jsx("div",{className:"text-xs font-medium leading-5",children:e.name.split(".")[0].length>10?jsxRuntimeExports.jsxs("span",{title:e.name,children:[e.name.split(".")[0].slice(0,10),"..."]}):e.name.split(".")[0]}),jsxRuntimeExports.jsx("div",{children:e.ext}),jsxRuntimeExports.jsx("div",{className:"text-xs leading-4",children:e.size<1e3?Math.round(e.size)+" KB":Math.round(e.size/100)/10+" MB"})]})})},e.id))),renderSelectedFile:(e,t,s)=>{const a=new Date(e.post_date),i=`${a.getDate()}/${a.getMonth()}/${a.getFullYear()}`,n=e.name,l=e.ext,r=e.alt,o=e.mime;return jsxRuntimeExports.jsxs("div",{className:"flex w-full flex-wrap gap-4 text-app-text",children:[jsxRuntimeExports.jsx("div",{className:"aspect-square h-auto w-1/2 overflow-hidden rounded bg-input-accent-darker "+([".png",".svg",".eps"].includes(l),""),children:o.includes("image")?jsxRuntimeExports.jsx("img",{draggable:!1,className:"h-full w-full "+([".png",".svg",".eps"].includes(l)?"object-contain":"object-cover"),alt:r,src:e.formats.thumbnail.url}):jsxRuntimeExports.jsx("div",{className:"flex h-full w-full items-center justify-center",title:n,children:jsxRuntimeExports.jsxs("div",{className:"flex flex-col items-center justify-center text-center",children:[jsxRuntimeExports.jsx("div",{className:"text-xs font-medium leading-5",children:n.length>10?jsxRuntimeExports.jsxs("span",{title:n,children:[n.slice(0,10),"..."]}):n}),jsxRuntimeExports.jsx("div",{children:l}),jsxRuntimeExports.jsx("div",{className:"text-xs leading-4",children:e.size<1e3?Math.round(e.size)+" KB":Math.round(e.size/100)/10+" MB"})]})})}),s&&jsxRuntimeExports.jsxs("div",{className:"flex w-full flex-col gap-1 text-xs font-medium leading-5 [overflow-wrap:anywhere]",children:[jsxRuntimeExports.jsxs("div",{className:"",children:["ID: ",e.id]}),jsxRuntimeExports.jsx("div",{className:"font-semibold",children:n}),jsxRuntimeExports.jsx("div",{className:"",children:i}),jsxRuntimeExports.jsxs("div",{className:"",children:[Math.round(e.size)," KB"]}),o&&o.includes("image")&&jsxRuntimeExports.jsxs("div",{className:"flex flex-col gap-3",children:[e.width," by ",e.height," pixels",jsxRuntimeExports.jsx("div",{className:"",children:"Alt Text:"}),jsxRuntimeExports.jsx(Input,{type:"textarea",rows:3,value:e.alt??"",onChange:s=>{t({...e,alt:s.target.value})}})]})]})]})}}}};export{wordpress as default};