2 lines
15 KiB
JavaScript
2 lines
15 KiB
JavaScript
/*! This file is auto-generated */
|
|
(()=>{"use strict";var e={d:(t,n)=>{for(var a in n)e.o(n,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:n[a]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{privateApis:()=>H,store:()=>h});var n={};e.r(n),e.d(n,{convertSyncedPatternToStatic:()=>y,createPattern:()=>_,createPatternFromFile:()=>g,setEditingPattern:()=>f});var a={};e.r(a),e.d(a,{isEditingPattern:()=>w});const r=window.wp.data;const o=(0,r.combineReducers)({isEditingPattern:function(e={},t){return"SET_EDITING_PATTERN"===t?.type?{...e,[t.clientId]:t.isEditing}:e}}),s=window.wp.blocks,i=window.wp.coreData,c=window.wp.blockEditor,l=window.wp.i18n,u={theme:"pattern",user:"wp_block"},p="all-patterns",d={full:"fully",unsynced:"unsynced"},m={"core/paragraph":{content:(0,l.__)("Content")},"core/heading":{content:(0,l.__)("Content")},"core/button":{text:(0,l.__)("Text"),url:(0,l.__)("URL"),linkTarget:(0,l.__)("Link Target")},"core/image":{url:(0,l.__)("URL"),title:(0,l.__)("Title"),alt:(0,l.__)("Alt Text")}},_=(e,t,n,a)=>async({registry:r})=>{const o=t===d.unsynced?{wp_pattern_sync_status:t}:void 0,s={title:e,content:n,status:"publish",meta:o,wp_pattern_category:a};return await r.dispatch(i.store).saveEntityRecord("postType","wp_block",s)},g=(e,t)=>async({dispatch:n})=>{const a=await e.text();let r;try{r=JSON.parse(a)}catch(e){throw new Error("Invalid JSON file")}if("wp_block"!==r.__file||!r.title||!r.content||"string"!=typeof r.title||"string"!=typeof r.content||r.syncStatus&&"string"!=typeof r.syncStatus)throw new Error("Invalid pattern JSON file");return await n.createPattern(r.title,r.syncStatus,r.content,t)},y=e=>({registry:t})=>{const n=t.select(c.store).getBlock(e);t.dispatch(c.store).replaceBlocks(n.clientId,function e(t){return t.map((t=>{let n=t.attributes.metadata;return n&&(n={...n},delete n.id,delete n.bindings),(0,s.cloneBlock)(t,{metadata:n&&Object.keys(n).length>0?n:void 0},e(t.innerBlocks))}))}(n.innerBlocks))};function f(e,t){return{type:"SET_EDITING_PATTERN",clientId:e,isEditing:t}}function w(e,t){return e.isEditingPattern[t]}const E=window.wp.privateApis,{lock:C,unlock:S}=(0,E.__dangerousOptInToUnstableAPIsOnlyForCoreModules)("I know using unstable features means my theme or plugin will inevitably break in the next version of WordPress.","@wordpress/patterns"),b={reducer:o},h=(0,r.createReduxStore)("core/patterns",{...b});(0,r.register)(h),S(h).registerPrivateActions(n),S(h).registerPrivateSelectors(a);const k=window.React,v=window.wp.components,P=window.wp.element,x=window.wp.notices,T=window.wp.compose,B=window.wp.htmlEntities,R=e=>(0,B.decodeEntities)(e),D="wp_pattern_category";function I({categoryTerms:e,onChange:t,categoryMap:n}){const[a,r]=(0,P.useState)(""),o=(0,T.useDebounce)(r,500),s=(0,P.useMemo)((()=>Array.from(n.values()).map((e=>R(e.label))).filter((e=>""===a||e.toLowerCase().includes(a.toLowerCase()))).sort(((e,t)=>e.localeCompare(t)))),[a,n]);return(0,k.createElement)(v.FormTokenField,{className:"patterns-menu-items__convert-modal-categories",value:e,suggestions:s,onChange:function(e){const n=e.reduce(((e,t)=>(e.some((e=>e.toLowerCase()===t.toLowerCase()))||e.push(t),e)),[]);t(n)},onInputChange:o,label:(0,l.__)("Categories"),tokenizeOnBlur:!0,__experimentalExpandOnFocus:!0,__next40pxDefaultSize:!0,__nextHasNoMarginBottom:!0})}function N({className:e="patterns-menu-items__convert-modal",modalTitle:t=(0,l.__)("Create pattern"),...n}){return(0,k.createElement)(v.Modal,{title:t,onRequestClose:n.onClose,overlayClassName:e},(0,k.createElement)(M,{...n}))}function M({confirmLabel:e=(0,l.__)("Create"),defaultCategories:t=[],content:n,onClose:a,onError:o,onSuccess:s,defaultSyncType:c=d.full,defaultTitle:u=""}){const[m,_]=(0,P.useState)(c),[g,y]=(0,P.useState)(t),[f,w]=(0,P.useState)(u),[E,C]=(0,P.useState)(!1),{createPattern:b}=S((0,r.useDispatch)(h)),{saveEntityRecord:T,invalidateResolution:B}=(0,r.useDispatch)(i.store),{createErrorNotice:R}=(0,r.useDispatch)(x.store),{corePatternCategories:N,userPatternCategories:M}=(0,r.useSelect)((e=>{const{getUserPatternCategories:t,getBlockPatternCategories:n}=e(i.store);return{corePatternCategories:n(),userPatternCategories:t()}})),O=(0,P.useMemo)((()=>{const e=new Map;return M.forEach((t=>{e.set(t.label.toLowerCase(),{label:t.label,name:t.name,id:t.id})})),N.forEach((t=>{e.has(t.label.toLowerCase())||"query"===t.name||e.set(t.label.toLowerCase(),{label:t.label,name:t.name})})),e}),[M,N]);async function A(e,t){if(f&&!E)try{C(!0);const a=await Promise.all(g.map((e=>async function(e){try{const t=O.get(e.toLowerCase());if(t&&t.id)return t.id;const n=t?{name:t.label,slug:t.name}:{name:e},a=await T("taxonomy",D,n,{throwOnError:!0});return B("getUserPatternCategories"),a.id}catch(e){if("term_exists"!==e.code)throw e;return e.data.term_id}}(e)))),r=await b(e,t,"function"==typeof n?n():n,a);s({pattern:r,categoryId:p})}catch(e){R(e.message,{type:"snackbar",id:"pattern-create"}),o?.()}finally{C(!1),y([]),w("")}}return(0,k.createElement)("form",{onSubmit:e=>{e.preventDefault(),A(f,m)}},(0,k.createElement)(v.__experimentalVStack,{spacing:"5"},(0,k.createElement)(v.TextControl,{label:(0,l.__)("Name"),value:f,onChange:w,placeholder:(0,l.__)("My pattern"),className:"patterns-create-modal__name-input",__nextHasNoMarginBottom:!0,__next40pxDefaultSize:!0}),(0,k.createElement)(I,{categoryTerms:g,onChange:y,categoryMap:O}),(0,k.createElement)(v.ToggleControl,{label:(0,l._x)("Synced","Option that makes an individual pattern synchronized"),help:(0,l.__)("Sync this pattern across multiple locations."),checked:m===d.full,onChange:()=>{_(m===d.full?d.unsynced:d.full)}}),(0,k.createElement)(v.__experimentalHStack,{justify:"right"},(0,k.createElement)(v.Button,{__next40pxDefaultSize:!0,variant:"tertiary",onClick:()=>{a(),w("")}},(0,l.__)("Cancel")),(0,k.createElement)(v.Button,{__next40pxDefaultSize:!0,variant:"primary",type:"submit","aria-disabled":!f||E,isBusy:E},e))))}function O(e,t){return e.type!==u.user?t.core?.filter((t=>e.categories.includes(t.name))).map((e=>e.label)):t.user?.filter((t=>e.wp_pattern_category.includes(t.id))).map((e=>e.label))}function A({pattern:e,onSuccess:t}){const{createSuccessNotice:n}=(0,r.useDispatch)(x.store),a=(0,r.useSelect)((e=>{const{getUserPatternCategories:t,getBlockPatternCategories:n}=e(i.store);return{core:n(),user:t()}}));return e?{content:e.content,defaultCategories:O(e,a),defaultSyncType:e.type!==u.user?d.unsynced:e.wp_pattern_sync_status||d.full,defaultTitle:(0,l.sprintf)((0,l.__)("%s (Copy)"),"string"==typeof e.title?e.title:e.title.raw),onSuccess:({pattern:e})=>{n((0,l.sprintf)((0,l.__)('"%s" duplicated.'),e.title.raw),{type:"snackbar",id:"patterns-create"}),t?.({pattern:e})}}:null}const L=window.wp.primitives,U=(0,k.createElement)(L.SVG,{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24"},(0,k.createElement)(L.Path,{d:"M21.3 10.8l-5.6-5.6c-.7-.7-1.8-.7-2.5 0l-5.6 5.6c-.7.7-.7 1.8 0 2.5l5.6 5.6c.3.3.8.5 1.2.5s.9-.2 1.2-.5l5.6-5.6c.8-.7.8-1.9.1-2.5zm-1 1.4l-5.6 5.6c-.1.1-.3.1-.4 0l-5.6-5.6c-.1-.1-.1-.3 0-.4l5.6-5.6s.1-.1.2-.1.1 0 .2.1l5.6 5.6c.1.1.1.3 0 .4zm-16.6-.4L10 5.5l-1-1-6.3 6.3c-.7.7-.7 1.8 0 2.5L9 19.5l1.1-1.1-6.3-6.3c-.2 0-.2-.2-.1-.3z"}));function z({clientIds:e,rootClientId:t,closeBlockSettingsMenu:n}){const{createSuccessNotice:a}=(0,r.useDispatch)(x.store),{replaceBlocks:o}=(0,r.useDispatch)(c.store),{setEditingPattern:u}=S((0,r.useDispatch)(h)),[p,m]=(0,P.useState)(!1),_=(0,r.useSelect)((n=>{var a;const{canUser:r}=n(i.store),{getBlocksByClientId:o,canInsertBlockType:l,getBlockRootClientId:u}=n(c.store),p=t||(e.length>0?u(e[0]):void 0),d=null!==(a=o(e))&&void 0!==a?a:[];return!(1===d.length&&d[0]&&(0,s.isReusableBlock)(d[0])&&!!n(i.store).getEntityRecord("postType","wp_block",d[0].attributes.ref))&&l("core/block",p)&&d.every((e=>!!e&&e.isValid&&(0,s.hasBlockSupport)(e.name,"reusable",!0)))&&!!r("create","blocks")}),[e,t]),{getBlocksByClientId:g}=(0,r.useSelect)(c.store),y=(0,P.useCallback)((()=>(0,s.serialize)(g(e))),[g,e]);if(!_)return null;return(0,k.createElement)(k.Fragment,null,(0,k.createElement)(v.MenuItem,{icon:U,onClick:()=>m(!0),"aria-expanded":p,"aria-haspopup":"dialog"},(0,l.__)("Create pattern")),p&&(0,k.createElement)(N,{content:y,onSuccess:t=>{(({pattern:t})=>{if(t.wp_pattern_sync_status!==d.unsynced){const a=(0,s.createBlock)("core/block",{ref:t.id});o(e,a),u(a.clientId,!0),n()}a(t.wp_pattern_sync_status===d.unsynced?(0,l.sprintf)((0,l.__)("Unsynced pattern created: %s"),t.title.raw):(0,l.sprintf)((0,l.__)("Synced pattern created: %s"),t.title.raw),{type:"snackbar",id:"convert-to-pattern-success"}),m(!1)})(t)},onError:()=>{m(!1)},onClose:()=>{m(!1)}}))}const j=window.wp.url;const F=function({clientId:e}){const{canRemove:t,isVisible:n,managePatternsUrl:a}=(0,r.useSelect)((t=>{const{getBlock:n,canRemoveBlock:a,getBlockCount:r,getSettings:o}=t(c.store),{canUser:l}=t(i.store),u=n(e),p=o().__unstableIsBlockBasedTheme;return{canRemove:a(e),isVisible:!!u&&(0,s.isReusableBlock)(u)&&!!l("update","blocks",u.attributes.ref),innerBlockCount:r(e),managePatternsUrl:p&&l("read","templates")?(0,j.addQueryArgs)("site-editor.php",{path:"/patterns"}):(0,j.addQueryArgs)("edit.php",{post_type:"wp_block"})}}),[e]),{convertSyncedPatternToStatic:o}=S((0,r.useDispatch)(h));return n?(0,k.createElement)(k.Fragment,null,t&&(0,k.createElement)(v.MenuItem,{onClick:()=>o(e)},(0,l.__)("Detach")),(0,k.createElement)(v.MenuItem,{href:a},(0,l.__)("Manage patterns"))):null};const V=window.wp.a11y;const H={};C(H,{CreatePatternModal:N,CreatePatternModalContents:M,DuplicatePatternModal:function({pattern:e,onClose:t,onSuccess:n}){const a=A({pattern:e,onSuccess:n});return e?(0,k.createElement)(N,{modalTitle:(0,l.__)("Duplicate pattern"),confirmLabel:(0,l.__)("Duplicate"),onClose:t,onError:t,...a}):null},useDuplicatePatternProps:A,RenamePatternModal:function({onClose:e,onError:t,onSuccess:n,pattern:a,...o}){const s=(0,B.decodeEntities)(a.title),[c,u]=(0,P.useState)(s),[p,d]=(0,P.useState)(!1),{editEntityRecord:m,__experimentalSaveSpecifiedEntityEdits:_}=(0,r.useDispatch)(i.store),{createSuccessNotice:g,createErrorNotice:y}=(0,r.useDispatch)(x.store);return(0,k.createElement)(v.Modal,{title:(0,l.__)("Rename"),...o,onRequestClose:e},(0,k.createElement)("form",{onSubmit:async r=>{if(r.preventDefault(),c&&c!==a.title&&!p)try{await m("postType",a.type,a.id,{title:c}),d(!0),u(""),e?.();const t=await _("postType",a.type,a.id,["title"],{throwOnError:!0});n?.(t),g((0,l.__)("Pattern renamed"),{type:"snackbar",id:"pattern-update"})}catch(e){t?.();const n=e.message&&"unknown_error"!==e.code?e.message:(0,l.__)("An error occurred while renaming the pattern.");y(n,{type:"snackbar",id:"pattern-update"})}finally{d(!1),u("")}}},(0,k.createElement)(v.__experimentalVStack,{spacing:"5"},(0,k.createElement)(v.TextControl,{__nextHasNoMarginBottom:!0,__next40pxDefaultSize:!0,label:(0,l.__)("Name"),value:c,onChange:u,required:!0}),(0,k.createElement)(v.__experimentalHStack,{justify:"right"},(0,k.createElement)(v.Button,{__next40pxDefaultSize:!0,variant:"tertiary",onClick:()=>{e?.(),u("")}},(0,l.__)("Cancel")),(0,k.createElement)(v.Button,{__next40pxDefaultSize:!0,variant:"primary",type:"submit"},(0,l.__)("Save"))))))},PatternsMenuItems:function({rootClientId:e}){return(0,k.createElement)(c.BlockSettingsMenuControls,null,(({selectedClientIds:t,onClose:n})=>(0,k.createElement)(k.Fragment,null,(0,k.createElement)(z,{clientIds:t,rootClientId:e,closeBlockSettingsMenu:n}),1===t.length&&(0,k.createElement)(F,{clientId:t[0]}))))},RenamePatternCategoryModal:function({category:e,existingCategories:t,onClose:n,onError:a,onSuccess:o,...s}){const c=(0,P.useId)(),u=(0,P.useRef)(),[p,d]=(0,P.useState)((0,B.decodeEntities)(e.name)),[m,_]=(0,P.useState)(!1),[g,y]=(0,P.useState)(!1),f=g?`patterns-rename-pattern-category-modal__validation-message-${c}`:void 0,{saveEntityRecord:w,invalidateResolution:E}=(0,r.useDispatch)(i.store),{createErrorNotice:C,createSuccessNotice:S}=(0,r.useDispatch)(x.store),b=()=>{n(),d("")};return(0,k.createElement)(v.Modal,{title:(0,l.__)("Rename"),onRequestClose:b,...s},(0,k.createElement)("form",{onSubmit:async r=>{if(r.preventDefault(),!m){if(!p||p===e.name){const e=(0,l.__)("Please enter a new name for this category.");return(0,V.speak)(e,"assertive"),y(e),void u.current?.focus()}if(t.patternCategories.find((t=>t.id!==e.id&&t.label.toLowerCase()===p.toLowerCase()))){const e=(0,l.__)("This category already exists. Please use a different name.");return(0,V.speak)(e,"assertive"),y(e),void u.current?.focus()}try{_(!0);const t=await w("taxonomy",D,{id:e.id,slug:e.slug,name:p});E("getUserPatternCategories"),o?.(t),n(),S((0,l.__)("Pattern category renamed."),{type:"snackbar",id:"pattern-category-update"})}catch(e){a?.(),C(e.message,{type:"snackbar",id:"pattern-category-update"})}finally{_(!1),d("")}}}},(0,k.createElement)(v.__experimentalVStack,{spacing:"5"},(0,k.createElement)(v.__experimentalVStack,{spacing:"2"},(0,k.createElement)(v.TextControl,{ref:u,__nextHasNoMarginBottom:!0,__next40pxDefaultSize:!0,label:(0,l.__)("Name"),value:p,onChange:e=>{g&&y(void 0),d(e)},"aria-describedby":f,required:!0}),g&&(0,k.createElement)("span",{className:"patterns-rename-pattern-category-modal__validation-message",id:f},g)),(0,k.createElement)(v.__experimentalHStack,{justify:"right"},(0,k.createElement)(v.Button,{__next40pxDefaultSize:!0,variant:"tertiary",onClick:b},(0,l.__)("Cancel")),(0,k.createElement)(v.Button,{__next40pxDefaultSize:!0,variant:"primary",type:"submit","aria-disabled":!p||p===e.name||m,isBusy:m},(0,l.__)("Save"))))))},PartialSyncingControls:function({name:e,attributes:t,setAttributes:n}){const a=m[e],r=Object.keys(a).map((e=>t.metadata?.bindings?.[e]?.source));if(r.every((e=>e&&"core/pattern-overrides"!==e)))return null;function o(e){let r={...t?.metadata?.bindings};if(!e){for(const e of Object.keys(a))"core/pattern-overrides"===r[e]?.source&&delete r[e];return Object.keys(r).length||(r=void 0),void n({metadata:{...t.metadata,bindings:r}})}for(const e of Object.keys(a))r[e]||(r[e]={source:"core/pattern-overrides"});if("string"==typeof t.metadata?.id)return void n({metadata:{...t.metadata,bindings:r}});const o=((e=21)=>crypto.getRandomValues(new Uint8Array(e)).reduce(((e,t)=>e+((t&=63)<36?t.toString(36):t<62?(t-26).toString(36).toUpperCase():t>62?"-":"_")),""))(6);n({metadata:{...t.metadata,id:o,bindings:r}})}return(0,k.createElement)(c.InspectorControls,{group:"advanced"},(0,k.createElement)(v.BaseControl,{__nextHasNoMarginBottom:!0},(0,k.createElement)(v.BaseControl.VisualLabel,null,(0,l.__)("Pattern overrides")),(0,k.createElement)(v.CheckboxControl,{__nextHasNoMarginBottom:!0,label:(0,l.__)("Allow instance overrides"),checked:r.some((e=>"core/pattern-overrides"===e)),onChange:e=>{o(e)}})))},PATTERN_TYPES:u,PATTERN_DEFAULT_CATEGORY:p,PATTERN_USER_CATEGORY:"my-patterns",EXCLUDED_PATTERN_SOURCES:["core","pattern-directory/core","pattern-directory/featured"],PATTERN_SYNC_TYPES:d,PARTIAL_SYNCING_SUPPORTED_BLOCKS:m}),(window.wp=window.wp||{}).patterns=t})(); |