@wordpress/fields 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.11.0 (2025-02-28)
6
+
5
7
  ## 0.10.0 (2025-02-12)
6
8
 
7
9
  ## 0.9.0 (2025-01-29)
@@ -27,6 +27,7 @@ var _jsxRuntime = require("react/jsx-runtime");
27
27
  */
28
28
  // @ts-expect-error block-editor is not typed correctly.
29
29
 
30
+ const EMPTY_ARRAY = [];
30
31
  const TemplateEdit = ({
31
32
  data,
32
33
  field,
@@ -39,14 +40,14 @@ const TemplateEdit = ({
39
40
  const postId = typeof data.id === 'number' ? data.id : parseInt(data.id, 10);
40
41
  const slug = data.slug;
41
42
  const {
42
- availableTemplates,
43
+ canSwitchTemplate,
43
44
  templates
44
45
  } = (0, _data.useSelect)(select => {
45
46
  var _select$getEntityReco;
46
47
  const allTemplates = (_select$getEntityReco = select(_coreData.store).getEntityRecords('postType', 'wp_template', {
47
48
  per_page: -1,
48
49
  post_type: postType
49
- })) !== null && _select$getEntityReco !== void 0 ? _select$getEntityReco : [];
50
+ })) !== null && _select$getEntityReco !== void 0 ? _select$getEntityReco : EMPTY_ARRAY;
50
51
  const {
51
52
  getHomePage,
52
53
  getPostsPageId
@@ -56,22 +57,28 @@ const TemplateEdit = ({
56
57
  const allowSwitchingTemplate = !isPostsPage && !isFrontPage;
57
58
  return {
58
59
  templates: allTemplates,
59
- availableTemplates: allowSwitchingTemplate ? allTemplates.filter(template => template.is_custom && template.slug !== data.template && !!template.content.raw // Skip empty templates.
60
- ) : []
60
+ canSwitchTemplate: allowSwitchingTemplate
61
61
  };
62
- }, [data.template, postId, postType]);
63
- const templatesAsPatterns = (0, _element.useMemo)(() => availableTemplates.map(template => ({
64
- name: template.slug,
65
- blocks: (0, _blocks.parse)(template.content.raw),
66
- title: (0, _htmlEntities.decodeEntities)(template.title.rendered),
67
- id: template.id
68
- })), [availableTemplates]);
62
+ }, [postId, postType]);
63
+ const templatesAsPatterns = (0, _element.useMemo)(() => {
64
+ if (!canSwitchTemplate) {
65
+ return [];
66
+ }
67
+ return templates.filter(template => template.is_custom && template.slug !== data.template &&
68
+ // Skip empty templates.
69
+ !!template.content.raw).map(template => ({
70
+ name: template.slug,
71
+ blocks: (0, _blocks.parse)(template.content.raw),
72
+ title: (0, _htmlEntities.decodeEntities)(template.title.rendered),
73
+ id: template.id
74
+ }));
75
+ }, [canSwitchTemplate, data.template, templates]);
69
76
  const shownTemplates = (0, _compose.useAsyncList)(templatesAsPatterns);
70
77
  const value = field.getValue({
71
78
  item: data
72
79
  });
80
+ const foundTemplate = templates.find(template => template.slug === value);
73
81
  const currentTemplate = (0, _data.useSelect)(select => {
74
- const foundTemplate = templates?.find(template => template.slug === value);
75
82
  if (foundTemplate) {
76
83
  return foundTemplate;
77
84
  }
@@ -91,7 +98,7 @@ const TemplateEdit = ({
91
98
  });
92
99
  return select(_coreData.store).getEntityRecord('postType', 'wp_template', templateId);
93
100
  }
94
- }, [postType, slug, templates, value]);
101
+ }, [foundTemplate, postType, slug]);
95
102
  const [showModal, setShowModal] = (0, _element.useState)(false);
96
103
  const onChangeControl = (0, _element.useCallback)(newValue => onChange({
97
104
  [id]: newValue
@@ -1 +1 @@
1
- {"version":3,"names":["_element","require","_blocks","_coreData","_blockEditor","_components","_compose","_data","_htmlEntities","_i18n","_utils","_lockUnlock","_jsxRuntime","TemplateEdit","data","field","onChange","id","postType","type","postId","parseInt","slug","availableTemplates","templates","useSelect","select","_select$getEntityReco","allTemplates","coreStore","getEntityRecords","per_page","post_type","getHomePage","getPostsPageId","unlock","isPostsPage","isFrontPage","allowSwitchingTemplate","filter","template","is_custom","content","raw","templatesAsPatterns","useMemo","map","name","blocks","parse","title","decodeEntities","rendered","shownTemplates","useAsyncList","value","getValue","item","currentTemplate","foundTemplate","find","slugToCheck","templateId","getDefaultTemplateId","getEntityRecord","showModal","setShowModal","useState","onChangeControl","useCallback","newValue","jsxs","className","children","jsx","Dropdown","popoverProps","placement","renderToggle","onToggle","Button","__next40pxDefaultSize","variant","size","onClick","getItemTitle","renderContent","MenuGroup","MenuItem","__","Modal","onRequestClose","overlayClassName","isFullScreen","__experimentalBlockPatternsList","label","blockPatterns","shownPatterns","onClickPattern","exports"],"sources":["@wordpress/fields/src/fields/template/template-edit.tsx"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCallback, useMemo, useState } from '@wordpress/element';\n// @ts-ignore\nimport { parse } from '@wordpress/blocks';\nimport type { WpTemplate } from '@wordpress/core-data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { DataFormControlProps } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\n// @ts-expect-error block-editor is not typed correctly.\nimport { __experimentalBlockPatternsList as BlockPatternsList } from '@wordpress/block-editor';\nimport {\n\tButton,\n\tDropdown,\n\tMenuGroup,\n\tMenuItem,\n\tModal,\n} from '@wordpress/components';\nimport { useAsyncList } from '@wordpress/compose';\nimport { useSelect } from '@wordpress/data';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { __ } from '@wordpress/i18n';\nimport { getItemTitle } from '../../actions/utils';\nimport type { BasePost } from '../../types';\nimport { unlock } from '../../lock-unlock';\n\nexport const TemplateEdit = ( {\n\tdata,\n\tfield,\n\tonChange,\n}: DataFormControlProps< BasePost > ) => {\n\tconst { id } = field;\n\tconst postType = data.type;\n\tconst postId =\n\t\ttypeof data.id === 'number' ? data.id : parseInt( data.id, 10 );\n\tconst slug = data.slug;\n\n\tconst { availableTemplates, templates } = useSelect(\n\t\t( select ) => {\n\t\t\tconst allTemplates =\n\t\t\t\tselect( coreStore ).getEntityRecords< WpTemplate >(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template',\n\t\t\t\t\t{\n\t\t\t\t\t\tper_page: -1,\n\t\t\t\t\t\tpost_type: postType,\n\t\t\t\t\t}\n\t\t\t\t) ?? [];\n\n\t\t\tconst { getHomePage, getPostsPageId } = unlock(\n\t\t\t\tselect( coreStore )\n\t\t\t);\n\n\t\t\tconst isPostsPage = getPostsPageId() === +postId;\n\t\t\tconst isFrontPage =\n\t\t\t\tpostType === 'page' && getHomePage()?.postId === +postId;\n\n\t\t\tconst allowSwitchingTemplate = ! isPostsPage && ! isFrontPage;\n\n\t\t\treturn {\n\t\t\t\ttemplates: allTemplates,\n\t\t\t\tavailableTemplates: allowSwitchingTemplate\n\t\t\t\t\t? allTemplates.filter(\n\t\t\t\t\t\t\t( template ) =>\n\t\t\t\t\t\t\t\ttemplate.is_custom &&\n\t\t\t\t\t\t\t\ttemplate.slug !== data.template &&\n\t\t\t\t\t\t\t\t!! template.content.raw // Skip empty templates.\n\t\t\t\t\t )\n\t\t\t\t\t: [],\n\t\t\t};\n\t\t},\n\t\t[ data.template, postId, postType ]\n\t);\n\n\tconst templatesAsPatterns = useMemo(\n\t\t() =>\n\t\t\tavailableTemplates.map( ( template ) => ( {\n\t\t\t\tname: template.slug,\n\t\t\t\tblocks: parse( template.content.raw ),\n\t\t\t\ttitle: decodeEntities( template.title.rendered ),\n\t\t\t\tid: template.id,\n\t\t\t} ) ),\n\t\t[ availableTemplates ]\n\t);\n\n\tconst shownTemplates = useAsyncList( templatesAsPatterns );\n\n\tconst value = field.getValue( { item: data } );\n\n\tconst currentTemplate = useSelect(\n\t\t( select ) => {\n\t\t\tconst foundTemplate = templates?.find(\n\t\t\t\t( template ) => template.slug === value\n\t\t\t);\n\n\t\t\tif ( foundTemplate ) {\n\t\t\t\treturn foundTemplate;\n\t\t\t}\n\n\t\t\tlet slugToCheck;\n\t\t\t// In `draft` status we might not have a slug available, so we use the `single`\n\t\t\t// post type templates slug(ex page, single-post, single-product etc..).\n\t\t\t// Pages do not need the `single` prefix in the slug to be prioritized\n\t\t\t// through template hierarchy.\n\t\t\tif ( slug ) {\n\t\t\t\tslugToCheck =\n\t\t\t\t\tpostType === 'page'\n\t\t\t\t\t\t? `${ postType }-${ slug }`\n\t\t\t\t\t\t: `single-${ postType }-${ slug }`;\n\t\t\t} else {\n\t\t\t\tslugToCheck =\n\t\t\t\t\tpostType === 'page' ? 'page' : `single-${ postType }`;\n\t\t\t}\n\n\t\t\tif ( postType ) {\n\t\t\t\tconst templateId = select( coreStore ).getDefaultTemplateId( {\n\t\t\t\t\tslug: slugToCheck,\n\t\t\t\t} );\n\n\t\t\t\treturn select( coreStore ).getEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template',\n\t\t\t\t\ttemplateId\n\t\t\t\t);\n\t\t\t}\n\t\t},\n\t\t[ postType, slug, templates, value ]\n\t);\n\n\tconst [ showModal, setShowModal ] = useState( false );\n\n\tconst onChangeControl = useCallback(\n\t\t( newValue: string ) =>\n\t\t\tonChange( {\n\t\t\t\t[ id ]: newValue,\n\t\t\t} ),\n\t\t[ id, onChange ]\n\t);\n\n\treturn (\n\t\t<fieldset className=\"fields-controls__template\">\n\t\t\t<Dropdown\n\t\t\t\tpopoverProps={ { placement: 'bottom-start' } }\n\t\t\t\trenderToggle={ ( { onToggle } ) => (\n\t\t\t\t\t<Button\n\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\tvariant=\"tertiary\"\n\t\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\t\tonClick={ onToggle }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ currentTemplate\n\t\t\t\t\t\t\t? getItemTitle( currentTemplate )\n\t\t\t\t\t\t\t: '' }\n\t\t\t\t\t</Button>\n\t\t\t\t) }\n\t\t\t\trenderContent={ ( { onToggle } ) => (\n\t\t\t\t\t<MenuGroup>\n\t\t\t\t\t\t<MenuItem\n\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\tsetShowModal( true );\n\t\t\t\t\t\t\t\tonToggle();\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{ __( 'Change template' ) }\n\t\t\t\t\t\t</MenuItem>\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t// The default template in a post is indicated by an empty string\n\t\t\t\t\t\t\tvalue !== '' && (\n\t\t\t\t\t\t\t\t<MenuItem\n\t\t\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\t\t\tonChangeControl( '' );\n\t\t\t\t\t\t\t\t\t\tonToggle();\n\t\t\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t{ __( 'Use default template' ) }\n\t\t\t\t\t\t\t\t</MenuItem>\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t</MenuGroup>\n\t\t\t\t) }\n\t\t\t/>\n\t\t\t{ showModal && (\n\t\t\t\t<Modal\n\t\t\t\t\ttitle={ __( 'Choose a template' ) }\n\t\t\t\t\tonRequestClose={ () => setShowModal( false ) }\n\t\t\t\t\toverlayClassName=\"fields-controls__template-modal\"\n\t\t\t\t\tisFullScreen\n\t\t\t\t>\n\t\t\t\t\t<div className=\"fields-controls__template-content\">\n\t\t\t\t\t\t<BlockPatternsList\n\t\t\t\t\t\t\tlabel={ __( 'Templates' ) }\n\t\t\t\t\t\t\tblockPatterns={ templatesAsPatterns }\n\t\t\t\t\t\t\tshownPatterns={ shownTemplates }\n\t\t\t\t\t\t\tonClickPattern={ (\n\t\t\t\t\t\t\t\ttemplate: ( typeof templatesAsPatterns )[ 0 ]\n\t\t\t\t\t\t\t) => {\n\t\t\t\t\t\t\t\tonChangeControl( template.name );\n\t\t\t\t\t\t\t\tsetShowModal( false );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t/>\n\t\t\t\t\t</div>\n\t\t\t\t</Modal>\n\t\t\t) }\n\t\t</fieldset>\n\t);\n};\n"],"mappings":";;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAEA,IAAAC,OAAA,GAAAD,OAAA;AAEA,IAAAE,SAAA,GAAAF,OAAA;AAOA,IAAA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,mBAAqB;UACrCqD,aAAa,EAAG5C,cAAgB;UAChC6C,cAAc,EACb1D,QAA6C,IACzC;YACJ4B,eAAe,CAAE5B,QAAQ,CAACO,IAAK,CAAC;YAChCmB,YAAY,CAAE,KAAM,CAAC;UACtB;QAAG,CACH;MAAC,CACE;IAAC,CACA,CACP;EAAA,CACQ,CAAC;AAEb,CAAC;AAACiC,OAAA,CAAAtF,YAAA,GAAAA,YAAA","ignoreList":[]}
1
+ {"version":3,"names":["_element","require","_blocks","_coreData","_blockEditor","_components","_compose","_data","_htmlEntities","_i18n","_utils","_lockUnlock","_jsxRuntime","EMPTY_ARRAY","TemplateEdit","data","field","onChange","id","postType","type","postId","parseInt","slug","canSwitchTemplate","templates","useSelect","select","_select$getEntityReco","allTemplates","coreStore","getEntityRecords","per_page","post_type","getHomePage","getPostsPageId","unlock","isPostsPage","isFrontPage","allowSwitchingTemplate","templatesAsPatterns","useMemo","filter","template","is_custom","content","raw","map","name","blocks","parse","title","decodeEntities","rendered","shownTemplates","useAsyncList","value","getValue","item","foundTemplate","find","currentTemplate","slugToCheck","templateId","getDefaultTemplateId","getEntityRecord","showModal","setShowModal","useState","onChangeControl","useCallback","newValue","jsxs","className","children","jsx","Dropdown","popoverProps","placement","renderToggle","onToggle","Button","__next40pxDefaultSize","variant","size","onClick","getItemTitle","renderContent","MenuGroup","MenuItem","__","Modal","onRequestClose","overlayClassName","isFullScreen","__experimentalBlockPatternsList","label","blockPatterns","shownPatterns","onClickPattern","exports"],"sources":["@wordpress/fields/src/fields/template/template-edit.tsx"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCallback, useMemo, useState } from '@wordpress/element';\n// @ts-ignore\nimport { parse } from '@wordpress/blocks';\nimport type { WpTemplate } from '@wordpress/core-data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { DataFormControlProps } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\n// @ts-expect-error block-editor is not typed correctly.\nimport { __experimentalBlockPatternsList as BlockPatternsList } from '@wordpress/block-editor';\nimport {\n\tButton,\n\tDropdown,\n\tMenuGroup,\n\tMenuItem,\n\tModal,\n} from '@wordpress/components';\nimport { useAsyncList } from '@wordpress/compose';\nimport { useSelect } from '@wordpress/data';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { __ } from '@wordpress/i18n';\nimport { getItemTitle } from '../../actions/utils';\nimport type { BasePost } from '../../types';\nimport { unlock } from '../../lock-unlock';\n\nconst EMPTY_ARRAY: [] = [];\n\nexport const TemplateEdit = ( {\n\tdata,\n\tfield,\n\tonChange,\n}: DataFormControlProps< BasePost > ) => {\n\tconst { id } = field;\n\tconst postType = data.type;\n\tconst postId =\n\t\ttypeof data.id === 'number' ? data.id : parseInt( data.id, 10 );\n\tconst slug = data.slug;\n\n\tconst { canSwitchTemplate, templates } = useSelect(\n\t\t( select ) => {\n\t\t\tconst allTemplates =\n\t\t\t\tselect( coreStore ).getEntityRecords< WpTemplate >(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template',\n\t\t\t\t\t{\n\t\t\t\t\t\tper_page: -1,\n\t\t\t\t\t\tpost_type: postType,\n\t\t\t\t\t}\n\t\t\t\t) ?? EMPTY_ARRAY;\n\n\t\t\tconst { getHomePage, getPostsPageId } = unlock(\n\t\t\t\tselect( coreStore )\n\t\t\t);\n\n\t\t\tconst isPostsPage = getPostsPageId() === +postId;\n\t\t\tconst isFrontPage =\n\t\t\t\tpostType === 'page' && getHomePage()?.postId === +postId;\n\n\t\t\tconst allowSwitchingTemplate = ! isPostsPage && ! isFrontPage;\n\n\t\t\treturn {\n\t\t\t\ttemplates: allTemplates,\n\t\t\t\tcanSwitchTemplate: allowSwitchingTemplate,\n\t\t\t};\n\t\t},\n\t\t[ postId, postType ]\n\t);\n\n\tconst templatesAsPatterns = useMemo( () => {\n\t\tif ( ! canSwitchTemplate ) {\n\t\t\treturn [];\n\t\t}\n\t\treturn templates\n\t\t\t.filter(\n\t\t\t\t( template ) =>\n\t\t\t\t\ttemplate.is_custom &&\n\t\t\t\t\ttemplate.slug !== data.template &&\n\t\t\t\t\t// Skip empty templates.\n\t\t\t\t\t!! template.content.raw\n\t\t\t)\n\t\t\t.map( ( template ) => ( {\n\t\t\t\tname: template.slug,\n\t\t\t\tblocks: parse( template.content.raw ),\n\t\t\t\ttitle: decodeEntities( template.title.rendered ),\n\t\t\t\tid: template.id,\n\t\t\t} ) );\n\t}, [ canSwitchTemplate, data.template, templates ] );\n\n\tconst shownTemplates = useAsyncList( templatesAsPatterns );\n\n\tconst value = field.getValue( { item: data } );\n\tconst foundTemplate = templates.find(\n\t\t( template ) => template.slug === value\n\t);\n\n\tconst currentTemplate = useSelect(\n\t\t( select ) => {\n\t\t\tif ( foundTemplate ) {\n\t\t\t\treturn foundTemplate;\n\t\t\t}\n\n\t\t\tlet slugToCheck;\n\t\t\t// In `draft` status we might not have a slug available, so we use the `single`\n\t\t\t// post type templates slug(ex page, single-post, single-product etc..).\n\t\t\t// Pages do not need the `single` prefix in the slug to be prioritized\n\t\t\t// through template hierarchy.\n\t\t\tif ( slug ) {\n\t\t\t\tslugToCheck =\n\t\t\t\t\tpostType === 'page'\n\t\t\t\t\t\t? `${ postType }-${ slug }`\n\t\t\t\t\t\t: `single-${ postType }-${ slug }`;\n\t\t\t} else {\n\t\t\t\tslugToCheck =\n\t\t\t\t\tpostType === 'page' ? 'page' : `single-${ postType }`;\n\t\t\t}\n\n\t\t\tif ( postType ) {\n\t\t\t\tconst templateId = select( coreStore ).getDefaultTemplateId( {\n\t\t\t\t\tslug: slugToCheck,\n\t\t\t\t} );\n\n\t\t\t\treturn select( coreStore ).getEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template',\n\t\t\t\t\ttemplateId\n\t\t\t\t);\n\t\t\t}\n\t\t},\n\t\t[ foundTemplate, postType, slug ]\n\t);\n\n\tconst [ showModal, setShowModal ] = useState( false );\n\n\tconst onChangeControl = useCallback(\n\t\t( newValue: string ) =>\n\t\t\tonChange( {\n\t\t\t\t[ id ]: newValue,\n\t\t\t} ),\n\t\t[ id, onChange ]\n\t);\n\n\treturn (\n\t\t<fieldset className=\"fields-controls__template\">\n\t\t\t<Dropdown\n\t\t\t\tpopoverProps={ { placement: 'bottom-start' } }\n\t\t\t\trenderToggle={ ( { onToggle } ) => (\n\t\t\t\t\t<Button\n\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\tvariant=\"tertiary\"\n\t\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\t\tonClick={ onToggle }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ currentTemplate\n\t\t\t\t\t\t\t? getItemTitle( currentTemplate )\n\t\t\t\t\t\t\t: '' }\n\t\t\t\t\t</Button>\n\t\t\t\t) }\n\t\t\t\trenderContent={ ( { onToggle } ) => (\n\t\t\t\t\t<MenuGroup>\n\t\t\t\t\t\t<MenuItem\n\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\tsetShowModal( true );\n\t\t\t\t\t\t\t\tonToggle();\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{ __( 'Change template' ) }\n\t\t\t\t\t\t</MenuItem>\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t// The default template in a post is indicated by an empty string\n\t\t\t\t\t\t\tvalue !== '' && (\n\t\t\t\t\t\t\t\t<MenuItem\n\t\t\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\t\t\tonChangeControl( '' );\n\t\t\t\t\t\t\t\t\t\tonToggle();\n\t\t\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t{ __( 'Use default template' ) }\n\t\t\t\t\t\t\t\t</MenuItem>\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t</MenuGroup>\n\t\t\t\t) }\n\t\t\t/>\n\t\t\t{ showModal && (\n\t\t\t\t<Modal\n\t\t\t\t\ttitle={ __( 'Choose a template' ) }\n\t\t\t\t\tonRequestClose={ () => setShowModal( false ) }\n\t\t\t\t\toverlayClassName=\"fields-controls__template-modal\"\n\t\t\t\t\tisFullScreen\n\t\t\t\t>\n\t\t\t\t\t<div className=\"fields-controls__template-content\">\n\t\t\t\t\t\t<BlockPatternsList\n\t\t\t\t\t\t\tlabel={ __( 'Templates' ) }\n\t\t\t\t\t\t\tblockPatterns={ templatesAsPatterns }\n\t\t\t\t\t\t\tshownPatterns={ shownTemplates }\n\t\t\t\t\t\t\tonClickPattern={ (\n\t\t\t\t\t\t\t\ttemplate: ( typeof templatesAsPatterns )[ 0 ]\n\t\t\t\t\t\t\t) => {\n\t\t\t\t\t\t\t\tonChangeControl( template.name );\n\t\t\t\t\t\t\t\tsetShowModal( false );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t/>\n\t\t\t\t\t</div>\n\t\t\t\t</Modal>\n\t\t\t) }\n\t\t</fieldset>\n\t);\n};\n"],"mappings":";;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAEA,IAAAC,OAAA,GAAAD,OAAA;AAEA,IAAAE,SAAA,GAAAF,OAAA;AAOA,IAAAG,YAAA,GAAAH,OAAA;AACA,IAAAI,WAAA,GAAAJ,OAAA;AAOA,IAAAK,QAAA,GAAAL,OAAA;AACA,IAAAM,KAAA,GAAAN,OAAA;AACA,IAAAO,aAAA,GAAAP,OAAA;AACA,IAAAQ,KAAA,GAAAR,OAAA;AACA,IAAAS,MAAA,GAAAT,OAAA;AAEA,IAAAU,WAAA,GAAAV,OAAA;AAA2C,IAAAW,WAAA,GAAAX,OAAA;AA5B3C;AACA;AACA;;AAEA;;AAMA;AACA;AACA;AACA;;AAiBA,MAAMY,WAAe,GAAG,EAAE;AAEnB,MAAMC,YAAY,GAAGA,CAAE;EAC7BC,IAAI;EACJC,KAAK;EACLC;AACiC,CAAC,KAAM;EACxC,MAAM;IAAEC;EAAG,CAAC,GAAGF,KAAK;EACpB,MAAMG,QAAQ,GAAGJ,IAAI,CA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QAAE,EAAE,iBAAkB;QAAC,CAChB,CAAC;QAEV;QACAlC,KAAK,KAAK,EAAE,iBACX,IAAA5C,WAAA,CAAA+D,GAAA,EAACtE,WAAA,CAAAoF,QAAQ;UACRJ,OAAO,EAAGA,CAAA,KAAM;YACfhB,eAAe,CAAE,EAAG,CAAC;YACrBW,QAAQ,CAAC,CAAC;UACX,CAAG;UAAAN,QAAA,EAED,IAAAgB,QAAE,EAAE,sBAAuB;QAAC,CACrB,CACV;MAAA,CAEQ;IACT,CACH,CAAC,EACAxB,SAAS,iBACV,IAAAtD,WAAA,CAAA+D,GAAA,EAACtE,WAAA,CAAAsF,KAAK;MACLxC,KAAK,EAAG,IAAAuC,QAAE,EAAE,mBAAoB,CAAG;MACnCE,cAAc,EAAGA,CAAA,KAAMzB,YAAY,CAAE,KAAM,CAAG;MAC9C0B,gBAAgB,EAAC,iCAAiC;MAClDC,YAAY;MAAApB,QAAA,eAEZ,IAAA9D,WAAA,CAAA+D,GAAA;QAAKF,SAAS,EAAC,mCAAmC;QAAAC,QAAA,eACjD,IAAA9D,WAAA,CAAA+D,GAAA,EAACvE,YAAA,CAAA2F,+BAAiB;UACjBC,KAAK,EAAG,IAAAN,QAAE,EAAE,WAAY,CAAG;UAC3BO,aAAa,EAAGzD,mBAAqB;UACrC0D,aAAa,EAAG5C,cAAgB;UAChC6C,cAAc,EACbxD,QAA6C,IACzC;YACJ0B,eAAe,CAAE1B,QAAQ,CAACK,IAAK,CAAC;YAChCmB,YAAY,CAAE,KAAM,CAAC;UACtB;QAAG,CACH;MAAC,CACE;IAAC,CACA,CACP;EAAA,CACQ,CAAC;AAEb,CAAC;AAACiC,OAAA,CAAAtF,YAAA,GAAAA,YAAA","ignoreList":[]}
@@ -18,6 +18,7 @@ import { __ } from '@wordpress/i18n';
18
18
  import { getItemTitle } from '../../actions/utils';
19
19
  import { unlock } from '../../lock-unlock';
20
20
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
21
+ const EMPTY_ARRAY = [];
21
22
  export const TemplateEdit = ({
22
23
  data,
23
24
  field,
@@ -30,14 +31,14 @@ export const TemplateEdit = ({
30
31
  const postId = typeof data.id === 'number' ? data.id : parseInt(data.id, 10);
31
32
  const slug = data.slug;
32
33
  const {
33
- availableTemplates,
34
+ canSwitchTemplate,
34
35
  templates
35
36
  } = useSelect(select => {
36
37
  var _select$getEntityReco;
37
38
  const allTemplates = (_select$getEntityReco = select(coreStore).getEntityRecords('postType', 'wp_template', {
38
39
  per_page: -1,
39
40
  post_type: postType
40
- })) !== null && _select$getEntityReco !== void 0 ? _select$getEntityReco : [];
41
+ })) !== null && _select$getEntityReco !== void 0 ? _select$getEntityReco : EMPTY_ARRAY;
41
42
  const {
42
43
  getHomePage,
43
44
  getPostsPageId
@@ -47,22 +48,28 @@ export const TemplateEdit = ({
47
48
  const allowSwitchingTemplate = !isPostsPage && !isFrontPage;
48
49
  return {
49
50
  templates: allTemplates,
50
- availableTemplates: allowSwitchingTemplate ? allTemplates.filter(template => template.is_custom && template.slug !== data.template && !!template.content.raw // Skip empty templates.
51
- ) : []
51
+ canSwitchTemplate: allowSwitchingTemplate
52
52
  };
53
- }, [data.template, postId, postType]);
54
- const templatesAsPatterns = useMemo(() => availableTemplates.map(template => ({
55
- name: template.slug,
56
- blocks: parse(template.content.raw),
57
- title: decodeEntities(template.title.rendered),
58
- id: template.id
59
- })), [availableTemplates]);
53
+ }, [postId, postType]);
54
+ const templatesAsPatterns = useMemo(() => {
55
+ if (!canSwitchTemplate) {
56
+ return [];
57
+ }
58
+ return templates.filter(template => template.is_custom && template.slug !== data.template &&
59
+ // Skip empty templates.
60
+ !!template.content.raw).map(template => ({
61
+ name: template.slug,
62
+ blocks: parse(template.content.raw),
63
+ title: decodeEntities(template.title.rendered),
64
+ id: template.id
65
+ }));
66
+ }, [canSwitchTemplate, data.template, templates]);
60
67
  const shownTemplates = useAsyncList(templatesAsPatterns);
61
68
  const value = field.getValue({
62
69
  item: data
63
70
  });
71
+ const foundTemplate = templates.find(template => template.slug === value);
64
72
  const currentTemplate = useSelect(select => {
65
- const foundTemplate = templates?.find(template => template.slug === value);
66
73
  if (foundTemplate) {
67
74
  return foundTemplate;
68
75
  }
@@ -82,7 +89,7 @@ export const TemplateEdit = ({
82
89
  });
83
90
  return select(coreStore).getEntityRecord('postType', 'wp_template', templateId);
84
91
  }
85
- }, [postType, slug, templates, value]);
92
+ }, [foundTemplate, postType, slug]);
86
93
  const [showModal, setShowModal] = useState(false);
87
94
  const onChangeControl = useCallback(newValue => onChange({
88
95
  [id]: newValue
@@ -1 +1 @@
1
- {"version":3,"names":["useCallback","useMemo","useState","parse","store","coreStore","__experimentalBlockPatternsList","BlockPatternsList","Button","Dropdown","MenuGroup","MenuItem","Modal","useAsyncList","useSelect","decodeEntities","__","getItemTitle","unlock","jsx","_jsx","jsxs","_jsxs","TemplateEdit","data","field","onChange","id","postType","type","postId","parseInt","slug","availableTemplates","templates","select","_select$getEntityReco","allTemplates","getEntityRecords","per_page","post_type","getHomePage","getPostsPageId","isPostsPage","isFrontPage","allowSwitchingTemplate","filter","template","is_custom","content","raw","templatesAsPatterns","map","name","blocks","title","rendered","shownTemplates","value","getValue","item","currentTemplate","foundTemplate","find","slugToCheck","templateId","getDefaultTemplateId","getEntityRecord","showModal","setShowModal","onChangeControl","newValue","className","children","popoverProps","placement","renderToggle","onToggle","__next40pxDefaultSize","variant","size","onClick","renderContent","onRequestClose","overlayClassName","isFullScreen","label","blockPatterns","shownPatterns","onClickPattern"],"sources":["@wordpress/fields/src/fields/template/template-edit.tsx"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCallback, useMemo, useState } from '@wordpress/element';\n// @ts-ignore\nimport { parse } from '@wordpress/blocks';\nimport type { WpTemplate } from '@wordpress/core-data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { DataFormControlProps } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\n// @ts-expect-error block-editor is not typed correctly.\nimport { __experimentalBlockPatternsList as BlockPatternsList } from '@wordpress/block-editor';\nimport {\n\tButton,\n\tDropdown,\n\tMenuGroup,\n\tMenuItem,\n\tModal,\n} from '@wordpress/components';\nimport { useAsyncList } from '@wordpress/compose';\nimport { useSelect } from '@wordpress/data';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { __ } from '@wordpress/i18n';\nimport { getItemTitle } from '../../actions/utils';\nimport type { BasePost } from '../../types';\nimport { unlock } from '../../lock-unlock';\n\nexport const TemplateEdit = ( {\n\tdata,\n\tfield,\n\tonChange,\n}: DataFormControlProps< BasePost > ) => {\n\tconst { id } = field;\n\tconst postType = data.type;\n\tconst postId =\n\t\ttypeof data.id === 'number' ? data.id : parseInt( data.id, 10 );\n\tconst slug = data.slug;\n\n\tconst { availableTemplates, templates } = useSelect(\n\t\t( select ) => {\n\t\t\tconst allTemplates =\n\t\t\t\tselect( coreStore ).getEntityRecords< WpTemplate >(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template',\n\t\t\t\t\t{\n\t\t\t\t\t\tper_page: -1,\n\t\t\t\t\t\tpost_type: postType,\n\t\t\t\t\t}\n\t\t\t\t) ?? [];\n\n\t\t\tconst { getHomePage, getPostsPageId } = unlock(\n\t\t\t\tselect( coreStore )\n\t\t\t);\n\n\t\t\tconst isPostsPage = getPostsPageId() === +postId;\n\t\t\tconst isFrontPage =\n\t\t\t\tpostType === 'page' && getHomePage()?.postId === +postId;\n\n\t\t\tconst allowSwitchingTemplate = ! isPostsPage && ! isFrontPage;\n\n\t\t\treturn {\n\t\t\t\ttemplates: allTemplates,\n\t\t\t\tavailableTemplates: allowSwitchingTemplate\n\t\t\t\t\t? allTemplates.filter(\n\t\t\t\t\t\t\t( template ) =>\n\t\t\t\t\t\t\t\ttemplate.is_custom &&\n\t\t\t\t\t\t\t\ttemplate.slug !== data.template &&\n\t\t\t\t\t\t\t\t!! template.content.raw // Skip empty templates.\n\t\t\t\t\t )\n\t\t\t\t\t: [],\n\t\t\t};\n\t\t},\n\t\t[ data.template, postId, postType ]\n\t);\n\n\tconst templatesAsPatterns = useMemo(\n\t\t() =>\n\t\t\tavailableTemplates.map( ( template ) => ( {\n\t\t\t\tname: template.slug,\n\t\t\t\tblocks: parse( template.content.raw ),\n\t\t\t\ttitle: decodeEntities( template.title.rendered ),\n\t\t\t\tid: template.id,\n\t\t\t} ) ),\n\t\t[ availableTemplates ]\n\t);\n\n\tconst shownTemplates = useAsyncList( templatesAsPatterns );\n\n\tconst value = field.getValue( { item: data } );\n\n\tconst currentTemplate = useSelect(\n\t\t( select ) => {\n\t\t\tconst foundTemplate = templates?.find(\n\t\t\t\t( template ) => template.slug === value\n\t\t\t);\n\n\t\t\tif ( foundTemplate ) {\n\t\t\t\treturn foundTemplate;\n\t\t\t}\n\n\t\t\tlet slugToCheck;\n\t\t\t// In `draft` status we might not have a slug available, so we use the `single`\n\t\t\t// post type templates slug(ex page, single-post, single-product etc..).\n\t\t\t// Pages do not need the `single` prefix in the slug to be prioritized\n\t\t\t// through template hierarchy.\n\t\t\tif ( slug ) {\n\t\t\t\tslugToCheck =\n\t\t\t\t\tpostType === 'page'\n\t\t\t\t\t\t? `${ postType }-${ slug }`\n\t\t\t\t\t\t: `single-${ postType }-${ slug }`;\n\t\t\t} else {\n\t\t\t\tslugToCheck =\n\t\t\t\t\tpostType === 'page' ? 'page' : `single-${ postType }`;\n\t\t\t}\n\n\t\t\tif ( postType ) {\n\t\t\t\tconst templateId = select( coreStore ).getDefaultTemplateId( {\n\t\t\t\t\tslug: slugToCheck,\n\t\t\t\t} );\n\n\t\t\t\treturn select( coreStore ).getEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template',\n\t\t\t\t\ttemplateId\n\t\t\t\t);\n\t\t\t}\n\t\t},\n\t\t[ postType, slug, templates, value ]\n\t);\n\n\tconst [ showModal, setShowModal ] = useState( false );\n\n\tconst onChangeControl = useCallback(\n\t\t( newValue: string ) =>\n\t\t\tonChange( {\n\t\t\t\t[ id ]: newValue,\n\t\t\t} ),\n\t\t[ id, onChange ]\n\t);\n\n\treturn (\n\t\t<fieldset className=\"fields-controls__template\">\n\t\t\t<Dropdown\n\t\t\t\tpopoverProps={ { placement: 'bottom-start' } }\n\t\t\t\trenderToggle={ ( { onToggle } ) => (\n\t\t\t\t\t<Button\n\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\tvariant=\"tertiary\"\n\t\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\t\tonClick={ onToggle }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ currentTemplate\n\t\t\t\t\t\t\t? getItemTitle( currentTemplate )\n\t\t\t\t\t\t\t: '' }\n\t\t\t\t\t</Button>\n\t\t\t\t) }\n\t\t\t\trenderContent={ ( { onToggle } ) => (\n\t\t\t\t\t<MenuGroup>\n\t\t\t\t\t\t<MenuItem\n\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\tsetShowModal( true );\n\t\t\t\t\t\t\t\tonToggle();\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{ __( 'Change template' ) }\n\t\t\t\t\t\t</MenuItem>\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t// The default template in a post is indicated by an empty string\n\t\t\t\t\t\t\tvalue !== '' && (\n\t\t\t\t\t\t\t\t<MenuItem\n\t\t\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\t\t\tonChangeControl( '' );\n\t\t\t\t\t\t\t\t\t\tonToggle();\n\t\t\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t{ __( 'Use default template' ) }\n\t\t\t\t\t\t\t\t</MenuItem>\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t</MenuGroup>\n\t\t\t\t) }\n\t\t\t/>\n\t\t\t{ showModal && (\n\t\t\t\t<Modal\n\t\t\t\t\ttitle={ __( 'Choose a template' ) }\n\t\t\t\t\tonRequestClose={ () => setShowModal( false ) }\n\t\t\t\t\toverlayClassName=\"fields-controls__template-modal\"\n\t\t\t\t\tisFullScreen\n\t\t\t\t>\n\t\t\t\t\t<div className=\"fields-controls__template-content\">\n\t\t\t\t\t\t<BlockPatternsList\n\t\t\t\t\t\t\tlabel={ __( 'Templates' ) }\n\t\t\t\t\t\t\tblockPatterns={ templatesAsPatterns }\n\t\t\t\t\t\t\tshownPatterns={ shownTemplates }\n\t\t\t\t\t\t\tonClickPattern={ (\n\t\t\t\t\t\t\t\ttemplate: ( typeof templatesAsPatterns )[ 0 ]\n\t\t\t\t\t\t\t) => {\n\t\t\t\t\t\t\t\tonChangeControl( template.name );\n\t\t\t\t\t\t\t\tsetShowModal( false );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t/>\n\t\t\t\t\t</div>\n\t\t\t\t</Modal>\n\t\t\t) }\n\t\t</fieldset>\n\t);\n};\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,WAAW,EAAEC,OAAO,EAAEC,QAAQ,QAAQ,oBAAoB;AACnE;AACA,SAASC,KAAK,QAAQ,mBAAmB;AAEzC,SAASC,KAAK,IAAIC,SAAS,QAAQ,sBAAsB;AAGzD;AACA;AACA;AACA;AACA,SAASC,+BAA+B,IAAIC,iBAAiB,QAAQ,yBAAyB;AAC9F,SACCC,MAAM,EACNC,QAAQ,EACRC,SAAS,EACTC,QAAQ,EACRC,KAAK,QACC,uBAAuB;AAC9B,SAASC,YAAY,QAAQ,oBAAoB;AACjD,SAASC,SAAS,QAAQ,iBAAiB;AAC3C,SAASC,cAAc,QAAQ,0BAA0B;AACzD,SAASC,EAAE,QAAQ,iBAAiB;AACpC,SAASC,YAAY,QAAQ,qBAAqB;AAElD,SAASC,MAAM,QAAQ,mBAAmB;AAAC,SAAAC,GAAA,IAAAC,IAAA,EAAAC,IAAA,IAAAC,KAAA;AAE3C,OAAO,MAAMC,YAAY,GAAGA,CAAE;EAC7BC,IAAI;EACJC,KAAK;EACLC;AACiC,CAAC,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YACrBO,QAAQ,CAAC,CAAC;UACX,CAAG;UAAAJ,QAAA,EAEDzD,EAAE,CAAE,sBAAuB;QAAC,CACrB,CACV;MAAA,CAEQ;IACT,CACH,CAAC,EACAoD,SAAS,iBACVhD,IAAA,CAACR,KAAK;MACL2C,KAAK,EAAGvC,EAAE,CAAE,mBAAoB,CAAG;MACnCmE,cAAc,EAAGA,CAAA,KAAMd,YAAY,CAAE,KAAM,CAAG;MAC9Ce,gBAAgB,EAAC,iCAAiC;MAClDC,YAAY;MAAAZ,QAAA,eAEZrD,IAAA;QAAKoD,SAAS,EAAC,mCAAmC;QAAAC,QAAA,eACjDrD,IAAA,CAACb,iBAAiB;UACjB+E,KAAK,EAAGtE,EAAE,CAAE,WAAY,CAAG;UAC3BuE,aAAa,EAAGpC,mBAAqB;UACrCqC,aAAa,EAAG/B,cAAgB;UAChCgC,cAAc,EACb1C,QAA6C,IACzC;YACJuB,eAAe,CAAEvB,QAAQ,CAACM,IAAK,CAAC;YAChCgB,YAAY,CAAE,KAAM,CAAC;UACtB;QAAG,CACH;MAAC,CACE;IAAC,CACA,CACP;EAAA,CACQ,CAAC;AAEb,CAAC","ignoreList":[]}
1
+ {"version":3,"names":["useCallback","useMemo","useState","parse","store","coreStore","__experimentalBlockPatternsList","BlockPatternsList","Button","Dropdown","MenuGroup","MenuItem","Modal","useAsyncList","useSelect","decodeEntities","__","getItemTitle","unlock","jsx","_jsx","jsxs","_jsxs","EMPTY_ARRAY","TemplateEdit","data","field","onChange","id","postType","type","postId","parseInt","slug","canSwitchTemplate","templates","select","_select$getEntityReco","allTemplates","getEntityRecords","per_page","post_type","getHomePage","getPostsPageId","isPostsPage","isFrontPage","allowSwitchingTemplate","templatesAsPatterns","filter","template","is_custom","content","raw","map","name","blocks","title","rendered","shownTemplates","value","getValue","item","foundTemplate","find","currentTemplate","slugToCheck","templateId","getDefaultTemplateId","getEntityRecord","showModal","setShowModal","onChangeControl","newValue","className","children","popoverProps","placement","renderToggle","onToggle","__next40pxDefaultSize","variant","size","onClick","renderContent","onRequestClose","overlayClassName","isFullScreen","label","blockPatterns","shownPatterns","onClickPattern"],"sources":["@wordpress/fields/src/fields/template/template-edit.tsx"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCallback, useMemo, useState } from '@wordpress/element';\n// @ts-ignore\nimport { parse } from '@wordpress/blocks';\nimport type { WpTemplate } from '@wordpress/core-data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { DataFormControlProps } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\n// @ts-expect-error block-editor is not typed correctly.\nimport { __experimentalBlockPatternsList as BlockPatternsList } from '@wordpress/block-editor';\nimport {\n\tButton,\n\tDropdown,\n\tMenuGroup,\n\tMenuItem,\n\tModal,\n} from '@wordpress/components';\nimport { useAsyncList } from '@wordpress/compose';\nimport { useSelect } from '@wordpress/data';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { __ } from '@wordpress/i18n';\nimport { getItemTitle } from '../../actions/utils';\nimport type { BasePost } from '../../types';\nimport { unlock } from '../../lock-unlock';\n\nconst EMPTY_ARRAY: [] = [];\n\nexport const TemplateEdit = ( {\n\tdata,\n\tfield,\n\tonChange,\n}: DataFormControlProps< BasePost > ) => {\n\tconst { id } = field;\n\tconst postType = data.type;\n\tconst postId =\n\t\ttypeof data.id === 'number' ? data.id : parseInt( data.id, 10 );\n\tconst slug = data.slug;\n\n\tconst { canSwitchTemplate, templates } = useSelect(\n\t\t( select ) => {\n\t\t\tconst allTemplates =\n\t\t\t\tselect( coreStore ).getEntityRecords< WpTemplate >(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template',\n\t\t\t\t\t{\n\t\t\t\t\t\tper_page: -1,\n\t\t\t\t\t\tpost_type: postType,\n\t\t\t\t\t}\n\t\t\t\t) ?? EMPTY_ARRAY;\n\n\t\t\tconst { getHomePage, getPostsPageId } = unlock(\n\t\t\t\tselect( coreStore )\n\t\t\t);\n\n\t\t\tconst isPostsPage = getPostsPageId() === +postId;\n\t\t\tconst isFrontPage =\n\t\t\t\tpostType === 'page' && getHomePage()?.postId === +postId;\n\n\t\t\tconst allowSwitchingTemplate = ! isPostsPage && ! isFrontPage;\n\n\t\t\treturn {\n\t\t\t\ttemplates: allTemplates,\n\t\t\t\tcanSwitchTemplate: allowSwitchingTemplate,\n\t\t\t};\n\t\t},\n\t\t[ postId, postType ]\n\t);\n\n\tconst templatesAsPatterns = useMemo( () => {\n\t\tif ( ! canSwitchTemplate ) {\n\t\t\treturn [];\n\t\t}\n\t\treturn templates\n\t\t\t.filter(\n\t\t\t\t( template ) =>\n\t\t\t\t\ttemplate.is_custom &&\n\t\t\t\t\ttemplate.slug !== data.template &&\n\t\t\t\t\t// Skip empty templates.\n\t\t\t\t\t!! template.content.raw\n\t\t\t)\n\t\t\t.map( ( template ) => ( {\n\t\t\t\tname: template.slug,\n\t\t\t\tblocks: parse( template.content.raw ),\n\t\t\t\ttitle: decodeEntities( template.title.rendered ),\n\t\t\t\tid: template.id,\n\t\t\t} ) );\n\t}, [ canSwitchTemplate, data.template, templates ] );\n\n\tconst shownTemplates = useAsyncList( templatesAsPatterns );\n\n\tconst value = field.getValue( { item: data } );\n\tconst foundTemplate = templates.find(\n\t\t( template ) => template.slug === value\n\t);\n\n\tconst currentTemplate = useSelect(\n\t\t( select ) => {\n\t\t\tif ( foundTemplate ) {\n\t\t\t\treturn foundTemplate;\n\t\t\t}\n\n\t\t\tlet slugToCheck;\n\t\t\t// In `draft` status we might not have a slug available, so we use the `single`\n\t\t\t// post type templates slug(ex page, single-post, single-product etc..).\n\t\t\t// Pages do not need the `single` prefix in the slug to be prioritized\n\t\t\t// through template hierarchy.\n\t\t\tif ( slug ) {\n\t\t\t\tslugToCheck =\n\t\t\t\t\tpostType === 'page'\n\t\t\t\t\t\t? `${ postType }-${ slug }`\n\t\t\t\t\t\t: `single-${ postType }-${ slug }`;\n\t\t\t} else {\n\t\t\t\tslugToCheck =\n\t\t\t\t\tpostType === 'page' ? 'page' : `single-${ postType }`;\n\t\t\t}\n\n\t\t\tif ( postType ) {\n\t\t\t\tconst templateId = select( coreStore ).getDefaultTemplateId( {\n\t\t\t\t\tslug: slugToCheck,\n\t\t\t\t} );\n\n\t\t\t\treturn select( coreStore ).getEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template',\n\t\t\t\t\ttemplateId\n\t\t\t\t);\n\t\t\t}\n\t\t},\n\t\t[ foundTemplate, postType, slug ]\n\t);\n\n\tconst [ showModal, setShowModal ] = useState( false );\n\n\tconst onChangeControl = useCallback(\n\t\t( newValue: string ) =>\n\t\t\tonChange( {\n\t\t\t\t[ id ]: newValue,\n\t\t\t} ),\n\t\t[ id, onChange ]\n\t);\n\n\treturn (\n\t\t<fieldset className=\"fields-controls__template\">\n\t\t\t<Dropdown\n\t\t\t\tpopoverProps={ { placement: 'bottom-start' } }\n\t\t\t\trenderToggle={ ( { onToggle } ) => (\n\t\t\t\t\t<Button\n\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\tvariant=\"tertiary\"\n\t\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\t\tonClick={ onToggle }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ currentTemplate\n\t\t\t\t\t\t\t? getItemTitle( currentTemplate )\n\t\t\t\t\t\t\t: '' }\n\t\t\t\t\t</Button>\n\t\t\t\t) }\n\t\t\t\trenderContent={ ( { onToggle } ) => (\n\t\t\t\t\t<MenuGroup>\n\t\t\t\t\t\t<MenuItem\n\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\tsetShowModal( true );\n\t\t\t\t\t\t\t\tonToggle();\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{ __( 'Change template' ) }\n\t\t\t\t\t\t</MenuItem>\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t// The default template in a post is indicated by an empty string\n\t\t\t\t\t\t\tvalue !== '' && (\n\t\t\t\t\t\t\t\t<MenuItem\n\t\t\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\t\t\tonChangeControl( '' );\n\t\t\t\t\t\t\t\t\t\tonToggle();\n\t\t\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t{ __( 'Use default template' ) }\n\t\t\t\t\t\t\t\t</MenuItem>\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t</MenuGroup>\n\t\t\t\t) }\n\t\t\t/>\n\t\t\t{ showModal && (\n\t\t\t\t<Modal\n\t\t\t\t\ttitle={ __( 'Choose a template' ) }\n\t\t\t\t\tonRequestClose={ () => setShowModal( false ) }\n\t\t\t\t\toverlayClassName=\"fields-controls__template-modal\"\n\t\t\t\t\tisFullScreen\n\t\t\t\t>\n\t\t\t\t\t<div className=\"fields-controls__template-content\">\n\t\t\t\t\t\t<BlockPatternsList\n\t\t\t\t\t\t\tlabel={ __( 'Templates' ) }\n\t\t\t\t\t\t\tblockPatterns={ templatesAsPatterns }\n\t\t\t\t\t\t\tshownPatterns={ shownTemplates }\n\t\t\t\t\t\t\tonClickPattern={ (\n\t\t\t\t\t\t\t\ttemplate: ( typeof templatesAsPatterns )[ 0 ]\n\t\t\t\t\t\t\t) => {\n\t\t\t\t\t\t\t\tonChangeControl( template.name );\n\t\t\t\t\t\t\t\tsetShowModal( false );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t/>\n\t\t\t\t\t</div>\n\t\t\t\t</Modal>\n\t\t\t) }\n\t\t</fieldset>\n\t);\n};\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,WAAW,EAAEC,OAAO,EAAEC,QAAQ,QAAQ,oBAAoB;AACnE;AACA,SAASC,KAAK,QAAQ,mBAAmB;AAEzC,SAASC,KAAK,IAAIC,SAAS,QAAQ,sBAAsB;AAGzD;AACA;AACA;AACA;AACA,SAASC,+BAA+B,IAAIC,iBAAiB,QAAQ,yBAAyB;AAC9F,SACCC,MA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+1 @@
1
- {"version":3,"file":"template-edit.d.ts","sourceRoot":"","sources":["../../../src/fields/template/template-edit.tsx"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAmBjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAG5C,eAAO,MAAM,YAAY,+BAItB,oBAAoB,CAAE,QAAQ,CAAE,gCA+KlC,CAAC"}
1
+ {"version":3,"file":"template-edit.d.ts","sourceRoot":"","sources":["../../../src/fields/template/template-edit.tsx"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAmBjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAK5C,eAAO,MAAM,YAAY,+BAItB,oBAAoB,CAAE,QAAQ,CAAE,gCAgLlC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/fields",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "DataViews is a component that provides an API to render datasets using different types of layouts (table, grid, list, etc.).",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -33,29 +33,29 @@
33
33
  ],
34
34
  "dependencies": {
35
35
  "@babel/runtime": "7.25.7",
36
- "@wordpress/api-fetch": "^7.18.0",
37
- "@wordpress/blob": "^4.18.0",
38
- "@wordpress/block-editor": "^14.13.0",
39
- "@wordpress/blocks": "^14.7.0",
40
- "@wordpress/components": "^29.4.0",
41
- "@wordpress/compose": "^7.18.0",
42
- "@wordpress/core-data": "^7.18.0",
43
- "@wordpress/data": "^10.18.0",
44
- "@wordpress/dataviews": "^4.14.0",
45
- "@wordpress/date": "^5.18.0",
46
- "@wordpress/element": "^6.18.0",
47
- "@wordpress/hooks": "^4.18.0",
48
- "@wordpress/html-entities": "^4.18.0",
49
- "@wordpress/i18n": "^5.18.0",
50
- "@wordpress/icons": "^10.18.0",
51
- "@wordpress/media-utils": "^5.18.0",
52
- "@wordpress/notices": "^5.18.0",
53
- "@wordpress/patterns": "^2.18.0",
54
- "@wordpress/primitives": "^4.18.0",
55
- "@wordpress/private-apis": "^1.18.0",
56
- "@wordpress/router": "^1.18.0",
57
- "@wordpress/url": "^4.18.0",
58
- "@wordpress/warning": "^3.18.0",
36
+ "@wordpress/api-fetch": "^7.19.0",
37
+ "@wordpress/blob": "^4.19.0",
38
+ "@wordpress/block-editor": "^14.14.0",
39
+ "@wordpress/blocks": "^14.8.0",
40
+ "@wordpress/components": "^29.5.0",
41
+ "@wordpress/compose": "^7.19.0",
42
+ "@wordpress/core-data": "^7.19.0",
43
+ "@wordpress/data": "^10.19.0",
44
+ "@wordpress/dataviews": "^4.15.0",
45
+ "@wordpress/date": "^5.19.0",
46
+ "@wordpress/element": "^6.19.0",
47
+ "@wordpress/hooks": "^4.19.0",
48
+ "@wordpress/html-entities": "^4.19.0",
49
+ "@wordpress/i18n": "^5.19.0",
50
+ "@wordpress/icons": "^10.19.0",
51
+ "@wordpress/media-utils": "^5.19.0",
52
+ "@wordpress/notices": "^5.19.0",
53
+ "@wordpress/patterns": "^2.19.0",
54
+ "@wordpress/primitives": "^4.19.0",
55
+ "@wordpress/private-apis": "^1.19.0",
56
+ "@wordpress/router": "^1.19.0",
57
+ "@wordpress/url": "^4.19.0",
58
+ "@wordpress/warning": "^3.19.0",
59
59
  "change-case": "4.1.2",
60
60
  "client-zip": "^2.4.5",
61
61
  "clsx": "2.1.1",
@@ -67,5 +67,5 @@
67
67
  "publishConfig": {
68
68
  "access": "public"
69
69
  },
70
- "gitHead": "afe4fb333177642180ac020f1030c5685eab7183"
70
+ "gitHead": "d6b0b20fa927b110140dc7fdd906a7e0bf662004"
71
71
  }
@@ -28,6 +28,8 @@ import { getItemTitle } from '../../actions/utils';
28
28
  import type { BasePost } from '../../types';
29
29
  import { unlock } from '../../lock-unlock';
30
30
 
31
+ const EMPTY_ARRAY: [] = [];
32
+
31
33
  export const TemplateEdit = ( {
32
34
  data,
33
35
  field,
@@ -39,7 +41,7 @@ export const TemplateEdit = ( {
39
41
  typeof data.id === 'number' ? data.id : parseInt( data.id, 10 );
40
42
  const slug = data.slug;
41
43
 
42
- const { availableTemplates, templates } = useSelect(
44
+ const { canSwitchTemplate, templates } = useSelect(
43
45
  ( select ) => {
44
46
  const allTemplates =
45
47
  select( coreStore ).getEntityRecords< WpTemplate >(
@@ -49,7 +51,7 @@ export const TemplateEdit = ( {
49
51
  per_page: -1,
50
52
  post_type: postType,
51
53
  }
52
- ) ?? [];
54
+ ) ?? EMPTY_ARRAY;
53
55
 
54
56
  const { getHomePage, getPostsPageId } = unlock(
55
57
  select( coreStore )
@@ -63,40 +65,41 @@ export const TemplateEdit = ( {
63
65
 
64
66
  return {
65
67
  templates: allTemplates,
66
- availableTemplates: allowSwitchingTemplate
67
- ? allTemplates.filter(
68
- ( template ) =>
69
- template.is_custom &&
70
- template.slug !== data.template &&
71
- !! template.content.raw // Skip empty templates.
72
- )
73
- : [],
68
+ canSwitchTemplate: allowSwitchingTemplate,
74
69
  };
75
70
  },
76
- [ data.template, postId, postType ]
71
+ [ postId, postType ]
77
72
  );
78
73
 
79
- const templatesAsPatterns = useMemo(
80
- () =>
81
- availableTemplates.map( ( template ) => ( {
74
+ const templatesAsPatterns = useMemo( () => {
75
+ if ( ! canSwitchTemplate ) {
76
+ return [];
77
+ }
78
+ return templates
79
+ .filter(
80
+ ( template ) =>
81
+ template.is_custom &&
82
+ template.slug !== data.template &&
83
+ // Skip empty templates.
84
+ !! template.content.raw
85
+ )
86
+ .map( ( template ) => ( {
82
87
  name: template.slug,
83
88
  blocks: parse( template.content.raw ),
84
89
  title: decodeEntities( template.title.rendered ),
85
90
  id: template.id,
86
- } ) ),
87
- [ availableTemplates ]
88
- );
91
+ } ) );
92
+ }, [ canSwitchTemplate, data.template, templates ] );
89
93
 
90
94
  const shownTemplates = useAsyncList( templatesAsPatterns );
91
95
 
92
96
  const value = field.getValue( { item: data } );
97
+ const foundTemplate = templates.find(
98
+ ( template ) => template.slug === value
99
+ );
93
100
 
94
101
  const currentTemplate = useSelect(
95
102
  ( select ) => {
96
- const foundTemplate = templates?.find(
97
- ( template ) => template.slug === value
98
- );
99
-
100
103
  if ( foundTemplate ) {
101
104
  return foundTemplate;
102
105
  }
@@ -128,7 +131,7 @@ export const TemplateEdit = ( {
128
131
  );
129
132
  }
130
133
  },
131
- [ postType, slug, templates, value ]
134
+ [ foundTemplate, postType, slug ]
132
135
  );
133
136
 
134
137
  const [ showModal, setShowModal ] = useState( false );