From 7f2c48b6b67a19652a6c876a612294551c3d0ba1 Mon Sep 17 00:00:00 2001
From: Pedro Figueroa <59029273+mardelnet@users.noreply.github.com>
Date: Thu, 31 Oct 2024 12:15:04 +0100
Subject: [PATCH] PLANET-7446: Remove lightbox (#1259)
PLANET-7446: Force no lightbox
- Remove lightbox from paragraphs by class name
---
assets/src/blocks/Gallery/GalleryFrontend.js | 30 +-
assets/src/components/Lightbox/Lightbox.js | 118 ----
.../Lightbox/setupLightboxForImages.js | 54 --
assets/src/components/Lightbox/useLightbox.js | 18 -
assets/src/frontendIndex.js | 3 -
assets/src/styles/lightbox.scss | 1 -
.../vendors/photoswipe/_main-settings.scss | 19 -
.../src/styles/vendors/photoswipe/main.scss | 189 ------
.../styles/vendors/photoswipe/p4-skin.scss | 601 ------------------
.../photoswipe/planet4-photoswipe.scss | 5 -
classes/class-loader.php | 7 -
.../vendors/photoswipe/default-skin.png | Bin 547 -> 0 bytes
.../vendors/photoswipe/default-skin.svg | 1 -
.../images/vendors/photoswipe/preloader.gif | Bin 866 -> 0 bytes
webpack.config.js | 1 -
15 files changed, 3 insertions(+), 1044 deletions(-)
delete mode 100644 assets/src/components/Lightbox/Lightbox.js
delete mode 100644 assets/src/components/Lightbox/setupLightboxForImages.js
delete mode 100644 assets/src/components/Lightbox/useLightbox.js
delete mode 100644 assets/src/styles/lightbox.scss
delete mode 100644 assets/src/styles/vendors/photoswipe/_main-settings.scss
delete mode 100644 assets/src/styles/vendors/photoswipe/main.scss
delete mode 100644 assets/src/styles/vendors/photoswipe/p4-skin.scss
delete mode 100644 assets/src/styles/vendors/photoswipe/planet4-photoswipe.scss
delete mode 100644 public/images/vendors/photoswipe/default-skin.png
delete mode 100644 public/images/vendors/photoswipe/default-skin.svg
delete mode 100644 public/images/vendors/photoswipe/preloader.gif
diff --git a/assets/src/blocks/Gallery/GalleryFrontend.js b/assets/src/blocks/Gallery/GalleryFrontend.js
index 57deb8569..32e47c3ca 100644
--- a/assets/src/blocks/Gallery/GalleryFrontend.js
+++ b/assets/src/blocks/Gallery/GalleryFrontend.js
@@ -3,34 +3,15 @@ import {GalleryCarousel} from './GalleryCarousel';
import {GalleryThreeColumns} from './GalleryThreeColumns';
import {GalleryGrid} from './GalleryGrid';
import {getGalleryLayout, GALLERY_BLOCK_CLASSES} from './getGalleryLayout';
-import {getCaptionWithCredits} from './getCaptionWithCredits.js';
-import {Lightbox} from '../../components/Lightbox/Lightbox';
-import {useLightbox} from '../../components/Lightbox/useLightbox';
-
-const imagesToItems = images => images.map(
- image => ({
- src: image.image_src,
- w: 0,
- h: 0,
- title: getCaptionWithCredits(image),
- })
-);
export const GalleryFrontend = ({
attributes = {},
- renderLightbox = false,
}) => {
const [images, setImages] = useState([]);
- const [items, setItems] = useState([]);
- const {isOpen, index, openLightbox, closeLightbox} = useLightbox();
const className = attributes.className ?? '';
const layout = getGalleryLayout(className, attributes.gallery_block_style ?? '');
const postType = document.body.getAttribute('data-post-type');
- useEffect(() => {
- setItems(imagesToItems(images));
- }, [images]);
-
useEffect(() => {
if (attributes.image_data.length && attributes.images.length) {
setImages(attributes.images);
@@ -54,16 +35,11 @@ export const GalleryFrontend = ({
{images.length ? (
<>
- {layout === 'slider' && }
- {layout === 'three-columns' && }
- {layout === 'grid' && }
+ {layout === 'slider' && }
+ {layout === 'three-columns' && }
+ {layout === 'grid' && }
>
) : null}
-
- {(renderLightbox && items.length) ?
- :
- null
- }
);
};
diff --git a/assets/src/components/Lightbox/Lightbox.js b/assets/src/components/Lightbox/Lightbox.js
deleted file mode 100644
index 40ee3b2be..000000000
--- a/assets/src/components/Lightbox/Lightbox.js
+++ /dev/null
@@ -1,118 +0,0 @@
-import {useEffect, useRef, useState} from '@wordpress/element';
-
-import PhotoSwipe from '../../../../node_modules/photoswipe/dist/photoswipe.js';
-import PhotoSwipeUI_Default from '../../../../node_modules/photoswipe/dist/photoswipe-ui-default.js';
-
-// `items` should be an array of object with this shape:
-// [{ src, w, h, title }, ...]
-// See: https://photoswipe.com/documentation/getting-started.html
-export const Lightbox = ({index, isOpen, items, onClose = () => {}}) => {
- let photoSwipeElement = useRef(null);
-
- const photoSwipeOptions = {
- index: index || 0,
- closeOnScroll: false,
- history: false,
- fullscreenEl: false,
- zoomEl: false,
- shareEl: false,
- counterEl: false,
- };
-
- const [currentIndex, setCurrentIndex] = useState(photoSwipeOptions.index);
-
- useEffect(() => {
- if (!isOpen) {
- return;
- }
-
- const photoSwipe = new PhotoSwipe(photoSwipeElement, PhotoSwipeUI_Default, items, photoSwipeOptions);
-
- // eslint-disable-next-line no-shadow
- photoSwipe.listen('gettingData', (index, galleryItem) => {
- if (galleryItem.w < 1 || galleryItem.h < 1) {
- const imageSizeHandler = new Image();
- imageSizeHandler.onload = function() {
- galleryItem.w = this.width;
- galleryItem.h = this.height;
- photoSwipe.updateSize(true);
- };
- imageSizeHandler.src = galleryItem.src;
- }
- });
-
- photoSwipe.listen('destroy', () => {
- onClose();
- });
-
- photoSwipe.listen('close', () => {
- onClose();
- });
-
- photoSwipe.listen('afterChange', () => {
- setCurrentIndex(photoSwipe.getCurrentIndex());
- });
-
- photoSwipe.init();
- }, [items, isOpen, index]);
-
- return wp.element.createPortal(
-
{
- photoSwipeElement = node;
- }}
- >
-
-
-
-
-
-
-
-
-
-
-
-
-
- {
- items.length > 1 && items.map((item, idx) =>
-
-
-
- )
- }
-
-
-
-
-
,
- document.body
- );
-};
-
diff --git a/assets/src/components/Lightbox/setupLightboxForImages.js b/assets/src/components/Lightbox/setupLightboxForImages.js
deleted file mode 100644
index 65fc5e57d..000000000
--- a/assets/src/components/Lightbox/setupLightboxForImages.js
+++ /dev/null
@@ -1,54 +0,0 @@
-import {Lightbox} from './Lightbox';
-import {createRoot} from 'react-dom/client';
-
-const setupImageAndCaption = (lightBoxNode, imageSelector = 'img', captionSelector = null) => {
- // Returns the callback for `forEach`
- return (imageBlock, index) => {
- const image = imageBlock.querySelector(imageSelector);
-
- if (!image) {
- return;
- }
-
- const item = {
- src: image.src ? image.src : image.dataset.src,
- w: 0,
- h: 0,
- };
-
- const caption = imageBlock.querySelector(captionSelector);
- if (caption) {
- item.title = caption.innerHTML;
- }
- const rootElement = createRoot(lightBoxNode);
-
- imageBlock.querySelector('img').addEventListener('click', () => rootElement.render());
- };
-};
-
-export const setupLightboxForImages = function() {
- // We can't use `createPortal` outside `render()`,
- // `React.render()` needs a node, even if it ends up being empty.
- // See: https://github.com/facebook/react/issues/12653#issuecomment-382851495
- const lightBoxNode = document.createElement('div');
- lightBoxNode.style.position = 'fixed';
-
- document.body.appendChild(lightBoxNode);
-
- const imageBlocks = [...document.querySelectorAll('.wp-block-image:not(.force-no-lightbox)')];
- // Images that are links should not have the lightbox
- const imageBlocksWithoutLinks = imageBlocks.filter(imageBlock => {
- const image = imageBlock.querySelector('img');
- return image.parentElement.tagName !== 'A';
- });
- imageBlocksWithoutLinks.forEach(setupImageAndCaption(lightBoxNode, 'img', 'figcaption'));
-
- const imagesWithCaptions = document.querySelectorAll('.post-content .wp-caption, .page-content .wp-caption');
- imagesWithCaptions.forEach(setupImageAndCaption(lightBoxNode, 'img', '.wp-caption-text'));
-
- const imagesInParagraphs = document.querySelectorAll('.post-content p:not(.wp-caption), .page-content p:not(.wp-caption)');
- imagesInParagraphs.forEach(setupImageAndCaption(lightBoxNode, 'img'));
-
- const mediaAndTextImages = document.querySelectorAll('.wp-block-media-text:not(.force-no-lightbox)');
- mediaAndTextImages.forEach(setupImageAndCaption(lightBoxNode));
-};
diff --git a/assets/src/components/Lightbox/useLightbox.js b/assets/src/components/Lightbox/useLightbox.js
deleted file mode 100644
index 559ab1e4b..000000000
--- a/assets/src/components/Lightbox/useLightbox.js
+++ /dev/null
@@ -1,18 +0,0 @@
-import {useState} from '@wordpress/element';
-
-export const useLightbox = () => {
- const [isOpen, setIsOpen] = useState(false);
- const [index, setIndex] = useState(0);
-
- const openLightbox = evt => {
- evt.preventDefault();
- setIsOpen(true);
- setIndex(parseInt(evt.currentTarget.dataset.index));
- };
-
- const closeLightbox = () => {
- setIsOpen(false);
- };
-
- return {isOpen, index, openLightbox, closeLightbox};
-};
diff --git a/assets/src/frontendIndex.js b/assets/src/frontendIndex.js
index 13070871d..7f99de947 100644
--- a/assets/src/frontendIndex.js
+++ b/assets/src/frontendIndex.js
@@ -5,7 +5,6 @@ import 'regenerator-runtime/runtime';
import {HappypointFrontend} from './blocks/Happypoint/HappypointFrontend';
import {ColumnsFrontend} from './blocks/Columns/ColumnsFrontend';
-import {setupLightboxForImages} from './components/Lightbox/setupLightboxForImages';
import {setupParallax} from './components/Parallax/setupParallax';
import {createRoot} from 'react-dom/client';
@@ -33,7 +32,5 @@ document.addEventListener('DOMContentLoaded', () => {
rootElement.render();
}
);
-
- setupLightboxForImages();
setupParallax();
});
diff --git a/assets/src/styles/lightbox.scss b/assets/src/styles/lightbox.scss
deleted file mode 100644
index 5edb810bc..000000000
--- a/assets/src/styles/lightbox.scss
+++ /dev/null
@@ -1 +0,0 @@
-@import "vendors/photoswipe/planet4-photoswipe";
diff --git a/assets/src/styles/vendors/photoswipe/_main-settings.scss b/assets/src/styles/vendors/photoswipe/_main-settings.scss
deleted file mode 100644
index 79a319548..000000000
--- a/assets/src/styles/vendors/photoswipe/_main-settings.scss
+++ /dev/null
@@ -1,19 +0,0 @@
-// This file mimics: https://github.com/dimsemenov/PhotoSwipe/blob/v4.1.3/src/css/_main-settings.scss
-
-$pswp__show-hide-transition-duration: 333ms !default;
-$pswp__controls-transition-duration: 333ms !default;
-$pswp__background-color: rgba(0, 0, 0, .85) !default;
-$pswp__placeholder-color: #111 !default;
-$pswp__box-sizing-border-box: true !default; // disable .pswp * { box-sizing:border-box } (in case you already have it in your site css)
-$pswp__root-z-index: 150000 !default;
-$pswp__assets-path: "../../public/images/vendors/photoswipe/" !default; // path to skin assets folder (preloader, PNG and SVG sprite)
-$pswp__error-text-color: #111 !default; // "Image not loaded" text color
-$pswp__include-minimal-style: true !default;
-
-// Planet4 specific rules
-$p4-pswp-counter-text-color: #111;
-$p4-pswp-caption-text-color: black;
-$p4-pswp-top-bar-background: white;
-$p4-pswp-caption-background: white;
-$p4-pswp-arrows-background: white;
-$p4-pswp-button-opacity: 1;
diff --git a/assets/src/styles/vendors/photoswipe/main.scss b/assets/src/styles/vendors/photoswipe/main.scss
deleted file mode 100644
index 89ba20af3..000000000
--- a/assets/src/styles/vendors/photoswipe/main.scss
+++ /dev/null
@@ -1,189 +0,0 @@
-// This file was obtained from: https://github.com/dimsemenov/PhotoSwipe/blob/v4.1.3/src/css/main.scss
-
-/* pswp = photoswipe */
-.pswp {
- display: none;
- position: absolute;
- width: 100%;
- height: 100%;
- left: 0;
- top: 0;
- overflow: hidden;
- touch-action: none;
- z-index: $pswp__root-z-index;
- text-size-adjust: 100%;
- /* create separate layer, to avoid paint on window.onscroll in webkit/blink */
- backface-visibility: hidden;
- outline: none;
-
- @if $pswp__box-sizing-border-box == true {
- * {
- box-sizing: border-box;
- }
- }
-
- img {
- max-width: none;
- }
-}
-
-/* style is added when JS option showHideOpacity is set to true */
-.pswp--animate_opacity {
- /* 0.001, because opacity:0 doesn't trigger Paint action, which causes lag at start of transition */
- opacity: 0.001;
- will-change: opacity;
- /* for open/close transition */
- transition: opacity $pswp__show-hide-transition-duration cubic-bezier(.4, 0, .22, 1);
-}
-
-.pswp--open {
- display: block;
-}
-
-.pswp--zoom-allowed .pswp__img {
- /* autoprefixer: off */
- cursor: -webkit-zoom-in;
- cursor: -moz-zoom-in;
- cursor: zoom-in;
-}
-
-.pswp--zoomed-in .pswp__img {
- /* autoprefixer: off */
- cursor: -webkit-grab;
- cursor: -moz-grab;
- cursor: grab;
-}
-
-.pswp--dragging .pswp__img {
- /* autoprefixer: off */
- cursor: -webkit-grabbing;
- cursor: -moz-grabbing;
- cursor: grabbing;
-}
-
-/*
- Background is added as a separate element.
- As animating opacity is much faster than animating rgba() background-color.
-*/
-
-.pswp__bg {
- position: absolute;
- left: 0;
- top: 0;
- width: 100%;
- height: 100%;
- background: $pswp__background-color;
- opacity: 0;
- transform: translateZ(0);
- backface-visibility: hidden;
- will-change: opacity;
- /* for open/close transition */
- transition: opacity $pswp__show-hide-transition-duration cubic-bezier(.4, 0, .22, 1);
-}
-
-.pswp__scroll-wrap {
- position: absolute;
- left: 0;
- top: 0;
- width: 100%;
- height: 100%;
- overflow: hidden;
-}
-
-.pswp__container,
-.pswp__zoom-wrap {
- touch-action: none;
- position: absolute;
- left: 0;
- right: 0;
- top: 0;
- bottom: 0;
- backface-visibility: hidden;
-}
-
-/* Prevent selection and tap highlights */
-.pswp__container,
-.pswp__img {
- user-select: none;
- -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
- -webkit-touch-callout: none;
-}
-
-.pswp__zoom-wrap {
- position: absolute;
- width: 100%;
- transform-origin: left top;
- /* for open/close transition */
- transition: transform $pswp__show-hide-transition-duration cubic-bezier(.4, 0, .22, 1);
-}
-
-.pswp--animated-in {
- .pswp__bg,
- .pswp__zoom-wrap {
- transition: none;
- }
-}
-
-.pswp__item {
- position: absolute;
- left: 0;
- right: 0;
- top: 0;
- bottom: 0;
- overflow: hidden;
-}
-
-.pswp__img {
- position: absolute;
- width: auto;
- height: auto;
- top: 0;
- left: 0;
-}
-
-/*
- stretched thumbnail or div placeholder element (see below)
- style is added to avoid flickering in webkit/blink when layers overlap
-*/
-
-.pswp__img--placeholder {
- backface-visibility: hidden;
-}
-
-/*
- div element that matches size of large image
- large image loads on top of it
-*/
-
-.pswp__img--placeholder--blank {
- background: $pswp__placeholder-color;
-}
-
-.pswp--ie .pswp__img {
- width: 100% !important;
- height: auto !important;
- left: 0;
- top: 0;
-}
-
-/*
- Error message appears when image is not loaded
- (JS option errorMsg controls markup)
-*/
-
-.pswp__error-msg {
- position: absolute;
- left: 0;
- top: 50%;
- width: 100%;
- text-align: center;
- font-size: 14px;
- line-height: 16px;
- margin-top: -8px;
- color: $pswp__error-text-color;
-}
-
-.pswp__error-msg a {
- color: $pswp__error-text-color;
- text-decoration: underline;
-}
diff --git a/assets/src/styles/vendors/photoswipe/p4-skin.scss b/assets/src/styles/vendors/photoswipe/p4-skin.scss
deleted file mode 100644
index 1e1b6288f..000000000
--- a/assets/src/styles/vendors/photoswipe/p4-skin.scss
+++ /dev/null
@@ -1,601 +0,0 @@
-// This file is based in: https://github.com/dimsemenov/PhotoSwipe/blob/v4.1.3/src/css/default-skin/default-skin.scss
-// ...with some minor tweaks/additions.
-
-/*! PhotoSwipe Default UI CSS by Dmitry Semenov | photoswipe.com | MIT license */
-
-/*
-
-Contents:
-
-1. Buttons
-2. Share modal and links
-3. Index indicator ("1 of X" counter)
-4. Caption
-5. Loading indicator
-6. Additional styles (root element, top bar, idle state, hidden state, etc.)
-
-*/
-
-// PhotoSwipe uses Autoprefixer, so vendor prefixed are added automatically when needed.
-
-/*
-
-1. Buttons
-
-*/
-
-/*