@wordpress/edit-site 4.8.0 → 4.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. package/CHANGELOG.md +6 -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 +204 -0
  5. package/build/components/add-new-template/add-custom-template-modal.js.map +1 -0
  6. package/build/components/add-new-template/new-template.js +91 -33
  7. package/build/components/add-new-template/new-template.js.map +1 -1
  8. package/build/components/add-new-template/utils.js +532 -0
  9. package/build/components/add-new-template/utils.js.map +1 -0
  10. package/build/components/block-editor/index.js +1 -3
  11. package/build/components/block-editor/index.js.map +1 -1
  12. package/build/components/code-editor/index.js +17 -4
  13. package/build/components/code-editor/index.js.map +1 -1
  14. package/build/components/editor/index.js +16 -0
  15. package/build/components/editor/index.js.map +1 -1
  16. package/build/components/error-boundary/index.js +6 -0
  17. package/build/components/error-boundary/index.js.map +1 -1
  18. package/build/components/global-styles/context-menu.js +6 -3
  19. package/build/components/global-styles/context-menu.js.map +1 -1
  20. package/build/components/global-styles/dimensions-panel.js +185 -19
  21. package/build/components/global-styles/dimensions-panel.js.map +1 -1
  22. package/build/components/global-styles/global-styles-provider.js +4 -2
  23. package/build/components/global-styles/global-styles-provider.js.map +1 -1
  24. package/build/components/global-styles/hooks.js +11 -2
  25. package/build/components/global-styles/hooks.js.map +1 -1
  26. package/build/components/global-styles/palette.js +2 -1
  27. package/build/components/global-styles/palette.js.map +1 -1
  28. package/build/components/global-styles/screen-block-list.js +4 -1
  29. package/build/components/global-styles/screen-block-list.js.map +1 -1
  30. package/build/components/global-styles/screen-button-color.js +80 -0
  31. package/build/components/global-styles/screen-button-color.js.map +1 -0
  32. package/build/components/global-styles/screen-color-palette.js +13 -17
  33. package/build/components/global-styles/screen-color-palette.js.map +1 -1
  34. package/build/components/global-styles/screen-colors.js +56 -8
  35. package/build/components/global-styles/screen-colors.js.map +1 -1
  36. package/build/components/global-styles/screen-link-color.js +48 -14
  37. package/build/components/global-styles/screen-link-color.js.map +1 -1
  38. package/build/components/global-styles/screen-root.js +4 -2
  39. package/build/components/global-styles/screen-root.js.map +1 -1
  40. package/build/components/global-styles/screen-typography-element.js +4 -0
  41. package/build/components/global-styles/screen-typography-element.js.map +1 -1
  42. package/build/components/global-styles/screen-typography.js +9 -1
  43. package/build/components/global-styles/screen-typography.js.map +1 -1
  44. package/build/components/global-styles/ui.js +11 -0
  45. package/build/components/global-styles/ui.js.map +1 -1
  46. package/build/components/global-styles/use-global-styles-output.js +282 -35
  47. package/build/components/global-styles/use-global-styles-output.js.map +1 -1
  48. package/build/components/global-styles/utils.js +35 -2
  49. package/build/components/global-styles/utils.js.map +1 -1
  50. package/build/components/header/index.js +29 -10
  51. package/build/components/header/index.js.map +1 -1
  52. package/build/components/header/more-menu/site-export.js +4 -1
  53. package/build/components/header/more-menu/site-export.js.map +1 -1
  54. package/build/components/header/undo-redo/redo.js +13 -4
  55. package/build/components/header/undo-redo/redo.js.map +1 -1
  56. package/build/components/header/undo-redo/undo.js +13 -4
  57. package/build/components/header/undo-redo/undo.js.map +1 -1
  58. package/build/components/keyboard-shortcut-help-modal/config.js +17 -0
  59. package/build/components/keyboard-shortcut-help-modal/config.js.map +1 -1
  60. package/build/components/keyboard-shortcut-help-modal/index.js +1 -3
  61. package/build/components/keyboard-shortcut-help-modal/index.js.map +1 -1
  62. package/build/components/sidebar/template-card/index.js +19 -7
  63. package/build/components/sidebar/template-card/index.js.map +1 -1
  64. package/build/components/sidebar/template-card/template-actions.js +64 -0
  65. package/build/components/sidebar/template-card/template-actions.js.map +1 -0
  66. package/build/components/template-details/edit-template-title.js +11 -3
  67. package/build/components/template-details/edit-template-title.js.map +1 -1
  68. package/build/components/template-details/index.js +1 -20
  69. package/build/components/template-details/index.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/store/selectors.js +4 -1
  75. package/build/store/selectors.js.map +1 -1
  76. package/build-module/components/add-new-template/add-custom-generic-template-modal.js +77 -0
  77. package/build-module/components/add-new-template/add-custom-generic-template-modal.js.map +1 -0
  78. package/build-module/components/add-new-template/add-custom-template-modal.js +189 -0
  79. package/build-module/components/add-new-template/add-custom-template-modal.js.map +1 -0
  80. package/build-module/components/add-new-template/new-template.js +90 -34
  81. package/build-module/components/add-new-template/new-template.js.map +1 -1
  82. package/build-module/components/add-new-template/utils.js +503 -0
  83. package/build-module/components/add-new-template/utils.js.map +1 -0
  84. package/build-module/components/block-editor/index.js +1 -2
  85. package/build-module/components/block-editor/index.js.map +1 -1
  86. package/build-module/components/code-editor/index.js +18 -5
  87. package/build-module/components/code-editor/index.js.map +1 -1
  88. package/build-module/components/editor/index.js +16 -0
  89. package/build-module/components/editor/index.js.map +1 -1
  90. package/build-module/components/error-boundary/index.js +5 -0
  91. package/build-module/components/error-boundary/index.js.map +1 -1
  92. package/build-module/components/global-styles/context-menu.js +6 -3
  93. package/build-module/components/global-styles/context-menu.js.map +1 -1
  94. package/build-module/components/global-styles/dimensions-panel.js +185 -20
  95. package/build-module/components/global-styles/dimensions-panel.js.map +1 -1
  96. package/build-module/components/global-styles/global-styles-provider.js +4 -2
  97. package/build-module/components/global-styles/global-styles-provider.js.map +1 -1
  98. package/build-module/components/global-styles/hooks.js +11 -2
  99. package/build-module/components/global-styles/hooks.js.map +1 -1
  100. package/build-module/components/global-styles/palette.js +2 -1
  101. package/build-module/components/global-styles/palette.js.map +1 -1
  102. package/build-module/components/global-styles/screen-block-list.js +4 -1
  103. package/build-module/components/global-styles/screen-block-list.js.map +1 -1
  104. package/build-module/components/global-styles/screen-button-color.js +67 -0
  105. package/build-module/components/global-styles/screen-button-color.js.map +1 -0
  106. package/build-module/components/global-styles/screen-color-palette.js +14 -19
  107. package/build-module/components/global-styles/screen-color-palette.js.map +1 -1
  108. package/build-module/components/global-styles/screen-colors.js +57 -9
  109. package/build-module/components/global-styles/screen-colors.js.map +1 -1
  110. package/build-module/components/global-styles/screen-link-color.js +47 -14
  111. package/build-module/components/global-styles/screen-link-color.js.map +1 -1
  112. package/build-module/components/global-styles/screen-root.js +4 -2
  113. package/build-module/components/global-styles/screen-root.js.map +1 -1
  114. package/build-module/components/global-styles/screen-typography-element.js +4 -0
  115. package/build-module/components/global-styles/screen-typography-element.js.map +1 -1
  116. package/build-module/components/global-styles/screen-typography.js +10 -2
  117. package/build-module/components/global-styles/screen-typography.js.map +1 -1
  118. package/build-module/components/global-styles/ui.js +10 -0
  119. package/build-module/components/global-styles/ui.js.map +1 -1
  120. package/build-module/components/global-styles/use-global-styles-output.js +280 -42
  121. package/build-module/components/global-styles/use-global-styles-output.js.map +1 -1
  122. package/build-module/components/global-styles/utils.js +34 -3
  123. package/build-module/components/global-styles/utils.js.map +1 -1
  124. package/build-module/components/header/index.js +31 -11
  125. package/build-module/components/header/index.js.map +1 -1
  126. package/build-module/components/header/more-menu/site-export.js +4 -1
  127. package/build-module/components/header/more-menu/site-export.js.map +1 -1
  128. package/build-module/components/header/undo-redo/redo.js +9 -3
  129. package/build-module/components/header/undo-redo/redo.js.map +1 -1
  130. package/build-module/components/header/undo-redo/undo.js +9 -3
  131. package/build-module/components/header/undo-redo/undo.js.map +1 -1
  132. package/build-module/components/keyboard-shortcut-help-modal/config.js +17 -0
  133. package/build-module/components/keyboard-shortcut-help-modal/config.js.map +1 -1
  134. package/build-module/components/keyboard-shortcut-help-modal/index.js +1 -2
  135. package/build-module/components/keyboard-shortcut-help-modal/index.js.map +1 -1
  136. package/build-module/components/sidebar/template-card/index.js +18 -7
  137. package/build-module/components/sidebar/template-card/index.js.map +1 -1
  138. package/build-module/components/sidebar/template-card/template-actions.js +49 -0
  139. package/build-module/components/sidebar/template-card/template-actions.js.map +1 -0
  140. package/build-module/components/template-details/edit-template-title.js +12 -3
  141. package/build-module/components/template-details/edit-template-title.js.map +1 -1
  142. package/build-module/components/template-details/index.js +2 -21
  143. package/build-module/components/template-details/index.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/store/selectors.js +5 -2
  149. package/build-module/store/selectors.js.map +1 -1
  150. package/build-style/style-rtl.css +198 -49
  151. package/build-style/style.css +198 -49
  152. package/package.json +29 -29
  153. package/src/components/add-new-template/add-custom-generic-template-modal.js +97 -0
  154. package/src/components/add-new-template/add-custom-template-modal.js +247 -0
  155. package/src/components/add-new-template/new-template.js +158 -70
  156. package/src/components/add-new-template/style.scss +149 -0
  157. package/src/components/add-new-template/utils.js +538 -0
  158. package/src/components/block-editor/index.js +0 -2
  159. package/src/components/code-editor/index.js +15 -5
  160. package/src/components/editor/index.js +11 -0
  161. package/src/components/error-boundary/index.js +5 -0
  162. package/src/components/global-styles/context-menu.js +3 -0
  163. package/src/components/global-styles/dimensions-panel.js +209 -21
  164. package/src/components/global-styles/global-styles-provider.js +8 -9
  165. package/src/components/global-styles/hooks.js +18 -0
  166. package/src/components/global-styles/palette.js +4 -1
  167. package/src/components/global-styles/screen-block-list.js +10 -1
  168. package/src/components/global-styles/screen-button-color.js +102 -0
  169. package/src/components/global-styles/screen-color-palette.js +25 -27
  170. package/src/components/global-styles/screen-colors.js +58 -7
  171. package/src/components/global-styles/screen-link-color.js +65 -23
  172. package/src/components/global-styles/screen-root.js +8 -2
  173. package/src/components/global-styles/screen-typography-element.js +4 -0
  174. package/src/components/global-styles/screen-typography.js +17 -2
  175. package/src/components/global-styles/style.scss +14 -8
  176. package/src/components/global-styles/test/use-global-styles-output.js +313 -16
  177. package/src/components/global-styles/ui.js +13 -0
  178. package/src/components/global-styles/use-global-styles-output.js +344 -38
  179. package/src/components/global-styles/utils.js +36 -2
  180. package/src/components/header/index.js +42 -17
  181. package/src/components/header/more-menu/site-export.js +3 -0
  182. package/src/components/header/style.scss +58 -8
  183. package/src/components/header/undo-redo/redo.js +6 -1
  184. package/src/components/header/undo-redo/undo.js +6 -1
  185. package/src/components/keyboard-shortcut-help-modal/config.js +12 -0
  186. package/src/components/keyboard-shortcut-help-modal/index.js +1 -2
  187. package/src/components/keyboard-shortcut-help-modal/style.scss +0 -5
  188. package/src/components/list/style.scss +0 -8
  189. package/src/components/sidebar/style.scss +4 -0
  190. package/src/components/sidebar/template-card/index.js +15 -6
  191. package/src/components/sidebar/template-card/style.scss +49 -35
  192. package/src/components/sidebar/template-card/template-actions.js +43 -0
  193. package/src/components/template-details/edit-template-title.js +10 -2
  194. package/src/components/template-details/index.js +4 -21
  195. package/src/components/test/error-boundary.js +38 -0
  196. package/src/hooks/index.js +1 -0
  197. package/src/hooks/template-part-edit.js +82 -0
  198. package/src/store/selectors.js +11 -5
  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
@@ -0,0 +1,538 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { get } from 'lodash';
5
+
6
+ /**
7
+ * WordPress dependencies
8
+ */
9
+ import { useSelect } from '@wordpress/data';
10
+ import { store as coreStore } from '@wordpress/core-data';
11
+ import { store as editorStore } from '@wordpress/editor';
12
+ import { decodeEntities } from '@wordpress/html-entities';
13
+ import { useMemo } from '@wordpress/element';
14
+ import { __, sprintf } from '@wordpress/i18n';
15
+ import { blockMeta, post } from '@wordpress/icons';
16
+
17
+ /**
18
+ * @typedef IHasNameAndId
19
+ * @property {string|number} id The entity's id.
20
+ * @property {string} name The entity's name.
21
+ */
22
+
23
+ /**
24
+ * Helper util to map records to add a `name` prop from a
25
+ * provided path, in order to handle all entities in the same
26
+ * fashion(implementing`IHasNameAndId` interface).
27
+ *
28
+ * @param {Object[]} entities The array of entities.
29
+ * @param {string} path The path to map a `name` property from the entity.
30
+ * @return {IHasNameAndId[]} An array of enitities that now implement the `IHasNameAndId` interface.
31
+ */
32
+ export const mapToIHasNameAndId = ( entities, path ) => {
33
+ return ( entities || [] ).map( ( entity ) => ( {
34
+ ...entity,
35
+ name: decodeEntities( get( entity, path ) ),
36
+ } ) );
37
+ };
38
+
39
+ /**
40
+ * @typedef {Object} EntitiesInfo
41
+ * @property {boolean} hasEntities If an entity has available records(posts, terms, etc..).
42
+ * @property {number[]} existingEntitiesIds An array of the existing entities ids.
43
+ */
44
+
45
+ export const useExistingTemplates = () => {
46
+ return useSelect(
47
+ ( select ) =>
48
+ select( coreStore ).getEntityRecords( 'postType', 'wp_template', {
49
+ per_page: -1,
50
+ } ),
51
+ []
52
+ );
53
+ };
54
+
55
+ export const useDefaultTemplateTypes = () => {
56
+ return useSelect(
57
+ ( select ) =>
58
+ select( editorStore ).__experimentalGetDefaultTemplateTypes(),
59
+ []
60
+ );
61
+ };
62
+
63
+ const usePublicPostTypes = () => {
64
+ const postTypes = useSelect(
65
+ ( select ) => select( coreStore ).getPostTypes( { per_page: -1 } ),
66
+ []
67
+ );
68
+ return useMemo( () => {
69
+ const excludedPostTypes = [ 'attachment' ];
70
+ return postTypes?.filter(
71
+ ( { viewable, slug } ) =>
72
+ viewable && ! excludedPostTypes.includes( slug )
73
+ );
74
+ }, [ postTypes ] );
75
+ };
76
+
77
+ const usePublicTaxonomies = () => {
78
+ const taxonomies = useSelect(
79
+ ( select ) => select( coreStore ).getTaxonomies( { per_page: -1 } ),
80
+ []
81
+ );
82
+ return useMemo( () => {
83
+ return taxonomies?.filter(
84
+ ( { visibility } ) => visibility?.publicly_queryable
85
+ );
86
+ }, [ taxonomies ] );
87
+ };
88
+
89
+ export const usePostTypeMenuItems = ( onClickMenuItem ) => {
90
+ const publicPostTypes = usePublicPostTypes();
91
+ const existingTemplates = useExistingTemplates();
92
+ const defaultTemplateTypes = useDefaultTemplateTypes();
93
+ // `page`is a special case in template hierarchy.
94
+ const templatePrefixes = useMemo(
95
+ () =>
96
+ publicPostTypes?.reduce( ( accumulator, { slug } ) => {
97
+ let suffix = slug;
98
+ if ( slug !== 'page' ) {
99
+ suffix = `single-${ suffix }`;
100
+ }
101
+ accumulator[ slug ] = suffix;
102
+ return accumulator;
103
+ }, {} ),
104
+ [ publicPostTypes ]
105
+ );
106
+ // We need to keep track of naming conflicts. If a conflict
107
+ // occurs, we need to add slug.
108
+ const postTypeLabels = publicPostTypes?.reduce(
109
+ ( accumulator, { labels } ) => {
110
+ const singularName = labels.singular_name.toLowerCase();
111
+ accumulator[ singularName ] =
112
+ ( accumulator[ singularName ] || 0 ) + 1;
113
+ return accumulator;
114
+ },
115
+ {}
116
+ );
117
+ const needsUniqueIdentifier = ( labels, slug ) => {
118
+ const singularName = labels.singular_name.toLowerCase();
119
+ return postTypeLabels[ singularName ] > 1 && singularName !== slug;
120
+ };
121
+ const postTypesInfo = useEntitiesInfo( 'postType', templatePrefixes );
122
+ const existingTemplateSlugs = ( existingTemplates || [] ).map(
123
+ ( { slug } ) => slug
124
+ );
125
+ const menuItems = ( publicPostTypes || [] ).reduce(
126
+ ( accumulator, postType ) => {
127
+ const { slug, labels, icon } = postType;
128
+ // We need to check if the general template is part of the
129
+ // defaultTemplateTypes. If it is, just use that info and
130
+ // augment it with the specific template functionality.
131
+ const generalTemplateSlug = templatePrefixes[ slug ];
132
+ const defaultTemplateType = defaultTemplateTypes?.find(
133
+ ( { slug: _slug } ) => _slug === generalTemplateSlug
134
+ );
135
+ const hasGeneralTemplate =
136
+ existingTemplateSlugs?.includes( generalTemplateSlug );
137
+ const _needsUniqueIdentifier = needsUniqueIdentifier(
138
+ labels,
139
+ slug
140
+ );
141
+ let menuItemTitle = sprintf(
142
+ // translators: %s: Name of the post type e.g: "Post".
143
+ __( 'Single item: %s' ),
144
+ labels.singular_name
145
+ );
146
+ if ( _needsUniqueIdentifier ) {
147
+ menuItemTitle = sprintf(
148
+ // translators: %1s: Name of the post type e.g: "Post"; %2s: Slug of the post type e.g: "book".
149
+ __( 'Single item: %1$s (%2$s)' ),
150
+ labels.singular_name,
151
+ slug
152
+ );
153
+ }
154
+ const menuItem = defaultTemplateType
155
+ ? { ...defaultTemplateType }
156
+ : {
157
+ slug: generalTemplateSlug,
158
+ title: menuItemTitle,
159
+ description: sprintf(
160
+ // translators: %s: Name of the post type e.g: "Post".
161
+ __( 'Displays a single item: %s.' ),
162
+ labels.singular_name
163
+ ),
164
+ // `icon` is the `menu_icon` property of a post type. We
165
+ // only handle `dashicons` for now, even if the `menu_icon`
166
+ // also supports urls and svg as values.
167
+ icon: icon?.startsWith( 'dashicons-' )
168
+ ? icon.slice( 10 )
169
+ : post,
170
+ };
171
+ const hasEntities = postTypesInfo?.[ slug ]?.hasEntities;
172
+ // We have a different template creation flow only if they have entities.
173
+ if ( hasEntities ) {
174
+ menuItem.onClick = ( template ) => {
175
+ onClickMenuItem( {
176
+ type: 'postType',
177
+ slug,
178
+ config: {
179
+ recordNamePath: 'title.rendered',
180
+ queryArgs: ( { search } ) => {
181
+ return {
182
+ _fields: 'id,title,slug,link',
183
+ orderBy: search ? 'relevance' : 'modified',
184
+ exclude:
185
+ postTypesInfo[ slug ]
186
+ .existingEntitiesIds,
187
+ };
188
+ },
189
+ getSpecificTemplate: ( suggestion ) => {
190
+ let title = sprintf(
191
+ // 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".
192
+ __( '%1$s: %2$s' ),
193
+ labels.singular_name,
194
+ suggestion.name
195
+ );
196
+ const description = sprintf(
197
+ // translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Page: Hello"
198
+ __( 'Template for %1$s' ),
199
+ title
200
+ );
201
+ if ( _needsUniqueIdentifier ) {
202
+ title = sprintf(
203
+ // 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)"
204
+ __( '%1$s %2$s' ),
205
+ title,
206
+ `(${ slug })`
207
+ );
208
+ }
209
+ return {
210
+ title,
211
+ description,
212
+ slug: `${ templatePrefixes[ slug ] }-${ suggestion.slug }`,
213
+ };
214
+ },
215
+ },
216
+ labels,
217
+ hasGeneralTemplate,
218
+ template,
219
+ } );
220
+ };
221
+ }
222
+ // We don't need to add the menu item if there are no
223
+ // entities and the general template exists.
224
+ if ( ! hasGeneralTemplate || hasEntities ) {
225
+ accumulator.push( menuItem );
226
+ }
227
+ return accumulator;
228
+ },
229
+ []
230
+ );
231
+ // Split menu items into two groups: one for the default post types
232
+ // and one for the rest.
233
+ const postTypesMenuItems = useMemo(
234
+ () =>
235
+ menuItems.reduce(
236
+ ( accumulator, postType ) => {
237
+ const { slug } = postType;
238
+ let key = 'postTypesMenuItems';
239
+ if ( slug === 'page' ) {
240
+ key = 'defaultPostTypesMenuItems';
241
+ }
242
+ accumulator[ key ].push( postType );
243
+ return accumulator;
244
+ },
245
+ { defaultPostTypesMenuItems: [], postTypesMenuItems: [] }
246
+ ),
247
+ [ menuItems ]
248
+ );
249
+ return postTypesMenuItems;
250
+ };
251
+
252
+ export const useTaxonomiesMenuItems = ( onClickMenuItem ) => {
253
+ const publicTaxonomies = usePublicTaxonomies();
254
+ const existingTemplates = useExistingTemplates();
255
+ const defaultTemplateTypes = useDefaultTemplateTypes();
256
+ // `category` and `post_tag` are special cases in template hierarchy.
257
+ const templatePrefixes = useMemo(
258
+ () =>
259
+ publicTaxonomies?.reduce( ( accumulator, { slug } ) => {
260
+ let suffix = slug;
261
+ if ( ! [ 'category', 'post_tag' ].includes( slug ) ) {
262
+ suffix = `taxonomy-${ suffix }`;
263
+ }
264
+ if ( slug === 'post_tag' ) {
265
+ suffix = `tag`;
266
+ }
267
+ accumulator[ slug ] = suffix;
268
+ return accumulator;
269
+ }, {} ),
270
+ [ publicTaxonomies ]
271
+ );
272
+ // We need to keep track of naming conflicts. If a conflict
273
+ // occurs, we need to add slug.
274
+ const taxonomyLabels = publicTaxonomies?.reduce(
275
+ ( accumulator, { labels } ) => {
276
+ const singularName = labels.singular_name.toLowerCase();
277
+ accumulator[ singularName ] =
278
+ ( accumulator[ singularName ] || 0 ) + 1;
279
+ return accumulator;
280
+ },
281
+ {}
282
+ );
283
+ const needsUniqueIdentifier = ( labels, slug ) => {
284
+ if ( [ 'category', 'post_tag' ].includes( slug ) ) {
285
+ return false;
286
+ }
287
+ const singularName = labels.singular_name.toLowerCase();
288
+ return taxonomyLabels[ singularName ] > 1 && singularName !== slug;
289
+ };
290
+ const taxonomiesInfo = useEntitiesInfo( 'taxonomy', templatePrefixes );
291
+ const existingTemplateSlugs = ( existingTemplates || [] ).map(
292
+ ( { slug } ) => slug
293
+ );
294
+ const menuItems = ( publicTaxonomies || [] ).reduce(
295
+ ( accumulator, taxonomy ) => {
296
+ const { slug, labels } = taxonomy;
297
+ // We need to check if the general template is part of the
298
+ // defaultTemplateTypes. If it is, just use that info and
299
+ // augment it with the specific template functionality.
300
+ const generalTemplateSlug = templatePrefixes[ slug ];
301
+ const defaultTemplateType = defaultTemplateTypes?.find(
302
+ ( { slug: _slug } ) => _slug === generalTemplateSlug
303
+ );
304
+ const hasGeneralTemplate =
305
+ existingTemplateSlugs?.includes( generalTemplateSlug );
306
+ const _needsUniqueIdentifier = needsUniqueIdentifier(
307
+ labels,
308
+ slug
309
+ );
310
+ let menuItemTitle = labels.singular_name;
311
+ if ( _needsUniqueIdentifier ) {
312
+ menuItemTitle = sprintf(
313
+ // translators: %1s: Name of the taxonomy e.g: "Category"; %2s: Slug of the taxonomy e.g: "product_cat".
314
+ __( '%1$s (%2$s)' ),
315
+ labels.singular_name,
316
+ slug
317
+ );
318
+ }
319
+ const menuItem = defaultTemplateType
320
+ ? { ...defaultTemplateType }
321
+ : {
322
+ slug: generalTemplateSlug,
323
+ title: menuItemTitle,
324
+ description: sprintf(
325
+ // translators: %s: Name of the taxonomy e.g: "Product Categories".
326
+ __( 'Displays taxonomy: %s.' ),
327
+ labels.singular_name
328
+ ),
329
+ icon: blockMeta,
330
+ };
331
+ const hasEntities = taxonomiesInfo?.[ slug ]?.hasEntities;
332
+ // We have a different template creation flow only if they have entities.
333
+ if ( hasEntities ) {
334
+ menuItem.onClick = ( template ) => {
335
+ onClickMenuItem( {
336
+ type: 'taxonomy',
337
+ slug,
338
+ config: {
339
+ queryArgs: ( { search } ) => {
340
+ return {
341
+ _fields: 'id,name,slug,link',
342
+ orderBy: search ? 'name' : 'count',
343
+ exclude:
344
+ taxonomiesInfo[ slug ]
345
+ .existingEntitiesIds,
346
+ };
347
+ },
348
+ getSpecificTemplate: ( suggestion ) => {
349
+ let title = sprintf(
350
+ // 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".
351
+ __( '%1$s: %2$s' ),
352
+ labels.singular_name,
353
+ suggestion.name
354
+ );
355
+ const description = sprintf(
356
+ // translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Category: shoes"
357
+ __( 'Template for %1$s' ),
358
+ title
359
+ );
360
+ if ( _needsUniqueIdentifier ) {
361
+ title = sprintf(
362
+ // 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)"
363
+ __( '%1$s %2$s' ),
364
+ title,
365
+ `(${ slug })`
366
+ );
367
+ }
368
+ return {
369
+ title,
370
+ description,
371
+ slug: `${ templatePrefixes[ slug ] }-${ suggestion.slug }`,
372
+ };
373
+ },
374
+ },
375
+ labels,
376
+ hasGeneralTemplate,
377
+ template,
378
+ } );
379
+ };
380
+ }
381
+ // We don't need to add the menu item if there are no
382
+ // entities and the general template exists.
383
+ if ( ! hasGeneralTemplate || hasEntities ) {
384
+ accumulator.push( menuItem );
385
+ }
386
+ return accumulator;
387
+ },
388
+ []
389
+ );
390
+ // Split menu items into two groups: one for the default taxonomies
391
+ // and one for the rest.
392
+ const taxonomiesMenuItems = useMemo(
393
+ () =>
394
+ menuItems.reduce(
395
+ ( accumulator, taxonomy ) => {
396
+ const { slug } = taxonomy;
397
+ let key = 'taxonomiesMenuItems';
398
+ if ( [ 'category', 'tag' ].includes( slug ) ) {
399
+ key = 'defaultTaxonomiesMenuItems';
400
+ }
401
+ accumulator[ key ].push( taxonomy );
402
+ return accumulator;
403
+ },
404
+ { defaultTaxonomiesMenuItems: [], taxonomiesMenuItems: [] }
405
+ ),
406
+ [ menuItems ]
407
+ );
408
+ return taxonomiesMenuItems;
409
+ };
410
+
411
+ /**
412
+ * Helper hook that filters all the existing templates by the given
413
+ * object with the entity's slug as key and the template prefix as value.
414
+ *
415
+ * Example:
416
+ * `existingTemplates` is: [ { slug: tag-apple }, { slug: page-about }, { slug: tag } ]
417
+ * `templatePrefixes` is: { post_tag: 'tag' }
418
+ * It will return: { post_tag: [apple] }
419
+ *
420
+ * Note: We append the `-` to the given template prefix in this function for our checks.
421
+ *
422
+ * @param {Record<string,string>} templatePrefixes An object with the entity's slug as key and the template prefix as value.
423
+ * @return {Record<string,string[]>} An object with the entity's slug as key and an array with the existing template slugs as value.
424
+ */
425
+ const useExistingTemplateSlugs = ( templatePrefixes ) => {
426
+ const existingTemplates = useExistingTemplates();
427
+ const existingSlugs = useMemo( () => {
428
+ return Object.entries( templatePrefixes || {} ).reduce(
429
+ ( accumulator, [ slug, prefix ] ) => {
430
+ const slugsWithTemplates = ( existingTemplates || [] ).reduce(
431
+ ( _accumulator, existingTemplate ) => {
432
+ const _prefix = `${ prefix }-`;
433
+ if ( existingTemplate.slug.startsWith( _prefix ) ) {
434
+ _accumulator.push(
435
+ existingTemplate.slug.substring(
436
+ _prefix.length
437
+ )
438
+ );
439
+ }
440
+ return _accumulator;
441
+ },
442
+ []
443
+ );
444
+ if ( slugsWithTemplates.length ) {
445
+ accumulator[ slug ] = slugsWithTemplates;
446
+ }
447
+ return accumulator;
448
+ },
449
+ {}
450
+ );
451
+ }, [ templatePrefixes, existingTemplates ] );
452
+ return existingSlugs;
453
+ };
454
+
455
+ /**
456
+ * Helper hook that finds the existing records with an associated template,
457
+ * as they need to be excluded from the template suggestions.
458
+ *
459
+ * @param {string} entityName The entity's name.
460
+ * @param {Record<string,string>} templatePrefixes An object with the entity's slug as key and the template prefix as value.
461
+ * @return {Record<string,EntitiesInfo>} An object with the entity's slug as key and the existing records as value.
462
+ */
463
+ const useTemplatesToExclude = ( entityName, templatePrefixes ) => {
464
+ const slugsToExcludePerEntity =
465
+ useExistingTemplateSlugs( templatePrefixes );
466
+ const recordsToExcludePerEntity = useSelect(
467
+ ( select ) => {
468
+ return Object.entries( slugsToExcludePerEntity || {} ).reduce(
469
+ ( accumulator, [ slug, slugsWithTemplates ] ) => {
470
+ const entitiesWithTemplates = select(
471
+ coreStore
472
+ ).getEntityRecords( entityName, slug, {
473
+ _fields: 'id',
474
+ context: 'view',
475
+ slug: slugsWithTemplates,
476
+ } );
477
+ if ( entitiesWithTemplates?.length ) {
478
+ accumulator[ slug ] = entitiesWithTemplates;
479
+ }
480
+ return accumulator;
481
+ },
482
+ {}
483
+ );
484
+ },
485
+ [ slugsToExcludePerEntity ]
486
+ );
487
+ return recordsToExcludePerEntity;
488
+ };
489
+
490
+ /**
491
+ * Helper hook that returns information about an entity having
492
+ * records that we can create a specific template for.
493
+ *
494
+ * For example we can search for `terms` in `taxonomy` entity or
495
+ * `posts` in `postType` entity.
496
+ *
497
+ * First we need to find the existing records with an associated template,
498
+ * to query afterwards for any remaining record, by excluding them.
499
+ *
500
+ * @param {string} entityName The entity's name.
501
+ * @param {Record<string,string>} templatePrefixes An object with the entity's slug as key and the template prefix as value.
502
+ * @return {Record<string,EntitiesInfo>} An object with the entity's slug as key and the EntitiesInfo as value.
503
+ */
504
+ const useEntitiesInfo = ( entityName, templatePrefixes ) => {
505
+ const recordsToExcludePerEntity = useTemplatesToExclude(
506
+ entityName,
507
+ templatePrefixes
508
+ );
509
+ const entitiesInfo = useSelect(
510
+ ( select ) => {
511
+ return Object.keys( templatePrefixes || {} ).reduce(
512
+ ( accumulator, slug ) => {
513
+ const existingEntitiesIds =
514
+ recordsToExcludePerEntity?.[ slug ]?.map(
515
+ ( { id } ) => id
516
+ ) || [];
517
+ accumulator[ slug ] = {
518
+ hasEntities: !! select( coreStore ).getEntityRecords(
519
+ entityName,
520
+ slug,
521
+ {
522
+ per_page: 1,
523
+ _fields: 'id',
524
+ context: 'view',
525
+ exclude: existingEntitiesIds,
526
+ }
527
+ )?.length,
528
+ existingEntitiesIds,
529
+ };
530
+ return accumulator;
531
+ },
532
+ {}
533
+ );
534
+ },
535
+ [ templatePrefixes, recordsToExcludePerEntity ]
536
+ );
537
+ return entitiesInfo;
538
+ };
@@ -38,7 +38,6 @@ import NavigateToLink from '../navigate-to-link';
38
38
  import { SidebarInspectorFill } from '../sidebar';
39
39
  import { store as editSiteStore } from '../../store';
40
40
  import BlockInspectorButton from './block-inspector-button';
41
- import EditTemplatePartMenuButton from '../edit-template-part-menu-button';
42
41
  import BackButton from './back-button';
43
42
  import ResizableEditor from './resizable-editor';
44
43
 
@@ -155,7 +154,6 @@ export default function BlockEditor( { setIsInserterOpen } ) {
155
154
  onChange={ onChange }
156
155
  useSubRegistry={ false }
157
156
  >
158
- <EditTemplatePartMenuButton />
159
157
  <TemplatePartConverter />
160
158
  <__experimentalLinkControl.ViewerFill>
161
159
  { useCallback(
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import { parse } from '@wordpress/blocks';
4
+ import { parse, __unstableSerializeAndClean } from '@wordpress/blocks';
5
5
  import { useEntityBlockEditor, useEntityProp } from '@wordpress/core-data';
6
6
  import { useSelect, useDispatch } from '@wordpress/data';
7
7
  import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
@@ -32,10 +32,20 @@ export default function CodeEditor() {
32
32
  'postType',
33
33
  templateType
34
34
  );
35
- const content =
36
- contentStructure instanceof Function
37
- ? contentStructure( { blocks } )
38
- : contentStructure;
35
+
36
+ // Replicates the logic found in getEditedPostContent().
37
+ let content;
38
+ if ( contentStructure instanceof Function ) {
39
+ content = contentStructure( { blocks } );
40
+ } else if ( blocks ) {
41
+ // If we have parsed blocks already, they should be our source of truth.
42
+ // Parsing applies block deprecations and legacy block conversions that
43
+ // unparsed content will not have.
44
+ content = __unstableSerializeAndClean( blocks );
45
+ } else {
46
+ content = contentStructure;
47
+ }
48
+
39
49
  const { switchEditorMode } = useDispatch( editSiteStore );
40
50
  return (
41
51
  <div className="edit-site-code-editor">
@@ -47,6 +47,17 @@ import { GlobalStylesProvider } from '../global-styles/global-styles-provider';
47
47
  import useTitle from '../routes/use-title';
48
48
 
49
49
  const interfaceLabels = {
50
+ /* translators: accessibility text for the editor top bar landmark region. */
51
+ header: __( 'Editor top bar' ),
52
+ /* translators: accessibility text for the editor content landmark region. */
53
+ body: __( 'Editor content' ),
54
+ /* translators: accessibility text for the editor settings landmark region. */
55
+ sidebar: __( 'Editor settings' ),
56
+ /* translators: accessibility text for the editor publish landmark region. */
57
+ actions: __( 'Editor publish' ),
58
+ /* translators: accessibility text for the editor footer landmark region. */
59
+ footer: __( 'Editor footer' ),
60
+ /* translators: accessibility text for the navigation sidebar landmark region. */
50
61
  drawer: __( 'Navigation Sidebar' ),
51
62
  };
52
63
 
@@ -3,6 +3,7 @@
3
3
  */
4
4
  import { Component } from '@wordpress/element';
5
5
  import { __ } from '@wordpress/i18n';
6
+ import { doAction } from '@wordpress/hooks';
6
7
 
7
8
  /**
8
9
  * Internal dependencies
@@ -20,6 +21,10 @@ export default class ErrorBoundary extends Component {
20
21
  };
21
22
  }
22
23
 
24
+ componentDidCatch( error ) {
25
+ doAction( 'editor.ErrorBoundary.errorLogged', error );
26
+ }
27
+
23
28
  static getDerivedStateFromError( error ) {
24
29
  return { error };
25
30
  }
@@ -27,6 +27,7 @@ function ContextMenu( { name, parentMenu = '' } ) {
27
27
  <NavigationButtonAsItem
28
28
  icon={ typography }
29
29
  path={ parentMenu + '/typography' }
30
+ aria-label={ __( 'Typography styles' ) }
30
31
  >
31
32
  { __( 'Typography' ) }
32
33
  </NavigationButtonAsItem>
@@ -35,6 +36,7 @@ function ContextMenu( { name, parentMenu = '' } ) {
35
36
  <NavigationButtonAsItem
36
37
  icon={ color }
37
38
  path={ parentMenu + '/colors' }
39
+ aria-label={ __( 'Colors styles' ) }
38
40
  >
39
41
  { __( 'Colors' ) }
40
42
  </NavigationButtonAsItem>
@@ -43,6 +45,7 @@ function ContextMenu( { name, parentMenu = '' } ) {
43
45
  <NavigationButtonAsItem
44
46
  icon={ layout }
45
47
  path={ parentMenu + '/layout' }
48
+ aria-label={ __( 'Layout styles' ) }
46
49
  >
47
50
  { __( 'Layout' ) }
48
51
  </NavigationButtonAsItem>