Files
laipower/wp-content/plugins/activitypub/build/follow-me/view.js

1 line
6.1 KiB
JavaScript

import*as t from"@wordpress/interactivity";const e=t,o=e.withSyncEvent??(t=>t),n={computedStyles:null,variables:{}};function r(t){if("undefined"==typeof window||!window.getComputedStyle)return!1;if(n.variables.hasOwnProperty(t))return n.variables[t];n.computedStyles||(n.computedStyles=window.getComputedStyle(document.documentElement));const e=n.computedStyles.getPropertyValue(t).trim();return n.variables[t]=""!==e,n.variables[t]}function l(t){if("string"!=typeof t)return null;if(t.match(/^#/))return t.substring(0,7);const[,,e]=t.split("|"),o=`--wp--preset--color--${e}`;return r(o)?`var(${o})`:null}function c(t,e,o=null,n=""){return o?`${t}${n} { ${e}: ${o}; }\n`:""}function i(t,e,o,n){return c(t,"background-color",e)+c(t,"color",o)+c(t,"background-color",n,":hover")+c(t,"background-color",n,":focus")}!function(){const{actions:t,callbacks:n}=(0,e.store)("activitypub/follow-me",{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 l=document.getElementById(n);if(!l)return;const c=l.querySelectorAll('[data-wp-on--click="actions.toggleModal"]');for(const t of c)if(t===o.target||t.contains(o.target))return;const i=l.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(),l=window.innerWidth,c=o.getBoundingClientRect(),i={top:r.bottom-c.top+8+"px",left:r.left-c.left-2+"px"};l-r.right<250&&(i.left="auto",i.right=c.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{actions:s,callbacks:a}=(0,e.store)("activitypub/follow-me",{actions:{copyToClipboard(){const t=(0,e.getContext)(),{i18n:o}=(0,e.getConfig)();navigator.clipboard.writeText(t.webfinger).then(()=>{t.copyButtonText=o.copied,setTimeout(()=>{t.copyButtonText=o.copy},1e3)},t=>{console.error("Could not copy text: ",t)})},updateRemoteProfile(t){const o=(0,e.getContext)();o.remoteProfile=t.target.value,o.isError=!1,o.errorMessage=""},onKeydown:o(t=>{"A"!==(0,e.getElement)().ref.tagName||"Enter"!==t.key&&" "!==t.key||(t.preventDefault(),s.toggleModal(t))}),handleKeyDown(t){"Enter"===t.key&&(t.preventDefault(),s.submitRemoteProfile())},*submitRemoteProfile(){const t=(0,e.getContext)(),{namespace:o,i18n:n}=(0,e.getConfig)(),{apiFetch:r}=window.wp,l=t.remoteProfile.trim();if(!l)return t.isError=!0,void(t.errorMessage=n.emptyProfileError);if(!a.isHandle(l))return t.isError=!0,void(t.errorMessage=n.invalidProfileError);t.isLoading=!0,t.isError=!1;const c=`/${o}/actors/${t.userId}/remote-follow?resource=${encodeURIComponent(l)}`;try{const e=yield r({path:c});t.isLoading=!1,window.open(e.url,"_blank"),s.closeModal(new Event("click"))}catch(e){console.error("Error submitting profile:",e),t.isLoading=!1,t.isError=!0,t.errorMessage=e.message||n.genericError}}},callbacks:{initButtonStyles:()=>{const{buttonStyle:t,backgroundColor:o,blockId:n}=(0,e.getContext)();if(n&&t){const e=document.createElement("style"),c=`#${n}`;e.textContent=function(t,e,o){const n=`${t} .wp-block-button__link`,c=function(t){if("string"==typeof t){const e=`--wp--preset--color--${t}`;return r(e)?`var(${e})`:null}return t?.color?.background||null}(o)||e?.color?.background;return i(n,l(e?.elements?.link?.color?.text),c,l(e?.elements?.link?.[":hover"]?.color?.text))}(c,t,o),document.head.appendChild(e);const s=document.createElement("style");s.textContent=function(t){const e=l(t?.elements?.link?.color?.text)||"var(--wp--preset--color--contrast, var(--wp--preset--color--foreground, #1e1e1e))";return i(".activitypub-dialog__button-group .wp-block-button__link",e,"var(--wp--preset--color--base, var(--wp--preset--color--background, #fff))",l(t?.elements?.link?.[":hover"]?.color?.text)||e)}(t),document.head.appendChild(s)}},isHandle(t){const e=t.replace(/^@/,"").split("@");return 2===e.length&&a.isUrl(`https://${e[1]}`)},isUrl(t){try{return new URL(t),!0}catch(t){return!1}},onModalClose(){(0,e.getContext)().isError=!1}}});