-
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
Add widget id to blocks in the widgets screen #28379
Changes from 9 commits
2a8caaa
3bbe9f1
f016988
f47bcd4
d55232d
de47502
38aedd0
28c7d14
46003d8
a7c6f56
7bc9850
d01fbcb
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
import { v4 as uuid } from 'uuid'; | ||
import { | ||
every, | ||
reduce, | ||
castArray, | ||
findIndex, | ||
isObjectLike, | ||
|
@@ -31,7 +30,7 @@ import { | |
getBlockTypes, | ||
getGroupingBlockName, | ||
} from './registration'; | ||
import { normalizeBlockType } from './utils'; | ||
import { normalizeBlockType, sanitizeBlockAttributes } from './utils'; | ||
|
||
/** | ||
* Returns a block object given its type and attributes. | ||
|
@@ -43,40 +42,7 @@ import { normalizeBlockType } from './utils'; | |
* @return {Object} Block object. | ||
*/ | ||
export function createBlock( name, attributes = {}, innerBlocks = [] ) { | ||
// Get the type definition associated with a registered block. | ||
const blockType = getBlockType( name ); | ||
|
||
if ( undefined === blockType ) { | ||
throw new Error( `Block type '${ name }' is not registered.` ); | ||
} | ||
|
||
// Ensure attributes contains only values defined by block type, and merge | ||
// default values for missing attributes. | ||
const sanitizedAttributes = reduce( | ||
blockType.attributes, | ||
( accumulator, schema, key ) => { | ||
const value = attributes[ key ]; | ||
|
||
if ( undefined !== value ) { | ||
accumulator[ key ] = value; | ||
} else if ( schema.hasOwnProperty( 'default' ) ) { | ||
accumulator[ key ] = schema.default; | ||
} | ||
|
||
if ( [ 'node', 'children' ].indexOf( schema.source ) !== -1 ) { | ||
// Ensure value passed is always an array, which we're expecting in | ||
// the RichText component to handle the deprecated value. | ||
if ( typeof accumulator[ key ] === 'string' ) { | ||
accumulator[ key ] = [ accumulator[ key ] ]; | ||
} else if ( ! Array.isArray( accumulator[ key ] ) ) { | ||
accumulator[ key ] = []; | ||
} | ||
} | ||
|
||
return accumulator; | ||
}, | ||
{} | ||
); | ||
const sanitizedAttributes = sanitizeBlockAttributes( name, attributes ); | ||
|
||
const clientId = uuid(); | ||
|
||
|
@@ -134,13 +100,15 @@ export function createBlocksFromInnerBlocksTemplate( | |
export function cloneBlock( block, mergeAttributes = {}, newInnerBlocks ) { | ||
const clientId = uuid(); | ||
|
||
const sanitizedAttributes = sanitizeBlockAttributes( block.name, { | ||
...block.attributes, | ||
...mergeAttributes, | ||
} ); | ||
|
||
return { | ||
...block, | ||
clientId, | ||
attributes: { | ||
...block.attributes, | ||
...mergeAttributes, | ||
}, | ||
attributes: sanitizedAttributes, | ||
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. Could you please add a new unit test for 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. Good idea! |
||
innerBlocks: | ||
newInnerBlocks || | ||
block.innerBlocks.map( ( innerBlock ) => cloneBlock( innerBlock ) ), | ||
|
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.
The addition of sanitize could potentially have a small performance impact (not sure if noticeable), the other thing is that it was not an expectation of cloneBock and I hope it doesn't produce unexpected side effects
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.
Does running
npm run test-performance
with and without this PR demonstrate any performance regression?