Skip to content

Commit

Permalink
[Experimental widgets screen] Remove "id" attribute of the legacy-wid…
Browse files Browse the repository at this point in the history
…get block (WordPress#24817)

* Fix saving of duplicated legacy widgets

* Remove id attribute from legacy-widget block
  • Loading branch information
adamziel authored Aug 27, 2020
1 parent 25f9e09 commit 44e1bfa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
3 changes: 0 additions & 3 deletions packages/block-library/src/legacy-widget/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
"widgetClass": {
"type": "string"
},
"id": {
"type": "string"
},
"idBase": {
"type": "string"
},
Expand Down
17 changes: 11 additions & 6 deletions packages/block-library/src/legacy-widget/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ class LegacyWidgetEdit extends Component {
hasPermissionsToManageWidgets,
isSelected,
setAttributes,
widgetId,
} = this.props;
const { isPreview, hasEditForm } = this.state;
const { id, widgetClass } = attributes;
const { widgetClass } = attributes;
const widgetObject =
( id && availableLegacyWidgets[ id ] ) ||
( widgetId && availableLegacyWidgets[ widgetId ] ) ||
( widgetClass && availableLegacyWidgets[ widgetClass ] );

if ( ! id && ! widgetClass ) {
if ( ! widgetId && ! widgetClass ) {
return (
<LegacyWidgetPlaceholder
availableLegacyWidgets={ availableLegacyWidgets }
Expand Down Expand Up @@ -123,8 +124,8 @@ class LegacyWidgetEdit extends Component {
<LegacyWidgetEditHandler
isSelected={ isSelected }
isVisible={ ! isPreview }
id={ id }
idBase={ attributes.idBase || attributes.id }
id={ widgetId }
idBase={ attributes.idBase || widgetId }
number={ attributes.number }
widgetName={ get( widgetObject, [ 'name' ] ) }
widgetClass={ attributes.widgetClass }
Expand Down Expand Up @@ -180,7 +181,10 @@ class LegacyWidgetEdit extends Component {
}
}

export default withSelect( ( select ) => {
export default withSelect( ( select, { clientId } ) => {
const widgetId = select( 'core/edit-widgets' ).getWidgetIdForClientId(
clientId
);
const editorSettings = select( 'core/block-editor' ).getSettings();
const {
availableLegacyWidgets,
Expand All @@ -189,5 +193,6 @@ export default withSelect( ( select ) => {
return {
hasPermissionsToManageWidgets,
availableLegacyWidgets,
widgetId,
};
} )( LegacyWidgetEdit );
8 changes: 7 additions & 1 deletion packages/edit-widgets/src/store/selectors.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { keyBy } from 'lodash';
import { invert, keyBy } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -41,6 +41,12 @@ export const getWidgetAreas = createRegistrySelector( ( select ) => () => {
);
} );

export const getWidgetIdForClientId = ( state, clientId ) => {
const widgetIdToClientId = state.mapping;
const clientIdToWidgetId = invert( widgetIdToClientId );
return clientIdToWidgetId[ clientId ];
};

export const getEditedWidgetAreas = createRegistrySelector(
( select ) => ( state, ids ) => {
let widgetAreas = select( 'core/edit-widgets' ).getWidgetAreas();
Expand Down
3 changes: 0 additions & 3 deletions packages/edit-widgets/src/store/transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export function transformWidgetToBlock( widget ) {
{
rendered: widget.rendered,
form: widget.form,
id: widget.id,
widgetClass: widget.widget_class,
instance: widget.settings,
idBase: widget.id_base,
Expand All @@ -32,9 +31,7 @@ export function transformBlockToWidget( block, relatedWidget = {} ) {
if ( name === 'core/legacy-widget' ) {
const widget = {
...relatedWidget,
id: attributes.id,
widget_class: attributes.widgetClass,
number: attributes.number,
id_base: attributes.idBase,
settings: attributes.instance,
};
Expand Down

0 comments on commit 44e1bfa

Please sign in to comment.