Skip to content

Commit

Permalink
Widgets: Fix creating and editing non-multi widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Jun 28, 2021
1 parent 96a8781 commit 3059852
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
9 changes: 6 additions & 3 deletions packages/edit-widgets/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,15 @@ export function* saveWidgetArea( widgetAreaId ) {
const widgetId = getWidgetIdFromBlock( block );
const oldWidget = widgets[ widgetId ];
const widget = transformBlockToWidget( block, oldWidget );

// We'll replace the null widgetId after save, but we track it here
// since order is important.
sidebarWidgetsIds.push( widgetId );

// We need to check for the id in the widget object here, because a deleted
// and restored widget won't have this id.
if ( widget.id ) {
// Check oldWidget as widgetId might refer to an ID which has been
// deleted, e.g. if a deleted block is restored via undo after saving.
if ( oldWidget ) {
// Update an existing widget.
yield dispatch(
'core',
'editEntityRecord',
Expand Down Expand Up @@ -184,6 +186,7 @@ export function* saveWidgetArea( widgetAreaId ) {
saveEditedEntityRecord( 'root', 'widget', widgetId )
);
} else {
// Create a new widget.
batchTasks.push( ( { saveEntityRecord } ) =>
saveEntityRecord( 'root', 'widget', {
...widget,
Expand Down
17 changes: 16 additions & 1 deletion packages/widgets/src/blocks/legacy-widget/edit/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ export default class Control {
// a fake but unique number.
this.number = ++lastNumber;

this.handleFormChange = debounce( this.saveForm.bind( this ), 200 );
this.handleFormChange = debounce(
this.handleFormChange.bind( this ),
200
);
this.handleFormSubmit = this.handleFormSubmit.bind( this );

this.initDOM();
Expand Down Expand Up @@ -214,6 +217,18 @@ export default class Control {
}
}

/**
* Perform a save when a multi widget's form is changed. Non-multi widgets
* are saved manually.
*
* @access private
*/
handleFormChange() {
if ( this.idBase ) {
this.saveForm();
}
}

/**
* Perform a save when the control's form is manually submitted.
*
Expand Down
3 changes: 2 additions & 1 deletion packages/widgets/src/blocks/legacy-widget/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ function NotEmpty( {
);
}

const mode = isNavigationMode || ! isSelected ? 'preview' : 'edit';
const mode =
idBase && ( isNavigationMode || ! isSelected ) ? 'preview' : 'edit';

return (
<>
Expand Down

0 comments on commit 3059852

Please sign in to comment.