MTG4WP/build/index.js

1 line
7.4 KiB
JavaScript
Raw Permalink Normal View History

2025-01-06 14:23:25 -06:00
(()=>{"use strict";var e,t={21:()=>{const e=window.wp.blocks,t=window.React,a=window.wp.element,n=window.wp.components,o=window.wp.blockEditor,r=window.wp.i18n,l={},s=(e,t)=>e.cmc!==t.cmc?e.cmc-t.cmc:e.name.localeCompare(t.name),c=(e,a)=>((e=>{e.length})(a),a.length>0?(0,t.createElement)("div",{key:e},(0,t.createElement)("h2",{className:"has-small-font-size"},`${e} (${(e=>e.reduce(((e,t)=>e+(t.quantity||1)),0))(a)})`),(0,t.createElement)("div",{className:"mtg-tools-card-category"},a.map(((e,a)=>(0,t.createElement)("div",{key:e.id||a,className:"mtg-tools-card has-small-font-size","data-card-name":e.scryfallName||e.name,"data-card-front-image-uri":e.frontImage||"","data-card-back-image-uri":e.backImage||"","data-card-foil":e.foil?"Yes":"No"},(0,t.createElement)("span",{className:"mtg-tools-quantity"},e.quantity||1),(0,t.createElement)("div",{className:"mtg-tools-card-name"},e.scryfallName||e.name),e.backImage&&(0,t.createElement)("div",{className:"mtg-tools-flip-button"},(0,t.createElement)("button",{className:"mtg-tools-flip dashicons dashicons-image-rotate",type:"button"}))))))):null),m=JSON.parse('{"UU":"mtg-tools/mtg-tools"}');(0,e.registerBlockType)(m.UU,{edit:function({attributes:e,setAttributes:s}){const c=(0,o.useBlockProps)(),[m,i]=(0,a.useState)(e.cards||[]),[d,u]=(0,a.useState)(!1),f=(0,a.useRef)(null),g=["Token","Creature","Land","Artifact","Enchantment","Planeswalker","Battle","Instant","Sorcery"],p=(e,t,a)=>{const n=[...m];n[e][t]=a,i(n),s({cards:n}),"name"!==t&&"set"!==t&&"number"!==t||((e,t)=>{clearTimeout(f.current),f.current=setTimeout((()=>{u(!0),(async(e,t)=>{const a=`${e.name}-${e.set}-${e.number}`;if(l[a])return l[a];let n="";n=e.set&&e.number?`https://api.scryfall.com/cards/${encodeURIComponent(e.set)}/${encodeURIComponent(e.number)}`:`https://api.scryfall.com/cards/named?fuzzy=${encodeURIComponent(e.name)}&set=${encodeURIComponent(e.set)}`;try{const e=await fetch(n),t=await e.json();if(200===e.status)return l[a]=t,t;if(404===e.status)return{status:404,message:"Not Found"}}catch(e){}finally{u(!1)}})(e).then((a=>{if(a){const n=[...m];let o=null,r=null,l="",c="",d=e.name;if(404===a.status)n[t]={...n[t],notFound:!0};else{"modal_dfc"===a.layout||"transform"===a.layout?(o=a.card_faces?.[0]?.image_uris?.normal||null,r=a.card_faces?.[1]?.image_uris?.normal||null,l=a.card_faces?.[0]?.mana_cost,c=a.card_faces?.[0]?.type_line,d=a.card_faces?.[0]?.name):"adventure"===a.layout?(o=a.image_uris?.normal||null,l=a.mana_cost,c=a.card_faces?.[0]?.type_line||a.type_line,d=a.card_faces?.[0]?.name||a.name):(o=a.image_uris?.normal||null,l=a.mana_cost,c=a.type_line,d=a.name);const e=(e=>{const t=e.split(" ");for(let e of g)if(t.includes(e))return e;return t[0]||"Unknown"})(c);n[t]={...n[t],scryfallName:d,scryfallSet:a.set_name,scryfallCollectorNumber:a.collector_number,cmc:a.cmc,manaCost:l,type:e,frontImage:o,backImage:r,scryfall_uri:a.scryfall_uri,notFound:!1}}i(n),s({cards:n})}}))}),1e3)})(n[e],e)};return(0,t.createElement)("div",{...c},d,m.map(((e,a)=>(0,t.createElement)("div",{key:a,className:"mtg-card"},(0,t.createElement)("div",{className:"card-inputs"},(0,t.createElement)(n.TextControl,{label:(0,r.__)("Name","mtg-tools"),value:e.name,onChange:e=>p(a,"name",e)}),(0,t.createElement)(n.TextControl,{label:(0,r.__)("Set","mtg-tools"),value:e.set,onChange:e=>p(a,"set",e)}),(0,t.createElement)(n.TextControl,{label:(0,r.__)("Number","mtg-tools"),value:e.number,onChange:e=>p(a,"number",e)}),(0,t.createElement)(n.TextControl,{label:(0,r.__)("Quantity","mtg-tools"),type:"number",value:e.quantity,onChange:e=>p(a,"quantity",parseInt(e))}),(0,t.createElement)(n.CheckboxControl,{label:(0,r.__)("Commander","mtg-tools"),checked:e.commander,onChange:e=>p(a,"commander",e)}),(0,t.createElement)(n.CheckboxControl,{label:(0,r.__)("Foil","mtg-tools"),checked:e.foil,onChange:e=>p(a,"foil",e)}),(0,t.createElement)(n.CheckboxControl,{label:(0,r.__)("Sideboard","mtg-tools"),checked:e.Sideboard,onChange:e=>p(a,"sideboard",e)})),(0,t.createElement)("div",{className:"card-preview"},e.notFound?(0,t.createElement)("div",{className:"not-found"},(0