-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Miguel Fonseca <[email protected]>
- Loading branch information
Showing
6 changed files
with
143 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { hasBlockSupport } from '@wordpress/blocks'; | ||
|
||
const hasSettingsSupport = ( blockType ) => | ||
hasBlockSupport( blockType, '__experimentalSettings', false ); | ||
|
||
function addAttribute( settings ) { | ||
if ( ! hasSettingsSupport( settings ) ) { | ||
return settings; | ||
} | ||
|
||
// Allow blocks to specify their own attribute definition with default values if needed. | ||
if ( ! settings?.attributes?.settings ) { | ||
settings.attributes = { | ||
...settings.attributes, | ||
settings: { | ||
type: 'object', | ||
}, | ||
}; | ||
} | ||
|
||
return settings; | ||
} | ||
|
||
addFilter( | ||
'blocks.registerBlockType', | ||
'core/settings/addAttribute', | ||
addAttribute | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { applyFilters } from '@wordpress/hooks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import '../settings'; | ||
|
||
describe( 'with settings', () => { | ||
const blockSettings = { | ||
save: () => <div className="default" />, | ||
category: 'text', | ||
title: 'block title', | ||
}; | ||
|
||
describe( 'addAttribute', () => { | ||
const addAttribute = applyFilters.bind( | ||
null, | ||
'blocks.registerBlockType' | ||
); | ||
|
||
it( 'does not have settings att if settings block support is not enabled', () => { | ||
const settings = addAttribute( { | ||
...blockSettings, | ||
supports: { | ||
__experimentalSettings: false, | ||
}, | ||
} ); | ||
|
||
expect( settings.attributes ).toBe( undefined ); | ||
} ); | ||
|
||
it( 'has settings att if settings block supports is enabled', () => { | ||
const settings = addAttribute( { | ||
...blockSettings, | ||
supports: { | ||
__experimentalSettings: true, | ||
}, | ||
} ); | ||
|
||
expect( settings.attributes ).toStrictEqual( { | ||
settings: { type: 'object' }, | ||
} ); | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters