-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Connnect gutenberg widgets screen to widget area endpoints (#15779)
* Connect gutenberg widget screen to widget-area endpoints. * Use saveEntityRecord. * Enhancements * Add test cases * Extracted a WidgetAreas component. * Improve the setup action to avoid the need for a parameter. * update test case * Refactor pass widget are by id, have a save action. * id instead of widgetAreaId + lint fix * Correct controls import
- Loading branch information
1 parent
27a6ead
commit f1bbc00
Showing
18 changed files
with
728 additions
and
29 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
27 changes: 27 additions & 0 deletions
27
packages/edit-widgets/src/components/edit-widgets-initializer/index.js
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,27 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { compose } from '@wordpress/compose'; | ||
import { useEffect } from '@wordpress/element'; | ||
import { withDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Layout from '../layout'; | ||
|
||
function EditWidgetsInitializer( { setupWidgetAreas } ) { | ||
useEffect( () => { | ||
setupWidgetAreas(); | ||
}, [] ); | ||
return <Layout />; | ||
} | ||
|
||
export default compose( [ | ||
withDispatch( ( dispatch ) => { | ||
const { setupWidgetAreas } = dispatch( 'core/edit-widgets' ); | ||
return { | ||
setupWidgetAreas, | ||
}; | ||
} ), | ||
] )( EditWidgetsInitializer ); |
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 |
---|---|---|
|
@@ -9,8 +9,3 @@ | |
margin-top: $header-height; | ||
} | ||
} | ||
|
||
.edit-widgets-layout__area { | ||
max-width: $content-width; | ||
margin: 0 auto 30px; | ||
} |
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,4 @@ | ||
.edit-widgets-widget-area { | ||
max-width: $content-width; | ||
margin: 0 auto 30px; | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/edit-widgets/src/components/widget-areas/index.js
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,30 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { compose } from '@wordpress/compose'; | ||
import { withSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import WidgetArea from '../widget-area'; | ||
|
||
function WidgetAreas( { areas } ) { | ||
return areas.map( ( { id }, index ) => ( | ||
<WidgetArea | ||
key={ id } | ||
id={ id } | ||
initialOpen={ index === 0 } | ||
/> | ||
) ); | ||
} | ||
|
||
export default compose( [ | ||
withSelect( ( select ) => { | ||
const { getWidgetAreas } = select( 'core/edit-widgets' ); | ||
const areas = getWidgetAreas(); | ||
return { | ||
areas, | ||
}; | ||
} ), | ||
] )( WidgetAreas ); |
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,76 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get, map } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { parse, serialize } from '@wordpress/blocks'; | ||
import { dispatch, select } from '@wordpress/data-controls'; | ||
|
||
/** | ||
* Yields an action object that setups the widget areas. | ||
* | ||
* @yields {Object} Action object. | ||
*/ | ||
export function* setupWidgetAreas() { | ||
const widgetAreas = yield select( | ||
'core', | ||
'getEntityRecords', | ||
'root', | ||
'widgetArea' | ||
); | ||
yield { | ||
type: 'SETUP_WIDGET_AREAS', | ||
widgetAreas: map( widgetAreas, ( { content, ...widgetAreaProperties } ) => { | ||
return { | ||
...widgetAreaProperties, | ||
blocks: parse( get( content, [ 'raw' ], '' ) ), | ||
}; | ||
} ), | ||
}; | ||
} | ||
|
||
/** | ||
* Returns an action object used to update the blocks in a specific widget area. | ||
* | ||
* @param {string} widgetAreaId Id of the widget area. | ||
* @param {Object[]} blocks Array of blocks that should be part of the widget area. | ||
* | ||
* @return {Object} Action object. | ||
*/ | ||
export function updateBlocksInWidgetArea( widgetAreaId, blocks = [] ) { | ||
return { | ||
type: 'UPDATE_BLOCKS_IN_WIDGET_AREA', | ||
widgetAreaId, | ||
blocks, | ||
}; | ||
} | ||
|
||
/** | ||
* Action that performs the logic to save widget areas. | ||
* | ||
* @yields {Object} Action object. | ||
*/ | ||
export function* saveWidgetAreas() { | ||
const widgetAreas = yield select( | ||
'core/edit-widgets', | ||
'getWidgetAreas', | ||
); | ||
for ( const { id } of widgetAreas ) { | ||
const blocks = yield select( | ||
'core/edit-widgets', | ||
'getBlocksFromWidgetArea', | ||
id | ||
); | ||
yield dispatch( | ||
'core', | ||
'saveWidgetArea', | ||
{ | ||
id, | ||
content: serialize( blocks ), | ||
} | ||
); | ||
} | ||
} |
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,6 @@ | ||
/** | ||
* Constant for the store module (or reducer) key. | ||
* @type {string} | ||
*/ | ||
export const STORE_KEY = 'core/edit-widgets'; | ||
|
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,23 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { registerStore } from '@wordpress/data'; | ||
import { controls } from '@wordpress/data-controls'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import reducer from './reducer'; | ||
import * as actions from './actions'; | ||
import * as selectors from './selectors'; | ||
|
||
const store = registerStore( 'core/edit-widgets', { | ||
reducer, | ||
actions, | ||
selectors, | ||
controls, | ||
} ); | ||
|
||
store.dispatch( { type: 'INIT' } ); | ||
|
||
export default store; |
Oops, something went wrong.