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

Lodash: Remove _.isEmpty() from site editor #50917

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { useContext, useMemo } from '@wordpress/element';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
/**
* External dependencies
*/
import { isEmpty } from 'lodash';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -75,7 +72,12 @@ export default function useGlobalStylesRevisions() {
}

// Adds an item for unsaved changes.
if ( isDirty && ! isEmpty( userConfig ) && currentUser ) {
if (
isDirty &&
userConfig &&
Object.keys( userConfig ).length > 0 &&
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

userConfig is already used as a potentially nullish object, which is why we're adding the extra checks.

currentUser
) {
const unsavedRevision = {
id: 'unsaved',
styles: userConfig?.styles,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -14,7 +9,7 @@ const { Fill: ToolsMoreMenuGroup, Slot } = createSlotFill(

ToolsMoreMenuGroup.Slot = ( { fillProps } ) => (
<Slot fillProps={ fillProps }>
{ ( fills ) => ! isEmpty( fills ) && fills }
{ ( fills ) => fills && fills.length > 0 }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fills are always an array:

const fills = ( getFills( name, this ) ?? [] )

I can actually remove the fills && check, but decided to keep it just in case.

</Slot>
);

Expand Down
13 changes: 6 additions & 7 deletions packages/edit-site/src/components/revisions/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -30,6 +25,10 @@ import EditorCanvasContainer from '../editor-canvas-container';
const { ExperimentalBlockEditorProvider, useGlobalStylesOutputWithConfig } =
unlock( blockEditorPrivateApis );

function isObjectEmpty( object ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used 4 times, so it can't hurt to have it as an inline helper function.

return ! object || Object.keys( object ).length === 0;
}

function Revisions( { onClose, userConfig, blocks } ) {
const { baseConfig } = useSelect(
( select ) => ( {
Expand All @@ -42,7 +41,7 @@ function Revisions( { onClose, userConfig, blocks } ) {
);

const mergedConfig = useMemo( () => {
if ( ! isEmpty( userConfig ) && ! isEmpty( baseConfig ) ) {
if ( ! isObjectEmpty( userConfig ) && ! isObjectEmpty( baseConfig ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both userConfig and baseConfig are objects, but we're adding safeguards just in case.

return mergeBaseAndUserConfigs( baseConfig, userConfig );
}
return {};
Expand All @@ -65,7 +64,7 @@ function Revisions( { onClose, userConfig, blocks } ) {
const [ globalStyles ] = useGlobalStylesOutputWithConfig( mergedConfig );

const editorStyles =
! isEmpty( globalStyles ) && ! isEmpty( userConfig )
! isObjectEmpty( globalStyles ) && ! isObjectEmpty( userConfig )
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both userConfig and globalStyles are objects, but we're adding safeguards just in case.

? globalStyles
: settings.styles;

Expand Down