-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
edit-template.js
69 lines (63 loc) · 1.89 KB
/
edit-template.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
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import { BlockContextProvider, BlockPreview } from '@wordpress/block-editor';
import { Button, __experimentalVStack as VStack } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../../store';
export default function EditTemplate() {
const { context, hasResolved, title, blocks } = useSelect( ( select ) => {
const { getEditedPostContext, getEditedPostType, getEditedPostId } =
select( editSiteStore );
const { getEditedEntityRecord, hasFinishedResolution } =
select( coreStore );
const _context = getEditedPostContext();
const queryArgs = [
'postType',
getEditedPostType(),
getEditedPostId(),
];
const template = getEditedEntityRecord( ...queryArgs );
return {
context: _context,
hasResolved: hasFinishedResolution(
'getEditedEntityRecord',
queryArgs
),
title: template?.title,
blocks: template?.blocks,
};
}, [] );
const { setHasPageContentFocus } = useDispatch( editSiteStore );
const blockContext = useMemo(
() => ( { ...context, postType: null, postId: null } ),
[ context ]
);
if ( ! hasResolved ) {
return null;
}
return (
<VStack>
<div>{ decodeEntities( title ) }</div>
<div className="edit-site-page-panels__edit-template-preview">
<BlockContextProvider value={ blockContext }>
<BlockPreview viewportWidth={ 1024 } blocks={ blocks } />
</BlockContextProvider>
</div>
<Button
className="edit-site-page-panels__edit-template-button"
variant="secondary"
onClick={ () => setHasPageContentFocus( false ) }
>
{ __( 'Edit template' ) }
</Button>
</VStack>
);
}