编辑文件:add-to-cart-with-options-variation-selector-attribute.js
(()=>{var e,t,o,r={7252:(e,t,o)=>{"use strict";const r=window.wp.blocks;var n=o(7871),i=o(6645);const l=JSON.parse('{"name":"woocommerce/add-to-cart-with-options-variation-selector-attribute","title":"Variation Selector: Template (Beta)","description":"A template for attribute name and options that will be applied to all variable products with attributes.","category":"woocommerce-product-elements","keywords":["WooCommerce"],"usesContext":["postId"],"ancestor":["woocommerce/add-to-cart-with-options-variation-selector"],"attributes":{"displayStyle":{"type":"string","default":"woocommerce/product-filter-chips"},"autoselect":{"type":"boolean","default":false},"disabledAttributesAction":{"type":"string","enum":["disable","hide"],"default":"disable"}},"textdomain":"woocommerce","apiVersion":3,"$schema":"https://schemas.wp.org/trunk/block.json","supports":{"inserter":false,"interactivity":true},"style":"file:../woocommerce/add-to-cart-with-options-variation-selector-attribute-style.css"}');var s=o(6087);const c=window.wp.blockEditor,a=window.wp.data,u=window.wc.wcBlocksData;var d=o(923),m=o.n(d);function p(e){const t=(0,s.useRef)(e);return m()(e,t.current)||(t.current=e),t.current}const b=window.wc.wcTypes,w=window.wc.wcBlocksSharedContext;var h=o(7723);const f=(e,t)=>{if(!e)return null;if(0===e.innerBlocks.length)return null;for(const o of e.innerBlocks){if(t(o))return o;const e=f(o,t);if(e)return e}return null},_=window.wp.components;var g=o(790);const y="woocommerce/selectableItems";function v(e){return Boolean(e)}function x(e){return e?Array.isArray(e)?e:[e]:[]}function k(e,t){return(0,r.getBlockTypes)().filter(o=>function(e,t,o){return!!t&&!!function(e){return!0===e.supports?.woocommerce?.innerBlockDisplayStyle}(e)&&x(e.ancestor).includes(t)&&x(e.usesContext).includes(o)}(o,e,t))}function S(e,t){return t.map(t=>{return o=e,r=t.name,f(o,function(e){return e.name===r});var o,r}).find(v)??null}function B(e,t){return t?.(e)??{rootClientId:e.clientId,index:e.innerBlocks.length}}const j=({clientId:e,currentStyle:t,onChange:o,contextKey:n=y,getFallbackDisplayStyleInsertionPoint:i})=>{const l=(0,a.select)("core/block-editor").getBlock(e),c=l?.name,u=k(c,n),{insertBlock:d,replaceBlock:m}=(0,a.useDispatch)("core/block-editor"),[p,b]=(0,s.useState)({});return 0===u.length?null:(0,g.jsx)(_.__experimentalToggleGroupControl,{value:t,isBlock:!0,__nextHasNoMarginBottom:!0,__next40pxDefaultSize:!0,label:"",hideLabelFromVision:!0,onChange:e=>{if(!e||"string"!=typeof e)return;if(!l)return;if(!u.some(t=>t.name===e))return;const t=S(l,u);if(t){const o={...p,[t.name]:t.attributes};b(o),m(t.clientId,(0,r.createBlock)(e,o[e]||{}))}else{const t=B(l,i);d((0,r.createBlock)(e),t.index,t.rootClientId,!1)}o(e)},style:{width:"100%"},children:u.map(e=>(0,g.jsx)(_.__experimentalToggleGroupControlOption,{label:e.title,value:e.name},e.name))})};function C(e,t,o,n=y){const i=(0,a.select)("core/block-editor").getBlock(e);if(!i)return;const l=k(i.name,n);if(!l.some(e=>e.name===t))return;const s=S(i,l),{insertBlock:c,replaceBlock:u}=(0,a.dispatch)("core/block-editor");if(s)u(s.clientId,(0,r.createBlock)(t));else{const e=B(i,o);c((0,r.createBlock)(t),e.index,e.rootClientId,!1)}}const A=window.wc.wcSettings,O=[{id:1,taxonomy:"pa_color",name:(0,h.__)("Color","woocommerce"),has_variations:!0,terms:[{id:-1,slug:"blue",name:(0,h.__)("Blue","woocommerce")},{id:-2,slug:"red",name:(0,h.__)("Red","woocommerce")},{id:-3,slug:"green",name:(0,h.__)("Green","woocommerce")}]},{id:2,taxonomy:"pa_size",name:(0,h.__)("Size","woocommerce"),has_variations:!0,terms:[{id:1,slug:"sm",name:(0,h.__)("Small","woocommerce")},{id:2,slug:"md",name:(0,h.__)("Medium","woocommerce")},{id:3,slug:"lg",name:(0,h.__)("Large","woocommerce")}]}],I={"-1":{type:"color",value:"#0000ff"},"-2":{type:"color",value:"#e10000"},"-3":{type:"color",value:"#009b00"}},T="woocommerce/product-filter-chips",P=e=>{const t=e.innerBlocks.find(e=>"core/group"===e.name);return t?{rootClientId:t.clientId,index:t.innerBlocks.length}:{rootClientId:e.clientId,index:e.innerBlocks.length}};function D({blocks:e,isSelected:t,onSelect:o}){const{data:r}=(0,w.useCustomDataContext)("attribute"),n=(0,s.useMemo)(()=>r?.terms?r.terms.map(e=>e.id).filter(e=>e>0):[],[r]),{results:i}=(e=>{const{namespace:t,resourceName:o,resourceValues:r=[],query:n={},shouldSelect:i=!0}=e;if(!t||!o)throw new Error("The options object must have valid values for the namespace and the resource properties.");const l=(0,s.useRef)({results:[],isLoading:!0}),c=p(n),d=p(r),m=(()=>{const[,e]=(0,s.useState)();return(0,s.useCallback)(t=>{e(()=>{throw t})},[])})(),w=(0,a.useSelect)(e=>{if(!i)return null;const r=e(u.COLLECTIONS_STORE_KEY),n=[t,o,c,d],l=r.getCollectionError(...n);if(l){if(!(0,b.isError)(l))throw new Error("TypeError: `error` object is not an instance of Error constructor");m(l)}return{results:r.getCollection(...n),isLoading:!r.hasFinishedResolution("getCollection",n)}},[t,o,d,c,i,m]);return null!==w&&(l.current=w),l.current})({namespace:"/wc/store/v1",resourceName:"products/attributes/terms",resourceValues:[r?.id||0],shouldSelect:!!r?.id&&n.length>0,query:{include:n,hide_empty:!1,__experimental_visual:!0}}),l=(0,A.getSetting)("experimentalVisualAttributes",!1),d=(0,s.useMemo)(()=>i.reduce((e,t)=>(t.__experimentalVisual&&(e[t.id]=t.__experimentalVisual),e),{}),[i]),m=(0,s.useMemo)(()=>{let e=[];return r&&Array.isArray(r?.terms)&&r.terms.length>0&&(e=r.terms.map(e=>{const t=d[e.id]||(l?I[e.id]:void 0);return{id:`${r.taxonomy}-${e.slug}`,label:e.name,value:e.slug,ariaLabel:e.name,...t?{visual:t}:{}}})),{items:e,selectionMode:"single",storeNamespace:"woocommerce/add-to-cart-with-options",groupLabel:""}},[r,l,d]),h=(0,c.__experimentalUseBlockPreview)({blocks:e}),f=(0,c.useInnerBlocksProps)();return r?(0,g.jsx)(c.BlockContextProvider,{value:{"woocommerce/selectableItems":m},children:t?(0,g.jsx)("div",{...f}):(0,g.jsx)("div",{...h,onClick:o})}):null}function E(){const e=c.useBlockProps.save(),t=c.useInnerBlocksProps.save({...e,role:"listitem"});return(0,g.jsx)("div",{...t})}const M="woocommerce/add-to-cart-with-options-variation-selector-attribute-options",N="woocommerce/product-filter-chips",V="woocommerce/dropdown";function L(e){return e.some(e=>"core/missing"===e.name&&e.attributes.originalName===M||!!e.innerBlocks?.length&&L(e.innerBlocks))}function G(e,t){return e.flatMap(e=>"core/missing"===e.name&&e.attributes.originalName===M?(e.originalContent?.includes('"autoselect":true')&&(t.autoselect=!0),e.originalContent?.includes('"disabledAttributesAction":"hide"')&&(t.disabledAttributesAction="hide"),e.originalContent?.includes('"optionStyle":"dropdown"')?(t.displayStyle=V,[(0,r.createBlock)(V)]):(t.displayStyle=N,[(0,r.createBlock)(N)])):e.innerBlocks?.length?[{...e,innerBlocks:G(e.innerBlocks,t)}]:[e])}const R=[{save:E,isEligible:(e,t)=>L(t),migrate(e,t){const o={displayStyle:l.attributes.displayStyle.default,autoselect:l.attributes.autoselect.default,disabledAttributesAction:l.attributes.disabledAttributesAction.default},r=G(t,o);return[{...e,displayStyle:o.displayStyle,autoselect:o.autoselect,disabledAttributesAction:o.disabledAttributesAction},r]}}];o(8023),(0,r.registerBlockType)(l,{edit:function(e){const{attributes:t,setAttributes:o,clientId:r}=e,{className:n,displayStyle:i,autoselect:l,disabledAttributesAction:u}=t,d=(0,c.useBlockProps)({className:n}),{product:m}=(0,w.useProductDataContext)(),p=(e=>!!e&&"id"in e&&0!==e.id)(m)&&"variable"===m.type?m.attributes:O,{blocks:b}=(0,a.useSelect)(e=>{const{getBlocks:t}=e(c.store);return{blocks:t(r)}},[r]),[f,y]=(0,s.useState)();return(0,g.jsxs)(g.Fragment,{children:[(0,g.jsxs)(c.InspectorControls,{children:[(0,g.jsx)(_.__experimentalToolsPanel,{label:(0,h.__)("Style","woocommerce"),resetAll:()=>{o({displayStyle:T}),C(r,T,P)},children:(0,g.jsx)(_.__experimentalToolsPanelItem,{hasValue:()=>i!==T,label:(0,h.__)("Style","woocommerce"),onDeselect:()=>{o({displayStyle:T}),C(r,T,P)},isShownByDefault:!0,children:(0,g.jsxs)("div",{children:[(0,g.jsx)("span",{className:"screen-reader-text",children:(0,h.__)("Style","woocommerce")}),(0,g.jsx)(j,{clientId:r,currentStyle:i,getFallbackDisplayStyleInsertionPoint:P,onChange:e=>{o({displayStyle:e})}})]})})}),(0,g.jsxs)(_.__experimentalToolsPanel,{label:(0,h.__)("Auto-select","woocommerce"),resetAll:()=>o({autoselect:!1,disabledAttributesAction:"disable"}),children:[(0,g.jsx)(_.__experimentalToolsPanelItem,{label:(0,h.__)("Auto-select when only one option is available","woocommerce"),hasValue:()=>l,onDeselect:()=>o({autoselect:!1}),isShownByDefault:!0,children:(0,g.jsx)(_.ToggleControl,{label:(0,h.__)("Auto-select when only one option is available","woocommerce"),help:(0,h.__)("Automatically select options on page load or after the shopper changes attributes, when only one valid choice is available.","woocommerce"),checked:l,onChange:()=>o({autoselect:!l}),__nextHasNoMarginBottom:!0})}),(0,g.jsx)(_.__experimentalToolsPanelItem,{label:(0,h.__)("Invalid options","woocommerce"),hasValue:()=>"disable"!==u,onDeselect:()=>o({disabledAttributesAction:"disable"}),isShownByDefault:!0,children:(0,g.jsxs)(_.__experimentalToggleGroupControl,{label:(0,h.__)("Invalid options","woocommerce"),help:(0,h.__)("Control the display of invalid options.","woocommerce"),value:u,onChange:e=>{"hide"!==e&&"disable"!==e||o({disabledAttributesAction:e})},isBlock:!0,size:"__unstable-large",children:[(0,g.jsx)(_.__experimentalToggleGroupControlOption,{value:"disable",label:(0,h.__)("Grayed-out","woocommerce")}),(0,g.jsx)(_.__experimentalToggleGroupControlOption,{value:"hide",label:(0,h.__)("Hidden","woocommerce")})]})})]})]}),(0,g.jsx)("div",{...d,children:p.map(e=>(0,g.jsx)(w.CustomDataProvider,{id:"attribute",data:e,children:(0,g.jsx)(D,{blocks:b,isSelected:(f||p[0]?.id)===e.id,onSelect:()=>y(e.id)})},e.id))})]})},icon:{src:(0,g.jsx)(n.A,{icon:i.A,className:"wc-block-editor-components-block-icon"})},save:E,deprecated:R})},8023:()=>{},790:e=>{"use strict";e.exports=window.ReactJSXRuntime},6087:e=>{"use strict";e.exports=window.wp.element},7723:e=>{"use strict";e.exports=window.wp.i18n},923:e=>{"use strict";e.exports=window.wp.isShallowEqual},5573:e=>{"use strict";e.exports=window.wp.primitives}},n={};function i(e){var t=n[e];if(void 0!==t)return t.exports;var o=n[e]={exports:{}};return r[e](o,o.exports,i),o.exports}i.m=r,e=[],i.O=(t,o,r,n)=>{if(!o){var l=1/0;for(u=0;u<e.length;u++){for(var[o,r,n]=e[u],s=!0,c=0;c<o.length;c++)(!1&n||l>=n)&&Object.keys(i.O).every(e=>i.O[e](o[c]))?o.splice(c--,1):(s=!1,n<l&&(l=n));if(s){e.splice(u--,1);var a=r();void 0!==a&&(t=a)}}return t}n=n||0;for(var u=e.length;u>0&&e[u-1][2]>n;u--)e[u]=e[u-1];e[u]=[o,r,n]},i.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return i.d(t,{a:t}),t},o=Object.getPrototypeOf?e=>Object.getPrototypeOf(e):e=>e.__proto__,i.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 n=Object.create(null);i.r(n);var l={};t=t||[null,o({}),o([]),o(o)];for(var s=2&r&&e;"object"==typeof s&&!~t.indexOf(s);s=o(s))Object.getOwnPropertyNames(s).forEach(t=>l[t]=()=>e[t]);return l.default=()=>e,i.d(n,l),n},i.d=(e,t)=>{for(var o in t)i.o(t,o)&&!i.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},i.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),i.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),i.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.j=9860,(()=>{var e={9860:0};i.O.j=t=>0===e[t];var t=(t,o)=>{var r,n,[l,s,c]=o,a=0;if(l.some(t=>0!==e[t])){for(r in s)i.o(s,r)&&(i.m[r]=s[r]);if(c)var u=c(i)}for(t&&t(o);a<l.length;a++)n=l[a],i.o(e,n)&&e[n]&&e[n][0](),e[n]=0;return i.O(u)},o=globalThis.webpackChunkwebpackWcBlocksMainJsonp=globalThis.webpackChunkwebpackWcBlocksMainJsonp||[];o.forEach(t.bind(null,0)),o.push=t.bind(null,o.push.bind(o))})();var l=i.O(void 0,[94],()=>i(7252));l=i.O(l),((this.wc=this.wc||{}).blocks=this.wc.blocks||{})["add-to-cart-with-options-variation-selector-attribute"]=l})();
保存
返回