Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
40299b2
Add a modal to allow template switching
scruffian Sep 19, 2023
d67576e
fetch template info
scruffian Sep 20, 2023
49301d5
Allow switching to different patterns
scruffian Sep 21, 2023
5d0c438
Allow switching to different patterns
scruffian Sep 21, 2023
0e5bdaa
Add columns
scruffian Sep 21, 2023
accc523
move availble templates to the actions
scruffian Sep 21, 2023
1947768
filter for the correct templates
scruffian Sep 21, 2023
7e5e8c9
create the right data structure in the use select
scruffian Sep 21, 2023
ac58a31
move to a hook
scruffian Sep 21, 2023
e1dc6be
inject theme attribute into pattern again
scruffian Sep 21, 2023
540dfbc
put the overlay over the top of the dropdown
scruffian Sep 21, 2023
7c9d71a
fix the pattern to templates hook
draganescu Sep 25, 2023
a00363f
set the template on click
draganescu Sep 25, 2023
c78d255
Also set the blocks
scruffian Sep 26, 2023
9b76d30
remove calls to set template with the current template, since setting…
draganescu Sep 26, 2023
c3a13a4
serialize blocks so that we have correctly processed template parts
scruffian Sep 26, 2023
5787636
remove duplicated code
scruffian Sep 26, 2023
59cbfab
Remove unnecessary mapping
scruffian Sep 26, 2023
3c513a3
refactor
scruffian Sep 26, 2023
a3763ea
memoize the patterns
scruffian Sep 26, 2023
af09641
combine the useSelect
scruffian Sep 26, 2023
083478b
Update packages/edit-site/src/components/sidebar-edit-mode/page-panel…
scruffian Sep 26, 2023
ca5935a
Fix ESLint error
Mamaduka Sep 26, 2023
e7226e4
Only show the button is there is more than 1 pattern
scruffian Sep 26, 2023
f5ef5d7
Copy update
scruffian Sep 26, 2023
59c7c24
Move the hook to a subdir
scruffian Sep 27, 2023
3f1fd65
check that there are patterns
scruffian Sep 27, 2023
dd80e09
move the check
scruffian Sep 27, 2023
4b4e835
remove useCallback
scruffian Sep 27, 2023
6c54a97
change condition to show the button
scruffian Sep 27, 2023
0b1af45
change condition
scruffian Sep 27, 2023
f7a087b
move to use editEntityRecord
scruffian Sep 27, 2023
7c7fdfd
combine filters
scruffian Sep 27, 2023
3521d12
add comments
scruffian Sep 27, 2023
03e26f3
Update packages/edit-site/src/components/sidebar-edit-mode/template-p…
scruffian Sep 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add a modal to allow template switching
  • Loading branch information
scruffian authored and draganescu committed Oct 3, 2023
commit 40299b2982388d093aa2215a1923e22a95799108
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* WordPress dependencies
*/
import { useDispatch } from '@wordpress/data';
import { useMemo, useState, useCallback } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import { __experimentalBlockPatternsList as BlockPatternsList } from '@wordpress/block-editor';
import { MenuItem, Modal } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useEntityRecord } from '@wordpress/core-data';
import { parse } from '@wordpress/blocks';
import { useAsyncList } from '@wordpress/compose';

/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../../store';
import {
useAvailableTemplates,
useEditedPostContext,
} from '../page-panels/hooks';

export default function ReplaceTemplateButton( { onClick } ) {
const [ showModal, setShowModal ] = useState( false );
const availableTemplates = useAvailableTemplates();
const onClose = useCallback( () => {
setShowModal( false );
}, [] );

// TODO - this isn't a post, it's a template, so fetch the template entity.
const { postType, postId } = useEditedPostContext();
const entitiy = useEntityRecord( 'postType', postType, postId );
const { setPage } = useDispatch( editSiteStore );
if ( ! availableTemplates?.length ) {
return null;
}
const onTemplateSelect = async ( template ) => {
entitiy.edit( { template: template.name }, { undoIgnore: true } );
// TODO - work out how to save the template.
await setPage( {
context: { postType, postId },
} );
onClose(); // Close the template suggestions modal first.
onClick();
};
return (
<>
<MenuItem
info={ __(
'Replace this template entirely with another like it.'
) }
onClick={ () => setShowModal( true ) }
>
{ __( 'Replace template' ) }
</MenuItem>

{ showModal && (
<Modal
title={ __( 'Choose a template' ) }
onRequestClose={ onClose }
overlayClassName="edit-site-template-panel__replace-template-modal"
isFullScreen
>
<div className="edit-site-template-panel__replace-template-modal__content">
<TemplatesList onSelect={ onTemplateSelect } />
</div>
</Modal>
) }
</>
);
}

function TemplatesList( { onSelect } ) {
const availableTemplates = useAvailableTemplates();
const templatesAsPatterns = useMemo(
() =>
availableTemplates.map( ( template ) => ( {
name: template.slug,
blocks: parse( template.content.raw ),
title: decodeEntities( template.title.rendered ),
id: template.id,
} ) ),
[ availableTemplates ]
);
const shownTemplates = useAsyncList( templatesAsPatterns );

// TODO - make this use a grid layout.
return (
<BlockPatternsList
label={ __( 'Templates' ) }
blockPatterns={ templatesAsPatterns }
shownPatterns={ shownTemplates }
onClickPattern={ onSelect }
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import { moreVertical } from '@wordpress/icons';
*/
import { store as editSiteStore } from '../../../store';
import isTemplateRevertable from '../../../utils/is-template-revertable';
import ReplaceTemplateButton from './replace-template-button';

export default function Actions( { template } ) {
const { revertTemplate } = useDispatch( editSiteStore );
const isRevertable = isTemplateRevertable( template );
// TODO - update this condition so that we also show the dropdown when there are other template options.
if ( ! isRevertable ) {
return null;
}

return (
<DropdownMenu
icon={ moreVertical }
Expand All @@ -38,6 +41,7 @@ export default function Actions( { template } ) {
>
{ __( 'Clear customizations' ) }
</MenuItem>
<ReplaceTemplateButton onClick={ onClose } />
</MenuGroup>
) }
</DropdownMenu>
Expand Down