-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Site Editor: Add Dropdown to Create Generic Templates (#26284)
Add a very basic interface to create generic templates from the Site Editor navigation sidebar. Clicking on the + button will open a dropdown listing all missing generic templates, filling them with the hierarchically closest template content, plus a "General" template that creates a blank template.
- Loading branch information
Showing
6 changed files
with
146 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
91 changes: 91 additions & 0 deletions
91
...ges/edit-site/src/components/navigation-sidebar/navigation-panel/new-template-dropdown.js
This file contains 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,91 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { map, omit } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
DropdownMenu, | ||
MenuGroup, | ||
MenuItem, | ||
NavigableMenu, | ||
} from '@wordpress/components'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { Icon, plus } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import getClosestAvailableTemplate from '../../../utils/get-closest-available-template'; | ||
import { TEMPLATES_DEFAULT_DETAILS } from '../../../utils/get-template-info/constants'; | ||
|
||
export default function NewTemplateDropdown() { | ||
const templates = useSelect( | ||
( select ) => | ||
select( 'core' ).getEntityRecords( 'postType', 'wp_template', { | ||
status: [ 'publish', 'auto-draft' ], | ||
per_page: -1, | ||
} ), | ||
[] | ||
); | ||
const { addTemplate } = useDispatch( 'core/edit-site' ); | ||
|
||
const createTemplate = ( slug ) => { | ||
const closestAvailableTemplate = getClosestAvailableTemplate( | ||
slug, | ||
templates | ||
); | ||
addTemplate( { | ||
content: closestAvailableTemplate.content.raw, | ||
slug, | ||
title: slug, | ||
status: 'draft', | ||
} ); | ||
}; | ||
|
||
const missingTemplates = omit( | ||
TEMPLATES_DEFAULT_DETAILS, | ||
map( templates, 'slug' ) | ||
); | ||
|
||
return ( | ||
<DropdownMenu | ||
className="edit-site-navigation-panel__new-template-dropdown" | ||
icon={ null } | ||
label={ __( 'Add Template' ) } | ||
popoverProps={ { | ||
noArrow: false, | ||
} } | ||
toggleProps={ { | ||
children: <Icon icon={ plus } />, | ||
isSmall: true, | ||
isTertiary: true, | ||
} } | ||
> | ||
{ ( { onClose } ) => ( | ||
<NavigableMenu> | ||
<MenuGroup label={ __( 'Add Template' ) }> | ||
{ map( | ||
missingTemplates, | ||
( { title, description }, slug ) => ( | ||
<MenuItem | ||
info={ description } | ||
key={ slug } | ||
onClick={ () => { | ||
createTemplate( slug ); | ||
onClose(); | ||
} } | ||
> | ||
{ title } | ||
</MenuItem> | ||
) | ||
) } | ||
</MenuGroup> | ||
</NavigableMenu> | ||
) } | ||
</DropdownMenu> | ||
); | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/edit-site/src/utils/get-closest-available-template.js
This file contains 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,23 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { find } from 'lodash'; | ||
|
||
export default function getClosestAvailableTemplate( slug, templates ) { | ||
if ( 'front-page' === slug ) { | ||
const homeTemplate = find( templates, { slug: 'home' } ); | ||
if ( homeTemplate ) { | ||
return homeTemplate; | ||
} | ||
} | ||
|
||
if ( 'single' === slug || 'page' === slug ) { | ||
const singularTemplate = find( templates, { slug: 'singular' } ); | ||
if ( singularTemplate ) { | ||
return singularTemplate; | ||
} | ||
} | ||
|
||
const indexTemplate = find( templates, { slug: 'index' } ); | ||
return indexTemplate; | ||
} |