@wordpress/edit-site 4.9.0 → 4.10.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.
Files changed (105) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/build/components/add-new-template/add-custom-generic-template-modal.js +84 -0
  3. package/build/components/add-new-template/add-custom-generic-template-modal.js.map +1 -0
  4. package/build/components/add-new-template/add-custom-template-modal.js +92 -53
  5. package/build/components/add-new-template/add-custom-template-modal.js.map +1 -1
  6. package/build/components/add-new-template/new-template.js +77 -81
  7. package/build/components/add-new-template/new-template.js.map +1 -1
  8. package/build/components/add-new-template/utils.js +310 -44
  9. package/build/components/add-new-template/utils.js.map +1 -1
  10. package/build/components/code-editor/index.js +17 -4
  11. package/build/components/code-editor/index.js.map +1 -1
  12. package/build/components/editor/index.js +16 -0
  13. package/build/components/editor/index.js.map +1 -1
  14. package/build/components/error-boundary/index.js +6 -0
  15. package/build/components/error-boundary/index.js.map +1 -1
  16. package/build/components/global-styles/dimensions-panel.js +2 -6
  17. package/build/components/global-styles/dimensions-panel.js.map +1 -1
  18. package/build/components/global-styles/global-styles-provider.js +4 -2
  19. package/build/components/global-styles/global-styles-provider.js.map +1 -1
  20. package/build/components/global-styles/hooks.js +10 -1
  21. package/build/components/global-styles/hooks.js.map +1 -1
  22. package/build/components/global-styles/screen-color-palette.js +13 -17
  23. package/build/components/global-styles/screen-color-palette.js.map +1 -1
  24. package/build/components/global-styles/screen-colors.js +9 -1
  25. package/build/components/global-styles/screen-colors.js.map +1 -1
  26. package/build/components/global-styles/screen-link-color.js +48 -14
  27. package/build/components/global-styles/screen-link-color.js.map +1 -1
  28. package/build/components/global-styles/use-global-styles-output.js +171 -33
  29. package/build/components/global-styles/use-global-styles-output.js.map +1 -1
  30. package/build/components/global-styles/utils.js +1 -1
  31. package/build/components/global-styles/utils.js.map +1 -1
  32. package/build/components/keyboard-shortcut-help-modal/index.js +1 -3
  33. package/build/components/keyboard-shortcut-help-modal/index.js.map +1 -1
  34. package/build/components/template-details/edit-template-title.js +11 -3
  35. package/build/components/template-details/edit-template-title.js.map +1 -1
  36. package/build/components/template-details/index.js +1 -20
  37. package/build/components/template-details/index.js.map +1 -1
  38. package/build/store/selectors.js +4 -1
  39. package/build/store/selectors.js.map +1 -1
  40. package/build-module/components/add-new-template/add-custom-generic-template-modal.js +77 -0
  41. package/build-module/components/add-new-template/add-custom-generic-template-modal.js.map +1 -0
  42. package/build-module/components/add-new-template/add-custom-template-modal.js +92 -53
  43. package/build-module/components/add-new-template/add-custom-template-modal.js.map +1 -1
  44. package/build-module/components/add-new-template/new-template.js +80 -83
  45. package/build-module/components/add-new-template/new-template.js.map +1 -1
  46. package/build-module/components/add-new-template/utils.js +286 -40
  47. package/build-module/components/add-new-template/utils.js.map +1 -1
  48. package/build-module/components/code-editor/index.js +18 -5
  49. package/build-module/components/code-editor/index.js.map +1 -1
  50. package/build-module/components/editor/index.js +16 -0
  51. package/build-module/components/editor/index.js.map +1 -1
  52. package/build-module/components/error-boundary/index.js +5 -0
  53. package/build-module/components/error-boundary/index.js.map +1 -1
  54. package/build-module/components/global-styles/dimensions-panel.js +2 -6
  55. package/build-module/components/global-styles/dimensions-panel.js.map +1 -1
  56. package/build-module/components/global-styles/global-styles-provider.js +4 -2
  57. package/build-module/components/global-styles/global-styles-provider.js.map +1 -1
  58. package/build-module/components/global-styles/hooks.js +10 -1
  59. package/build-module/components/global-styles/hooks.js.map +1 -1
  60. package/build-module/components/global-styles/screen-color-palette.js +14 -19
  61. package/build-module/components/global-styles/screen-color-palette.js.map +1 -1
  62. package/build-module/components/global-styles/screen-colors.js +9 -1
  63. package/build-module/components/global-styles/screen-colors.js.map +1 -1
  64. package/build-module/components/global-styles/screen-link-color.js +47 -14
  65. package/build-module/components/global-styles/screen-link-color.js.map +1 -1
  66. package/build-module/components/global-styles/use-global-styles-output.js +171 -35
  67. package/build-module/components/global-styles/use-global-styles-output.js.map +1 -1
  68. package/build-module/components/global-styles/utils.js +2 -2
  69. package/build-module/components/global-styles/utils.js.map +1 -1
  70. package/build-module/components/keyboard-shortcut-help-modal/index.js +1 -2
  71. package/build-module/components/keyboard-shortcut-help-modal/index.js.map +1 -1
  72. package/build-module/components/template-details/edit-template-title.js +12 -3
  73. package/build-module/components/template-details/edit-template-title.js.map +1 -1
  74. package/build-module/components/template-details/index.js +2 -21
  75. package/build-module/components/template-details/index.js.map +1 -1
  76. package/build-module/store/selectors.js +5 -2
  77. package/build-module/store/selectors.js.map +1 -1
  78. package/build-style/style-rtl.css +21 -23
  79. package/build-style/style.css +21 -23
  80. package/package.json +29 -29
  81. package/src/components/add-new-template/add-custom-generic-template-modal.js +97 -0
  82. package/src/components/add-new-template/add-custom-template-modal.js +92 -58
  83. package/src/components/add-new-template/new-template.js +142 -94
  84. package/src/components/add-new-template/style.scss +21 -0
  85. package/src/components/add-new-template/utils.js +290 -46
  86. package/src/components/code-editor/index.js +15 -5
  87. package/src/components/editor/index.js +11 -0
  88. package/src/components/error-boundary/index.js +5 -0
  89. package/src/components/global-styles/dimensions-panel.js +2 -7
  90. package/src/components/global-styles/global-styles-provider.js +8 -9
  91. package/src/components/global-styles/hooks.js +15 -0
  92. package/src/components/global-styles/screen-color-palette.js +25 -27
  93. package/src/components/global-styles/screen-colors.js +9 -3
  94. package/src/components/global-styles/screen-link-color.js +65 -23
  95. package/src/components/global-styles/style.scss +7 -11
  96. package/src/components/global-styles/test/use-global-styles-output.js +168 -0
  97. package/src/components/global-styles/use-global-styles-output.js +234 -59
  98. package/src/components/global-styles/utils.js +2 -2
  99. package/src/components/keyboard-shortcut-help-modal/index.js +1 -2
  100. package/src/components/keyboard-shortcut-help-modal/style.scss +0 -5
  101. package/src/components/list/style.scss +0 -8
  102. package/src/components/template-details/edit-template-title.js +10 -2
  103. package/src/components/template-details/index.js +4 -21
  104. package/src/components/test/error-boundary.js +38 -0
  105. package/src/store/selectors.js +11 -5
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/edit-site/src/components/add-new-template/new-template.js"],"names":["DEFAULT_TEMPLATE_SLUGS","TEMPLATE_ICONS","home","single","post","page","archive","search","notFound","index","list","category","author","postAuthor","taxonomy","blockMeta","date","postDate","tag","attachment","media","NewTemplate","postType","history","postTypes","showCustomTemplateModal","setShowCustomTemplateModal","entityForSuggestions","setEntityForSuggestions","existingTemplates","defaultTemplateTypes","select","coreStore","getEntityRecords","per_page","editorStore","__experimentalGetDefaultTemplateTypes","postTypesEntitiesInfo","saveEntityRecord","createErrorNotice","noticesStore","setTemplate","editSiteStore","createTemplate","template","title","description","slug","newTemplate","toString","status","is_wp_suggestion","throwOnError","id","push","postId","type","error","errorMessage","message","code","existingTemplateSlugs","map","missingTemplates","extraTemplates","reduce","accumulator","_postType","labels","icon","hasGeneralTemplate","includes","hasEntities","menuItem","singular_name","startsWith","slice","onClick","postsToExclude","existingPosts","length","sort","template1","template2","indexOf","add_new","add_new_item","noArrow","variant"],"mappings":";;;;;;;;;AAcA;;AAXA;;AAKA;;AAOA;;AACA;;AACA;;AACA;;AAeA;;AACA;;AAKA;;AACA;;AACA;;AACA;;AA1CA;AACA;AACA;;AAGA;AACA;AACA;;AA6BA;AACA;AACA;AAMA,MAAMA,sBAAsB,GAAG,CAC9B,YAD8B,EAE9B,QAF8B,EAG9B,MAH8B,EAI9B,OAJ8B,EAK9B,SAL8B,EAM9B,QAN8B,EAO9B,UAP8B,EAQ9B,MAR8B,EAS9B,KAT8B,EAU9B,UAV8B,EAW9B,QAX8B,EAY9B,KAZ8B,CAA/B;AAeA,MAAMC,cAAc,GAAG;AACtB,gBAAcC,WADQ;AAEtBC,EAAAA,MAAM,EAAEC,WAFc;AAGtBC,EAAAA,IAAI,EAAJA,WAHsB;AAItBC,EAAAA,OAAO,EAAPA,cAJsB;AAKtBC,EAAAA,MAAM,EAANA,aALsB;AAMtB,OAAKC,eANiB;AAOtBC,EAAAA,KAAK,EAAEC,WAPe;AAQtBC,EAAAA,QAAQ,EAARA,eARsB;AAStBC,E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ALK,QAMFzB,QANJ;AAOA,WACC,4BAAC,oBAAD;AACC,MAAA,IAAI,EACHyB,IAAI,IACJpE,cAAc,CAAE8C,IAAF,CADd,IAEA3C,WAJF;AAMC,MAAA,YAAY,EAAC,MANd;AAOC,MAAA,IAAI,EAAG0C,WAPR;AAQC,MAAA,GAAG,EAAGC,IARP;AASC,MAAA,OAAO,EAAG,MACT8B,OAAO,GACJA,OAAO,CAAEjC,QAAF,CADH,GAEJD,cAAc,CAAEC,QAAF;AAZnB,OAeGC,KAfH,CADD;AAmBA,GA3BC,CADH,CADD,CAbF,CADD,EAgDGpB,uBAAuB,IACxB,4BAAC,+BAAD;AACC,IAAA,OAAO,EAAG,MAAMC,0BAA0B,CAAE,KAAF,CAD3C;AAEC,IAAA,QAAQ,EAAGiB,cAFZ;AAGC,IAAA,oBAAoB,EAAGhB;AAHxB,IAjDF,CADD;AA0DA","sourcesContent":["/**\n * External dependencies\n */\nimport { filter, includes } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tDropdownMenu,\n\tMenuGroup,\n\tMenuItem,\n\tNavigableMenu,\n} from '@wordpress/components';\nimport { useState } from '@wordpress/element';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { store as editorStore } from '@wordpress/editor';\nimport {\n\tarchive,\n\tblockMeta,\n\tcategory,\n\thome,\n\tlist,\n\tmedia,\n\tnotFound,\n\tpage,\n\tpost,\n\tpostAuthor,\n\tpostDate,\n\tsearch,\n\ttag,\n} from '@wordpress/icons';\nimport { __, sprintf } from '@wordpress/i18n';\nimport { store as noticesStore } from '@wordpress/notices';\n\n/**\n * Internal dependencies\n */\nimport AddCustomTemplateModal from './add-custom-template-modal';\nimport { usePostTypes, usePostTypesEntitiesInfo } from './utils';\nimport { useHistory } from '../routes';\nimport { store as editSiteStore } from '../../store';\n\nconst DEFAULT_TEMPLATE_SLUGS = [\n\t'front-page',\n\t'single',\n\t'page',\n\t'index',\n\t'archive',\n\t'author',\n\t'category',\n\t'date',\n\t'tag',\n\t'taxonomy',\n\t'search',\n\t'404',\n];\n\nconst TEMPLATE_ICONS = {\n\t'front-page': home,\n\tsingle: post,\n\tpage,\n\tarchive,\n\tsearch,\n\t404: notFound,\n\tindex: list,\n\tcategory,\n\tauthor: postAuthor,\n\ttaxonomy: blockMeta,\n\tdate: postDate,\n\ttag,\n\tattachment: media,\n};\n\nexport default function NewTemplate( { postType } ) {\n\tconst history = useHistory();\n\tconst postTypes = usePostTypes();\n\tconst [ showCustomTemplateModal, setShowCustomTemplateModal ] =\n\t\tuseState( false );\n\tconst [ entityForSuggestions, setEntityForSuggestions ] = useState( {} );\n\tconst { existingTemplates, defaultTemplateTypes } = useSelect(\n\t\t( select ) => ( {\n\t\t\texistingTemplates: select( coreStore ).getEntityRecords(\n\t\t\t\t'postType',\n\t\t\t\t'wp_template',\n\t\t\t\t{ per_page: -1 }\n\t\t\t),\n\t\t\tdefaultTemplateTypes:\n\t\t\t\tselect( editorStore ).__experimentalGetDefaultTemplateTypes(),\n\t\t} ),\n\t\t[]\n\t);\n\tconst postTypesEntitiesInfo = usePostTypesEntitiesInfo( existingTemplates );\n\tconst { saveEntityRecord } = useDispatch( coreStore );\n\tconst { createErrorNotice } = useDispatch( noticesStore );\n\tconst { setTemplate } = useDispatch( editSiteStore );\n\n\tasync function createTemplate( template ) {\n\t\ttry {\n\t\t\tconst { title, description, slug } = template;\n\t\t\tconst newTemplate = await saveEntityRecord(\n\t\t\t\t'postType',\n\t\t\t\t'wp_template',\n\t\t\t\t{\n\t\t\t\t\tdescription,\n\t\t\t\t\t// Slugs need to be strings, so this is for template `404`\n\t\t\t\t\tslug: slug.toString(),\n\t\t\t\t\tstatus: 'publish',\n\t\t\t\t\ttitle,\n\t\t\t\t\t// This adds a post meta field in template that is part of `is_custom` value calculation.\n\t\t\t\t\tis_wp_suggestion: true,\n\t\t\t\t},\n\t\t\t\t{ throwOnError: true }\n\t\t\t);\n\n\t\t\t// Set template before navigating away to avoid initial stale value.\n\t\t\tsetTemplate( newTemplate.id, newTemplate.slug );\n\n\t\t\t// Navigate to the created template editor.\n\t\t\thistory.push( {\n\t\t\t\tpostId: newTemplate.id,\n\t\t\t\tpostType: newTemplate.type,\n\t\t\t} );\n\n\t\t\t// TODO: Add a success notice?\n\t\t} catch ( error ) {\n\t\t\tconst errorMessage =\n\t\t\t\terror.message && error.code !== 'unknown_error'\n\t\t\t\t\t? error.message\n\t\t\t\t\t: __( 'An error occurred while creating the template.' );\n\n\t\t\tcreateErrorNotice( errorMessage, {\n\t\t\t\ttype: 'snackbar',\n\t\t\t} );\n\t\t}\n\t}\n\tconst existingTemplateSlugs = ( existingTemplates || [] ).map(\n\t\t( { slug } ) => slug\n\t);\n\tconst missingTemplates = filter(\n\t\tdefaultTemplateTypes,\n\t\t( template ) =>\n\t\t\tincludes( DEFAULT_TEMPLATE_SLUGS, template.slug ) &&\n\t\t\t! includes( existingTemplateSlugs, template.slug )\n\t);\n\n\tconst extraTemplates = ( postTypes || [] ).reduce(\n\t\t( accumulator, _postType ) => {\n\t\t\tconst { slug, labels, icon } = _postType;\n\t\t\tconst hasGeneralTemplate = existingTemplateSlugs?.includes(\n\t\t\t\t`single-${ slug }`\n\t\t\t);\n\t\t\tconst hasEntities = postTypesEntitiesInfo?.[ slug ]?.hasEntities;\n\t\t\tconst menuItem = {\n\t\t\t\tslug: `single-${ slug }`,\n\t\t\t\ttitle: sprintf(\n\t\t\t\t\t// translators: %s: Name of the post type e.g: \"Post\".\n\t\t\t\t\t__( 'Single item: %s' ),\n\t\t\t\t\tlabels.singular_name\n\t\t\t\t),\n\t\t\t\tdescription: sprintf(\n\t\t\t\t\t// translators: %s: Name of the post type e.g: \"Post\".\n\t\t\t\t\t__( 'Displays a single item: %s.' ),\n\t\t\t\t\tlabels.singular_name\n\t\t\t\t),\n\t\t\t\t// `icon` is the `menu_icon` property of a post type. We\n\t\t\t\t// only handle `dashicons` for now, even if the `menu_icon`\n\t\t\t\t// also supports urls and svg as values.\n\t\t\t\ticon: icon?.startsWith( 'dashicons-' )\n\t\t\t\t\t? icon.slice( 10 )\n\t\t\t\t\t: null,\n\t\t\t};\n\t\t\t// We have a different template creation flow only if they have entities.\n\t\t\tif ( hasEntities ) {\n\t\t\t\tmenuItem.onClick = ( template ) => {\n\t\t\t\t\tsetShowCustomTemplateModal( true );\n\t\t\t\t\tsetEntityForSuggestions( {\n\t\t\t\t\t\ttype: 'postType',\n\t\t\t\t\t\tslug,\n\t\t\t\t\t\tlabels,\n\t\t\t\t\t\thasGeneralTemplate,\n\t\t\t\t\t\ttemplate,\n\t\t\t\t\t\tpostsToExclude:\n\t\t\t\t\t\t\tpostTypesEntitiesInfo[ slug ].existingPosts,\n\t\t\t\t\t} );\n\t\t\t\t};\n\t\t\t}\n\t\t\t// We don't need to add the menu item if there are no\n\t\t\t// entities and the general template exists.\n\t\t\tif ( ! hasGeneralTemplate || hasEntities ) {\n\t\t\t\taccumulator.push( menuItem );\n\t\t\t}\n\t\t\treturn accumulator;\n\t\t},\n\t\t[]\n\t);\n\tif ( ! missingTemplates.length && ! extraTemplates.length ) {\n\t\treturn null;\n\t}\n\t// Update the sort order to match the DEFAULT_TEMPLATE_SLUGS order.\n\tmissingTemplates?.sort( ( template1, template2 ) => {\n\t\treturn (\n\t\t\tDEFAULT_TEMPLATE_SLUGS.indexOf( template1.slug ) -\n\t\t\tDEFAULT_TEMPLATE_SLUGS.indexOf( template2.slug )\n\t\t);\n\t} );\n\t// Append all extra templates at the end of the list for now.\n\tmissingTemplates.push( ...extraTemplates );\n\treturn (\n\t\t<>\n\t\t\t<DropdownMenu\n\t\t\t\tclassName=\"edit-site-new-template-dropdown\"\n\t\t\t\ticon={ null }\n\t\t\t\ttext={ postType.labels.add_new }\n\t\t\t\tlabel={ postType.labels.add_new_item }\n\t\t\t\tpopoverProps={ {\n\t\t\t\t\tnoArrow: false,\n\t\t\t\t} }\n\t\t\t\ttoggleProps={ {\n\t\t\t\t\tvariant: 'primary',\n\t\t\t\t} }\n\t\t\t>\n\t\t\t\t{ () => (\n\t\t\t\t\t<NavigableMenu className=\"edit-site-new-template-dropdown__popover\">\n\t\t\t\t\t\t<MenuGroup label={ postType.labels.add_new_item }>\n\t\t\t\t\t\t\t{ missingTemplates.map( ( template ) => {\n\t\t\t\t\t\t\t\tconst {\n\t\t\t\t\t\t\t\t\ttitle,\n\t\t\t\t\t\t\t\t\tdescription,\n\t\t\t\t\t\t\t\t\tslug,\n\t\t\t\t\t\t\t\t\tonClick,\n\t\t\t\t\t\t\t\t\ticon,\n\t\t\t\t\t\t\t\t} = template;\n\t\t\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\t\t\t<MenuItem\n\t\t\t\t\t\t\t\t\t\ticon={\n\t\t\t\t\t\t\t\t\t\t\ticon ||\n\t\t\t\t\t\t\t\t\t\t\tTEMPLATE_ICONS[ slug ] ||\n\t\t\t\t\t\t\t\t\t\t\tpost\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\ticonPosition=\"left\"\n\t\t\t\t\t\t\t\t\t\tinfo={ description }\n\t\t\t\t\t\t\t\t\t\tkey={ slug }\n\t\t\t\t\t\t\t\t\t\tonClick={ () =>\n\t\t\t\t\t\t\t\t\t\t\tonClick\n\t\t\t\t\t\t\t\t\t\t\t\t? onClick( template )\n\t\t\t\t\t\t\t\t\t\t\t\t: createTemplate( template )\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t\t{ title }\n\t\t\t\t\t\t\t\t\t</MenuItem>\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t} ) }\n\t\t\t\t\t\t</MenuGroup>\n\t\t\t\t\t</NavigableMenu>\n\t\t\t\t) }\n\t\t\t</DropdownMenu>\n\t\t\t{ showCustomTemplateModal && (\n\t\t\t\t<AddCustomTemplateModal\n\t\t\t\t\tonClose={ () => setShowCustomTemplateModal( false ) }\n\t\t\t\t\tonSelect={ createTemplate }\n\t\t\t\t\tentityForSuggestions={ entityForSuggestions }\n\t\t\t\t/>\n\t\t\t) }\n\t\t</>\n\t);\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/edit-site/src/components/add-new-template/new-template.js"],"names":["DEFAULT_TEMPLATE_SLUGS","TEMPLATE_ICONS","home","single","post","page","archive","search","notFound","index","list","category","author","postAuthor","taxonomy","blockMeta","date","postDate","tag","attachment","media","NewTemplate","postType","showCustomTemplateModal","setShowCustomTemplateModal","showCustomGenericTemplateModal","setShowCustomGenericTemplateModal","entityForSuggestions","setEntityForSuggestions","history","saveEntityRecord","coreStore","createErrorNotice","noticesStore","setTemplate","editSiteStore","createTemplate","template","isWPSuggestion","title","description","slug","newTemplate","toString","status","is_wp_suggestion","throwOnError","id","push","postId","type","error","errorMessage","message","code","missingTemplates","useMissingTemplates","length","labels","add_new","add_new_item","noArrow","variant","map","onClick","icon","customGenericTemplateIcon","postTypes","pagePostType","taxonomies","categoryTaxonomy","tagTaxonomy","existingTemplates","defaultTemplateTypes","existingTemplateSlugs","missingDefaultTemplates","filter","includes","onClickMenuItem","_entityForSuggestions","categoryMenuItem","entitiesConfig","tagMenuItem","pageMenuItem","enhancedMissingDefaultTemplateTypes","forEach","menuItem","matchIndex","findIndex","splice","sort","template1","template2","indexOf","extraPostTypeTemplates","extraTaxonomyTemplates"],"mappings":";;;;;;;;;AASA;;AANA;;AAOA;;AACA;;AACA;;AAgBA;;AACA;;AAKA;;AACA;;AAWA;;AACA;;AACA;;AAhDA;AACA;AACA;;AA6BA;AACA;AACA;AAiBA,MAAMA,sBAAsB,GAAG,CAC9B,YAD8B,EAE9B,QAF8B,EAG9B,MAH8B,EAI9B,OAJ8B,EAK9B,SAL8B,EAM9B,QAN8B,EAO9B,UAP8B,EAQ9B,MAR8B,EAS9B,KAT8B,EAU9B,UAV8B,EAW9B,QAX8B,EAY9B,KAZ8B,CAA/B;AAeA,MAAMC,cAAc,GAAG;AACtB,gBAAcC,WADQ;AA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xE,QAAK,EAAEA,QAAF,aAAEA,QAAF,eAAEA,QAAQ,CAAE7B,MAAZ,CAAL,EAA0B;AACzB;AACA;;AACD,UAAM8B,UAAU,GAAGH,mCAAmC,CAACI,SAApC,CAChBnD,QAAF,IAAgBA,QAAQ,CAACI,IAAT,KAAkB6C,QAAQ,CAAE,CAAF,CAAR,CAAc7C,IAD9B,CAAnB,CAJwE,CAOxE;AACA;AACA;AACA;AACA;;AACA,QAAK8C,UAAU,GAAG,CAAC,CAAnB,EAAuB;AACtBH,MAAAA,mCAAmC,CAACK,MAApC,CACCF,UADD,EAEC,CAFD,EAGCD,QAAQ,CAAE,CAAF,CAHT;AAKA,KAND,MAMO;AACNF,MAAAA,mCAAmC,CAACpC,IAApC,CAA0CsC,QAAQ,CAAE,CAAF,CAAlD;AACA;AACD,GArBD,EA5CC,CAkED;;AACAF,EAAAA,mCAAmC,SAAnC,IAAAA,mCAAmC,WAAnC,YAAAA,mCAAmC,CAAEM,IAArC,CAA2C,CAAEC,SAAF,EAAaC,SAAb,KAA4B;AACtE,WACC5F,sBAAsB,CAAC6F,OAAvB,CAAgCF,SAAS,CAAClD,IAA1C,IACAzC,sBAAsB,CAAC6F,OAAvB,CAAgCD,SAAS,CAACnD,IAA1C,CAFD;AAIA,GALD;AAMA,QAAMqD,sBAAsB,GAAG,8BAC9B3B,SAD8B,EAE9Bc,sBAAe3D,QAFe,EAG9BwD,eAH8B,CAA/B;AAKA,QAAMiB,sBAAsB,GAAG,8BAC9B1B,UAD8B,EAE9BY,sBAAenE,QAFe,EAG9BgE,eAH8B,CAA/B;AAKA,QAAMvB,gBAAgB,GAAG,CACxB,GAAG6B,mCADqB,EAExB,GAAGU,sBAFqB,EAGxB,GAAGC,sBAHqB,CAAzB;AAKA,SAAOxC,gBAAP;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport {\n\tDropdownMenu,\n\tMenuGroup,\n\tMenuItem,\n\tNavigableMenu,\n} from '@wordpress/components';\nimport { useState } from '@wordpress/element';\nimport { useDispatch } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport {\n\tarchive,\n\tblockMeta,\n\tcategory,\n\thome,\n\tlist,\n\tmedia,\n\tnotFound,\n\tpage,\n\tpost,\n\tpostAuthor,\n\tpostDate,\n\tsearch,\n\ttag,\n\tlayout as customGenericTemplateIcon,\n} from '@wordpress/icons';\nimport { __ } from '@wordpress/i18n';\nimport { store as noticesStore } from '@wordpress/notices';\n\n/**\n * Internal dependencies\n */\nimport AddCustomTemplateModal from './add-custom-template-modal';\nimport {\n\tuseExistingTemplates,\n\tuseDefaultTemplateTypes,\n\tentitiesConfig,\n\tusePostTypes,\n\tusePostTypePage,\n\tuseTaxonomies,\n\tuseTaxonomyCategory,\n\tuseTaxonomyTag,\n\tuseExtraTemplates,\n} from './utils';\nimport AddCustomGenericTemplateModal from './add-custom-generic-template-modal';\nimport { useHistory } from '../routes';\nimport { store as editSiteStore } from '../../store';\n\nconst DEFAULT_TEMPLATE_SLUGS = [\n\t'front-page',\n\t'single',\n\t'page',\n\t'index',\n\t'archive',\n\t'author',\n\t'category',\n\t'date',\n\t'tag',\n\t'taxonomy',\n\t'search',\n\t'404',\n];\n\nconst TEMPLATE_ICONS = {\n\t'front-page': home,\n\tsingle: post,\n\tpage,\n\tarchive,\n\tsearch,\n\t404: notFound,\n\tindex: list,\n\tcategory,\n\tauthor: postAuthor,\n\ttaxonomy: blockMeta,\n\tdate: postDate,\n\ttag,\n\tattachment: media,\n};\n\nexport default function NewTemplate( { postType } ) {\n\tconst [ showCustomTemplateModal, setShowCustomTemplateModal ] =\n\t\tuseState( false );\n\tconst [\n\t\tshowCustomGenericTemplateModal,\n\t\tsetShowCustomGenericTemplateModal,\n\t] = useState( false );\n\tconst [ entityForSuggestions, setEntityForSuggestions ] = useState( {} );\n\n\tconst history = useHistory();\n\tconst { saveEntityRecord } = useDispatch( coreStore );\n\tconst { createErrorNotice } = useDispatch( noticesStore );\n\tconst { setTemplate } = useDispatch( editSiteStore );\n\n\tasync function createTemplate( template, isWPSuggestion = true ) {\n\t\ttry {\n\t\t\tconst { title, description, slug } = template;\n\t\t\tconst newTemplate = await saveEntityRecord(\n\t\t\t\t'postType',\n\t\t\t\t'wp_template',\n\t\t\t\t{\n\t\t\t\t\tdescription,\n\t\t\t\t\t// Slugs need to be strings, so this is for template `404`\n\t\t\t\t\tslug: slug.toString(),\n\t\t\t\t\tstatus: 'publish',\n\t\t\t\t\ttitle,\n\t\t\t\t\t// This adds a post meta field in template that is part of `is_custom` value calculation.\n\t\t\t\t\tis_wp_suggestion: isWPSuggestion,\n\t\t\t\t},\n\t\t\t\t{ throwOnError: true }\n\t\t\t);\n\n\t\t\t// Set template before navigating away to avoid initial stale value.\n\t\t\tsetTemplate( newTemplate.id, newTemplate.slug );\n\n\t\t\t// Navigate to the created template editor.\n\t\t\thistory.push( {\n\t\t\t\tpostId: newTemplate.id,\n\t\t\t\tpostType: newTemplate.type,\n\t\t\t} );\n\n\t\t\t// TODO: Add a success notice?\n\t\t} catch ( error ) {\n\t\t\tconst errorMessage =\n\t\t\t\terror.message && error.code !== 'unknown_error'\n\t\t\t\t\t? error.message\n\t\t\t\t\t: __( 'An error occurred while creating the template.' );\n\n\t\t\tcreateErrorNotice( errorMessage, {\n\t\t\t\ttype: 'snackbar',\n\t\t\t} );\n\t\t}\n\t}\n\n\tconst missingTemplates = useMissingTemplates(\n\t\tsetEntityForSuggestions,\n\t\tsetShowCustomTemplateModal\n\t);\n\tif ( ! missingTemplates.length ) {\n\t\treturn null;\n\t}\n\treturn (\n\t\t<>\n\t\t\t<DropdownMenu\n\t\t\t\tclassName=\"edit-site-new-template-dropdown\"\n\t\t\t\ticon={ null }\n\t\t\t\ttext={ postType.labels.add_new }\n\t\t\t\tlabel={ postType.labels.add_new_item }\n\t\t\t\tpopoverProps={ {\n\t\t\t\t\tnoArrow: false,\n\t\t\t\t} }\n\t\t\t\ttoggleProps={ {\n\t\t\t\t\tvariant: 'primary',\n\t\t\t\t} }\n\t\t\t>\n\t\t\t\t{ () => (\n\t\t\t\t\t<NavigableMenu className=\"edit-site-new-template-dropdown__popover\">\n\t\t\t\t\t\t<MenuGroup label={ postType.labels.add_new_item }>\n\t\t\t\t\t\t\t{ missingTemplates.map( ( template ) => {\n\t\t\t\t\t\t\t\tconst {\n\t\t\t\t\t\t\t\t\ttitle,\n\t\t\t\t\t\t\t\t\tdescription,\n\t\t\t\t\t\t\t\t\tslug,\n\t\t\t\t\t\t\t\t\tonClick,\n\t\t\t\t\t\t\t\t\ticon,\n\t\t\t\t\t\t\t\t} = template;\n\t\t\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\t\t\t<MenuItem\n\t\t\t\t\t\t\t\t\t\ticon={\n\t\t\t\t\t\t\t\t\t\t\ticon ||\n\t\t\t\t\t\t\t\t\t\t\tTEMPLATE_ICONS[ slug ] ||\n\t\t\t\t\t\t\t\t\t\t\tpost\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\ticonPosition=\"left\"\n\t\t\t\t\t\t\t\t\t\tinfo={ description }\n\t\t\t\t\t\t\t\t\t\tkey={ slug }\n\t\t\t\t\t\t\t\t\t\tonClick={ () =>\n\t\t\t\t\t\t\t\t\t\t\tonClick\n\t\t\t\t\t\t\t\t\t\t\t\t? onClick( template )\n\t\t\t\t\t\t\t\t\t\t\t\t: createTemplate( template )\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t\t{ title }\n\t\t\t\t\t\t\t\t\t</MenuItem>\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t} ) }\n\t\t\t\t\t\t</MenuGroup>\n\t\t\t\t\t\t<MenuGroup>\n\t\t\t\t\t\t\t<MenuItem\n\t\t\t\t\t\t\t\ticon={ customGenericTemplateIcon }\n\t\t\t\t\t\t\t\ticonPosition=\"left\"\n\t\t\t\t\t\t\t\tinfo={ __(\n\t\t\t\t\t\t\t\t\t'Custom templates can be applied to any post or page.'\n\t\t\t\t\t\t\t\t) }\n\t\t\t\t\t\t\t\tkey=\"custom-template\"\n\t\t\t\t\t\t\t\tonClick={ () =>\n\t\t\t\t\t\t\t\t\tsetShowCustomGenericTemplateModal( true )\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t{ __( 'Custom template' ) }\n\t\t\t\t\t\t\t</MenuItem>\n\t\t\t\t\t\t</MenuGroup>\n\t\t\t\t\t</NavigableMenu>\n\t\t\t\t) }\n\t\t\t</DropdownMenu>\n\t\t\t{ showCustomTemplateModal && (\n\t\t\t\t<AddCustomTemplateModal\n\t\t\t\t\tonClose={ () => setShowCustomTemplateModal( false ) }\n\t\t\t\t\tonSelect={ createTemplate }\n\t\t\t\t\tentityForSuggestions={ entityForSuggestions }\n\t\t\t\t/>\n\t\t\t) }\n\t\t\t{ showCustomGenericTemplateModal && (\n\t\t\t\t<AddCustomGenericTemplateModal\n\t\t\t\t\tonClose={ () => setShowCustomGenericTemplateModal( false ) }\n\t\t\t\t\tcreateTemplate={ createTemplate }\n\t\t\t\t/>\n\t\t\t) }\n\t\t</>\n\t);\n}\n\nfunction useMissingTemplates(\n\tsetEntityForSuggestions,\n\tsetShowCustomTemplateModal\n) {\n\tconst postTypes = usePostTypes();\n\tconst pagePostType = usePostTypePage();\n\tconst taxonomies = useTaxonomies();\n\tconst categoryTaxonomy = useTaxonomyCategory();\n\tconst tagTaxonomy = useTaxonomyTag();\n\n\tconst existingTemplates = useExistingTemplates();\n\tconst defaultTemplateTypes = useDefaultTemplateTypes();\n\n\tconst existingTemplateSlugs = ( existingTemplates || [] ).map(\n\t\t( { slug } ) => slug\n\t);\n\n\tconst missingDefaultTemplates = ( defaultTemplateTypes || [] ).filter(\n\t\t( template ) =>\n\t\t\tDEFAULT_TEMPLATE_SLUGS.includes( template.slug ) &&\n\t\t\t! existingTemplateSlugs.includes( template.slug )\n\t);\n\tconst onClickMenuItem = ( _entityForSuggestions ) => {\n\t\tsetShowCustomTemplateModal( true );\n\t\tsetEntityForSuggestions( _entityForSuggestions );\n\t};\n\t// TODO: find better names for these variables. `useExtraTemplates` returns an array of items.\n\tconst categoryMenuItem = useExtraTemplates(\n\t\tcategoryTaxonomy,\n\t\tentitiesConfig.category,\n\t\tonClickMenuItem\n\t);\n\tconst tagMenuItem = useExtraTemplates(\n\t\ttagTaxonomy,\n\t\tentitiesConfig.tag,\n\t\tonClickMenuItem\n\t);\n\tconst pageMenuItem = useExtraTemplates(\n\t\tpagePostType,\n\t\tentitiesConfig.page,\n\t\tonClickMenuItem\n\t);\n\t// We need to replace existing default template types with\n\t// the create specific template functionality. The original\n\t// info (title, description, etc.) is preserved in the\n\t// `useExtraTemplates` hook.\n\tconst enhancedMissingDefaultTemplateTypes = [ ...missingDefaultTemplates ];\n\t[ categoryMenuItem, tagMenuItem, pageMenuItem ].forEach( ( menuItem ) => {\n\t\tif ( ! menuItem?.length ) {\n\t\t\treturn;\n\t\t}\n\t\tconst matchIndex = enhancedMissingDefaultTemplateTypes.findIndex(\n\t\t\t( template ) => template.slug === menuItem[ 0 ].slug\n\t\t);\n\t\t// Some default template types might have been filtered above from\n\t\t// `missingDefaultTemplates` because they only check for the general\n\t\t// template. So here we either replace or append the item, augmented\n\t\t// with the check if it has available specific item to create a\n\t\t// template for.\n\t\tif ( matchIndex > -1 ) {\n\t\t\tenhancedMissingDefaultTemplateTypes.splice(\n\t\t\t\tmatchIndex,\n\t\t\t\t1,\n\t\t\t\tmenuItem[ 0 ]\n\t\t\t);\n\t\t} else {\n\t\t\tenhancedMissingDefaultTemplateTypes.push( menuItem[ 0 ] );\n\t\t}\n\t} );\n\t// Update the sort order to match the DEFAULT_TEMPLATE_SLUGS order.\n\tenhancedMissingDefaultTemplateTypes?.sort( ( template1, template2 ) => {\n\t\treturn (\n\t\t\tDEFAULT_TEMPLATE_SLUGS.indexOf( template1.slug ) -\n\t\t\tDEFAULT_TEMPLATE_SLUGS.indexOf( template2.slug )\n\t\t);\n\t} );\n\tconst extraPostTypeTemplates = useExtraTemplates(\n\t\tpostTypes,\n\t\tentitiesConfig.postType,\n\t\tonClickMenuItem\n\t);\n\tconst extraTaxonomyTemplates = useExtraTemplates(\n\t\ttaxonomies,\n\t\tentitiesConfig.taxonomy,\n\t\tonClickMenuItem\n\t);\n\tconst missingTemplates = [\n\t\t...enhancedMissingDefaultTemplateTypes,\n\t\t...extraPostTypeTemplates,\n\t\t...extraTaxonomyTemplates,\n\t];\n\treturn missingTemplates;\n}\n"]}
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.usePostTypesEntitiesInfo = exports.usePostTypes = exports.mapToIHasNameAndId = void 0;
6
+ exports.useTaxonomyTag = exports.useTaxonomyCategory = exports.useTaxonomies = exports.usePostTypes = exports.usePostTypePage = exports.useExtraTemplates = exports.useExistingTemplates = exports.useDefaultTemplateTypes = exports.mapToIHasNameAndId = exports.entitiesConfig = void 0;
7
7
 
8
8
  var _lodash = require("lodash");
9
9
 
@@ -11,10 +11,16 @@ var _data = require("@wordpress/data");
11
11
 
12
12
  var _coreData = require("@wordpress/core-data");
13
13
 
14
+ var _editor = require("@wordpress/editor");
15
+
14
16
  var _htmlEntities = require("@wordpress/html-entities");
15
17
 
16
18
  var _element = require("@wordpress/element");
17
19
 
20
+ var _i18n = require("@wordpress/i18n");
21
+
22
+ var _icons = require("@wordpress/icons");
23
+
18
24
  /**
19
25
  * External dependencies
20
26
  */
@@ -22,70 +28,275 @@ var _element = require("@wordpress/element");
22
28
  /**
23
29
  * WordPress dependencies
24
30
  */
25
- const usePostTypes = () => {
31
+
32
+ /**
33
+ * @typedef IHasNameAndId
34
+ * @property {string|number} id The entity's id.
35
+ * @property {string} name The entity's name.
36
+ */
37
+
38
+ /**
39
+ * Helper util to map records to add a `name` prop from a
40
+ * provided path, in order to handle all entities in the same
41
+ * fashion(implementing`IHasNameAndId` interface).
42
+ *
43
+ * @param {Object[]} entities The array of entities.
44
+ * @param {string} path The path to map a `name` property from the entity.
45
+ * @return {IHasNameAndId[]} An array of enitities that now implement the `IHasNameAndId` interface.
46
+ */
47
+ const mapToIHasNameAndId = (entities, path) => {
48
+ return (entities || []).map(entity => ({ ...entity,
49
+ name: (0, _htmlEntities.decodeEntities)((0, _lodash.get)(entity, path))
50
+ }));
51
+ };
52
+ /**
53
+ * @typedef {Object} EntitiesInfo
54
+ * @property {boolean} hasEntities If an entity has available records(posts, terms, etc..).
55
+ * @property {number[]} existingEntitiesIds An array of the existing entities ids.
56
+ */
57
+
58
+ /**
59
+ * @typedef {Object} EntityConfig
60
+ * @property {string} entityName The entity's name.
61
+ * @property {Function} getOrderBy Getter for an entity's `orderBy` query parameter, given the object
62
+ * {search} as argument.
63
+ * @property {Function} getIcon Getter function for returning an entity's icon for the menu item.
64
+ * @property {Function} getTitle Getter function for returning an entity's title for the menu item.
65
+ * @property {Function} getDescription Getter function for returning an entity's description for the menu item.
66
+ * @property {string} recordNamePath The path to an entity's properties to use as a `name`. If not provided
67
+ * is assumed that `name` property exists.
68
+ * @property {string} templatePrefix The template prefix to create new templates and check against existing
69
+ * templates. For example custom post types need a `single-` prefix to all
70
+ * templates(`single-post-hello`), whereas `pages` don't (`page-hello`).
71
+ * @property {string} templateSlug If this property is provided, it is going to be used for the creation of
72
+ * new templates and the check against existing templates in the place
73
+ * of the actual entity's `slug`. An example is `Tag` templates where the
74
+ * the Tag's taxonomy slug is `post_tag`, but template hierarchy is based
75
+ * on `tag` alias.
76
+ */
77
+
78
+
79
+ exports.mapToIHasNameAndId = mapToIHasNameAndId;
80
+ const taxonomyBaseConfig = {
81
+ entityName: 'taxonomy',
82
+ getOrderBy: _ref => {
83
+ let {
84
+ search
85
+ } = _ref;
86
+ return search ? 'name' : 'count';
87
+ },
88
+ getIcon: () => _icons.blockMeta,
89
+ getTitle: labels => (0, _i18n.sprintf)( // translators: %s: Name of the taxonomy e.g: "Cagegory".
90
+ (0, _i18n.__)('Single taxonomy: %s'), labels.singular_name),
91
+ getDescription: labels => (0, _i18n.sprintf)( // translators: %s: Name of the taxonomy e.g: "Product Categories".
92
+ (0, _i18n.__)('Displays a single taxonomy: %s.'), labels.singular_name)
93
+ };
94
+ const postTypeBaseConfig = {
95
+ entityName: 'postType',
96
+ getOrderBy: _ref2 => {
97
+ let {
98
+ search
99
+ } = _ref2;
100
+ return search ? 'relevance' : 'modified';
101
+ },
102
+ recordNamePath: 'title.rendered',
103
+ // `icon` is the `menu_icon` property of a post type. We
104
+ // only handle `dashicons` for now, even if the `menu_icon`
105
+ // also supports urls and svg as values.
106
+ getIcon: _icon => _icon !== null && _icon !== void 0 && _icon.startsWith('dashicons-') ? _icon.slice(10) : _icons.post,
107
+ getTitle: labels => (0, _i18n.sprintf)( // translators: %s: Name of the post type e.g: "Post".
108
+ (0, _i18n.__)('Single item: %s'), labels.singular_name),
109
+ getDescription: labels => (0, _i18n.sprintf)( // translators: %s: Name of the post type e.g: "Post".
110
+ (0, _i18n.__)('Displays a single item: %s.'), labels.singular_name)
111
+ };
112
+ const entitiesConfig = {
113
+ postType: { ...postTypeBaseConfig,
114
+ templatePrefix: 'single-'
115
+ },
116
+ page: { ...postTypeBaseConfig
117
+ },
118
+ taxonomy: { ...taxonomyBaseConfig,
119
+ templatePrefix: 'taxonomy-'
120
+ },
121
+ category: { ...taxonomyBaseConfig
122
+ },
123
+ tag: { ...taxonomyBaseConfig,
124
+ templateSlug: 'tag'
125
+ }
126
+ };
127
+ exports.entitiesConfig = entitiesConfig;
128
+
129
+ const useExistingTemplates = () => {
130
+ return (0, _data.useSelect)(select => select(_coreData.store).getEntityRecords('postType', 'wp_template', {
131
+ per_page: -1
132
+ }), []);
133
+ };
134
+
135
+ exports.useExistingTemplates = useExistingTemplates;
136
+
137
+ const useDefaultTemplateTypes = () => {
138
+ return (0, _data.useSelect)(select => select(_editor.store).__experimentalGetDefaultTemplateTypes(), []);
139
+ };
140
+
141
+ exports.useDefaultTemplateTypes = useDefaultTemplateTypes;
142
+
143
+ const usePublicPostTypes = () => {
26
144
  const postTypes = (0, _data.useSelect)(select => select(_coreData.store).getPostTypes({
27
145
  per_page: -1
28
146
  }), []);
29
147
  return (0, _element.useMemo)(() => {
30
- const excludedPostTypes = ['attachment', 'page'];
31
- return postTypes === null || postTypes === void 0 ? void 0 : postTypes.filter(_ref => {
148
+ const excludedPostTypes = ['attachment'];
149
+ return postTypes === null || postTypes === void 0 ? void 0 : postTypes.filter(_ref3 => {
32
150
  let {
33
151
  viewable,
34
152
  slug
35
- } = _ref;
153
+ } = _ref3;
36
154
  return viewable && !excludedPostTypes.includes(slug);
37
155
  });
38
156
  }, [postTypes]);
157
+ }; // `page` post type is a special case in the template hierarchy,
158
+ // so we exclude it from the list of post types and we handle it
159
+ // separately.
160
+
161
+
162
+ const usePostTypes = () => {
163
+ const postTypes = usePublicPostTypes();
164
+ return (0, _element.useMemo)(() => {
165
+ return postTypes === null || postTypes === void 0 ? void 0 : postTypes.filter(_ref4 => {
166
+ let {
167
+ slug
168
+ } = _ref4;
169
+ return slug !== 'page';
170
+ });
171
+ }, [postTypes]);
172
+ };
173
+
174
+ exports.usePostTypes = usePostTypes;
175
+
176
+ const usePostTypePage = () => {
177
+ const postTypes = usePublicPostTypes();
178
+ return (0, _element.useMemo)(() => {
179
+ return postTypes === null || postTypes === void 0 ? void 0 : postTypes.filter(_ref5 => {
180
+ let {
181
+ slug
182
+ } = _ref5;
183
+ return slug === 'page';
184
+ });
185
+ }, [postTypes]);
186
+ };
187
+
188
+ exports.usePostTypePage = usePostTypePage;
189
+
190
+ const usePublicTaxonomies = () => {
191
+ const taxonomies = (0, _data.useSelect)(select => select(_coreData.store).getTaxonomies({
192
+ per_page: -1
193
+ }), []);
194
+ return (0, _element.useMemo)(() => {
195
+ return taxonomies === null || taxonomies === void 0 ? void 0 : taxonomies.filter(_ref6 => {
196
+ let {
197
+ visibility
198
+ } = _ref6;
199
+ return visibility === null || visibility === void 0 ? void 0 : visibility.publicly_queryable;
200
+ });
201
+ }, [taxonomies]);
39
202
  };
40
203
  /**
41
- * @typedef {Object} PostTypeEntitiesInfo
42
- * @property {boolean} hasEntities If a postType has available entities.
43
- * @property {number[]} existingPosts An array of the existing entities ids.
204
+ * `category` and `post_tag` are handled specifically in template
205
+ * hierarchy so we need to differentiate them and return the rest,
206
+ * e.g. `category-$slug` and `taxonomy-$taxonomy-$term`.
44
207
  */
45
208
 
209
+
210
+ const useTaxonomies = () => {
211
+ const taxonomies = usePublicTaxonomies();
212
+ const specialTaxonomies = ['category', 'post_tag'];
213
+ return (0, _element.useMemo)(() => taxonomies === null || taxonomies === void 0 ? void 0 : taxonomies.filter(_ref7 => {
214
+ let {
215
+ slug
216
+ } = _ref7;
217
+ return !specialTaxonomies.includes(slug);
218
+ }), [taxonomies]);
219
+ };
220
+
221
+ exports.useTaxonomies = useTaxonomies;
222
+
223
+ const useTaxonomyCategory = () => {
224
+ const taxonomies = usePublicTaxonomies();
225
+ return (0, _element.useMemo)(() => taxonomies === null || taxonomies === void 0 ? void 0 : taxonomies.filter(_ref8 => {
226
+ let {
227
+ slug
228
+ } = _ref8;
229
+ return slug === 'category';
230
+ }), [taxonomies]);
231
+ };
232
+
233
+ exports.useTaxonomyCategory = useTaxonomyCategory;
234
+
235
+ const useTaxonomyTag = () => {
236
+ const taxonomies = usePublicTaxonomies();
237
+ return (0, _element.useMemo)(() => taxonomies === null || taxonomies === void 0 ? void 0 : taxonomies.filter(_ref9 => {
238
+ let {
239
+ slug
240
+ } = _ref9;
241
+ return slug === 'post_tag';
242
+ }), [taxonomies]);
243
+ };
46
244
  /**
47
- * Helper hook that returns information about a post type having
48
- * posts that we can create a specific template for.
245
+ * Helper hook that returns information about an entity having
246
+ * records that we can create a specific template for.
247
+ *
248
+ * For example we can search for `terms` in `taxonomy` entity or
249
+ * `posts` in `postType` entity.
49
250
  *
50
- * First we need to find the existing posts with an associated template,
51
- * to query afterwards for any remaing post, by excluding them.
251
+ * First we need to find the existing records with an associated template,
252
+ * to query afterwards for any remaing record, by excluding them.
52
253
  *
53
- * @param {string[]} existingTemplates The existing templates.
54
- * @return {Record<string,PostTypeEntitiesInfo>} An object with the postTypes as `keys` and PostTypeEntitiesInfo as values.
254
+ * @param {string[]} existingTemplates The existing templates.
255
+ * @param {Object[]} entities The array of entities we need to get extra information.
256
+ * @param {EntityConfig} entityConfig The entity config.
257
+ * @return {Record<string,EntitiesInfo>} An object with the `entities.slug` as `keys` and EntitiesInfo as values.
55
258
  */
56
259
 
57
260
 
58
- exports.usePostTypes = usePostTypes;
261
+ exports.useTaxonomyTag = useTaxonomyTag;
59
262
 
60
- const usePostTypesEntitiesInfo = existingTemplates => {
61
- const postTypes = usePostTypes();
263
+ const useEntitiesInfo = (existingTemplates, entities, _ref10) => {
264
+ let {
265
+ entityName,
266
+ templatePrefix,
267
+ templateSlug
268
+ } = _ref10;
62
269
  const slugsToExcludePerEntity = (0, _element.useMemo)(() => {
63
- return postTypes === null || postTypes === void 0 ? void 0 : postTypes.reduce((accumulator, _postType) => {
64
- const slugsWithTemplates = (existingTemplates || []).reduce((_accumulator, existingTemplate) => {
65
- const prefix = `single-${_postType.slug}-`;
270
+ return entities === null || entities === void 0 ? void 0 : entities.reduce((accumulator, entity) => {
271
+ let _prefix = `${templateSlug || entity.slug}-`;
272
+
273
+ if (templatePrefix) {
274
+ _prefix = templatePrefix + _prefix;
275
+ }
66
276
 
67
- if (existingTemplate.slug.startsWith(prefix)) {
68
- _accumulator.push(existingTemplate.slug.substring(prefix.length));
277
+ const slugsWithTemplates = (existingTemplates || []).reduce((_accumulator, existingTemplate) => {
278
+ if (existingTemplate.slug.startsWith(_prefix)) {
279
+ _accumulator.push(existingTemplate.slug.substring(_prefix.length));
69
280
  }
70
281
 
71
282
  return _accumulator;
72
283
  }, []);
73
284
 
74
285
  if (slugsWithTemplates.length) {
75
- accumulator[_postType.slug] = slugsWithTemplates;
286
+ accumulator[entity.slug] = slugsWithTemplates;
76
287
  }
77
288
 
78
289
  return accumulator;
79
290
  }, {});
80
- }, [postTypes, existingTemplates]);
81
- const postsToExcludePerEntity = (0, _data.useSelect)(select => {
291
+ }, [entities, existingTemplates]);
292
+ const recordsToExcludePerEntity = (0, _data.useSelect)(select => {
82
293
  if (!slugsToExcludePerEntity) {
83
294
  return;
84
295
  }
85
296
 
86
- const postsToExclude = Object.entries(slugsToExcludePerEntity).reduce((accumulator, _ref2) => {
87
- let [slug, slugsWithTemplates] = _ref2;
88
- const postsWithTemplates = select(_coreData.store).getEntityRecords('postType', slug, {
297
+ return Object.entries(slugsToExcludePerEntity).reduce((accumulator, _ref11) => {
298
+ let [slug, slugsWithTemplates] = _ref11;
299
+ const postsWithTemplates = select(_coreData.store).getEntityRecords(entityName, slug, {
89
300
  _fields: 'id',
90
301
  context: 'view',
91
302
  slug: slugsWithTemplates
@@ -97,43 +308,98 @@ const usePostTypesEntitiesInfo = existingTemplates => {
97
308
 
98
309
  return accumulator;
99
310
  }, {});
100
- return postsToExclude;
101
311
  }, [slugsToExcludePerEntity]);
102
312
  const entitiesInfo = (0, _data.useSelect)(select => {
103
- return postTypes === null || postTypes === void 0 ? void 0 : postTypes.reduce((accumulator, _ref3) => {
104
- var _postsToExcludePerEnt, _select$getEntityReco;
313
+ return entities === null || entities === void 0 ? void 0 : entities.reduce((accumulator, _ref12) => {
314
+ var _recordsToExcludePerE, _select$getEntityReco;
105
315
 
106
316
  let {
107
317
  slug
108
- } = _ref3;
109
- const existingPosts = (postsToExcludePerEntity === null || postsToExcludePerEntity === void 0 ? void 0 : (_postsToExcludePerEnt = postsToExcludePerEntity[slug]) === null || _postsToExcludePerEnt === void 0 ? void 0 : _postsToExcludePerEnt.map(_ref4 => {
318
+ } = _ref12;
319
+ const existingEntitiesIds = (recordsToExcludePerEntity === null || recordsToExcludePerEntity === void 0 ? void 0 : (_recordsToExcludePerE = recordsToExcludePerEntity[slug]) === null || _recordsToExcludePerE === void 0 ? void 0 : _recordsToExcludePerE.map(_ref13 => {
110
320
  let {
111
321
  id
112
- } = _ref4;
322
+ } = _ref13;
113
323
  return id;
114
324
  })) || [];
115
325
  accumulator[slug] = {
116
- hasEntities: !!((_select$getEntityReco = select(_coreData.store).getEntityRecords('postType', slug, {
326
+ hasEntities: !!((_select$getEntityReco = select(_coreData.store).getEntityRecords(entityName, slug, {
117
327
  per_page: 1,
118
328
  _fields: 'id',
119
329
  context: 'view',
120
- exclude: existingPosts
330
+ exclude: existingEntitiesIds
121
331
  })) !== null && _select$getEntityReco !== void 0 && _select$getEntityReco.length),
122
- existingPosts
332
+ existingEntitiesIds
123
333
  };
124
334
  return accumulator;
125
335
  }, {});
126
- }, [postTypes, postsToExcludePerEntity]);
336
+ }, [entities, recordsToExcludePerEntity]);
127
337
  return entitiesInfo;
128
338
  };
129
339
 
130
- exports.usePostTypesEntitiesInfo = usePostTypesEntitiesInfo;
340
+ const useExtraTemplates = (entities, entityConfig, onClickMenuItem) => {
341
+ const existingTemplates = useExistingTemplates();
342
+ const defaultTemplateTypes = useDefaultTemplateTypes();
343
+ const entitiesInfo = useEntitiesInfo(existingTemplates, entities, entityConfig);
344
+ const existingTemplateSlugs = (existingTemplates || []).map(_ref14 => {
345
+ let {
346
+ slug
347
+ } = _ref14;
348
+ return slug;
349
+ });
350
+ const extraTemplates = (entities || []).reduce((accumulator, entity) => {
351
+ var _entityConfig$getIcon, _entitiesInfo$slug;
131
352
 
132
- const mapToIHasNameAndId = (entities, path) => {
133
- return (entities || []).map(entity => ({ ...entity,
134
- name: (0, _htmlEntities.decodeEntities)((0, _lodash.get)(entity, path))
135
- }));
353
+ const {
354
+ slug,
355
+ labels,
356
+ icon
357
+ } = entity;
358
+ const slugForGeneralTemplate = entityConfig.templateSlug || slug; // We need to check if the general template is part of the
359
+ // defaultTemplateTypes. If it is, just use that info and
360
+ // augment it with the specific template functionality.
361
+
362
+ const defaultTemplateType = defaultTemplateTypes === null || defaultTemplateTypes === void 0 ? void 0 : defaultTemplateTypes.find(_ref15 => {
363
+ let {
364
+ slug: _slug
365
+ } = _ref15;
366
+ return _slug === slugForGeneralTemplate;
367
+ });
368
+ const generalTemplateSlug = (defaultTemplateType === null || defaultTemplateType === void 0 ? void 0 : defaultTemplateType.slug) || `${entityConfig.templatePrefix}${slug}`;
369
+ const hasGeneralTemplate = existingTemplateSlugs === null || existingTemplateSlugs === void 0 ? void 0 : existingTemplateSlugs.includes(generalTemplateSlug);
370
+ const menuItem = defaultTemplateType ? { ...defaultTemplateType
371
+ } : {
372
+ slug: generalTemplateSlug,
373
+ title: entityConfig.getTitle(labels),
374
+ description: entityConfig.getDescription(labels),
375
+ icon: (_entityConfig$getIcon = entityConfig.getIcon) === null || _entityConfig$getIcon === void 0 ? void 0 : _entityConfig$getIcon.call(entityConfig, icon)
376
+ };
377
+ const hasEntities = entitiesInfo === null || entitiesInfo === void 0 ? void 0 : (_entitiesInfo$slug = entitiesInfo[slug]) === null || _entitiesInfo$slug === void 0 ? void 0 : _entitiesInfo$slug.hasEntities; // We have a different template creation flow only if they have entities.
378
+
379
+ if (hasEntities) {
380
+ menuItem.onClick = template => {
381
+ onClickMenuItem({
382
+ type: entityConfig.entityName,
383
+ slug,
384
+ config: entityConfig,
385
+ labels,
386
+ hasGeneralTemplate,
387
+ template,
388
+ postsToExclude: entitiesInfo[slug].existingEntitiesIds
389
+ });
390
+ };
391
+ } // We don't need to add the menu item if there are no
392
+ // entities and the general template exists.
393
+
394
+
395
+ if (!hasGeneralTemplate || hasEntities) {
396
+ accumulator.push(menuItem);
397
+ }
398
+
399
+ return accumulator;
400
+ }, []);
401
+ return extraTemplates;
136
402
  };
137
403
 
138
- exports.mapToIHasNameAndId = mapToIHasNameAndId;
404
+ exports.useExtraTemplates = useExtraTemplates;
139
405
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/edit-site/src/components/add-new-template/utils.js"],"names":["usePostTypes","postTypes","select","coreStore","getPostTypes","per_page","excludedPostTypes","filter","viewable","slug","includes","usePostTypesEntitiesInfo","existingTemplates","slugsToExcludePerEntity","reduce","accumulator","_postType","slugsWithTemplates","_accumulator","existingTemplate","prefix","startsWith","push","substring","length","postsToExcludePerEntity","postsToExclude","Object","entries","postsWithTemplates","getEntityRecords","_fields","context","entitiesInfo","existingPosts","map","id","hasEntities","exclude","mapToIHasNameAndId","entities","path","entity","name"],"mappings":";;;;;;;AAGA;;AAKA;;AACA;;AACA;;AACA;;AAXA;AACA;AACA;;AAGA;AACA;AACA;AAMO,MAAMA,YAAY,GAAG,MAAM;AACjC,QAAMC,SAAS,GAAG,qBACfC,MAAF,IAAcA,MAAM,CAAEC,eAAF,CAAN,CAAoBC,YAApB,CAAkC;AAAEC,IAAAA,QAAQ,EAAE,CAAC;AAAb,GAAlC,CADG,EAEjB,EAFiB,CAAlB;AAIA,SAAO,sBAAS,MAAM;AACrB,UAAMC,iBAAiB,GAAG,CAAE,YAAF,EAAgB,MAAhB,CAA1B;AACA,WAAOL,SAAP,aAAOA,SAAP,uBAAOA,SAAS,CAAEM,MAAX,CACN;AAAA,UAAE;AAAEC,QAAAA,QAAF;AAAYC,QAAAA;AAAZ,OAAF;AAAA,aACCD,QAAQ,IAAI,CAAEF,iBAAiB,CAACI,QAAlB,CAA4BD,IAA5B,CADf;AAAA,KADM,CAAP;AAIA,GANM,EAMJ,CAAER,SAAF,CANI,CAAP;AAOA,CAZM;AAcP;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;AACO,MAAMU,wBAAwB,GAAKC,iBAAF,IAAyB;AAChE,QAAMX,SAAS,GAAGD,YAAY,EAA9B;AACA,QAAMa,uBAAuB,GAAG,sBAAS,MAAM;AAC9C,WAAOZ,SAAP,aAAOA,SAAP,uBAAOA,SAAS,CAAEa,MAAX,CAAmB,CAAEC,WAAF,EAAeC,SAAf,KAA8B;AACvD,YAAMC,kBAAkB,GAAG,CAAEL,iBAAiB,IAAI,EAAvB,EAA4BE,MAA5B,CAC1B,CAAE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AhB,CAAxB,CAAP;AAIA,CALM","sourcesContent":["/**\n * External dependencies\n */\nimport { get } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { useMemo } from '@wordpress/element';\n\nexport const usePostTypes = () => {\n\tconst postTypes = useSelect(\n\t\t( select ) => select( coreStore ).getPostTypes( { per_page: -1 } ),\n\t\t[]\n\t);\n\treturn useMemo( () => {\n\t\tconst excludedPostTypes = [ 'attachment', 'page' ];\n\t\treturn postTypes?.filter(\n\t\t\t( { viewable, slug } ) =>\n\t\t\t\tviewable && ! excludedPostTypes.includes( slug )\n\t\t);\n\t}, [ postTypes ] );\n};\n\n/**\n * @typedef {Object} PostTypeEntitiesInfo\n * @property {boolean} hasEntities If a postType has available entities.\n * @property {number[]} existingPosts An array of the existing entities ids.\n */\n\n/**\n * Helper hook that returns information about a post type having\n * posts that we can create a specific template for.\n *\n * First we need to find the existing posts with an associated template,\n * to query afterwards for any remaing post, by excluding them.\n *\n * @param {string[]} existingTemplates The existing templates.\n * @return {Record<string,PostTypeEntitiesInfo>} An object with the postTypes as `keys` and PostTypeEntitiesInfo as values.\n */\nexport const usePostTypesEntitiesInfo = ( existingTemplates ) => {\n\tconst postTypes = usePostTypes();\n\tconst slugsToExcludePerEntity = useMemo( () => {\n\t\treturn postTypes?.reduce( ( accumulator, _postType ) => {\n\t\t\tconst slugsWithTemplates = ( existingTemplates || [] ).reduce(\n\t\t\t\t( _accumulator, existingTemplate ) => {\n\t\t\t\t\tconst prefix = `single-${ _postType.slug }-`;\n\t\t\t\t\tif ( existingTemplate.slug.startsWith( prefix ) ) {\n\t\t\t\t\t\t_accumulator.push(\n\t\t\t\t\t\t\texistingTemplate.slug.substring( prefix.length )\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn _accumulator;\n\t\t\t\t},\n\t\t\t\t[]\n\t\t\t);\n\t\t\tif ( slugsWithTemplates.length ) {\n\t\t\t\taccumulator[ _postType.slug ] = slugsWithTemplates;\n\t\t\t}\n\t\t\treturn accumulator;\n\t\t}, {} );\n\t}, [ postTypes, existingTemplates ] );\n\tconst postsToExcludePerEntity = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! slugsToExcludePerEntity ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst postsToExclude = Object.entries(\n\t\t\t\tslugsToExcludePerEntity\n\t\t\t).reduce( ( accumulator, [ slug, slugsWithTemplates ] ) => {\n\t\t\t\tconst postsWithTemplates = select( coreStore ).getEntityRecords(\n\t\t\t\t\t'postType',\n\t\t\t\t\tslug,\n\t\t\t\t\t{\n\t\t\t\t\t\t_fields: 'id',\n\t\t\t\t\t\tcontext: 'view',\n\t\t\t\t\t\tslug: slugsWithTemplates,\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t\tif ( postsWithTemplates?.length ) {\n\t\t\t\t\taccumulator[ slug ] = postsWithTemplates;\n\t\t\t\t}\n\t\t\t\treturn accumulator;\n\t\t\t}, {} );\n\t\t\treturn postsToExclude;\n\t\t},\n\t\t[ slugsToExcludePerEntity ]\n\t);\n\tconst entitiesInfo = useSelect(\n\t\t( select ) => {\n\t\t\treturn postTypes?.reduce( ( accumulator, { slug } ) => {\n\t\t\t\tconst existingPosts =\n\t\t\t\t\tpostsToExcludePerEntity?.[ slug ]?.map(\n\t\t\t\t\t\t( { id } ) => id\n\t\t\t\t\t) || [];\n\t\t\t\taccumulator[ slug ] = {\n\t\t\t\t\thasEntities: !! select( coreStore ).getEntityRecords(\n\t\t\t\t\t\t'postType',\n\t\t\t\t\t\tslug,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tper_page: 1,\n\t\t\t\t\t\t\t_fields: 'id',\n\t\t\t\t\t\t\tcontext: 'view',\n\t\t\t\t\t\t\texclude: existingPosts,\n\t\t\t\t\t\t}\n\t\t\t\t\t)?.length,\n\t\t\t\t\texistingPosts,\n\t\t\t\t};\n\t\t\t\treturn accumulator;\n\t\t\t}, {} );\n\t\t},\n\t\t[ postTypes, postsToExcludePerEntity ]\n\t);\n\treturn entitiesInfo;\n};\n\nexport const mapToIHasNameAndId = ( entities, path ) => {\n\treturn ( entities || [] ).map( ( entity ) => ( {\n\t\t...entity,\n\t\tname: decodeEntities( get( entity, path ) ),\n\t} ) );\n};\n"]}
1
+ {"version":3,"sources":["@wordpress/edit-site/src/components/add-new-template/utils.js"],"names":["mapToIHasNameAndId","entities","path","map","entity","name","taxonomyBaseConfig","entityName","getOrderBy","search","getIcon","blockMeta","getTitle","labels","singular_name","getDescription","postTypeBaseConfig","recordNamePath","_icon","startsWith","slice","post","entitiesConfig","postType","templatePrefix","page","taxonomy","category","tag","templateSlug","useExistingTemplates","select","coreStore","getEntityRecords","per_page","useDefaultTemplateTypes","editorStore","__experimentalGetDefaultTemplateTypes","usePublicPostTypes","postTypes","getPostTypes","excludedPostTypes","filter","viewable","slug","includes","usePostTypes","usePostTypePage","usePublicTaxonomies","taxonomies","getTaxonomies","visibility","publicly_queryable","useTaxonomies","specialTaxonomies","useTaxonomyCategory","useTaxonomyTag","useEntitiesInfo","existingTemplates","slugsToExcludePerEntity","reduce","accumulator","_prefix","slugsWithTemplates","_accumulator","existingTemplate","push","substring","length","recordsToExcludePerEntity","Object","entries","postsWithTemplates","_fields","context","entitiesInfo","existingEntitiesIds","id","hasEntities","exclude","useExtraTemplates","entityConfig","onClickMenuItem","defaultTemplateTypes","existingTemplateSlugs","extraTemplates","icon","slugForGeneralTemplate","defaultTemplateType","find","_slug","generalTemplateSlug","hasGeneralTemplate","menuItem","title","description","onClick","template","type","config","postsToExclude"],"mappings":";;;;;;;AAGA;;AAKA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AAdA;AACA;AACA;;AAGA;AACA;AACA;;AASA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,kBAAkB,GAAG,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,CAAvB;AAgDA,SAAOwB,cAAP;AACA,CAhEM","sourcesContent":["/**\n * External dependencies\n */\nimport { get } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { store as editorStore } from '@wordpress/editor';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { useMemo } from '@wordpress/element';\nimport { __, sprintf } from '@wordpress/i18n';\nimport { blockMeta, post } from '@wordpress/icons';\n\n/**\n * @typedef IHasNameAndId\n * @property {string|number} id The entity's id.\n * @property {string} name The entity's name.\n */\n\n/**\n * Helper util to map records to add a `name` prop from a\n * provided path, in order to handle all entities in the same\n * fashion(implementing`IHasNameAndId` interface).\n *\n * @param {Object[]} entities The array of entities.\n * @param {string} path The path to map a `name` property from the entity.\n * @return {IHasNameAndId[]} An array of enitities that now implement the `IHasNameAndId` interface.\n */\nexport const mapToIHasNameAndId = ( entities, path ) => {\n\treturn ( entities || [] ).map( ( entity ) => ( {\n\t\t...entity,\n\t\tname: decodeEntities( get( entity, path ) ),\n\t} ) );\n};\n\n/**\n * @typedef {Object} EntitiesInfo\n * @property {boolean} hasEntities If an entity has available records(posts, terms, etc..).\n * @property {number[]} existingEntitiesIds An array of the existing entities ids.\n */\n\n/**\n * @typedef {Object} EntityConfig\n * @property {string} entityName The entity's name.\n * @property {Function} getOrderBy Getter for an entity's `orderBy` query parameter, given the object\n * {search} as argument.\n * @property {Function} getIcon Getter function for returning an entity's icon for the menu item.\n * @property {Function} getTitle Getter function for returning an entity's title for the menu item.\n * @property {Function} getDescription Getter function for returning an entity's description for the menu item.\n * @property {string} recordNamePath The path to an entity's properties to use as a `name`. If not provided\n * is assumed that `name` property exists.\n * @property {string} templatePrefix The template prefix to create new templates and check against existing\n * templates. For example custom post types need a `single-` prefix to all\n * templates(`single-post-hello`), whereas `pages` don't (`page-hello`).\n * @property {string} templateSlug If this property is provided, it is going to be used for the creation of\n * new templates and the check against existing templates in the place\n * of the actual entity's `slug`. An example is `Tag` templates where the\n * the Tag's taxonomy slug is `post_tag`, but template hierarchy is based\n * on `tag` alias.\n */\n\nconst taxonomyBaseConfig = {\n\tentityName: 'taxonomy',\n\tgetOrderBy: ( { search } ) => ( search ? 'name' : 'count' ),\n\tgetIcon: () => blockMeta,\n\tgetTitle: ( labels ) =>\n\t\tsprintf(\n\t\t\t// translators: %s: Name of the taxonomy e.g: \"Cagegory\".\n\t\t\t__( 'Single taxonomy: %s' ),\n\t\t\tlabels.singular_name\n\t\t),\n\tgetDescription: ( labels ) =>\n\t\tsprintf(\n\t\t\t// translators: %s: Name of the taxonomy e.g: \"Product Categories\".\n\t\t\t__( 'Displays a single taxonomy: %s.' ),\n\t\t\tlabels.singular_name\n\t\t),\n};\nconst postTypeBaseConfig = {\n\tentityName: 'postType',\n\tgetOrderBy: ( { search } ) => ( search ? 'relevance' : 'modified' ),\n\trecordNamePath: 'title.rendered',\n\t// `icon` is the `menu_icon` property of a post type. We\n\t// only handle `dashicons` for now, even if the `menu_icon`\n\t// also supports urls and svg as values.\n\tgetIcon: ( _icon ) =>\n\t\t_icon?.startsWith( 'dashicons-' ) ? _icon.slice( 10 ) : post,\n\tgetTitle: ( labels ) =>\n\t\tsprintf(\n\t\t\t// translators: %s: Name of the post type e.g: \"Post\".\n\t\t\t__( 'Single item: %s' ),\n\t\t\tlabels.singular_name\n\t\t),\n\tgetDescription: ( labels ) =>\n\t\tsprintf(\n\t\t\t// translators: %s: Name of the post type e.g: \"Post\".\n\t\t\t__( 'Displays a single item: %s.' ),\n\t\t\tlabels.singular_name\n\t\t),\n};\nexport const entitiesConfig = {\n\tpostType: {\n\t\t...postTypeBaseConfig,\n\t\ttemplatePrefix: 'single-',\n\t},\n\tpage: { ...postTypeBaseConfig },\n\ttaxonomy: {\n\t\t...taxonomyBaseConfig,\n\t\ttemplatePrefix: 'taxonomy-',\n\t},\n\tcategory: { ...taxonomyBaseConfig },\n\ttag: { ...taxonomyBaseConfig, templateSlug: 'tag' },\n};\n\nexport const useExistingTemplates = () => {\n\treturn useSelect(\n\t\t( select ) =>\n\t\t\tselect( coreStore ).getEntityRecords( 'postType', 'wp_template', {\n\t\t\t\tper_page: -1,\n\t\t\t} ),\n\t\t[]\n\t);\n};\n\nexport const useDefaultTemplateTypes = () => {\n\treturn useSelect(\n\t\t( select ) =>\n\t\t\tselect( editorStore ).__experimentalGetDefaultTemplateTypes(),\n\t\t[]\n\t);\n};\n\nconst usePublicPostTypes = () => {\n\tconst postTypes = useSelect(\n\t\t( select ) => select( coreStore ).getPostTypes( { per_page: -1 } ),\n\t\t[]\n\t);\n\treturn useMemo( () => {\n\t\tconst excludedPostTypes = [ 'attachment' ];\n\t\treturn postTypes?.filter(\n\t\t\t( { viewable, slug } ) =>\n\t\t\t\tviewable && ! excludedPostTypes.includes( slug )\n\t\t);\n\t}, [ postTypes ] );\n};\n\n// `page` post type is a special case in the template hierarchy,\n// so we exclude it from the list of post types and we handle it\n// separately.\nexport const usePostTypes = () => {\n\tconst postTypes = usePublicPostTypes();\n\treturn useMemo( () => {\n\t\treturn postTypes?.filter( ( { slug } ) => slug !== 'page' );\n\t}, [ postTypes ] );\n};\nexport const usePostTypePage = () => {\n\tconst postTypes = usePublicPostTypes();\n\treturn useMemo( () => {\n\t\treturn postTypes?.filter( ( { slug } ) => slug === 'page' );\n\t}, [ postTypes ] );\n};\n\nconst usePublicTaxonomies = () => {\n\tconst taxonomies = useSelect(\n\t\t( select ) => select( coreStore ).getTaxonomies( { per_page: -1 } ),\n\t\t[]\n\t);\n\treturn useMemo( () => {\n\t\treturn taxonomies?.filter(\n\t\t\t( { visibility } ) => visibility?.publicly_queryable\n\t\t);\n\t}, [ taxonomies ] );\n};\n\n/**\n * `category` and `post_tag` are handled specifically in template\n * hierarchy so we need to differentiate them and return the rest,\n * e.g. `category-$slug` and `taxonomy-$taxonomy-$term`.\n */\nexport const useTaxonomies = () => {\n\tconst taxonomies = usePublicTaxonomies();\n\tconst specialTaxonomies = [ 'category', 'post_tag' ];\n\treturn useMemo(\n\t\t() =>\n\t\t\ttaxonomies?.filter(\n\t\t\t\t( { slug } ) => ! specialTaxonomies.includes( slug )\n\t\t\t),\n\t\t[ taxonomies ]\n\t);\n};\n\nexport const useTaxonomyCategory = () => {\n\tconst taxonomies = usePublicTaxonomies();\n\treturn useMemo(\n\t\t() => taxonomies?.filter( ( { slug } ) => slug === 'category' ),\n\t\t[ taxonomies ]\n\t);\n};\nexport const useTaxonomyTag = () => {\n\tconst taxonomies = usePublicTaxonomies();\n\treturn useMemo(\n\t\t() => taxonomies?.filter( ( { slug } ) => slug === 'post_tag' ),\n\t\t[ taxonomies ]\n\t);\n};\n\n/**\n * Helper hook that returns information about an entity having\n * records that we can create a specific template for.\n *\n * For example we can search for `terms` in `taxonomy` entity or\n * `posts` in `postType` entity.\n *\n * First we need to find the existing records with an associated template,\n * to query afterwards for any remaing record, by excluding them.\n *\n * @param {string[]} existingTemplates The existing templates.\n * @param {Object[]} entities The array of entities we need to get extra information.\n * @param {EntityConfig} entityConfig The entity config.\n * @return {Record<string,EntitiesInfo>} An object with the `entities.slug` as `keys` and EntitiesInfo as values.\n */\nconst useEntitiesInfo = (\n\texistingTemplates,\n\tentities,\n\t{ entityName, templatePrefix, templateSlug }\n) => {\n\tconst slugsToExcludePerEntity = useMemo( () => {\n\t\treturn entities?.reduce( ( accumulator, entity ) => {\n\t\t\tlet _prefix = `${ templateSlug || entity.slug }-`;\n\t\t\tif ( templatePrefix ) {\n\t\t\t\t_prefix = templatePrefix + _prefix;\n\t\t\t}\n\t\t\tconst slugsWithTemplates = ( existingTemplates || [] ).reduce(\n\t\t\t\t( _accumulator, existingTemplate ) => {\n\t\t\t\t\tif ( existingTemplate.slug.startsWith( _prefix ) ) {\n\t\t\t\t\t\t_accumulator.push(\n\t\t\t\t\t\t\texistingTemplate.slug.substring( _prefix.length )\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn _accumulator;\n\t\t\t\t},\n\t\t\t\t[]\n\t\t\t);\n\t\t\tif ( slugsWithTemplates.length ) {\n\t\t\t\taccumulator[ entity.slug ] = slugsWithTemplates;\n\t\t\t}\n\t\t\treturn accumulator;\n\t\t}, {} );\n\t}, [ entities, existingTemplates ] );\n\tconst recordsToExcludePerEntity = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! slugsToExcludePerEntity ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\treturn Object.entries( slugsToExcludePerEntity ).reduce(\n\t\t\t\t( accumulator, [ slug, slugsWithTemplates ] ) => {\n\t\t\t\t\tconst postsWithTemplates = select(\n\t\t\t\t\t\tcoreStore\n\t\t\t\t\t).getEntityRecords( entityName, slug, {\n\t\t\t\t\t\t_fields: 'id',\n\t\t\t\t\t\tcontext: 'view',\n\t\t\t\t\t\tslug: slugsWithTemplates,\n\t\t\t\t\t} );\n\t\t\t\t\tif ( postsWithTemplates?.length ) {\n\t\t\t\t\t\taccumulator[ slug ] = postsWithTemplates;\n\t\t\t\t\t}\n\t\t\t\t\treturn accumulator;\n\t\t\t\t},\n\t\t\t\t{}\n\t\t\t);\n\t\t},\n\t\t[ slugsToExcludePerEntity ]\n\t);\n\tconst entitiesInfo = useSelect(\n\t\t( select ) => {\n\t\t\treturn entities?.reduce( ( accumulator, { slug } ) => {\n\t\t\t\tconst existingEntitiesIds =\n\t\t\t\t\trecordsToExcludePerEntity?.[ slug ]?.map(\n\t\t\t\t\t\t( { id } ) => id\n\t\t\t\t\t) || [];\n\t\t\t\taccumulator[ slug ] = {\n\t\t\t\t\thasEntities: !! select( coreStore ).getEntityRecords(\n\t\t\t\t\t\tentityName,\n\t\t\t\t\t\tslug,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tper_page: 1,\n\t\t\t\t\t\t\t_fields: 'id',\n\t\t\t\t\t\t\tcontext: 'view',\n\t\t\t\t\t\t\texclude: existingEntitiesIds,\n\t\t\t\t\t\t}\n\t\t\t\t\t)?.length,\n\t\t\t\t\texistingEntitiesIds,\n\t\t\t\t};\n\t\t\t\treturn accumulator;\n\t\t\t}, {} );\n\t\t},\n\t\t[ entities, recordsToExcludePerEntity ]\n\t);\n\treturn entitiesInfo;\n};\n\nexport const useExtraTemplates = (\n\tentities,\n\tentityConfig,\n\tonClickMenuItem\n) => {\n\tconst existingTemplates = useExistingTemplates();\n\tconst defaultTemplateTypes = useDefaultTemplateTypes();\n\tconst entitiesInfo = useEntitiesInfo(\n\t\texistingTemplates,\n\t\tentities,\n\t\tentityConfig\n\t);\n\tconst existingTemplateSlugs = ( existingTemplates || [] ).map(\n\t\t( { slug } ) => slug\n\t);\n\tconst extraTemplates = ( entities || [] ).reduce(\n\t\t( accumulator, entity ) => {\n\t\t\tconst { slug, labels, icon } = entity;\n\t\t\tconst slugForGeneralTemplate = entityConfig.templateSlug || slug;\n\t\t\t// We need to check if the general template is part of the\n\t\t\t// defaultTemplateTypes. If it is, just use that info and\n\t\t\t// augment it with the specific template functionality.\n\t\t\tconst defaultTemplateType = defaultTemplateTypes?.find(\n\t\t\t\t( { slug: _slug } ) => _slug === slugForGeneralTemplate\n\t\t\t);\n\t\t\tconst generalTemplateSlug =\n\t\t\t\tdefaultTemplateType?.slug ||\n\t\t\t\t`${ entityConfig.templatePrefix }${ slug }`;\n\t\t\tconst hasGeneralTemplate =\n\t\t\t\texistingTemplateSlugs?.includes( generalTemplateSlug );\n\t\t\tconst menuItem = defaultTemplateType\n\t\t\t\t? { ...defaultTemplateType }\n\t\t\t\t: {\n\t\t\t\t\t\tslug: generalTemplateSlug,\n\t\t\t\t\t\ttitle: entityConfig.getTitle( labels ),\n\t\t\t\t\t\tdescription: entityConfig.getDescription( labels ),\n\t\t\t\t\t\ticon: entityConfig.getIcon?.( icon ),\n\t\t\t\t };\n\t\t\tconst hasEntities = entitiesInfo?.[ slug ]?.hasEntities;\n\t\t\t// We have a different template creation flow only if they have entities.\n\t\t\tif ( hasEntities ) {\n\t\t\t\tmenuItem.onClick = ( template ) => {\n\t\t\t\t\tonClickMenuItem( {\n\t\t\t\t\t\ttype: entityConfig.entityName,\n\t\t\t\t\t\tslug,\n\t\t\t\t\t\tconfig: entityConfig,\n\t\t\t\t\t\tlabels,\n\t\t\t\t\t\thasGeneralTemplate,\n\t\t\t\t\t\ttemplate,\n\t\t\t\t\t\tpostsToExclude:\n\t\t\t\t\t\t\tentitiesInfo[ slug ].existingEntitiesIds,\n\t\t\t\t\t} );\n\t\t\t\t};\n\t\t\t}\n\t\t\t// We don't need to add the menu item if there are no\n\t\t\t// entities and the general template exists.\n\t\t\tif ( ! hasGeneralTemplate || hasEntities ) {\n\t\t\t\taccumulator.push( menuItem );\n\t\t\t}\n\t\t\treturn accumulator;\n\t\t},\n\t\t[]\n\t);\n\treturn extraTemplates;\n};\n"]}
@@ -49,10 +49,23 @@ function CodeEditor() {
49
49
  };
50
50
  }, []);
51
51
  const [contentStructure, setContent] = (0, _coreData.useEntityProp)('postType', templateType, 'content');
52
- const [blocks,, onChange] = (0, _coreData.useEntityBlockEditor)('postType', templateType);
53
- const content = contentStructure instanceof Function ? contentStructure({
54
- blocks
55
- }) : contentStructure;
52
+ const [blocks,, onChange] = (0, _coreData.useEntityBlockEditor)('postType', templateType); // Replicates the logic found in getEditedPostContent().
53
+
54
+ let content;
55
+
56
+ if (contentStructure instanceof Function) {
57
+ content = contentStructure({
58
+ blocks
59
+ });
60
+ } else if (blocks) {
61
+ // If we have parsed blocks already, they should be our source of truth.
62
+ // Parsing applies block deprecations and legacy block conversions that
63
+ // unparsed content will not have.
64
+ content = (0, _blocks.__unstableSerializeAndClean)(blocks);
65
+ } else {
66
+ content = contentStructure;
67
+ }
68
+
56
69
  const {
57
70
  switchEditorMode
58
71
  } = (0, _data.useDispatch)(_store.store);