← Back
Editing: product-gallery.js
(()=>{"use strict";var e,t,o,r={91107:(e,t,o)=>{const r=window.wp.blocks,i=window.wp.data,n="core/editor",s=window.wc.wcTypes;class l{blocks=new Map;initialized=!1;attemptedRegisteredBlocks=new Set;constructor(){this.initializeSubscriptions()}static getInstance(){return l.instance||(l.instance=new l),l.instance}initializeSubscriptions(){if(this.initialized)return;const e=(0,i.subscribe)(()=>{const t=(0,i.select)(n);if(!t)return;const o=t.getCurrentPostType();if(o)if("post"===o||"page"===o)e(),this.blocks.forEach(e=>{if(e.isAvailableOnPostEditor){const t=e.variationName||e.metadata.name;this.hasAttemptedRegistration(t)||this.registerBlock(e)}}),this.initialized=!0;else{e();const o=String(t.getEditedPostSlug());this.currentTemplateId=o,this.handleTemplateChange(void 0),(0,i.subscribe)(()=>{const e=this.currentTemplateId;this.currentTemplateId=String(t.getEditedPostSlug()),e!==this.currentTemplateId&&this.handleTemplateChange(e)},n),this.initialized=!0}})}handleTemplateChange(e){(this.currentTemplateId?.includes("single-product")||e?.includes("single-product"))&&this.blocks.forEach(e=>{this.unregisterBlock(e),this.registerBlock(e)})}hasAttemptedRegistration(e){return this.attemptedRegisteredBlocks.has(e)}unregisterBlock(e){const{metadata:t,isVariationBlock:o,variationName:i}=e;try{o&&i?((0,r.unregisterBlockVariation)(t.name,i),this.attemptedRegisteredBlocks.delete(i)):((0,r.unregisterBlockType)(t.name),this.attemptedRegisteredBlocks.delete(t.name))}catch(e){console.debug(`Failed to unregister block ${t.name}:`,e)}}registerBlock(e){const{metadata:t,isVariationBlock:o,variationName:n,isAvailableOnPostEditor:l}=e;try{const c=n||t.name;if(this.hasAttemptedRegistration(c))return;const a=(0,i.select)("core/edit-site");if(!a&&!l)return;if(o)(0,r.registerBlockVariation)(t.name,{...t,name:n||t.name});else{const o=(0,s.isEmpty)(t?.ancestor)?["woocommerce/single-product"]:t?.ancestor,i=a&&this.currentTemplateId?.includes("single-product"),{title:n,description:l,keywords:d,styles:u,variations:m,...p}=t,h=(0,r.registerBlockType)(t,{...p,ancestor:i?void 0:o});this.blocks.set(c,{...e,metadata:{...t,title:h?.title??t.title,description:h?.description??t.description}})}this.attemptedRegisteredBlocks.add(c)}catch(e){console.error(`Failed to register block ${t.name}:`,e)}}registerBlockConfig(e){const t=e.variationName||e.metadata.name;this.blocks.set(t,e),this.registerBlock(e)}}const c=JSON.parse('{"$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":3,"name":"woocommerce/product-gallery","title":"Product Gallery","description":"Showcase your products relevant images and media.","category":"woocommerce","keywords":["WooCommerce"],"supports":{"align":true,"interactivity":true,"layout":{"default":{"type":"flex","flexWrap":"nowrap","orientation":"horizontal"},"allowOrientation":true,"allowEditing":true,"allowJustification":false},"email":true},"textdomain":"woocommerce","usesContext":["postId"],"providesContext":{"hoverZoom":"hoverZoom","fullScreenOnClick":"fullScreenOnClick"},"ancestor":["woocommerce/single-product"],"attributes":{"hoverZoom":{"type":"boolean","default":true},"fullScreenOnClick":{"type":"boolean","default":true}},"viewScript":"wc-product-gallery-frontend","example":{},"viewScriptModule":"woocommerce/product-gallery","editorStyle":"file:../woocommerce/product-gallery-editor.css","style":"file:../woocommerce/product-gallery-style.css"}'),a=window.wp.blockEditor,d=window.wc.wcBlocksSharedHocs,u=window.wc.wcBlocksSharedContext,m=window.wc.wcSettings;var p=o(4921),h=o(27723);const g=window.wp.components;var w=o(10790);const f=({attributes:e,setAttributes:t})=>{const{hoverZoom:o,fullScreenOnClick:r}=e;return(0,w.jsx)(a.InspectorControls,{children:(0,w.jsxs)(g.__experimentalToolsPanel,{label:(0,h.__)("Media Settings","woocommerce"),resetAll:()=>{t({hoverZoom:!0,fullScreenOnClick:!0})},children:[(0,w.jsx)(g.__experimentalToolsPanelItem,{hasValue:()=>!0!==o,label:(0,h.__)("Zoom while hovering","woocommerce"),onDeselect:()=>t({hoverZoom:!0}),isShownByDefault:!0,children:(0,w.jsx)(g.ToggleControl,{label:(0,h.__)("Zoom while hovering","woocommerce"),help:(0,h.__)("While hovering the image in the viewer will zoom in by 30%.","woocommerce"),checked:o,onChange:()=>t({hoverZoom:!o})})}),(0,w.jsx)(g.__experimentalToolsPanelItem,{hasValue:()=>!0!==r,label:(0,h.__)("Open pop-up when clicked","woocommerce"),onDeselect:()=>t({fullScreenOnClick:!0}),isShownByDefault:!0,children:(0,w.jsx)(g.ToggleControl,{label:(0,h.__)("Open pop-up when clicked","woocommerce"),help:(0,h.__)("Clicking on the image in the viewer will open a full-screen gallery experience.","woocommerce"),checked:r,onChange:()=>t({fullScreenOnClick:!r})})})]})})};let b=function(e){return e.SINGLE="single",e.THUMBNAIL="thumbnail",e.CROPPED="cropped",e}({});const k=[["woocommerce/product-gallery-thumbnails"],["woocommerce/product-gallery-large-image",{},[["woocommerce/product-image",{showProductLink:!1,showSaleBadge:!1}],["woocommerce/product-sale-badge",{align:"right"}],["woocommerce/product-gallery-large-image-next-previous"]]]],v={width:"1",height:"1"},y=e=>"woocommerce/product-image"===e.name,x=e=>{for(const t of e){if(y(t))return t;const e=x(t.innerBlocks);if(e)return e}};((e,t)=>{if(!(e=>"string"==typeof e?.name)(e))return void console.error("registerProductBlockType: Block name is required for registration");const o={metadata:e,isVariationBlock:t?.isVariationBlock??!1,variationName:t?.variationName??void 0,isAvailableOnPostEditor:t?.isAvailableOnPostEditor??!1};l.getInstance().registerBlockConfig(o)})({...c,icon:()=>(0,w.jsxs)("svg",{width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:[(0,w.jsx)("path",{d:"M19 3H5C4.4 3 4 3.4 4 4V11C4 11.5 4.4 12 5 12H19C19.5 12 20 11.6 20 11V4C20 3.4 19.6 3 19 3ZM5.5 10.5V10.1L7.3 8.8L8.6 9.6C8.9 9.8 9.3 9.8 9.5 9.5L11 8.1L13.4 10.5H5.5ZM18.5 10.5H15.6L11.6 6.5C11.3 6.2 10.8 6.2 10.5 6.5L8.9 8L7.7 7.2C7.4 7 7.1 7 6.8 7.2L5.5 8.2V4.5H18.5V10.5Z",fill:"currentColor"}),(0,w.jsx)("rect",{x:"4.75",y:"15.5",width:"5",height:"4.5",rx:"1",stroke:"currentColor",strokeWidth:"1.5",fill:"none"}),(0,w.jsx)("rect",{x:"12.25",y:"15.5",width:"5",height:"4.5",rx:"1",stroke:"currentColor",strokeWidth:"1.5",fill:"none"})]}),edit:(0,d.withProductDataContext)(({clientId:e,attributes:t,setAttributes:o,context:r})=>{const{product:n,isLoading:s}=(0,u.useProductDataContext)(),l=n?.images||[],c=Boolean(r?.postId&&n?.id)&&!s&&l.length<=1,d=(0,m.getSetting)("thumbnailAspectRatio",null),h=(e=>{if(!e)return v;const t=e.split("/").map(e=>e.trim()).filter(Boolean);if(0===t.length||t.length>2)return v;const o=Number(t[0]),r=Number(t[1]??t[0]);return Number.isFinite(o)&&Number.isFinite(r)?{width:String(o),height:String(r)}:v})((0,i.useSelect)(t=>{const o=x(t(a.store).getBlocks(e));return((e,t)=>{const{style:o,aspectRatio:r,imageSizing:i}=e??{};return((e,t,o,r)=>e&&e.dimensions&&e.dimensions.aspectRatio&&"string"==typeof e.dimensions.aspectRatio?e.dimensions.aspectRatio:t&&"string"==typeof t?t:!r||r!==b.THUMBNAIL&&r!==b.CROPPED?void 0:o??void 0)(o,r,t,i)})(o?.attributes,d)},[e,d])),g=(0,a.useBlockProps)({className:(0,p.A)("wc-block-product-gallery",{"wc-block-product-gallery--has-one-or-no-images":c}),style:{"--wc-block-product-gallery-large-image-ratio-width":h.width,"--wc-block-product-gallery-large-image-ratio-height":h.height}});return(0,w.jsxs)("div",{...g,children:[(0,w.jsx)(a.InspectorControls,{children:(0,w.jsx)(f,{attributes:t,setAttributes:o})}),(0,w.jsx)(a.InnerBlocks,{allowedBlocks:["woocommerce/product-gallery-large-image","woocommerce/product-gallery-thumbnails"],template:k})]})}),save:()=>{const e=a.useBlockProps.save({className:"wc-block-product-gallery"}),t=a.useInnerBlocksProps.save(e);return(0,w.jsx)("div",{...t})}},{isAvailableOnPostEditor:!0})},10790:e=>{e.exports=window.ReactJSXRuntime},27723:e=>{e.exports=window.wp.i18n}},i={};function n(e){var t=i[e];if(void 0!==t)return t.exports;var o=i[e]={exports:{}};return r[e](o,o.exports,n),o.exports}n.m=r,e=[],n.O=(t,o,r,i)=>{if(!o){var s=1/0;for(d=0;d<e.length;d++){for(var[o,r,i]=e[d],l=!0,c=0;c<o.length;c++)(!1&i||s>=i)&&Object.keys(n.O).every(e=>n.O[e](o[c]))?o.splice(c--,1):(l=!1,i<s&&(s=i));if(l){e.splice(d--,1);var a=r();void 0!==a&&(t=a)}}return t}i=i||0;for(var d=e.length;d>0&&e[d-1][2]>i;d--)e[d]=e[d-1];e[d]=[o,r,i]},n.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return n.d(t,{a:t}),t},o=Object.getPrototypeOf?e=>Object.getPrototypeOf(e):e=>e.__proto__,n.t=function(e,r){if(1&r&&(e=this(e)),8&r)return e;if("object"==typeof e&&e){if(4&r&&e.__esModule)return e;if(16&r&&"function"==typeof e.then)return e}var i=Object.create(null);n.r(i);var s={};t=t||[null,o({}),o([]),o(o)];for(var l=2&r&&e;"object"==typeof l&&!~t.indexOf(l);l=o(l))Object.getOwnPropertyNames(l).forEach(t=>s[t]=()=>e[t]);return s.default=()=>e,n.d(i,s),i},n.d=(e,t)=>{for(var o in t)n.o(t,o)&&!n.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},n.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.j=6749,(()=>{var e={6749:0};n.O.j=t=>0===e[t];var t=(t,o)=>{var r,i,[s,l,c]=o,a=0;if(s.some(t=>0!==e[t])){for(r in l)n.o(l,r)&&(n.m[r]=l[r]);if(c)var d=c(n)}for(t&&t(o);a<s.length;a++)i=s[a],n.o(e,i)&&e[i]&&e[i][0](),e[i]=0;return n.O(d)},o=globalThis.webpackChunkwebpackWcBlocksMainJsonp=globalThis.webpackChunkwebpackWcBlocksMainJsonp||[];o.forEach(t.bind(null,0)),o.push=t.bind(null,o.push.bind(o))})();var s=n.O(void 0,[94],()=>n(91107));s=n.O(s),((this.wc=this.wc||{}).blocks=this.wc.blocks||{})["product-gallery"]=s})();
Save File
Cancel