Files
ruseemrooz/dist/client/_astro/hoisted.ClNlXW51.js
mrfelfel dfb0e857f1 fix
2026-07-18 19:41:33 +03:30

8 lines
4.8 KiB
JavaScript

const d={file:"#new-image-file",image:"#new-image-field",thumb:"#new-thumb-field",preview:"#new-image-preview",status:"#upload-status",button:"#new-upload-btn"},u=(e,t)=>{const n=document.querySelector(d.status);n&&(n.textContent=t,n.className="text-xs rounded-lg px-2.5 py-1 "+(e==="busy"?"bg-gray-100 text-gray-600":e==="ok"?"bg-green-50 text-green-700":e==="err"?"bg-red-50 text-brand":"text-gray-400"))},m=e=>{const t=document.querySelector(d.preview);t&&(t.innerHTML=e?`<img src="${e}" alt="" class="w-full h-full object-cover" />`:'<span class="text-gray-400 text-sm">تصویری انتخاب نشده</span>')},l={async uploadImage(e){const t=new FormData;t.append("image",e);const n=await fetch("/api/admin/upload",{method:"POST",body:t}),r=await n.json().catch(()=>({error:"پاسخ نامعتبر سرور"}));if(!n.ok||r.error)throw new Error(r.error??"آپلود ناموفق بود");return r},async runTassAgent(e=3){const t=await fetch("/api/agents/tass",{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({limit:e})});if(!t.ok)throw new Error("خطا در اجرای agent");return t.json()},initUpload(){const e=document.querySelector(d.file);if(!e)return;const t=document.querySelector(d.image),n=document.querySelector(d.thumb),r=document.querySelector(d.button),c=()=>{const a=t?.value??"";document.querySelectorAll("[data-insert-image]").forEach(o=>{o.dataset.insertImage=a,o.disabled=!a})},i=(a,o)=>{t&&(t.value=a),n&&(n.value=o),m(o||a),c()},s=async a=>{u("busy","در حال آپلود و پردازش..."),r&&(r.disabled=!0);try{const o=await l.uploadImage(a);i(o.image,o.thumbnail),u("ok","تصویر آپلود شد و آماده استفاده است.")}catch(o){u("err",o instanceof Error?o.message:"آپلود ناموفق بود.")}finally{r&&(r.disabled=!1)}};e.addEventListener("change",()=>{const a=e.files?.[0];a&&s(a)}),r?.addEventListener("click",()=>{const a=e.files?.[0];a?s(a):u("err","ابتدا یک فایل انتخاب کنید.")}),c()},initEditor(){const e=document.querySelector("#post-body-editor");if(!e)return;const t=n=>{const r=e.selectionStart,c=e.selectionEnd,i=e.value.slice(0,r),s=e.value.slice(c),a=i.endsWith(`
`)||i.length===0?"":`
`,o=s.startsWith(`
`)||s.length===0?"":`
`;e.value=`${i}${a}${n}${o}${s}`,e.focus();const g=i.length+a.length+n.length;e.setSelectionRange(g,g)};document.querySelectorAll("[data-insert-image]").forEach(n=>{n.addEventListener("click",()=>{const r=n.dataset.insertImage;r&&t(`[image:${r}|توضیح تصویر]`)})}),document.querySelectorAll("[data-editor-command]").forEach(n=>{n.addEventListener("click",()=>{const r=n.dataset.editorCommand;r==="heading"&&t("## تیتر میانی"),r==="quote"&&t("> متن نقل‌قول"),r==="separator"&&t("---")})})},initConfirm(){document.querySelectorAll("[data-confirm-form]").forEach(e=>{e.addEventListener("submit",t=>{const n=e.dataset.confirmMessage||"برای تایید حذف عبارت confirm را تایپ کنید.",r=window.prompt(n);if(r!=="confirm"){t.preventDefault(),window.alert("حذف لغو شد. عبارت واردشده باید دقیقاً confirm باشد.");return}const c=e.querySelector("[data-confirm-input]");c&&(c.value=r)})})},initTass(){const e=document.querySelector("#run-tass-agent"),t=document.querySelector("#tass-agent-result");!e||!t||e.addEventListener("click",async()=>{e.disabled=!0,e.textContent="در حال اجرا...",t.classList.remove("hidden"),t.textContent="در حال دریافت و ترجمه خبرها...";try{const n=await l.runTassAgent(3);t.textContent=JSON.stringify(n,null,2)}catch(n){t.textContent=n instanceof Error?n.message:"خطا در اجرای agent"}finally{e.disabled=!1,e.textContent="اجرای agent و ساخت پست"}})},initTabs(){const t=document.querySelector("[data-active-tab]")?.dataset.activeTab??"pending",n=Array.from(document.querySelectorAll("[data-tab]")),r=Array.from(document.querySelectorAll("[data-panel]")),c=i=>{n.forEach(s=>{const a=s.dataset.tab===i;s.classList.toggle("bg-brand",a),s.classList.toggle("text-white",a),s.classList.toggle("border-brand",a),s.classList.toggle("bg-white",!a),s.classList.toggle("text-gray-700",!a),s.classList.toggle("border-gray-200",!a),a?s.setAttribute("aria-current","page"):s.removeAttribute("aria-current")}),r.forEach(s=>{s.classList.toggle("hidden",s.dataset.panel!==i)})};c(t),n.forEach(i=>{i.addEventListener("click",s=>{const a=i.dataset.tab;if(!a)return;s.preventDefault(),c(a);const o=new URL(window.location.href);o.searchParams.set("tab",a),o.searchParams.delete("pendingPage"),o.searchParams.delete("approvedPage"),o.searchParams.delete("edit"),window.history.pushState({},"",o)})}),window.addEventListener("popstate",()=>{const i=new URLSearchParams(window.location.search);c(i.get("tab")||t)})}};l.initTabs();l.initUpload();l.initEditor();l.initConfirm();l.initTass();