Skip to content

Commit

Permalink
This commit:
Browse files Browse the repository at this point in the history
- refactors useGlobalOutput by splitting out the build logic into a separate hook so that we can pass a custom config
- uses wp.date.getSettings().formats.datetimeAbbreviated for formatted date in the revisions buttons
- removes unnecessary role on the ordered list
- updated copy
- replacing modal component with the confirm dialog component
- minor code optimizations
- making the revisions store methods stable
- updating tests
- removing shouldShowClose button prop on the editor-canvas-container

update e2e
  • Loading branch information
ramonjd committed May 8, 2023
1 parent 2000f9f commit d2f7264
Show file tree
Hide file tree
Showing 19 changed files with 157 additions and 161 deletions.
12 changes: 12 additions & 0 deletions docs/reference-guides/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ _Returns_

- `any`: The current theme.

### getCurrentThemeGlobalStylesRevisions

Returns the revisions of the current global styles theme.

_Parameters_

- _state_ `State`: Data state.

_Returns_

- `Object | null`: The current global styles.

### getCurrentUser

Returns the current user.
Expand Down
7 changes: 5 additions & 2 deletions packages/block-editor/src/components/global-styles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ export {
useSettingsForBlockElement,
} from './hooks';
export { getBlockCSSSelector } from './get-block-css-selector';
export { useGlobalStylesOutput } from './use-global-styles-output';
export {
useGlobalStylesOutput,
useGlobalStylesOutputWithConfig,
} from './use-global-styles-output';
export { GlobalStylesContext } from './context';
export {
default as TypographyPanel,
Expand All @@ -20,4 +23,4 @@ export { default as ColorPanel, useHasColorPanel } from './color-panel';
export { default as EffectsPanel, useHasEffectsPanel } from './effects-panel';
export { default as FiltersPanel, useHasFiltersPanel } from './filters-panel';
export { default as AdvancedPanel } from './advanced-panel';
export { isGlobalStyleConfigEqual } from './utils';
export { areGlobalStyleConfigsEqual } from './utils';
15 changes: 8 additions & 7 deletions packages/block-editor/src/components/global-styles/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import {
isGlobalStyleConfigEqual,
areGlobalStyleConfigsEqual,
getPresetVariableFromValue,
getValueFromVariable,
} from '../utils';
Expand Down Expand Up @@ -208,9 +208,10 @@ describe( 'editor utils', () => {
} );
} );

describe( 'isGlobalStyleConfigEqual', () => {
describe( 'areGlobalStyleConfigsEqual', () => {
test.each( [
{ original: null, variation: null, expected: false },
{ original: null, variation: null, expected: true },
{ original: {}, variation: {}, expected: true },
{ original: {}, variation: undefined, expected: false },
{
original: {
Expand Down Expand Up @@ -250,11 +251,11 @@ describe( 'editor utils', () => {
expected: true,
},
] )(
'.isGlobalStyleConfigEqual( $original, $variation )',
'.areGlobalStyleConfigsEqual( $original, $variation )',
( { original, variation, expected } ) => {
expect( isGlobalStyleConfigEqual( original, variation ) ).toBe(
expected
);
expect(
areGlobalStyleConfigsEqual( original, variation )
).toBe( expected );
}
);
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1106,12 +1106,17 @@ const processCSSNesting = ( css, blockSelector ) => {
return processedCSS;
};

export function useGlobalStylesOutput( customMergedConfig = null ) {
let { merged: mergedConfig } = useContext( GlobalStylesContext );
if ( !! customMergedConfig ) {
mergedConfig = customMergedConfig;
}

/**
* Returns the global styles output using a global styles configuration.
* If wishing to generate global styles and settings based on the
* global styles config loaded in the editor context, use `useGlobalStylesOutput()`.
* The use case for a custom config is to generate bespoke styles
* and settings for previews, or other out-of-editor experiences.
*
* @param {Object} mergedConfig Global styles configuration.
* @return {Array} Array of stylesheets and settings.
*/
export function useGlobalStylesOutputWithConfig( mergedConfig = {} ) {
const [ blockGap ] = useGlobalSetting( 'spacing.blockGap' );
const hasBlockGapSupport = blockGap !== null;
const hasFallbackGapSupport = ! hasBlockGapSupport; // This setting isn't useful yet: it exists as a placeholder for a future explicit fallback styles support.
Expand Down Expand Up @@ -1193,3 +1198,13 @@ export function useGlobalStylesOutput( customMergedConfig = null ) {
disableLayoutStyles,
] );
}

/**
* Returns the global styles output based on the current state of global styles config loaded in the editor context.
*
* @return {Array} Array of stylesheets and settings.
*/
export function useGlobalStylesOutput() {
const { merged: mergedConfig } = useContext( GlobalStylesContext );
return useGlobalStylesOutputWithConfig( mergedConfig );
}
8 changes: 4 additions & 4 deletions packages/block-editor/src/components/global-styles/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ export function scopeSelector( scope, selector ) {
* ```js
* const globalStyles = { styles: { typography: { fontSize: '10px' } }, settings: {} };
* const variation = { styles: { typography: { fontSize: '10000px' } }, settings: {} };
* const isEqual = isGlobalStyleConfigEqual( globalStyles, variation );
* const isEqual = areGlobalStyleConfigsEqual( globalStyles, variation );
* // false
* ```
*
Expand All @@ -394,9 +394,9 @@ export function scopeSelector( scope, selector ) {
*
* @return {boolean} Whether `original` and `variation` match.
*/
export function isGlobalStyleConfigEqual( original, variation ) {
if ( ! original || ! variation ) {
return false;
export function areGlobalStyleConfigsEqual( original, variation ) {
if ( typeof original !== 'object' || typeof variation !== 'object' ) {
return original === variation;
}
return (
fastDeepEqual( original?.styles, variation?.styles ) &&
Expand Down
12 changes: 12 additions & 0 deletions packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,18 @@ _Returns_

- `any`: The current theme.

### getCurrentThemeGlobalStylesRevisions

Returns the revisions of the current global styles theme.

_Parameters_

- _state_ `State`: Data state.

_Returns_

- `Object | null`: The current global styles.

### getCurrentUser

Returns the current user.
Expand Down
5 changes: 1 addition & 4 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,7 @@ export function receiveThemeSupports() {
*
* @return {Object} Action object.
*/
export function __experimentalReceiveThemeGlobalStyleRevisions(
currentId,
revisions
) {
export function receiveThemeGlobalStyleRevisions( currentId, revisions ) {
return {
type: 'RECEIVE_THEME_GLOBAL_STYLE_REVISIONS',
currentId,
Expand Down
11 changes: 6 additions & 5 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,10 @@ export const __experimentalGetCurrentThemeGlobalStylesVariations =
);
};

export const __experimentalGetCurrentThemeGlobalStylesRevisions =
/**
* Fetches and returns the revisions of the current global styles theme.
*/
export const getCurrentThemeGlobalStylesRevisions =
() =>
async ( { resolveSelect, dispatch } ) => {
const globalStylesId =
Expand All @@ -526,16 +529,14 @@ export const __experimentalGetCurrentThemeGlobalStylesRevisions =
] )
)
);
dispatch.__experimentalReceiveThemeGlobalStyleRevisions(
dispatch.receiveThemeGlobalStyleRevisions(
globalStylesId,
revisions
);
}
};

__experimentalGetCurrentThemeGlobalStylesRevisions.shouldInvalidate = (
action
) => {
getCurrentThemeGlobalStylesRevisions.shouldInvalidate = ( action ) => {
return (
action.type === 'SAVE_ENTITY_RECORD_FINISH' &&
action.kind === 'root' &&
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,7 @@ export function getNavigationFallbackId(
*
* @return The current global styles.
*/
export function __experimentalGetCurrentThemeGlobalStylesRevisions(
export function getCurrentThemeGlobalStylesRevisions(
state: State
): Object | null {
const currentGlobalStylesId =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ const {
Fill: EditorCanvasContainerFill,
} = createPrivateSlotFill( SLOT_FILL_NAME );

function EditorCanvasContainer( {
children,
showCloseButton = false,
closeButtonLabel,
onClose = () => {},
} ) {
function EditorCanvasContainer( { children, closeButtonLabel, onClose } ) {
const editorCanvasContainerView = useSelect(
( select ) =>
unlock( select( editSiteStore ) ).getEditorCanvasContainerView(),
Expand All @@ -68,7 +63,9 @@ function EditorCanvasContainer( {
[ editorCanvasContainerView ]
);
function onCloseContainer() {
onClose();
if ( typeof onClose === 'function' ) {
onClose();
}
setEditorCanvasContainerView( undefined );
setIsClosed( true );
}
Expand Down Expand Up @@ -96,7 +93,7 @@ function EditorCanvasContainer( {
return null;
}

const shouldShowCloseButton = showCloseButton || !! closeButtonLabel;
const shouldShowCloseButton = onClose || closeButtonLabel;

return (
<EditorCanvasContainerFill>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { __ } from '@wordpress/i18n';
import {
Button,
__experimentalUseNavigator as useNavigator,
__experimentalConfirmDialog as ConfirmDialog,
Spinner,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
Expand All @@ -22,11 +23,10 @@ import { unlock } from '../../../private-apis';
import Revisions from '../../revisions';
import SidebarFixedBottom from '../../sidebar-edit-mode/sidebar-fixed-bottom';
import { store as editSiteStore } from '../../../store';
import useGetGlobalStylesRevisions from './use-get-global-styles-revisions';
import RestoreGlobalStylesRevisionModal from './restore-global-styles-revision-modal';
import useGlobalStylesRevisions from './use-global-styles-revisions';
import RevisionsButtons from './revisions-buttons';

const { GlobalStylesContext, isGlobalStyleConfigEqual } = unlock(
const { GlobalStylesContext, areGlobalStyleConfigsEqual } = unlock(
blockEditorPrivateApis
);

Expand All @@ -44,11 +44,16 @@ function ScreenRevisions() {
}, [] );

const { revisions, isLoading, hasUnsavedChanges } =
useGetGlobalStylesRevisions();
const [ globalStylesRevision, setGlobalStylesRevision ] = useState( {} );
useGlobalStylesRevisions();
const [ globalStylesRevision, setGlobalStylesRevision ] =
useState( userConfig );

const [ currentRevisionId, setCurrentRevisionId ] = useState(
revisions?.[ 0 ]?.id
/*
* We need this for the first render,
* otherwise the unsaved changes haven't been merged into the revisions array yet.
*/
hasUnsavedChanges ? 'unsaved' : revisions?.[ 0 ]?.id
);
const [
isLoadingRevisionWithUnsavedChanges,
Expand Down Expand Up @@ -89,7 +94,7 @@ function ScreenRevisions() {

const isLoadButtonEnabled =
!! globalStylesRevision?.id &&
! isGlobalStyleConfigEqual( globalStylesRevision, userConfig );
! areGlobalStyleConfigsEqual( globalStylesRevision, userConfig );

return (
<>
Expand Down Expand Up @@ -135,18 +140,36 @@ function ScreenRevisions() {
}
} }
>
{ __( 'Load revision' ) }
{ __( 'Apply' ) }
</Button>
</SidebarFixedBottom>
) }
</div>
{ isLoadingRevisionWithUnsavedChanges && (
<RestoreGlobalStylesRevisionModal
onClose={ () =>
<ConfirmDialog
title={ __(
'Loading this revision will discard all unsaved changes.'
) }
isOpen={ isLoadingRevisionWithUnsavedChanges }
confirmButtonText={ __( ' Discard unsaved changes' ) }
onConfirm={ () => restoreRevision( globalStylesRevision ) }
onCancel={ () =>
setIsLoadingRevisionWithUnsavedChanges( false )
}
onSubmit={ () => restoreRevision( globalStylesRevision ) }
/>
>
<>
<h2>
{ __(
'Loading this revision will discard all unsaved changes.'
) }
</h2>
<p>
{ __(
'Do you want to replace your unsaved changes in the editor?'
) }
</p>
</>
</ConfirmDialog>
) }
</>
);
Expand Down

This file was deleted.

Loading

0 comments on commit d2f7264

Please sign in to comment.