-
Notifications
You must be signed in to change notification settings - Fork 4k
/
home-template-details.js
232 lines (217 loc) · 6.82 KB
/
home-template-details.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { debounce } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import {
CheckboxControl,
__experimentalUseNavigator as useNavigator,
__experimentalInputControl as InputControl,
__experimentalNumberControl as NumberControl,
__experimentalTruncate as Truncate,
__experimentalItemGroup as ItemGroup,
} from '@wordpress/components';
import { header, footer, layout } from '@wordpress/icons';
import { useMemo, useState, useEffect } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
/**
* Internal dependencies
*/
import {
SidebarNavigationScreenDetailsPanel,
SidebarNavigationScreenDetailsPanelRow,
} from '../sidebar-navigation-screen-details-panel';
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';
import { useLink } from '../routes/link';
import SidebarNavigationItem from '../sidebar-navigation-item';
const EMPTY_OBJECT = {};
function TemplateAreaButton( { postId, icon, title } ) {
const icons = {
header,
footer,
};
const linkInfo = useLink( {
postType: 'wp_template_part',
postId,
} );
return (
<SidebarNavigationItem
className="edit-site-sidebar-navigation-screen-template__template-area-button"
{ ...linkInfo }
icon={ icons[ icon ] ?? layout }
withChevron
>
<Truncate
limit={ 20 }
ellipsizeMode="tail"
numberOfLines={ 1 }
className="edit-site-sidebar-navigation-screen-template__template-area-label-text"
>
{ decodeEntities( title ) }
</Truncate>
</SidebarNavigationItem>
);
}
export default function HomeTemplateDetails() {
const navigator = useNavigator();
const {
params: { postType, postId },
} = navigator;
const { editEntityRecord } = useDispatch( coreStore );
const {
allowCommentsOnNewPosts,
templatePartAreas,
postsPerPage,
postsPageTitle,
postsPageId,
currentTemplateParts,
} = useSelect(
( select ) => {
const { getEntityRecord } = select( coreStore );
const siteSettings = getEntityRecord( 'root', 'site' );
const { getSettings } = unlock( select( editSiteStore ) );
const _currentTemplateParts =
select( editSiteStore ).getCurrentTemplateTemplateParts();
const siteEditorSettings = getSettings();
const _postsPageRecord = siteSettings?.page_for_posts
? select( coreStore ).getEntityRecord(
'postType',
'page',
siteSettings?.page_for_posts
)
: EMPTY_OBJECT;
return {
allowCommentsOnNewPosts:
siteSettings?.default_comment_status === 'open',
postsPageTitle: _postsPageRecord?.title?.rendered,
postsPageId: _postsPageRecord?.id,
postsPerPage: siteSettings?.posts_per_page,
templatePartAreas: siteEditorSettings?.defaultTemplatePartAreas,
currentTemplateParts: _currentTemplateParts,
};
},
[ postType, postId ]
);
const [ commentsOnNewPostsValue, setCommentsOnNewPostsValue ] =
useState( '' );
const [ postsCountValue, setPostsCountValue ] = useState( 1 );
const [ postsPageTitleValue, setPostsPageTitleValue ] = useState( '' );
/*
* This hook serves to set the server-retrieved values,
* postsPageTitle, allowCommentsOnNewPosts, postsPerPage,
* to local state.
*/
useEffect( () => {
setCommentsOnNewPostsValue( allowCommentsOnNewPosts );
setPostsPageTitleValue( postsPageTitle );
setPostsCountValue( postsPerPage );
}, [ postsPageTitle, allowCommentsOnNewPosts, postsPerPage ] );
/*
* Merge data in currentTemplateParts with templatePartAreas,
* which contains the template icon and fallback labels
*/
const templateAreas = useMemo( () => {
return currentTemplateParts.length && templatePartAreas
? currentTemplateParts.map( ( { templatePart } ) => ( {
...templatePartAreas?.find(
( { area } ) => area === templatePart?.area
),
...templatePart,
} ) )
: [];
}, [ currentTemplateParts, templatePartAreas ] );
const setAllowCommentsOnNewPosts = ( newValue ) => {
setCommentsOnNewPostsValue( newValue );
editEntityRecord( 'root', 'site', undefined, {
default_comment_status: newValue ? 'open' : null,
} );
};
const setPostsPageTitle = ( newValue ) => {
setPostsPageTitleValue( newValue );
editEntityRecord( 'postType', 'page', postsPageId, {
title: newValue,
} );
};
const setPostsPerPage = ( newValue ) => {
setPostsCountValue( newValue );
editEntityRecord( 'root', 'site', undefined, {
posts_per_page: newValue,
} );
};
return (
<>
<SidebarNavigationScreenDetailsPanel spacing={ 6 }>
{ postsPageId && (
<SidebarNavigationScreenDetailsPanelRow>
<InputControl
className="edit-site-sidebar-navigation-screen__input-control"
placeholder={ __( 'No Title' ) }
size={ '__unstable-large' }
value={ postsPageTitleValue }
onChange={ debounce( setPostsPageTitle, 300 ) }
label={ __( 'Blog title' ) }
help={ __(
'Set the Posts Page title. Appears in search results, and when the page is shared on social media.'
) }
/>
</SidebarNavigationScreenDetailsPanelRow>
) }
<SidebarNavigationScreenDetailsPanelRow>
<NumberControl
className="edit-site-sidebar-navigation-screen__input-control"
placeholder={ 0 }
value={ postsCountValue }
size={ '__unstable-large' }
spinControls="custom"
step="1"
min="1"
onChange={ setPostsPerPage }
label={ __( 'Posts per page' ) }
help={ __(
'Set the default number of posts to display on blog pages, including categories and tags. Some templates may override this setting.'
) }
/>
</SidebarNavigationScreenDetailsPanelRow>
</SidebarNavigationScreenDetailsPanel>
<SidebarNavigationScreenDetailsPanel
title={ __( 'Discussion' ) }
spacing={ 3 }
>
<SidebarNavigationScreenDetailsPanelRow>
<CheckboxControl
className="edit-site-sidebar-navigation-screen__input-control"
label={ __( 'Allow comments on new posts' ) }
help={ __(
'Changes will apply to new posts only. Individual posts may override these settings.'
) }
checked={ commentsOnNewPostsValue }
onChange={ setAllowCommentsOnNewPosts }
/>
</SidebarNavigationScreenDetailsPanelRow>
</SidebarNavigationScreenDetailsPanel>
<SidebarNavigationScreenDetailsPanel
title={ __( 'Areas' ) }
spacing={ 3 }
>
<ItemGroup>
{ templateAreas.map(
( { label, icon, theme, slug, title } ) => (
<SidebarNavigationScreenDetailsPanelRow
key={ slug }
>
<TemplateAreaButton
postId={ `${ theme }//${ slug }` }
title={ title?.rendered || label }
icon={ icon }
/>
</SidebarNavigationScreenDetailsPanelRow>
)
) }
</ItemGroup>
</SidebarNavigationScreenDetailsPanel>
</>
);
}