-
Notifications
You must be signed in to change notification settings - Fork 4k
/
convert-to-template-part.js
199 lines (192 loc) · 5.19 KB
/
convert-to-template-part.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
/**
* External dependencies
*/
import { kebabCase } from 'lodash';
/**
* WordPress dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';
import {
BlockSettingsMenuControls,
store as blockEditorStore,
} from '@wordpress/block-editor';
import {
MenuItem,
Icon,
BaseControl,
TextControl,
Flex,
FlexItem,
FlexBlock,
Button,
Modal,
__experimentalRadioGroup as RadioGroup,
__experimentalRadio as Radio,
} from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { createBlock, serialize } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';
import { store as noticesStore } from '@wordpress/notices';
import { store as editorStore } from '@wordpress/editor';
import { check } from '@wordpress/icons';
/**
* Internal dependencies
*/
import { TEMPLATE_PART_AREA_GENERAL } from '../../store/constants';
export default function ConvertToTemplatePart( { clientIds, blocks } ) {
const instanceId = useInstanceId( ConvertToTemplatePart );
const [ isModalOpen, setIsModalOpen ] = useState( false );
const [ title, setTitle ] = useState( '' );
const { replaceBlocks } = useDispatch( blockEditorStore );
const { saveEntityRecord } = useDispatch( coreStore );
const { createSuccessNotice } = useDispatch( noticesStore );
const [ area, setArea ] = useState( TEMPLATE_PART_AREA_GENERAL );
const templatePartAreas = useSelect(
( select ) =>
select( editorStore ).__experimentalGetDefaultTemplatePartAreas(),
[]
);
const onConvert = async ( templatePartTitle ) => {
const defaultTitle = __( 'Untitled Template Part' );
const templatePart = await saveEntityRecord(
'postType',
'wp_template_part',
{
slug: kebabCase( templatePartTitle || defaultTitle ),
title: templatePartTitle || defaultTitle,
content: serialize( blocks ),
area,
}
);
replaceBlocks(
clientIds,
createBlock( 'core/template-part', {
slug: templatePart.slug,
theme: templatePart.theme,
} )
);
createSuccessNotice( __( 'Template part created.' ), {
type: 'snackbar',
} );
};
return (
<BlockSettingsMenuControls>
{ ( { onClose } ) => (
<>
<MenuItem
onClick={ () => {
setIsModalOpen( true );
} }
>
{ __( 'Make template part' ) }
</MenuItem>
{ isModalOpen && (
<Modal
title={ __( 'Create a template part' ) }
closeLabel={ __( 'Close' ) }
onRequestClose={ () => {
setIsModalOpen( false );
setTitle( '' );
} }
overlayClassName="edit-site-template-part-converter__modal"
>
<form
onSubmit={ ( event ) => {
event.preventDefault();
onConvert( title );
setIsModalOpen( false );
setTitle( '' );
onClose();
} }
>
<TextControl
label={ __( 'Name' ) }
value={ title }
onChange={ setTitle }
/>
<BaseControl
label={ __( 'Area' ) }
id={ `edit-site-template-part-converter__area-selection-${ instanceId }` }
className="edit-site-template-part-converter__area-base-control"
>
<RadioGroup
label={ __( 'Area' ) }
className="edit-site-template-part-converter__area-radio-group"
id={ `edit-site-template-part-converter__area-selection-${ instanceId }` }
onChange={ setArea }
checked={ area }
>
{ templatePartAreas.map(
( {
icon,
label,
area: value,
description,
} ) => (
<Radio
key={ label }
value={ value }
className="edit-site-template-part-converter__area-radio"
>
<Flex
align="start"
justify="start"
>
<FlexItem>
<Icon
icon={ icon }
/>
</FlexItem>
<FlexBlock className="edit-site-template-part-converter__option-label">
{ label }
<div>
{ description }
</div>
</FlexBlock>
<FlexItem className="edit-site-template-part-converter__checkbox">
{ area ===
value && (
<Icon
icon={
check
}
/>
) }
</FlexItem>
</Flex>
</Radio>
)
) }
</RadioGroup>
</BaseControl>
<Flex
className="edit-site-template-part-converter__convert-modal-actions"
justify="flex-end"
>
<FlexItem>
<Button
variant="secondary"
onClick={ () => {
setIsModalOpen( false );
setTitle( '' );
} }
>
{ __( 'Cancel' ) }
</Button>
</FlexItem>
<FlexItem>
<Button variant="primary" type="submit">
{ __( 'Create' ) }
</Button>
</FlexItem>
</Flex>
</form>
</Modal>
) }
</>
) }
</BlockSettingsMenuControls>
);
}