-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Extract BlockThemePreviews-related code from the editor package #50863
Merged
scruffian
merged 11 commits into
WordPress:trunk
from
okmttdhr:update/extract-block-theme-previews-to-edit-sites
May 25, 2023
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ca67f2b
Extract BlockThemePreviews-related code from the editor package
okmttdhr dabe20e
Use props to pass values
okmttdhr 59f0b06
Lift up the states
okmttdhr b6c20af
Reuse existing functions
okmttdhr 0ed41e4
extract _EntitiesSavedStates component
okmttdhr 023e17b
Make EntitiesSavedStatesExtensible provate API
okmttdhr 415c7fb
Remove window.__experimentalEnableThemePreviews
okmttdhr 1f8961a
Remove the experiment option
okmttdhr 2afc222
Fix missing props
okmttdhr 457a357
Revert "Remove the experiment option"
okmttdhr 032a8b6
Remove unnecessary destructuring assignment
okmttdhr 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,17 +7,75 @@ import classnames from 'classnames'; | |
* WordPress dependencies | ||
*/ | ||
import { Button, Modal } from '@wordpress/components'; | ||
import { EntitiesSavedStates } from '@wordpress/editor'; | ||
import { | ||
EntitiesSavedStates, | ||
useEntitiesSavedStatesIsDirty, | ||
privateApis, | ||
} from '@wordpress/editor'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { NavigableRegion } from '@wordpress/interface'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editSiteStore } from '../../store'; | ||
import { unlock } from '../../private-apis'; | ||
import { useActivateTheme } from '../../utils/use-activate-theme'; | ||
import { | ||
currentlyPreviewingTheme, | ||
isPreviewingTheme, | ||
} from '../../utils/is-previewing-theme'; | ||
|
||
const { EntitiesSavedStatesExtensible } = unlock( privateApis ); | ||
|
||
const EntitiesSavedStatesForPreview = ( { onClose } ) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is nice. I would have just used an if statement for all this, but I really like the way you encapsulated this logic in a component. |
||
const isDirtyProps = useEntitiesSavedStatesIsDirty(); | ||
let activateSaveLabel; | ||
if ( isDirtyProps.isDirty ) { | ||
activateSaveLabel = __( 'Activate & Save' ); | ||
} else { | ||
activateSaveLabel = __( 'Activate' ); | ||
} | ||
|
||
const { getTheme } = useSelect( coreStore ); | ||
const theme = getTheme( currentlyPreviewingTheme() ); | ||
const additionalPrompt = ( | ||
<p> | ||
{ sprintf( | ||
'Saving your changes will change your active theme to %1$s.', | ||
theme?.name?.rendered | ||
) } | ||
</p> | ||
); | ||
|
||
const activateTheme = useActivateTheme(); | ||
const onSave = async ( values ) => { | ||
await activateTheme(); | ||
return values; | ||
}; | ||
|
||
return ( | ||
<EntitiesSavedStatesExtensible | ||
{ ...{ | ||
...isDirtyProps, | ||
additionalPrompt, | ||
close: onClose, | ||
onSave, | ||
saveEnabled: true, | ||
saveLabel: activateSaveLabel, | ||
} } | ||
/> | ||
); | ||
}; | ||
|
||
const _EntitiesSavedStates = ( { onClose } ) => { | ||
if ( isPreviewingTheme() ) { | ||
return <EntitiesSavedStatesForPreview onClose={ onClose } />; | ||
} | ||
return <EntitiesSavedStates close={ onClose } />; | ||
}; | ||
|
||
export default function SavePanel() { | ||
const { isSaveViewOpen, canvasMode } = useSelect( ( select ) => { | ||
|
@@ -33,18 +91,7 @@ export default function SavePanel() { | |
}; | ||
}, [] ); | ||
const { setIsSaveViewOpened } = useDispatch( editSiteStore ); | ||
const activateTheme = useActivateTheme(); | ||
const onClose = () => setIsSaveViewOpened( false ); | ||
const onSave = async ( values ) => { | ||
await activateTheme(); | ||
return values; | ||
}; | ||
|
||
const entitySavedStates = window?.__experimentalEnableThemePreviews ? ( | ||
<EntitiesSavedStates close={ onClose } onSave={ onSave } /> | ||
) : ( | ||
<EntitiesSavedStates close={ onClose } /> | ||
); | ||
|
||
if ( canvasMode === 'view' ) { | ||
return isSaveViewOpen ? ( | ||
|
@@ -56,7 +103,7 @@ export default function SavePanel() { | |
'Save site, content, and template changes' | ||
) } | ||
> | ||
{ entitySavedStates } | ||
<_EntitiesSavedStates onClose={ onClose } /> | ||
</Modal> | ||
) : null; | ||
} | ||
|
@@ -69,7 +116,7 @@ export default function SavePanel() { | |
ariaLabel={ __( 'Save panel' ) } | ||
> | ||
{ isSaveViewOpen ? ( | ||
entitySavedStates | ||
<_EntitiesSavedStates onClose={ onClose } /> | ||
) : ( | ||
<div className="edit-site-editor__toggle-save-panel"> | ||
<Button | ||
|
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
85 changes: 85 additions & 0 deletions
85
packages/editor/src/components/entities-saved-states/hooks/use-is-dirty.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,85 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { useState } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
const TRANSLATED_SITE_PROPERTIES = { | ||
title: __( 'Title' ), | ||
description: __( 'Tagline' ), | ||
site_logo: __( 'Logo' ), | ||
site_icon: __( 'Icon' ), | ||
show_on_front: __( 'Show on front' ), | ||
page_on_front: __( 'Page on front' ), | ||
}; | ||
|
||
export const useIsDirty = () => { | ||
const { dirtyEntityRecords } = useSelect( ( select ) => { | ||
const dirtyRecords = | ||
select( coreStore ).__experimentalGetDirtyEntityRecords(); | ||
|
||
// Remove site object and decouple into its edited pieces. | ||
const dirtyRecordsWithoutSite = dirtyRecords.filter( | ||
( record ) => ! ( record.kind === 'root' && record.name === 'site' ) | ||
); | ||
|
||
const siteEdits = select( coreStore ).getEntityRecordEdits( | ||
'root', | ||
'site' | ||
); | ||
|
||
const siteEditsAsEntities = []; | ||
for ( const property in siteEdits ) { | ||
siteEditsAsEntities.push( { | ||
kind: 'root', | ||
name: 'site', | ||
title: TRANSLATED_SITE_PROPERTIES[ property ] || property, | ||
property, | ||
} ); | ||
} | ||
const dirtyRecordsWithSiteItems = [ | ||
...dirtyRecordsWithoutSite, | ||
...siteEditsAsEntities, | ||
]; | ||
|
||
return { | ||
dirtyEntityRecords: dirtyRecordsWithSiteItems, | ||
}; | ||
}, [] ); | ||
|
||
// Unchecked entities to be ignored by save function. | ||
const [ unselectedEntities, _setUnselectedEntities ] = useState( [] ); | ||
|
||
const setUnselectedEntities = ( | ||
{ kind, name, key, property }, | ||
checked | ||
) => { | ||
if ( checked ) { | ||
_setUnselectedEntities( | ||
unselectedEntities.filter( | ||
( elt ) => | ||
elt.kind !== kind || | ||
elt.name !== name || | ||
elt.key !== key || | ||
elt.property !== property | ||
) | ||
); | ||
} else { | ||
_setUnselectedEntities( [ | ||
...unselectedEntities, | ||
{ kind, name, key, property }, | ||
] ); | ||
} | ||
}; | ||
|
||
const isDirty = dirtyEntityRecords.length - unselectedEntities.length > 0; | ||
|
||
return { | ||
dirtyEntityRecords, | ||
isDirty, | ||
setUnselectedEntities, | ||
unselectedEntities, | ||
}; | ||
}; |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be better to keep this PR surface area small and not remove the experiment in this PR.
Why? Because if we decide we need to roll back to making this an experiment we'll also have to revert all the other code changes. If there have been other changes to those files since then, the unfortunate developer will have to handle that which isn't something we should impose upon them.
Let's make the removal of the experiment a dedicated follow up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to create another PR for 1f8961a 👍 (reverted it for this PR)
c.f. #50863 (comment)