@@ -31,7 +31,6 @@ import {
3131} from '../../store/constants' ;
3232import { store as editorStore } from '../../store' ;
3333import { unlock } from '../../lock-unlock' ;
34- import isTemplateRevertable from '../../store/utils/is-template-revertable' ;
3534import { exportPatternAsJSONAction } from './export-pattern-action' ;
3635import { CreateTemplatePartModalContents } from '../create-template-part-modal' ;
3736
@@ -57,14 +56,6 @@ function isTemplateRemovable( template ) {
5756 ! template ?. has_theme_file
5857 ) ;
5958}
60- const canDeleteOrReset = ( item ) => {
61- const isTemplatePart = item . type === TEMPLATE_PART_POST_TYPE ;
62- const isUserPattern = item . type === PATTERN_TYPES . user ;
63- return (
64- isUserPattern ||
65- ( isTemplatePart && item . source === TEMPLATE_ORIGINS . custom )
66- ) ;
67- } ;
6859
6960function getItemTitle ( item ) {
7061 if ( typeof item . title === 'string' ) {
@@ -795,132 +786,6 @@ const useDuplicatePostAction = ( postType ) => {
795786 ) ;
796787} ;
797788
798- const isTemplatePartRevertable = ( item ) => {
799- if ( ! item ) {
800- return false ;
801- }
802- const hasThemeFile = item ?. has_theme_file ;
803- return canDeleteOrReset ( item ) && hasThemeFile ;
804- } ;
805-
806- const resetTemplateAction = {
807- id : 'reset-template' ,
808- label : __ ( 'Reset' ) ,
809- isEligible : ( item ) => {
810- return item . type === TEMPLATE_PART_POST_TYPE
811- ? isTemplatePartRevertable ( item )
812- : isTemplateRevertable ( item ) ;
813- } ,
814- icon : backup ,
815- supportsBulk : true ,
816- hideModalHeader : true ,
817- RenderModal : ( { items, closeModal, onActionPerformed } ) => {
818- const [ isBusy , setIsBusy ] = useState ( false ) ;
819- const { revertTemplate, removeTemplates } = unlock (
820- useDispatch ( editorStore )
821- ) ;
822- const { saveEditedEntityRecord } = useDispatch ( coreStore ) ;
823- const { createSuccessNotice, createErrorNotice } =
824- useDispatch ( noticesStore ) ;
825- const onConfirm = async ( ) => {
826- try {
827- if ( items [ 0 ] . type === TEMPLATE_PART_POST_TYPE ) {
828- await removeTemplates ( items ) ;
829- } else {
830- for ( const template of items ) {
831- if ( template . type === TEMPLATE_POST_TYPE ) {
832- await revertTemplate ( template , {
833- allowUndo : false ,
834- } ) ;
835- await saveEditedEntityRecord (
836- 'postType' ,
837- template . type ,
838- template . id
839- ) ;
840- }
841- }
842- createSuccessNotice (
843- items . length > 1
844- ? sprintf (
845- /* translators: The number of items. */
846- __ ( '%s items reset.' ) ,
847- items . length
848- )
849- : sprintf (
850- /* translators: The template/part's name. */
851- __ ( '"%s" reset.' ) ,
852- decodeEntities ( getItemTitle ( items [ 0 ] ) )
853- ) ,
854- {
855- type : 'snackbar' ,
856- id : 'revert-template-action' ,
857- }
858- ) ;
859- }
860- } catch ( error ) {
861- let fallbackErrorMessage ;
862- if ( items [ 0 ] . type === TEMPLATE_POST_TYPE ) {
863- fallbackErrorMessage =
864- items . length === 1
865- ? __ (
866- 'An error occurred while reverting the template.'
867- )
868- : __ (
869- 'An error occurred while reverting the templates.'
870- ) ;
871- } else {
872- fallbackErrorMessage =
873- items . length === 1
874- ? __ (
875- 'An error occurred while reverting the template part.'
876- )
877- : __ (
878- 'An error occurred while reverting the template parts.'
879- ) ;
880- }
881- const errorMessage =
882- error . message && error . code !== 'unknown_error'
883- ? error . message
884- : fallbackErrorMessage ;
885-
886- createErrorNotice ( errorMessage , { type : 'snackbar' } ) ;
887- }
888- } ;
889- return (
890- < VStack spacing = "5" >
891- < Text >
892- { __ ( 'Reset to default and clear all customizations?' ) }
893- </ Text >
894- < HStack justify = "right" >
895- < Button
896- variant = "tertiary"
897- onClick = { closeModal }
898- disabled = { isBusy }
899- __experimentalIsFocusable
900- >
901- { __ ( 'Cancel' ) }
902- </ Button >
903- < Button
904- variant = "primary"
905- onClick = { async ( ) => {
906- setIsBusy ( true ) ;
907- await onConfirm ( items ) ;
908- onActionPerformed ?. ( items ) ;
909- setIsBusy ( false ) ;
910- closeModal ( ) ;
911- } }
912- isBusy = { isBusy }
913- disabled = { isBusy }
914- __experimentalIsFocusable
915- >
916- { __ ( 'Reset' ) }
917- </ Button >
918- </ HStack >
919- </ VStack >
920- ) ;
921- } ,
922- } ;
923-
924789export const duplicatePatternAction = {
925790 id : 'duplicate-pattern' ,
926791 label : _x ( 'Duplicate' , 'action label' ) ,
@@ -1046,9 +911,7 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
1046911 isPattern && userCanCreatePostType && duplicatePatternAction ,
1047912 supportsTitle && renamePostActionForPostType ,
1048913 isPattern && exportPatternAsJSONAction ,
1049- isTemplateOrTemplatePart
1050- ? resetTemplateAction
1051- : restorePostActionForPostType ,
914+ ! isTemplateOrTemplatePart && restorePostActionForPostType ,
1052915 ! isTemplateOrTemplatePart &&
1053916 ! isPattern &&
1054917 trashPostActionForPostType ,
0 commit comments