@wordpress/edit-site 4.9.0 → 4.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- package/build/components/add-new-template/add-custom-generic-template-modal.js +84 -0
- package/build/components/add-new-template/add-custom-generic-template-modal.js.map +1 -0
- package/build/components/add-new-template/add-custom-template-modal.js +92 -53
- package/build/components/add-new-template/add-custom-template-modal.js.map +1 -1
- package/build/components/add-new-template/new-template.js +77 -81
- package/build/components/add-new-template/new-template.js.map +1 -1
- package/build/components/add-new-template/utils.js +310 -44
- package/build/components/add-new-template/utils.js.map +1 -1
- package/build/components/code-editor/index.js +17 -4
- package/build/components/code-editor/index.js.map +1 -1
- package/build/components/editor/index.js +16 -0
- package/build/components/editor/index.js.map +1 -1
- package/build/components/error-boundary/index.js +6 -0
- package/build/components/error-boundary/index.js.map +1 -1
- package/build/components/global-styles/dimensions-panel.js +2 -6
- package/build/components/global-styles/dimensions-panel.js.map +1 -1
- package/build/components/global-styles/global-styles-provider.js +4 -2
- package/build/components/global-styles/global-styles-provider.js.map +1 -1
- package/build/components/global-styles/hooks.js +10 -1
- package/build/components/global-styles/hooks.js.map +1 -1
- package/build/components/global-styles/screen-color-palette.js +13 -17
- package/build/components/global-styles/screen-color-palette.js.map +1 -1
- package/build/components/global-styles/screen-colors.js +9 -1
- package/build/components/global-styles/screen-colors.js.map +1 -1
- package/build/components/global-styles/screen-link-color.js +48 -14
- package/build/components/global-styles/screen-link-color.js.map +1 -1
- package/build/components/global-styles/use-global-styles-output.js +171 -33
- package/build/components/global-styles/use-global-styles-output.js.map +1 -1
- package/build/components/global-styles/utils.js +1 -1
- package/build/components/global-styles/utils.js.map +1 -1
- package/build/components/keyboard-shortcut-help-modal/index.js +1 -3
- package/build/components/keyboard-shortcut-help-modal/index.js.map +1 -1
- package/build/components/template-details/edit-template-title.js +11 -3
- package/build/components/template-details/edit-template-title.js.map +1 -1
- package/build/components/template-details/index.js +1 -20
- package/build/components/template-details/index.js.map +1 -1
- package/build/store/selectors.js +4 -1
- package/build/store/selectors.js.map +1 -1
- package/build-module/components/add-new-template/add-custom-generic-template-modal.js +77 -0
- package/build-module/components/add-new-template/add-custom-generic-template-modal.js.map +1 -0
- package/build-module/components/add-new-template/add-custom-template-modal.js +92 -53
- package/build-module/components/add-new-template/add-custom-template-modal.js.map +1 -1
- package/build-module/components/add-new-template/new-template.js +80 -83
- package/build-module/components/add-new-template/new-template.js.map +1 -1
- package/build-module/components/add-new-template/utils.js +286 -40
- package/build-module/components/add-new-template/utils.js.map +1 -1
- package/build-module/components/code-editor/index.js +18 -5
- package/build-module/components/code-editor/index.js.map +1 -1
- package/build-module/components/editor/index.js +16 -0
- package/build-module/components/editor/index.js.map +1 -1
- package/build-module/components/error-boundary/index.js +5 -0
- package/build-module/components/error-boundary/index.js.map +1 -1
- package/build-module/components/global-styles/dimensions-panel.js +2 -6
- package/build-module/components/global-styles/dimensions-panel.js.map +1 -1
- package/build-module/components/global-styles/global-styles-provider.js +4 -2
- package/build-module/components/global-styles/global-styles-provider.js.map +1 -1
- package/build-module/components/global-styles/hooks.js +10 -1
- package/build-module/components/global-styles/hooks.js.map +1 -1
- package/build-module/components/global-styles/screen-color-palette.js +14 -19
- package/build-module/components/global-styles/screen-color-palette.js.map +1 -1
- package/build-module/components/global-styles/screen-colors.js +9 -1
- package/build-module/components/global-styles/screen-colors.js.map +1 -1
- package/build-module/components/global-styles/screen-link-color.js +47 -14
- package/build-module/components/global-styles/screen-link-color.js.map +1 -1
- package/build-module/components/global-styles/use-global-styles-output.js +171 -35
- package/build-module/components/global-styles/use-global-styles-output.js.map +1 -1
- package/build-module/components/global-styles/utils.js +2 -2
- package/build-module/components/global-styles/utils.js.map +1 -1
- package/build-module/components/keyboard-shortcut-help-modal/index.js +1 -2
- package/build-module/components/keyboard-shortcut-help-modal/index.js.map +1 -1
- package/build-module/components/template-details/edit-template-title.js +12 -3
- package/build-module/components/template-details/edit-template-title.js.map +1 -1
- package/build-module/components/template-details/index.js +2 -21
- package/build-module/components/template-details/index.js.map +1 -1
- package/build-module/store/selectors.js +5 -2
- package/build-module/store/selectors.js.map +1 -1
- package/build-style/style-rtl.css +21 -23
- package/build-style/style.css +21 -23
- package/package.json +29 -29
- package/src/components/add-new-template/add-custom-generic-template-modal.js +97 -0
- package/src/components/add-new-template/add-custom-template-modal.js +92 -58
- package/src/components/add-new-template/new-template.js +142 -94
- package/src/components/add-new-template/style.scss +21 -0
- package/src/components/add-new-template/utils.js +290 -46
- package/src/components/code-editor/index.js +15 -5
- package/src/components/editor/index.js +11 -0
- package/src/components/error-boundary/index.js +5 -0
- package/src/components/global-styles/dimensions-panel.js +2 -7
- package/src/components/global-styles/global-styles-provider.js +8 -9
- package/src/components/global-styles/hooks.js +15 -0
- package/src/components/global-styles/screen-color-palette.js +25 -27
- package/src/components/global-styles/screen-colors.js +9 -3
- package/src/components/global-styles/screen-link-color.js +65 -23
- package/src/components/global-styles/style.scss +7 -11
- package/src/components/global-styles/test/use-global-styles-output.js +168 -0
- package/src/components/global-styles/use-global-styles-output.js +234 -59
- package/src/components/global-styles/utils.js +2 -2
- package/src/components/keyboard-shortcut-help-modal/index.js +1 -2
- package/src/components/keyboard-shortcut-help-modal/style.scss +0 -5
- package/src/components/list/style.scss +0 -8
- package/src/components/template-details/edit-template-title.js +10 -2
- package/src/components/template-details/index.js +4 -21
- package/src/components/test/error-boundary.js +38 -0
- package/src/store/selectors.js +11 -5
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _element = require("@wordpress/element");
|
|
9
|
+
|
|
10
|
+
var _lodash = require("lodash");
|
|
11
|
+
|
|
12
|
+
var _i18n = require("@wordpress/i18n");
|
|
13
|
+
|
|
14
|
+
var _components = require("@wordpress/components");
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* External dependencies
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* WordPress dependencies
|
|
22
|
+
*/
|
|
23
|
+
function AddCustomGenericTemplateModal(_ref) {
|
|
24
|
+
let {
|
|
25
|
+
onClose,
|
|
26
|
+
createTemplate
|
|
27
|
+
} = _ref;
|
|
28
|
+
const [title, setTitle] = (0, _element.useState)('');
|
|
29
|
+
const defaultTitle = (0, _i18n.__)('Custom Template');
|
|
30
|
+
const [isBusy, setIsBusy] = (0, _element.useState)(false);
|
|
31
|
+
|
|
32
|
+
async function onCreateTemplate(event) {
|
|
33
|
+
event.preventDefault();
|
|
34
|
+
|
|
35
|
+
if (isBusy) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
setIsBusy(true);
|
|
40
|
+
createTemplate({
|
|
41
|
+
slug: 'wp-custom-template-' + (0, _lodash.kebabCase)(title || defaultTitle),
|
|
42
|
+
title: title || defaultTitle
|
|
43
|
+
}, false);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return (0, _element.createElement)(_components.Modal, {
|
|
47
|
+
title: (0, _i18n.__)('Create custom template'),
|
|
48
|
+
closeLabel: (0, _i18n.__)('Close'),
|
|
49
|
+
onRequestClose: () => {
|
|
50
|
+
onClose();
|
|
51
|
+
},
|
|
52
|
+
overlayClassName: "edit-site-custom-generic-template__modal"
|
|
53
|
+
}, (0, _element.createElement)("form", {
|
|
54
|
+
onSubmit: onCreateTemplate
|
|
55
|
+
}, (0, _element.createElement)(_components.Flex, {
|
|
56
|
+
align: "flex-start",
|
|
57
|
+
gap: 8
|
|
58
|
+
}, (0, _element.createElement)(_components.FlexItem, null, (0, _element.createElement)(_components.TextControl, {
|
|
59
|
+
label: (0, _i18n.__)('Name'),
|
|
60
|
+
value: title,
|
|
61
|
+
onChange: setTitle,
|
|
62
|
+
placeholder: defaultTitle,
|
|
63
|
+
disabled: isBusy,
|
|
64
|
+
help: (0, _i18n.__)('Describe the template, e.g. "Post with sidebar".')
|
|
65
|
+
}))), (0, _element.createElement)(_components.Flex, {
|
|
66
|
+
className: "edit-site-custom-generic-template__modal-actions",
|
|
67
|
+
justify: "flex-end",
|
|
68
|
+
expanded: false
|
|
69
|
+
}, (0, _element.createElement)(_components.FlexItem, null, (0, _element.createElement)(_components.Button, {
|
|
70
|
+
variant: "tertiary",
|
|
71
|
+
onClick: () => {
|
|
72
|
+
onClose();
|
|
73
|
+
}
|
|
74
|
+
}, (0, _i18n.__)('Cancel'))), (0, _element.createElement)(_components.FlexItem, null, (0, _element.createElement)(_components.Button, {
|
|
75
|
+
variant: "primary",
|
|
76
|
+
type: "submit",
|
|
77
|
+
isBusy: isBusy,
|
|
78
|
+
"aria-disabled": isBusy
|
|
79
|
+
}, (0, _i18n.__)('Create'))))));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
var _default = AddCustomGenericTemplateModal;
|
|
83
|
+
exports.default = _default;
|
|
84
|
+
//# sourceMappingURL=add-custom-generic-template-modal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["@wordpress/edit-site/src/components/add-new-template/add-custom-generic-template-modal.js"],"names":["AddCustomGenericTemplateModal","onClose","createTemplate","title","setTitle","defaultTitle","isBusy","setIsBusy","onCreateTemplate","event","preventDefault","slug"],"mappings":";;;;;;;AAQA;;AALA;;AAMA;;AACA;;AAVA;AACA;AACA;;AAGA;AACA;AACA;AAWA,SAASA,6BAAT,OAAsE;AAAA,MAA9B;AAAEC,IAAAA,OAAF;AAAWC,IAAAA;AAAX,GAA8B;AACrE,QAAM,CAAEC,KAAF,EAASC,QAAT,IAAsB,uBAAU,EAAV,CAA5B;AACA,QAAMC,YAAY,GAAG,cAAI,iBAAJ,CAArB;AACA,QAAM,CAAEC,MAAF,EAAUC,SAAV,IAAwB,uBAAU,KAAV,CAA9B;;AACA,iBAA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EAAC,SADT;AAEC,IAAA,IAAI,EAAC,QAFN;AAGC,IAAA,MAAM,EAAGK,MAHV;AAIC,qBAAgBA;AAJjB,KAMG,cAAI,QAAJ,CANH,CADD,CAfD,CAhBD,CARD,CADD;AAsDA;;eAEcN,6B","sourcesContent":["/**\n * External dependencies\n */\nimport { kebabCase } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport { useState } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\nimport {\n\tButton,\n\tFlex,\n\tFlexItem,\n\tModal,\n\tTextControl,\n} from '@wordpress/components';\n\nfunction AddCustomGenericTemplateModal( { onClose, createTemplate } ) {\n\tconst [ title, setTitle ] = useState( '' );\n\tconst defaultTitle = __( 'Custom Template' );\n\tconst [ isBusy, setIsBusy ] = useState( false );\n\tasync function onCreateTemplate( event ) {\n\t\tevent.preventDefault();\n\n\t\tif ( isBusy ) {\n\t\t\treturn;\n\t\t}\n\n\t\tsetIsBusy( true );\n\n\t\tcreateTemplate(\n\t\t\t{\n\t\t\t\tslug:\n\t\t\t\t\t'wp-custom-template-' + kebabCase( title || defaultTitle ),\n\t\t\t\ttitle: title || defaultTitle,\n\t\t\t},\n\t\t\tfalse\n\t\t);\n\t}\n\treturn (\n\t\t<Modal\n\t\t\ttitle={ __( 'Create custom template' ) }\n\t\t\tcloseLabel={ __( 'Close' ) }\n\t\t\tonRequestClose={ () => {\n\t\t\t\tonClose();\n\t\t\t} }\n\t\t\toverlayClassName=\"edit-site-custom-generic-template__modal\"\n\t\t>\n\t\t\t<form onSubmit={ onCreateTemplate }>\n\t\t\t\t<Flex align=\"flex-start\" gap={ 8 }>\n\t\t\t\t\t<FlexItem>\n\t\t\t\t\t\t<TextControl\n\t\t\t\t\t\t\tlabel={ __( 'Name' ) }\n\t\t\t\t\t\t\tvalue={ title }\n\t\t\t\t\t\t\tonChange={ setTitle }\n\t\t\t\t\t\t\tplaceholder={ defaultTitle }\n\t\t\t\t\t\t\tdisabled={ isBusy }\n\t\t\t\t\t\t\thelp={ __(\n\t\t\t\t\t\t\t\t'Describe the template, e.g. \"Post with sidebar\".'\n\t\t\t\t\t\t\t) }\n\t\t\t\t\t\t/>\n\t\t\t\t\t</FlexItem>\n\t\t\t\t</Flex>\n\n\t\t\t\t<Flex\n\t\t\t\t\tclassName=\"edit-site-custom-generic-template__modal-actions\"\n\t\t\t\t\tjustify=\"flex-end\"\n\t\t\t\t\texpanded={ false }\n\t\t\t\t>\n\t\t\t\t\t<FlexItem>\n\t\t\t\t\t\t<Button\n\t\t\t\t\t\t\tvariant=\"tertiary\"\n\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\tonClose();\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{ __( 'Cancel' ) }\n\t\t\t\t\t\t</Button>\n\t\t\t\t\t</FlexItem>\n\t\t\t\t\t<FlexItem>\n\t\t\t\t\t\t<Button\n\t\t\t\t\t\t\tvariant=\"primary\"\n\t\t\t\t\t\t\ttype=\"submit\"\n\t\t\t\t\t\t\tisBusy={ isBusy }\n\t\t\t\t\t\t\taria-disabled={ isBusy }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{ __( 'Create' ) }\n\t\t\t\t\t\t</Button>\n\t\t\t\t\t</FlexItem>\n\t\t\t\t</Flex>\n\t\t\t</form>\n\t\t</Modal>\n\t);\n}\n\nexport default AddCustomGenericTemplateModal;\n"]}
|
|
@@ -29,11 +29,32 @@ var _utils = require("./utils");
|
|
|
29
29
|
* Internal dependencies
|
|
30
30
|
*/
|
|
31
31
|
const EMPTY_ARRAY = [];
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
|
|
33
|
+
function selectSuggestion(suggestion, onSelect, entityForSuggestions) {
|
|
34
|
+
const {
|
|
35
|
+
labels,
|
|
36
|
+
slug,
|
|
37
|
+
config: {
|
|
38
|
+
templateSlug,
|
|
39
|
+
templatePrefix
|
|
40
|
+
}
|
|
41
|
+
} = entityForSuggestions;
|
|
42
|
+
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 or taxonomy and %2$s is the name of the post or term, e.g. "Post: Hello, WordPress", "Category: shoes"
|
|
43
|
+
(0, _i18n.__)('%1$s: %2$s'), labels.singular_name, suggestion.name);
|
|
44
|
+
let newTemplateSlug = `${templateSlug || slug}-${suggestion.slug}`;
|
|
45
|
+
|
|
46
|
+
if (templatePrefix) {
|
|
47
|
+
newTemplateSlug = templatePrefix + newTemplateSlug;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const newTemplate = {
|
|
51
|
+
title,
|
|
52
|
+
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"
|
|
53
|
+
(0, _i18n.__)('Template for %1$s'), title),
|
|
54
|
+
slug: newTemplateSlug
|
|
55
|
+
};
|
|
56
|
+
onSelect(newTemplate);
|
|
57
|
+
}
|
|
37
58
|
|
|
38
59
|
function SuggestionListItem(_ref) {
|
|
39
60
|
let {
|
|
@@ -49,16 +70,7 @@ function SuggestionListItem(_ref) {
|
|
|
49
70
|
as: _components.Button
|
|
50
71
|
}, composite, {
|
|
51
72
|
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
|
-
}
|
|
73
|
+
onClick: () => selectSuggestion(suggestion, onSelect, entityForSuggestions)
|
|
62
74
|
}), (0, _element.createElement)("span", {
|
|
63
75
|
className: `${baseCssClass}__title`
|
|
64
76
|
}, (0, _element.createElement)(_components.TextHighlight, {
|
|
@@ -69,62 +81,89 @@ function SuggestionListItem(_ref) {
|
|
|
69
81
|
}, suggestion.link));
|
|
70
82
|
}
|
|
71
83
|
|
|
72
|
-
function
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
84
|
+
function useDebouncedInput() {
|
|
85
|
+
const [input, setInput] = (0, _element.useState)('');
|
|
86
|
+
const [debounced, setter] = (0, _element.useState)('');
|
|
87
|
+
const setDebounced = (0, _compose.useDebounce)(setter, 250);
|
|
88
|
+
(0, _element.useEffect)(() => {
|
|
89
|
+
if (debounced !== input) {
|
|
90
|
+
setDebounced(input);
|
|
91
|
+
}
|
|
92
|
+
}, [debounced, input]);
|
|
93
|
+
return [input, setInput, debounced];
|
|
94
|
+
}
|
|
82
95
|
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
96
|
+
function useSearchSuggestions(entityForSuggestions, search) {
|
|
97
|
+
const {
|
|
98
|
+
config,
|
|
99
|
+
postsToExclude
|
|
100
|
+
} = entityForSuggestions;
|
|
101
|
+
const query = (0, _element.useMemo)(() => ({
|
|
102
|
+
order: 'asc',
|
|
103
|
+
_fields: 'id,name,title,slug,link',
|
|
104
|
+
context: 'view',
|
|
87
105
|
search,
|
|
88
|
-
|
|
89
|
-
|
|
106
|
+
orderBy: config.getOrderBy({
|
|
107
|
+
search
|
|
108
|
+
}),
|
|
109
|
+
exclude: postsToExclude,
|
|
90
110
|
per_page: search ? 20 : 10
|
|
91
|
-
};
|
|
111
|
+
}), [search, config, postsToExclude]);
|
|
92
112
|
const {
|
|
93
113
|
records: searchResults,
|
|
94
114
|
hasResolved: searchHasResolved
|
|
95
115
|
} = (0, _coreData.useEntityRecords)(entityForSuggestions.type, entityForSuggestions.slug, query);
|
|
96
|
-
(0, _element.
|
|
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
|
-
|
|
116
|
+
const [suggestions, setSuggestions] = (0, _element.useState)(EMPTY_ARRAY);
|
|
106
117
|
(0, _element.useEffect)(() => {
|
|
107
118
|
if (!searchHasResolved) return;
|
|
108
|
-
|
|
109
|
-
|
|
119
|
+
let newSuggestions = EMPTY_ARRAY;
|
|
120
|
+
|
|
121
|
+
if (searchResults !== null && searchResults !== void 0 && searchResults.length) {
|
|
122
|
+
newSuggestions = searchResults;
|
|
123
|
+
|
|
124
|
+
if (config.recordNamePath) {
|
|
125
|
+
newSuggestions = (0, _utils.mapToIHasNameAndId)(newSuggestions, config.recordNamePath);
|
|
126
|
+
}
|
|
127
|
+
} // Update suggestions only when the query has resolved, so as to keep
|
|
128
|
+
// the previous results in the UI.
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
setSuggestions(newSuggestions);
|
|
132
|
+
}, [searchResults, searchHasResolved]);
|
|
133
|
+
return suggestions;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function SuggestionList(_ref2) {
|
|
137
|
+
let {
|
|
138
|
+
entityForSuggestions,
|
|
139
|
+
onSelect
|
|
140
|
+
} = _ref2;
|
|
141
|
+
const composite = (0, _components.__unstableUseCompositeState)({
|
|
142
|
+
orientation: 'vertical'
|
|
143
|
+
});
|
|
144
|
+
const [search, setSearch, debouncedSearch] = useDebouncedInput();
|
|
145
|
+
const suggestions = useSearchSuggestions(entityForSuggestions, debouncedSearch);
|
|
146
|
+
const {
|
|
147
|
+
labels
|
|
148
|
+
} = entityForSuggestions;
|
|
110
149
|
return (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)(_components.SearchControl, {
|
|
111
|
-
onChange:
|
|
112
|
-
value:
|
|
113
|
-
label:
|
|
114
|
-
placeholder:
|
|
150
|
+
onChange: setSearch,
|
|
151
|
+
value: search,
|
|
152
|
+
label: labels.search_items,
|
|
153
|
+
placeholder: labels.search_items
|
|
115
154
|
}), !!(suggestions !== null && suggestions !== void 0 && suggestions.length) && (0, _element.createElement)(_components.__unstableComposite, (0, _extends2.default)({}, composite, {
|
|
116
155
|
role: "listbox",
|
|
117
156
|
className: "edit-site-custom-template-modal__suggestions_list"
|
|
118
157
|
}), suggestions.map(suggestion => (0, _element.createElement)(SuggestionListItem, {
|
|
119
158
|
key: suggestion.slug,
|
|
120
159
|
suggestion: suggestion,
|
|
121
|
-
search:
|
|
160
|
+
search: debouncedSearch,
|
|
122
161
|
onSelect: onSelect,
|
|
123
162
|
entityForSuggestions: entityForSuggestions,
|
|
124
163
|
composite: composite
|
|
125
|
-
}))),
|
|
164
|
+
}))), debouncedSearch && !(suggestions !== null && suggestions !== void 0 && suggestions.length) && (0, _element.createElement)("p", {
|
|
126
165
|
className: "edit-site-custom-template-modal__no-results"
|
|
127
|
-
},
|
|
166
|
+
}, labels.not_found));
|
|
128
167
|
}
|
|
129
168
|
|
|
130
169
|
function AddCustomTemplateModal(_ref3) {
|
|
@@ -163,7 +202,7 @@ function AddCustomTemplateModal(_ref3) {
|
|
|
163
202
|
level: 5
|
|
164
203
|
}, entityForSuggestions.labels.all_items), (0, _element.createElement)(_components.__experimentalText, {
|
|
165
204
|
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.
|
|
205
|
+
}, // 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.
|
|
167
206
|
(0, _i18n.__)('For all items'))), (0, _element.createElement)(_components.FlexItem, {
|
|
168
207
|
isBlock: true,
|
|
169
208
|
onClick: () => {
|
|
@@ -173,7 +212,7 @@ function AddCustomTemplateModal(_ref3) {
|
|
|
173
212
|
level: 5
|
|
174
213
|
}, entityForSuggestions.labels.singular_name), (0, _element.createElement)(_components.__experimentalText, {
|
|
175
214
|
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.
|
|
215
|
+
}, // 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.
|
|
177
216
|
(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
217
|
entityForSuggestions: entityForSuggestions,
|
|
179
218
|
onSelect: onSelect
|
|
@@ -1 +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,OAAO,EAAE,oBAFS;AAGlBC,EAAAA,OAAO,EAAE;AAHS,C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\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
|
+
{"version":3,"sources":["@wordpress/edit-site/src/components/add-new-template/add-custom-template-modal.js"],"names":["EMPTY_ARRAY","selectSuggestion","suggestion","onSelect","entityForSuggestions","labels","slug","config","templateSlug","templatePrefix","title","singular_name","name","newTemplateSlug","newTemplate","description","SuggestionListItem","search","composite","baseCssClass","Button","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":";;;;;;;;;AAGA;;;;AACA;;AACA;;AAaA;;AACA;;AAKA;;AAxBA;AACA;AACA;;AAmBA;AACA;AACA;AAGA,MAAMA,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,GAAG,oBACb;AACA,gBAAI,YAAJ,CAFa,EAGbL,MAAM,C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AAA,KAAK,EAAG;AAAjB,KACGX,oBAAoB,CAACC,MAArB,CAA4BwD,SAD/B,CARD,EAWC,4BAAC,8BAAD;AAAM,IAAA,EAAE,EAAC;AAAT,KAEE;AACA,gBAAI,eAAJ,CAHF,CAXD,CALD,EAuBC,4BAAC,oBAAD;AACC,IAAA,OAAO,MADR;AAEC,IAAA,OAAO,EAAG,MAAM;AACfH,MAAAA,qBAAqB,CAAE,IAAF,CAArB;AACA;AAJF,KAMC,4BAAC,iCAAD;AAAS,IAAA,KAAK,EAAG;AAAjB,KACGtD,oBAAoB,CAACC,MAArB,CAA4BM,aAD/B,CAND,EASC,4BAAC,8BAAD;AAAM,IAAA,EAAE,EAAC;AAAT,KAEE;AACA,gBAAI,qBAAJ,CAHF,CATD,CAvBD,CAND,CAXF,EA2DG8C,kBAAkB,IACnB,qDACC,uCACG,cACD,+DADC,CADH,CADD,EAMC,4BAAC,cAAD;AACC,IAAA,oBAAoB,EAAGrD,oBADxB;AAEC,IAAA,QAAQ,EAAGD;AAFZ,IAND,CA5DF,CADD;AA2EA;;eAEcoD,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 = [];\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"]}
|
|
@@ -9,16 +9,12 @@ exports.default = NewTemplate;
|
|
|
9
9
|
|
|
10
10
|
var _element = require("@wordpress/element");
|
|
11
11
|
|
|
12
|
-
var _lodash = require("lodash");
|
|
13
|
-
|
|
14
12
|
var _components = require("@wordpress/components");
|
|
15
13
|
|
|
16
14
|
var _data = require("@wordpress/data");
|
|
17
15
|
|
|
18
16
|
var _coreData = require("@wordpress/core-data");
|
|
19
17
|
|
|
20
|
-
var _editor = require("@wordpress/editor");
|
|
21
|
-
|
|
22
18
|
var _icons = require("@wordpress/icons");
|
|
23
19
|
|
|
24
20
|
var _i18n = require("@wordpress/i18n");
|
|
@@ -29,14 +25,12 @@ var _addCustomTemplateModal = _interopRequireDefault(require("./add-custom-templ
|
|
|
29
25
|
|
|
30
26
|
var _utils = require("./utils");
|
|
31
27
|
|
|
28
|
+
var _addCustomGenericTemplateModal = _interopRequireDefault(require("./add-custom-generic-template-modal"));
|
|
29
|
+
|
|
32
30
|
var _routes = require("../routes");
|
|
33
31
|
|
|
34
32
|
var _store = require("../../store");
|
|
35
33
|
|
|
36
|
-
/**
|
|
37
|
-
* External dependencies
|
|
38
|
-
*/
|
|
39
|
-
|
|
40
34
|
/**
|
|
41
35
|
* WordPress dependencies
|
|
42
36
|
*/
|
|
@@ -65,20 +59,10 @@ function NewTemplate(_ref) {
|
|
|
65
59
|
let {
|
|
66
60
|
postType
|
|
67
61
|
} = _ref;
|
|
68
|
-
const history = (0, _routes.useHistory)();
|
|
69
|
-
const postTypes = (0, _utils.usePostTypes)();
|
|
70
62
|
const [showCustomTemplateModal, setShowCustomTemplateModal] = (0, _element.useState)(false);
|
|
63
|
+
const [showCustomGenericTemplateModal, setShowCustomGenericTemplateModal] = (0, _element.useState)(false);
|
|
71
64
|
const [entityForSuggestions, setEntityForSuggestions] = (0, _element.useState)({});
|
|
72
|
-
const
|
|
73
|
-
existingTemplates,
|
|
74
|
-
defaultTemplateTypes
|
|
75
|
-
} = (0, _data.useSelect)(select => ({
|
|
76
|
-
existingTemplates: select(_coreData.store).getEntityRecords('postType', 'wp_template', {
|
|
77
|
-
per_page: -1
|
|
78
|
-
}),
|
|
79
|
-
defaultTemplateTypes: select(_editor.store).__experimentalGetDefaultTemplateTypes()
|
|
80
|
-
}), []);
|
|
81
|
-
const postTypesEntitiesInfo = (0, _utils.usePostTypesEntitiesInfo)(existingTemplates);
|
|
65
|
+
const history = (0, _routes.useHistory)();
|
|
82
66
|
const {
|
|
83
67
|
saveEntityRecord
|
|
84
68
|
} = (0, _data.useDispatch)(_coreData.store);
|
|
@@ -90,6 +74,8 @@ function NewTemplate(_ref) {
|
|
|
90
74
|
} = (0, _data.useDispatch)(_store.store);
|
|
91
75
|
|
|
92
76
|
async function createTemplate(template) {
|
|
77
|
+
let isWPSuggestion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
78
|
+
|
|
93
79
|
try {
|
|
94
80
|
const {
|
|
95
81
|
title,
|
|
@@ -103,7 +89,7 @@ function NewTemplate(_ref) {
|
|
|
103
89
|
status: 'publish',
|
|
104
90
|
title,
|
|
105
91
|
// This adds a post meta field in template that is part of `is_custom` value calculation.
|
|
106
|
-
is_wp_suggestion:
|
|
92
|
+
is_wp_suggestion: isWPSuggestion
|
|
107
93
|
}, {
|
|
108
94
|
throwOnError: true
|
|
109
95
|
}); // Set template before navigating away to avoid initial stale value.
|
|
@@ -122,68 +108,12 @@ function NewTemplate(_ref) {
|
|
|
122
108
|
}
|
|
123
109
|
}
|
|
124
110
|
|
|
125
|
-
const
|
|
126
|
-
let {
|
|
127
|
-
slug
|
|
128
|
-
} = _ref2;
|
|
129
|
-
return slug;
|
|
130
|
-
});
|
|
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
|
-
}, []);
|
|
111
|
+
const missingTemplates = useMissingTemplates(setEntityForSuggestions, setShowCustomTemplateModal);
|
|
176
112
|
|
|
177
|
-
if (!missingTemplates.length
|
|
113
|
+
if (!missingTemplates.length) {
|
|
178
114
|
return null;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
missingTemplates === null || missingTemplates === void 0 ? void 0 : missingTemplates.sort((template1, template2) => {
|
|
183
|
-
return DEFAULT_TEMPLATE_SLUGS.indexOf(template1.slug) - DEFAULT_TEMPLATE_SLUGS.indexOf(template2.slug);
|
|
184
|
-
}); // Append all extra templates at the end of the list for now.
|
|
115
|
+
}
|
|
185
116
|
|
|
186
|
-
missingTemplates.push(...extraTemplates);
|
|
187
117
|
return (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)(_components.DropdownMenu, {
|
|
188
118
|
className: "edit-site-new-template-dropdown",
|
|
189
119
|
icon: null,
|
|
@@ -214,10 +144,76 @@ function NewTemplate(_ref) {
|
|
|
214
144
|
key: slug,
|
|
215
145
|
onClick: () => onClick ? onClick(template) : createTemplate(template)
|
|
216
146
|
}, title);
|
|
217
|
-
})))
|
|
147
|
+
})), (0, _element.createElement)(_components.MenuGroup, null, (0, _element.createElement)(_components.MenuItem, {
|
|
148
|
+
icon: _icons.layout,
|
|
149
|
+
iconPosition: "left",
|
|
150
|
+
info: (0, _i18n.__)('Custom templates can be applied to any post or page.'),
|
|
151
|
+
key: "custom-template",
|
|
152
|
+
onClick: () => setShowCustomGenericTemplateModal(true)
|
|
153
|
+
}, (0, _i18n.__)('Custom template'))))), showCustomTemplateModal && (0, _element.createElement)(_addCustomTemplateModal.default, {
|
|
218
154
|
onClose: () => setShowCustomTemplateModal(false),
|
|
219
155
|
onSelect: createTemplate,
|
|
220
156
|
entityForSuggestions: entityForSuggestions
|
|
157
|
+
}), showCustomGenericTemplateModal && (0, _element.createElement)(_addCustomGenericTemplateModal.default, {
|
|
158
|
+
onClose: () => setShowCustomGenericTemplateModal(false),
|
|
159
|
+
createTemplate: createTemplate
|
|
221
160
|
}));
|
|
222
161
|
}
|
|
162
|
+
|
|
163
|
+
function useMissingTemplates(setEntityForSuggestions, setShowCustomTemplateModal) {
|
|
164
|
+
const postTypes = (0, _utils.usePostTypes)();
|
|
165
|
+
const pagePostType = (0, _utils.usePostTypePage)();
|
|
166
|
+
const taxonomies = (0, _utils.useTaxonomies)();
|
|
167
|
+
const categoryTaxonomy = (0, _utils.useTaxonomyCategory)();
|
|
168
|
+
const tagTaxonomy = (0, _utils.useTaxonomyTag)();
|
|
169
|
+
const existingTemplates = (0, _utils.useExistingTemplates)();
|
|
170
|
+
const defaultTemplateTypes = (0, _utils.useDefaultTemplateTypes)();
|
|
171
|
+
const existingTemplateSlugs = (existingTemplates || []).map(_ref2 => {
|
|
172
|
+
let {
|
|
173
|
+
slug
|
|
174
|
+
} = _ref2;
|
|
175
|
+
return slug;
|
|
176
|
+
});
|
|
177
|
+
const missingDefaultTemplates = (defaultTemplateTypes || []).filter(template => DEFAULT_TEMPLATE_SLUGS.includes(template.slug) && !existingTemplateSlugs.includes(template.slug));
|
|
178
|
+
|
|
179
|
+
const onClickMenuItem = _entityForSuggestions => {
|
|
180
|
+
setShowCustomTemplateModal(true);
|
|
181
|
+
setEntityForSuggestions(_entityForSuggestions);
|
|
182
|
+
}; // TODO: find better names for these variables. `useExtraTemplates` returns an array of items.
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
const categoryMenuItem = (0, _utils.useExtraTemplates)(categoryTaxonomy, _utils.entitiesConfig.category, onClickMenuItem);
|
|
186
|
+
const tagMenuItem = (0, _utils.useExtraTemplates)(tagTaxonomy, _utils.entitiesConfig.tag, onClickMenuItem);
|
|
187
|
+
const pageMenuItem = (0, _utils.useExtraTemplates)(pagePostType, _utils.entitiesConfig.page, onClickMenuItem); // We need to replace existing default template types with
|
|
188
|
+
// the create specific template functionality. The original
|
|
189
|
+
// info (title, description, etc.) is preserved in the
|
|
190
|
+
// `useExtraTemplates` hook.
|
|
191
|
+
|
|
192
|
+
const enhancedMissingDefaultTemplateTypes = [...missingDefaultTemplates];
|
|
193
|
+
[categoryMenuItem, tagMenuItem, pageMenuItem].forEach(menuItem => {
|
|
194
|
+
if (!(menuItem !== null && menuItem !== void 0 && menuItem.length)) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const matchIndex = enhancedMissingDefaultTemplateTypes.findIndex(template => template.slug === menuItem[0].slug); // Some default template types might have been filtered above from
|
|
199
|
+
// `missingDefaultTemplates` because they only check for the general
|
|
200
|
+
// template. So here we either replace or append the item, augmented
|
|
201
|
+
// with the check if it has available specific item to create a
|
|
202
|
+
// template for.
|
|
203
|
+
|
|
204
|
+
if (matchIndex > -1) {
|
|
205
|
+
enhancedMissingDefaultTemplateTypes.splice(matchIndex, 1, menuItem[0]);
|
|
206
|
+
} else {
|
|
207
|
+
enhancedMissingDefaultTemplateTypes.push(menuItem[0]);
|
|
208
|
+
}
|
|
209
|
+
}); // Update the sort order to match the DEFAULT_TEMPLATE_SLUGS order.
|
|
210
|
+
|
|
211
|
+
enhancedMissingDefaultTemplateTypes === null || enhancedMissingDefaultTemplateTypes === void 0 ? void 0 : enhancedMissingDefaultTemplateTypes.sort((template1, template2) => {
|
|
212
|
+
return DEFAULT_TEMPLATE_SLUGS.indexOf(template1.slug) - DEFAULT_TEMPLATE_SLUGS.indexOf(template2.slug);
|
|
213
|
+
});
|
|
214
|
+
const extraPostTypeTemplates = (0, _utils.useExtraTemplates)(postTypes, _utils.entitiesConfig.postType, onClickMenuItem);
|
|
215
|
+
const extraTaxonomyTemplates = (0, _utils.useExtraTemplates)(taxonomies, _utils.entitiesConfig.taxonomy, onClickMenuItem);
|
|
216
|
+
const missingTemplates = [...enhancedMissingDefaultTemplateTypes, ...extraPostTypeTemplates, ...extraTaxonomyTemplates];
|
|
217
|
+
return missingTemplates;
|
|
218
|
+
}
|
|
223
219
|
//# sourceMappingURL=new-template.js.map
|