From c62934108b8e461e8bfcc98eb09e43ad139e4334 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Thu, 18 Mar 2021 16:29:17 +1100 Subject: [PATCH] Update Widgets screen --- .../edit-widgets/src/store/transformers.js | 97 +++++++++---------- 1 file changed, 45 insertions(+), 52 deletions(-) diff --git a/packages/edit-widgets/src/store/transformers.js b/packages/edit-widgets/src/store/transformers.js index ac1a40134c1f1..6f1f9f36e805d 100644 --- a/packages/edit-widgets/src/store/transformers.js +++ b/packages/edit-widgets/src/store/transformers.js @@ -3,7 +3,7 @@ */ import { createBlock, parse, serialize } from '@wordpress/blocks'; -function __experimentalAddWidgetIdToBlock( block, widgetId ) { +function addWidgetIdToBlock( block, widgetId ) { return { ...block, attributes: { @@ -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; }