Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
blocks.findTransform(
[
{
type: 'block',
blocks: [],
priority: 1,
transform(atts) {
return blocks.createBlock('my/foo');
},
},
],
transform => transform.type === 'block'
);
declare const RAW_TRANSFORM_ARRAY: Array>;
blocks.findTransform(RAW_TRANSFORM_ARRAY, ({ isMatch }) => true);
// $ExpectType string
blocks.getBlockTransforms('to', 'my/foo')[0].blockName;
// $ExpectType string
blocks.getBlockTransforms<{ foo: string }>('to', 'my/foo')[0].blockName;
// $ExpectType Block>[]
blocks.getPossibleBlockTransformations([BLOCK_INSTANCE]);
// $ExpectType BlockInstance<{ [k: string]: any; }>[] | null
blocks.switchToBlockType(BLOCK_INSTANCE, 'core/paragraph');
// $ExpectType BlockInstance<{ [k: string]: any; }>[] | null
blocks.switchToBlockType([BLOCK_INSTANCE], 'core/paragraph');
function getOutboundType( blockName ) {
// Grab the first outbound transform
const transform = findTransform(
getBlockTransforms( 'to', blockName ),
( { type, blocks } ) => type === 'block' && blocks.length === 1 // What about when .length > 1?
);
if ( ! transform ) {
return null;
}
return getBlockType( transform.blocks[ 0 ] );
}
function getOutboundType( blockName ) {
// Grab the first outbound transform
const transform = findTransform(
getBlockTransforms( 'to', blockName ),
( { type, blocks } ) => type === 'block' && blocks.length === 1 // What about when .length > 1?
);
if ( ! transform ) {
return null;
}
return getBlockType( transform.blocks[ 0 ] );
}
if ( ! onReplace ) {
return;
}
const { start, text } = value;
const characterBefore = text.slice( start - 1, start );
// The character right before the caret must be a plain space.
if ( characterBefore !== ' ' ) {
return;
}
const trimmedTextBefore = text.slice( 0, start ).trim();
const prefixTransforms = getBlockTransforms( 'from' )
.filter( ( { type } ) => type === 'prefix' );
const transformation = findTransform( prefixTransforms, ( { prefix } ) => {
return trimmedTextBefore === prefix;
} );
if ( ! transformation ) {
return;
}
const content = valueToFormat( slice( value, start, text.length ) );
const block = transformation.transform( content );
onReplace( [ block ] );
__unstableMarkAutomaticChange();
}, [ onReplace, __unstableMarkAutomaticChange ] );
( record, onReplace, valueToFormat ) => {
if ( ! onReplace ) {
return record;
}
const { start } = record;
const text = getTextContent( record );
const characterBefore = text.slice( start - 1, start );
if ( ! /\s/.test( characterBefore ) ) {
return record;
}
const trimmedTextBefore = text.slice( 0, start ).trim();
const transformation = findTransform( prefixTransforms, ( { prefix } ) => {
return trimmedTextBefore === prefix;
} );
if ( ! transformation ) {
return record;
}
const content = valueToFormat( slice( record, start, text.length ) );
const block = transformation.transform( content );
onReplace( [ block ] );
return record;
},
( record ) => {
return ( record, onReplace ) => {
const text = getTextContent( record );
const transformation = findTransform( transforms, ( item ) => {
return item.regExp.test( text );
} );
if ( ! transformation ) {
return record;
}
onReplace( [
transformation.transform( { content: text } ),
] );
};
}
onFilesDrop( files, position ) {
const transformation = findTransform(
getBlockTransforms( 'from' ),
( transform ) => transform.type === 'files' && transform.isMatch( files )
);
if ( transformation ) {
const insertIndex = this.getInsertIndex( position );
const blocks = transformation.transform( files, this.props.updateBlockAttributes );
this.props.insertBlocks( blocks, insertIndex );
}
}
onFilesDrop( files, position ) {
if ( ! this.props.hasUploadPermissions ) {
return;
}
const transformation = findTransform(
getBlockTransforms( 'from' ),
( transform ) => transform.type === 'files' && transform.isMatch( files )
);
if ( transformation ) {
const insertIndex = this.getInsertIndex( position );
const blocks = transformation.transform( files, this.props.updateBlockAttributes );
this.props.insertBlocks( blocks, insertIndex );
}
}
onFilesDrop (files, position) {
const transformation = findTransform(
getBlockTransforms('from'),
transform => transform.type === 'files' && transform.isMatch(files)
);
if (transformation) {
const insertIndex = this.getInsertIndex(position);
const blocks = transformation.transform(files, this.props.updateBlockAttributes);
this.props.insertBlocks(blocks, insertIndex);
}
}