-
Notifications
You must be signed in to change notification settings - Fork 4k
/
block-theme.js
138 lines (130 loc) · 3.63 KB
/
block-theme.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
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';
import { DropdownMenu, MenuGroup, MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useEntityRecord, store as coreStore } from '@wordpress/core-data';
import { check } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';
/**
* Internal dependencies
*/
import { store as editorStore } from '../../store';
import SwapTemplateButton from './swap-template-button';
import ResetDefaultTemplate from './reset-default-template';
import { unlock } from '../../lock-unlock';
import CreateNewTemplate from './create-new-template';
const POPOVER_PROPS = {
className: 'editor-post-template__dropdown',
placement: 'bottom-start',
};
export default function BlockThemeControl( { id } ) {
const {
isTemplateHidden,
onNavigateToEntityRecord,
getEditorSettings,
hasGoBack,
} = useSelect( ( select ) => {
const { getRenderingMode, getEditorSettings: _getEditorSettings } =
unlock( select( editorStore ) );
const editorSettings = _getEditorSettings();
return {
isTemplateHidden: getRenderingMode() === 'post-only',
onNavigateToEntityRecord: editorSettings.onNavigateToEntityRecord,
getEditorSettings: _getEditorSettings,
hasGoBack: editorSettings.hasOwnProperty(
'onNavigateToPreviousEntityRecord'
),
};
}, [] );
const { editedRecord: template, hasResolved } = useEntityRecord(
'postType',
'wp_template',
id
);
const { createSuccessNotice } = useDispatch( noticesStore );
const { setRenderingMode } = useDispatch( editorStore );
const canCreateTemplate = useSelect(
( select ) =>
select( coreStore ).canUser( 'create', 'templates' ) ?? false
);
if ( ! hasResolved ) {
return null;
}
// The site editor does not have a `onNavigateToPreviousEntityRecord` setting as it uses its own routing
// and assigns its own backlink to focusMode pages.
const notificationAction = hasGoBack
? [
{
label: __( 'Go back' ),
onClick: () =>
getEditorSettings().onNavigateToPreviousEntityRecord(),
},
]
: undefined;
return (
<DropdownMenu
popoverProps={ POPOVER_PROPS }
focusOnMount
toggleProps={ {
__next40pxDefaultSize: true,
variant: 'tertiary',
} }
label={ __( 'Template options' ) }
text={ decodeEntities( template.title ) }
icon={ null }
>
{ ( { onClose } ) => (
<>
<MenuGroup>
{ canCreateTemplate && (
<MenuItem
onClick={ () => {
onNavigateToEntityRecord( {
postId: template.id,
postType: 'wp_template',
} );
onClose();
createSuccessNotice(
__(
'Editing template. Changes made here affect all posts and pages that use the template.'
),
{
type: 'snackbar',
actions: notificationAction,
}
);
} }
>
{ __( 'Edit template' ) }
</MenuItem>
) }
<SwapTemplateButton onClick={ onClose } />
<ResetDefaultTemplate onClick={ onClose } />
{ canCreateTemplate && (
<CreateNewTemplate onClick={ onClose } />
) }
</MenuGroup>
<MenuGroup>
<MenuItem
icon={ ! isTemplateHidden ? check : undefined }
isSelected={ ! isTemplateHidden }
role="menuitemcheckbox"
onClick={ () => {
setRenderingMode(
isTemplateHidden
? 'template-locked'
: 'post-only'
);
} }
>
{ __( 'Template preview' ) }
</MenuItem>
</MenuGroup>
</>
) }
</DropdownMenu>
);
}