WordPress/wp-includes/js/dist/patterns.min.js

2 lines
16 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:()=>G,store:()=>k});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"},d="all-patterns",p={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"),rel:(0,l.__)("Link Relationship")},"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===p.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:b,unlock:C}=(0,E.__dangerousOptInToUnstableAPIsOnlyForCoreModules)("I know using unstable features means my theme or plugin will inevitably break in the next version of WordPress.","@wordpress/patterns"),S={reducer:o},k=(0,r.createReduxStore)("core/patterns",{...S});(0,r.register)(k),C(k).registerPrivateActions(n),C(k).registerPrivateSelectors(a);const v=window.React,h=window.wp.components,P=window.wp.element,T=window.wp.notices,x=window.wp.compose,B=window.wp.htmlEntities,R=e=>(0,B.decodeEntities)(e),I="wp_pattern_category";function D({categoryTerms:e,onChange:t,categoryMap:n}){const[a,r]=(0,P.useState)(""),o=(0,x.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,v.createElement)(h.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,v.createElement)(h.Modal,{title:t,onRequestClose:n.onClose,overlayClassName:e},(0,v.createElement)(M,{...n}))}function M({confirmLabel:e=(0,l.__)("Create"),defaultCategories:t=[],content:n,onClose:a,onError:o,onSuccess:s,defaultSyncType:c=p.full,defaultTitle:u=""}){const[m,_]=(0,P.useState)(c),[g,y]=(0,P.useState)(t),[f,w]=(0,P.useState)(u),[E,b]=(0,P.useState)(!1),{createPattern:S}=C((0,r.useDispatch)(k)),{saveEntityRecord:x,invalidateResolution:B}=(0,r.useDispatch)(i.store),{createErrorNotice:R}=(0,r.useDispatch)(T.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{b(!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 x("taxonomy",I,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 S(e,t,"function"==typeof n?n():n,a);s({pattern:r,categoryId:d})}catch(e){R(e.message,{type:"snackbar",id:"pattern-create"}),o?.()}finally{b(!1),y([]),w("")}}return(0,v.createElement)("form",{onSubmit:e=>{e.preventDefault(),A(f,m)}},(0,v.createElement)(h.__experimentalVStack,{spacing:"5"},(0,v.createElement)(h.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,v.createElement)(D,{categoryTerms:g,onChange:y,categoryMap:O}),(0,v.createElement)(h.ToggleControl,{label:(0,l._x)("Synced","Option that makes an individual pattern synchronized"),help:(0,l.__)("Sync this pattern across multiple locations."),checked:m===p.full,onChange:()=>{_(m===p.full?p.unsynced:p.full)}}),(0,v.createElement)(h.__experimentalHStack,{justify:"right"},(0,v.createElement)(h.Button,{__next40pxDefaultSize:!0,variant:"tertiary",onClick:()=>{a(),w("")}},(0,l.__)("Cancel")),(0,v.createElement)(h.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)(T.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?p.unsynced:e.wp_pattern_sync_status||p.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,v.createElement)(L.SVG,{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24"},(0,v.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)(T.store),{replaceBlocks:o}=(0,r.useDispatch)(c.store),{setEditingPattern:u}=C((0,r.useDispatch)(k)),[d,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),d=t||(e.length>0?u(e[0]):void 0),p=null!==(a=o(e))&&void 0!==a?a:[];return!(1===p.length&&p[0]&&(0,s.isReusableBlock)(p[0])&&!!n(i.store).getEntityRecord("postType","wp_block",p[0].attributes.ref))&&l("core/block",d)&&p.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,v.createElement)(v.Fragment,null,(0,v.createElement)(h.MenuItem,{icon:U,onClick:()=>m(!0),"aria-expanded":d,"aria-haspopup":"dialog"},(0,l.__)("Create pattern")),d&&(0,v.createElement)(N,{content:y,onSuccess:t=>{(({pattern:t})=>{if(t.wp_pattern_sync_status!==p.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===p.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),d=o().__unstableIsBlockBasedTheme;return{canRemove:a(e),isVisible:!!u&&(0,s.isReusableBlock)(u)&&!!l("update","blocks",u.attributes.ref),innerBlockCount:r(e),managePatternsUrl:d&&l("read","templates")?(0,j.addQueryArgs)("site-editor.php",{path:"/patterns"}):(0,j.addQueryArgs)("edit.php",{post_type:"wp_block"})}}),[e]),{convertSyncedPatternToStatic:o}=C((0,r.useDispatch)(k));return n?(0,v.createElement)(v.Fragment,null,t&&(0,v.createElement)(h.MenuItem,{onClick:()=>o(e)},(0,l.__)("Detach")),(0,v.createElement)(h.MenuItem,{href:a},(0,l.__)("Manage patterns"))):null};const V=window.wp.a11y;function H(e,t){for(const n of e){if(n.attributes.metadata?.id===t)return n;const e=H(n.innerBlocks,t);if(e)return e}}const G={};b(G,{CreatePatternModal:N,CreatePatternModalContents:M,DuplicatePatternModal:function({pattern:e,onClose:t,onSuccess:n}){const a=A({pattern:e,onSuccess:n});return e?(0,v.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),[d,p]=(0,P.useState)(!1),{editEntityRecord:m,__experimentalSaveSpecifiedEntityEdits:_}=(0,r.useDispatch)(i.store),{createSuccessNotice:g,createErrorNotice:y}=(0,r.useDispatch)(T.store);return(0,v.createElement)(h.Modal,{title:(0,l.__)("Rename"),...o,onRequestClose:e},(0,v.createElement)("form",{onSubmit:async r=>{if(r.preventDefault(),c&&c!==a.title&&!d)try{await m("postType",a.type,a.id,{title:c}),p(!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{p(!1),u("")}}},(0,v.createElement)(h.__experimentalVStack,{spacing:"5"},(0,v.createElement)(h.TextControl,{__nextHasNoMarginBottom:!0,__next40pxDefaultSize:!0,label:(0,l.__)("Name"),value:c,onChange:u,required:!0}),(0,v.createElement)(h.__experimentalHStack,{justify:"right"},(0,v.createElement)(h.Button,{__next40pxDefaultSize:!0,variant:"tertiary",onClick:()=>{e?.(),u("")}},(0,l.__)("Cancel")),(0,v.createElement)(h.Button,{__next40pxDefaultSize:!0,variant:"primary",type:"submit"},(0,l.__)("Save"))))))},PatternsMenuItems:function({rootClientId:e}){return(0,v.createElement)(c.BlockSettingsMenuControls,null,(({selectedClientIds:t,onClose:n})=>(0,v.createElement)(v.Fragment,null,(0,v.createElement)(z,{clientIds:t,rootClientId:e,closeBlockSettingsMenu:n}),1===t.length&&(0,v.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)(),[d,p]=(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:b,createSuccessNotice:C}=(0,r.useDispatch)(T.store),S=()=>{n(),p("")};return(0,v.createElement)(h.Modal,{title:(0,l.__)("Rename"),onRequestClose:S,...s},(0,v.createElement)("form",{onSubmit:async r=>{if(r.preventDefault(),!m){if(!d||d===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()===d.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",I,{id:e.id,slug:e.slug,name:d});E("getUserPatternCategories"),o?.(t),n(),C((0,l.__)("Pattern category renamed."),{type:"snackbar",id:"pattern-category-update"})}catch(e){a?.(),b(e.message,{type:"snackbar",id:"pattern-category-update"})}finally{_(!1),p("")}}}},(0,v.createElement)(h.__experimentalVStack,{spacing:"5"},(0,v.createElement)(h.__experimentalVStack,{spacing:"2"},(0,v.createElement)(h.TextControl,{ref:u,__nextHasNoMarginBottom:!0,__next40pxDefaultSize:!0,label:(0,l.__)("Name"),value:d,onChange:e=>{g&&y(void 0),p(e)},"aria-describedby":f,required:!0}),g&&(0,v.createElement)("span",{className:"patterns-rename-pattern-category-modal__validation-message",id:f},g)),(0,v.createElement)(h.__experimentalHStack,{justify:"right"},(0,v.createElement)(h.Button,{__next40pxDefaultSize:!0,variant:"tertiary",onClick:S},(0,l.__)("Cancel")),(0,v.createElement)(h.Button,{__next40pxDefaultSize:!0,variant:"primary",type:"submit","aria-disabled":!d||d===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,v.createElement)(c.InspectorControls,{group:"advanced"},(0,v.createElement)(h.BaseControl,{__nextHasNoMarginBottom:!0},(0,v.createElement)(h.BaseControl.VisualLabel,null,(0,l.__)("Pattern overrides")),(0,v.createElement)(h.CheckboxControl,{__nextHasNoMarginBottom:!0,label:(0,l.__)("Allow instance overrides"),checked:r.some((e=>"core/pattern-overrides"===e)),onChange:e=>{o(e)}})))},ResetOverridesControl:function(e){const t=(0,r.useRegistry)(),n=e.attributes.metadata?.id,a=(0,r.useSelect)((t=>{if(!n)return;const{getBlockParentsByBlockName:a,getBlocksByClientId:r}=t(c.store),o=r(a(e.clientId,"core/block"))[0];return o?.attributes.content?.[n]?o:void 0}),[e.clientId,n]);return(0,v.createElement)(c.BlockControls,{group:"other"},(0,v.createElement)(h.ToolbarGroup,null,(0,v.createElement)(h.ToolbarButton,{onClick:async()=>{var r;const o=await t.resolveSelect(i.store).getEditedEntityRecord("postType","wp_block",a.attributes.ref),c=H(null!==(r=o.blocks)&&void 0!==r?r:(0,s.parse)(o.content),n);e.setAttributes(c.attributes)},disabled:!a,__experimentalIsFocusable:!0},(0,l.__)("Reset"))))},PATTERN_TYPES:u,PATTERN_DEFAULT_CATEGORY:d,PATTERN_USER_CATEGORY:"my-patterns",EXCLUDED_PATTERN_SOURCES:["core","pattern-directory/core","pattern-directory/featured"],PATTERN_SYNC_TYPES:p,PARTIAL_SYNCING_SUPPORTED_BLOCKS:m}),(window.wp=window.wp||{}).patterns=t})();