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

Blocks: remove pipe usage and dependency on compose #62127

Merged
merged 2 commits into from
May 30, 2024
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
2 changes: 0 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"@wordpress/autop": "file:../autop",
"@wordpress/blob": "file:../blob",
"@wordpress/block-serialization-default-parser": "file:../block-serialization-default-parser",
"@wordpress/compose": "file:../compose",
"@wordpress/data": "file:../data",
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/dom": "file:../dom",
Expand Down
35 changes: 8 additions & 27 deletions packages/blocks/src/api/parser/get-block-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import memoize from 'memize';
/**
* WordPress dependencies
*/
import { pipe } from '@wordpress/compose';
import { applyFilters } from '@wordpress/hooks';
import { RichTextData } from '@wordpress/rich-text';

Expand Down Expand Up @@ -37,24 +36,8 @@ import { normalizeBlockType, getDefault } from '../utils';
*
* @return {Function} Enhanced hpq matcher.
*/
export const toBooleanAttributeMatcher = ( matcher ) =>
pipe( [
matcher,
// Expected values from `attr( 'disabled' )`:
//
// <input>
// - Value: `undefined`
// - Transformed: `false`
//
// <input disabled>
// - Value: `''`
// - Transformed: `true`
//
// <input disabled="disabled">
// - Value: `'disabled'`
// - Transformed: `true`
( value ) => value !== undefined,
] );
export const toBooleanAttributeMatcher = ( matcher ) => ( value ) =>
matcher( value ) !== undefined;

/**
* Returns true if value is of the given JSON schema type, or false otherwise.
Expand Down Expand Up @@ -214,13 +197,13 @@ export function isValidByEnum( value, enumSet ) {
*/
export const matcherFromSource = memoize( ( sourceConfig ) => {
switch ( sourceConfig.source ) {
case 'attribute':
case 'attribute': {
let matcher = attr( sourceConfig.selector, sourceConfig.attribute );
if ( sourceConfig.type === 'boolean' ) {
matcher = toBooleanAttributeMatcher( matcher );
}

return matcher;
}
case 'html':
return html( sourceConfig.selector, sourceConfig.multiline );
case 'text':
Expand All @@ -244,12 +227,10 @@ export const matcherFromSource = memoize( ( sourceConfig ) => {
)
);
return query( sourceConfig.selector, subMatchers );
case 'tag':
return pipe( [
prop( sourceConfig.selector, 'nodeName' ),
( nodeName ) =>
nodeName ? nodeName.toLowerCase() : undefined,
] );
case 'tag': {
const matcher = prop( sourceConfig.selector, 'nodeName' );
return ( domNode ) => matcher( domNode )?.toLowerCase();
}
default:
// eslint-disable-next-line no-console
console.error( `Unknown source type "${ sourceConfig.source }"` );
Expand Down
37 changes: 15 additions & 22 deletions packages/blocks/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import removeAccents from 'remove-accents';
/**
* WordPress dependencies
*/
import { pipe } from '@wordpress/compose';
import { createSelector } from '@wordpress/data';

/**
Expand Down Expand Up @@ -645,6 +644,18 @@ export function hasBlockSupport( state, nameOrType, feature, defaultSupports ) {
return !! getBlockSupport( state, nameOrType, feature, defaultSupports );
}

/**
* Normalizes a search term string: removes accents, converts to lowercase, removes extra whitespace.
*
* @param {string|null|undefined} term Search term to normalize.
* @return {string} Normalized search term.
*/
function getNormalizedSearchTerm( term ) {
return removeAccents( term ?? '' )
.toLowerCase()
.trim();
}

/**
* Returns true if the block type by the given name or object value matches a
* search term, or false otherwise.
Expand Down Expand Up @@ -684,30 +695,12 @@ export function hasBlockSupport( state, nameOrType, feature, defaultSupports ) {
*
* @return {Object[]} Whether block type matches search term.
*/
export function isMatchingSearchTerm( state, nameOrType, searchTerm ) {
export function isMatchingSearchTerm( state, nameOrType, searchTerm = '' ) {
const blockType = getNormalizedBlockType( state, nameOrType );

const getNormalizedSearchTerm = pipe( [
// Disregard diacritics.
// Input: "média"
( term ) => removeAccents( term ?? '' ),

// Lowercase.
// Input: "MEDIA"
( term ) => term.toLowerCase(),

// Strip leading and trailing whitespace.
// Input: " media "
( term ) => term.trim(),
] );

const normalizedSearchTerm = getNormalizedSearchTerm( searchTerm );

const isSearchMatch = pipe( [
getNormalizedSearchTerm,
( normalizedCandidate ) =>
normalizedCandidate.includes( normalizedSearchTerm ),
] );
const isSearchMatch = ( candidate ) =>
getNormalizedSearchTerm( candidate ).includes( normalizedSearchTerm );

return (
isSearchMatch( blockType.title ) ||
Expand Down
Loading