-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Editor: Defer block validation until after source application #16569
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: ! isNewPost } ); | ||
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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Edit: In reflection, this statement is totally wrong, and the reason it's set up the way it is is so that we can call |
||
validate( blocks ); | ||
} | ||
|
||
return { | ||
type: 'RESET_EDITOR_BLOCKS', | ||
blocks: yield* getBlocksWithSourcedAttributes( blocks ), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also test for validation failures?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's necessary. The purpose of these test cases is not to verify validation (which are a separate set of test cases), but to ensure the assignment of the validation result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense.