@wordpress/edit-site 4.10.0 → 4.12.1-next.d6164808d3.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 (204) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/build/components/add-new-template/add-custom-template-modal.js +26 -44
  3. package/build/components/add-new-template/add-custom-template-modal.js.map +1 -1
  4. package/build/components/add-new-template/new-template.js +45 -23
  5. package/build/components/add-new-template/new-template.js.map +1 -1
  6. package/build/components/add-new-template/utils.js +493 -242
  7. package/build/components/add-new-template/utils.js.map +1 -1
  8. package/build/components/block-editor/index.js +1 -3
  9. package/build/components/block-editor/index.js.map +1 -1
  10. package/build/components/block-editor/resizable-editor.js +26 -12
  11. package/build/components/block-editor/resizable-editor.js.map +1 -1
  12. package/build/components/global-styles/border-panel.js +3 -3
  13. package/build/components/global-styles/border-panel.js.map +1 -1
  14. package/build/components/global-styles/dimensions-panel.js +280 -20
  15. package/build/components/global-styles/dimensions-panel.js.map +1 -1
  16. package/build/components/global-styles/hooks.js +4 -4
  17. package/build/components/global-styles/hooks.js.map +1 -1
  18. package/build/components/global-styles/screen-color-palette.js +1 -1
  19. package/build/components/global-styles/screen-color-palette.js.map +1 -1
  20. package/build/components/global-styles/screen-colors.js +51 -7
  21. package/build/components/global-styles/screen-colors.js.map +1 -1
  22. package/build/components/global-styles/screen-heading-color.js +157 -0
  23. package/build/components/global-styles/screen-heading-color.js.map +1 -0
  24. package/build/components/global-styles/screen-typography-element.js +4 -0
  25. package/build/components/global-styles/screen-typography-element.js.map +1 -1
  26. package/build/components/global-styles/screen-typography.js +5 -0
  27. package/build/components/global-styles/screen-typography.js.map +1 -1
  28. package/build/components/global-styles/typography-panel.js +82 -14
  29. package/build/components/global-styles/typography-panel.js.map +1 -1
  30. package/build/components/global-styles/typography-utils.js +217 -0
  31. package/build/components/global-styles/typography-utils.js.map +1 -0
  32. package/build/components/global-styles/ui.js +11 -0
  33. package/build/components/global-styles/ui.js.map +1 -1
  34. package/build/components/global-styles/use-global-styles-output.js +183 -52
  35. package/build/components/global-styles/use-global-styles-output.js.map +1 -1
  36. package/build/components/global-styles/utils.js +85 -5
  37. package/build/components/global-styles/utils.js.map +1 -1
  38. package/build/components/header/document-actions/index.js +1 -0
  39. package/build/components/header/document-actions/index.js.map +1 -1
  40. package/build/components/header/index.js +27 -12
  41. package/build/components/header/index.js.map +1 -1
  42. package/build/components/header/mode-switcher/index.js +0 -4
  43. package/build/components/header/mode-switcher/index.js.map +1 -1
  44. package/build/components/header/more-menu/index.js +13 -3
  45. package/build/components/header/more-menu/index.js.map +1 -1
  46. package/build/components/header/undo-redo/redo.js +2 -1
  47. package/build/components/header/undo-redo/redo.js.map +1 -1
  48. package/build/components/list/actions/index.js +1 -1
  49. package/build/components/list/actions/index.js.map +1 -1
  50. package/build/components/save-button/index.js +2 -3
  51. package/build/components/save-button/index.js.map +1 -1
  52. package/build/components/sidebar/default-sidebar.js +11 -1
  53. package/build/components/sidebar/default-sidebar.js.map +1 -1
  54. package/build/components/sidebar/navigation-menu-sidebar/navigation-menu.js +2 -2
  55. package/build/components/sidebar/navigation-menu-sidebar/navigation-menu.js.map +1 -1
  56. package/build/components/sidebar/plugin-sidebar/index.js +11 -1
  57. package/build/components/sidebar/plugin-sidebar/index.js.map +1 -1
  58. package/build/components/sidebar/template-card/template-actions.js +1 -1
  59. package/build/components/sidebar/template-card/template-actions.js.map +1 -1
  60. package/build/components/template-details/edit-template-title.js +1 -1
  61. package/build/components/template-details/edit-template-title.js.map +1 -1
  62. package/build/components/template-details/index.js +19 -9
  63. package/build/components/template-details/index.js.map +1 -1
  64. package/build/components/template-details/template-areas.js +1 -1
  65. package/build/components/template-details/template-areas.js.map +1 -1
  66. package/build/components/template-details/template-part-area-selector.js +47 -0
  67. package/build/components/template-details/template-part-area-selector.js.map +1 -0
  68. package/build/components/template-part-converter/convert-to-template-part.js +4 -1
  69. package/build/components/template-part-converter/convert-to-template-part.js.map +1 -1
  70. package/build/hooks/index.js +2 -0
  71. package/build/hooks/index.js.map +1 -1
  72. package/build/hooks/template-part-edit.js +86 -0
  73. package/build/hooks/template-part-edit.js.map +1 -0
  74. package/build/index.js +1 -1
  75. package/build/index.js.map +1 -1
  76. package/build-module/components/add-new-template/add-custom-template-modal.js +27 -45
  77. package/build-module/components/add-new-template/add-custom-template-modal.js.map +1 -1
  78. package/build-module/components/add-new-template/new-template.js +45 -25
  79. package/build-module/components/add-new-template/new-template.js.map +1 -1
  80. package/build-module/components/add-new-template/utils.js +489 -230
  81. package/build-module/components/add-new-template/utils.js.map +1 -1
  82. package/build-module/components/block-editor/index.js +1 -2
  83. package/build-module/components/block-editor/index.js.map +1 -1
  84. package/build-module/components/block-editor/resizable-editor.js +26 -12
  85. package/build-module/components/block-editor/resizable-editor.js.map +1 -1
  86. package/build-module/components/global-styles/border-panel.js +3 -3
  87. package/build-module/components/global-styles/border-panel.js.map +1 -1
  88. package/build-module/components/global-styles/dimensions-panel.js +278 -22
  89. package/build-module/components/global-styles/dimensions-panel.js.map +1 -1
  90. package/build-module/components/global-styles/hooks.js +4 -4
  91. package/build-module/components/global-styles/hooks.js.map +1 -1
  92. package/build-module/components/global-styles/screen-color-palette.js +1 -1
  93. package/build-module/components/global-styles/screen-color-palette.js.map +1 -1
  94. package/build-module/components/global-styles/screen-colors.js +51 -7
  95. package/build-module/components/global-styles/screen-colors.js.map +1 -1
  96. package/build-module/components/global-styles/screen-heading-color.js +143 -0
  97. package/build-module/components/global-styles/screen-heading-color.js.map +1 -0
  98. package/build-module/components/global-styles/screen-typography-element.js +4 -0
  99. package/build-module/components/global-styles/screen-typography-element.js.map +1 -1
  100. package/build-module/components/global-styles/screen-typography.js +5 -0
  101. package/build-module/components/global-styles/screen-typography.js.map +1 -1
  102. package/build-module/components/global-styles/typography-panel.js +83 -15
  103. package/build-module/components/global-styles/typography-panel.js.map +1 -1
  104. package/build-module/components/global-styles/typography-utils.js +204 -0
  105. package/build-module/components/global-styles/typography-utils.js.map +1 -0
  106. package/build-module/components/global-styles/ui.js +10 -0
  107. package/build-module/components/global-styles/ui.js.map +1 -1
  108. package/build-module/components/global-styles/use-global-styles-output.js +182 -61
  109. package/build-module/components/global-styles/use-global-styles-output.js.map +1 -1
  110. package/build-module/components/global-styles/utils.js +82 -5
  111. package/build-module/components/global-styles/utils.js.map +1 -1
  112. package/build-module/components/header/document-actions/index.js +1 -0
  113. package/build-module/components/header/document-actions/index.js.map +1 -1
  114. package/build-module/components/header/index.js +30 -14
  115. package/build-module/components/header/index.js.map +1 -1
  116. package/build-module/components/header/mode-switcher/index.js +0 -4
  117. package/build-module/components/header/mode-switcher/index.js.map +1 -1
  118. package/build-module/components/header/more-menu/index.js +13 -3
  119. package/build-module/components/header/more-menu/index.js.map +1 -1
  120. package/build-module/components/header/undo-redo/redo.js +3 -2
  121. package/build-module/components/header/undo-redo/redo.js.map +1 -1
  122. package/build-module/components/list/actions/index.js +1 -1
  123. package/build-module/components/list/actions/index.js.map +1 -1
  124. package/build-module/components/save-button/index.js +3 -4
  125. package/build-module/components/save-button/index.js.map +1 -1
  126. package/build-module/components/sidebar/default-sidebar.js +9 -1
  127. package/build-module/components/sidebar/default-sidebar.js.map +1 -1
  128. package/build-module/components/sidebar/navigation-menu-sidebar/navigation-menu.js +3 -3
  129. package/build-module/components/sidebar/navigation-menu-sidebar/navigation-menu.js.map +1 -1
  130. package/build-module/components/sidebar/plugin-sidebar/index.js +9 -1
  131. package/build-module/components/sidebar/plugin-sidebar/index.js.map +1 -1
  132. package/build-module/components/sidebar/template-card/template-actions.js +1 -1
  133. package/build-module/components/sidebar/template-card/template-actions.js.map +1 -1
  134. package/build-module/components/template-details/edit-template-title.js +1 -1
  135. package/build-module/components/template-details/edit-template-title.js.map +1 -1
  136. package/build-module/components/template-details/index.js +19 -10
  137. package/build-module/components/template-details/index.js.map +1 -1
  138. package/build-module/components/template-details/template-areas.js +1 -1
  139. package/build-module/components/template-details/template-areas.js.map +1 -1
  140. package/build-module/components/template-details/template-part-area-selector.js +35 -0
  141. package/build-module/components/template-details/template-part-area-selector.js.map +1 -0
  142. package/build-module/components/template-part-converter/convert-to-template-part.js +3 -1
  143. package/build-module/components/template-part-converter/convert-to-template-part.js.map +1 -1
  144. package/build-module/hooks/index.js +1 -0
  145. package/build-module/hooks/index.js.map +1 -1
  146. package/build-module/hooks/template-part-edit.js +67 -0
  147. package/build-module/hooks/template-part-edit.js.map +1 -0
  148. package/build-module/index.js +1 -1
  149. package/build-module/index.js.map +1 -1
  150. package/build-style/style-rtl.css +40 -33
  151. package/build-style/style.css +40 -33
  152. package/package.json +29 -29
  153. package/src/components/add-new-template/add-custom-template-modal.js +39 -48
  154. package/src/components/add-new-template/new-template.js +50 -58
  155. package/src/components/add-new-template/style.scss +20 -8
  156. package/src/components/add-new-template/utils.js +529 -231
  157. package/src/components/block-editor/index.js +0 -2
  158. package/src/components/block-editor/resizable-editor.js +28 -18
  159. package/src/components/editor/style.scss +1 -0
  160. package/src/components/global-styles/border-panel.js +3 -3
  161. package/src/components/global-styles/dimensions-panel.js +344 -45
  162. package/src/components/global-styles/hooks.js +6 -3
  163. package/src/components/global-styles/screen-color-palette.js +1 -1
  164. package/src/components/global-styles/screen-colors.js +46 -4
  165. package/src/components/global-styles/screen-heading-color.js +201 -0
  166. package/src/components/global-styles/screen-typography-element.js +4 -0
  167. package/src/components/global-styles/screen-typography.js +6 -0
  168. package/src/components/global-styles/style.scss +14 -6
  169. package/src/components/global-styles/test/typography-utils.js +130 -0
  170. package/src/components/global-styles/test/use-global-styles-output.js +143 -4
  171. package/src/components/global-styles/test/utils.js +68 -8
  172. package/src/components/global-styles/typography-panel.js +119 -48
  173. package/src/components/global-styles/typography-utils.js +228 -0
  174. package/src/components/global-styles/ui.js +13 -0
  175. package/src/components/global-styles/use-global-styles-output.js +203 -53
  176. package/src/components/global-styles/utils.js +70 -4
  177. package/src/components/header/document-actions/index.js +3 -0
  178. package/src/components/header/index.js +41 -14
  179. package/src/components/header/mode-switcher/index.js +0 -3
  180. package/src/components/header/more-menu/index.js +7 -2
  181. package/src/components/header/style.scss +5 -3
  182. package/src/components/header/undo-redo/redo.js +6 -2
  183. package/src/components/list/actions/index.js +3 -1
  184. package/src/components/save-button/index.js +10 -13
  185. package/src/components/sidebar/default-sidebar.js +12 -0
  186. package/src/components/sidebar/navigation-menu-sidebar/navigation-menu.js +1 -5
  187. package/src/components/sidebar/plugin-sidebar/index.js +12 -0
  188. package/src/components/sidebar/style.scss +4 -0
  189. package/src/components/sidebar/template-card/template-actions.js +3 -1
  190. package/src/components/template-details/edit-template-title.js +7 -3
  191. package/src/components/template-details/index.js +23 -8
  192. package/src/components/template-details/style.scss +0 -5
  193. package/src/components/template-details/template-areas.js +3 -1
  194. package/src/components/template-details/template-part-area-selector.js +38 -0
  195. package/src/components/template-part-converter/convert-to-template-part.js +3 -1
  196. package/src/hooks/index.js +1 -0
  197. package/src/hooks/template-part-edit.js +82 -0
  198. package/src/index.js +1 -1
  199. package/src/style.scss +0 -1
  200. package/build/components/edit-template-part-menu-button/index.js +0 -90
  201. package/build/components/edit-template-part-menu-button/index.js.map +0 -1
  202. package/build-module/components/edit-template-part-menu-button/index.js +0 -72
  203. package/build-module/components/edit-template-part-menu-button/index.js.map +0 -1
  204. package/src/components/edit-template-part-menu-button/index.js +0 -82
@@ -3,7 +3,9 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.useTaxonomyTag = exports.useTaxonomyCategory = exports.useTaxonomies = exports.usePostTypes = exports.usePostTypePage = exports.useExtraTemplates = exports.useExistingTemplates = exports.useDefaultTemplateTypes = exports.mapToIHasNameAndId = exports.entitiesConfig = void 0;
6
+ exports.mapToIHasNameAndId = void 0;
7
+ exports.useAuthorMenuItem = useAuthorMenuItem;
8
+ exports.useTaxonomiesMenuItems = exports.usePostTypeMenuItems = exports.useExistingTemplates = exports.useDefaultTemplateTypes = void 0;
7
9
 
8
10
  var _lodash = require("lodash");
9
11
 
@@ -55,76 +57,8 @@ const mapToIHasNameAndId = (entities, path) => {
55
57
  * @property {number[]} existingEntitiesIds An array of the existing entities ids.
56
58
  */
57
59
 
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
60
 
79
61
  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
62
 
129
63
  const useExistingTemplates = () => {
130
64
  return (0, _data.useSelect)(select => select(_coreData.store).getEntityRecords('postType', 'wp_template', {
@@ -146,246 +80,317 @@ const usePublicPostTypes = () => {
146
80
  }), []);
147
81
  return (0, _element.useMemo)(() => {
148
82
  const excludedPostTypes = ['attachment'];
149
- return postTypes === null || postTypes === void 0 ? void 0 : postTypes.filter(_ref3 => {
83
+ return postTypes === null || postTypes === void 0 ? void 0 : postTypes.filter(_ref => {
150
84
  let {
151
85
  viewable,
152
86
  slug
153
- } = _ref3;
87
+ } = _ref;
154
88
  return viewable && !excludedPostTypes.includes(slug);
155
89
  });
156
90
  }, [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
91
  };
173
92
 
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
93
  const usePublicTaxonomies = () => {
191
94
  const taxonomies = (0, _data.useSelect)(select => select(_coreData.store).getTaxonomies({
192
95
  per_page: -1
193
96
  }), []);
194
97
  return (0, _element.useMemo)(() => {
195
- return taxonomies === null || taxonomies === void 0 ? void 0 : taxonomies.filter(_ref6 => {
98
+ return taxonomies === null || taxonomies === void 0 ? void 0 : taxonomies.filter(_ref2 => {
196
99
  let {
197
100
  visibility
198
- } = _ref6;
101
+ } = _ref2;
199
102
  return visibility === null || visibility === void 0 ? void 0 : visibility.publicly_queryable;
200
103
  });
201
104
  }, [taxonomies]);
202
105
  };
203
- /**
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`.
207
- */
208
106
 
107
+ const usePostTypeMenuItems = onClickMenuItem => {
108
+ const publicPostTypes = usePublicPostTypes();
109
+ const existingTemplates = useExistingTemplates();
110
+ const defaultTemplateTypes = useDefaultTemplateTypes(); // `page`is a special case in template hierarchy.
209
111
 
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 => {
112
+ const templatePrefixes = (0, _element.useMemo)(() => publicPostTypes === null || publicPostTypes === void 0 ? void 0 : publicPostTypes.reduce((accumulator, _ref3) => {
214
113
  let {
215
114
  slug
216
- } = _ref7;
217
- return !specialTaxonomies.includes(slug);
218
- }), [taxonomies]);
219
- };
115
+ } = _ref3;
116
+ let suffix = slug;
117
+
118
+ if (slug !== 'page') {
119
+ suffix = `single-${suffix}`;
120
+ }
220
121
 
221
- exports.useTaxonomies = useTaxonomies;
122
+ accumulator[slug] = suffix;
123
+ return accumulator;
124
+ }, {}), [publicPostTypes]); // We need to keep track of naming conflicts. If a conflict
125
+ // occurs, we need to add slug.
222
126
 
223
- const useTaxonomyCategory = () => {
224
- const taxonomies = usePublicTaxonomies();
225
- return (0, _element.useMemo)(() => taxonomies === null || taxonomies === void 0 ? void 0 : taxonomies.filter(_ref8 => {
127
+ const postTypeLabels = publicPostTypes === null || publicPostTypes === void 0 ? void 0 : publicPostTypes.reduce((accumulator, _ref4) => {
226
128
  let {
227
- slug
228
- } = _ref8;
229
- return slug === 'category';
230
- }), [taxonomies]);
231
- };
129
+ labels
130
+ } = _ref4;
131
+ const singularName = labels.singular_name.toLowerCase();
132
+ accumulator[singularName] = (accumulator[singularName] || 0) + 1;
133
+ return accumulator;
134
+ }, {});
232
135
 
233
- exports.useTaxonomyCategory = useTaxonomyCategory;
136
+ const needsUniqueIdentifier = (labels, slug) => {
137
+ const singularName = labels.singular_name.toLowerCase();
138
+ return postTypeLabels[singularName] > 1 && singularName !== slug;
139
+ };
234
140
 
235
- const useTaxonomyTag = () => {
236
- const taxonomies = usePublicTaxonomies();
237
- return (0, _element.useMemo)(() => taxonomies === null || taxonomies === void 0 ? void 0 : taxonomies.filter(_ref9 => {
141
+ const postTypesInfo = useEntitiesInfo('postType', templatePrefixes);
142
+ const existingTemplateSlugs = (existingTemplates || []).map(_ref5 => {
238
143
  let {
239
144
  slug
240
- } = _ref9;
241
- return slug === 'post_tag';
242
- }), [taxonomies]);
243
- };
244
- /**
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.
250
- *
251
- * First we need to find the existing records with an associated template,
252
- * to query afterwards for any remaing record, by excluding them.
253
- *
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.
258
- */
145
+ } = _ref5;
146
+ return slug;
147
+ });
148
+ const menuItems = (publicPostTypes || []).reduce((accumulator, postType) => {
149
+ var _postTypesInfo$slug;
259
150
 
151
+ const {
152
+ slug,
153
+ labels,
154
+ icon
155
+ } = postType; // We need to check if the general template is part of the
156
+ // defaultTemplateTypes. If it is, just use that info and
157
+ // augment it with the specific template functionality.
260
158
 
261
- exports.useTaxonomyTag = useTaxonomyTag;
159
+ const generalTemplateSlug = templatePrefixes[slug];
160
+ const defaultTemplateType = defaultTemplateTypes === null || defaultTemplateTypes === void 0 ? void 0 : defaultTemplateTypes.find(_ref6 => {
161
+ let {
162
+ slug: _slug
163
+ } = _ref6;
164
+ return _slug === generalTemplateSlug;
165
+ });
166
+ const hasGeneralTemplate = existingTemplateSlugs === null || existingTemplateSlugs === void 0 ? void 0 : existingTemplateSlugs.includes(generalTemplateSlug);
262
167
 
263
- const useEntitiesInfo = (existingTemplates, entities, _ref10) => {
264
- let {
265
- entityName,
266
- templatePrefix,
267
- templateSlug
268
- } = _ref10;
269
- const slugsToExcludePerEntity = (0, _element.useMemo)(() => {
270
- return entities === null || entities === void 0 ? void 0 : entities.reduce((accumulator, entity) => {
271
- let _prefix = `${templateSlug || entity.slug}-`;
168
+ const _needsUniqueIdentifier = needsUniqueIdentifier(labels, slug);
272
169
 
273
- if (templatePrefix) {
274
- _prefix = templatePrefix + _prefix;
275
- }
170
+ let menuItemTitle = (0, _i18n.sprintf)( // translators: %s: Name of the post type e.g: "Post".
171
+ (0, _i18n.__)('Single item: %s'), labels.singular_name);
276
172
 
277
- const slugsWithTemplates = (existingTemplates || []).reduce((_accumulator, existingTemplate) => {
278
- if (existingTemplate.slug.startsWith(_prefix)) {
279
- _accumulator.push(existingTemplate.slug.substring(_prefix.length));
280
- }
173
+ if (_needsUniqueIdentifier) {
174
+ menuItemTitle = (0, _i18n.sprintf)( // translators: %1s: Name of the post type e.g: "Post"; %2s: Slug of the post type e.g: "book".
175
+ (0, _i18n.__)('Single item: %1$s (%2$s)'), labels.singular_name, slug);
176
+ }
281
177
 
282
- return _accumulator;
283
- }, []);
178
+ const menuItem = defaultTemplateType ? { ...defaultTemplateType,
179
+ templatePrefix: templatePrefixes[slug]
180
+ } : {
181
+ slug: generalTemplateSlug,
182
+ title: menuItemTitle,
183
+ description: (0, _i18n.sprintf)( // translators: %s: Name of the post type e.g: "Post".
184
+ (0, _i18n.__)('Displays a single item: %s.'), labels.singular_name),
185
+ // `icon` is the `menu_icon` property of a post type. We
186
+ // only handle `dashicons` for now, even if the `menu_icon`
187
+ // also supports urls and svg as values.
188
+ icon: icon !== null && icon !== void 0 && icon.startsWith('dashicons-') ? icon.slice(10) : _icons.post,
189
+ templatePrefix: templatePrefixes[slug]
190
+ };
191
+ const hasEntities = postTypesInfo === null || postTypesInfo === void 0 ? void 0 : (_postTypesInfo$slug = postTypesInfo[slug]) === null || _postTypesInfo$slug === void 0 ? void 0 : _postTypesInfo$slug.hasEntities; // We have a different template creation flow only if they have entities.
284
192
 
285
- if (slugsWithTemplates.length) {
286
- accumulator[entity.slug] = slugsWithTemplates;
287
- }
193
+ if (hasEntities) {
194
+ menuItem.onClick = template => {
195
+ onClickMenuItem({
196
+ type: 'postType',
197
+ slug,
198
+ config: {
199
+ recordNamePath: 'title.rendered',
200
+ queryArgs: _ref7 => {
201
+ let {
202
+ search
203
+ } = _ref7;
204
+ return {
205
+ _fields: 'id,title,slug,link',
206
+ orderBy: search ? 'relevance' : 'modified',
207
+ exclude: postTypesInfo[slug].existingEntitiesIds
208
+ };
209
+ },
210
+ getSpecificTemplate: suggestion => {
211
+ let title = (0, _i18n.sprintf)( // translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the singular name of a post type and %2$s is the name of the post, e.g. "Page: Hello".
212
+ (0, _i18n.__)('%1$s: %2$s'), labels.singular_name, suggestion.name);
213
+ const description = (0, _i18n.sprintf)( // translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Page: Hello"
214
+ (0, _i18n.__)('Template for %1$s'), title);
215
+
216
+ if (_needsUniqueIdentifier) {
217
+ title = (0, _i18n.sprintf)( // translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the template title and %2$s is the slug of the post type, e.g. "Project: Hello (project_type)"
218
+ (0, _i18n.__)('%1$s (%2$s)'), title, slug);
219
+ }
220
+
221
+ return {
222
+ title,
223
+ description,
224
+ slug: `${templatePrefixes[slug]}-${suggestion.slug}`,
225
+ templatePrefix: templatePrefixes[slug]
226
+ };
227
+ }
228
+ },
229
+ labels,
230
+ hasGeneralTemplate,
231
+ template
232
+ });
233
+ };
234
+ } // We don't need to add the menu item if there are no
235
+ // entities and the general template exists.
288
236
 
289
- return accumulator;
290
- }, {});
291
- }, [entities, existingTemplates]);
292
- const recordsToExcludePerEntity = (0, _data.useSelect)(select => {
293
- if (!slugsToExcludePerEntity) {
294
- return;
237
+
238
+ if (!hasGeneralTemplate || hasEntities) {
239
+ accumulator.push(menuItem);
295
240
  }
296
241
 
297
- return Object.entries(slugsToExcludePerEntity).reduce((accumulator, _ref11) => {
298
- let [slug, slugsWithTemplates] = _ref11;
299
- const postsWithTemplates = select(_coreData.store).getEntityRecords(entityName, slug, {
300
- _fields: 'id',
301
- context: 'view',
302
- slug: slugsWithTemplates
303
- });
242
+ return accumulator;
243
+ }, []); // Split menu items into two groups: one for the default post types
244
+ // and one for the rest.
304
245
 
305
- if (postsWithTemplates !== null && postsWithTemplates !== void 0 && postsWithTemplates.length) {
306
- accumulator[slug] = postsWithTemplates;
307
- }
246
+ const postTypesMenuItems = (0, _element.useMemo)(() => menuItems.reduce((accumulator, postType) => {
247
+ const {
248
+ slug
249
+ } = postType;
250
+ let key = 'postTypesMenuItems';
308
251
 
309
- return accumulator;
310
- }, {});
311
- }, [slugsToExcludePerEntity]);
312
- const entitiesInfo = (0, _data.useSelect)(select => {
313
- return entities === null || entities === void 0 ? void 0 : entities.reduce((accumulator, _ref12) => {
314
- var _recordsToExcludePerE, _select$getEntityReco;
252
+ if (slug === 'page') {
253
+ key = 'defaultPostTypesMenuItems';
254
+ }
315
255
 
316
- let {
317
- slug
318
- } = _ref12;
319
- const existingEntitiesIds = (recordsToExcludePerEntity === null || recordsToExcludePerEntity === void 0 ? void 0 : (_recordsToExcludePerE = recordsToExcludePerEntity[slug]) === null || _recordsToExcludePerE === void 0 ? void 0 : _recordsToExcludePerE.map(_ref13 => {
320
- let {
321
- id
322
- } = _ref13;
323
- return id;
324
- })) || [];
325
- accumulator[slug] = {
326
- hasEntities: !!((_select$getEntityReco = select(_coreData.store).getEntityRecords(entityName, slug, {
327
- per_page: 1,
328
- _fields: 'id',
329
- context: 'view',
330
- exclude: existingEntitiesIds
331
- })) !== null && _select$getEntityReco !== void 0 && _select$getEntityReco.length),
332
- existingEntitiesIds
333
- };
334
- return accumulator;
335
- }, {});
336
- }, [entities, recordsToExcludePerEntity]);
337
- return entitiesInfo;
256
+ accumulator[key].push(postType);
257
+ return accumulator;
258
+ }, {
259
+ defaultPostTypesMenuItems: [],
260
+ postTypesMenuItems: []
261
+ }), [menuItems]);
262
+ return postTypesMenuItems;
338
263
  };
339
264
 
340
- const useExtraTemplates = (entities, entityConfig, onClickMenuItem) => {
265
+ exports.usePostTypeMenuItems = usePostTypeMenuItems;
266
+
267
+ const useTaxonomiesMenuItems = onClickMenuItem => {
268
+ const publicTaxonomies = usePublicTaxonomies();
341
269
  const existingTemplates = useExistingTemplates();
342
- const defaultTemplateTypes = useDefaultTemplateTypes();
343
- const entitiesInfo = useEntitiesInfo(existingTemplates, entities, entityConfig);
344
- const existingTemplateSlugs = (existingTemplates || []).map(_ref14 => {
270
+ const defaultTemplateTypes = useDefaultTemplateTypes(); // `category` and `post_tag` are special cases in template hierarchy.
271
+
272
+ const templatePrefixes = (0, _element.useMemo)(() => publicTaxonomies === null || publicTaxonomies === void 0 ? void 0 : publicTaxonomies.reduce((accumulator, _ref8) => {
345
273
  let {
346
274
  slug
347
- } = _ref14;
275
+ } = _ref8;
276
+ let suffix = slug;
277
+
278
+ if (!['category', 'post_tag'].includes(slug)) {
279
+ suffix = `taxonomy-${suffix}`;
280
+ }
281
+
282
+ if (slug === 'post_tag') {
283
+ suffix = `tag`;
284
+ }
285
+
286
+ accumulator[slug] = suffix;
287
+ return accumulator;
288
+ }, {}), [publicTaxonomies]); // We need to keep track of naming conflicts. If a conflict
289
+ // occurs, we need to add slug.
290
+
291
+ const taxonomyLabels = publicTaxonomies === null || publicTaxonomies === void 0 ? void 0 : publicTaxonomies.reduce((accumulator, _ref9) => {
292
+ let {
293
+ labels
294
+ } = _ref9;
295
+ const singularName = labels.singular_name.toLowerCase();
296
+ accumulator[singularName] = (accumulator[singularName] || 0) + 1;
297
+ return accumulator;
298
+ }, {});
299
+
300
+ const needsUniqueIdentifier = (labels, slug) => {
301
+ if (['category', 'post_tag'].includes(slug)) {
302
+ return false;
303
+ }
304
+
305
+ const singularName = labels.singular_name.toLowerCase();
306
+ return taxonomyLabels[singularName] > 1 && singularName !== slug;
307
+ };
308
+
309
+ const taxonomiesInfo = useEntitiesInfo('taxonomy', templatePrefixes);
310
+ const existingTemplateSlugs = (existingTemplates || []).map(_ref10 => {
311
+ let {
312
+ slug
313
+ } = _ref10;
348
314
  return slug;
349
315
  });
350
- const extraTemplates = (entities || []).reduce((accumulator, entity) => {
351
- var _entityConfig$getIcon, _entitiesInfo$slug;
316
+ const menuItems = (publicTaxonomies || []).reduce((accumulator, taxonomy) => {
317
+ var _taxonomiesInfo$slug;
352
318
 
353
319
  const {
354
320
  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
321
+ labels
322
+ } = taxonomy; // We need to check if the general template is part of the
359
323
  // defaultTemplateTypes. If it is, just use that info and
360
324
  // augment it with the specific template functionality.
361
325
 
362
- const defaultTemplateType = defaultTemplateTypes === null || defaultTemplateTypes === void 0 ? void 0 : defaultTemplateTypes.find(_ref15 => {
326
+ const generalTemplateSlug = templatePrefixes[slug];
327
+ const defaultTemplateType = defaultTemplateTypes === null || defaultTemplateTypes === void 0 ? void 0 : defaultTemplateTypes.find(_ref11 => {
363
328
  let {
364
329
  slug: _slug
365
- } = _ref15;
366
- return _slug === slugForGeneralTemplate;
330
+ } = _ref11;
331
+ return _slug === generalTemplateSlug;
367
332
  });
368
- const generalTemplateSlug = (defaultTemplateType === null || defaultTemplateType === void 0 ? void 0 : defaultTemplateType.slug) || `${entityConfig.templatePrefix}${slug}`;
369
333
  const hasGeneralTemplate = existingTemplateSlugs === null || existingTemplateSlugs === void 0 ? void 0 : existingTemplateSlugs.includes(generalTemplateSlug);
370
- const menuItem = defaultTemplateType ? { ...defaultTemplateType
334
+
335
+ const _needsUniqueIdentifier = needsUniqueIdentifier(labels, slug);
336
+
337
+ let menuItemTitle = labels.singular_name;
338
+
339
+ if (_needsUniqueIdentifier) {
340
+ menuItemTitle = (0, _i18n.sprintf)( // translators: %1s: Name of the taxonomy e.g: "Category"; %2s: Slug of the taxonomy e.g: "product_cat".
341
+ (0, _i18n.__)('%1$s (%2$s)'), labels.singular_name, slug);
342
+ }
343
+
344
+ const menuItem = defaultTemplateType ? { ...defaultTemplateType,
345
+ templatePrefix: templatePrefixes[slug]
371
346
  } : {
372
347
  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)
348
+ title: menuItemTitle,
349
+ description: (0, _i18n.sprintf)( // translators: %s: Name of the taxonomy e.g: "Product Categories".
350
+ (0, _i18n.__)('Displays taxonomy: %s.'), labels.singular_name),
351
+ icon: _icons.blockMeta,
352
+ templatePrefix: templatePrefixes[slug]
376
353
  };
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.
354
+ const hasEntities = taxonomiesInfo === null || taxonomiesInfo === void 0 ? void 0 : (_taxonomiesInfo$slug = taxonomiesInfo[slug]) === null || _taxonomiesInfo$slug === void 0 ? void 0 : _taxonomiesInfo$slug.hasEntities; // We have a different template creation flow only if they have entities.
378
355
 
379
356
  if (hasEntities) {
380
357
  menuItem.onClick = template => {
381
358
  onClickMenuItem({
382
- type: entityConfig.entityName,
359
+ type: 'taxonomy',
383
360
  slug,
384
- config: entityConfig,
361
+ config: {
362
+ queryArgs: _ref12 => {
363
+ let {
364
+ search
365
+ } = _ref12;
366
+ return {
367
+ _fields: 'id,name,slug,link',
368
+ orderBy: search ? 'name' : 'count',
369
+ exclude: taxonomiesInfo[slug].existingEntitiesIds
370
+ };
371
+ },
372
+ getSpecificTemplate: suggestion => {
373
+ let title = (0, _i18n.sprintf)( // translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the singular name of a taxonomy and %2$s is the name of the term, e.g. "Category: shoes".
374
+ (0, _i18n.__)('%1$s: %2$s'), labels.singular_name, suggestion.name);
375
+ const description = (0, _i18n.sprintf)( // translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Category: shoes"
376
+ (0, _i18n.__)('Template for %1$s'), title);
377
+
378
+ if (_needsUniqueIdentifier) {
379
+ title = (0, _i18n.sprintf)( // translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the template title and %2$s is the slug of the taxonomy, e.g. "Category: shoes (product_tag)"
380
+ (0, _i18n.__)('%1$s (%2$s)'), title, slug);
381
+ }
382
+
383
+ return {
384
+ title,
385
+ description,
386
+ slug: `${templatePrefixes[slug]}-${suggestion.slug}`,
387
+ templatePrefix: templatePrefixes[slug]
388
+ };
389
+ }
390
+ },
385
391
  labels,
386
392
  hasGeneralTemplate,
387
- template,
388
- postsToExclude: entitiesInfo[slug].existingEntitiesIds
393
+ template
389
394
  });
390
395
  };
391
396
  } // We don't need to add the menu item if there are no
@@ -397,9 +402,255 @@ const useExtraTemplates = (entities, entityConfig, onClickMenuItem) => {
397
402
  }
398
403
 
399
404
  return accumulator;
400
- }, []);
401
- return extraTemplates;
405
+ }, []); // Split menu items into two groups: one for the default taxonomies
406
+ // and one for the rest.
407
+
408
+ const taxonomiesMenuItems = (0, _element.useMemo)(() => menuItems.reduce((accumulator, taxonomy) => {
409
+ const {
410
+ slug
411
+ } = taxonomy;
412
+ let key = 'taxonomiesMenuItems';
413
+
414
+ if (['category', 'tag'].includes(slug)) {
415
+ key = 'defaultTaxonomiesMenuItems';
416
+ }
417
+
418
+ accumulator[key].push(taxonomy);
419
+ return accumulator;
420
+ }, {
421
+ defaultTaxonomiesMenuItems: [],
422
+ taxonomiesMenuItems: []
423
+ }), [menuItems]);
424
+ return taxonomiesMenuItems;
402
425
  };
403
426
 
404
- exports.useExtraTemplates = useExtraTemplates;
427
+ exports.useTaxonomiesMenuItems = useTaxonomiesMenuItems;
428
+
429
+ function useAuthorNeedsUniqueIndentifier() {
430
+ const authors = (0, _data.useSelect)(select => select(_coreData.store).getUsers({
431
+ who: 'authors',
432
+ per_page: -1
433
+ }), []);
434
+ const authorsCountByName = (0, _element.useMemo)(() => {
435
+ return (authors || []).reduce((authorsCount, _ref13) => {
436
+ let {
437
+ name
438
+ } = _ref13;
439
+ authorsCount[name] = (authorsCount[name] || 0) + 1;
440
+ return authorsCount;
441
+ }, {});
442
+ }, [authors]);
443
+ return (0, _element.useCallback)(name => {
444
+ return authorsCountByName[name] > 1;
445
+ }, [authorsCountByName]);
446
+ }
447
+
448
+ const USE_AUTHOR_MENU_ITEM_TEMPLATE_PREFIX = {
449
+ user: 'author'
450
+ };
451
+ const USE_AUTHOR_MENU_ITEM_QUERY_PARAMETERS = {
452
+ user: {
453
+ who: 'authors'
454
+ }
455
+ };
456
+
457
+ function useAuthorMenuItem(onClickMenuItem) {
458
+ var _authorInfo$user, _authorInfo$user2;
459
+
460
+ const existingTemplates = useExistingTemplates();
461
+ const defaultTemplateTypes = useDefaultTemplateTypes();
462
+ const authorInfo = useEntitiesInfo('root', USE_AUTHOR_MENU_ITEM_TEMPLATE_PREFIX, USE_AUTHOR_MENU_ITEM_QUERY_PARAMETERS);
463
+ const authorNeedsUniqueId = useAuthorNeedsUniqueIndentifier();
464
+ let authorMenuItem = defaultTemplateTypes === null || defaultTemplateTypes === void 0 ? void 0 : defaultTemplateTypes.find(_ref14 => {
465
+ let {
466
+ slug
467
+ } = _ref14;
468
+ return slug === 'author';
469
+ });
470
+
471
+ if (!authorMenuItem) {
472
+ authorMenuItem = {
473
+ description: (0, _i18n.__)('Displays latest posts written by a single author.'),
474
+ slug: 'author',
475
+ title: 'Author'
476
+ };
477
+ }
478
+
479
+ const hasGeneralTemplate = !!(existingTemplates !== null && existingTemplates !== void 0 && existingTemplates.find(_ref15 => {
480
+ let {
481
+ slug
482
+ } = _ref15;
483
+ return slug === 'author';
484
+ }));
485
+
486
+ if ((_authorInfo$user = authorInfo.user) !== null && _authorInfo$user !== void 0 && _authorInfo$user.hasEntities) {
487
+ authorMenuItem = { ...authorMenuItem,
488
+ templatePrefix: 'author'
489
+ };
490
+
491
+ authorMenuItem.onClick = template => {
492
+ onClickMenuItem({
493
+ type: 'root',
494
+ slug: 'user',
495
+ config: {
496
+ queryArgs: _ref16 => {
497
+ let {
498
+ search
499
+ } = _ref16;
500
+ return {
501
+ _fields: 'id,name,slug,link',
502
+ orderBy: search ? 'name' : 'registered_date',
503
+ exclude: authorInfo.user.existingEntitiesIds,
504
+ who: 'authors'
505
+ };
506
+ },
507
+ getSpecificTemplate: suggestion => {
508
+ const needsUniqueId = authorNeedsUniqueId(suggestion.name);
509
+ const title = needsUniqueId ? (0, _i18n.sprintf)( // translators: %1$s: Represents the name of an author e.g: "Jorge", %2$s: Represents the slug of an author e.g: "author-jorge-slug".
510
+ (0, _i18n.__)('Author: %1$s (%2$s)'), suggestion.name, suggestion.slug) : (0, _i18n.sprintf)( // translators: %s: Represents the name of an author e.g: "Jorge".
511
+ (0, _i18n.__)('Author: %s'), suggestion.name);
512
+ const description = (0, _i18n.sprintf)( // translators: %s: Represents the name of an author e.g: "Jorge".
513
+ (0, _i18n.__)('Template for Author: %s'), suggestion.name);
514
+ return {
515
+ title,
516
+ description,
517
+ slug: `author-${suggestion.slug}`,
518
+ templatePrefix: 'author'
519
+ };
520
+ }
521
+ },
522
+ labels: {
523
+ singular_name: (0, _i18n.__)('Author'),
524
+ search_items: (0, _i18n.__)('Search Authors'),
525
+ not_found: (0, _i18n.__)('No authors found.'),
526
+ all_items: (0, _i18n.__)('All Authors')
527
+ },
528
+ hasGeneralTemplate,
529
+ template
530
+ });
531
+ };
532
+ }
533
+
534
+ if (!hasGeneralTemplate || (_authorInfo$user2 = authorInfo.user) !== null && _authorInfo$user2 !== void 0 && _authorInfo$user2.hasEntities) {
535
+ return authorMenuItem;
536
+ }
537
+ }
538
+ /**
539
+ * Helper hook that filters all the existing templates by the given
540
+ * object with the entity's slug as key and the template prefix as value.
541
+ *
542
+ * Example:
543
+ * `existingTemplates` is: [ { slug: 'tag-apple' }, { slug: 'page-about' }, { slug: 'tag' } ]
544
+ * `templatePrefixes` is: { post_tag: 'tag' }
545
+ * It will return: { post_tag: ['apple'] }
546
+ *
547
+ * Note: We append the `-` to the given template prefix in this function for our checks.
548
+ *
549
+ * @param {Record<string,string>} templatePrefixes An object with the entity's slug as key and the template prefix as value.
550
+ * @return {Record<string,string[]>} An object with the entity's slug as key and an array with the existing template slugs as value.
551
+ */
552
+
553
+
554
+ const useExistingTemplateSlugs = templatePrefixes => {
555
+ const existingTemplates = useExistingTemplates();
556
+ const existingSlugs = (0, _element.useMemo)(() => {
557
+ return Object.entries(templatePrefixes || {}).reduce((accumulator, _ref17) => {
558
+ let [slug, prefix] = _ref17;
559
+ const slugsWithTemplates = (existingTemplates || []).reduce((_accumulator, existingTemplate) => {
560
+ const _prefix = `${prefix}-`;
561
+
562
+ if (existingTemplate.slug.startsWith(_prefix)) {
563
+ _accumulator.push(existingTemplate.slug.substring(_prefix.length));
564
+ }
565
+
566
+ return _accumulator;
567
+ }, []);
568
+
569
+ if (slugsWithTemplates.length) {
570
+ accumulator[slug] = slugsWithTemplates;
571
+ }
572
+
573
+ return accumulator;
574
+ }, {});
575
+ }, [templatePrefixes, existingTemplates]);
576
+ return existingSlugs;
577
+ };
578
+ /**
579
+ * Helper hook that finds the existing records with an associated template,
580
+ * as they need to be excluded from the template suggestions.
581
+ *
582
+ * @param {string} entityName The entity's name.
583
+ * @param {Record<string,string>} templatePrefixes An object with the entity's slug as key and the template prefix as value.
584
+ * @param {Record<string,Object>} additionalQueryParameters An object with the entity's slug as key and additional query parameters as value.
585
+ * @return {Record<string,EntitiesInfo>} An object with the entity's slug as key and the existing records as value.
586
+ */
587
+
588
+
589
+ const useTemplatesToExclude = function (entityName, templatePrefixes) {
590
+ let additionalQueryParameters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
591
+ const slugsToExcludePerEntity = useExistingTemplateSlugs(templatePrefixes);
592
+ const recordsToExcludePerEntity = (0, _data.useSelect)(select => {
593
+ return Object.entries(slugsToExcludePerEntity || {}).reduce((accumulator, _ref18) => {
594
+ let [slug, slugsWithTemplates] = _ref18;
595
+ const entitiesWithTemplates = select(_coreData.store).getEntityRecords(entityName, slug, {
596
+ _fields: 'id',
597
+ context: 'view',
598
+ slug: slugsWithTemplates,
599
+ ...additionalQueryParameters[slug]
600
+ });
601
+
602
+ if (entitiesWithTemplates !== null && entitiesWithTemplates !== void 0 && entitiesWithTemplates.length) {
603
+ accumulator[slug] = entitiesWithTemplates;
604
+ }
605
+
606
+ return accumulator;
607
+ }, {});
608
+ }, [slugsToExcludePerEntity]);
609
+ return recordsToExcludePerEntity;
610
+ };
611
+ /**
612
+ * Helper hook that returns information about an entity having
613
+ * records that we can create a specific template for.
614
+ *
615
+ * For example we can search for `terms` in `taxonomy` entity or
616
+ * `posts` in `postType` entity.
617
+ *
618
+ * First we need to find the existing records with an associated template,
619
+ * to query afterwards for any remaining record, by excluding them.
620
+ *
621
+ * @param {string} entityName The entity's name.
622
+ * @param {Record<string,string>} templatePrefixes An object with the entity's slug as key and the template prefix as value.
623
+ * @param {Record<string,Object>} additionalQueryParameters An object with the entity's slug as key and additional query parameters as value.
624
+ * @return {Record<string,EntitiesInfo>} An object with the entity's slug as key and the EntitiesInfo as value.
625
+ */
626
+
627
+
628
+ const useEntitiesInfo = function (entityName, templatePrefixes) {
629
+ let additionalQueryParameters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
630
+ const recordsToExcludePerEntity = useTemplatesToExclude(entityName, templatePrefixes, additionalQueryParameters);
631
+ const entitiesInfo = (0, _data.useSelect)(select => {
632
+ return Object.keys(templatePrefixes || {}).reduce((accumulator, slug) => {
633
+ var _recordsToExcludePerE, _select$getEntityReco;
634
+
635
+ const existingEntitiesIds = (recordsToExcludePerEntity === null || recordsToExcludePerEntity === void 0 ? void 0 : (_recordsToExcludePerE = recordsToExcludePerEntity[slug]) === null || _recordsToExcludePerE === void 0 ? void 0 : _recordsToExcludePerE.map(_ref19 => {
636
+ let {
637
+ id
638
+ } = _ref19;
639
+ return id;
640
+ })) || [];
641
+ accumulator[slug] = {
642
+ hasEntities: !!((_select$getEntityReco = select(_coreData.store).getEntityRecords(entityName, slug, {
643
+ per_page: 1,
644
+ _fields: 'id',
645
+ context: 'view',
646
+ exclude: existingEntitiesIds,
647
+ ...additionalQueryParameters[slug]
648
+ })) !== null && _select$getEntityReco !== void 0 && _select$getEntityReco.length),
649
+ existingEntitiesIds
650
+ };
651
+ return accumulator;
652
+ }, {});
653
+ }, [templatePrefixes, recordsToExcludePerEntity]);
654
+ return entitiesInfo;
655
+ };
405
656
  //# sourceMappingURL=utils.js.map