-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd3e09e
commit f12c10d
Showing
3 changed files
with
84 additions
and
25 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
81 changes: 81 additions & 0 deletions
81
packages/edit-site/src/hooks/commands/use-edit-mode-commands.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,81 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { trash } from '@wordpress/icons'; | ||
import { privateApis as commandsPrivateApis } from '@wordpress/commands'; | ||
import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editSiteStore } from '../../store'; | ||
import useEditedEntityRecord from '../../components/use-edited-entity-record'; | ||
import isTemplateRemovable from '../../utils/is-template-removable'; | ||
import isTemplateRevertable from '../../utils/is-template-revertable'; | ||
import { unlock } from '../../private-apis'; | ||
|
||
const { useCommandLoader } = unlock( commandsPrivateApis ); | ||
const { useHistory } = unlock( routerPrivateApis ); | ||
|
||
function useEditModeCommandLoader() { | ||
const { removeTemplate, revertTemplate } = useDispatch( editSiteStore ); | ||
const history = useHistory(); | ||
const { isLoaded, record: template } = useEditedEntityRecord(); | ||
const isRemovable = | ||
isLoaded && !! template && isTemplateRemovable( template ); | ||
const isRevertable = | ||
isLoaded && !! template && isTemplateRevertable( template ); | ||
|
||
const commands = []; | ||
if ( isRemovable ) { | ||
const label = | ||
template.type === 'wp_template' | ||
? __( 'Delete template' ) | ||
: __( 'Delete template part' ); | ||
commands.push( { | ||
name: label, | ||
label, | ||
icon: trash, | ||
context: 'site-editor-edit', | ||
callback: ( { close } ) => { | ||
removeTemplate( template ); | ||
// Navigate to the template list | ||
history.push( { | ||
path: '/' + template.type, | ||
} ); | ||
close(); | ||
}, | ||
} ); | ||
} | ||
if ( isRevertable ) { | ||
const label = | ||
template.type === 'wp_template' | ||
? __( 'Clear template customizations' ) | ||
: __( 'Clear template part customizations' ); | ||
commands.push( { | ||
name: label, | ||
label, | ||
icon: trash, | ||
callback: ( { close } ) => { | ||
revertTemplate( template, { allowUndo: false } ); | ||
close(); | ||
}, | ||
} ); | ||
} | ||
|
||
return { | ||
isLoading: ! isLoaded, | ||
commands, | ||
}; | ||
} | ||
|
||
export function useEditModeCommands() { | ||
useCommandLoader( { | ||
name: 'core/edit-site/manipulate-document', | ||
group: __( 'Templates' ), | ||
hook: useEditModeCommandLoader, | ||
context: 'site-editor-edit', | ||
} ); | ||
} |