From b16b36402fc547ed9b44209727ab12e3f9371d7a Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 12 Jul 2019 14:58:37 -0400 Subject: [PATCH] Editor: Defer block validation until after source application --- .../developers/data/data-core-editor.md | 2 +- .../src/components/post-text-editor/index.js | 4 +-- packages/editor/src/store/actions.js | 30 +++++++++++++++---- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/docs/designers-developers/developers/data/data-core-editor.md b/docs/designers-developers/developers/data/data-core-editor.md index e3456d65168dbd..3e1f0780850df4 100644 --- a/docs/designers-developers/developers/data/data-core-editor.md +++ b/docs/designers-developers/developers/data/data-core-editor.md @@ -1209,7 +1209,7 @@ Returns an action object used to signal that the blocks have been updated. _Parameters_ - _blocks_ `Array`: Block Array. -- _options_ `?Object`: Optional options. +- _options_ `?WPEditorResetEditorBlocksActionOptions`: Optional options. _Returns_ diff --git a/packages/editor/src/components/post-text-editor/index.js b/packages/editor/src/components/post-text-editor/index.js index 667aaa76fcd700..861758eb95a78a 100644 --- a/packages/editor/src/components/post-text-editor/index.js +++ b/packages/editor/src/components/post-text-editor/index.js @@ -99,8 +99,8 @@ export default compose( [ editPost( { content } ); }, onPersist( content ) { - const blocks = parse( content ); - resetEditorBlocks( blocks ); + const blocks = parse( content, { validate: false } ); + resetEditorBlocks( blocks, { validate: true } ); }, }; } ), diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 8ea2c9a408de84..e3b719836f6f76 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -11,6 +11,7 @@ import deprecated from '@wordpress/deprecated'; import { dispatch, select, apiFetch } from '@wordpress/data-controls'; import { parse, + validate, synchronizeBlocksWithTemplate, } from '@wordpress/blocks'; import isShallowEqual from '@wordpress/is-shallow-equal'; @@ -36,6 +37,17 @@ import { import { awaitNextStateChange, getRegistry } from './controls'; import * as sources from './block-sources'; +/** + * Default values for `resetEditorBlocks` action creator options. + * + * @typedef {Object} WPEditorResetEditorBlocksActionOptions + * + * @property {boolean} validate Whether to run validator over provided blocks. + */ +const DEFAULT_RESET_EDITOR_BLOCKS_OPTIONS = { + validate: false, +}; + /** * Map of Registry instance to WeakMap of dependencies by custom source. * @@ -167,7 +179,7 @@ export function* setupEditor( post, edits, template ) { content = post.content.raw; } - let blocks = parse( content ); + let blocks = parse( content, { validate: false } ); // Apply a template for new posts only, if exists. const isNewPost = post.status === 'auto-draft'; @@ -183,7 +195,7 @@ export function* setupEditor( post, edits, template ) { edits, template, }; - yield resetEditorBlocks( blocks ); + yield resetEditorBlocks( blocks, { validate: true } ); yield setupEditorState( post ); yield* __experimentalSubscribeSources(); } @@ -901,12 +913,16 @@ export function unlockPostSaving( lockName ) { /** * Returns an action object used to signal that the blocks have been updated. * - * @param {Array} blocks Block Array. - * @param {?Object} options Optional options. + * @param {Array} blocks Block Array. + * @param {?WPEditorResetEditorBlocksActionOptions} options Optional options. * * @return {Object} Action object */ -export function* resetEditorBlocks( blocks, options = {} ) { +export function* resetEditorBlocks( blocks, options = DEFAULT_RESET_EDITOR_BLOCKS_OPTIONS ) { + if ( options !== DEFAULT_RESET_EDITOR_BLOCKS_OPTIONS ) { + options = { ...DEFAULT_RESET_EDITOR_BLOCKS_OPTIONS, ...options }; + } + const lastBlockAttributesChange = yield select( 'core/block-editor', '__experimentalGetLastBlockAttributeChanges' ); // Sync to sources from block attributes updates. @@ -943,6 +959,10 @@ export function* resetEditorBlocks( blocks, options = {} ) { yield* resetLastBlockSourceDependencies( Array.from( updatedSources ) ); } + if ( options.validate ) { + validate( blocks ); + } + return { type: 'RESET_EDITOR_BLOCKS', blocks: yield* getBlocksWithSourcedAttributes( blocks ),