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

Shadow Panel: Generates unique shadow slugs by finding max suffix and incrementing it #61997

Merged
merged 5 commits into from
May 29, 2024

Conversation

amitraj2203
Copy link
Contributor

What?

Fixes: #61989

Why?

Shadow slugs were getting duplicated when we try to delete any custom shadow and add a new. See this for more details.

How?

This PR adds a function to generate unique names and slugs for shadows by finding the highest suffix number used and incrementing it.

Testing Instructions

  1. Add three custom shadows. The labels are "Shadow 1", "Shadow 2", and "Shadow3" respectively.
  2. Delete "Shadow 2".
  3. Add a new shadow.
  4. Now the new shadow will have unique slug and name.

Screenshots or screencast

Screen.Recording.2024-05-26.at.10.23.54.PM.mov

Copy link

github-actions bot commented May 26, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: amitraj2203 <[email protected]>
Co-authored-by: t-hamano <[email protected]>
Co-authored-by: talldan <[email protected]>
Co-authored-by: vk17-starlord <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

I think the approach makes sense, but personally I think this function needs to be made more generic. Similar problems will be faced when the UI for customizing font size presets and spacing scale values are implemented.

I haven't tested it, but what about a function that simply returns just the index, like below? This allows us to apply it to other presets in the future.

export function getNewIndexFromPresets( presets, slugPrefix ) {
	const nameRegex = new RegExp( `^${ slugPrefix }([\\d]+)$` );
	return presets.reduce( ( previousValue, currentValue ) => {
		if ( typeof currentValue?.slug === 'string' ) {
			const matches = currentValue?.slug.match( nameRegex );
			if ( matches ) {
				const id = parseInt( matches[ 1 ], 10 );
				if ( id >= previousValue ) {
					return id + 1;
				}
			}
		}
		return previousValue;
	}, 1 );
}

const handleAddShadow = () => {
	const newIndex = getNewIndexFromPresets( shadows, 'shadow-' );
	onCreate( {
		name: sprintf(
			/* translators: %s: is an index for a preset */
			__( 'Shadow %s' ),
			newIndex
		),
		slug: `shadow-${ newIndex }`,
		shadow: defaultShadow,
	} );
};

Additionally, we may want to consider:

  • Considering the possibility that this function will be used for general purposes in the future, move the function to packages/edit-site/src/components/global-styles/utils.js.
  • Add JSDoc to the function
  • Add some unit tests to packages/edit-site/src/components/global-styles/test

@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels May 27, 2024
@amitraj2203
Copy link
Contributor Author

Hi @t-hamano, as suggested I have created a generic function for getting the Index and added some unit tests. Let me know if there are any further improvements you'd like to make.

@amitraj2203 amitraj2203 requested a review from t-hamano May 27, 2024 08:56
@t-hamano
Copy link
Contributor

@amitraj2203

Thanks for the update!

@madhusudhand @jorgefilipecosta What do you think about this approach? I'm sure you've implemented shadow UI, so I'd love to hear your advice 🙇‍♂️

Comment on lines 14 to 22
/**
* Returns a new index based on the presets and slug prefix.
*
* @param {Array} presets The array of presets.
* @param {string} slugPrefix The prefix for the slug.
*
* @return {number} The new index.
*/
export function getNewIndexFromPresets( presets, slugPrefix ) {
Copy link
Contributor

@talldan talldan May 28, 2024

Choose a reason for hiding this comment

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

The logic in the function looks good to me - find the highest preset and increment it by one, nice work putting that together and adding the associated tests.

I think the docblock could be a little more descriptive about what the function does. It mentions 'new index', but the reader has no idea what kind of value that new index will be. It should be possible to understand exactly what the function returns without reading any of the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Acknowledged! I've updated the docblock to provide a clearer description of the function's purpose and its return value.

Comment on lines 24 to 35
return presets.reduce( ( previousValue, currentValue ) => {
if ( typeof currentValue?.slug === 'string' ) {
const matches = currentValue?.slug.match( nameRegex );
if ( matches ) {
const id = parseInt( matches[ 1 ], 10 );
if ( id >= previousValue ) {
return id + 1;
}
}
}
return previousValue;
}, 1 );
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor optional feedback (it doesn't need to be addressed), but I think the function can be more self documenting with some small changes to the variable naming and if the increment happens at the end:

	const highestPresetValue = presets.reduce( ( currentHighest, preset ) => {
		if ( typeof preset?.slug === 'string' ) {
			const matches = preset?.slug.match( nameRegex );
			if ( matches ) {
				const id = parseInt( matches[ 1 ], 10 );
				if ( id > currentHighest ) {
					return id;
				}
			}
		}
		return currentHighest;
	}, 0 );
	return highestPresetValue + 1;

I haven't tested it though, so not sure if this code still works 😄

Copy link
Contributor Author

@amitraj2203 amitraj2203 May 28, 2024

Choose a reason for hiding this comment

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

Thanks for the review. I completely agree with your suggestion and also I've tested these changes and it is working as expected.

@amitraj2203 amitraj2203 requested a review from talldan May 28, 2024 07:46
Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

LGTM! I think it's working as expected.

c42ca7267e953f15aba0486d6039eaa7.mp4

Could you rebase this PR with the latest trunk? Then the failing GitHub Actions should pass.

I would be happy if you could give us a second re-review, @talldan.

Copy link
Contributor

@talldan talldan left a comment

Choose a reason for hiding this comment

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

Looks good to me too, thanks for your contribution 🎉

I'll go ahead and merge the PR, as I don't think it will have had an impact on bundle size.

@talldan talldan merged commit e8bec8d into WordPress:trunk May 29, 2024
63 of 65 checks passed
@github-actions github-actions bot added this to the Gutenberg 18.5 milestone May 29, 2024
carstingaxion pushed a commit to carstingaxion/gutenberg that referenced this pull request Jun 4, 2024
… incrementing it (WordPress#61997)

* Add function to generate unique shadow slugs by finding max suffix and incrementing it

* Created a generic function getNewIndexFromPresets that returns the next available index.

* Adds JSdoc to getNewIndexFromPresets

* Add unit tests for getNewIndexFromPresets function

* Refactor getNewIndexFromPresets function to improve readability.

Co-authored-by: amitraj2203 <[email protected]>
Co-authored-by: t-hamano <[email protected]>
Co-authored-by: talldan <[email protected]>
Co-authored-by: vk17-starlord <[email protected]>
patil-vipul pushed a commit to patil-vipul/gutenberg that referenced this pull request Jun 17, 2024
… incrementing it (WordPress#61997)

* Add function to generate unique shadow slugs by finding max suffix and incrementing it

* Created a generic function getNewIndexFromPresets that returns the next available index.

* Adds JSdoc to getNewIndexFromPresets

* Add unit tests for getNewIndexFromPresets function

* Refactor getNewIndexFromPresets function to improve readability.

Co-authored-by: amitraj2203 <[email protected]>
Co-authored-by: t-hamano <[email protected]>
Co-authored-by: talldan <[email protected]>
Co-authored-by: vk17-starlord <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shadow Panel: slugs may conflict
3 participants