WordPress/wp-includes/blocks/image/view.min.js

1 line
5.4 KiB
JavaScript
Raw Normal View History

import*as t from"@wordpress/interactivity";var e={d:(t,n)=>{for(var i in n)e.o(n,i)&&!e.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:n[i]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)};const n=(t=>{var n={};return e.d(n,t),n})({getContext:()=>t.getContext,getElement:()=>t.getElement,store:()=>t.store}),i=["a[href]","area[href]",'input:not([disabled]):not([type="hidden"]):not([aria-hidden])',"select:not([disabled]):not([aria-hidden])","textarea:not([disabled]):not([aria-hidden])","button:not([disabled]):not([aria-hidden])","iframe","object","embed","[contenteditable]",'[tabindex]:not([tabindex^="-"])'];let o,l=!1,a=0;function r(t){!l&&Date.now()-a>450&&window.scrollTo(t.scrollLeftReset,t.scrollTopReset)}const{state:d,actions:s,callbacks:g}=(0,n.store)("core/image",{state:{windowWidth:window.innerWidth,windowHeight:window.innerHeight,get roleAttribute(){return(0,n.getContext)().lightboxEnabled?"dialog":null},get ariaModal(){return(0,n.getContext)().lightboxEnabled?"true":null},get dialogLabel(){const t=(0,n.getContext)();return t.lightboxEnabled?t.dialogLabel:null},get lightboxObjectFit(){if((0,n.getContext)().initialized)return"cover"},get enlargedImgSrc(){const t=(0,n.getContext)();return t.initialized?t.imageUploadedSrc:"data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs="}},actions:{showLightbox(t){const e=(0,n.getContext)();e.imageLoaded&&(e.initialized=!0,e.lastFocusedElement=window.document.activeElement,e.scrollDelta=0,e.pointerType=t.pointerType,e.lightboxEnabled=!0,c(e,e.imageRef),e.scrollTopReset=window.pageYOffset||document.documentElement.scrollTop,e.scrollLeftReset=window.pageXOffset||document.documentElement.scrollLeft,o=r.bind(null,e),window.addEventListener("scroll",o,!1))},hideLightbox(){const t=(0,n.getContext)();t.hideAnimationEnabled=!0,t.lightboxEnabled&&(setTimeout((function(){window.removeEventListener("scroll",o),t.lightboxTriggerRef.focus({preventScroll:!0})}),450),t.lightboxEnabled=!1)},handleKeydown(t){const e=(0,n.getContext)();e.lightboxEnabled&&("Tab"!==t.key&&9!==t.keyCode||(t.shiftKey&&window.document.activeElement===e.firstFocusableElement?(t.preventDefault(),e.lastFocusableElement.focus()):t.shiftKey||window.document.activeElement!==e.lastFocusableElement||(t.preventDefault(),e.firstFocusableElement.focus())),"Escape"!==t.key&&27!==t.keyCode||s.hideLightbox(t))},handleLoad(){const t=(0,n.getContext)(),{ref:e}=(0,n.getElement)();t.imageLoaded=!0,t.imageCurrentSrc=e.currentSrc,g.setButtonStyles()},handleTouchStart(){l=!0},handleTouchMove(t){(0,n.getContext)().lightboxEnabled&&t.preventDefault()},handleTouchEnd(){a=Date.now(),l=!1}},callbacks:{initOriginImage(){const t=(0,n.getContext)(),{ref:e}=(0,n.getElement)();t.imageRef=e,e.complete&&(t.imageLoaded=!0,t.imageCurrentSrc=e.currentSrc)},initTriggerButton(){const t=(0,n.getContext)(),{ref:e}=(0,n.getElement)();t.lightboxTriggerRef=e},initLightbox(){const t=(0,n.getContext)(),{ref:e}=(0,n.getElement)();if(t.lightboxEnabled){const n=e.querySelectorAll(i);t.firstFocusableElement=n[0],t.lastFocusableElement=n[n.length-1],e.focus()}},setButtonStyles(){const{ref:t}=(0,n.getElement)(),{naturalWidth:e,naturalHeight:i,offsetWidth:o,offsetHeight:l}=t;if(0===e||0===i)return;const a=t.parentElement,r=t.parentElement.clientWidth;let d=t.parentElement.clientHeight;const s=a.querySelector("figcaption");if(s){const t=window.getComputedStyle(s);["absolute","fixed"].includes(t.position)||(d=d-s.offsetHeight-parseFloat(t.marginTop)-parseFloat(t.marginBottom))}const g=d-l,c=r-o,h=(0,n.getContext)();if("contain"===h.scaleAttr){const t=e/i;if(t>=o/l){const e=o/t;h.imageButtonTop=(l-e)/2+g+16,h.imageButtonRight=c+16}else{const e=l*t;h.imageButtonTop=g+16,h.imageButtonRight=(o-e)/2+c+16}}else h.imageButtonTop=g+16,h.imageButtonRight=c+16},setStylesOnResize(){const t=(0,n.getContext)(),{ref:e}=(0,n.getElement)();t.lightboxEnabled&&(d.windowWidth||d.windowHeight)&&c(t,e)}}});function c(t,e){let{naturalWidth:n,naturalHeight:i,offsetWidth:o,offsetHeight:l}=e,{x:a,y:r}=e.getBoundingClientRect();const d=n/i;let s=o/l;if("contain"===t.scaleAttr)if