-
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
Quote v2: update how attributes are registered #39729
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,18 +3,14 @@ | |
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { quote as icon } from '@wordpress/icons'; | ||
import { addFilter } from '@wordpress/hooks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
import edit from './edit'; | ||
import save from './save'; | ||
|
||
const { name } = metadata; | ||
|
||
export { metadata, name }; | ||
|
||
const settings = { | ||
icon, | ||
example: { | ||
|
@@ -35,3 +31,46 @@ const settings = { | |
}; | ||
|
||
export default settings; | ||
|
||
/** | ||
* This function updates the attributes for the quote v2. | ||
* This should be moved to block.json when v2 becomes the default. | ||
* | ||
* @param {Array} blockSettings The settings of the block to be registered. | ||
* @param {string} blockName The name of the block to be registered. | ||
* @return {Array} New settings. | ||
*/ | ||
function registerQuoteV2Attributes( blockSettings, blockName ) { | ||
if ( ! window?.__experimentalEnableQuoteBlockV2 ) { | ||
return blockSettings; | ||
} | ||
|
||
if ( blockName !== 'core/quote' ) { | ||
return blockSettings; | ||
} | ||
|
||
// Register the new attribute. | ||
Object.assign( blockSettings.attributes, { | ||
attribution: { | ||
type: 'string', | ||
source: 'html', | ||
selector: 'figcaption', | ||
default: '', | ||
__experimentalRole: 'content', | ||
}, | ||
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. What would be the issue if we use the same block.json file for both versions. I mean aside having all the old attributes in the version, which doesn't bother me much specially since we need to keep them to implement the "migration effect" behavior. 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. It was the other way around I was concerned about: I didn't want to pollute the existing quote block with the v2 attributes. Technically, it might work in this case, as we're not modifying existing atts but adding new ones. I can't think of anything that would break. Though I still not a fan of this approach, I rather keep things separated. 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. Ok, let's keep them separated for now, it doesn't hurt either. |
||
} ); | ||
|
||
// Deregister the old ones. | ||
delete blockSettings.attributes.value; | ||
delete blockSettings.attributes.citation; | ||
|
||
return blockSettings; | ||
} | ||
|
||
// Importing this file includes side effects. | ||
// This has been declared in block-library/package.json under sideEffects. | ||
addFilter( | ||
'blocks.registerBlockType', | ||
'core/quote/v2-attributes', | ||
registerQuoteV2Attributes | ||
); |
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.
Curious about this change, how do we decide to add a file here.
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.
My understanding is that we use this to signal webpack not to remove things that aren't part of the export in those files. In theory, it should remove the
addFilter
call. In practice, though, I tested without adding this and things worked as expected. Not sure if there's some webpack config missing, but wanted to play it safe and avoid the situation in which the v2 block stops working because of some webpack config changed.