Skip to content

Commit

Permalink
PaletteEdit: Fix order numbers (WordPress#52212)
Browse files Browse the repository at this point in the history
Using FSE to change styles on a page template. When using the English language in site and add new colors to Styles, the default name appear with numbers in order. However, when switching to Japanese language, the sequential order numbers did not work anymore in the default name.

Gutenberg was trying to create slugs for colors using translated strings, e.g., 色 6 but that didn't pass the regex new RegExp( ^${ slugPrefix }color-([\d]+)$ )

This commit hardcodes the English `color-` slug fragment to avoid this.

Updates unit tests.

---------

Co-authored-by: megane9988 <[email protected]>
Co-authored-by: ramonjd <[email protected]>
Co-authored-by: t-hamano <[email protected]>
Co-authored-by: mirka <[email protected]>
  • Loading branch information
5 people authored and carstingaxion committed Mar 27, 2024
1 parent f1623ff commit c945b05
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
5 changes: 5 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

- `Button`: Keep deprecated props in type definitions ([#59913](https://github.com/WordPress/gutenberg/pull/59913)).

### Bug Fix
- `PaletteEdit`: Fix number incrementing of default names for new colors added in non-en-US locales ([#52212](https://github.com/WordPress/gutenberg/pull/52212)).



## 27.1.0 (2024-03-06)

### Bug Fix
Expand Down
40 changes: 20 additions & 20 deletions packages/components/src/palette-edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,17 @@ function NameInput( { value, onChange, label }: NameInputProps ) {
}

/**
* Returns a name for a palette item in the format "Color + id".
* Returns a name and slug for a palette item. The name takes the format "Color + id".
* To ensure there are no duplicate ids, this function checks all slugs.
* It expects slugs to be in the format: slugPrefix + color- + number.
* It then sets the id component of the new name based on the incremented id of the highest existing slug id.
*
* @param elements An array of color palette items.
* @param slugPrefix The slug prefix used to match the element slug.
*
* @return A unique name for a palette item.
* @return A name and slug for the new palette item.
*/
export function getNameForPosition(
export function getNameAndSlugForPosition(
elements: PaletteElement[],
slugPrefix: string
) {
Expand All @@ -102,11 +102,14 @@ export function getNameForPosition(
return previousValue;
}, 1 );

return sprintf(
/* translators: %s: is an id for a custom color */
__( 'Color %s' ),
position
);
return {
name: sprintf(
/* translators: %s: is an id for a custom color */
__( 'Color %s' ),
position
),
slug: `${ slugPrefix }color-${ position }`,
};
}

function ColorPickerPopover< T extends Color | Gradient >( {
Expand Down Expand Up @@ -434,31 +437,28 @@ export function PaletteEdit( {
: __( 'Add color' )
}
onClick={ () => {
const optionName = getNameForPosition(
elements,
slugPrefix
);
const { name, slug } =
getNameAndSlugForPosition(
elements,
slugPrefix
);

if ( !! gradients ) {
onChange( [
...gradients,
{
gradient: DEFAULT_GRADIENT,
name: optionName,
slug:
slugPrefix +
kebabCase( optionName ),
name,
slug,
},
] );
} else {
onChange( [
...colors,
{
color: DEFAULT_COLOR,
name: optionName,
slug:
slugPrefix +
kebabCase( optionName ),
name,
slug,
},
] );
}
Expand Down
38 changes: 21 additions & 17 deletions packages/components/src/palette-edit/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { click, type, press } from '@ariakit/test';
/**
* Internal dependencies
*/
import PaletteEdit, { getNameForPosition } from '..';
import PaletteEdit, { getNameAndSlugForPosition } from '..';
import type { PaletteElement } from '../types';

const noop = () => {};
Expand All @@ -21,17 +21,18 @@ async function clearInput( input: HTMLInputElement ) {
}
}

describe( 'getNameForPosition', () => {
describe( 'getNameAndSlugForPosition', () => {
test( 'should return 1 by default', () => {
const slugPrefix = 'test-';
const elements: PaletteElement[] = [];

expect( getNameForPosition( elements, slugPrefix ) ).toEqual(
'Color 1'
);
expect( getNameAndSlugForPosition( elements, slugPrefix ) ).toEqual( {
name: 'Color 1',
slug: 'test-color-1',
} );
} );

test( 'should return a new color name with an incremented slug id', () => {
test( 'should return a new color name and slug with an incremented slug id', () => {
const slugPrefix = 'test-';
const elements = [
{
Expand All @@ -41,12 +42,13 @@ describe( 'getNameForPosition', () => {
},
];

expect( getNameForPosition( elements, slugPrefix ) ).toEqual(
'Color 2'
);
expect( getNameAndSlugForPosition( elements, slugPrefix ) ).toEqual( {
name: 'Color 2',
slug: 'test-color-2',
} );
} );

test( 'should ignore user-defined color names', () => {
test( 'should ignore user-defined color name and slug', () => {
const slugPrefix = 'test-';
const elements = [
{
Expand All @@ -56,12 +58,13 @@ describe( 'getNameForPosition', () => {
},
];

expect( getNameForPosition( elements, slugPrefix ) ).toEqual(
'Color 1'
);
expect( getNameAndSlugForPosition( elements, slugPrefix ) ).toEqual( {
name: 'Color 1',
slug: 'test-color-1',
} );
} );

test( 'should return a new color name with an incremented slug id one higher than the current highest', () => {
test( 'should return a new color name and slug with an incremented slug id one higher than the current highest', () => {
const slugPrefix = 'test-';
const elements = [
{
Expand All @@ -86,9 +89,10 @@ describe( 'getNameForPosition', () => {
},
];

expect( getNameForPosition( elements, slugPrefix ) ).toEqual(
'Color 151'
);
expect( getNameAndSlugForPosition( elements, slugPrefix ) ).toEqual( {
name: 'Color 151',
slug: 'test-color-151',
} );
} );
} );

Expand Down

0 comments on commit c945b05

Please sign in to comment.