How to use the @wordpress/blocks.findTransform function in @wordpress/blocks

To help you get started, we’ve selected a few @wordpress/blocks examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / wordpress__blocks / wordpress__blocks-tests.tsx View on Github external
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');
github Automattic / wp-calypso / client / gutenberg / editor / edit-post / hooks / validate-multiple-use / index.js View on Github external
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 ] );
}
github WordPress / gutenberg / edit-post / hooks / validate-use-once / index.js View on Github external
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 ] );
}
github WordPress / gutenberg / packages / block-editor / src / components / rich-text / index.js View on Github external
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 ] );
github WordPress / gutenberg / packages / block-editor / src / components / rich-text / patterns.js View on Github external
( 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 ) => {
github WordPress / gutenberg / packages / block-editor / src / components / rich-text / patterns.js View on Github external
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 } ),
		] );
	};
}
github WordPress / gutenberg / editor / components / block-drop-zone / index.js View on Github external
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 );
		}
	}
github WordPress / gutenberg / packages / block-editor / src / components / block-drop-zone / index.js View on Github external
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 );
		}
	}
github front / gutenberg-js / src / js / gutenberg-overrides / packages / editor / build-module / components / block-drop-zone / index.js View on Github external
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);
    }
  }