-
Notifications
You must be signed in to change notification settings - Fork 2k
/
page-patterns-plugin.tsx
113 lines (98 loc) · 3.52 KB
/
page-patterns-plugin.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
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
import { PagePatternModal, PatternDefinition } from '@automattic/page-pattern-modal';
import { useSelect, useDispatch } from '@wordpress/data';
import { useCallback } from '@wordpress/element';
import { addFilter, removeFilter } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';
import '@wordpress/nux';
const INSERTING_HOOK_NAME = 'isInsertingPagePattern';
const INSERTING_HOOK_NAMESPACE = 'automattic/full-site-editing/inserting-pattern';
interface PagePatternsPluginProps {
patterns: PatternDefinition[];
locale?: string;
theme?: string;
}
export function PagePatternsPlugin( props: PagePatternsPluginProps ) {
const { setOpenState } = useDispatch( 'automattic/starter-page-layouts' );
const { setUsedPageOrPatternsModal } = useDispatch( 'automattic/wpcom-welcome-guide' );
const { replaceInnerBlocks } = useDispatch( 'core/block-editor' );
const { editPost } = useDispatch( 'core/editor' );
const { toggleFeature } = useDispatch( 'core/edit-post' );
const { disableTips } = useDispatch( 'core/nux' );
const selectProps = useSelect( ( select ) => {
const { isOpen, isPatternPicker } = select( 'automattic/starter-page-layouts' );
return {
isOpen: isOpen(),
isWelcomeGuideActive: select( 'core/edit-post' ).isFeatureActive( 'welcomeGuide' ) as boolean, // Gutenberg 7.2.0 or higher
areTipsEnabled: select( 'core/nux' )
? ( select( 'core/nux' ).areTipsEnabled() as boolean )
: false, // Gutenberg 7.1.0 or lower
...( isPatternPicker() && {
title: __( 'Choose a Pattern', 'full-site-editing' ),
description: __(
'Pick a pre-defined layout or continue with a blank page',
'full-site-editing'
),
} ),
};
} );
const { getMeta, postContentBlock } = useSelect( ( select ) => {
const getMeta = () => select( 'core/editor' ).getEditedPostAttribute( 'meta' );
const currentBlocks = select( 'core/editor' ).getBlocks();
return {
getMeta,
postContentBlock: currentBlocks.find( ( block ) => block.name === 'a8c/post-content' ),
};
} );
const savePatternChoice = useCallback(
( name: string ) => {
// Save selected pattern slug in meta.
const currentMeta = getMeta();
editPost( {
meta: {
...currentMeta,
_starter_page_template: name,
},
} );
},
[ editPost, getMeta ]
);
const insertPattern = useCallback(
( title, blocks ) => {
// Add filter to let the tracking library know we are inserting a template.
addFilter( INSERTING_HOOK_NAME, INSERTING_HOOK_NAMESPACE, () => true );
// Set post title.
if ( title ) {
editPost( { title } );
}
// Replace blocks.
replaceInnerBlocks( postContentBlock ? postContentBlock.clientId : '', blocks, false );
// Remove filter.
removeFilter( INSERTING_HOOK_NAME, INSERTING_HOOK_NAMESPACE );
},
[ editPost, postContentBlock, replaceInnerBlocks ]
);
const { isWelcomeGuideActive, areTipsEnabled } = selectProps;
const hideWelcomeGuide = useCallback( () => {
if ( isWelcomeGuideActive ) {
// Gutenberg 7.2.0 or higher.
toggleFeature( 'welcomeGuide' );
} else if ( areTipsEnabled ) {
// Gutenberg 7.1.0 or lower.
disableTips();
}
}, [ areTipsEnabled, disableTips, isWelcomeGuideActive, toggleFeature ] );
const handleClose = useCallback( () => {
setUsedPageOrPatternsModal();
setOpenState( 'CLOSED' );
}, [ setOpenState, setUsedPageOrPatternsModal ] );
return (
<PagePatternModal
{ ...selectProps }
onClose={ handleClose }
savePatternChoice={ savePatternChoice }
insertPattern={ insertPattern }
hideWelcomeGuide={ hideWelcomeGuide }
{ ...props }
/>
);
}