How to use the @wordpress/blocks.parse 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 WordPress / gutenberg / packages / editor / src / store / actions.js View on Github external
export function* setupEditor( post, edits, template ) {
	// In order to ensure maximum of a single parse during setup, edits are
	// included as part of editor setup action. Assume edited content as
	// canonical if provided, falling back to post.
	let content;
	if ( has( edits, [ 'content' ] ) ) {
		content = edits.content;
	} else {
		content = post.content.raw;
	}

	let blocks = parse( content );

	// Apply a template for new posts only, if exists.
	const isNewPost = post.status === 'auto-draft';
	if ( isNewPost && template ) {
		blocks = synchronizeBlocksWithTemplate( blocks, template );
	}

	yield resetPost( post );
	yield {
		type: 'SETUP_EDITOR',
		post,
		edits,
		template,
	};
	yield resetEditorBlocks( blocks, { __unstableShouldCreateUndoLevel: false } );
	yield setupEditorState( post );
github wordpress-mobile / gutenberg-mobile / src / store / reducers / index.js View on Github external
refresh: ! state.refresh,
			};
		}
		case ActionTypes.BLOCK.CREATE: {
			// TODO we need to set focused: true and search for the currently focused block and
			// set that one to `focused: false`.
			const index = insertBlock( blocks, action.block, action.clientIdAbove );
			state.listAnimator.splice( index, 0, action.block );
			return {
				...state,
				blocks: blocks,
				refresh: ! state.refresh,
			};
		}
		case ActionTypes.BLOCK.PARSE: {
			const parsed = parse( action.html );
			return {
				...state,
				blocks: parsed,
				listAnimator: newListAnimator( parsed ),
				refresh: ! state.refresh,
			};
		}
		default:
			return state;
	}
};
github WordPress / gutenberg / src / app / AppContainer.js View on Github external
constructor( props: PropsType ) {
		super( props );

		const post = props.post || {
			id: 1,
			content: {
				raw: '',// props.initialHtml,
			},
			type: 'draft',
		};

		this.props.setupEditor( post );
		this.lastHtml = serialize( parse( props.initialHtml ) );
	}
github WordPress / gutenberg / editor / store / effects.js View on Github external
SETUP_EDITOR( action ) {
		const { post, settings } = action;

		// Parse content as blocks
		let blocks;
		let isValidTemplate = true;
		if ( post.content.raw ) {
			blocks = parse( post.content.raw );

			// Unlocked templates are considered always valid because they act as default values only.
			isValidTemplate = (
				! settings.template ||
				settings.templateLock !== 'all' ||
				doBlocksMatchTemplate( blocks, settings.template )
			);
		} else if ( settings.template ) {
			blocks = synchronizeBlocksWithTemplate( [], settings.template );
		} else if ( getDefaultBlockForPostFormat( post.format ) ) {
			blocks = [ createBlock( getDefaultBlockForPostFormat( post.format ) ) ];
		} else {
			blocks = [];
		}

		// Include auto draft title in edits while not flagging post as dirty
github WordPress / gutenberg / editor / store / effects.js View on Github external
( sharedBlock ) => ( {
						sharedBlock,
						parsedBlock: parse( sharedBlock.content )[ 0 ],
					} )
				) ) );
github WordPress / gutenberg / packages / editor / src / components / provider / index.native.js View on Github external
updateHtmlAction( html ) {
		const parsed = parse( html );
		this.props.resetEditorBlocksWithoutUndoLevel( parsed );
	}
github WordPress / gutenberg / src / app / AppContainer.js View on Github external
updateHtmlAction( html: string = '' ) {
		const parsed = parse( html );
		this.props.resetEditorBlocksWithoutUndoLevel( parsed );
	}
github WordPress / gutenberg / packages / block-library / src / post-content / edit.js View on Github external
const initialBlocks = useMemo( () => {
		const parsedContent = parse( content );
		return parsedContent.length ? parsedContent : undefined;
	}, [] );
	const [ blocks = initialBlocks, setBlocks ] = useEntityProp(
github wordpress-mobile / gutenberg-mobile / src / app / AppContainer.js View on Github external
updateHtmlAction( html: string = '' ) {
		const parsed = parse( html );
		this.props.resetEditorBlocksWithoutUndoLevel( parsed );
	}
github Automattic / wp-calypso / apps / wpcom-block-editor / src / calypso / iframe-bridge-server.js View on Github external
function onLoadRevision( message ) {
		const action = get( message, 'data.action', '' );
		const payload = get( message, 'data.payload', null );

		if ( action === 'loadRevision' && payload ) {
			const blocks = parse( payload.content );
			dispatch( 'core/editor' ).editPost( payload );
			dispatch( 'core/editor' ).resetBlocks( blocks );
			dispatch( 'core/notices' ).removeNotice( 'autosave-exists' );

			calypsoPort.removeEventListener( 'message', onLoadRevision, false );
		}
	}
}