-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
form.js
141 lines (133 loc) · 3.84 KB
/
form.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* WordPress dependencies
*/
import { useState, useMemo } from '@wordpress/element';
import { __experimentalInspectorPopoverHeader as InspectorPopoverHeader } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { addTemplate } from '@wordpress/icons';
import { Notice, SelectControl, Button } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import { store as editPostStore } from '../../../store';
import PostTemplateCreateModal from './create-modal';
export default function PostTemplateForm( { onClose } ) {
const {
isPostsPage,
availableTemplates,
fetchedTemplates,
selectedTemplateSlug,
canCreate,
canEdit,
} = useSelect( ( select ) => {
const { canUser, getEntityRecord, getEntityRecords } =
select( coreStore );
const editorSettings = select( editorStore ).getEditorSettings();
const siteSettings = canUser( 'read', 'settings' )
? getEntityRecord( 'root', 'site' )
: undefined;
const _isPostsPage =
select( editorStore ).getCurrentPostId() ===
siteSettings?.page_for_posts;
const canCreateTemplates = canUser( 'create', 'templates' );
return {
isPostsPage: _isPostsPage,
availableTemplates: editorSettings.availableTemplates,
fetchedTemplates: canCreateTemplates
? getEntityRecords( 'postType', 'wp_template', {
post_type: select( editorStore ).getCurrentPostType(),
per_page: -1,
} )
: undefined,
selectedTemplateSlug:
select( editorStore ).getEditedPostAttribute( 'template' ),
canCreate:
canCreateTemplates &&
! _isPostsPage &&
editorSettings.supportsTemplateMode,
canEdit:
canCreateTemplates &&
editorSettings.supportsTemplateMode &&
!! select( editPostStore ).getEditedPostTemplate(),
};
}, [] );
const options = useMemo(
() =>
Object.entries( {
...availableTemplates,
...Object.fromEntries(
( fetchedTemplates ?? [] ).map( ( { slug, title } ) => [
slug,
title.rendered,
] )
),
} ).map( ( [ slug, title ] ) => ( { value: slug, label: title } ) ),
[ availableTemplates, fetchedTemplates ]
);
const selectedOption =
options.find( ( option ) => option.value === selectedTemplateSlug ) ??
options.find( ( option ) => ! option.value ); // The default option has '' value.
const { editPost } = useDispatch( editorStore );
const { __unstableSwitchToTemplateMode } = useDispatch( editPostStore );
const [ isCreateModalOpen, setIsCreateModalOpen ] = useState( false );
return (
<div className="edit-post-post-template__form">
<InspectorPopoverHeader
title={ __( 'Template' ) }
help={ __(
'Templates define the way content is displayed when viewing your site.'
) }
actions={
canCreate
? [
{
icon: addTemplate,
label: __( 'Add template' ),
onClick: () => setIsCreateModalOpen( true ),
},
]
: []
}
onClose={ onClose }
/>
{ isPostsPage ? (
<Notice
className="edit-post-post-template__notice"
status="warning"
isDismissible={ false }
>
{ __( 'The posts page template cannot be changed.' ) }
</Notice>
) : (
<SelectControl
__nextHasNoMarginBottom
hideLabelFromVision
label={ __( 'Template' ) }
value={ selectedOption?.value ?? '' }
options={ options }
onChange={ ( slug ) =>
editPost( { template: slug || '' } )
}
/>
) }
{ canEdit && (
<p>
<Button
variant="link"
onClick={ () => __unstableSwitchToTemplateMode() }
>
{ __( 'Edit template' ) }
</Button>
</p>
) }
{ isCreateModalOpen && (
<PostTemplateCreateModal
onClose={ () => setIsCreateModalOpen( false ) }
/>
) }
</div>
);
}