-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add template replace flow to template inspector #54609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 d67576e
fetch template info
scruffian 49301d5
Allow switching to different patterns
scruffian 5d0c438
Allow switching to different patterns
scruffian 0e5bdaa
Add columns
scruffian accc523
move availble templates to the actions
scruffian 1947768
filter for the correct templates
scruffian 7e5e8c9
create the right data structure in the use select
scruffian ac58a31
move to a hook
scruffian e1dc6be
inject theme attribute into pattern again
scruffian 540dfbc
put the overlay over the top of the dropdown
scruffian 7c9d71a
fix the pattern to templates hook
draganescu a00363f
set the template on click
draganescu c78d255
Also set the blocks
scruffian 9b76d30
remove calls to set template with the current template, since setting…
draganescu c3a13a4
serialize blocks so that we have correctly processed template parts
scruffian 5787636
remove duplicated code
scruffian 59cbfab
Remove unnecessary mapping
scruffian 3c513a3
refactor
scruffian a3763ea
memoize the patterns
scruffian af09641
combine the useSelect
scruffian 083478b
Update packages/edit-site/src/components/sidebar-edit-mode/page-panel…
scruffian ca5935a
Fix ESLint error
Mamaduka e7226e4
Only show the button is there is more than 1 pattern
scruffian f5ef5d7
Copy update
scruffian 59c7c24
Move the hook to a subdir
scruffian 3f1fd65
check that there are patterns
scruffian dd80e09
move the check
scruffian 4b4e835
remove useCallback
scruffian 6c54a97
change condition to show the button
scruffian 0b1af45
change condition
scruffian f7a087b
move to use editEntityRecord
scruffian 7c7fdfd
combine filters
scruffian 3521d12
add comments
scruffian 03e26f3
Update packages/edit-site/src/components/sidebar-edit-mode/template-p…
scruffian File filter
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
commit 40299b2982388d093aa2215a1923e22a95799108
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
...ages/edit-site/src/components/sidebar-edit-mode/template-panel/replace-template-button.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } | ||
| /> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.