-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Query block]: Add Posts List variation (#26990)
* [Query block]: Add Posts List variation * fix typo Co-authored-by: Greg Ziółkowski <[email protected]> * add variation description * expose __experimentalGetMatchingVariation * augment Query Placeholder - try to match a block variation * fix import Co-authored-by: Greg Ziółkowski <[email protected]>
- Loading branch information
1 parent
33fec4c
commit 63bfdcb
Showing
7 changed files
with
148 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 0 additions & 75 deletions
75
packages/block-editor/src/components/block-variation-transforms/test/index.js
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
packages/block-editor/src/utils/block-variation-transforms.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { isMatch } from 'lodash'; | ||
|
||
/** @typedef {import('@wordpress/blocks').WPBlockVariation} WPBlockVariation */ | ||
|
||
/** | ||
* Matches the provided block variations with a block's attributes. If no match | ||
* or more than one matches are found it returns `undefined`. If a single match is | ||
* found it returns it. | ||
* | ||
* This is a simple implementation for now as it takes into account only the attributes | ||
* of a block variation and not `InnerBlocks`. | ||
* | ||
* @param {Object} blockAttributes - The block attributes to try to find a match. | ||
* @param {WPBlockVariation[]} variations - A list of block variations to test for a match. | ||
* @return {?WPBlockVariation} - If a match is found returns it. If not or more than one matches are found returns `undefined`. | ||
*/ | ||
export const __experimentalGetMatchingVariation = ( | ||
blockAttributes, | ||
variations | ||
) => { | ||
if ( ! variations || ! blockAttributes ) return; | ||
const matches = variations.filter( ( { attributes } ) => { | ||
if ( ! attributes || ! Object.keys( attributes ).length ) return false; | ||
return isMatch( blockAttributes, attributes ); | ||
} ); | ||
if ( matches.length !== 1 ) return; | ||
return matches[ 0 ]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as transformStyles } from './transform-styles'; | ||
export * from './theme'; | ||
export * from './block-variation-transforms'; |
70 changes: 70 additions & 0 deletions
70
packages/block-editor/src/utils/test/block-variation-transforms.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { __experimentalGetMatchingVariation as getMatchingVariation } from '../block-variation-transforms'; | ||
|
||
describe( 'getMatchingVariation', () => { | ||
describe( 'should not find a match', () => { | ||
it( 'when no variations or attributes passed', () => { | ||
expect( | ||
getMatchingVariation( null, { content: 'hi' } ) | ||
).toBeUndefined(); | ||
expect( getMatchingVariation( {} ) ).toBeUndefined(); | ||
} ); | ||
it( 'when no variation matched', () => { | ||
const variations = [ | ||
{ name: 'one', attributes: { level: 1 } }, | ||
{ name: 'two', attributes: { level: 2 } }, | ||
]; | ||
expect( | ||
getMatchingVariation( { level: 4 }, variations ) | ||
).toBeUndefined(); | ||
} ); | ||
it( 'when more than one match found', () => { | ||
const variations = [ | ||
{ name: 'one', attributes: { level: 1 } }, | ||
{ name: 'two', attributes: { level: 1, content: 'hi' } }, | ||
]; | ||
expect( | ||
getMatchingVariation( | ||
{ level: 1, content: 'hi', other: 'prop' }, | ||
variations | ||
) | ||
).toBeUndefined(); | ||
} ); | ||
it( 'when variation is a superset of attributes', () => { | ||
const variations = [ | ||
{ name: 'one', attributes: { level: 1, content: 'hi' } }, | ||
]; | ||
expect( | ||
getMatchingVariation( { level: 1, other: 'prop' }, variations ) | ||
).toBeUndefined(); | ||
} ); | ||
} ); | ||
describe( 'should find a match', () => { | ||
it( 'when variation has one attribute', () => { | ||
const variations = [ | ||
{ name: 'one', attributes: { level: 1 } }, | ||
{ name: 'two', attributes: { level: 2 } }, | ||
]; | ||
expect( | ||
getMatchingVariation( | ||
{ level: 2, content: 'hi', other: 'prop' }, | ||
variations | ||
).name | ||
).toEqual( 'two' ); | ||
} ); | ||
it( 'when variation has many attributes', () => { | ||
const variations = [ | ||
{ name: 'one', attributes: { level: 1, content: 'hi' } }, | ||
{ name: 'two', attributes: { level: 2 } }, | ||
]; | ||
expect( | ||
getMatchingVariation( | ||
{ level: 1, content: 'hi', other: 'prop' }, | ||
variations | ||
).name | ||
).toEqual( 'one' ); | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters