You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

532 lines
20 KiB

1 year ago
  1. "use strict";
  2. (self["__WordPressPrivateInteractivityAPI__"] = self["__WordPressPrivateInteractivityAPI__"] || []).push([[354],{
  3. /***/ 699:
  4. /***/ (function(__unused_webpack_module, __unused_webpack___webpack_exports__, __webpack_require__) {
  5. /* harmony import */ var _wordpress_interactivity__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(754);
  6. /**
  7. * WordPress dependencies
  8. */
  9. const focusableSelectors = ['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^="-"])'];
  10. /*
  11. * Stores a context-bound scroll handler.
  12. *
  13. * This callback could be defined inline inside of the store
  14. * object but it's created externally to avoid confusion about
  15. * how its logic is called. This logic is not referenced directly
  16. * by the directives in the markup because the scroll event we
  17. * need to listen to is triggered on the window; so by defining it
  18. * outside of the store, we signal that the behavior here is different.
  19. * If we find a compelling reason to move it to the store, feel free.
  20. *
  21. * @type {Function}
  22. */
  23. let scrollCallback;
  24. /*
  25. * Tracks whether user is touching screen; used to
  26. * differentiate behavior for touch and mouse input.
  27. *
  28. * @type {boolean}
  29. */
  30. let isTouching = false;
  31. /*
  32. * Tracks the last time the screen was touched; used to
  33. * differentiate behavior for touch and mouse input.
  34. *
  35. * @type {number}
  36. */
  37. let lastTouchTime = 0;
  38. /*
  39. * Lightbox page-scroll handler: prevents scrolling.
  40. *
  41. * This handler is added to prevent scrolling behaviors that
  42. * trigger content shift while the lightbox is open.
  43. *
  44. * It would be better to accomplish this through CSS alone, but
  45. * using overflow: hidden is currently the only way to do so, and
  46. * that causes the layout to shift and prevents the zoom animation
  47. * from working in some cases because we're unable to account for
  48. * the layout shift when doing the animation calculations. Instead,
  49. * here we use JavaScript to prevent and reset the scrolling
  50. * behavior. In the future, we may be able to use CSS or overflow: hidden
  51. * instead to not rely on JavaScript, but this seems to be the best approach
  52. * for now that provides the best visual experience.
  53. *
  54. * @param {Object} context Interactivity page context?
  55. */
  56. function handleScroll(context) {
  57. // We can't override the scroll behavior on mobile devices
  58. // because doing so breaks the pinch to zoom functionality, and we
  59. // want to allow users to zoom in further on the high-res image.
  60. if (!isTouching && Date.now() - lastTouchTime > 450) {
  61. // We are unable to use event.preventDefault() to prevent scrolling
  62. // because the scroll event can't be canceled, so we reset the position instead.
  63. window.scrollTo(context.core.image.scrollLeftReset, context.core.image.scrollTopReset);
  64. }
  65. }
  66. (0,_wordpress_interactivity__WEBPACK_IMPORTED_MODULE_0__/* .store */ .h)({
  67. state: {
  68. core: {
  69. image: {
  70. windowWidth: window.innerWidth,
  71. windowHeight: window.innerHeight
  72. }
  73. }
  74. },
  75. actions: {
  76. core: {
  77. image: {
  78. showLightbox: ({
  79. context,
  80. event
  81. }) => {
  82. // We can't initialize the lightbox until the reference
  83. // image is loaded, otherwise the UX is broken.
  84. if (!context.core.image.imageLoaded) {
  85. return;
  86. }
  87. context.core.image.initialized = true;
  88. context.core.image.lastFocusedElement = window.document.activeElement;
  89. context.core.image.scrollDelta = 0;
  90. context.core.image.pointerType = event.pointerType;
  91. context.core.image.lightboxEnabled = true;
  92. setStyles(context, context.core.image.imageRef);
  93. context.core.image.scrollTopReset = window.pageYOffset || document.documentElement.scrollTop;
  94. // In most cases, this value will be 0, but this is included
  95. // in case a user has created a page with horizontal scrolling.
  96. context.core.image.scrollLeftReset = window.pageXOffset || document.documentElement.scrollLeft;
  97. // We define and bind the scroll callback here so
  98. // that we can pass the context and as an argument.
  99. // We may be able to change this in the future if we
  100. // define the scroll callback in the store instead, but
  101. // this approach seems to tbe clearest for now.
  102. scrollCallback = handleScroll.bind(null, context);
  103. // We need to add a scroll event listener to the window
  104. // here because we are unable to otherwise access it via
  105. // the Interactivity API directives. If we add a native way
  106. // to access the window, we can remove this.
  107. window.addEventListener('scroll', scrollCallback, false);
  108. },
  109. hideLightbox: async ({
  110. context
  111. }) => {
  112. context.core.image.hideAnimationEnabled = true;
  113. if (context.core.image.lightboxEnabled) {
  114. // We want to wait until the close animation is completed
  115. // before allowing a user to scroll again. The duration of this
  116. // animation is defined in the styles.scss and depends on if the
  117. // animation is 'zoom' or 'fade', but in any case we should wait
  118. // a few milliseconds longer than the duration, otherwise a user
  119. // may scroll too soon and cause the animation to look sloppy.
  120. setTimeout(function () {
  121. window.removeEventListener('scroll', scrollCallback);
  122. // If we don't delay before changing the focus,
  123. // the focus ring will appear on Firefox before
  124. // the image has finished animating, which looks broken.
  125. context.core.image.lightboxTriggerRef.focus({
  126. preventScroll: true
  127. });
  128. }, 450);
  129. context.core.image.lightboxEnabled = false;
  130. }
  131. },
  132. handleKeydown: ({
  133. context,
  134. actions,
  135. event
  136. }) => {
  137. if (context.core.image.lightboxEnabled) {
  138. if (event.key === 'Tab' || event.keyCode === 9) {
  139. // If shift + tab it change the direction
  140. if (event.shiftKey && window.document.activeElement === context.core.image.firstFocusableElement) {
  141. event.preventDefault();
  142. context.core.image.lastFocusableElement.focus();
  143. } else if (!event.shiftKey && window.document.activeElement === context.core.image.lastFocusableElement) {
  144. event.preventDefault();
  145. context.core.image.firstFocusableElement.focus();
  146. }
  147. }
  148. if (event.key === 'Escape' || event.keyCode === 27) {
  149. actions.core.image.hideLightbox({
  150. context,
  151. event
  152. });
  153. }
  154. }
  155. },
  156. // This is fired just by lazily loaded
  157. // images on the page, not all images.
  158. handleLoad: ({
  159. context,
  160. effects,
  161. ref
  162. }) => {
  163. context.core.image.imageLoaded = true;
  164. context.core.image.imageCurrentSrc = ref.currentSrc;
  165. effects.core.image.setButtonStyles({
  166. context,
  167. ref
  168. });
  169. },
  170. handleTouchStart: () => {
  171. isTouching = true;
  172. },
  173. handleTouchMove: ({
  174. context,
  175. event
  176. }) => {
  177. // On mobile devices, we want to prevent triggering the
  178. // scroll event because otherwise the page jumps around as
  179. // we reset the scroll position. This also means that closing
  180. // the lightbox requires that a user perform a simple tap. This
  181. // may be changed in the future if we find a better alternative
  182. // to override or reset the scroll position during swipe actions.
  183. if (context.core.image.lightboxEnabled) {
  184. event.preventDefault();
  185. }
  186. },
  187. handleTouchEnd: () => {
  188. // We need to wait a few milliseconds before resetting
  189. // to ensure that pinch to zoom works consistently
  190. // on mobile devices when the lightbox is open.
  191. lastTouchTime = Date.now();
  192. isTouching = false;
  193. }
  194. }
  195. }
  196. },
  197. selectors: {
  198. core: {
  199. image: {
  200. roleAttribute: ({
  201. context
  202. }) => {
  203. return context.core.image.lightboxEnabled ? 'dialog' : null;
  204. },
  205. ariaModal: ({
  206. context
  207. }) => {
  208. return context.core.image.lightboxEnabled ? 'true' : null;
  209. },
  210. dialogLabel: ({
  211. context
  212. }) => {
  213. return context.core.image.lightboxEnabled ? context.core.image.dialogLabel : null;
  214. },
  215. lightboxObjectFit: ({
  216. context
  217. }) => {
  218. if (context.core.image.initialized) {
  219. return 'cover';
  220. }
  221. },
  222. enlargedImgSrc: ({
  223. context
  224. }) => {
  225. return context.core.image.initialized ? context.core.image.imageUploadedSrc : 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=';
  226. }
  227. }
  228. }
  229. },
  230. effects: {
  231. core: {
  232. image: {
  233. initOriginImage: ({
  234. context,
  235. ref
  236. }) => {
  237. context.core.image.imageRef = ref;
  238. context.core.image.lightboxTriggerRef = ref.parentElement.querySelector('.lightbox-trigger');
  239. if (ref.complete) {
  240. context.core.image.imageLoaded = true;
  241. context.core.image.imageCurrentSrc = ref.currentSrc;
  242. }
  243. },
  244. initLightbox: async ({
  245. context,
  246. ref
  247. }) => {
  248. if (context.core.image.lightboxEnabled) {
  249. const focusableElements = ref.querySelectorAll(focusableSelectors);
  250. context.core.image.firstFocusableElement = focusableElements[0];
  251. context.core.image.lastFocusableElement = focusableElements[focusableElements.length - 1];
  252. // Move focus to the dialog when opening it.
  253. ref.focus();
  254. }
  255. },
  256. setButtonStyles: ({
  257. context,
  258. ref
  259. }) => {
  260. const {
  261. naturalWidth,
  262. naturalHeight,
  263. offsetWidth,
  264. offsetHeight
  265. } = ref;
  266. // If the image isn't loaded yet, we can't
  267. // calculate where the button should be.
  268. if (naturalWidth === 0 || naturalHeight === 0) {
  269. return;
  270. }
  271. const figure = ref.parentElement;
  272. const figureWidth = ref.parentElement.clientWidth;
  273. // We need special handling for the height because
  274. // a caption will cause the figure to be taller than
  275. // the image, which means we need to account for that
  276. // when calculating the placement of the button in the
  277. // top right corner of the image.
  278. let figureHeight = ref.parentElement.clientHeight;
  279. const caption = figure.querySelector('figcaption');
  280. if (caption) {
  281. const captionComputedStyle = window.getComputedStyle(caption);
  282. figureHeight = figureHeight - caption.offsetHeight - parseFloat(captionComputedStyle.marginTop) - parseFloat(captionComputedStyle.marginBottom);
  283. }
  284. const buttonOffsetTop = figureHeight - offsetHeight;
  285. const buttonOffsetRight = figureWidth - offsetWidth;
  286. // In the case of an image with object-fit: contain, the
  287. // size of the <img> element can be larger than the image itself,
  288. // so we need to calculate where to place the button.
  289. if (context.core.image.scaleAttr === 'contain') {
  290. // Natural ratio of the image.
  291. const naturalRatio = naturalWidth / naturalHeight;
  292. // Offset ratio of the image.
  293. const offsetRatio = offsetWidth / offsetHeight;
  294. if (naturalRatio >= offsetRatio) {
  295. // If it reaches the width first, keep
  296. // the width and compute the height.
  297. const referenceHeight = offsetWidth / naturalRatio;
  298. context.core.image.imageButtonTop = (offsetHeight - referenceHeight) / 2 + buttonOffsetTop + 16;
  299. context.core.image.imageButtonRight = buttonOffsetRight + 16;
  300. } else {
  301. // If it reaches the height first, keep
  302. // the height and compute the width.
  303. const referenceWidth = offsetHeight * naturalRatio;
  304. context.core.image.imageButtonTop = buttonOffsetTop + 16;
  305. context.core.image.imageButtonRight = (offsetWidth - referenceWidth) / 2 + buttonOffsetRight + 16;
  306. }
  307. } else {
  308. context.core.image.imageButtonTop = buttonOffsetTop + 16;
  309. context.core.image.imageButtonRight = buttonOffsetRight + 16;
  310. }
  311. },
  312. setStylesOnResize: ({
  313. state,
  314. context,
  315. ref
  316. }) => {
  317. if (context.core.image.lightboxEnabled && (state.core.image.windowWidth || state.core.image.windowHeight)) {
  318. setStyles(context, ref);
  319. }
  320. }
  321. }
  322. }
  323. }
  324. }, {
  325. afterLoad: ({
  326. state
  327. }) => {
  328. window.addEventListener('resize', debounce(() => {
  329. state.core.image.windowWidth = window.innerWidth;
  330. state.core.image.windowHeight = window.innerHeight;
  331. }));
  332. }
  333. });
  334. /*
  335. * Computes styles for the lightbox and adds them to the document.
  336. *
  337. * @function
  338. * @param {Object} context - An Interactivity API context
  339. * @param {Object} event - A triggering event
  340. */
  341. function setStyles(context, ref) {
  342. // The reference img element lies adjacent
  343. // to the event target button in the DOM.
  344. let {
  345. naturalWidth,
  346. naturalHeight,
  347. offsetWidth: originalWidth,
  348. offsetHeight: originalHeight
  349. } = ref;
  350. let {
  351. x: screenPosX,
  352. y: screenPosY
  353. } = ref.getBoundingClientRect();
  354. // Natural ratio of the image clicked to open the lightbox.
  355. const naturalRatio = naturalWidth / naturalHeight;
  356. // Original ratio of the image clicked to open the lightbox.
  357. let originalRatio = originalWidth / originalHeight;
  358. // If it has object-fit: contain, recalculate the original sizes
  359. // and the screen position without the blank spaces.
  360. if (context.core.image.scaleAttr === 'contain') {
  361. if (naturalRatio > originalRatio) {
  362. const heightWithoutSpace = originalWidth / naturalRatio;
  363. // Recalculate screen position without the top space.
  364. screenPosY += (originalHeight - heightWithoutSpace) / 2;
  365. originalHeight = heightWithoutSpace;
  366. } else {
  367. const widthWithoutSpace = originalHeight * naturalRatio;
  368. // Recalculate screen position without the left space.
  369. screenPosX += (originalWidth - widthWithoutSpace) / 2;
  370. originalWidth = widthWithoutSpace;
  371. }
  372. }
  373. originalRatio = originalWidth / originalHeight;
  374. // Typically, we use the image's full-sized dimensions. If those
  375. // dimensions have not been set (i.e. an external image with only one size),
  376. // the image's dimensions in the lightbox are the same
  377. // as those of the image in the content.
  378. let imgMaxWidth = parseFloat(context.core.image.targetWidth !== 'none' ? context.core.image.targetWidth : naturalWidth);
  379. let imgMaxHeight = parseFloat(context.core.image.targetHeight !== 'none' ? context.core.image.targetHeight : naturalHeight);
  380. // Ratio of the biggest image stored in the database.
  381. let imgRatio = imgMaxWidth / imgMaxHeight;
  382. let containerMaxWidth = imgMaxWidth;
  383. let containerMaxHeight = imgMaxHeight;
  384. let containerWidth = imgMaxWidth;
  385. let containerHeight = imgMaxHeight;
  386. // Check if the target image has a different ratio than the original one (thumbnail).
  387. // Recalculate the width and height.
  388. if (naturalRatio.toFixed(2) !== imgRatio.toFixed(2)) {
  389. if (naturalRatio > imgRatio) {
  390. // If the width is reached before the height, we keep the maxWidth
  391. // and recalculate the height.
  392. // Unless the difference between the maxHeight and the reducedHeight
  393. // is higher than the maxWidth, where we keep the reducedHeight and
  394. // recalculate the width.
  395. const reducedHeight = imgMaxWidth / naturalRatio;
  396. if (imgMaxHeight - reducedHeight > imgMaxWidth) {
  397. imgMaxHeight = reducedHeight;
  398. imgMaxWidth = reducedHeight * naturalRatio;
  399. } else {
  400. imgMaxHeight = imgMaxWidth / naturalRatio;
  401. }
  402. } else {
  403. // If the height is reached before the width, we keep the maxHeight
  404. // and recalculate the width.
  405. // Unless the difference between the maxWidth and the reducedWidth
  406. // is higher than the maxHeight, where we keep the reducedWidth and
  407. // recalculate the height.
  408. const reducedWidth = imgMaxHeight * naturalRatio;
  409. if (imgMaxWidth - reducedWidth > imgMaxHeight) {
  410. imgMaxWidth = reducedWidth;
  411. imgMaxHeight = reducedWidth / naturalRatio;
  412. } else {
  413. imgMaxWidth = imgMaxHeight * naturalRatio;
  414. }
  415. }
  416. containerWidth = imgMaxWidth;
  417. containerHeight = imgMaxHeight;
  418. imgRatio = imgMaxWidth / imgMaxHeight;
  419. // Calculate the max size of the container.
  420. if (originalRatio > imgRatio) {
  421. containerMaxWidth = imgMaxWidth;
  422. containerMaxHeight = containerMaxWidth / originalRatio;
  423. } else {
  424. containerMaxHeight = imgMaxHeight;
  425. containerMaxWidth = containerMaxHeight * originalRatio;
  426. }
  427. }
  428. // If the image has been pixelated on purpose, keep that size.
  429. if (originalWidth > containerWidth || originalHeight > containerHeight) {
  430. containerWidth = originalWidth;
  431. containerHeight = originalHeight;
  432. }
  433. // Calculate the final lightbox image size and the
  434. // scale factor. MaxWidth is either the window container
  435. // (accounting for padding) or the image resolution.
  436. let horizontalPadding = 0;
  437. if (window.innerWidth > 480) {
  438. horizontalPadding = 80;
  439. } else if (window.innerWidth > 1920) {
  440. horizontalPadding = 160;
  441. }
  442. const verticalPadding = 80;
  443. const targetMaxWidth = Math.min(window.innerWidth - horizontalPadding, containerWidth);
  444. const targetMaxHeight = Math.min(window.innerHeight - verticalPadding, containerHeight);
  445. const targetContainerRatio = targetMaxWidth / targetMaxHeight;
  446. if (originalRatio > targetContainerRatio) {
  447. // If targetMaxWidth is reached before targetMaxHeight
  448. containerWidth = targetMaxWidth;
  449. containerHeight = containerWidth / originalRatio;
  450. } else {
  451. // If targetMaxHeight is reached before targetMaxWidth
  452. containerHeight = targetMaxHeight;
  453. containerWidth = containerHeight * originalRatio;
  454. }
  455. const containerScale = originalWidth / containerWidth;
  456. const lightboxImgWidth = imgMaxWidth * (containerWidth / containerMaxWidth);
  457. const lightboxImgHeight = imgMaxHeight * (containerHeight / containerMaxHeight);
  458. // Add the CSS variables needed.
  459. let styleTag = document.getElementById('wp-lightbox-styles');
  460. if (!styleTag) {
  461. styleTag = document.createElement('style');
  462. styleTag.id = 'wp-lightbox-styles';
  463. document.head.appendChild(styleTag);
  464. }
  465. // As of this writing, using the calculations above will render the lightbox
  466. // with a small, erroneous whitespace on the left side of the image in iOS Safari,
  467. // perhaps due to an inconsistency in how browsers handle absolute positioning and CSS
  468. // transformation. In any case, adding 1 pixel to the container width and height solves
  469. // the problem, though this can be removed if the issue is fixed in the future.
  470. styleTag.innerHTML = `
  471. :root {
  472. --wp--lightbox-initial-top-position: ${screenPosY}px;
  473. --wp--lightbox-initial-left-position: ${screenPosX}px;
  474. --wp--lightbox-container-width: ${containerWidth + 1}px;
  475. --wp--lightbox-container-height: ${containerHeight + 1}px;
  476. --wp--lightbox-image-width: ${lightboxImgWidth}px;
  477. --wp--lightbox-image-height: ${lightboxImgHeight}px;
  478. --wp--lightbox-scale: ${containerScale};
  479. }
  480. `;
  481. }
  482. /*
  483. * Debounces a function call.
  484. *
  485. * @function
  486. * @param {Function} func - A function to be called
  487. * @param {number} wait - The time to wait before calling the function
  488. */
  489. function debounce(func, wait = 50) {
  490. let timeout;
  491. return () => {
  492. const later = () => {
  493. timeout = null;
  494. func();
  495. };
  496. clearTimeout(timeout);
  497. timeout = setTimeout(later, wait);
  498. };
  499. }
  500. /***/ })
  501. },
  502. /******/ function(__webpack_require__) { // webpackRuntimeModules
  503. /******/ var __webpack_exec__ = function(moduleId) { return __webpack_require__(__webpack_require__.s = moduleId); }
  504. /******/ var __webpack_exports__ = (__webpack_exec__(699));
  505. /******/ }
  506. ]);