-
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.
Connect gutenberg widget screen to widget-area endpoints.
- Loading branch information
1 parent
fab0281
commit 21b434e
Showing
12 changed files
with
364 additions
and
20 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
37 changes: 37 additions & 0 deletions
37
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,37 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { compose } from '@wordpress/compose'; | ||
import { useEffect } from '@wordpress/element'; | ||
import { withDispatch, withSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Layout from '../layout'; | ||
|
||
function EditWidgetsInitializer( { widgetAreas, setupWidgetAreas } ) { | ||
useEffect( () => { | ||
if ( ! widgetAreas ) { | ||
return; | ||
} | ||
console.log( widgetAreas ); | ||
setupWidgetAreas( widgetAreas ); | ||
}, [ widgetAreas, setupWidgetAreas ] ); | ||
return <Layout />; | ||
} | ||
|
||
export default compose( [ | ||
withSelect( ( select ) => { | ||
const widgetAreas = select( 'core' ).getEntityRecords( 'root', 'widgetArea' ); | ||
return { | ||
widgetAreas, | ||
}; | ||
} ), | ||
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
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,62 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { map } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
parse, | ||
serialize, | ||
} from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { STORE_KEY } from './constants'; | ||
import { | ||
select, | ||
apiFetch, | ||
} from './controls'; | ||
|
||
export function setupWidgetAreas( widgetAreas ) { | ||
return { | ||
type: 'SETUP_WIDGET_AREAS', | ||
widgetAreas: map( widgetAreas, ( area ) => { | ||
return { | ||
...area, | ||
blocks: parse( ( area.content && area.content.raw ) || '' ), | ||
}; | ||
} ), | ||
}; | ||
} | ||
|
||
export function updateBlocksInWidgetArea( widgetAreaId, blocks ) { | ||
return { | ||
type: 'UPDATE_BLOCKS_IN_WIDGET_AREA', | ||
widgetAreaId, | ||
blocks, | ||
}; | ||
} | ||
|
||
export function* saveWidgetAreas() { | ||
const widgetAreas = yield select( | ||
STORE_KEY, | ||
'getWidgetAreas' | ||
); | ||
for ( const widgetArea of widgetAreas ) { | ||
const widgetAreaBlocks = yield select( | ||
STORE_KEY, | ||
'getWidgetAreaBlocks', | ||
widgetArea.id | ||
); | ||
const content = serialize( widgetAreaBlocks ); | ||
const path = `/__experimental/widget-areas/${ widgetArea.id }`; | ||
yield apiFetch( { | ||
path, | ||
method: 'POST', | ||
data: { content }, | ||
} ); | ||
} | ||
} |
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,113 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import triggerFetch from '@wordpress/api-fetch'; | ||
import { createRegistryControl } from '@wordpress/data'; | ||
|
||
/** | ||
* Dispatches a control action for triggering an api fetch call. | ||
* | ||
* @param {Object} request Arguments for the fetch request. | ||
* | ||
* @return {Object} control descriptor. | ||
*/ | ||
export function apiFetch( request ) { | ||
return { | ||
type: 'API_FETCH', | ||
request, | ||
}; | ||
} | ||
|
||
/** | ||
* Dispatches a control action for triggering a registry select. | ||
* | ||
* @param {string} storeKey | ||
* @param {string} selectorName | ||
* @param {Array} args Arguments for the select. | ||
* | ||
* @return {Object} control descriptor. | ||
*/ | ||
export function select( storeKey, selectorName, ...args ) { | ||
return { | ||
type: 'SELECT', | ||
storeKey, | ||
selectorName, | ||
args, | ||
}; | ||
} | ||
|
||
/** | ||
* Dispatches a control action for triggering a registry select that has a | ||
* resolver. | ||
* | ||
* @param {string} storeKey | ||
* @param {string} selectorName | ||
* @param {Array} args Arguments for the select. | ||
* | ||
* @return {Object} control descriptor. | ||
*/ | ||
export function resolveSelect( storeKey, selectorName, ...args ) { | ||
return { | ||
type: 'RESOLVE_SELECT', | ||
storeKey, | ||
selectorName, | ||
args, | ||
}; | ||
} | ||
|
||
/** | ||
* Dispatches a control action for triggering a registry dispatch. | ||
* | ||
* @param {string} storeKey | ||
* @param {string} actionName | ||
* @param {Array} args Arguments for the dispatch action. | ||
* | ||
* @return {Object} control descriptor. | ||
*/ | ||
export function dispatch( storeKey, actionName, ...args ) { | ||
return { | ||
type: 'DISPATCH', | ||
storeKey, | ||
actionName, | ||
args, | ||
}; | ||
} | ||
|
||
export default { | ||
API_FETCH( { request } ) { | ||
return triggerFetch( request ); | ||
}, | ||
SELECT: createRegistryControl( | ||
( registry ) => ( { storeKey, selectorName, args } ) => { | ||
return registry.select( storeKey )[ selectorName ]( ...args ); | ||
} | ||
), | ||
DISPATCH: createRegistryControl( | ||
( registry ) => ( { storeKey, actionName, args } ) => { | ||
return registry.dispatch( storeKey )[ actionName ]( ...args ); | ||
} | ||
), | ||
RESOLVE_SELECT: createRegistryControl( | ||
( registry ) => ( { storeKey, selectorName, args } ) => { | ||
return new Promise( ( resolve ) => { | ||
const hasFinished = () => registry.select( 'core/data' ) | ||
.hasFinishedResolution( storeKey, selectorName, args ); | ||
const getResult = () => registry.select( storeKey )[ selectorName ] | ||
.apply( null, args ); | ||
|
||
// trigger the selector (to trigger the resolver) | ||
const result = getResult(); | ||
if ( hasFinished() ) { | ||
return resolve( result ); | ||
} | ||
|
||
const unsubscribe = registry.subscribe( () => { | ||
if ( hasFinished() ) { | ||
unsubscribe(); | ||
resolve( getResult() ); | ||
} | ||
} ); | ||
} ); | ||
} | ||
), | ||
}; |
Oops, something went wrong.