-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathactions.js
143 lines (134 loc) · 3.63 KB
/
actions.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
/**
* External dependencies
*/
import { kebabCase } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
Button,
Modal,
TextControl,
Flex,
FlexItem,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { useState } from '@wordpress/element';
import { store as editorStore } from '@wordpress/editor';
import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import { store as editPostStore } from '../../../store';
import { createBlock, serialize } from '@wordpress/blocks';
function PostTemplateActions() {
const [ isModalOpen, setIsModalOpen ] = useState( false );
const [ title, setTitle ] = useState( '' );
const { template, supportsTemplateMode } = useSelect( ( select ) => {
const { getCurrentPostType } = select( editorStore );
const { getPostType } = select( coreStore );
const { getEditedPostTemplate } = select( editPostStore );
const isViewable =
getPostType( getCurrentPostType() )?.viewable ?? false;
const _supportsTemplateMode =
select( editorStore ).getEditorSettings().supportsTemplateMode &&
isViewable;
return {
template: _supportsTemplateMode && getEditedPostTemplate(),
supportsTemplateMode: _supportsTemplateMode,
};
}, [] );
const { __unstableSwitchToTemplateMode } = useDispatch( editPostStore );
if ( ! supportsTemplateMode ) {
return null;
}
return (
<>
<div className="edit-post-template__actions">
{ !! template && (
<Button
variant="link"
onClick={ () => __unstableSwitchToTemplateMode() }
>
{ __( 'Edit' ) }
</Button>
) }
<Button variant="link" onClick={ () => setIsModalOpen( true ) }>
{ __( 'New' ) }
</Button>
</div>
{ isModalOpen && (
<Modal
title={ __( 'Create custom template' ) }
closeLabel={ __( 'Close' ) }
onRequestClose={ () => {
setIsModalOpen( false );
setTitle( '' );
} }
overlayClassName="edit-post-template__modal"
>
<form
onSubmit={ ( event ) => {
event.preventDefault();
const defaultTitle = __( 'Custom Template' );
const templateContent = [
createBlock( 'core/site-title' ),
createBlock( 'core/site-tagline' ),
createBlock( 'core/separator' ),
createBlock( 'core/post-title' ),
createBlock( 'core/post-content', {
layout: { inherit: true },
} ),
];
__unstableSwitchToTemplateMode( {
slug:
'wp-custom-template-' +
kebabCase( title ?? defaultTitle ),
content: serialize( templateContent ),
title: title ?? defaultTitle,
} );
setIsModalOpen( false );
} }
>
<Flex align="flex-start" gap={ 8 }>
<FlexItem>
<TextControl
label={ __( 'Name' ) }
value={ title }
onChange={ setTitle }
help={ __(
'Describe the purpose of the template, e.g. "Full Width". Custom templates can be applied to any post or page.'
) }
/>
</FlexItem>
</Flex>
<Flex
className="edit-post-template__modal-actions"
justify="flex-end"
expanded={ false }
>
<FlexItem>
<Button
variant="tertiary"
onClick={ () => {
setIsModalOpen( false );
setTitle( '' );
} }
>
{ __( 'Cancel' ) }
</Button>
</FlexItem>
<FlexItem>
<Button variant="primary" type="submit">
{ __( 'Create' ) }
</Button>
</FlexItem>
</Flex>
</form>
</Modal>
) }
</>
);
}
export default PostTemplateActions;