Skip to content
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

3 changes: 0 additions & 3 deletions lib/experimental/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ function gutenberg_enable_experiments() {
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-details-blocks', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableDetailsBlocks = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-theme-previews', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableThemePreviews = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-pattern-enhancements', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnablePatternEnhancements = true', 'before' );
}
Expand Down
77 changes: 62 additions & 15 deletions packages/edit-site/src/components/save-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } ) => {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ) => {
Expand All @@ -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 ? (
Expand All @@ -56,7 +103,7 @@ export default function SavePanel() {
'Save site, content, and template changes'
) }
>
{ entitySavedStates }
<_EntitiesSavedStates onClose={ onClose } />
</Modal>
) : null;
}
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions packages/edit-site/src/utils/is-previewing-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import { getQueryArg } from '@wordpress/url';

export function isPreviewingTheme() {
return (
window?.__experimentalEnableThemePreviews &&
getQueryArg( window.location.href, 'theme_preview' ) !== undefined
);
return getQueryArg( window.location.href, 'theme_preview' ) !== undefined;
}

export function currentlyPreviewingTheme() {
Expand Down
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,
};
};
Loading