@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
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { createElement } from "@wordpress/element";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* External dependencies
|
|
5
|
+
*/
|
|
6
|
+
import { kebabCase } from 'lodash';
|
|
7
|
+
/**
|
|
8
|
+
* WordPress dependencies
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { useState } from '@wordpress/element';
|
|
12
|
+
import { __ } from '@wordpress/i18n';
|
|
13
|
+
import { Button, Flex, FlexItem, Modal, TextControl } from '@wordpress/components';
|
|
14
|
+
|
|
15
|
+
function AddCustomGenericTemplateModal(_ref) {
|
|
16
|
+
let {
|
|
17
|
+
onClose,
|
|
18
|
+
createTemplate
|
|
19
|
+
} = _ref;
|
|
20
|
+
const [title, setTitle] = useState('');
|
|
21
|
+
|
|
22
|
+
const defaultTitle = __('Custom Template');
|
|
23
|
+
|
|
24
|
+
const [isBusy, setIsBusy] = useState(false);
|
|
25
|
+
|
|
26
|
+
async function onCreateTemplate(event) {
|
|
27
|
+
event.preventDefault();
|
|
28
|
+
|
|
29
|
+
if (isBusy) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
setIsBusy(true);
|
|
34
|
+
createTemplate({
|
|
35
|
+
slug: 'wp-custom-template-' + kebabCase(title || defaultTitle),
|
|
36
|
+
title: title || defaultTitle
|
|
37
|
+
}, false);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return createElement(Modal, {
|
|
41
|
+
title: __('Create custom template'),
|
|
42
|
+
closeLabel: __('Close'),
|
|
43
|
+
onRequestClose: () => {
|
|
44
|
+
onClose();
|
|
45
|
+
},
|
|
46
|
+
overlayClassName: "edit-site-custom-generic-template__modal"
|
|
47
|
+
}, createElement("form", {
|
|
48
|
+
onSubmit: onCreateTemplate
|
|
49
|
+
}, createElement(Flex, {
|
|
50
|
+
align: "flex-start",
|
|
51
|
+
gap: 8
|
|
52
|
+
}, createElement(FlexItem, null, createElement(TextControl, {
|
|
53
|
+
label: __('Name'),
|
|
54
|
+
value: title,
|
|
55
|
+
onChange: setTitle,
|
|
56
|
+
placeholder: defaultTitle,
|
|
57
|
+
disabled: isBusy,
|
|
58
|
+
help: __('Describe the template, e.g. "Post with sidebar".')
|
|
59
|
+
}))), createElement(Flex, {
|
|
60
|
+
className: "edit-site-custom-generic-template__modal-actions",
|
|
61
|
+
justify: "flex-end",
|
|
62
|
+
expanded: false
|
|
63
|
+
}, createElement(FlexItem, null, createElement(Button, {
|
|
64
|
+
variant: "tertiary",
|
|
65
|
+
onClick: () => {
|
|
66
|
+
onClose();
|
|
67
|
+
}
|
|
68
|
+
}, __('Cancel'))), createElement(FlexItem, null, createElement(Button, {
|
|
69
|
+
variant: "primary",
|
|
70
|
+
type: "submit",
|
|
71
|
+
isBusy: isBusy,
|
|
72
|
+
"aria-disabled": isBusy
|
|
73
|
+
}, __('Create'))))));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export default AddCustomGenericTemplateModal;
|
|
77
|
+
//# 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":["kebabCase","useState","__","Button","Flex","FlexItem","Modal","TextControl","AddCustomGenericTemplateModal","onClose","createTemplate","title","setTitle","defaultTitle","isBusy","setIsBusy","onCreateTemplate","event","preventDefault","slug"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,SAAT,QAA0B,QAA1B;AAEA;AACA;AACA;;AACA,SAASC,QAAT,QAAyB,oBAAzB;AACA,SAASC,EAAT,QAAmB,iBAAnB;AACA,SACCC,MADD,EAECC,IAFD,EAGCC,QAHD,EAICC,KAJD,EAKCC,WALD,QAMO,uBANP;;AAQA,SAASC,6BAAT,OAAsE;AAAA,MAA9B;AAAEC,IAAAA,OAAF;AAAWC,IAAAA;AAAX,GAA8B;AACrE,QAAM,CAAEC,KAAF,EAASC,QAAT,IAAsBX,QAAQ,CAAE,EAAF,CAApC;;AACA,QAAMY,YAAY,GAAGX,EAAE,CAAE,iBAAF,CAAvB;;AACA,QAAM,CAAEY,MAAF,EAAUC,SAAV,IAAwBd,QAAQ,CAAE,KAAF,CAAtC;;AACA,iBAAee,gBAAf,CAAiCC,KAAjC,EAAyC;AACxCA,IAAAA,KAAK,CAACC,cAAN;;AAEA,QAAKJ,MAAL,EAAc;AACb;AACA;;AAEDC,IAAAA,SAAS,CAAE,IAAF,C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F,CANL,CADD,CAfD,CAhBD,CARD,CADD;AAsDA;;AAED,eAAeM,6BAAf","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"]}
|
|
@@ -15,11 +15,32 @@ import { useEntityRecords } from '@wordpress/core-data';
|
|
|
15
15
|
|
|
16
16
|
import { mapToIHasNameAndId } from './utils';
|
|
17
17
|
const EMPTY_ARRAY = [];
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
|
|
19
|
+
function selectSuggestion(suggestion, onSelect, entityForSuggestions) {
|
|
20
|
+
const {
|
|
21
|
+
labels,
|
|
22
|
+
slug,
|
|
23
|
+
config: {
|
|
24
|
+
templateSlug,
|
|
25
|
+
templatePrefix
|
|
26
|
+
}
|
|
27
|
+
} = entityForSuggestions;
|
|
28
|
+
const title = sprintf( // translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the singular name of a post type or taxonomy and %2$s is the name of the post or term, e.g. "Post: Hello, WordPress", "Category: shoes"
|
|
29
|
+
__('%1$s: %2$s'), labels.singular_name, suggestion.name);
|
|
30
|
+
let newTemplateSlug = `${templateSlug || slug}-${suggestion.slug}`;
|
|
31
|
+
|
|
32
|
+
if (templatePrefix) {
|
|
33
|
+
newTemplateSlug = templatePrefix + newTemplateSlug;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const newTemplate = {
|
|
37
|
+
title,
|
|
38
|
+
description: sprintf( // translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Post: Hello, WordPress"
|
|
39
|
+
__('Template for %1$s'), title),
|
|
40
|
+
slug: newTemplateSlug
|
|
41
|
+
};
|
|
42
|
+
onSelect(newTemplate);
|
|
43
|
+
}
|
|
23
44
|
|
|
24
45
|
function SuggestionListItem(_ref) {
|
|
25
46
|
let {
|
|
@@ -35,16 +56,7 @@ function SuggestionListItem(_ref) {
|
|
|
35
56
|
as: Button
|
|
36
57
|
}, composite, {
|
|
37
58
|
className: baseCssClass,
|
|
38
|
-
onClick: () =>
|
|
39
|
-
const title = sprintf( // translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the singular name of a post type and %2$s is the name of the post, e.g. "Post: Hello, WordPress"
|
|
40
|
-
__('%1$s: %2$s'), entityForSuggestions.labels.singular_name, suggestion.name);
|
|
41
|
-
onSelect({
|
|
42
|
-
title,
|
|
43
|
-
description: sprintf( // translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Post: Hello, WordPress"
|
|
44
|
-
__('Template for %1$s'), title),
|
|
45
|
-
slug: `single-${entityForSuggestions.slug}-${suggestion.slug}`
|
|
46
|
-
});
|
|
47
|
-
}
|
|
59
|
+
onClick: () => selectSuggestion(suggestion, onSelect, entityForSuggestions)
|
|
48
60
|
}), createElement("span", {
|
|
49
61
|
className: `${baseCssClass}__title`
|
|
50
62
|
}, createElement(TextHighlight, {
|
|
@@ -55,62 +67,89 @@ function SuggestionListItem(_ref) {
|
|
|
55
67
|
}, suggestion.link));
|
|
56
68
|
}
|
|
57
69
|
|
|
58
|
-
function
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
70
|
+
function useDebouncedInput() {
|
|
71
|
+
const [input, setInput] = useState('');
|
|
72
|
+
const [debounced, setter] = useState('');
|
|
73
|
+
const setDebounced = useDebounce(setter, 250);
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
if (debounced !== input) {
|
|
76
|
+
setDebounced(input);
|
|
77
|
+
}
|
|
78
|
+
}, [debounced, input]);
|
|
79
|
+
return [input, setInput, debounced];
|
|
80
|
+
}
|
|
68
81
|
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
82
|
+
function useSearchSuggestions(entityForSuggestions, search) {
|
|
83
|
+
const {
|
|
84
|
+
config,
|
|
85
|
+
postsToExclude
|
|
86
|
+
} = entityForSuggestions;
|
|
87
|
+
const query = useMemo(() => ({
|
|
88
|
+
order: 'asc',
|
|
89
|
+
_fields: 'id,name,title,slug,link',
|
|
90
|
+
context: 'view',
|
|
73
91
|
search,
|
|
74
|
-
|
|
75
|
-
|
|
92
|
+
orderBy: config.getOrderBy({
|
|
93
|
+
search
|
|
94
|
+
}),
|
|
95
|
+
exclude: postsToExclude,
|
|
76
96
|
per_page: search ? 20 : 10
|
|
77
|
-
};
|
|
97
|
+
}), [search, config, postsToExclude]);
|
|
78
98
|
const {
|
|
79
99
|
records: searchResults,
|
|
80
100
|
hasResolved: searchHasResolved
|
|
81
101
|
} = useEntityRecords(entityForSuggestions.type, entityForSuggestions.slug, query);
|
|
82
|
-
|
|
83
|
-
if (search !== searchInputValue) {
|
|
84
|
-
debouncedSearch(searchInputValue);
|
|
85
|
-
}
|
|
86
|
-
}, [search, searchInputValue]);
|
|
87
|
-
const entitiesInfo = useMemo(() => {
|
|
88
|
-
if (!(searchResults !== null && searchResults !== void 0 && searchResults.length)) return EMPTY_ARRAY;
|
|
89
|
-
return mapToIHasNameAndId(searchResults, 'title.rendered');
|
|
90
|
-
}, [searchResults]); // Update suggestions only when the query has resolved.
|
|
91
|
-
|
|
102
|
+
const [suggestions, setSuggestions] = useState(EMPTY_ARRAY);
|
|
92
103
|
useEffect(() => {
|
|
93
104
|
if (!searchHasResolved) return;
|
|
94
|
-
|
|
95
|
-
|
|
105
|
+
let newSuggestions = EMPTY_ARRAY;
|
|
106
|
+
|
|
107
|
+
if (searchResults !== null && searchResults !== void 0 && searchResults.length) {
|
|
108
|
+
newSuggestions = searchResults;
|
|
109
|
+
|
|
110
|
+
if (config.recordNamePath) {
|
|
111
|
+
newSuggestions = mapToIHasNameAndId(newSuggestions, config.recordNamePath);
|
|
112
|
+
}
|
|
113
|
+
} // Update suggestions only when the query has resolved, so as to keep
|
|
114
|
+
// the previous results in the UI.
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
setSuggestions(newSuggestions);
|
|
118
|
+
}, [searchResults, searchHasResolved]);
|
|
119
|
+
return suggestions;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function SuggestionList(_ref2) {
|
|
123
|
+
let {
|
|
124
|
+
entityForSuggestions,
|
|
125
|
+
onSelect
|
|
126
|
+
} = _ref2;
|
|
127
|
+
const composite = useCompositeState({
|
|
128
|
+
orientation: 'vertical'
|
|
129
|
+
});
|
|
130
|
+
const [search, setSearch, debouncedSearch] = useDebouncedInput();
|
|
131
|
+
const suggestions = useSearchSuggestions(entityForSuggestions, debouncedSearch);
|
|
132
|
+
const {
|
|
133
|
+
labels
|
|
134
|
+
} = entityForSuggestions;
|
|
96
135
|
return createElement(Fragment, null, createElement(SearchControl, {
|
|
97
|
-
onChange:
|
|
98
|
-
value:
|
|
99
|
-
label:
|
|
100
|
-
placeholder:
|
|
136
|
+
onChange: setSearch,
|
|
137
|
+
value: search,
|
|
138
|
+
label: labels.search_items,
|
|
139
|
+
placeholder: labels.search_items
|
|
101
140
|
}), !!(suggestions !== null && suggestions !== void 0 && suggestions.length) && createElement(Composite, _extends({}, composite, {
|
|
102
141
|
role: "listbox",
|
|
103
142
|
className: "edit-site-custom-template-modal__suggestions_list"
|
|
104
143
|
}), suggestions.map(suggestion => createElement(SuggestionListItem, {
|
|
105
144
|
key: suggestion.slug,
|
|
106
145
|
suggestion: suggestion,
|
|
107
|
-
search:
|
|
146
|
+
search: debouncedSearch,
|
|
108
147
|
onSelect: onSelect,
|
|
109
148
|
entityForSuggestions: entityForSuggestions,
|
|
110
149
|
composite: composite
|
|
111
|
-
}))),
|
|
150
|
+
}))), debouncedSearch && !(suggestions !== null && suggestions !== void 0 && suggestions.length) && createElement("p", {
|
|
112
151
|
className: "edit-site-custom-template-modal__no-results"
|
|
113
|
-
},
|
|
152
|
+
}, labels.not_found));
|
|
114
153
|
}
|
|
115
154
|
|
|
116
155
|
function AddCustomTemplateModal(_ref3) {
|
|
@@ -149,7 +188,7 @@ function AddCustomTemplateModal(_ref3) {
|
|
|
149
188
|
level: 5
|
|
150
189
|
}, entityForSuggestions.labels.all_items), createElement(Text, {
|
|
151
190
|
as: "span"
|
|
152
|
-
}, // translators: The user is given the choice to set up a template for all items of a post type, or just a specific one.
|
|
191
|
+
}, // translators: The user is given the choice to set up a template for all items of a post type or taxonomy, or just a specific one.
|
|
153
192
|
__('For all items'))), createElement(FlexItem, {
|
|
154
193
|
isBlock: true,
|
|
155
194
|
onClick: () => {
|
|
@@ -159,7 +198,7 @@ function AddCustomTemplateModal(_ref3) {
|
|
|
159
198
|
level: 5
|
|
160
199
|
}, entityForSuggestions.labels.singular_name), createElement(Text, {
|
|
161
200
|
as: "span"
|
|
162
|
-
}, // translators: The user is given the choice to set up a template for all items of a post type, or just a specific one.
|
|
201
|
+
}, // translators: The user is given the choice to set up a template for all items of a post type or taxonomy, or just a specific one.
|
|
163
202
|
__('For a specific item'))))), showSearchEntities && createElement(Fragment, null, createElement("p", null, __('This template will be used only for the specific item chosen.')), createElement(SuggestionList, {
|
|
164
203
|
entityForSuggestions: entityForSuggestions,
|
|
165
204
|
onSelect: onSelect
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/edit-site/src/components/add-new-template/add-custom-template-modal.js"],"names":["useState","useMemo","useEffect","__","sprintf","Button","Flex","FlexItem","Modal","SearchControl","TextHighlight","__experimentalText","Text","__experimentalHeading","Heading","__unstableComposite","Composite","__unstableUseCompositeState","useCompositeState","__unstableCompositeItem","CompositeItem","useDebounce","useEntityRecords","mapToIHasNameAndId","EMPTY_ARRAY","BASE_QUERY","order","_fields","context","SuggestionListItem","suggestion","search","onSelect","entityForSuggestions","composite","baseCssClass","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":";;;AAAA;AACA;AACA;AACA,SAASA,QAAT,EAAmBC,OAAnB,EAA4BC,SAA5B,QAA6C,oBAA7C;AACA,SAASC,EAAT,EAAaC,OAAb,QAA4B,iBAA5B;AACA,SACCC,MADD,EAECC,IAFD,EAGCC,QAHD,EAICC,KAJD,EAKCC,aALD,EAMCC,aAND,EAOCC,kBAAkB,IAAIC,IAPvB,EAQCC,qBAAqB,IAAIC,OAR1B,EASCC,mBAAmB,IAAIC,SATxB,EAUCC,2BAA2B,IAAIC,iBAVhC,EAWCC,uBAAuB,IAAIC,aAX5B,QAYO,uBAZP;AAaA,SAASC,WAAT,QAA4B,oBAA5B;AACA,SAASC,gBAAT,QAAiC,sBAAjC;AAEA;AACA;AACA;;AACA,SAASC,kBAAT,QAAmC,SAAnC;AAEA,MAAMC,WAAW,GAAG,EAApB;AACA,MAAMC,UAAU,GAAG;AAClBC,EAAAA,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AAA,EAAE,EAAC;AAAT,KAEE;AACAnC,EAAAA,EAAE,CAAE,qBAAF,CAHJ,CATD,CAvBD,CAND,CAXF,EA2DGiE,kBAAkB,IACnB,8BACC,yBACGjE,EAAE,CACH,+DADG,CADL,CADD,EAMC,cAAC,cAAD;AACC,IAAA,oBAAoB,EAAG8B,oBADxB;AAEC,IAAA,QAAQ,EAAGD;AAFZ,IAND,CA5DF,CADD;AA2EA;;AAED,eAAekC,sBAAf","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useState, useMemo, useEffect } from '@wordpress/element';\nimport { __, sprintf } from '@wordpress/i18n';\nimport {\n\tButton,\n\tFlex,\n\tFlexItem,\n\tModal,\n\tSearchControl,\n\tTextHighlight,\n\t__experimentalText as Text,\n\t__experimentalHeading as Heading,\n\t__unstableComposite as Composite,\n\t__unstableUseCompositeState as useCompositeState,\n\t__unstableCompositeItem as CompositeItem,\n} from '@wordpress/components';\nimport { useDebounce } from '@wordpress/compose';\nimport { useEntityRecords } from '@wordpress/core-data';\n\n/**\n * Internal dependencies\n */\nimport { mapToIHasNameAndId } from './utils';\n\nconst EMPTY_ARRAY = [];\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":["useState","useMemo","useEffect","__","sprintf","Button","Flex","FlexItem","Modal","SearchControl","TextHighlight","__experimentalText","Text","__experimentalHeading","Heading","__unstableComposite","Composite","__unstableUseCompositeState","useCompositeState","__unstableCompositeItem","CompositeItem","useDebounce","useEntityRecords","mapToIHasNameAndId","EMPTY_ARRAY","selectSuggestion","suggestion","onSelect","entityForSuggestions","labels","slug","config","templateSlug","templatePrefix","title","singular_name","name","newTemplateSlug","newTemplate","description","SuggestionListItem","search","composite","baseCssClass","link","useDebouncedInput","input","setInput","debounced","setter","setDebounced","useSearchSuggestions","postsToExclude","query","order","_fields","context","orderBy","getOrderBy","exclude","per_page","records","searchResults","hasResolved","searchHasResolved","type","suggestions","setSuggestions","newSuggestions","length","recordNamePath","SuggestionList","orientation","setSearch","debouncedSearch","search_items","map","not_found","AddCustomTemplateModal","onClose","showSearchEntities","setShowSearchEntities","hasGeneralTemplate","template","all_items"],"mappings":";;;AAAA;AACA;AACA;AACA,SAASA,QAAT,EAAmBC,OAAnB,EAA4BC,SAA5B,QAA6C,oBAA7C;AACA,SAASC,EAAT,EAAaC,OAAb,QAA4B,iBAA5B;AACA,SACCC,MADD,EAECC,IAFD,EAGCC,QAHD,EAICC,KAJD,EAKCC,aALD,EAMCC,aAND,EAOCC,kBAAkB,IAAIC,IAPvB,EAQCC,qBAAqB,IAAIC,OAR1B,EASCC,mBAAmB,IAAIC,SATxB,EAUCC,2BAA2B,IAAIC,iBAVhC,EAWCC,uBAAuB,IAAIC,aAX5B,QAYO,uBAZP;AAaA,SAASC,WAAT,QAA4B,oBAA5B;AACA,SAASC,gBAAT,QAAiC,sBAAjC;AAEA;AACA;AACA;;AACA,SAASC,kBAAT,QAAmC,SAAnC;AAEA,MAAMC,WAAW,GAAG,EAApB;;AAEA,SAASC,gBAAT,CAA2BC,UAA3B,EAAuCC,QAAvC,EAAiDC,oBAAjD,EAAwE;AACvE,QAAM;AACLC,IAAAA,MADK;AAELC,IAAAA,IAFK;AAGLC,IAAAA,MAAM,EAAE;AAAEC,MAAAA,YAAF;AAAgBC,MAAAA;AAAhB;AAHH,MAIFL,oBAJJ;AAKA,QAAMM,KAAK,GAAG9B,OAAO,EACpB;AACAD,EAAAA,EAAE,CAAE,YAAF,CAFkB,EAGpB0B,MAAM,CAACM,aAHa,EAIpBT,UAAU,CAACU,IAJS,CAArB;AAMA,MAAIC,eAAe,GAAI,GAAGL,YAAY,IAAIF,IAAM,IAAIJ,UAAU,CAACI,IAAM,EAArE;;AACA,MAAKG,cAAL,EAAsB;AACrBI,IAAAA,eAAe,GAAGJ,cAAc,GAAGI,eAAnC;AACA;;AACD,QAAMC,WAAW,GAAG;AACnBJ,IAAAA,KADmB;AAEnBK,IAAAA,WAAW,EAAEnC,OAAO,EACnB;AACAD,IAAAA,EAAE,CAAE,mBAAF,CAFiB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,CAXD,CALD,EAuBC,cAAC,QAAD;AACC,IAAA,OAAO,MADR;AAEC,IAAA,OAAO,EAAG,MAAM;AACf8E,MAAAA,qBAAqB,CAAE,IAAF,CAArB;AACA;AAJF,KAMC,cAAC,OAAD;AAAS,IAAA,KAAK,EAAG;AAAjB,KACGrD,oBAAoB,CAACC,MAArB,CAA4BM,aAD/B,CAND,EASC,cAAC,IAAD;AAAM,IAAA,EAAE,EAAC;AAAT,KAEE;AACAhC,EAAAA,EAAE,CAAE,qBAAF,CAHJ,CATD,CAvBD,CAND,CAXF,EA2DG6E,kBAAkB,IACnB,8BACC,yBACG7E,EAAE,CACH,+DADG,CADL,CADD,EAMC,cAAC,cAAD;AACC,IAAA,oBAAoB,EAAGyB,oBADxB;AAEC,IAAA,QAAQ,EAAGD;AAFZ,IAND,CA5DF,CADD;AA2EA;;AAED,eAAemD,sBAAf","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useState, useMemo, useEffect } from '@wordpress/element';\nimport { __, sprintf } from '@wordpress/i18n';\nimport {\n\tButton,\n\tFlex,\n\tFlexItem,\n\tModal,\n\tSearchControl,\n\tTextHighlight,\n\t__experimentalText as Text,\n\t__experimentalHeading as Heading,\n\t__unstableComposite as Composite,\n\t__unstableUseCompositeState as useCompositeState,\n\t__unstableCompositeItem as CompositeItem,\n} from '@wordpress/components';\nimport { useDebounce } from '@wordpress/compose';\nimport { useEntityRecords } from '@wordpress/core-data';\n\n/**\n * Internal dependencies\n */\nimport { mapToIHasNameAndId } from './utils';\n\nconst EMPTY_ARRAY = [];\n\nfunction selectSuggestion( suggestion, onSelect, entityForSuggestions ) {\n\tconst {\n\t\tlabels,\n\t\tslug,\n\t\tconfig: { templateSlug, templatePrefix },\n\t} = entityForSuggestions;\n\tconst title = sprintf(\n\t\t// translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the singular name of a post type or taxonomy and %2$s is the name of the post or term, e.g. \"Post: Hello, WordPress\", \"Category: shoes\"\n\t\t__( '%1$s: %2$s' ),\n\t\tlabels.singular_name,\n\t\tsuggestion.name\n\t);\n\tlet newTemplateSlug = `${ templateSlug || slug }-${ suggestion.slug }`;\n\tif ( templatePrefix ) {\n\t\tnewTemplateSlug = templatePrefix + newTemplateSlug;\n\t}\n\tconst newTemplate = {\n\t\ttitle,\n\t\tdescription: sprintf(\n\t\t\t// translators: Represents the description of a user's custom template in the Site Editor, e.g. \"Template for Post: Hello, WordPress\"\n\t\t\t__( 'Template for %1$s' ),\n\t\t\ttitle\n\t\t),\n\t\tslug: newTemplateSlug,\n\t};\n\tonSelect( newTemplate );\n}\n\nfunction SuggestionListItem( {\n\tsuggestion,\n\tsearch,\n\tonSelect,\n\tentityForSuggestions,\n\tcomposite,\n} ) {\n\tconst baseCssClass =\n\t\t'edit-site-custom-template-modal__suggestions_list__list-item';\n\treturn (\n\t\t<CompositeItem\n\t\t\trole=\"option\"\n\t\t\tas={ Button }\n\t\t\t{ ...composite }\n\t\t\tclassName={ baseCssClass }\n\t\t\tonClick={ () =>\n\t\t\t\tselectSuggestion( suggestion, onSelect, entityForSuggestions )\n\t\t\t}\n\t\t>\n\t\t\t<span className={ `${ baseCssClass }__title` }>\n\t\t\t\t<TextHighlight text={ suggestion.name } highlight={ search } />\n\t\t\t</span>\n\t\t\t{ suggestion.link && (\n\t\t\t\t<span className={ `${ baseCssClass }__info` }>\n\t\t\t\t\t{ suggestion.link }\n\t\t\t\t</span>\n\t\t\t) }\n\t\t</CompositeItem>\n\t);\n}\n\nfunction useDebouncedInput() {\n\tconst [ input, setInput ] = useState( '' );\n\tconst [ debounced, setter ] = useState( '' );\n\tconst setDebounced = useDebounce( setter, 250 );\n\tuseEffect( () => {\n\t\tif ( debounced !== input ) {\n\t\t\tsetDebounced( input );\n\t\t}\n\t}, [ debounced, input ] );\n\treturn [ input, setInput, debounced ];\n}\n\nfunction useSearchSuggestions( entityForSuggestions, search ) {\n\tconst { config, postsToExclude } = entityForSuggestions;\n\tconst query = useMemo(\n\t\t() => ( {\n\t\t\torder: 'asc',\n\t\t\t_fields: 'id,name,title,slug,link',\n\t\t\tcontext: 'view',\n\t\t\tsearch,\n\t\t\torderBy: config.getOrderBy( { search } ),\n\t\t\texclude: postsToExclude,\n\t\t\tper_page: search ? 20 : 10,\n\t\t} ),\n\t\t[ search, config, postsToExclude ]\n\t);\n\tconst { records: searchResults, hasResolved: searchHasResolved } =\n\t\tuseEntityRecords(\n\t\t\tentityForSuggestions.type,\n\t\t\tentityForSuggestions.slug,\n\t\t\tquery\n\t\t);\n\tconst [ suggestions, setSuggestions ] = useState( EMPTY_ARRAY );\n\tuseEffect( () => {\n\t\tif ( ! searchHasResolved ) return;\n\t\tlet newSuggestions = EMPTY_ARRAY;\n\t\tif ( searchResults?.length ) {\n\t\t\tnewSuggestions = searchResults;\n\t\t\tif ( config.recordNamePath ) {\n\t\t\t\tnewSuggestions = mapToIHasNameAndId(\n\t\t\t\t\tnewSuggestions,\n\t\t\t\t\tconfig.recordNamePath\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\t// Update suggestions only when the query has resolved, so as to keep\n\t\t// the previous results in the UI.\n\t\tsetSuggestions( newSuggestions );\n\t}, [ searchResults, searchHasResolved ] );\n\treturn suggestions;\n}\n\nfunction SuggestionList( { entityForSuggestions, onSelect } ) {\n\tconst composite = useCompositeState( { orientation: 'vertical' } );\n\tconst [ search, setSearch, debouncedSearch ] = useDebouncedInput();\n\tconst suggestions = useSearchSuggestions(\n\t\tentityForSuggestions,\n\t\tdebouncedSearch\n\t);\n\tconst { labels } = entityForSuggestions;\n\treturn (\n\t\t<>\n\t\t\t<SearchControl\n\t\t\t\tonChange={ setSearch }\n\t\t\t\tvalue={ search }\n\t\t\t\tlabel={ labels.search_items }\n\t\t\t\tplaceholder={ labels.search_items }\n\t\t\t/>\n\t\t\t{ !! suggestions?.length && (\n\t\t\t\t<Composite\n\t\t\t\t\t{ ...composite }\n\t\t\t\t\trole=\"listbox\"\n\t\t\t\t\tclassName=\"edit-site-custom-template-modal__suggestions_list\"\n\t\t\t\t>\n\t\t\t\t\t{ suggestions.map( ( suggestion ) => (\n\t\t\t\t\t\t<SuggestionListItem\n\t\t\t\t\t\t\tkey={ suggestion.slug }\n\t\t\t\t\t\t\tsuggestion={ suggestion }\n\t\t\t\t\t\t\tsearch={ debouncedSearch }\n\t\t\t\t\t\t\tonSelect={ onSelect }\n\t\t\t\t\t\t\tentityForSuggestions={ entityForSuggestions }\n\t\t\t\t\t\t\tcomposite={ composite }\n\t\t\t\t\t\t/>\n\t\t\t\t\t) ) }\n\t\t\t\t</Composite>\n\t\t\t) }\n\t\t\t{ debouncedSearch && ! suggestions?.length && (\n\t\t\t\t<p className=\"edit-site-custom-template-modal__no-results\">\n\t\t\t\t\t{ labels.not_found }\n\t\t\t\t</p>\n\t\t\t) }\n\t\t</>\n\t);\n}\n\nfunction AddCustomTemplateModal( { onClose, onSelect, entityForSuggestions } ) {\n\tconst [ showSearchEntities, setShowSearchEntities ] = useState(\n\t\tentityForSuggestions.hasGeneralTemplate\n\t);\n\tconst baseCssClass = 'edit-site-custom-template-modal';\n\treturn (\n\t\t<Modal\n\t\t\ttitle={ sprintf(\n\t\t\t\t// translators: %s: Name of the post type e.g: \"Post\".\n\t\t\t\t__( 'Add template: %s' ),\n\t\t\t\tentityForSuggestions.labels.singular_name\n\t\t\t) }\n\t\t\tclassName={ baseCssClass }\n\t\t\tcloseLabel={ __( 'Close' ) }\n\t\t\tonRequestClose={ onClose }\n\t\t>\n\t\t\t{ ! showSearchEntities && (\n\t\t\t\t<>\n\t\t\t\t\t<p>\n\t\t\t\t\t\t{ __(\n\t\t\t\t\t\t\t'Select whether to create a single template for all items or a specific one.'\n\t\t\t\t\t\t) }\n\t\t\t\t\t</p>\n\t\t\t\t\t<Flex\n\t\t\t\t\t\tclassName={ `${ baseCssClass }__contents` }\n\t\t\t\t\t\tgap=\"4\"\n\t\t\t\t\t\talign=\"initial\"\n\t\t\t\t\t>\n\t\t\t\t\t\t<FlexItem\n\t\t\t\t\t\t\tisBlock\n\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\tconst { slug, title, description } =\n\t\t\t\t\t\t\t\t\tentityForSuggestions.template;\n\t\t\t\t\t\t\t\tonSelect( { slug, title, description } );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t<Heading level={ 5 }>\n\t\t\t\t\t\t\t\t{ entityForSuggestions.labels.all_items }\n\t\t\t\t\t\t\t</Heading>\n\t\t\t\t\t\t\t<Text as=\"span\">\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t// translators: The user is given the choice to set up a template for all items of a post type or taxonomy, or just a specific one.\n\t\t\t\t\t\t\t\t\t__( 'For all items' )\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t</Text>\n\t\t\t\t\t\t</FlexItem>\n\t\t\t\t\t\t<FlexItem\n\t\t\t\t\t\t\tisBlock\n\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\tsetShowSearchEntities( true );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t<Heading level={ 5 }>\n\t\t\t\t\t\t\t\t{ entityForSuggestions.labels.singular_name }\n\t\t\t\t\t\t\t</Heading>\n\t\t\t\t\t\t\t<Text as=\"span\">\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t// translators: The user is given the choice to set up a template for all items of a post type or taxonomy, or just a specific one.\n\t\t\t\t\t\t\t\t\t__( 'For a specific item' )\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t</Text>\n\t\t\t\t\t\t</FlexItem>\n\t\t\t\t\t</Flex>\n\t\t\t\t</>\n\t\t\t) }\n\t\t\t{ showSearchEntities && (\n\t\t\t\t<>\n\t\t\t\t\t<p>\n\t\t\t\t\t\t{ __(\n\t\t\t\t\t\t\t'This template will be used only for the specific item chosen.'\n\t\t\t\t\t\t) }\n\t\t\t\t\t</p>\n\t\t\t\t\t<SuggestionList\n\t\t\t\t\t\tentityForSuggestions={ entityForSuggestions }\n\t\t\t\t\t\tonSelect={ onSelect }\n\t\t\t\t\t/>\n\t\t\t\t</>\n\t\t\t) }\n\t\t</Modal>\n\t);\n}\n\nexport default AddCustomTemplateModal;\n"]}
|
|
@@ -1,27 +1,22 @@
|
|
|
1
1
|
import { createElement, Fragment } from "@wordpress/element";
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* External dependencies
|
|
5
|
-
*/
|
|
6
|
-
import { filter, includes } from 'lodash';
|
|
7
3
|
/**
|
|
8
4
|
* WordPress dependencies
|
|
9
5
|
*/
|
|
10
|
-
|
|
11
6
|
import { DropdownMenu, MenuGroup, MenuItem, NavigableMenu } from '@wordpress/components';
|
|
12
7
|
import { useState } from '@wordpress/element';
|
|
13
|
-
import {
|
|
8
|
+
import { useDispatch } from '@wordpress/data';
|
|
14
9
|
import { store as coreStore } from '@wordpress/core-data';
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import { __, sprintf } from '@wordpress/i18n';
|
|
10
|
+
import { archive, blockMeta, category, home, list, media, notFound, page, post, postAuthor, postDate, search, tag, layout as customGenericTemplateIcon } from '@wordpress/icons';
|
|
11
|
+
import { __ } from '@wordpress/i18n';
|
|
18
12
|
import { store as noticesStore } from '@wordpress/notices';
|
|
19
13
|
/**
|
|
20
14
|
* Internal dependencies
|
|
21
15
|
*/
|
|
22
16
|
|
|
23
17
|
import AddCustomTemplateModal from './add-custom-template-modal';
|
|
24
|
-
import { usePostTypes,
|
|
18
|
+
import { useExistingTemplates, useDefaultTemplateTypes, entitiesConfig, usePostTypes, usePostTypePage, useTaxonomies, useTaxonomyCategory, useTaxonomyTag, useExtraTemplates } from './utils';
|
|
19
|
+
import AddCustomGenericTemplateModal from './add-custom-generic-template-modal';
|
|
25
20
|
import { useHistory } from '../routes';
|
|
26
21
|
import { store as editSiteStore } from '../../store';
|
|
27
22
|
const DEFAULT_TEMPLATE_SLUGS = ['front-page', 'single', 'page', 'index', 'archive', 'author', 'category', 'date', 'tag', 'taxonomy', 'search', '404'];
|
|
@@ -44,20 +39,10 @@ export default function NewTemplate(_ref) {
|
|
|
44
39
|
let {
|
|
45
40
|
postType
|
|
46
41
|
} = _ref;
|
|
47
|
-
const history = useHistory();
|
|
48
|
-
const postTypes = usePostTypes();
|
|
49
42
|
const [showCustomTemplateModal, setShowCustomTemplateModal] = useState(false);
|
|
43
|
+
const [showCustomGenericTemplateModal, setShowCustomGenericTemplateModal] = useState(false);
|
|
50
44
|
const [entityForSuggestions, setEntityForSuggestions] = useState({});
|
|
51
|
-
const
|
|
52
|
-
existingTemplates,
|
|
53
|
-
defaultTemplateTypes
|
|
54
|
-
} = useSelect(select => ({
|
|
55
|
-
existingTemplates: select(coreStore).getEntityRecords('postType', 'wp_template', {
|
|
56
|
-
per_page: -1
|
|
57
|
-
}),
|
|
58
|
-
defaultTemplateTypes: select(editorStore).__experimentalGetDefaultTemplateTypes()
|
|
59
|
-
}), []);
|
|
60
|
-
const postTypesEntitiesInfo = usePostTypesEntitiesInfo(existingTemplates);
|
|
45
|
+
const history = useHistory();
|
|
61
46
|
const {
|
|
62
47
|
saveEntityRecord
|
|
63
48
|
} = useDispatch(coreStore);
|
|
@@ -69,6 +54,8 @@ export default function NewTemplate(_ref) {
|
|
|
69
54
|
} = useDispatch(editSiteStore);
|
|
70
55
|
|
|
71
56
|
async function createTemplate(template) {
|
|
57
|
+
let isWPSuggestion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
58
|
+
|
|
72
59
|
try {
|
|
73
60
|
const {
|
|
74
61
|
title,
|
|
@@ -82,7 +69,7 @@ export default function NewTemplate(_ref) {
|
|
|
82
69
|
status: 'publish',
|
|
83
70
|
title,
|
|
84
71
|
// This adds a post meta field in template that is part of `is_custom` value calculation.
|
|
85
|
-
is_wp_suggestion:
|
|
72
|
+
is_wp_suggestion: isWPSuggestion
|
|
86
73
|
}, {
|
|
87
74
|
throwOnError: true
|
|
88
75
|
}); // Set template before navigating away to avoid initial stale value.
|
|
@@ -101,68 +88,12 @@ export default function NewTemplate(_ref) {
|
|
|
101
88
|
}
|
|
102
89
|
}
|
|
103
90
|
|
|
104
|
-
const
|
|
105
|
-
let {
|
|
106
|
-
slug
|
|
107
|
-
} = _ref2;
|
|
108
|
-
return slug;
|
|
109
|
-
});
|
|
110
|
-
const missingTemplates = filter(defaultTemplateTypes, template => includes(DEFAULT_TEMPLATE_SLUGS, template.slug) && !includes(existingTemplateSlugs, template.slug));
|
|
111
|
-
const extraTemplates = (postTypes || []).reduce((accumulator, _postType) => {
|
|
112
|
-
var _postTypesEntitiesInf;
|
|
113
|
-
|
|
114
|
-
const {
|
|
115
|
-
slug,
|
|
116
|
-
labels,
|
|
117
|
-
icon
|
|
118
|
-
} = _postType;
|
|
119
|
-
const hasGeneralTemplate = existingTemplateSlugs === null || existingTemplateSlugs === void 0 ? void 0 : existingTemplateSlugs.includes(`single-${slug}`);
|
|
120
|
-
const hasEntities = postTypesEntitiesInfo === null || postTypesEntitiesInfo === void 0 ? void 0 : (_postTypesEntitiesInf = postTypesEntitiesInfo[slug]) === null || _postTypesEntitiesInf === void 0 ? void 0 : _postTypesEntitiesInf.hasEntities;
|
|
121
|
-
const menuItem = {
|
|
122
|
-
slug: `single-${slug}`,
|
|
123
|
-
title: sprintf( // translators: %s: Name of the post type e.g: "Post".
|
|
124
|
-
__('Single item: %s'), labels.singular_name),
|
|
125
|
-
description: sprintf( // translators: %s: Name of the post type e.g: "Post".
|
|
126
|
-
__('Displays a single item: %s.'), labels.singular_name),
|
|
127
|
-
// `icon` is the `menu_icon` property of a post type. We
|
|
128
|
-
// only handle `dashicons` for now, even if the `menu_icon`
|
|
129
|
-
// also supports urls and svg as values.
|
|
130
|
-
icon: icon !== null && icon !== void 0 && icon.startsWith('dashicons-') ? icon.slice(10) : null
|
|
131
|
-
}; // We have a different template creation flow only if they have entities.
|
|
132
|
-
|
|
133
|
-
if (hasEntities) {
|
|
134
|
-
menuItem.onClick = template => {
|
|
135
|
-
setShowCustomTemplateModal(true);
|
|
136
|
-
setEntityForSuggestions({
|
|
137
|
-
type: 'postType',
|
|
138
|
-
slug,
|
|
139
|
-
labels,
|
|
140
|
-
hasGeneralTemplate,
|
|
141
|
-
template,
|
|
142
|
-
postsToExclude: postTypesEntitiesInfo[slug].existingPosts
|
|
143
|
-
});
|
|
144
|
-
};
|
|
145
|
-
} // We don't need to add the menu item if there are no
|
|
146
|
-
// entities and the general template exists.
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
if (!hasGeneralTemplate || hasEntities) {
|
|
150
|
-
accumulator.push(menuItem);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return accumulator;
|
|
154
|
-
}, []);
|
|
91
|
+
const missingTemplates = useMissingTemplates(setEntityForSuggestions, setShowCustomTemplateModal);
|
|
155
92
|
|
|
156
|
-
if (!missingTemplates.length
|
|
93
|
+
if (!missingTemplates.length) {
|
|
157
94
|
return null;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
missingTemplates === null || missingTemplates === void 0 ? void 0 : missingTemplates.sort((template1, template2) => {
|
|
162
|
-
return DEFAULT_TEMPLATE_SLUGS.indexOf(template1.slug) - DEFAULT_TEMPLATE_SLUGS.indexOf(template2.slug);
|
|
163
|
-
}); // Append all extra templates at the end of the list for now.
|
|
95
|
+
}
|
|
164
96
|
|
|
165
|
-
missingTemplates.push(...extraTemplates);
|
|
166
97
|
return createElement(Fragment, null, createElement(DropdownMenu, {
|
|
167
98
|
className: "edit-site-new-template-dropdown",
|
|
168
99
|
icon: null,
|
|
@@ -193,10 +124,76 @@ export default function NewTemplate(_ref) {
|
|
|
193
124
|
key: slug,
|
|
194
125
|
onClick: () => onClick ? onClick(template) : createTemplate(template)
|
|
195
126
|
}, title);
|
|
196
|
-
}))
|
|
127
|
+
})), createElement(MenuGroup, null, createElement(MenuItem, {
|
|
128
|
+
icon: customGenericTemplateIcon,
|
|
129
|
+
iconPosition: "left",
|
|
130
|
+
info: __('Custom templates can be applied to any post or page.'),
|
|
131
|
+
key: "custom-template",
|
|
132
|
+
onClick: () => setShowCustomGenericTemplateModal(true)
|
|
133
|
+
}, __('Custom template'))))), showCustomTemplateModal && createElement(AddCustomTemplateModal, {
|
|
197
134
|
onClose: () => setShowCustomTemplateModal(false),
|
|
198
135
|
onSelect: createTemplate,
|
|
199
136
|
entityForSuggestions: entityForSuggestions
|
|
137
|
+
}), showCustomGenericTemplateModal && createElement(AddCustomGenericTemplateModal, {
|
|
138
|
+
onClose: () => setShowCustomGenericTemplateModal(false),
|
|
139
|
+
createTemplate: createTemplate
|
|
200
140
|
}));
|
|
201
141
|
}
|
|
142
|
+
|
|
143
|
+
function useMissingTemplates(setEntityForSuggestions, setShowCustomTemplateModal) {
|
|
144
|
+
const postTypes = usePostTypes();
|
|
145
|
+
const pagePostType = usePostTypePage();
|
|
146
|
+
const taxonomies = useTaxonomies();
|
|
147
|
+
const categoryTaxonomy = useTaxonomyCategory();
|
|
148
|
+
const tagTaxonomy = useTaxonomyTag();
|
|
149
|
+
const existingTemplates = useExistingTemplates();
|
|
150
|
+
const defaultTemplateTypes = useDefaultTemplateTypes();
|
|
151
|
+
const existingTemplateSlugs = (existingTemplates || []).map(_ref2 => {
|
|
152
|
+
let {
|
|
153
|
+
slug
|
|
154
|
+
} = _ref2;
|
|
155
|
+
return slug;
|
|
156
|
+
});
|
|
157
|
+
const missingDefaultTemplates = (defaultTemplateTypes || []).filter(template => DEFAULT_TEMPLATE_SLUGS.includes(template.slug) && !existingTemplateSlugs.includes(template.slug));
|
|
158
|
+
|
|
159
|
+
const onClickMenuItem = _entityForSuggestions => {
|
|
160
|
+
setShowCustomTemplateModal(true);
|
|
161
|
+
setEntityForSuggestions(_entityForSuggestions);
|
|
162
|
+
}; // TODO: find better names for these variables. `useExtraTemplates` returns an array of items.
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
const categoryMenuItem = useExtraTemplates(categoryTaxonomy, entitiesConfig.category, onClickMenuItem);
|
|
166
|
+
const tagMenuItem = useExtraTemplates(tagTaxonomy, entitiesConfig.tag, onClickMenuItem);
|
|
167
|
+
const pageMenuItem = useExtraTemplates(pagePostType, entitiesConfig.page, onClickMenuItem); // We need to replace existing default template types with
|
|
168
|
+
// the create specific template functionality. The original
|
|
169
|
+
// info (title, description, etc.) is preserved in the
|
|
170
|
+
// `useExtraTemplates` hook.
|
|
171
|
+
|
|
172
|
+
const enhancedMissingDefaultTemplateTypes = [...missingDefaultTemplates];
|
|
173
|
+
[categoryMenuItem, tagMenuItem, pageMenuItem].forEach(menuItem => {
|
|
174
|
+
if (!(menuItem !== null && menuItem !== void 0 && menuItem.length)) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const matchIndex = enhancedMissingDefaultTemplateTypes.findIndex(template => template.slug === menuItem[0].slug); // Some default template types might have been filtered above from
|
|
179
|
+
// `missingDefaultTemplates` because they only check for the general
|
|
180
|
+
// template. So here we either replace or append the item, augmented
|
|
181
|
+
// with the check if it has available specific item to create a
|
|
182
|
+
// template for.
|
|
183
|
+
|
|
184
|
+
if (matchIndex > -1) {
|
|
185
|
+
enhancedMissingDefaultTemplateTypes.splice(matchIndex, 1, menuItem[0]);
|
|
186
|
+
} else {
|
|
187
|
+
enhancedMissingDefaultTemplateTypes.push(menuItem[0]);
|
|
188
|
+
}
|
|
189
|
+
}); // Update the sort order to match the DEFAULT_TEMPLATE_SLUGS order.
|
|
190
|
+
|
|
191
|
+
enhancedMissingDefaultTemplateTypes === null || enhancedMissingDefaultTemplateTypes === void 0 ? void 0 : enhancedMissingDefaultTemplateTypes.sort((template1, template2) => {
|
|
192
|
+
return DEFAULT_TEMPLATE_SLUGS.indexOf(template1.slug) - DEFAULT_TEMPLATE_SLUGS.indexOf(template2.slug);
|
|
193
|
+
});
|
|
194
|
+
const extraPostTypeTemplates = useExtraTemplates(postTypes, entitiesConfig.postType, onClickMenuItem);
|
|
195
|
+
const extraTaxonomyTemplates = useExtraTemplates(taxonomies, entitiesConfig.taxonomy, onClickMenuItem);
|
|
196
|
+
const missingTemplates = [...enhancedMissingDefaultTemplateTypes, ...extraPostTypeTemplates, ...extraTaxonomyTemplates];
|
|
197
|
+
return missingTemplates;
|
|
198
|
+
}
|
|
202
199
|
//# sourceMappingURL=new-template.js.map
|