Skip to content

Commit

Permalink
Remove context usage; Pass only small subset of settings from the ser…
Browse files Browse the repository at this point in the history
…ver. (+1 squashed commit)

Squashed commits:
[46e57de] Add blockEditor settings on the widget screen; Required to make legacy widgets work.
  • Loading branch information
jorgefilipecosta committed May 23, 2019
1 parent e91dc28 commit bb20459
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 14 deletions.
31 changes: 30 additions & 1 deletion lib/widgets-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,38 @@ function gutenberg_widgets_init( $hook ) {
return;
}

// Media settings.
$max_upload_size = wp_max_upload_size();
if ( ! $max_upload_size ) {
$max_upload_size = 0;
}

$color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
$font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) );

$settings = array_merge(
array(
'disableCustomColors' => get_theme_support( 'disable-custom-colors' ),
'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
'maxUploadFileSize' => $max_upload_size,
),
gutenberg_get_legacy_widget_settings()
);

if ( false !== $color_palette ) {
$settings['colors'] = $color_palette;
}

if ( false !== $font_sizes ) {
$settings['fontSizes'] = $font_sizes;
}

wp_add_inline_script(
'wp-edit-widgets',
'wp.editWidgets.initialize( "widgets-editor" );'
sprintf(
'wp.editWidgets.initialize( "widgets-editor", %s );',
wp_json_encode( $settings )
)
);
// Preload server-registered block schemas.
wp_add_inline_script(
Expand Down
21 changes: 16 additions & 5 deletions lib/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ function gutenberg_block_editor_admin_footer() {
}
add_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' );


/**
* Extends default editor settings with values supporting legacy widgets.
*
* @param array $settings Default editor settings.
* Returns the settings required by legacy widgets blocks.
*
* @return array Filtered editor settings.
* @return array Legacy widget settings.
*/
function gutenberg_legacy_widget_settings( $settings ) {
function gutenberg_get_legacy_widget_settings() {
$settings = array();
/**
* TODO: The hardcoded array should be replaced with a mechanism to allow
* core and third party blocks to specify they already have equivalent
Expand Down Expand Up @@ -125,6 +125,17 @@ function gutenberg_legacy_widget_settings( $settings ) {

return $settings;
}

/**
* Extends default editor settings with values supporting legacy widgets.
*
* @param array $settings Default editor settings.
*
* @return array Filtered editor settings.
*/
function gutenberg_legacy_widget_settings( $settings ) {
return array_merge( $settings, gutenberg_get_legacy_widget_settings() );
}
add_filter( 'block_editor_settings', 'gutenberg_legacy_widget_settings' );

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import { withDispatch } from '@wordpress/data';
*/
import Layout from '../layout';

function EditWidgetsInitializer( { setupWidgetAreas } ) {
function EditWidgetsInitializer( { setupWidgetAreas, settings } ) {
useEffect( () => {
setupWidgetAreas();
}, [] );
return <Layout />;
return (
<Layout
blockEditorSettings={ settings }
/>
);
}

export default compose( [
Expand Down
6 changes: 4 additions & 2 deletions packages/edit-widgets/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Header from '../header';
import Sidebar from '../sidebar';
import WidgetAreas from '../widget-areas';

function Layout() {
function Layout( { blockEditorSettings } ) {
return (
<>
<Header />
Expand All @@ -22,7 +22,9 @@ function Layout() {
aria-label={ __( 'Widgets screen content' ) }
tabIndex="-1"
>
<WidgetAreas />
<WidgetAreas
blockEditorSettings={ blockEditorSettings }
/>
</div>
</>
);
Expand Down
2 changes: 2 additions & 0 deletions packages/edit-widgets/src/components/widget-area/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { withDispatch, withSelect } from '@wordpress/data';

function WidgetArea( {
blockEditorSettings,
blocks,
initialOpen,
updateBlocks,
Expand All @@ -25,6 +26,7 @@ function WidgetArea( {
value={ blocks }
onInput={ updateBlocks }
onChange={ updateBlocks }
settings={ blockEditorSettings }
>
<BlockList />
</BlockEditorProvider>
Expand Down
3 changes: 2 additions & 1 deletion packages/edit-widgets/src/components/widget-areas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { withSelect } from '@wordpress/data';
*/
import WidgetArea from '../widget-area';

function WidgetAreas( { areas } ) {
function WidgetAreas( { areas, blockEditorSettings } ) {
return areas.map( ( { id }, index ) => (
<WidgetArea
blockEditorSettings={ blockEditorSettings }
key={ id }
id={ id }
initialOpen={ index === 0 }
Expand Down
9 changes: 6 additions & 3 deletions packages/edit-widgets/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import EditWidgetsInitializer from './components/edit-widgets-initializer';
/**
* Initilizes the widgets screen
*
* @param {string} id Id of the root element to render the screen.
* @param {string} id Id of the root element to render the screen.
* @param {Object} settings Id of the root element to render the screen.
*/
export function initialize( id ) {
export function initialize( id, settings ) {
registerCoreBlocks();
render(
<EditWidgetsInitializer />,
<EditWidgetsInitializer
settings={ settings }
/>,
document.getElementById( id )
);
}

0 comments on commit bb20459

Please sign in to comment.