Skip to content
Open
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
27 changes: 22 additions & 5 deletions packages/block-library/src/separator/transforms.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
/**
* WordPress dependencies
*/
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
import {
createBlock,
getBlockType,
getDefaultBlockName,
} from '@wordpress/blocks';

const transforms = {
from: [
{
type: 'input',
regExp: /^-{3,}$/,
transform: () => [
createBlock( 'core/separator' ),
createBlock( getDefaultBlockName() ),
],
transform: () => {
// Check for default variation to preserve attributes.
const blockType = getBlockType( 'core/separator' );
Comment on lines +16 to +17
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the getBlockVariations method here. See packages/blocks/src/api/registration.js.

const defaultVariation = blockType?.variations?.find(
( variation ) => variation.isDefault
);

// Fall back to empty attributes if no default variation is found.
const attributes = defaultVariation
? defaultVariation.attributes
: {};

return [
createBlock( 'core/separator', attributes ),
createBlock( getDefaultBlockName() ),
];
},
},
{
type: 'raw',
Expand Down
Loading