1 line
6.5 KiB
JavaScript
1 line
6.5 KiB
JavaScript
import*as t from"@wordpress/interactivity";const e=t,o=e.withSyncEvent??(t=>t);!function(){const{actions:t,callbacks:n}=(0,e.store)("activitypub/reactions",{actions:{openModal(t){const o=(0,e.getContext)();if(o.modal.isOpen=!0,o.modal.isCompact)setTimeout(n.positionModal,0);else{const t=document.getElementById(o.blockId);if(t){const e=t.querySelector(".activitypub-modal__overlay");e&&["top","left","right","bottom"].forEach(t=>{e.style.removeProperty(t)})}setTimeout(()=>{if(t){const e=t.querySelector(".activitypub-modal__frame");e&&n.trapFocus(e)}},50)}"function"==typeof n.onModalOpen&&n.onModalOpen(t)},closeModal(t){const o=(0,e.getContext)();o.modal.isOpen=!1;const r=(0,e.getElement)();if("actions.toggleModal"===r.ref.dataset["wpOn-Click"])r.ref.focus();else{const t=document.getElementById(o.blockId);if(t){const e=t.querySelector('[data-wp-on--click="actions.toggleModal"]');e&&e.focus()}}"function"==typeof n.onModalClose&&n.onModalClose(t)},toggleModal:o(o=>{o?.preventDefault?.();const{modal:n}=(0,e.getContext)();n.isOpen?t.closeModal(o):t.openModal(o)})},callbacks:{_abortController:null,handleModalEffects(){const{modal:t}=(0,e.getContext)();if(t.isOpen&&!t.isCompact?document.body.classList.add("modal-open"):document.body.classList.remove("modal-open"),n._abortController&&(n._abortController.abort(),n._abortController=null),t.isOpen){n._abortController=new AbortController;const{signal:t}=n._abortController;document.addEventListener("keydown",n.documentKeydown,{signal:t}),document.addEventListener("click",n.documentClick,{signal:t})}},documentKeydown(o){const{modal:n}=(0,e.getContext)();n.isOpen&&"Escape"===o.key&&t.closeModal()},documentClick(o){const{blockId:n,modal:r}=(0,e.getContext)();if(!r.isOpen)return;const a=document.getElementById(n);if(!a)return;const s=a.querySelectorAll('[data-wp-on--click="actions.toggleModal"]');for(const t of s)if(t===o.target||t.contains(o.target))return;const i=a.querySelector(".activitypub-modal__frame");i&&!i.contains(o.target)&&t.closeModal()},positionModal(){const{blockId:t}=(0,e.getContext)(),o=document.getElementById(t);if(!o)return;const n=o.querySelector(".activitypub-modal__overlay");if(!n)return;n.style.top="",n.style.left="",n.style.right="",n.style.bottom="";const r=(0,e.getElement)().ref.getBoundingClientRect(),a=window.innerWidth,s=o.getBoundingClientRect(),i={top:r.bottom-s.top+8+"px",left:r.left-s.left-2+"px"};a-r.right<250&&(i.left="auto",i.right=s.right-r.right+"px"),Object.assign(n.style,i)},trapFocus(t){const e=t.querySelectorAll('a[href]:not([disabled]), button:not([disabled]), textarea:not([disabled]), input[type="text"]:not([disabled]):not([readonly]), input[type="radio"]:not([disabled]), input[type="checkbox"]:not([disabled]), select:not([disabled])'),o=e[0],n=e[e.length-1];o&&o.classList.contains("activitypub-modal__close")&&e.length>1?e[1].focus():o.focus(),t.addEventListener("keydown",function(e){if("Tab"!==e.key&&9!==e.keyCode)return;const r=t.ownerDocument.activeElement;e.shiftKey?r===o&&(n.focus(),e.preventDefault()):r===n&&(o.focus(),e.preventDefault())})}}})}();const n={like:"intentLabelLike",announce:"intentLabelAnnounce"},r="fediverse-remote-user",{actions:a,callbacks:s,state:i}=(0,e.store)("activitypub/reactions",{actions:{async fetchReactions(){const t=(0,e.getContext)();if(!t.postId)return;const{namespace:o}=(0,e.getConfig)(),{apiFetch:n}=window.wp;try{t.reactions=await n({path:`/${o}/posts/${t.postId}/reactions`})}catch(t){console.error("Error fetching reactions:",t)}},openIntentModal(){const t=(0,e.getContext)(),o=(0,e.getElement)().ref.dataset.intent;t.modal.isCompact=!1,t.modal.intent=o;const{i18n:r}=(0,e.getConfig)();t.modal.title=r[n[o]]||o;const{profileURL:i}=s.getStore();i&&(t.remoteProfile=i),a.openModal()},updateIntentProfile(t){const o=(0,e.getContext)();o.remoteProfile=t.target.value,o.isError=!1,o.errorMessage=""},onIntentKeydown(t){"Enter"===t.key&&(t.preventDefault(),a.submitIntent())},*submitIntent(){const t=(0,e.getContext)(),{namespace:o,i18n:n}=(0,e.getConfig)(),{apiFetch:r}=window.wp,i=t.remoteProfile.trim();if(!i)return t.isError=!0,void(t.errorMessage=n.emptyProfileError);if(!s.isHandle(i)&&!s.isUrl(i))return t.isError=!0,void(t.errorMessage=n.invalidProfileError);t.isLoading=!0,t.isError=!1,t.errorMessage="";const l=`/${o}/posts/${t.postId}/remote-intent?resource=${encodeURIComponent(i)}&intent=${encodeURIComponent(t.modal.intent)}`;try{const e=yield r({path:l});t.isLoading=!1,window.open(e.url,"_blank","noopener,noreferrer"),a.closeModal(),t.shouldSaveProfile&&s.setStore({profileURL:i})}catch(e){console.error("Error submitting intent:",e),t.isLoading=!1,t.isError=!0,t.errorMessage=e.message||n.genericError}},copyPostUrl(){const t=(0,e.getContext)(),{i18n:o}=(0,e.getConfig)();navigator.clipboard.writeText(t.postUrl).then(()=>{t.copyButtonText=o.copied,setTimeout(()=>{t.copyButtonText=o.copy},1e3)},t=>{console.error("Could not copy text: ",t)})},toggleRememberProfile(){const t=(0,e.getContext)();t.shouldSaveProfile=!t.shouldSaveProfile}},callbacks:{initReactions(){const t=new ResizeObserver((0,e.withScope)(s.calculateVisibleAvatars));return(0,e.getElement)().ref.querySelectorAll(".reaction-group").forEach(e=>{t.observe(e)}),()=>{t.disconnect()}},calculateVisibleAvatars(){const{postId:t}=(0,e.getContext)();(i.reactions&&i.reactions[t]?Object.keys(i.reactions[t]):[]).forEach(o=>{i.reactions?.[t][o]?.items?.length&&(0,e.getElement)().ref.querySelectorAll(`.reaction-group[data-reaction-type="${o}"]`).forEach(e=>{const n=e.querySelector(".reaction-label").offsetWidth||0,r=e.querySelector(".reaction-action-button"),a=r?r.offsetWidth+12:0,s=e.offsetWidth-n-a-12;let l=1;s>32&&(l+=Math.floor((s-32)/22));const c=i.reactions[t][o].items,d=Math.min(l,c.length),u=e.querySelector(".reaction-avatars");u&&u.querySelectorAll("li").forEach((t,e)=>{e<d?t.removeAttribute("hidden"):t.setAttribute("hidden","hidden")})})})},setDefaultAvatar(t){t.target.src=(0,e.getConfig)().defaultAvatarUrl},onModalOpen(){const t=(0,e.getContext)();if(t.modal.isCompact){const o=(0,e.getElement)().ref.dataset.reactionType;t.modal.items=i.reactions[t.postId][o].items}},onModalClose(){const t=(0,e.getContext)();t.modal.isCompact=!0,t.modal.intent="",t.modal.title="",t.isError=!1,t.errorMessage=""},getStore(){const t=localStorage.getItem(r);if(!t)return{};try{return JSON.parse(t)}catch(t){return localStorage.removeItem(r),{}}},setStore(t){localStorage.setItem(r,JSON.stringify(t))},isHandle(t){const e=t.replace(/^@/,"").split("@");return 2===e.length&&s.isUrl(`https://${e[1]}`)},isUrl(t){try{return new URL(t),!0}catch(t){return!1}}}}); |