Skip to content

Commit

Permalink
Editor: Defer block validation until after source application
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jul 30, 2019
1 parent 272e660 commit 44d7e5c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,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_

Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/components/post-text-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } );
},
};
} ),
Expand Down
30 changes: 25 additions & 5 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
*
Expand Down Expand Up @@ -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';
Expand All @@ -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();
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 ),
Expand Down

0 comments on commit 44d7e5c

Please sign in to comment.