-
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.
Extract BlockThemePreviews-related code from the editor package (#50863)
* Extract BlockThemePreviews-related code from the editor package * Use props to pass values * Lift up the states * Reuse existing functions * extract _EntitiesSavedStates component * Make EntitiesSavedStatesExtensible provate API * Remove window.__experimentalEnableThemePreviews * Remove the experiment option * Fix missing props * Revert "Remove the experiment option" This reverts commit 1f8961a. * Remove unnecessary destructuring assignment
- Loading branch information
Showing
7 changed files
with
176 additions
and
128 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
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.
1100545
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.
Flaky tests detected in 1100545.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5081763755
📝 Reported issues:
No Behaviors
should be the default as defined in the core theme.json #50923 in/test/e2e/specs/editor/various/behaviors.spec.js