-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
duplicate-template-part.tsx
70 lines (67 loc) · 2 KB
/
duplicate-template-part.tsx
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
/**
* WordPress dependencies
*/
import { useDispatch } from '@wordpress/data';
import { __, sprintf, _x } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { useMemo } from '@wordpress/element';
// @ts-ignore
import { parse } from '@wordpress/blocks';
import type { Action } from '@wordpress/dataviews';
/**
* Internal dependencies
*/
import { TEMPLATE_PART_POST_TYPE } from '../../store/constants';
import { CreateTemplatePartModalContents } from '../../components/create-template-part-modal';
import { getItemTitle } from './utils';
import type { TemplatePart } from '../types';
const duplicateTemplatePart: Action< TemplatePart > = {
id: 'duplicate-template-part',
label: _x( 'Duplicate', 'action label' ),
isEligible: ( item ) => item.type === TEMPLATE_PART_POST_TYPE,
modalHeader: _x( 'Duplicate template part', 'action label' ),
RenderModal: ( { items, closeModal } ) => {
const [ item ] = items;
const blocks = useMemo( () => {
return (
item.blocks ??
parse(
typeof item.content === 'string'
? item.content
: item.content.raw,
{
__unstableSkipMigrationLogs: true,
}
)
);
}, [ item.content, item.blocks ] );
const { createSuccessNotice } = useDispatch( noticesStore );
function onTemplatePartSuccess() {
createSuccessNotice(
sprintf(
// translators: %s: The new template part's title e.g. 'Call to action (copy)'.
__( '"%s" duplicated.' ),
getItemTitle( item )
),
{ type: 'snackbar', id: 'edit-site-patterns-success' }
);
closeModal?.();
}
return (
<CreateTemplatePartModalContents
blocks={ blocks }
defaultArea={ item.area }
defaultTitle={ sprintf(
/* translators: %s: Existing template part title */
__( '%s (Copy)' ),
getItemTitle( item )
) }
onCreate={ onTemplatePartSuccess }
onError={ closeModal }
confirmLabel={ _x( 'Duplicate', 'action label' ) }
closeModal={ closeModal }
/>
);
},
};
export default duplicateTemplatePart;