@wordpress/edit-site 4.6.0 → 4.9.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 (205) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/build/components/add-new-template/add-custom-template-modal.js +185 -0
  3. package/build/components/add-new-template/add-custom-template-modal.js.map +1 -0
  4. package/build/components/add-new-template/new-template.js +92 -32
  5. package/build/components/add-new-template/new-template.js.map +1 -1
  6. package/build/components/add-new-template/utils.js +139 -0
  7. package/build/components/add-new-template/utils.js.map +1 -0
  8. package/build/components/block-editor/block-inspector-button.js.map +1 -1
  9. package/build/components/block-editor/index.js.map +1 -1
  10. package/build/components/code-editor/code-editor-text-area.js +11 -9
  11. package/build/components/code-editor/code-editor-text-area.js.map +1 -1
  12. package/build/components/edit-template-part-menu-button/index.js.map +1 -1
  13. package/build/components/editor/index.js.map +1 -1
  14. package/build/components/global-styles/context-menu.js +6 -3
  15. package/build/components/global-styles/context-menu.js.map +1 -1
  16. package/build/components/global-styles/global-styles-provider.js.map +1 -1
  17. package/build/components/global-styles/gradients-palette-panel.js +3 -7
  18. package/build/components/global-styles/gradients-palette-panel.js.map +1 -1
  19. package/build/components/global-styles/hooks.js +1 -1
  20. package/build/components/global-styles/hooks.js.map +1 -1
  21. package/build/components/global-styles/palette.js +2 -1
  22. package/build/components/global-styles/palette.js.map +1 -1
  23. package/build/components/global-styles/preview.js +13 -2
  24. package/build/components/global-styles/preview.js.map +1 -1
  25. package/build/components/global-styles/screen-block-list.js +4 -1
  26. package/build/components/global-styles/screen-block-list.js.map +1 -1
  27. package/build/components/global-styles/screen-button-color.js +80 -0
  28. package/build/components/global-styles/screen-button-color.js.map +1 -0
  29. package/build/components/global-styles/screen-colors.js +47 -7
  30. package/build/components/global-styles/screen-colors.js.map +1 -1
  31. package/build/components/global-styles/screen-root.js +4 -2
  32. package/build/components/global-styles/screen-root.js.map +1 -1
  33. package/build/components/global-styles/screen-style-variations.js +9 -1
  34. package/build/components/global-styles/screen-style-variations.js.map +1 -1
  35. package/build/components/global-styles/screen-typography-element.js +4 -0
  36. package/build/components/global-styles/screen-typography-element.js.map +1 -1
  37. package/build/components/global-styles/screen-typography.js +9 -1
  38. package/build/components/global-styles/screen-typography.js.map +1 -1
  39. package/build/components/global-styles/ui.js +11 -0
  40. package/build/components/global-styles/ui.js.map +1 -1
  41. package/build/components/global-styles/use-global-styles-output.js +40 -9
  42. package/build/components/global-styles/use-global-styles-output.js.map +1 -1
  43. package/build/components/global-styles/utils.js +3 -1
  44. package/build/components/global-styles/utils.js.map +1 -1
  45. package/build/components/header/index.js +28 -10
  46. package/build/components/header/index.js.map +1 -1
  47. package/build/components/header/mode-switcher/index.js.map +1 -1
  48. package/build/components/header/more-menu/copy-content-menu-item.js +1 -1
  49. package/build/components/header/more-menu/copy-content-menu-item.js.map +1 -1
  50. package/build/components/header/more-menu/site-export.js +4 -1
  51. package/build/components/header/more-menu/site-export.js.map +1 -1
  52. package/build/components/header/undo-redo/redo.js +13 -4
  53. package/build/components/header/undo-redo/redo.js.map +1 -1
  54. package/build/components/header/undo-redo/undo.js +13 -4
  55. package/build/components/header/undo-redo/undo.js.map +1 -1
  56. package/build/components/keyboard-shortcut-help-modal/config.js +17 -0
  57. package/build/components/keyboard-shortcut-help-modal/config.js.map +1 -1
  58. package/build/components/keyboard-shortcuts/index.js.map +1 -1
  59. package/build/components/list/actions/index.js.map +1 -1
  60. package/build/components/list/actions/rename-menu-item.js.map +1 -1
  61. package/build/components/list/added-by.js.map +1 -1
  62. package/build/components/navigation-sidebar/index.js.map +1 -1
  63. package/build/components/save-button/index.js.map +1 -1
  64. package/build/components/sidebar/index.js.map +1 -1
  65. package/build/components/sidebar/navigation-menu-sidebar/navigation-inspector.js.map +1 -1
  66. package/build/components/sidebar/template-card/index.js +19 -7
  67. package/build/components/sidebar/template-card/index.js.map +1 -1
  68. package/build/components/sidebar/template-card/template-actions.js +64 -0
  69. package/build/components/sidebar/template-card/template-actions.js.map +1 -0
  70. package/build/components/sidebar/template-card/template-areas.js.map +1 -1
  71. package/build/components/template-details/template-areas.js.map +1 -1
  72. package/build/components/template-part-converter/index.js.map +1 -1
  73. package/build/components/url-query-controller/index.js.map +1 -1
  74. package/build/store/actions.js.map +1 -1
  75. package/build/store/selectors.js.map +1 -1
  76. package/build-module/components/add-new-template/add-custom-template-modal.js +170 -0
  77. package/build-module/components/add-new-template/add-custom-template-modal.js.map +1 -0
  78. package/build-module/components/add-new-template/new-template.js +92 -35
  79. package/build-module/components/add-new-template/new-template.js.map +1 -1
  80. package/build-module/components/add-new-template/utils.js +119 -0
  81. package/build-module/components/add-new-template/utils.js.map +1 -0
  82. package/build-module/components/block-editor/block-inspector-button.js.map +1 -1
  83. package/build-module/components/block-editor/index.js.map +1 -1
  84. package/build-module/components/code-editor/code-editor-text-area.js +12 -10
  85. package/build-module/components/code-editor/code-editor-text-area.js.map +1 -1
  86. package/build-module/components/edit-template-part-menu-button/index.js.map +1 -1
  87. package/build-module/components/editor/index.js.map +1 -1
  88. package/build-module/components/global-styles/context-menu.js +6 -3
  89. package/build-module/components/global-styles/context-menu.js.map +1 -1
  90. package/build-module/components/global-styles/global-styles-provider.js.map +1 -1
  91. package/build-module/components/global-styles/gradients-palette-panel.js +3 -5
  92. package/build-module/components/global-styles/gradients-palette-panel.js.map +1 -1
  93. package/build-module/components/global-styles/hooks.js +1 -1
  94. package/build-module/components/global-styles/hooks.js.map +1 -1
  95. package/build-module/components/global-styles/palette.js +2 -1
  96. package/build-module/components/global-styles/palette.js.map +1 -1
  97. package/build-module/components/global-styles/preview.js +14 -3
  98. package/build-module/components/global-styles/preview.js.map +1 -1
  99. package/build-module/components/global-styles/screen-block-list.js +4 -1
  100. package/build-module/components/global-styles/screen-block-list.js.map +1 -1
  101. package/build-module/components/global-styles/screen-button-color.js +67 -0
  102. package/build-module/components/global-styles/screen-button-color.js.map +1 -0
  103. package/build-module/components/global-styles/screen-colors.js +48 -8
  104. package/build-module/components/global-styles/screen-colors.js.map +1 -1
  105. package/build-module/components/global-styles/screen-root.js +4 -2
  106. package/build-module/components/global-styles/screen-root.js.map +1 -1
  107. package/build-module/components/global-styles/screen-style-variations.js +9 -1
  108. package/build-module/components/global-styles/screen-style-variations.js.map +1 -1
  109. package/build-module/components/global-styles/screen-typography-element.js +4 -0
  110. package/build-module/components/global-styles/screen-typography-element.js.map +1 -1
  111. package/build-module/components/global-styles/screen-typography.js +10 -2
  112. package/build-module/components/global-styles/screen-typography.js.map +1 -1
  113. package/build-module/components/global-styles/ui.js +10 -0
  114. package/build-module/components/global-styles/ui.js.map +1 -1
  115. package/build-module/components/global-styles/use-global-styles-output.js +39 -9
  116. package/build-module/components/global-styles/use-global-styles-output.js.map +1 -1
  117. package/build-module/components/global-styles/utils.js +3 -1
  118. package/build-module/components/global-styles/utils.js.map +1 -1
  119. package/build-module/components/header/index.js +29 -11
  120. package/build-module/components/header/index.js.map +1 -1
  121. package/build-module/components/header/mode-switcher/index.js.map +1 -1
  122. package/build-module/components/header/more-menu/copy-content-menu-item.js +1 -1
  123. package/build-module/components/header/more-menu/copy-content-menu-item.js.map +1 -1
  124. package/build-module/components/header/more-menu/site-export.js +4 -1
  125. package/build-module/components/header/more-menu/site-export.js.map +1 -1
  126. package/build-module/components/header/undo-redo/redo.js +9 -3
  127. package/build-module/components/header/undo-redo/redo.js.map +1 -1
  128. package/build-module/components/header/undo-redo/undo.js +9 -3
  129. package/build-module/components/header/undo-redo/undo.js.map +1 -1
  130. package/build-module/components/keyboard-shortcut-help-modal/config.js +17 -0
  131. package/build-module/components/keyboard-shortcut-help-modal/config.js.map +1 -1
  132. package/build-module/components/keyboard-shortcuts/index.js.map +1 -1
  133. package/build-module/components/list/actions/index.js.map +1 -1
  134. package/build-module/components/list/actions/rename-menu-item.js.map +1 -1
  135. package/build-module/components/list/added-by.js.map +1 -1
  136. package/build-module/components/navigation-sidebar/index.js.map +1 -1
  137. package/build-module/components/save-button/index.js.map +1 -1
  138. package/build-module/components/sidebar/index.js.map +1 -1
  139. package/build-module/components/sidebar/navigation-menu-sidebar/navigation-inspector.js.map +1 -1
  140. package/build-module/components/sidebar/template-card/index.js +18 -7
  141. package/build-module/components/sidebar/template-card/index.js.map +1 -1
  142. package/build-module/components/sidebar/template-card/template-actions.js +49 -0
  143. package/build-module/components/sidebar/template-card/template-actions.js.map +1 -0
  144. package/build-module/components/sidebar/template-card/template-areas.js.map +1 -1
  145. package/build-module/components/template-details/template-areas.js.map +1 -1
  146. package/build-module/components/template-part-converter/index.js.map +1 -1
  147. package/build-module/components/url-query-controller/index.js.map +1 -1
  148. package/build-module/store/actions.js.map +1 -1
  149. package/build-module/store/selectors.js.map +1 -1
  150. package/build-style/style-rtl.css +170 -23
  151. package/build-style/style.css +170 -23
  152. package/package.json +30 -30
  153. package/src/components/add-new-template/add-custom-template-modal.js +231 -0
  154. package/src/components/add-new-template/new-template.js +135 -59
  155. package/src/components/add-new-template/style.scss +116 -0
  156. package/src/components/add-new-template/utils.js +125 -0
  157. package/src/components/block-editor/block-inspector-button.js +2 -3
  158. package/src/components/block-editor/index.js +4 -9
  159. package/src/components/code-editor/code-editor-text-area.js +12 -7
  160. package/src/components/edit-template-part-menu-button/index.js +2 -3
  161. package/src/components/editor/index.js +4 -5
  162. package/src/components/global-styles/context-menu.js +3 -0
  163. package/src/components/global-styles/global-styles-provider.js +4 -8
  164. package/src/components/global-styles/gradients-palette-panel.js +2 -5
  165. package/src/components/global-styles/hooks.js +5 -3
  166. package/src/components/global-styles/palette.js +4 -1
  167. package/src/components/global-styles/preview.js +17 -2
  168. package/src/components/global-styles/screen-block-list.js +14 -5
  169. package/src/components/global-styles/screen-button-color.js +102 -0
  170. package/src/components/global-styles/screen-colors.js +49 -4
  171. package/src/components/global-styles/screen-root.js +12 -5
  172. package/src/components/global-styles/screen-style-variations.js +10 -4
  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 +10 -0
  176. package/src/components/global-styles/test/use-global-styles-output.js +82 -16
  177. package/src/components/global-styles/ui.js +13 -0
  178. package/src/components/global-styles/use-global-styles-output.js +43 -4
  179. package/src/components/global-styles/utils.js +3 -0
  180. package/src/components/header/index.js +38 -13
  181. package/src/components/header/mode-switcher/index.js +4 -4
  182. package/src/components/header/more-menu/copy-content-menu-item.js +3 -4
  183. package/src/components/header/more-menu/site-export.js +5 -3
  184. package/src/components/header/style.scss +53 -5
  185. package/src/components/header/undo-redo/redo.js +6 -1
  186. package/src/components/header/undo-redo/undo.js +6 -1
  187. package/src/components/keyboard-shortcut-help-modal/config.js +12 -0
  188. package/src/components/keyboard-shortcuts/index.js +6 -10
  189. package/src/components/list/actions/index.js +2 -3
  190. package/src/components/list/actions/rename-menu-item.js +4 -6
  191. package/src/components/list/added-by.js +4 -3
  192. package/src/components/navigation-sidebar/index.js +2 -4
  193. package/src/components/save-button/index.js +2 -4
  194. package/src/components/sidebar/index.js +6 -6
  195. package/src/components/sidebar/navigation-menu-sidebar/navigation-inspector.js +6 -9
  196. package/src/components/sidebar/navigation-menu-sidebar/style.scss +0 -1
  197. package/src/components/sidebar/template-card/index.js +17 -9
  198. package/src/components/sidebar/template-card/style.scss +49 -35
  199. package/src/components/sidebar/template-card/template-actions.js +43 -0
  200. package/src/components/sidebar/template-card/template-areas.js +6 -6
  201. package/src/components/template-details/template-areas.js +6 -6
  202. package/src/components/template-part-converter/index.js +2 -3
  203. package/src/components/url-query-controller/index.js +2 -3
  204. package/src/store/actions.js +257 -233
  205. package/src/store/selectors.js +9 -10
package/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 4.9.0 (2022-06-29)
6
+
7
+ ## 4.8.0 (2022-06-15)
8
+
9
+ ## 4.7.0 (2022-06-01)
10
+
5
11
  ## 4.6.0 (2022-05-18)
6
12
 
7
13
  ## 4.5.0 (2022-05-04)
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+
10
+ var _element = require("@wordpress/element");
11
+
12
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
+
14
+ var _i18n = require("@wordpress/i18n");
15
+
16
+ var _components = require("@wordpress/components");
17
+
18
+ var _compose = require("@wordpress/compose");
19
+
20
+ var _coreData = require("@wordpress/core-data");
21
+
22
+ var _utils = require("./utils");
23
+
24
+ /**
25
+ * WordPress dependencies
26
+ */
27
+
28
+ /**
29
+ * Internal dependencies
30
+ */
31
+ const EMPTY_ARRAY = [];
32
+ const BASE_QUERY = {
33
+ order: 'asc',
34
+ _fields: 'id,title,slug,link',
35
+ context: 'view'
36
+ };
37
+
38
+ function SuggestionListItem(_ref) {
39
+ let {
40
+ suggestion,
41
+ search,
42
+ onSelect,
43
+ entityForSuggestions,
44
+ composite
45
+ } = _ref;
46
+ const baseCssClass = 'edit-site-custom-template-modal__suggestions_list__list-item';
47
+ return (0, _element.createElement)(_components.__unstableCompositeItem, (0, _extends2.default)({
48
+ role: "option",
49
+ as: _components.Button
50
+ }, composite, {
51
+ className: baseCssClass,
52
+ onClick: () => {
53
+ const title = (0, _i18n.sprintf)( // translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the singular name of a post type and %2$s is the name of the post, e.g. "Post: Hello, WordPress"
54
+ (0, _i18n.__)('%1$s: %2$s'), entityForSuggestions.labels.singular_name, suggestion.name);
55
+ onSelect({
56
+ title,
57
+ description: (0, _i18n.sprintf)( // translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Post: Hello, WordPress"
58
+ (0, _i18n.__)('Template for %1$s'), title),
59
+ slug: `single-${entityForSuggestions.slug}-${suggestion.slug}`
60
+ });
61
+ }
62
+ }), (0, _element.createElement)("span", {
63
+ className: `${baseCssClass}__title`
64
+ }, (0, _element.createElement)(_components.TextHighlight, {
65
+ text: suggestion.name,
66
+ highlight: search
67
+ })), suggestion.link && (0, _element.createElement)("span", {
68
+ className: `${baseCssClass}__info`
69
+ }, suggestion.link));
70
+ }
71
+
72
+ function SuggestionList(_ref2) {
73
+ let {
74
+ entityForSuggestions,
75
+ onSelect
76
+ } = _ref2;
77
+ const composite = (0, _components.__unstableUseCompositeState)({
78
+ orientation: 'vertical'
79
+ });
80
+ const [suggestions, setSuggestions] = (0, _element.useState)(EMPTY_ARRAY); // We need to track two values, the search input's value(searchInputValue)
81
+ // and the one we want to debounce(search) and make REST API requests.
82
+
83
+ const [searchInputValue, setSearchInputValue] = (0, _element.useState)('');
84
+ const [search, setSearch] = (0, _element.useState)('');
85
+ const debouncedSearch = (0, _compose.useDebounce)(setSearch, 250);
86
+ const query = { ...BASE_QUERY,
87
+ search,
88
+ orderby: search ? 'relevance' : 'modified',
89
+ exclude: entityForSuggestions.postsToExclude,
90
+ per_page: search ? 20 : 10
91
+ };
92
+ const {
93
+ records: searchResults,
94
+ hasResolved: searchHasResolved
95
+ } = (0, _coreData.useEntityRecords)(entityForSuggestions.type, entityForSuggestions.slug, query);
96
+ (0, _element.useEffect)(() => {
97
+ if (search !== searchInputValue) {
98
+ debouncedSearch(searchInputValue);
99
+ }
100
+ }, [search, searchInputValue]);
101
+ const entitiesInfo = (0, _element.useMemo)(() => {
102
+ if (!(searchResults !== null && searchResults !== void 0 && searchResults.length)) return EMPTY_ARRAY;
103
+ return (0, _utils.mapToIHasNameAndId)(searchResults, 'title.rendered');
104
+ }, [searchResults]); // Update suggestions only when the query has resolved.
105
+
106
+ (0, _element.useEffect)(() => {
107
+ if (!searchHasResolved) return;
108
+ setSuggestions(entitiesInfo);
109
+ }, [entitiesInfo, searchHasResolved]);
110
+ return (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)(_components.SearchControl, {
111
+ onChange: setSearchInputValue,
112
+ value: searchInputValue,
113
+ label: entityForSuggestions.labels.search_items,
114
+ placeholder: entityForSuggestions.labels.search_items
115
+ }), !!(suggestions !== null && suggestions !== void 0 && suggestions.length) && (0, _element.createElement)(_components.__unstableComposite, (0, _extends2.default)({}, composite, {
116
+ role: "listbox",
117
+ className: "edit-site-custom-template-modal__suggestions_list"
118
+ }), suggestions.map(suggestion => (0, _element.createElement)(SuggestionListItem, {
119
+ key: suggestion.slug,
120
+ suggestion: suggestion,
121
+ search: search,
122
+ onSelect: onSelect,
123
+ entityForSuggestions: entityForSuggestions,
124
+ composite: composite
125
+ }))), search && !(suggestions !== null && suggestions !== void 0 && suggestions.length) && (0, _element.createElement)("p", {
126
+ className: "edit-site-custom-template-modal__no-results"
127
+ }, entityForSuggestions.labels.not_found));
128
+ }
129
+
130
+ function AddCustomTemplateModal(_ref3) {
131
+ let {
132
+ onClose,
133
+ onSelect,
134
+ entityForSuggestions
135
+ } = _ref3;
136
+ const [showSearchEntities, setShowSearchEntities] = (0, _element.useState)(entityForSuggestions.hasGeneralTemplate);
137
+ const baseCssClass = 'edit-site-custom-template-modal';
138
+ return (0, _element.createElement)(_components.Modal, {
139
+ title: (0, _i18n.sprintf)( // translators: %s: Name of the post type e.g: "Post".
140
+ (0, _i18n.__)('Add template: %s'), entityForSuggestions.labels.singular_name),
141
+ className: baseCssClass,
142
+ closeLabel: (0, _i18n.__)('Close'),
143
+ onRequestClose: onClose
144
+ }, !showSearchEntities && (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)("p", null, (0, _i18n.__)('Select whether to create a single template for all items or a specific one.')), (0, _element.createElement)(_components.Flex, {
145
+ className: `${baseCssClass}__contents`,
146
+ gap: "4",
147
+ align: "initial"
148
+ }, (0, _element.createElement)(_components.FlexItem, {
149
+ isBlock: true,
150
+ onClick: () => {
151
+ const {
152
+ slug,
153
+ title,
154
+ description
155
+ } = entityForSuggestions.template;
156
+ onSelect({
157
+ slug,
158
+ title,
159
+ description
160
+ });
161
+ }
162
+ }, (0, _element.createElement)(_components.__experimentalHeading, {
163
+ level: 5
164
+ }, entityForSuggestions.labels.all_items), (0, _element.createElement)(_components.__experimentalText, {
165
+ as: "span"
166
+ }, // translators: The user is given the choice to set up a template for all items of a post type, or just a specific one.
167
+ (0, _i18n.__)('For all items'))), (0, _element.createElement)(_components.FlexItem, {
168
+ isBlock: true,
169
+ onClick: () => {
170
+ setShowSearchEntities(true);
171
+ }
172
+ }, (0, _element.createElement)(_components.__experimentalHeading, {
173
+ level: 5
174
+ }, entityForSuggestions.labels.singular_name), (0, _element.createElement)(_components.__experimentalText, {
175
+ as: "span"
176
+ }, // translators: The user is given the choice to set up a template for all items of a post type, or just a specific one.
177
+ (0, _i18n.__)('For a specific item'))))), showSearchEntities && (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)("p", null, (0, _i18n.__)('This template will be used only for the specific item chosen.')), (0, _element.createElement)(SuggestionList, {
178
+ entityForSuggestions: entityForSuggestions,
179
+ onSelect: onSelect
180
+ })));
181
+ }
182
+
183
+ var _default = AddCustomTemplateModal;
184
+ exports.default = _default;
185
+ //# 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":["EMPTY_ARRAY","BASE_QUERY","order","_fields","context","SuggestionListItem","suggestion","search","onSelect","entityForSuggestions","composite","baseCssClass","Button","title","labels","singular_name","name","description","slug","link","SuggestionList","orientation","suggestions","setSuggestions","searchInputValue","setSearchInputValue","setSearch","debouncedSearch","query","orderby","exclude","postsToExclude","per_page","records","searchResults","hasResolved","searchHasResolved","type","entitiesInfo","length","search_items","map","not_found","AddCustomTemplateModal","onClose","showSearchEntities","setShowSearchEntities","hasGeneralTemplate","template","all_items"],"mappings":";;;;;;;;;AAGA;;;;AACA;;AACA;;AAaA;;AACA;;AAKA;;AAxBA;AACA;AACA;;AAmBA;AACA;AACA;AAGA,MAAMA,WAAW,GAAG,EAApB;AACA,MAAMC,UAAU,GAAG;AAClBC,EAAAA,KAAK,EAAE,KADW;AAElBC,EAAAA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AFZ,IAND,CA5DF,CADD;AA2EA;;eAEcmC,sB","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 = [];\nconst BASE_QUERY = {\n\torder: 'asc',\n\t_fields: 'id,title,slug,link',\n\tcontext: 'view',\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\tconst title = sprintf(\n\t\t\t\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 and %2$s is the name of the post, e.g. \"Post: Hello, WordPress\"\n\t\t\t\t\t__( '%1$s: %2$s' ),\n\t\t\t\t\tentityForSuggestions.labels.singular_name,\n\t\t\t\t\tsuggestion.name\n\t\t\t\t);\n\t\t\t\tonSelect( {\n\t\t\t\t\ttitle,\n\t\t\t\t\tdescription: sprintf(\n\t\t\t\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\t\t\t__( 'Template for %1$s' ),\n\t\t\t\t\t\ttitle\n\t\t\t\t\t),\n\t\t\t\t\tslug: `single-${ entityForSuggestions.slug }-${ suggestion.slug }`,\n\t\t\t\t} );\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 SuggestionList( { entityForSuggestions, onSelect } ) {\n\tconst composite = useCompositeState( { orientation: 'vertical' } );\n\tconst [ suggestions, setSuggestions ] = useState( EMPTY_ARRAY );\n\t// We need to track two values, the search input's value(searchInputValue)\n\t// and the one we want to debounce(search) and make REST API requests.\n\tconst [ searchInputValue, setSearchInputValue ] = useState( '' );\n\tconst [ search, setSearch ] = useState( '' );\n\tconst debouncedSearch = useDebounce( setSearch, 250 );\n\tconst query = {\n\t\t...BASE_QUERY,\n\t\tsearch,\n\t\torderby: search ? 'relevance' : 'modified',\n\t\texclude: entityForSuggestions.postsToExclude,\n\t\tper_page: search ? 20 : 10,\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\tuseEffect( () => {\n\t\tif ( search !== searchInputValue ) {\n\t\t\tdebouncedSearch( searchInputValue );\n\t\t}\n\t}, [ search, searchInputValue ] );\n\tconst entitiesInfo = useMemo( () => {\n\t\tif ( ! searchResults?.length ) return EMPTY_ARRAY;\n\t\treturn mapToIHasNameAndId( searchResults, 'title.rendered' );\n\t}, [ searchResults ] );\n\t// Update suggestions only when the query has resolved.\n\tuseEffect( () => {\n\t\tif ( ! searchHasResolved ) return;\n\t\tsetSuggestions( entitiesInfo );\n\t}, [ entitiesInfo, searchHasResolved ] );\n\treturn (\n\t\t<>\n\t\t\t<SearchControl\n\t\t\t\tonChange={ setSearchInputValue }\n\t\t\t\tvalue={ searchInputValue }\n\t\t\t\tlabel={ entityForSuggestions.labels.search_items }\n\t\t\t\tplaceholder={ entityForSuggestions.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={ search }\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{ search && ! suggestions?.length && (\n\t\t\t\t<p className=\"edit-site-custom-template-modal__no-results\">\n\t\t\t\t\t{ entityForSuggestions.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 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 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,5 +1,7 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
3
5
  Object.defineProperty(exports, "__esModule", {
4
6
  value: true
5
7
  });
@@ -23,6 +25,10 @@ var _i18n = require("@wordpress/i18n");
23
25
 
24
26
  var _notices = require("@wordpress/notices");
25
27
 
28
+ var _addCustomTemplateModal = _interopRequireDefault(require("./add-custom-template-modal"));
29
+
30
+ var _utils = require("./utils");
31
+
26
32
  var _routes = require("../routes");
27
33
 
28
34
  var _store = require("../../store");
@@ -60,15 +66,19 @@ function NewTemplate(_ref) {
60
66
  postType
61
67
  } = _ref;
62
68
  const history = (0, _routes.useHistory)();
69
+ const postTypes = (0, _utils.usePostTypes)();
70
+ const [showCustomTemplateModal, setShowCustomTemplateModal] = (0, _element.useState)(false);
71
+ const [entityForSuggestions, setEntityForSuggestions] = (0, _element.useState)({});
63
72
  const {
64
- templates,
73
+ existingTemplates,
65
74
  defaultTemplateTypes
66
75
  } = (0, _data.useSelect)(select => ({
67
- templates: select(_coreData.store).getEntityRecords('postType', 'wp_template', {
76
+ existingTemplates: select(_coreData.store).getEntityRecords('postType', 'wp_template', {
68
77
  per_page: -1
69
78
  }),
70
79
  defaultTemplateTypes: select(_editor.store).__experimentalGetDefaultTemplateTypes()
71
80
  }), []);
81
+ const postTypesEntitiesInfo = (0, _utils.usePostTypesEntitiesInfo)(existingTemplates);
72
82
  const {
73
83
  saveEntityRecord
74
84
  } = (0, _data.useDispatch)(_coreData.store);
@@ -79,33 +89,30 @@ function NewTemplate(_ref) {
79
89
  setTemplate
80
90
  } = (0, _data.useDispatch)(_store.store);
81
91
 
82
- async function createTemplate(_ref2) {
83
- let {
84
- slug
85
- } = _ref2;
86
-
92
+ async function createTemplate(template) {
87
93
  try {
88
94
  const {
89
95
  title,
90
- description
91
- } = (0, _lodash.find)(defaultTemplateTypes, {
96
+ description,
92
97
  slug
93
- });
94
- const template = await saveEntityRecord('postType', 'wp_template', {
95
- excerpt: description,
98
+ } = template;
99
+ const newTemplate = await saveEntityRecord('postType', 'wp_template', {
100
+ description,
96
101
  // Slugs need to be strings, so this is for template `404`
97
102
  slug: slug.toString(),
98
103
  status: 'publish',
99
- title
104
+ title,
105
+ // This adds a post meta field in template that is part of `is_custom` value calculation.
106
+ is_wp_suggestion: true
100
107
  }, {
101
108
  throwOnError: true
102
109
  }); // Set template before navigating away to avoid initial stale value.
103
110
 
104
- setTemplate(template.id, template.slug); // Navigate to the created template editor.
111
+ setTemplate(newTemplate.id, newTemplate.slug); // Navigate to the created template editor.
105
112
 
106
113
  history.push({
107
- postId: template.id,
108
- postType: template.type
114
+ postId: newTemplate.id,
115
+ postType: newTemplate.type
109
116
  }); // TODO: Add a success notice?
110
117
  } catch (error) {
111
118
  const errorMessage = error.message && error.code !== 'unknown_error' ? error.message : (0, _i18n.__)('An error occurred while creating the template.');
@@ -115,18 +122,69 @@ function NewTemplate(_ref) {
115
122
  }
116
123
  }
117
124
 
118
- const existingTemplateSlugs = (0, _lodash.map)(templates, 'slug');
125
+ const existingTemplateSlugs = (existingTemplates || []).map(_ref2 => {
126
+ let {
127
+ slug
128
+ } = _ref2;
129
+ return slug;
130
+ });
119
131
  const missingTemplates = (0, _lodash.filter)(defaultTemplateTypes, template => (0, _lodash.includes)(DEFAULT_TEMPLATE_SLUGS, template.slug) && !(0, _lodash.includes)(existingTemplateSlugs, template.slug));
132
+ const extraTemplates = (postTypes || []).reduce((accumulator, _postType) => {
133
+ var _postTypesEntitiesInf;
134
+
135
+ const {
136
+ slug,
137
+ labels,
138
+ icon
139
+ } = _postType;
140
+ const hasGeneralTemplate = existingTemplateSlugs === null || existingTemplateSlugs === void 0 ? void 0 : existingTemplateSlugs.includes(`single-${slug}`);
141
+ const hasEntities = postTypesEntitiesInfo === null || postTypesEntitiesInfo === void 0 ? void 0 : (_postTypesEntitiesInf = postTypesEntitiesInfo[slug]) === null || _postTypesEntitiesInf === void 0 ? void 0 : _postTypesEntitiesInf.hasEntities;
142
+ const menuItem = {
143
+ slug: `single-${slug}`,
144
+ title: (0, _i18n.sprintf)( // translators: %s: Name of the post type e.g: "Post".
145
+ (0, _i18n.__)('Single item: %s'), labels.singular_name),
146
+ description: (0, _i18n.sprintf)( // translators: %s: Name of the post type e.g: "Post".
147
+ (0, _i18n.__)('Displays a single item: %s.'), labels.singular_name),
148
+ // `icon` is the `menu_icon` property of a post type. We
149
+ // only handle `dashicons` for now, even if the `menu_icon`
150
+ // also supports urls and svg as values.
151
+ icon: icon !== null && icon !== void 0 && icon.startsWith('dashicons-') ? icon.slice(10) : null
152
+ }; // We have a different template creation flow only if they have entities.
153
+
154
+ if (hasEntities) {
155
+ menuItem.onClick = template => {
156
+ setShowCustomTemplateModal(true);
157
+ setEntityForSuggestions({
158
+ type: 'postType',
159
+ slug,
160
+ labels,
161
+ hasGeneralTemplate,
162
+ template,
163
+ postsToExclude: postTypesEntitiesInfo[slug].existingPosts
164
+ });
165
+ };
166
+ } // We don't need to add the menu item if there are no
167
+ // entities and the general template exists.
168
+
169
+
170
+ if (!hasGeneralTemplate || hasEntities) {
171
+ accumulator.push(menuItem);
172
+ }
173
+
174
+ return accumulator;
175
+ }, []);
120
176
 
121
- if (!missingTemplates.length) {
177
+ if (!missingTemplates.length && !extraTemplates.length) {
122
178
  return null;
123
179
  } // Update the sort order to match the DEFAULT_TEMPLATE_SLUGS order.
124
180
 
125
181
 
126
- missingTemplates.sort((template1, template2) => {
182
+ missingTemplates === null || missingTemplates === void 0 ? void 0 : missingTemplates.sort((template1, template2) => {
127
183
  return DEFAULT_TEMPLATE_SLUGS.indexOf(template1.slug) - DEFAULT_TEMPLATE_SLUGS.indexOf(template2.slug);
128
- });
129
- return (0, _element.createElement)(_components.DropdownMenu, {
184
+ }); // Append all extra templates at the end of the list for now.
185
+
186
+ missingTemplates.push(...extraTemplates);
187
+ return (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)(_components.DropdownMenu, {
130
188
  className: "edit-site-new-template-dropdown",
131
189
  icon: null,
132
190
  text: postType.labels.add_new,
@@ -141,23 +199,25 @@ function NewTemplate(_ref) {
141
199
  className: "edit-site-new-template-dropdown__popover"
142
200
  }, (0, _element.createElement)(_components.MenuGroup, {
143
201
  label: postType.labels.add_new_item
144
- }, (0, _lodash.map)(missingTemplates, _ref3 => {
145
- let {
202
+ }, missingTemplates.map(template => {
203
+ const {
146
204
  title,
147
205
  description,
148
- slug
149
- } = _ref3;
206
+ slug,
207
+ onClick,
208
+ icon
209
+ } = template;
150
210
  return (0, _element.createElement)(_components.MenuItem, {
151
- icon: TEMPLATE_ICONS[slug],
211
+ icon: icon || TEMPLATE_ICONS[slug] || _icons.post,
152
212
  iconPosition: "left",
153
213
  info: description,
154
214
  key: slug,
155
- onClick: () => {
156
- createTemplate({
157
- slug
158
- }); // We will be navigated way so no need to close the dropdown.
159
- }
215
+ onClick: () => onClick ? onClick(template) : createTemplate(template)
160
216
  }, title);
161
- }))));
217
+ })))), showCustomTemplateModal && (0, _element.createElement)(_addCustomTemplateModal.default, {
218
+ onClose: () => setShowCustomTemplateModal(false),
219
+ onSelect: createTemplate,
220
+ entityForSuggestions: entityForSuggestions
221
+ }));
162
222
  }
163
223
  //# sourceMappingURL=new-template.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/edit-site/src/components/add-new-template/new-template.js"],"names":["DEFAULT_TEMPLATE_SLUGS","TEMPLATE_ICONS","home","single","post","page","archive","search","notFound","index","list","category","author","postAuthor","taxonomy","blockMeta","date","postDate","tag","attachment","media","NewTemplate","postType","history","templates","defaultTemplateTypes","select","coreStore","getEntityRecords","per_page","editorStore","__experimentalGetDefaultTemplateTypes","saveEntityRecord","createErrorNotice","noticesStore","setTemplate","editSiteStore","createTemplate","slug","title","description","template","excerpt","toString","status","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":";;;;;;;;;AAGA;;AAKA;;AAMA;;AACA;;AACA;;AACA;;AAeA;;AACA;;AAKA;;AACA;;AAvCA;AACA;AACA;;AAGA;AACA;AACA;;AA4BA;AACA;AACA;AAIA,MAAMA,sBAAsB,GAAG,CAC9B,YAD8B,EAE9B,QAF8B,EAG9B,MAH8B,EAI9B,OAJ8B,EAK9B,SAL8B,EAM9B,QAN8B,EAO9B,UAP8B,EAQ9B,MAR8B,EAS9B,KAT8B,EAU9B,UAV8B,EAW9B,QAX8B,EAY9B,KAZ8B,CAA/B;AAeA,MAAMC,cAAc,GAAG;AACtB,gBAAcC,WADQ;AAEtBC,EAAAA,MAAM,EAAEC,WAFc;AAGtBC,EAAAA,IAAI,EAAJA,WAHsB;AAItBC,EAAAA,OAAO,EAAPA,cAJsB;AAKtBC,EAAAA,MAAM,EAANA,aALsB;AAMtB,OAAKC,eANiB;AAOtBC,EAAAA,KAAK,EAAEC,WAPe;AAQtBC,EAAAA,QAAQ,EAARA,eARsB;AAStBC,EAAAA,MAAM,EAAEC,iBATc;AAUtBC,EAAAA,QAAQ,EAAEC,gBAVY;AAWtBC,EAAAA,IAAI,EAAEC,eAXgB;AAYtBC,EAAAA,GAAG,EAAHA,UAZsB;AAatBC,EAAAA,UAAU,EAAEC;AAbU,CAAvB;;AAgBe,SAASC,WAAT,OAAqC;AAAA,M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ACbC,MAAAA,OAAO,EAAE;AADI;AARf,KAYG,MACD,4BAAC,yBAAD;AAAe,IAAA,SAAS,EAAC;AAAzB,KACC,4BAAC,qBAAD;AAAW,IAAA,KAAK,EAAG3C,QAAQ,CAACuC,MAAT,CAAgBE;AAAnC,KACG,iBACDR,gBADC,EAED;AAAA,QAAE;AAAEhB,MAAAA,KAAF;AAASC,MAAAA,WAAT;AAAsBF,MAAAA;AAAtB,KAAF;AAAA,WACC,4BAAC,oBAAD;AACC,MAAA,IAAI,EAAGrC,cAAc,CAAEqC,IAAF,CADtB;AAEC,MAAA,YAAY,EAAC,MAFd;AAGC,MAAA,IAAI,EAAGE,WAHR;AAIC,MAAA,GAAG,EAAGF,IAJP;AAKC,MAAA,OAAO,EAAG,MAAM;AACfD,QAAAA,cAAc,CAAE;AAAEC,UAAAA;AAAF,SAAF,CAAd,CADe,CAEf;AACA;AARF,OAUGC,KAVH,CADD;AAAA,GAFC,CADH,CADD,CAbF,CADD;AAsCA","sourcesContent":["/**\n * External dependencies\n */\nimport { filter, find, 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( { slug } ) {\n\t\ttry {\n\t\t\tconst { title, description } = find( defaultTemplateTypes, {\n\t\t\t\tslug,\n\t\t\t} );\n\n\t\t\tconst template = 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\texcerpt: description,\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},\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( template.id, template.slug );\n\n\t\t\t// Navigate to the created template editor.\n\t\t\thistory.push( {\n\t\t\t\tpostId: template.id,\n\t\t\t\tpostType: template.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(\n\t\t\t\t\t\t\tmissingTemplates,\n\t\t\t\t\t\t\t( { title, description, slug } ) => (\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( { slug } );\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":["DEFAULT_TEMPLATE_SLUGS","TEMPLATE_ICONS","home","single","post","page","archive","search","notFound","index","list","category","author","postAuthor","taxonomy","blockMeta","date","postDate","tag","attachment","media","NewTemplate","postType","history","postTypes","showCustomTemplateModal","setShowCustomTemplateModal","entityForSuggestions","setEntityForSuggestions","existingTemplates","defaultTemplateTypes","select","coreStore","getEntityRecords","per_page","editorStore","__experimentalGetDefaultTemplateTypes","postTypesEntitiesInfo","saveEntityRecord","createErrorNotice","noticesStore","setTemplate","editSiteStore","createTemplate","template","title","description","slug","newTemplate","toString","status","is_wp_suggestion","throwOnError","id","push","postId","type","error","errorMessage","message","code","existingTemplateSlugs","map","missingTemplates","extraTemplates","reduce","accumulator","_postType","labels","icon","hasGeneralTemplate","includes","hasEntities","menuItem","singular_name","startsWith","slice","onClick","postsToExclude","existingPosts","length","sort","template1","template2","indexOf","add_new","add_new_item","noArrow","variant"],"mappings":";;;;;;;;;AAcA;;AAXA;;AAKA;;AAOA;;AACA;;AACA;;AACA;;AAeA;;AACA;;AAKA;;AACA;;AACA;;AACA;;AA1CA;AACA;AACA;;AAGA;AACA;AACA;;AA6BA;AACA;AACA;AAMA,MAAMA,sBAAsB,GAAG,CAC9B,YAD8B,EAE9B,QAF8B,EAG9B,MAH8B,EAI9B,OAJ8B,EAK9B,SAL8B,EAM9B,QAN8B,EAO9B,UAP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AADK,KALhB;AAQC,IAAA,WAAW,EAAG;AACbC,MAAAA,OAAO,EAAE;AADI;AARf,KAYG,MACD,4BAAC,yBAAD;AAAe,IAAA,SAAS,EAAC;AAAzB,KACC,4BAAC,qBAAD;AAAW,IAAA,KAAK,EAAGlE,QAAQ,CAAC8C,MAAT,CAAgBkB;AAAnC,KACGvB,gBAAgB,CAACD,GAAjB,CAAwBlB,QAAF,IAAgB;AACvC,UAAM;AACLC,MAAAA,KADK;AAELC,MAAAA,WAFK;AAGLC,MAAAA,IAHK;AAIL8B,MAAAA,OAJK;AAKLR,MAAAA;AALK,QAMFzB,QANJ;AAOA,WACC,4BAAC,oBAAD;AACC,MAAA,IAAI,EACHyB,IAAI,IACJpE,cAAc,CAAE8C,IAAF,CADd,IAEA3C,WAJF;AAMC,MAAA,YAAY,EAAC,MANd;AAOC,MAAA,IAAI,EAAG0C,WAPR;AAQC,MAAA,GAAG,EAAGC,IARP;AASC,MAAA,OAAO,EAAG,MACT8B,OAAO,GACJA,OAAO,CAAEjC,QAAF,CADH,GAEJD,cAAc,CAAEC,QAAF;AAZnB,OAeGC,KAfH,CADD;AAmBA,GA3BC,CADH,CADD,CAbF,CADD,EAgDGpB,uBAAuB,IACxB,4BAAC,+BAAD;AACC,IAAA,OAAO,EAAG,MAAMC,0BAA0B,CAAE,KAAF,CAD3C;AAEC,IAAA,QAAQ,EAAGiB,cAFZ;AAGC,IAAA,oBAAoB,EAAGhB;AAHxB,IAjDF,CADD;AA0DA","sourcesContent":["/**\n * External dependencies\n */\nimport { filter, includes } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tDropdownMenu,\n\tMenuGroup,\n\tMenuItem,\n\tNavigableMenu,\n} from '@wordpress/components';\nimport { useState } from '@wordpress/element';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { store as editorStore } from '@wordpress/editor';\nimport {\n\tarchive,\n\tblockMeta,\n\tcategory,\n\thome,\n\tlist,\n\tmedia,\n\tnotFound,\n\tpage,\n\tpost,\n\tpostAuthor,\n\tpostDate,\n\tsearch,\n\ttag,\n} from '@wordpress/icons';\nimport { __, sprintf } from '@wordpress/i18n';\nimport { store as noticesStore } from '@wordpress/notices';\n\n/**\n * Internal dependencies\n */\nimport AddCustomTemplateModal from './add-custom-template-modal';\nimport { usePostTypes, usePostTypesEntitiesInfo } from './utils';\nimport { useHistory } from '../routes';\nimport { store as editSiteStore } from '../../store';\n\nconst DEFAULT_TEMPLATE_SLUGS = [\n\t'front-page',\n\t'single',\n\t'page',\n\t'index',\n\t'archive',\n\t'author',\n\t'category',\n\t'date',\n\t'tag',\n\t'taxonomy',\n\t'search',\n\t'404',\n];\n\nconst TEMPLATE_ICONS = {\n\t'front-page': home,\n\tsingle: post,\n\tpage,\n\tarchive,\n\tsearch,\n\t404: notFound,\n\tindex: list,\n\tcategory,\n\tauthor: postAuthor,\n\ttaxonomy: blockMeta,\n\tdate: postDate,\n\ttag,\n\tattachment: media,\n};\n\nexport default function NewTemplate( { postType } ) {\n\tconst history = useHistory();\n\tconst postTypes = usePostTypes();\n\tconst [ showCustomTemplateModal, setShowCustomTemplateModal ] =\n\t\tuseState( false );\n\tconst [ entityForSuggestions, setEntityForSuggestions ] = useState( {} );\n\tconst { existingTemplates, defaultTemplateTypes } = useSelect(\n\t\t( select ) => ( {\n\t\t\texistingTemplates: select( coreStore ).getEntityRecords(\n\t\t\t\t'postType',\n\t\t\t\t'wp_template',\n\t\t\t\t{ per_page: -1 }\n\t\t\t),\n\t\t\tdefaultTemplateTypes:\n\t\t\t\tselect( editorStore ).__experimentalGetDefaultTemplateTypes(),\n\t\t} ),\n\t\t[]\n\t);\n\tconst postTypesEntitiesInfo = usePostTypesEntitiesInfo( existingTemplates );\n\tconst { saveEntityRecord } = useDispatch( coreStore );\n\tconst { createErrorNotice } = useDispatch( noticesStore );\n\tconst { setTemplate } = useDispatch( editSiteStore );\n\n\tasync function createTemplate( template ) {\n\t\ttry {\n\t\t\tconst { title, description, slug } = template;\n\t\t\tconst newTemplate = await saveEntityRecord(\n\t\t\t\t'postType',\n\t\t\t\t'wp_template',\n\t\t\t\t{\n\t\t\t\t\tdescription,\n\t\t\t\t\t// Slugs need to be strings, so this is for template `404`\n\t\t\t\t\tslug: slug.toString(),\n\t\t\t\t\tstatus: 'publish',\n\t\t\t\t\ttitle,\n\t\t\t\t\t// This adds a post meta field in template that is part of `is_custom` value calculation.\n\t\t\t\t\tis_wp_suggestion: true,\n\t\t\t\t},\n\t\t\t\t{ throwOnError: true }\n\t\t\t);\n\n\t\t\t// Set template before navigating away to avoid initial stale value.\n\t\t\tsetTemplate( newTemplate.id, newTemplate.slug );\n\n\t\t\t// Navigate to the created template editor.\n\t\t\thistory.push( {\n\t\t\t\tpostId: newTemplate.id,\n\t\t\t\tpostType: newTemplate.type,\n\t\t\t} );\n\n\t\t\t// TODO: Add a success notice?\n\t\t} catch ( error ) {\n\t\t\tconst errorMessage =\n\t\t\t\terror.message && error.code !== 'unknown_error'\n\t\t\t\t\t? error.message\n\t\t\t\t\t: __( 'An error occurred while creating the template.' );\n\n\t\t\tcreateErrorNotice( errorMessage, {\n\t\t\t\ttype: 'snackbar',\n\t\t\t} );\n\t\t}\n\t}\n\tconst existingTemplateSlugs = ( existingTemplates || [] ).map(\n\t\t( { slug } ) => slug\n\t);\n\tconst missingTemplates = filter(\n\t\tdefaultTemplateTypes,\n\t\t( template ) =>\n\t\t\tincludes( DEFAULT_TEMPLATE_SLUGS, template.slug ) &&\n\t\t\t! includes( existingTemplateSlugs, template.slug )\n\t);\n\n\tconst extraTemplates = ( postTypes || [] ).reduce(\n\t\t( accumulator, _postType ) => {\n\t\t\tconst { slug, labels, icon } = _postType;\n\t\t\tconst hasGeneralTemplate = existingTemplateSlugs?.includes(\n\t\t\t\t`single-${ slug }`\n\t\t\t);\n\t\t\tconst hasEntities = postTypesEntitiesInfo?.[ slug ]?.hasEntities;\n\t\t\tconst menuItem = {\n\t\t\t\tslug: `single-${ slug }`,\n\t\t\t\ttitle: sprintf(\n\t\t\t\t\t// translators: %s: Name of the post type e.g: \"Post\".\n\t\t\t\t\t__( 'Single item: %s' ),\n\t\t\t\t\tlabels.singular_name\n\t\t\t\t),\n\t\t\t\tdescription: sprintf(\n\t\t\t\t\t// translators: %s: Name of the post type e.g: \"Post\".\n\t\t\t\t\t__( 'Displays a single item: %s.' ),\n\t\t\t\t\tlabels.singular_name\n\t\t\t\t),\n\t\t\t\t// `icon` is the `menu_icon` property of a post type. We\n\t\t\t\t// only handle `dashicons` for now, even if the `menu_icon`\n\t\t\t\t// also supports urls and svg as values.\n\t\t\t\ticon: icon?.startsWith( 'dashicons-' )\n\t\t\t\t\t? icon.slice( 10 )\n\t\t\t\t\t: null,\n\t\t\t};\n\t\t\t// We have a different template creation flow only if they have entities.\n\t\t\tif ( hasEntities ) {\n\t\t\t\tmenuItem.onClick = ( template ) => {\n\t\t\t\t\tsetShowCustomTemplateModal( true );\n\t\t\t\t\tsetEntityForSuggestions( {\n\t\t\t\t\t\ttype: 'postType',\n\t\t\t\t\t\tslug,\n\t\t\t\t\t\tlabels,\n\t\t\t\t\t\thasGeneralTemplate,\n\t\t\t\t\t\ttemplate,\n\t\t\t\t\t\tpostsToExclude:\n\t\t\t\t\t\t\tpostTypesEntitiesInfo[ slug ].existingPosts,\n\t\t\t\t\t} );\n\t\t\t\t};\n\t\t\t}\n\t\t\t// We don't need to add the menu item if there are no\n\t\t\t// entities and the general template exists.\n\t\t\tif ( ! hasGeneralTemplate || hasEntities ) {\n\t\t\t\taccumulator.push( menuItem );\n\t\t\t}\n\t\t\treturn accumulator;\n\t\t},\n\t\t[]\n\t);\n\tif ( ! missingTemplates.length && ! extraTemplates.length ) {\n\t\treturn null;\n\t}\n\t// Update the sort order to match the DEFAULT_TEMPLATE_SLUGS order.\n\tmissingTemplates?.sort( ( template1, template2 ) => {\n\t\treturn (\n\t\t\tDEFAULT_TEMPLATE_SLUGS.indexOf( template1.slug ) -\n\t\t\tDEFAULT_TEMPLATE_SLUGS.indexOf( template2.slug )\n\t\t);\n\t} );\n\t// Append all extra templates at the end of the list for now.\n\tmissingTemplates.push( ...extraTemplates );\n\treturn (\n\t\t<>\n\t\t\t<DropdownMenu\n\t\t\t\tclassName=\"edit-site-new-template-dropdown\"\n\t\t\t\ticon={ null }\n\t\t\t\ttext={ postType.labels.add_new }\n\t\t\t\tlabel={ postType.labels.add_new_item }\n\t\t\t\tpopoverProps={ {\n\t\t\t\t\tnoArrow: false,\n\t\t\t\t} }\n\t\t\t\ttoggleProps={ {\n\t\t\t\t\tvariant: 'primary',\n\t\t\t\t} }\n\t\t\t>\n\t\t\t\t{ () => (\n\t\t\t\t\t<NavigableMenu className=\"edit-site-new-template-dropdown__popover\">\n\t\t\t\t\t\t<MenuGroup label={ postType.labels.add_new_item }>\n\t\t\t\t\t\t\t{ missingTemplates.map( ( template ) => {\n\t\t\t\t\t\t\t\tconst {\n\t\t\t\t\t\t\t\t\ttitle,\n\t\t\t\t\t\t\t\t\tdescription,\n\t\t\t\t\t\t\t\t\tslug,\n\t\t\t\t\t\t\t\t\tonClick,\n\t\t\t\t\t\t\t\t\ticon,\n\t\t\t\t\t\t\t\t} = template;\n\t\t\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\t\t\t<MenuItem\n\t\t\t\t\t\t\t\t\t\ticon={\n\t\t\t\t\t\t\t\t\t\t\ticon ||\n\t\t\t\t\t\t\t\t\t\t\tTEMPLATE_ICONS[ slug ] ||\n\t\t\t\t\t\t\t\t\t\t\tpost\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\ticonPosition=\"left\"\n\t\t\t\t\t\t\t\t\t\tinfo={ description }\n\t\t\t\t\t\t\t\t\t\tkey={ slug }\n\t\t\t\t\t\t\t\t\t\tonClick={ () =>\n\t\t\t\t\t\t\t\t\t\t\tonClick\n\t\t\t\t\t\t\t\t\t\t\t\t? onClick( template )\n\t\t\t\t\t\t\t\t\t\t\t\t: createTemplate( template )\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t\t{ title }\n\t\t\t\t\t\t\t\t\t</MenuItem>\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t} ) }\n\t\t\t\t\t\t</MenuGroup>\n\t\t\t\t\t</NavigableMenu>\n\t\t\t\t) }\n\t\t\t</DropdownMenu>\n\t\t\t{ showCustomTemplateModal && (\n\t\t\t\t<AddCustomTemplateModal\n\t\t\t\t\tonClose={ () => setShowCustomTemplateModal( false ) }\n\t\t\t\t\tonSelect={ createTemplate }\n\t\t\t\t\tentityForSuggestions={ entityForSuggestions }\n\t\t\t\t/>\n\t\t\t) }\n\t\t</>\n\t);\n}\n"]}
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.usePostTypesEntitiesInfo = exports.usePostTypes = exports.mapToIHasNameAndId = void 0;
7
+
8
+ var _lodash = require("lodash");
9
+
10
+ var _data = require("@wordpress/data");
11
+
12
+ var _coreData = require("@wordpress/core-data");
13
+
14
+ var _htmlEntities = require("@wordpress/html-entities");
15
+
16
+ var _element = require("@wordpress/element");
17
+
18
+ /**
19
+ * External dependencies
20
+ */
21
+
22
+ /**
23
+ * WordPress dependencies
24
+ */
25
+ const usePostTypes = () => {
26
+ const postTypes = (0, _data.useSelect)(select => select(_coreData.store).getPostTypes({
27
+ per_page: -1
28
+ }), []);
29
+ return (0, _element.useMemo)(() => {
30
+ const excludedPostTypes = ['attachment', 'page'];
31
+ return postTypes === null || postTypes === void 0 ? void 0 : postTypes.filter(_ref => {
32
+ let {
33
+ viewable,
34
+ slug
35
+ } = _ref;
36
+ return viewable && !excludedPostTypes.includes(slug);
37
+ });
38
+ }, [postTypes]);
39
+ };
40
+ /**
41
+ * @typedef {Object} PostTypeEntitiesInfo
42
+ * @property {boolean} hasEntities If a postType has available entities.
43
+ * @property {number[]} existingPosts An array of the existing entities ids.
44
+ */
45
+
46
+ /**
47
+ * Helper hook that returns information about a post type having
48
+ * posts that we can create a specific template for.
49
+ *
50
+ * First we need to find the existing posts with an associated template,
51
+ * to query afterwards for any remaing post, by excluding them.
52
+ *
53
+ * @param {string[]} existingTemplates The existing templates.
54
+ * @return {Record<string,PostTypeEntitiesInfo>} An object with the postTypes as `keys` and PostTypeEntitiesInfo as values.
55
+ */
56
+
57
+
58
+ exports.usePostTypes = usePostTypes;
59
+
60
+ const usePostTypesEntitiesInfo = existingTemplates => {
61
+ const postTypes = usePostTypes();
62
+ const slugsToExcludePerEntity = (0, _element.useMemo)(() => {
63
+ return postTypes === null || postTypes === void 0 ? void 0 : postTypes.reduce((accumulator, _postType) => {
64
+ const slugsWithTemplates = (existingTemplates || []).reduce((_accumulator, existingTemplate) => {
65
+ const prefix = `single-${_postType.slug}-`;
66
+
67
+ if (existingTemplate.slug.startsWith(prefix)) {
68
+ _accumulator.push(existingTemplate.slug.substring(prefix.length));
69
+ }
70
+
71
+ return _accumulator;
72
+ }, []);
73
+
74
+ if (slugsWithTemplates.length) {
75
+ accumulator[_postType.slug] = slugsWithTemplates;
76
+ }
77
+
78
+ return accumulator;
79
+ }, {});
80
+ }, [postTypes, existingTemplates]);
81
+ const postsToExcludePerEntity = (0, _data.useSelect)(select => {
82
+ if (!slugsToExcludePerEntity) {
83
+ return;
84
+ }
85
+
86
+ const postsToExclude = Object.entries(slugsToExcludePerEntity).reduce((accumulator, _ref2) => {
87
+ let [slug, slugsWithTemplates] = _ref2;
88
+ const postsWithTemplates = select(_coreData.store).getEntityRecords('postType', slug, {
89
+ _fields: 'id',
90
+ context: 'view',
91
+ slug: slugsWithTemplates
92
+ });
93
+
94
+ if (postsWithTemplates !== null && postsWithTemplates !== void 0 && postsWithTemplates.length) {
95
+ accumulator[slug] = postsWithTemplates;
96
+ }
97
+
98
+ return accumulator;
99
+ }, {});
100
+ return postsToExclude;
101
+ }, [slugsToExcludePerEntity]);
102
+ const entitiesInfo = (0, _data.useSelect)(select => {
103
+ return postTypes === null || postTypes === void 0 ? void 0 : postTypes.reduce((accumulator, _ref3) => {
104
+ var _postsToExcludePerEnt, _select$getEntityReco;
105
+
106
+ let {
107
+ slug
108
+ } = _ref3;
109
+ const existingPosts = (postsToExcludePerEntity === null || postsToExcludePerEntity === void 0 ? void 0 : (_postsToExcludePerEnt = postsToExcludePerEntity[slug]) === null || _postsToExcludePerEnt === void 0 ? void 0 : _postsToExcludePerEnt.map(_ref4 => {
110
+ let {
111
+ id
112
+ } = _ref4;
113
+ return id;
114
+ })) || [];
115
+ accumulator[slug] = {
116
+ hasEntities: !!((_select$getEntityReco = select(_coreData.store).getEntityRecords('postType', slug, {
117
+ per_page: 1,
118
+ _fields: 'id',
119
+ context: 'view',
120
+ exclude: existingPosts
121
+ })) !== null && _select$getEntityReco !== void 0 && _select$getEntityReco.length),
122
+ existingPosts
123
+ };
124
+ return accumulator;
125
+ }, {});
126
+ }, [postTypes, postsToExcludePerEntity]);
127
+ return entitiesInfo;
128
+ };
129
+
130
+ exports.usePostTypesEntitiesInfo = usePostTypesEntitiesInfo;
131
+
132
+ const mapToIHasNameAndId = (entities, path) => {
133
+ return (entities || []).map(entity => ({ ...entity,
134
+ name: (0, _htmlEntities.decodeEntities)((0, _lodash.get)(entity, path))
135
+ }));
136
+ };
137
+
138
+ exports.mapToIHasNameAndId = mapToIHasNameAndId;
139
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/edit-site/src/components/add-new-template/utils.js"],"names":["usePostTypes","postTypes","select","coreStore","getPostTypes","per_page","excludedPostTypes","filter","viewable","slug","includes","usePostTypesEntitiesInfo","existingTemplates","slugsToExcludePerEntity","reduce","accumulator","_postType","slugsWithTemplates","_accumulator","existingTemplate","prefix","startsWith","push","substring","length","postsToExcludePerEntity","postsToExclude","Object","entries","postsWithTemplates","getEntityRecords","_fields","context","entitiesInfo","existingPosts","map","id","hasEntities","exclude","m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CAAmB,CAAEC,WAAF,YAA6B;AAAA;;AAAA,UAAd;AAAEN,QAAAA;AAAF,OAAc;AACtD,YAAMyB,aAAa,GAClB,CAAAT,uBAAuB,SAAvB,IAAAA,uBAAuB,WAAvB,qCAAAA,uBAAuB,CAAIhB,IAAJ,CAAvB,gFAAmC0B,GAAnC,CACC;AAAA,YAAE;AAAEC,UAAAA;AAAF,SAAF;AAAA,eAAcA,EAAd;AAAA,OADD,MAEK,EAHN;AAIArB,MAAAA,WAAW,CAAEN,IAAF,CAAX,GAAsB;AACrB4B,QAAAA,WAAW,EAAE,CAAC,2BAAEnC,MAAM,CAAEC,eAAF,CAAN,CAAoB2B,gBAApB,CACf,UADe,EAEfrB,IAFe,EAGf;AACCJ,UAAAA,QAAQ,EAAE,CADX;AAEC0B,UAAAA,OAAO,EAAE,IAFV;AAGCC,UAAAA,OAAO,EAAE,MAHV;AAICM,UAAAA,OAAO,EAAEJ;AAJV,SAHe,CAAF,kDAAE,sBASbV,MATW,CADO;AAWrBU,QAAAA;AAXqB,OAAtB;AAaA,aAAOnB,WAAP;AACA,KAnBM,EAmBJ,EAnBI,CAAP;AAoBA,GAtBmB,EAuBpB,CAAEd,SAAF,EAAawB,uBAAb,CAvBoB,CAArB;AAyBA,SAAOQ,YAAP;AACA,CA1EM;;;;AA4EA,MAAMM,kBAAkB,GAAG,CAAEC,QAAF,EAAYC,IAAZ,KAAsB;AACvD,SAAO,CAAED,QAAQ,IAAI,EAAd,EAAmBL,GAAnB,CAA0BO,MAAF,KAAgB,EAC9C,GAAGA,MAD2C;AAE9CC,IAAAA,IAAI,EAAE,kCAAgB,iBAAKD,MAAL,EAAaD,IAAb,CAAhB;AAFwC,GAAhB,CAAxB,CAAP;AAIA,CALM","sourcesContent":["/**\n * External dependencies\n */\nimport { get } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { useMemo } from '@wordpress/element';\n\nexport const usePostTypes = () => {\n\tconst postTypes = useSelect(\n\t\t( select ) => select( coreStore ).getPostTypes( { per_page: -1 } ),\n\t\t[]\n\t);\n\treturn useMemo( () => {\n\t\tconst excludedPostTypes = [ 'attachment', 'page' ];\n\t\treturn postTypes?.filter(\n\t\t\t( { viewable, slug } ) =>\n\t\t\t\tviewable && ! excludedPostTypes.includes( slug )\n\t\t);\n\t}, [ postTypes ] );\n};\n\n/**\n * @typedef {Object} PostTypeEntitiesInfo\n * @property {boolean} hasEntities If a postType has available entities.\n * @property {number[]} existingPosts An array of the existing entities ids.\n */\n\n/**\n * Helper hook that returns information about a post type having\n * posts that we can create a specific template for.\n *\n * First we need to find the existing posts with an associated template,\n * to query afterwards for any remaing post, by excluding them.\n *\n * @param {string[]} existingTemplates The existing templates.\n * @return {Record<string,PostTypeEntitiesInfo>} An object with the postTypes as `keys` and PostTypeEntitiesInfo as values.\n */\nexport const usePostTypesEntitiesInfo = ( existingTemplates ) => {\n\tconst postTypes = usePostTypes();\n\tconst slugsToExcludePerEntity = useMemo( () => {\n\t\treturn postTypes?.reduce( ( accumulator, _postType ) => {\n\t\t\tconst slugsWithTemplates = ( existingTemplates || [] ).reduce(\n\t\t\t\t( _accumulator, existingTemplate ) => {\n\t\t\t\t\tconst prefix = `single-${ _postType.slug }-`;\n\t\t\t\t\tif ( existingTemplate.slug.startsWith( prefix ) ) {\n\t\t\t\t\t\t_accumulator.push(\n\t\t\t\t\t\t\texistingTemplate.slug.substring( prefix.length )\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn _accumulator;\n\t\t\t\t},\n\t\t\t\t[]\n\t\t\t);\n\t\t\tif ( slugsWithTemplates.length ) {\n\t\t\t\taccumulator[ _postType.slug ] = slugsWithTemplates;\n\t\t\t}\n\t\t\treturn accumulator;\n\t\t}, {} );\n\t}, [ postTypes, existingTemplates ] );\n\tconst postsToExcludePerEntity = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! slugsToExcludePerEntity ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst postsToExclude = Object.entries(\n\t\t\t\tslugsToExcludePerEntity\n\t\t\t).reduce( ( accumulator, [ slug, slugsWithTemplates ] ) => {\n\t\t\t\tconst postsWithTemplates = select( coreStore ).getEntityRecords(\n\t\t\t\t\t'postType',\n\t\t\t\t\tslug,\n\t\t\t\t\t{\n\t\t\t\t\t\t_fields: 'id',\n\t\t\t\t\t\tcontext: 'view',\n\t\t\t\t\t\tslug: slugsWithTemplates,\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t\tif ( postsWithTemplates?.length ) {\n\t\t\t\t\taccumulator[ slug ] = postsWithTemplates;\n\t\t\t\t}\n\t\t\t\treturn accumulator;\n\t\t\t}, {} );\n\t\t\treturn postsToExclude;\n\t\t},\n\t\t[ slugsToExcludePerEntity ]\n\t);\n\tconst entitiesInfo = useSelect(\n\t\t( select ) => {\n\t\t\treturn postTypes?.reduce( ( accumulator, { slug } ) => {\n\t\t\t\tconst existingPosts =\n\t\t\t\t\tpostsToExcludePerEntity?.[ slug ]?.map(\n\t\t\t\t\t\t( { id } ) => id\n\t\t\t\t\t) || [];\n\t\t\t\taccumulator[ slug ] = {\n\t\t\t\t\thasEntities: !! select( coreStore ).getEntityRecords(\n\t\t\t\t\t\t'postType',\n\t\t\t\t\t\tslug,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tper_page: 1,\n\t\t\t\t\t\t\t_fields: 'id',\n\t\t\t\t\t\t\tcontext: 'view',\n\t\t\t\t\t\t\texclude: existingPosts,\n\t\t\t\t\t\t}\n\t\t\t\t\t)?.length,\n\t\t\t\t\texistingPosts,\n\t\t\t\t};\n\t\t\t\treturn accumulator;\n\t\t\t}, {} );\n\t\t},\n\t\t[ postTypes, postsToExcludePerEntity ]\n\t);\n\treturn entitiesInfo;\n};\n\nexport const mapToIHasNameAndId = ( entities, path ) => {\n\treturn ( entities || [] ).map( ( entity ) => ( {\n\t\t...entity,\n\t\tname: decodeEntities( get( entity, path ) ),\n\t} ) );\n};\n"]}