Skip to content

Commit 0379666

Browse files
ramonjdandrewserongt-hamanojasmussenjameskoster
authored
Global style changes: refactor output for a more flexible UI and grouping (#59055)
* Refactoring output for a more flexible UI and grouping * update e2e test * Update comments and tests remove top margin * Localize comma separators and period Co-authored-by: ramonjd <[email protected]> Co-authored-by: andrewserong <[email protected]> Co-authored-by: t-hamano <[email protected]> Co-authored-by: jasmussen <[email protected]> Co-authored-by: jameskoster <[email protected]>
1 parent a2e3291 commit 0379666

File tree

7 files changed

+223
-120
lines changed

7 files changed

+223
-120
lines changed

packages/block-editor/src/components/global-styles/get-global-styles-changes.js

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const translationMap = {
2222
h4: __( 'H4' ),
2323
h5: __( 'H5' ),
2424
h6: __( 'H6' ),
25-
'settings.color': __( 'Color settings' ),
26-
'settings.typography': __( 'Typography settings' ),
25+
'settings.color': __( 'Color' ),
26+
'settings.typography': __( 'Typography' ),
2727
'styles.color': __( 'Colors' ),
2828
'styles.spacing': __( 'Spacing' ),
2929
'styles.typography': __( 'Typography' ),
@@ -54,12 +54,7 @@ function getTranslation( key ) {
5454
}
5555

5656
if ( keyArray?.[ 0 ] === 'elements' ) {
57-
const elementName = translationMap[ keyArray[ 1 ] ] || keyArray[ 1 ];
58-
return sprintf(
59-
// translators: %s: element name, e.g., heading button, link, caption.
60-
__( '%s element' ),
61-
elementName
62-
);
57+
return translationMap[ keyArray[ 1 ] ] || keyArray[ 1 ];
6358
}
6459

6560
return undefined;
@@ -114,9 +109,9 @@ function deepCompare( changedObject, originalObject, parentPath = '' ) {
114109
*
115110
* @param {Object} next The changed object to compare.
116111
* @param {Object} previous The original object to compare against.
117-
* @return {string[]} An array of translated changes.
112+
* @return {Array[]} A 2-dimensional array of tuples: [ "group", "translated change" ].
118113
*/
119-
function getGlobalStylesChangelist( next, previous ) {
114+
export function getGlobalStylesChangelist( next, previous ) {
120115
const cacheKey = JSON.stringify( { next, previous } );
121116

122117
if ( globalStylesChangesCache.has( cacheKey ) ) {
@@ -160,12 +155,12 @@ function getGlobalStylesChangelist( next, previous ) {
160155
const result = [ ...new Set( changedValueTree ) ]
161156
/*
162157
* Translate the keys.
163-
* Remove duplicate or empty translations.
158+
* Remove empty translations.
164159
*/
165160
.reduce( ( acc, curr ) => {
166161
const translation = getTranslation( curr );
167-
if ( translation && ! acc.includes( translation ) ) {
168-
acc.push( translation );
162+
if ( translation ) {
163+
acc.push( [ curr.split( '.' )[ 0 ], translation ] );
169164
}
170165
return acc;
171166
}, [] );
@@ -176,29 +171,80 @@ function getGlobalStylesChangelist( next, previous ) {
176171
}
177172

178173
/**
179-
* From a getGlobalStylesChangelist() result, returns a truncated array of translated changes.
180-
* Appends a translated string indicating the number of changes that were truncated.
174+
* From a getGlobalStylesChangelist() result, returns an array of translated global styles changes, grouped by type.
175+
* The types are 'blocks', 'elements', 'settings', and 'styles'.
181176
*
182177
* @param {Object} next The changed object to compare.
183178
* @param {Object} previous The original object to compare against.
184179
* @param {{maxResults:number}} options Options. maxResults: results to return before truncating.
185-
* @return {string[]} An array of translated changes.
180+
* @return {string[]} An array of translated changes.
186181
*/
187182
export default function getGlobalStylesChanges( next, previous, options = {} ) {
188-
const changes = getGlobalStylesChangelist( next, previous );
189-
const changesLength = changes.length;
183+
let changeList = getGlobalStylesChangelist( next, previous );
184+
const changesLength = changeList.length;
190185
const { maxResults } = options;
191186

192-
// Truncate to `n` results if necessary.
193-
if ( !! maxResults && changesLength && changesLength > maxResults ) {
194-
const deleteCount = changesLength - maxResults;
195-
const andMoreText = sprintf(
196-
// translators: %d: number of global styles changes that are not displayed in the UI.
197-
_n( '…and %d more change', '…and %d more changes', deleteCount ),
198-
deleteCount
199-
);
200-
changes.splice( maxResults, deleteCount, andMoreText );
187+
if ( changesLength ) {
188+
// Truncate to `n` results if necessary.
189+
if ( !! maxResults && changesLength > maxResults ) {
190+
changeList = changeList.slice( 0, maxResults );
191+
}
192+
return Object.entries(
193+
changeList.reduce( ( acc, curr ) => {
194+
const group = acc[ curr[ 0 ] ] || [];
195+
if ( ! group.includes( curr[ 1 ] ) ) {
196+
acc[ curr[ 0 ] ] = [ ...group, curr[ 1 ] ];
197+
}
198+
return acc;
199+
}, {} )
200+
).map( ( [ key, changeValues ] ) => {
201+
const changeValuesLength = changeValues.length;
202+
const joinedChangesValue = changeValues.join( __( ', ' ) );
203+
switch ( key ) {
204+
case 'blocks': {
205+
return sprintf(
206+
// translators: %2$s: a list of block names separated by a comma.
207+
_n( '%2$s block.', '%2$s blocks.', changeValuesLength ),
208+
changeValuesLength,
209+
joinedChangesValue
210+
);
211+
}
212+
case 'elements': {
213+
return sprintf(
214+
// translators: %2$s: a list of element names separated by a comma.
215+
_n(
216+
'%2$s element.',
217+
'%2$s elements.',
218+
changeValuesLength
219+
),
220+
changeValuesLength,
221+
joinedChangesValue
222+
);
223+
}
224+
case 'settings': {
225+
return sprintf(
226+
// translators: %s: a list of theme.json setting labels separated by a comma.
227+
__( '%s settings.' ),
228+
joinedChangesValue
229+
);
230+
}
231+
case 'styles': {
232+
return sprintf(
233+
// translators: %s: a list of theme.json top-level styles labels separated by a comma.
234+
__( '%s styles.' ),
235+
joinedChangesValue
236+
);
237+
}
238+
default: {
239+
return sprintf(
240+
// translators: %s: a list of global styles changes separated by a comma.
241+
__( '%s.' ),
242+
joinedChangesValue
243+
);
244+
}
245+
}
246+
} );
201247
}
202248

203-
return changes;
249+
return EMPTY_ARRAY;
204250
}

packages/block-editor/src/components/global-styles/test/get-global-styles-changes.js

Lines changed: 114 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/**
22
* Internal dependencies
33
*/
4-
import getGlobalStylesChanges from '../get-global-styles-changes';
4+
import getGlobalStylesChanges, {
5+
getGlobalStylesChangelist,
6+
} from '../get-global-styles-changes';
57

68
/**
79
* WordPress dependencies
@@ -12,24 +14,8 @@ import {
1214
getBlockTypes,
1315
} from '@wordpress/blocks';
1416

15-
describe( 'getGlobalStylesChanges', () => {
16-
beforeEach( () => {
17-
registerBlockType( 'core/test-fiori-di-zucca', {
18-
save: () => {},
19-
category: 'text',
20-
title: 'Test pumpkin flowers',
21-
edit: () => {},
22-
} );
23-
} );
24-
25-
afterEach( () => {
26-
getBlockTypes().forEach( ( block ) => {
27-
unregisterBlockType( block.name );
28-
} );
29-
} );
30-
31-
const revision = {
32-
id: 10,
17+
describe( 'getGlobalStylesChanges and utils', () => {
18+
const next = {
3319
styles: {
3420
typography: {
3521
fontSize: 'var(--wp--preset--font-size--potato)',
@@ -85,11 +71,18 @@ describe( 'getGlobalStylesChanges', () => {
8571
},
8672
],
8773
},
74+
gradients: [
75+
{
76+
name: 'Something something',
77+
gradient:
78+
'linear-gradient(105deg,rgba(6,147,100,1) 0%,rgb(155,81,100) 100%)',
79+
slug: 'something-something',
80+
},
81+
],
8882
},
8983
},
9084
};
91-
const previousRevision = {
92-
id: 9,
85+
const previous = {
9386
styles: {
9487
typography: {
9588
fontSize: 'var(--wp--preset--font-size--fungus)',
@@ -161,74 +154,120 @@ describe( 'getGlobalStylesChanges', () => {
161154
color: 'blue',
162155
},
163156
],
157+
custom: [
158+
{
159+
slug: 'one',
160+
color: 'tomato',
161+
},
162+
],
164163
},
164+
gradients: [
165+
{
166+
name: 'Vivid cyan blue to vivid purple',
167+
gradient:
168+
'linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)',
169+
slug: 'vivid-cyan-blue-to-vivid-purple',
170+
},
171+
],
172+
},
173+
typography: {
174+
fluid: true,
165175
},
166176
},
167177
};
168178

169-
it( 'returns a list of changes and caches them', () => {
170-
const resultA = getGlobalStylesChanges( revision, previousRevision );
171-
expect( resultA ).toEqual( [
172-
'Colors',
173-
'Typography',
174-
'Test pumpkin flowers',
175-
'H3 element',
176-
'Caption element',
177-
'H6 element',
178-
'Link element',
179-
'Color settings',
180-
] );
181-
182-
const resultB = getGlobalStylesChanges( revision, previousRevision );
183-
184-
expect( resultA ).toBe( resultB );
179+
beforeEach( () => {
180+
registerBlockType( 'core/test-fiori-di-zucca', {
181+
save: () => {},
182+
category: 'text',
183+
title: 'Test pumpkin flowers',
184+
edit: () => {},
185+
} );
185186
} );
186187

187-
it( 'returns a list of truncated changes', () => {
188-
const resultA = getGlobalStylesChanges( revision, previousRevision, {
189-
maxResults: 3,
188+
afterEach( () => {
189+
getBlockTypes().forEach( ( block ) => {
190+
unregisterBlockType( block.name );
190191
} );
191-
expect( resultA ).toEqual( [
192-
'Colors',
193-
'Typography',
194-
'Test pumpkin flowers',
195-
'…and 5 more changes',
196-
] );
197192
} );
198193

199-
it( 'skips unknown and unchanged keys', () => {
200-
const result = getGlobalStylesChanges(
201-
{
202-
styles: {
203-
frogs: {
204-
legs: 'green',
205-
},
206-
typography: {
207-
fontSize: '1rem',
208-
},
209-
settings: {
210-
'': {
211-
'': 'foo',
194+
describe( 'getGlobalStylesChanges()', () => {
195+
it( 'returns a list of changes', () => {
196+
const result = getGlobalStylesChanges( next, previous );
197+
expect( result ).toEqual( [
198+
'Colors, Typography styles.',
199+
'Test pumpkin flowers block.',
200+
'H3, Caption, H6, Link elements.',
201+
'Color, Typography settings.',
202+
] );
203+
} );
204+
205+
it( 'returns a list of truncated changes', () => {
206+
const resultA = getGlobalStylesChanges( next, previous, {
207+
maxResults: 3,
208+
} );
209+
expect( resultA ).toEqual( [
210+
'Colors, Typography styles.',
211+
'Test pumpkin flowers block.',
212+
] );
213+
} );
214+
215+
it( 'skips unknown and unchanged keys', () => {
216+
const result = getGlobalStylesChanges(
217+
{
218+
styles: {
219+
frogs: {
220+
legs: 'green',
221+
},
222+
typography: {
223+
fontSize: '1rem',
224+
},
225+
settings: {
226+
'': {
227+
'': 'foo',
228+
},
212229
},
213230
},
214231
},
215-
},
216-
{
217-
styles: {
218-
frogs: {
219-
legs: 'yellow',
220-
},
221-
typography: {
222-
fontSize: '1rem',
223-
},
224-
settings: {
225-
'': {
226-
'': 'bar',
232+
{
233+
styles: {
234+
frogs: {
235+
legs: 'yellow',
236+
},
237+
typography: {
238+
fontSize: '1rem',
239+
},
240+
settings: {
241+
'': {
242+
'': 'bar',
243+
},
227244
},
228245
},
229-
},
230-
}
231-
);
232-
expect( result ).toEqual( [] );
246+
}
247+
);
248+
expect( result ).toEqual( [] );
249+
} );
250+
} );
251+
252+
describe( 'getGlobalStylesChangelist()', () => {
253+
it( 'compares two objects and returns a cached list of changed keys', () => {
254+
const resultA = getGlobalStylesChangelist( next, previous );
255+
256+
expect( resultA ).toEqual( [
257+
[ 'styles', 'Colors' ],
258+
[ 'styles', 'Typography' ],
259+
[ 'blocks', 'Test pumpkin flowers' ],
260+
[ 'elements', 'H3' ],
261+
[ 'elements', 'Caption' ],
262+
[ 'elements', 'H6' ],
263+
[ 'elements', 'Link' ],
264+
[ 'settings', 'Color' ],
265+
[ 'settings', 'Typography' ],
266+
] );
267+
268+
const resultB = getGlobalStylesChangelist( next, previous );
269+
270+
expect( resultB ).toEqual( resultA );
271+
} );
233272
} );
234273
} );

0 commit comments

Comments
 (0)