Skip to content

Commit

Permalink
Update Widgets screen
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Mar 18, 2021
1 parent ee8507d commit c629341
Showing 1 changed file with 45 additions and 52 deletions.
97 changes: 45 additions & 52 deletions packages/edit-widgets/src/store/transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { createBlock, parse, serialize } from '@wordpress/blocks';

function __experimentalAddWidgetIdToBlock( block, widgetId ) {
function addWidgetIdToBlock( block, widgetId ) {
return {
...block,
attributes: {
Expand All @@ -14,75 +14,68 @@ function __experimentalAddWidgetIdToBlock( block, widgetId ) {
}

export function transformWidgetToBlock( widget ) {
if ( widget.widget_class === 'WP_Widget_Block' ) {
const parsedBlocks = parse( widget.settings.content );
if ( widget.id_base === 'block' ) {
const parsedBlocks = parse( widget.instance.raw.content );
if ( ! parsedBlocks.length ) {
return __experimentalAddWidgetIdToBlock(
return addWidgetIdToBlock(
createBlock( 'core/paragraph', {}, [] ),
widget.id
);
}
return __experimentalAddWidgetIdToBlock( parsedBlocks[ 0 ], widget.id );
return addWidgetIdToBlock( parsedBlocks[ 0 ], widget.id );
}

const attributes = {
name: widget.name,
form: widget.form,
instance: widget.settings,
idBase: widget.id_base,
number: widget.number,
};

const isReferenceWidget = ! widget.widget_class;
if ( isReferenceWidget ) {
attributes.referenceWidgetName = widget.id;
let attributes;
// TODO: Don't use widget_class.
if ( widget.widget_class ) {
attributes = {
idBase: widget.id_base,
instance: widget.instance,
};
} else {
attributes.widgetClass = widget.widget_class;
attributes = {
id: widget.id,
};
}

return __experimentalAddWidgetIdToBlock(
return addWidgetIdToBlock(
createBlock( 'core/legacy-widget', attributes, [] ),
widget.id
);
}

export function transformBlockToWidget( block, relatedWidget = {} ) {
const { name } = block;
if ( name === 'core/legacy-widget' ) {
let widget;
if ( block.attributes.referenceWidgetName ) {
widget = transformReferenceBlockToWidget( block, relatedWidget );
} else {
widget = transformClassBlockToWidget( block, relatedWidget );
}
delete widget.form;
delete widget.rendered;
return widget;
let widget;

if ( block.name === 'core/legacy-widget' ) {
widget = {
...relatedWidget,
id: block.attributes.id ?? relatedWidget.id,
id_base: block.attributes.idBase ?? relatedWidget.id_base,
instance: block.attributes.instance ?? relatedWidget.instance,
};
} else {
widget = {
...relatedWidget,
id_base: 'block',
instance: {
raw: {
content: serialize( block ),
},
},
};
}

return {
...relatedWidget,
id_base: 'block',
widget_class: 'WP_Widget_Block',
settings: {
content: serialize( block ),
},
};
}
// Delete deprecated properties.
delete widget.description;
delete widget.name;
delete widget.number;
delete widget.settings;
delete widget.widget_class;

function transformClassBlockToWidget( { attributes }, relatedWidget ) {
return {
...relatedWidget,
widget_class: attributes.widgetClass,
id_base: attributes.idBase,
settings: attributes.instance,
};
}
// Delete read-only properties.
delete widget.rendered;
delete widget.rendered_form;

function transformReferenceBlockToWidget( { attributes }, relatedWidget ) {
return {
...relatedWidget,
id: attributes.referenceWidgetName,
settings: attributes.instance,
};
return widget;
}

0 comments on commit c629341

Please sign in to comment.