Skip to content

Commit

Permalink
Lodash: Remove from blocks API raw handling (#43575)
Browse files Browse the repository at this point in the history
* Lodash: Remove from blocks API raw handling

* Simplify key in obj expressions
  • Loading branch information
tyxla authored Aug 24, 2022
1 parent ec588b4 commit e3e79e1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { has } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -25,7 +20,7 @@ function isFigureContent( node, schema ) {
return false;
}

return has( schema, [ 'figure', 'children', tag ] );
return tag in ( schema?.figure?.children ?? {} );
}

/**
Expand All @@ -39,7 +34,7 @@ function isFigureContent( node, schema ) {
function canHaveAnchor( node, schema ) {
const tag = node.nodeName.toLowerCase();

return has( schema, [ 'figure', 'children', 'a', 'children', tag ] );
return tag in ( schema?.figure?.children?.a?.children ?? {} );
}

/**
Expand Down
13 changes: 4 additions & 9 deletions packages/blocks/src/api/raw-handling/get-raw-transforms.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
/**
* External dependencies
*/
import { filter } from 'lodash';

/**
* Internal dependencies
*/
import { getBlockTransforms } from '../factory';

export function getRawTransforms() {
return filter( getBlockTransforms( 'from' ), { type: 'raw' } ).map(
( transform ) => {
return getBlockTransforms( 'from' )
.filter( ( { type } ) => type === 'raw' )
.map( ( transform ) => {
return transform.isMatch
? transform
: {
Expand All @@ -19,6 +15,5 @@ export function getRawTransforms() {
transform.selector &&
node.matches( transform.selector ),
};
}
);
} );
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { includes } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -33,7 +28,7 @@ export default function phrasingContentReducer( node, doc ) {
// fallback.
if (
textDecorationLine === 'line-through' ||
includes( textDecoration, 'line-through' )
textDecoration.includes( 'line-through' )
) {
wrap( doc.createElement( 's' ), node );
}
Expand Down
35 changes: 17 additions & 18 deletions packages/blocks/src/api/raw-handling/shortcode-converter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { some, castArray, find, mapValues, pickBy, includes } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -16,6 +11,9 @@ import { getBlockType } from '../registration';
import { getBlockAttributes } from '../parser/get-block-attributes';
import { applyBuiltInValidationFixes } from '../parser/apply-built-in-validation-fixes';

const castArray = ( maybeArray ) =>
Array.isArray( maybeArray ) ? maybeArray : [ maybeArray ];

function segmentHTMLToShortcodeBlock(
HTML,
lastIndex = 0,
Expand All @@ -29,7 +27,7 @@ function segmentHTMLToShortcodeBlock(
( transform ) =>
excludedBlockNames.indexOf( transform.blockName ) === -1 &&
transform.type === 'shortcode' &&
some( castArray( transform.tag ), ( tag ) =>
castArray( transform.tag ).some( ( tag ) =>
regexp( tag ).test( HTML )
)
);
Expand All @@ -39,7 +37,7 @@ function segmentHTMLToShortcodeBlock(
}

const transformTags = castArray( transformation.tag );
const transformTag = find( transformTags, ( tag ) =>
const transformTag = transformTags.find( ( tag ) =>
regexp( tag ).test( HTML )
);

Expand All @@ -56,7 +54,7 @@ function segmentHTMLToShortcodeBlock(
// consider the shortcode as inline text, and thus skip conversion for
// this segment.
if (
! includes( match.shortcode.content || '', '<' ) &&
! match.shortcode.content?.includes( '<' ) &&
! (
/(\n|<p>)\s*$/.test( beforeHTML ) &&
/^\s*(\n|<\/p>)/.test( afterHTML )
Expand Down Expand Up @@ -102,16 +100,17 @@ function segmentHTMLToShortcodeBlock(
);
} );
} else {
const attributes = mapValues(
pickBy(
transformation.attributes,
( schema ) => schema.shortcode
),
// Passing all of `match` as second argument is intentionally broad
// but shouldn't be too relied upon.
//
// See: https://github.com/WordPress/gutenberg/pull/3610#discussion_r152546926
( schema ) => schema.shortcode( match.shortcode.attrs, match )
const attributes = Object.fromEntries(
Object.entries( transformation.attributes )
.filter( ( [ , schema ] ) => schema.shortcode )
// Passing all of `match` as second argument is intentionally broad
// but shouldn't be too relied upon.
//
// See: https://github.com/WordPress/gutenberg/pull/3610#discussion_r152546926
.map( ( [ key, schema ] ) => [
key,
schema.shortcode( match.shortcode.attrs, match ),
] )
);

const blockType = getBlockType( transformation.blockName );
Expand Down

0 comments on commit e3e79e1

Please sign in to comment.