@wordpress/edit-site 4.7.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 (248) 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 +224 -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 +94 -33
  7. package/build/components/add-new-template/new-template.js.map +1 -1
  8. package/build/components/add-new-template/utils.js +405 -0
  9. package/build/components/add-new-template/utils.js.map +1 -0
  10. package/build/components/block-editor/block-inspector-button.js.map +1 -1
  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/edit-template-part-menu-button/index.js.map +1 -1
  15. package/build/components/editor/index.js +16 -0
  16. package/build/components/editor/index.js.map +1 -1
  17. package/build/components/error-boundary/index.js +6 -0
  18. package/build/components/error-boundary/index.js.map +1 -1
  19. package/build/components/global-styles/context-menu.js +6 -3
  20. package/build/components/global-styles/context-menu.js.map +1 -1
  21. package/build/components/global-styles/dimensions-panel.js +2 -6
  22. package/build/components/global-styles/dimensions-panel.js.map +1 -1
  23. package/build/components/global-styles/global-styles-provider.js +4 -2
  24. package/build/components/global-styles/global-styles-provider.js.map +1 -1
  25. package/build/components/global-styles/gradients-palette-panel.js +3 -7
  26. package/build/components/global-styles/gradients-palette-panel.js.map +1 -1
  27. package/build/components/global-styles/hooks.js +11 -2
  28. package/build/components/global-styles/hooks.js.map +1 -1
  29. package/build/components/global-styles/palette.js +2 -1
  30. package/build/components/global-styles/palette.js.map +1 -1
  31. package/build/components/global-styles/screen-block-list.js +4 -1
  32. package/build/components/global-styles/screen-block-list.js.map +1 -1
  33. package/build/components/global-styles/screen-button-color.js +80 -0
  34. package/build/components/global-styles/screen-button-color.js.map +1 -0
  35. package/build/components/global-styles/screen-color-palette.js +13 -17
  36. package/build/components/global-styles/screen-color-palette.js.map +1 -1
  37. package/build/components/global-styles/screen-colors.js +56 -8
  38. package/build/components/global-styles/screen-colors.js.map +1 -1
  39. package/build/components/global-styles/screen-link-color.js +48 -14
  40. package/build/components/global-styles/screen-link-color.js.map +1 -1
  41. package/build/components/global-styles/screen-root.js +4 -2
  42. package/build/components/global-styles/screen-root.js.map +1 -1
  43. package/build/components/global-styles/screen-style-variations.js +9 -1
  44. package/build/components/global-styles/screen-style-variations.js.map +1 -1
  45. package/build/components/global-styles/screen-typography-element.js +4 -0
  46. package/build/components/global-styles/screen-typography-element.js.map +1 -1
  47. package/build/components/global-styles/screen-typography.js +9 -1
  48. package/build/components/global-styles/screen-typography.js.map +1 -1
  49. package/build/components/global-styles/ui.js +11 -0
  50. package/build/components/global-styles/ui.js.map +1 -1
  51. package/build/components/global-styles/use-global-styles-output.js +199 -30
  52. package/build/components/global-styles/use-global-styles-output.js.map +1 -1
  53. package/build/components/global-styles/utils.js +4 -2
  54. package/build/components/global-styles/utils.js.map +1 -1
  55. package/build/components/header/index.js +28 -10
  56. package/build/components/header/index.js.map +1 -1
  57. package/build/components/header/mode-switcher/index.js.map +1 -1
  58. package/build/components/header/more-menu/copy-content-menu-item.js +1 -1
  59. package/build/components/header/more-menu/copy-content-menu-item.js.map +1 -1
  60. package/build/components/header/more-menu/site-export.js +4 -1
  61. package/build/components/header/more-menu/site-export.js.map +1 -1
  62. package/build/components/header/undo-redo/redo.js +13 -4
  63. package/build/components/header/undo-redo/redo.js.map +1 -1
  64. package/build/components/header/undo-redo/undo.js +13 -4
  65. package/build/components/header/undo-redo/undo.js.map +1 -1
  66. package/build/components/keyboard-shortcut-help-modal/config.js +17 -0
  67. package/build/components/keyboard-shortcut-help-modal/config.js.map +1 -1
  68. package/build/components/keyboard-shortcut-help-modal/index.js +1 -3
  69. package/build/components/keyboard-shortcut-help-modal/index.js.map +1 -1
  70. package/build/components/keyboard-shortcuts/index.js.map +1 -1
  71. package/build/components/list/actions/index.js.map +1 -1
  72. package/build/components/list/actions/rename-menu-item.js.map +1 -1
  73. package/build/components/list/added-by.js.map +1 -1
  74. package/build/components/navigation-sidebar/index.js.map +1 -1
  75. package/build/components/save-button/index.js.map +1 -1
  76. package/build/components/sidebar/index.js.map +1 -1
  77. package/build/components/sidebar/navigation-menu-sidebar/navigation-inspector.js.map +1 -1
  78. package/build/components/sidebar/template-card/index.js +19 -7
  79. package/build/components/sidebar/template-card/index.js.map +1 -1
  80. package/build/components/sidebar/template-card/template-actions.js +64 -0
  81. package/build/components/sidebar/template-card/template-actions.js.map +1 -0
  82. package/build/components/sidebar/template-card/template-areas.js.map +1 -1
  83. package/build/components/template-details/edit-template-title.js +11 -3
  84. package/build/components/template-details/edit-template-title.js.map +1 -1
  85. package/build/components/template-details/index.js +1 -20
  86. package/build/components/template-details/index.js.map +1 -1
  87. package/build/components/template-details/template-areas.js.map +1 -1
  88. package/build/components/template-part-converter/index.js.map +1 -1
  89. package/build/components/url-query-controller/index.js.map +1 -1
  90. package/build/store/actions.js.map +1 -1
  91. package/build/store/selectors.js +4 -1
  92. package/build/store/selectors.js.map +1 -1
  93. package/build-module/components/add-new-template/add-custom-generic-template-modal.js +77 -0
  94. package/build-module/components/add-new-template/add-custom-generic-template-modal.js.map +1 -0
  95. package/build-module/components/add-new-template/add-custom-template-modal.js +209 -0
  96. package/build-module/components/add-new-template/add-custom-template-modal.js.map +1 -0
  97. package/build-module/components/add-new-template/new-template.js +93 -34
  98. package/build-module/components/add-new-template/new-template.js.map +1 -1
  99. package/build-module/components/add-new-template/utils.js +365 -0
  100. package/build-module/components/add-new-template/utils.js.map +1 -0
  101. package/build-module/components/block-editor/block-inspector-button.js.map +1 -1
  102. package/build-module/components/block-editor/index.js.map +1 -1
  103. package/build-module/components/code-editor/index.js +18 -5
  104. package/build-module/components/code-editor/index.js.map +1 -1
  105. package/build-module/components/edit-template-part-menu-button/index.js.map +1 -1
  106. package/build-module/components/editor/index.js +16 -0
  107. package/build-module/components/editor/index.js.map +1 -1
  108. package/build-module/components/error-boundary/index.js +5 -0
  109. package/build-module/components/error-boundary/index.js.map +1 -1
  110. package/build-module/components/global-styles/context-menu.js +6 -3
  111. package/build-module/components/global-styles/context-menu.js.map +1 -1
  112. package/build-module/components/global-styles/dimensions-panel.js +2 -6
  113. package/build-module/components/global-styles/dimensions-panel.js.map +1 -1
  114. package/build-module/components/global-styles/global-styles-provider.js +4 -2
  115. package/build-module/components/global-styles/global-styles-provider.js.map +1 -1
  116. package/build-module/components/global-styles/gradients-palette-panel.js +3 -5
  117. package/build-module/components/global-styles/gradients-palette-panel.js.map +1 -1
  118. package/build-module/components/global-styles/hooks.js +11 -2
  119. package/build-module/components/global-styles/hooks.js.map +1 -1
  120. package/build-module/components/global-styles/palette.js +2 -1
  121. package/build-module/components/global-styles/palette.js.map +1 -1
  122. package/build-module/components/global-styles/screen-block-list.js +4 -1
  123. package/build-module/components/global-styles/screen-block-list.js.map +1 -1
  124. package/build-module/components/global-styles/screen-button-color.js +67 -0
  125. package/build-module/components/global-styles/screen-button-color.js.map +1 -0
  126. package/build-module/components/global-styles/screen-color-palette.js +14 -19
  127. package/build-module/components/global-styles/screen-color-palette.js.map +1 -1
  128. package/build-module/components/global-styles/screen-colors.js +57 -9
  129. package/build-module/components/global-styles/screen-colors.js.map +1 -1
  130. package/build-module/components/global-styles/screen-link-color.js +47 -14
  131. package/build-module/components/global-styles/screen-link-color.js.map +1 -1
  132. package/build-module/components/global-styles/screen-root.js +4 -2
  133. package/build-module/components/global-styles/screen-root.js.map +1 -1
  134. package/build-module/components/global-styles/screen-style-variations.js +9 -1
  135. package/build-module/components/global-styles/screen-style-variations.js.map +1 -1
  136. package/build-module/components/global-styles/screen-typography-element.js +4 -0
  137. package/build-module/components/global-styles/screen-typography-element.js.map +1 -1
  138. package/build-module/components/global-styles/screen-typography.js +10 -2
  139. package/build-module/components/global-styles/screen-typography.js.map +1 -1
  140. package/build-module/components/global-styles/ui.js +10 -0
  141. package/build-module/components/global-styles/ui.js.map +1 -1
  142. package/build-module/components/global-styles/use-global-styles-output.js +198 -32
  143. package/build-module/components/global-styles/use-global-styles-output.js.map +1 -1
  144. package/build-module/components/global-styles/utils.js +5 -3
  145. package/build-module/components/global-styles/utils.js.map +1 -1
  146. package/build-module/components/header/index.js +29 -11
  147. package/build-module/components/header/index.js.map +1 -1
  148. package/build-module/components/header/mode-switcher/index.js.map +1 -1
  149. package/build-module/components/header/more-menu/copy-content-menu-item.js +1 -1
  150. package/build-module/components/header/more-menu/copy-content-menu-item.js.map +1 -1
  151. package/build-module/components/header/more-menu/site-export.js +4 -1
  152. package/build-module/components/header/more-menu/site-export.js.map +1 -1
  153. package/build-module/components/header/undo-redo/redo.js +9 -3
  154. package/build-module/components/header/undo-redo/redo.js.map +1 -1
  155. package/build-module/components/header/undo-redo/undo.js +9 -3
  156. package/build-module/components/header/undo-redo/undo.js.map +1 -1
  157. package/build-module/components/keyboard-shortcut-help-modal/config.js +17 -0
  158. package/build-module/components/keyboard-shortcut-help-modal/config.js.map +1 -1
  159. package/build-module/components/keyboard-shortcut-help-modal/index.js +1 -2
  160. package/build-module/components/keyboard-shortcut-help-modal/index.js.map +1 -1
  161. package/build-module/components/keyboard-shortcuts/index.js.map +1 -1
  162. package/build-module/components/list/actions/index.js.map +1 -1
  163. package/build-module/components/list/actions/rename-menu-item.js.map +1 -1
  164. package/build-module/components/list/added-by.js.map +1 -1
  165. package/build-module/components/navigation-sidebar/index.js.map +1 -1
  166. package/build-module/components/save-button/index.js.map +1 -1
  167. package/build-module/components/sidebar/index.js.map +1 -1
  168. package/build-module/components/sidebar/navigation-menu-sidebar/navigation-inspector.js.map +1 -1
  169. package/build-module/components/sidebar/template-card/index.js +18 -7
  170. package/build-module/components/sidebar/template-card/index.js.map +1 -1
  171. package/build-module/components/sidebar/template-card/template-actions.js +49 -0
  172. package/build-module/components/sidebar/template-card/template-actions.js.map +1 -0
  173. package/build-module/components/sidebar/template-card/template-areas.js.map +1 -1
  174. package/build-module/components/template-details/edit-template-title.js +12 -3
  175. package/build-module/components/template-details/edit-template-title.js.map +1 -1
  176. package/build-module/components/template-details/index.js +2 -21
  177. package/build-module/components/template-details/index.js.map +1 -1
  178. package/build-module/components/template-details/template-areas.js.map +1 -1
  179. package/build-module/components/template-part-converter/index.js.map +1 -1
  180. package/build-module/components/url-query-controller/index.js.map +1 -1
  181. package/build-module/store/actions.js.map +1 -1
  182. package/build-module/store/selectors.js +5 -2
  183. package/build-module/store/selectors.js.map +1 -1
  184. package/build-style/style-rtl.css +188 -42
  185. package/build-style/style.css +188 -42
  186. package/package.json +29 -29
  187. package/src/components/add-new-template/add-custom-generic-template-modal.js +97 -0
  188. package/src/components/add-new-template/add-custom-template-modal.js +265 -0
  189. package/src/components/add-new-template/new-template.js +194 -70
  190. package/src/components/add-new-template/style.scss +137 -0
  191. package/src/components/add-new-template/utils.js +369 -0
  192. package/src/components/block-editor/block-inspector-button.js +2 -3
  193. package/src/components/block-editor/index.js +4 -9
  194. package/src/components/code-editor/index.js +15 -5
  195. package/src/components/edit-template-part-menu-button/index.js +2 -3
  196. package/src/components/editor/index.js +15 -5
  197. package/src/components/error-boundary/index.js +5 -0
  198. package/src/components/global-styles/context-menu.js +3 -0
  199. package/src/components/global-styles/dimensions-panel.js +2 -7
  200. package/src/components/global-styles/global-styles-provider.js +12 -17
  201. package/src/components/global-styles/gradients-palette-panel.js +2 -5
  202. package/src/components/global-styles/hooks.js +20 -3
  203. package/src/components/global-styles/palette.js +4 -1
  204. package/src/components/global-styles/screen-block-list.js +14 -5
  205. package/src/components/global-styles/screen-button-color.js +102 -0
  206. package/src/components/global-styles/screen-color-palette.js +25 -27
  207. package/src/components/global-styles/screen-colors.js +58 -7
  208. package/src/components/global-styles/screen-link-color.js +65 -23
  209. package/src/components/global-styles/screen-root.js +12 -5
  210. package/src/components/global-styles/screen-style-variations.js +10 -4
  211. package/src/components/global-styles/screen-typography-element.js +4 -0
  212. package/src/components/global-styles/screen-typography.js +17 -2
  213. package/src/components/global-styles/style.scss +14 -8
  214. package/src/components/global-styles/test/use-global-styles-output.js +250 -16
  215. package/src/components/global-styles/ui.js +13 -0
  216. package/src/components/global-styles/use-global-styles-output.js +246 -32
  217. package/src/components/global-styles/utils.js +5 -2
  218. package/src/components/header/index.js +38 -13
  219. package/src/components/header/mode-switcher/index.js +4 -4
  220. package/src/components/header/more-menu/copy-content-menu-item.js +3 -4
  221. package/src/components/header/more-menu/site-export.js +5 -3
  222. package/src/components/header/style.scss +53 -5
  223. package/src/components/header/undo-redo/redo.js +6 -1
  224. package/src/components/header/undo-redo/undo.js +6 -1
  225. package/src/components/keyboard-shortcut-help-modal/config.js +12 -0
  226. package/src/components/keyboard-shortcut-help-modal/index.js +1 -2
  227. package/src/components/keyboard-shortcut-help-modal/style.scss +0 -5
  228. package/src/components/keyboard-shortcuts/index.js +6 -10
  229. package/src/components/list/actions/index.js +2 -3
  230. package/src/components/list/actions/rename-menu-item.js +4 -6
  231. package/src/components/list/added-by.js +4 -3
  232. package/src/components/list/style.scss +0 -8
  233. package/src/components/navigation-sidebar/index.js +2 -4
  234. package/src/components/save-button/index.js +2 -4
  235. package/src/components/sidebar/index.js +6 -6
  236. package/src/components/sidebar/navigation-menu-sidebar/navigation-inspector.js +6 -9
  237. package/src/components/sidebar/template-card/index.js +17 -9
  238. package/src/components/sidebar/template-card/style.scss +49 -35
  239. package/src/components/sidebar/template-card/template-actions.js +43 -0
  240. package/src/components/sidebar/template-card/template-areas.js +6 -6
  241. package/src/components/template-details/edit-template-title.js +10 -2
  242. package/src/components/template-details/index.js +4 -21
  243. package/src/components/template-details/template-areas.js +6 -6
  244. package/src/components/template-part-converter/index.js +2 -3
  245. package/src/components/test/error-boundary.js +38 -0
  246. package/src/components/url-query-controller/index.js +2 -3
  247. package/src/store/actions.js +257 -233
  248. package/src/store/selectors.js +19 -14
@@ -0,0 +1,209 @@
1
+ import _extends from "@babel/runtime/helpers/esm/extends";
2
+ import { createElement, Fragment } from "@wordpress/element";
3
+
4
+ /**
5
+ * WordPress dependencies
6
+ */
7
+ import { useState, useMemo, useEffect } from '@wordpress/element';
8
+ import { __, sprintf } from '@wordpress/i18n';
9
+ import { Button, Flex, FlexItem, Modal, SearchControl, TextHighlight, __experimentalText as Text, __experimentalHeading as Heading, __unstableComposite as Composite, __unstableUseCompositeState as useCompositeState, __unstableCompositeItem as CompositeItem } from '@wordpress/components';
10
+ import { useDebounce } from '@wordpress/compose';
11
+ import { useEntityRecords } from '@wordpress/core-data';
12
+ /**
13
+ * Internal dependencies
14
+ */
15
+
16
+ import { mapToIHasNameAndId } from './utils';
17
+ const EMPTY_ARRAY = [];
18
+
19
+ function selectSuggestion(suggestion, onSelect, entityForSuggestions) {
20
+ const {
21
+ labels,
22
+ slug,
23
+ config: {
24
+ templateSlug,
25
+ templatePrefix
26
+ }
27
+ } = entityForSuggestions;
28
+ const title = 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 or taxonomy and %2$s is the name of the post or term, e.g. "Post: Hello, WordPress", "Category: shoes"
29
+ __('%1$s: %2$s'), labels.singular_name, suggestion.name);
30
+ let newTemplateSlug = `${templateSlug || slug}-${suggestion.slug}`;
31
+
32
+ if (templatePrefix) {
33
+ newTemplateSlug = templatePrefix + newTemplateSlug;
34
+ }
35
+
36
+ const newTemplate = {
37
+ title,
38
+ description: sprintf( // translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Post: Hello, WordPress"
39
+ __('Template for %1$s'), title),
40
+ slug: newTemplateSlug
41
+ };
42
+ onSelect(newTemplate);
43
+ }
44
+
45
+ function SuggestionListItem(_ref) {
46
+ let {
47
+ suggestion,
48
+ search,
49
+ onSelect,
50
+ entityForSuggestions,
51
+ composite
52
+ } = _ref;
53
+ const baseCssClass = 'edit-site-custom-template-modal__suggestions_list__list-item';
54
+ return createElement(CompositeItem, _extends({
55
+ role: "option",
56
+ as: Button
57
+ }, composite, {
58
+ className: baseCssClass,
59
+ onClick: () => selectSuggestion(suggestion, onSelect, entityForSuggestions)
60
+ }), createElement("span", {
61
+ className: `${baseCssClass}__title`
62
+ }, createElement(TextHighlight, {
63
+ text: suggestion.name,
64
+ highlight: search
65
+ })), suggestion.link && createElement("span", {
66
+ className: `${baseCssClass}__info`
67
+ }, suggestion.link));
68
+ }
69
+
70
+ function useDebouncedInput() {
71
+ const [input, setInput] = useState('');
72
+ const [debounced, setter] = useState('');
73
+ const setDebounced = useDebounce(setter, 250);
74
+ useEffect(() => {
75
+ if (debounced !== input) {
76
+ setDebounced(input);
77
+ }
78
+ }, [debounced, input]);
79
+ return [input, setInput, debounced];
80
+ }
81
+
82
+ function useSearchSuggestions(entityForSuggestions, search) {
83
+ const {
84
+ config,
85
+ postsToExclude
86
+ } = entityForSuggestions;
87
+ const query = useMemo(() => ({
88
+ order: 'asc',
89
+ _fields: 'id,name,title,slug,link',
90
+ context: 'view',
91
+ search,
92
+ orderBy: config.getOrderBy({
93
+ search
94
+ }),
95
+ exclude: postsToExclude,
96
+ per_page: search ? 20 : 10
97
+ }), [search, config, postsToExclude]);
98
+ const {
99
+ records: searchResults,
100
+ hasResolved: searchHasResolved
101
+ } = useEntityRecords(entityForSuggestions.type, entityForSuggestions.slug, query);
102
+ const [suggestions, setSuggestions] = useState(EMPTY_ARRAY);
103
+ useEffect(() => {
104
+ if (!searchHasResolved) return;
105
+ let newSuggestions = EMPTY_ARRAY;
106
+
107
+ if (searchResults !== null && searchResults !== void 0 && searchResults.length) {
108
+ newSuggestions = searchResults;
109
+
110
+ if (config.recordNamePath) {
111
+ newSuggestions = mapToIHasNameAndId(newSuggestions, config.recordNamePath);
112
+ }
113
+ } // Update suggestions only when the query has resolved, so as to keep
114
+ // the previous results in the UI.
115
+
116
+
117
+ setSuggestions(newSuggestions);
118
+ }, [searchResults, searchHasResolved]);
119
+ return suggestions;
120
+ }
121
+
122
+ function SuggestionList(_ref2) {
123
+ let {
124
+ entityForSuggestions,
125
+ onSelect
126
+ } = _ref2;
127
+ const composite = useCompositeState({
128
+ orientation: 'vertical'
129
+ });
130
+ const [search, setSearch, debouncedSearch] = useDebouncedInput();
131
+ const suggestions = useSearchSuggestions(entityForSuggestions, debouncedSearch);
132
+ const {
133
+ labels
134
+ } = entityForSuggestions;
135
+ return createElement(Fragment, null, createElement(SearchControl, {
136
+ onChange: setSearch,
137
+ value: search,
138
+ label: labels.search_items,
139
+ placeholder: labels.search_items
140
+ }), !!(suggestions !== null && suggestions !== void 0 && suggestions.length) && createElement(Composite, _extends({}, composite, {
141
+ role: "listbox",
142
+ className: "edit-site-custom-template-modal__suggestions_list"
143
+ }), suggestions.map(suggestion => createElement(SuggestionListItem, {
144
+ key: suggestion.slug,
145
+ suggestion: suggestion,
146
+ search: debouncedSearch,
147
+ onSelect: onSelect,
148
+ entityForSuggestions: entityForSuggestions,
149
+ composite: composite
150
+ }))), debouncedSearch && !(suggestions !== null && suggestions !== void 0 && suggestions.length) && createElement("p", {
151
+ className: "edit-site-custom-template-modal__no-results"
152
+ }, labels.not_found));
153
+ }
154
+
155
+ function AddCustomTemplateModal(_ref3) {
156
+ let {
157
+ onClose,
158
+ onSelect,
159
+ entityForSuggestions
160
+ } = _ref3;
161
+ const [showSearchEntities, setShowSearchEntities] = useState(entityForSuggestions.hasGeneralTemplate);
162
+ const baseCssClass = 'edit-site-custom-template-modal';
163
+ return createElement(Modal, {
164
+ title: sprintf( // translators: %s: Name of the post type e.g: "Post".
165
+ __('Add template: %s'), entityForSuggestions.labels.singular_name),
166
+ className: baseCssClass,
167
+ closeLabel: __('Close'),
168
+ onRequestClose: onClose
169
+ }, !showSearchEntities && createElement(Fragment, null, createElement("p", null, __('Select whether to create a single template for all items or a specific one.')), createElement(Flex, {
170
+ className: `${baseCssClass}__contents`,
171
+ gap: "4",
172
+ align: "initial"
173
+ }, createElement(FlexItem, {
174
+ isBlock: true,
175
+ onClick: () => {
176
+ const {
177
+ slug,
178
+ title,
179
+ description
180
+ } = entityForSuggestions.template;
181
+ onSelect({
182
+ slug,
183
+ title,
184
+ description
185
+ });
186
+ }
187
+ }, createElement(Heading, {
188
+ level: 5
189
+ }, entityForSuggestions.labels.all_items), createElement(Text, {
190
+ as: "span"
191
+ }, // translators: The user is given the choice to set up a template for all items of a post type or taxonomy, or just a specific one.
192
+ __('For all items'))), createElement(FlexItem, {
193
+ isBlock: true,
194
+ onClick: () => {
195
+ setShowSearchEntities(true);
196
+ }
197
+ }, createElement(Heading, {
198
+ level: 5
199
+ }, entityForSuggestions.labels.singular_name), createElement(Text, {
200
+ as: "span"
201
+ }, // translators: The user is given the choice to set up a template for all items of a post type or taxonomy, or just a specific one.
202
+ __('For a specific item'))))), showSearchEntities && createElement(Fragment, null, createElement("p", null, __('This template will be used only for the specific item chosen.')), createElement(SuggestionList, {
203
+ entityForSuggestions: entityForSuggestions,
204
+ onSelect: onSelect
205
+ })));
206
+ }
207
+
208
+ export default AddCustomTemplateModal;
209
+ //# sourceMappingURL=add-custom-template-modal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/edit-site/src/components/add-new-template/add-custom-template-modal.js"],"names":["useState","useMemo","useEffect","__","sprintf","Button","Flex","FlexItem","Modal","SearchControl","TextHighlight","__experimentalText","Text","__experimentalHeading","Heading","__unstableComposite","Composite","__unstableUseCompositeState","useCompositeState","__unstableCompositeItem","CompositeItem","useDebounce","useEntityRecords","mapToIHasNameAndId","EMPTY_ARRAY","selectSuggestion","suggestion","onSelect","entityForSuggestions","labels","slug","config","templateSlug","templatePrefix","title","singular_name","name","newTemplateSlug","newTemplate","description","SuggestionListItem","search","composite","baseCssClass","link","useDebouncedInput","input","setInput","debounced","setter","setDebounced","useSearchSuggestions","postsToExclude","query","order","_fields","context","orderBy","getOrderBy","exclude","per_page","records","searchResults","hasResolved","searchHasResolved","type","suggestions","setSuggestions","newSuggestions","length","recordNamePath","SuggestionList","orientation","setSearch","debouncedSearch","search_items","map","not_found","AddCustomTemplateModal","onClose","showSearchEntities","setShowSearchEntities","hasGeneralTemplate","template","all_items"],"mappings":";;;AAAA;AACA;AACA;AACA,SAASA,QAAT,EAAmBC,OAAnB,EAA4BC,SAA5B,QAA6C,oBAA7C;AACA,SAASC,EAAT,EAAaC,OAAb,QAA4B,iBAA5B;AACA,SACCC,MADD,EAECC,IAFD,EAGCC,QAHD,EAICC,KAJD,EAKCC,aALD,EAMCC,aAND,EAOCC,kBAAkB,IAAIC,IAPvB,EAQCC,qBAAqB,IAAIC,OAR1B,EASCC,mBAAmB,IAAIC,SATxB,EAUCC,2BAA2B,IAAIC,iBAVhC,EAWCC,uBAAuB,IAAIC,aAX5B,QAYO,uBAZP;AAaA,SAASC,WAAT,QAA4B,oBAA5B;AACA,SAASC,gBAAT,QAAiC,sBAAjC;AAEA;AACA;AACA;;AACA,SAASC,kBAAT,QAAmC,SAAnC;AAEA,MAAMC,WAAW,GAAG,EAApB;;AAEA,SAASC,gBAAT,CAA2BC,UAA3B,EAAuCC,QAAvC,EAAiDC,oBAAjD,EAAwE;AACvE,QAAM;AACLC,IAAAA,MADK;AAELC,IAAAA,IAFK;AAGLC,IAAAA,MAAM,EAAE;AAAEC,MAAAA,YAAF;AAAgBC,MAAAA;AAAhB;AAHH,MAIFL,oBAJJ;AAKA,QAAMM,KAAK,GAAG9B,OAAO,EACpB;AACAD,EAAAA,EAAE,CAAE,YAAF,CAFkB,EAGpB0B,MAAM,CAACM,aAHa,EAIpBT,UAAU,CAACU,IAJS,CAArB;AAMA,MAAIC,eAAe,GAAI,GAAGL,YAAY,IAAIF,IAAM,IAAIJ,UAAU,CAACI,IAAM,EAArE;;AACA,MAAKG,cAAL,EAAsB;AACrBI,IAAAA,eAAe,GAAGJ,cAAc,GAAGI,eAAnC;AACA;;AACD,QAAMC,WAAW,GAAG;AACnBJ,IAAAA,KADmB;AAEnBK,IAAAA,WAAW,EAAEnC,OAAO,EACnB;AACAD,IAAAA,EAAE,CAAE,mBA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AAF,CAHJ,CAXD,CALD,EAuBC,cAAC,QAAD;AACC,IAAA,OAAO,MADR;AAEC,IAAA,OAAO,EAAG,MAAM;AACf8E,MAAAA,qBAAqB,CAAE,IAAF,CAArB;AACA;AAJF,KAMC,cAAC,OAAD;AAAS,IAAA,KAAK,EAAG;AAAjB,KACGrD,oBAAoB,CAACC,MAArB,CAA4BM,aAD/B,CAND,EASC,cAAC,IAAD;AAAM,IAAA,EAAE,EAAC;AAAT,KAEE;AACAhC,EAAAA,EAAE,CAAE,qBAAF,CAHJ,CATD,CAvBD,CAND,CAXF,EA2DG6E,kBAAkB,IACnB,8BACC,yBACG7E,EAAE,CACH,+DADG,CADL,CADD,EAMC,cAAC,cAAD;AACC,IAAA,oBAAoB,EAAGyB,oBADxB;AAEC,IAAA,QAAQ,EAAGD;AAFZ,IAND,CA5DF,CADD;AA2EA;;AAED,eAAemD,sBAAf","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useState, useMemo, useEffect } from '@wordpress/element';\nimport { __, sprintf } from '@wordpress/i18n';\nimport {\n\tButton,\n\tFlex,\n\tFlexItem,\n\tModal,\n\tSearchControl,\n\tTextHighlight,\n\t__experimentalText as Text,\n\t__experimentalHeading as Heading,\n\t__unstableComposite as Composite,\n\t__unstableUseCompositeState as useCompositeState,\n\t__unstableCompositeItem as CompositeItem,\n} from '@wordpress/components';\nimport { useDebounce } from '@wordpress/compose';\nimport { useEntityRecords } from '@wordpress/core-data';\n\n/**\n * Internal dependencies\n */\nimport { mapToIHasNameAndId } from './utils';\n\nconst EMPTY_ARRAY = [];\n\nfunction selectSuggestion( suggestion, onSelect, entityForSuggestions ) {\n\tconst {\n\t\tlabels,\n\t\tslug,\n\t\tconfig: { templateSlug, templatePrefix },\n\t} = entityForSuggestions;\n\tconst title = sprintf(\n\t\t// 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 or taxonomy and %2$s is the name of the post or term, e.g. \"Post: Hello, WordPress\", \"Category: shoes\"\n\t\t__( '%1$s: %2$s' ),\n\t\tlabels.singular_name,\n\t\tsuggestion.name\n\t);\n\tlet newTemplateSlug = `${ templateSlug || slug }-${ suggestion.slug }`;\n\tif ( templatePrefix ) {\n\t\tnewTemplateSlug = templatePrefix + newTemplateSlug;\n\t}\n\tconst newTemplate = {\n\t\ttitle,\n\t\tdescription: sprintf(\n\t\t\t// translators: Represents the description of a user's custom template in the Site Editor, e.g. \"Template for Post: Hello, WordPress\"\n\t\t\t__( 'Template for %1$s' ),\n\t\t\ttitle\n\t\t),\n\t\tslug: newTemplateSlug,\n\t};\n\tonSelect( newTemplate );\n}\n\nfunction SuggestionListItem( {\n\tsuggestion,\n\tsearch,\n\tonSelect,\n\tentityForSuggestions,\n\tcomposite,\n} ) {\n\tconst baseCssClass =\n\t\t'edit-site-custom-template-modal__suggestions_list__list-item';\n\treturn (\n\t\t<CompositeItem\n\t\t\trole=\"option\"\n\t\t\tas={ Button }\n\t\t\t{ ...composite }\n\t\t\tclassName={ baseCssClass }\n\t\t\tonClick={ () =>\n\t\t\t\tselectSuggestion( suggestion, onSelect, entityForSuggestions )\n\t\t\t}\n\t\t>\n\t\t\t<span className={ `${ baseCssClass }__title` }>\n\t\t\t\t<TextHighlight text={ suggestion.name } highlight={ search } />\n\t\t\t</span>\n\t\t\t{ suggestion.link && (\n\t\t\t\t<span className={ `${ baseCssClass }__info` }>\n\t\t\t\t\t{ suggestion.link }\n\t\t\t\t</span>\n\t\t\t) }\n\t\t</CompositeItem>\n\t);\n}\n\nfunction useDebouncedInput() {\n\tconst [ input, setInput ] = useState( '' );\n\tconst [ debounced, setter ] = useState( '' );\n\tconst setDebounced = useDebounce( setter, 250 );\n\tuseEffect( () => {\n\t\tif ( debounced !== input ) {\n\t\t\tsetDebounced( input );\n\t\t}\n\t}, [ debounced, input ] );\n\treturn [ input, setInput, debounced ];\n}\n\nfunction useSearchSuggestions( entityForSuggestions, search ) {\n\tconst { config, postsToExclude } = entityForSuggestions;\n\tconst query = useMemo(\n\t\t() => ( {\n\t\t\torder: 'asc',\n\t\t\t_fields: 'id,name,title,slug,link',\n\t\t\tcontext: 'view',\n\t\t\tsearch,\n\t\t\torderBy: config.getOrderBy( { search } ),\n\t\t\texclude: postsToExclude,\n\t\t\tper_page: search ? 20 : 10,\n\t\t} ),\n\t\t[ search, config, postsToExclude ]\n\t);\n\tconst { records: searchResults, hasResolved: searchHasResolved } =\n\t\tuseEntityRecords(\n\t\t\tentityForSuggestions.type,\n\t\t\tentityForSuggestions.slug,\n\t\t\tquery\n\t\t);\n\tconst [ suggestions, setSuggestions ] = useState( EMPTY_ARRAY );\n\tuseEffect( () => {\n\t\tif ( ! searchHasResolved ) return;\n\t\tlet newSuggestions = EMPTY_ARRAY;\n\t\tif ( searchResults?.length ) {\n\t\t\tnewSuggestions = searchResults;\n\t\t\tif ( config.recordNamePath ) {\n\t\t\t\tnewSuggestions = mapToIHasNameAndId(\n\t\t\t\t\tnewSuggestions,\n\t\t\t\t\tconfig.recordNamePath\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\t// Update suggestions only when the query has resolved, so as to keep\n\t\t// the previous results in the UI.\n\t\tsetSuggestions( newSuggestions );\n\t}, [ searchResults, searchHasResolved ] );\n\treturn suggestions;\n}\n\nfunction SuggestionList( { entityForSuggestions, onSelect } ) {\n\tconst composite = useCompositeState( { orientation: 'vertical' } );\n\tconst [ search, setSearch, debouncedSearch ] = useDebouncedInput();\n\tconst suggestions = useSearchSuggestions(\n\t\tentityForSuggestions,\n\t\tdebouncedSearch\n\t);\n\tconst { labels } = entityForSuggestions;\n\treturn (\n\t\t<>\n\t\t\t<SearchControl\n\t\t\t\tonChange={ setSearch }\n\t\t\t\tvalue={ search }\n\t\t\t\tlabel={ labels.search_items }\n\t\t\t\tplaceholder={ labels.search_items }\n\t\t\t/>\n\t\t\t{ !! suggestions?.length && (\n\t\t\t\t<Composite\n\t\t\t\t\t{ ...composite }\n\t\t\t\t\trole=\"listbox\"\n\t\t\t\t\tclassName=\"edit-site-custom-template-modal__suggestions_list\"\n\t\t\t\t>\n\t\t\t\t\t{ suggestions.map( ( suggestion ) => (\n\t\t\t\t\t\t<SuggestionListItem\n\t\t\t\t\t\t\tkey={ suggestion.slug }\n\t\t\t\t\t\t\tsuggestion={ suggestion }\n\t\t\t\t\t\t\tsearch={ debouncedSearch }\n\t\t\t\t\t\t\tonSelect={ onSelect }\n\t\t\t\t\t\t\tentityForSuggestions={ entityForSuggestions }\n\t\t\t\t\t\t\tcomposite={ composite }\n\t\t\t\t\t\t/>\n\t\t\t\t\t) ) }\n\t\t\t\t</Composite>\n\t\t\t) }\n\t\t\t{ debouncedSearch && ! suggestions?.length && (\n\t\t\t\t<p className=\"edit-site-custom-template-modal__no-results\">\n\t\t\t\t\t{ labels.not_found }\n\t\t\t\t</p>\n\t\t\t) }\n\t\t</>\n\t);\n}\n\nfunction AddCustomTemplateModal( { onClose, onSelect, entityForSuggestions } ) {\n\tconst [ showSearchEntities, setShowSearchEntities ] = useState(\n\t\tentityForSuggestions.hasGeneralTemplate\n\t);\n\tconst baseCssClass = 'edit-site-custom-template-modal';\n\treturn (\n\t\t<Modal\n\t\t\ttitle={ sprintf(\n\t\t\t\t// translators: %s: Name of the post type e.g: \"Post\".\n\t\t\t\t__( 'Add template: %s' ),\n\t\t\t\tentityForSuggestions.labels.singular_name\n\t\t\t) }\n\t\t\tclassName={ baseCssClass }\n\t\t\tcloseLabel={ __( 'Close' ) }\n\t\t\tonRequestClose={ onClose }\n\t\t>\n\t\t\t{ ! showSearchEntities && (\n\t\t\t\t<>\n\t\t\t\t\t<p>\n\t\t\t\t\t\t{ __(\n\t\t\t\t\t\t\t'Select whether to create a single template for all items or a specific one.'\n\t\t\t\t\t\t) }\n\t\t\t\t\t</p>\n\t\t\t\t\t<Flex\n\t\t\t\t\t\tclassName={ `${ baseCssClass }__contents` }\n\t\t\t\t\t\tgap=\"4\"\n\t\t\t\t\t\talign=\"initial\"\n\t\t\t\t\t>\n\t\t\t\t\t\t<FlexItem\n\t\t\t\t\t\t\tisBlock\n\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\tconst { slug, title, description } =\n\t\t\t\t\t\t\t\t\tentityForSuggestions.template;\n\t\t\t\t\t\t\t\tonSelect( { slug, title, description } );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t<Heading level={ 5 }>\n\t\t\t\t\t\t\t\t{ entityForSuggestions.labels.all_items }\n\t\t\t\t\t\t\t</Heading>\n\t\t\t\t\t\t\t<Text as=\"span\">\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t// translators: The user is given the choice to set up a template for all items of a post type or taxonomy, or just a specific one.\n\t\t\t\t\t\t\t\t\t__( 'For all items' )\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t</Text>\n\t\t\t\t\t\t</FlexItem>\n\t\t\t\t\t\t<FlexItem\n\t\t\t\t\t\t\tisBlock\n\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\tsetShowSearchEntities( true );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t<Heading level={ 5 }>\n\t\t\t\t\t\t\t\t{ entityForSuggestions.labels.singular_name }\n\t\t\t\t\t\t\t</Heading>\n\t\t\t\t\t\t\t<Text as=\"span\">\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t// translators: The user is given the choice to set up a template for all items of a post type or taxonomy, or just a specific one.\n\t\t\t\t\t\t\t\t\t__( 'For a specific item' )\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t</Text>\n\t\t\t\t\t\t</FlexItem>\n\t\t\t\t\t</Flex>\n\t\t\t\t</>\n\t\t\t) }\n\t\t\t{ showSearchEntities && (\n\t\t\t\t<>\n\t\t\t\t\t<p>\n\t\t\t\t\t\t{ __(\n\t\t\t\t\t\t\t'This template will be used only for the specific item chosen.'\n\t\t\t\t\t\t) }\n\t\t\t\t\t</p>\n\t\t\t\t\t<SuggestionList\n\t\t\t\t\t\tentityForSuggestions={ entityForSuggestions }\n\t\t\t\t\t\tonSelect={ onSelect }\n\t\t\t\t\t/>\n\t\t\t\t</>\n\t\t\t) }\n\t\t</Modal>\n\t);\n}\n\nexport default AddCustomTemplateModal;\n"]}
@@ -1,24 +1,22 @@
1
- import { createElement } from "@wordpress/element";
1
+ import { createElement, Fragment } from "@wordpress/element";
2
2
 
3
- /**
4
- * External dependencies
5
- */
6
- import { filter, includes, map } from 'lodash';
7
3
  /**
8
4
  * WordPress dependencies
9
5
  */
10
-
11
6
  import { DropdownMenu, MenuGroup, MenuItem, NavigableMenu } from '@wordpress/components';
12
- import { useSelect, useDispatch } from '@wordpress/data';
7
+ import { useState } from '@wordpress/element';
8
+ import { useDispatch } from '@wordpress/data';
13
9
  import { store as coreStore } from '@wordpress/core-data';
14
- import { store as editorStore } from '@wordpress/editor';
15
- import { archive, blockMeta, category, home, list, media, notFound, page, post, postAuthor, postDate, search, tag } from '@wordpress/icons';
10
+ import { archive, blockMeta, category, home, list, media, notFound, page, post, postAuthor, postDate, search, tag, layout as customGenericTemplateIcon } from '@wordpress/icons';
16
11
  import { __ } from '@wordpress/i18n';
17
12
  import { store as noticesStore } from '@wordpress/notices';
18
13
  /**
19
14
  * Internal dependencies
20
15
  */
21
16
 
17
+ import AddCustomTemplateModal from './add-custom-template-modal';
18
+ import { useExistingTemplates, useDefaultTemplateTypes, entitiesConfig, usePostTypes, usePostTypePage, useTaxonomies, useTaxonomyCategory, useTaxonomyTag, useExtraTemplates } from './utils';
19
+ import AddCustomGenericTemplateModal from './add-custom-generic-template-modal';
22
20
  import { useHistory } from '../routes';
23
21
  import { store as editSiteStore } from '../../store';
24
22
  const DEFAULT_TEMPLATE_SLUGS = ['front-page', 'single', 'page', 'index', 'archive', 'author', 'category', 'date', 'tag', 'taxonomy', 'search', '404'];
@@ -41,16 +39,10 @@ export default function NewTemplate(_ref) {
41
39
  let {
42
40
  postType
43
41
  } = _ref;
42
+ const [showCustomTemplateModal, setShowCustomTemplateModal] = useState(false);
43
+ const [showCustomGenericTemplateModal, setShowCustomGenericTemplateModal] = useState(false);
44
+ const [entityForSuggestions, setEntityForSuggestions] = useState({});
44
45
  const history = useHistory();
45
- const {
46
- templates,
47
- defaultTemplateTypes
48
- } = useSelect(select => ({
49
- templates: select(coreStore).getEntityRecords('postType', 'wp_template', {
50
- per_page: -1
51
- }),
52
- defaultTemplateTypes: select(editorStore).__experimentalGetDefaultTemplateTypes()
53
- }), []);
54
46
  const {
55
47
  saveEntityRecord
56
48
  } = useDispatch(coreStore);
@@ -62,6 +54,8 @@ export default function NewTemplate(_ref) {
62
54
  } = useDispatch(editSiteStore);
63
55
 
64
56
  async function createTemplate(template) {
57
+ let isWPSuggestion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
58
+
65
59
  try {
66
60
  const {
67
61
  title,
@@ -75,7 +69,7 @@ export default function NewTemplate(_ref) {
75
69
  status: 'publish',
76
70
  title,
77
71
  // This adds a post meta field in template that is part of `is_custom` value calculation.
78
- is_wp_suggestion: true
72
+ is_wp_suggestion: isWPSuggestion
79
73
  }, {
80
74
  throwOnError: true
81
75
  }); // Set template before navigating away to avoid initial stale value.
@@ -94,18 +88,13 @@ export default function NewTemplate(_ref) {
94
88
  }
95
89
  }
96
90
 
97
- const existingTemplateSlugs = map(templates, 'slug');
98
- const missingTemplates = filter(defaultTemplateTypes, template => includes(DEFAULT_TEMPLATE_SLUGS, template.slug) && !includes(existingTemplateSlugs, template.slug));
91
+ const missingTemplates = useMissingTemplates(setEntityForSuggestions, setShowCustomTemplateModal);
99
92
 
100
93
  if (!missingTemplates.length) {
101
94
  return null;
102
- } // Update the sort order to match the DEFAULT_TEMPLATE_SLUGS order.
103
-
95
+ }
104
96
 
105
- missingTemplates.sort((template1, template2) => {
106
- return DEFAULT_TEMPLATE_SLUGS.indexOf(template1.slug) - DEFAULT_TEMPLATE_SLUGS.indexOf(template2.slug);
107
- });
108
- return createElement(DropdownMenu, {
97
+ return createElement(Fragment, null, createElement(DropdownMenu, {
109
98
  className: "edit-site-new-template-dropdown",
110
99
  icon: null,
111
100
  text: postType.labels.add_new,
@@ -120,21 +109,91 @@ export default function NewTemplate(_ref) {
120
109
  className: "edit-site-new-template-dropdown__popover"
121
110
  }, createElement(MenuGroup, {
122
111
  label: postType.labels.add_new_item
123
- }, map(missingTemplates, template => {
112
+ }, missingTemplates.map(template => {
124
113
  const {
125
114
  title,
126
115
  description,
127
- slug
116
+ slug,
117
+ onClick,
118
+ icon
128
119
  } = template;
129
120
  return createElement(MenuItem, {
130
- icon: TEMPLATE_ICONS[slug],
121
+ icon: icon || TEMPLATE_ICONS[slug] || post,
131
122
  iconPosition: "left",
132
123
  info: description,
133
124
  key: slug,
134
- onClick: () => {
135
- createTemplate(template); // We will be navigated way so no need to close the dropdown.
136
- }
125
+ onClick: () => onClick ? onClick(template) : createTemplate(template)
137
126
  }, title);
138
- }))));
127
+ })), createElement(MenuGroup, null, createElement(MenuItem, {
128
+ icon: customGenericTemplateIcon,
129
+ iconPosition: "left",
130
+ info: __('Custom templates can be applied to any post or page.'),
131
+ key: "custom-template",
132
+ onClick: () => setShowCustomGenericTemplateModal(true)
133
+ }, __('Custom template'))))), showCustomTemplateModal && createElement(AddCustomTemplateModal, {
134
+ onClose: () => setShowCustomTemplateModal(false),
135
+ onSelect: createTemplate,
136
+ entityForSuggestions: entityForSuggestions
137
+ }), showCustomGenericTemplateModal && createElement(AddCustomGenericTemplateModal, {
138
+ onClose: () => setShowCustomGenericTemplateModal(false),
139
+ createTemplate: createTemplate
140
+ }));
141
+ }
142
+
143
+ function useMissingTemplates(setEntityForSuggestions, setShowCustomTemplateModal) {
144
+ const postTypes = usePostTypes();
145
+ const pagePostType = usePostTypePage();
146
+ const taxonomies = useTaxonomies();
147
+ const categoryTaxonomy = useTaxonomyCategory();
148
+ const tagTaxonomy = useTaxonomyTag();
149
+ const existingTemplates = useExistingTemplates();
150
+ const defaultTemplateTypes = useDefaultTemplateTypes();
151
+ const existingTemplateSlugs = (existingTemplates || []).map(_ref2 => {
152
+ let {
153
+ slug
154
+ } = _ref2;
155
+ return slug;
156
+ });
157
+ const missingDefaultTemplates = (defaultTemplateTypes || []).filter(template => DEFAULT_TEMPLATE_SLUGS.includes(template.slug) && !existingTemplateSlugs.includes(template.slug));
158
+
159
+ const onClickMenuItem = _entityForSuggestions => {
160
+ setShowCustomTemplateModal(true);
161
+ setEntityForSuggestions(_entityForSuggestions);
162
+ }; // TODO: find better names for these variables. `useExtraTemplates` returns an array of items.
163
+
164
+
165
+ const categoryMenuItem = useExtraTemplates(categoryTaxonomy, entitiesConfig.category, onClickMenuItem);
166
+ const tagMenuItem = useExtraTemplates(tagTaxonomy, entitiesConfig.tag, onClickMenuItem);
167
+ const pageMenuItem = useExtraTemplates(pagePostType, entitiesConfig.page, onClickMenuItem); // We need to replace existing default template types with
168
+ // the create specific template functionality. The original
169
+ // info (title, description, etc.) is preserved in the
170
+ // `useExtraTemplates` hook.
171
+
172
+ const enhancedMissingDefaultTemplateTypes = [...missingDefaultTemplates];
173
+ [categoryMenuItem, tagMenuItem, pageMenuItem].forEach(menuItem => {
174
+ if (!(menuItem !== null && menuItem !== void 0 && menuItem.length)) {
175
+ return;
176
+ }
177
+
178
+ const matchIndex = enhancedMissingDefaultTemplateTypes.findIndex(template => template.slug === menuItem[0].slug); // Some default template types might have been filtered above from
179
+ // `missingDefaultTemplates` because they only check for the general
180
+ // template. So here we either replace or append the item, augmented
181
+ // with the check if it has available specific item to create a
182
+ // template for.
183
+
184
+ if (matchIndex > -1) {
185
+ enhancedMissingDefaultTemplateTypes.splice(matchIndex, 1, menuItem[0]);
186
+ } else {
187
+ enhancedMissingDefaultTemplateTypes.push(menuItem[0]);
188
+ }
189
+ }); // Update the sort order to match the DEFAULT_TEMPLATE_SLUGS order.
190
+
191
+ enhancedMissingDefaultTemplateTypes === null || enhancedMissingDefaultTemplateTypes === void 0 ? void 0 : enhancedMissingDefaultTemplateTypes.sort((template1, template2) => {
192
+ return DEFAULT_TEMPLATE_SLUGS.indexOf(template1.slug) - DEFAULT_TEMPLATE_SLUGS.indexOf(template2.slug);
193
+ });
194
+ const extraPostTypeTemplates = useExtraTemplates(postTypes, entitiesConfig.postType, onClickMenuItem);
195
+ const extraTaxonomyTemplates = useExtraTemplates(taxonomies, entitiesConfig.taxonomy, onClickMenuItem);
196
+ const missingTemplates = [...enhancedMissingDefaultTemplateTypes, ...extraPostTypeTemplates, ...extraTaxonomyTemplates];
197
+ return missingTemplates;
139
198
  }
140
199
  //# sourceMappingURL=new-template.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/edit-site/src/components/add-new-template/new-template.js"],"names":["filter","includes","map","DropdownMenu","MenuGroup","MenuItem","NavigableMenu","useSelect","useDispatch","store","coreStore","editorStore","archive","blockMeta","category","home","list","media","notFound","page","post","postAuthor","postDate","search","tag","__","noticesStore","useHistory","editSiteStore","DEFAULT_TEMPLATE_SLUGS","TEMPLATE_ICONS","single","index","author","taxonomy","date","attachment","NewTemplate","postType","history","templates","defaultTemplateTypes","select","getEntityRecords","per_page","__experimentalGetDefaultTemplateTypes","saveEntityRecord","createErrorNotice","setTemplate","createTemplate","template","title","description","slug","newTemplate","toString","status","is_wp_suggestion","throwOnError","id","push","postId","type","error","errorMessage","message","code","existingTemplateSlugs","missingTemplates","length","sort","template1","template2","indexOf","labels","add_new","add_new_item","noArrow","variant"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,MAAT,EAAiBC,QAAjB,EAA2BC,GAA3B,QAAsC,QAAtC;AAEA;AACA;AACA;;AACA,SACCC,YADD,EAECC,SAFD,EAGCC,QAHD,EAICC,aAJD,QAKO,uBALP;AAMA,SAASC,SAAT,EAAoBC,WAApB,QAAuC,iBAAvC;AACA,SAASC,KAAK,IAAIC,SAAlB,QAAmC,sBAAnC;AACA,SAASD,KAAK,IAAIE,WAAlB,QAAqC,mBAArC;AACA,SACCC,OADD,EAECC,SAFD,EA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lB,QAAF,IAAgB;AACxC,UAAM;AAAEC,MAAAA,KAAF;AAASC,MAAAA,WAAT;AAAsBC,MAAAA;AAAtB,QAA+BH,QAArC;AACA,WACC,cAAC,QAAD;AACC,MAAA,IAAI,EAAGpB,cAAc,CAAEuB,IAAF,CADtB;AAEC,MAAA,YAAY,EAAC,MAFd;AAGC,MAAA,IAAI,EAAGD,WAHR;AAIC,MAAA,GAAG,EAAGC,IAJP;AAKC,MAAA,OAAO,EAAG,MAAM;AACfJ,QAAAA,cAAc,CAAEC,QAAF,CAAd,CADe,CAEf;AACA;AARF,OAUGC,KAVH,CADD;AAcA,GAhBI,CADN,CADD,CAbF,CADD;AAsCA","sourcesContent":["/**\n * External dependencies\n */\nimport { filter, includes, map } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tDropdownMenu,\n\tMenuGroup,\n\tMenuItem,\n\tNavigableMenu,\n} from '@wordpress/components';\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 { __ } from '@wordpress/i18n';\nimport { store as noticesStore } from '@wordpress/notices';\n\n/**\n * Internal dependencies\n */\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 { templates, defaultTemplateTypes } = useSelect(\n\t\t( select ) => ( {\n\t\t\ttemplates: 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: select(\n\t\t\t\teditorStore\n\t\t\t).__experimentalGetDefaultTemplateTypes(),\n\t\t} ),\n\t\t[]\n\t);\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\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\n\tconst existingTemplateSlugs = map( templates, 'slug' );\n\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\tif ( ! missingTemplates.length ) {\n\t\treturn null;\n\t}\n\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\n\treturn (\n\t\t<DropdownMenu\n\t\t\tclassName=\"edit-site-new-template-dropdown\"\n\t\t\ticon={ null }\n\t\t\ttext={ postType.labels.add_new }\n\t\t\tlabel={ postType.labels.add_new_item }\n\t\t\tpopoverProps={ {\n\t\t\t\tnoArrow: false,\n\t\t\t} }\n\t\t\ttoggleProps={ {\n\t\t\t\tvariant: 'primary',\n\t\t\t} }\n\t\t>\n\t\t\t{ () => (\n\t\t\t\t<NavigableMenu className=\"edit-site-new-template-dropdown__popover\">\n\t\t\t\t\t<MenuGroup label={ postType.labels.add_new_item }>\n\t\t\t\t\t\t{ map( missingTemplates, ( template ) => {\n\t\t\t\t\t\t\tconst { title, description, slug } = template;\n\t\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\t\t<MenuItem\n\t\t\t\t\t\t\t\t\ticon={ TEMPLATE_ICONS[ slug ] }\n\t\t\t\t\t\t\t\t\ticonPosition=\"left\"\n\t\t\t\t\t\t\t\t\tinfo={ description }\n\t\t\t\t\t\t\t\t\tkey={ slug }\n\t\t\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\t\t\tcreateTemplate( template );\n\t\t\t\t\t\t\t\t\t\t// We will be navigated way so no need to close the dropdown.\n\t\t\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t{ title }\n\t\t\t\t\t\t\t\t</MenuItem>\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t} ) }\n\t\t\t\t\t</MenuGroup>\n\t\t\t\t</NavigableMenu>\n\t\t\t) }\n\t\t</DropdownMenu>\n\t);\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/edit-site/src/components/add-new-template/new-template.js"],"names":["DropdownMenu","MenuGroup","MenuItem","NavigableMenu","useState","useDispatch","store","coreStore","archive","blockMeta","category","home","list","media","notFound","page","post","postAuthor","postDate","search","tag","layout","customGenericTemplateIcon","__","noticesStore","AddCustomTemplateModal","useExistingTemplates","useDefaultTemplateTypes","entitiesConfig","usePostTypes","usePostTypePage","useTaxonomies","useTaxonomyCategory","useTaxonomyTag","useExtraTemplates","AddCustomGenericTemplateModal","useHistory","editSiteStore","DEFAULT_TEMPLATE_SLUGS","TEMPLATE_ICONS","single","index","author","taxonomy","date","attachment","NewTemplate","postType","showCustomTemplateModal","setShowCustomTemplateModal","showCustomGenericTemplateModal","setShowCustomGenericTemplateModal","entityForSuggestions","setEntityForSuggestions","history","saveEntityRecord","createErrorNotice","setTemplate","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","postTypes","pagePostType","taxonomies","categoryTaxonomy","tagTaxonomy","existingTemplates","defaultTemplateTypes","existingTemplateSlugs","missingDefaultTemplates","filter","includes","onClickMenuItem","_entityForSuggestions","categoryMenuItem","tagMenuItem","pageMenuItem","enhancedMissingDefaultTemplateTypes","forEach","menuItem","matchIndex","findIndex","splice","sort","template1","template2","indexOf","extraPostTypeTemplates","extraTaxonomyTemplates"],"mappings":";;AAAA;AACA;AACA;AACA,SACCA,YADD,EAECC,SAFD,EAGCC,QAHD,EAICC,aAJD,QAKO,uBALP;AAMA,SAASC,Q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,QAAF,IAAgBA,QAAQ,CAACI,IAAT,KAAkB2C,QAAQ,CAAE,CAAF,CAAR,CAAc3C,IAD9B,CAAnB,CAJwE,CAOxE;AACA;AACA;AACA;AACA;;AACA,QAAK4C,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,CAAClC,IAApC,CAA0CoC,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,WACC1E,sBAAsB,CAAC2E,OAAvB,CAAgCF,SAAS,CAAChD,IAA1C,IACAzB,sBAAsB,CAAC2E,OAAvB,CAAgCD,SAAS,CAACjD,IAA1C,CAFD;AAIA,GALD;AAMA,QAAMmD,sBAAsB,GAAGhF,iBAAiB,CAC/CsD,SAD+C,EAE/C5D,cAAc,CAACmB,QAFgC,EAG/CoD,eAH+C,CAAhD;AAKA,QAAMgB,sBAAsB,GAAGjF,iBAAiB,CAC/CwD,UAD+C,EAE/C9D,cAAc,CAACe,QAFgC,EAG/CwD,eAH+C,CAAhD;AAKA,QAAMtB,gBAAgB,GAAG,CACxB,GAAG2B,mCADqB,EAExB,GAAGU,sBAFqB,EAGxB,GAAGC,sBAHqB,CAAzB;AAKA,SAAOtC,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"]}