Widgets: Update preview for Gallery widget when one of its attachments is modified in the media modal, outside the customized state.

* Ensure that changes to captions are shown in preview when modified in media modal.
* Also keep `wp.customize.widgetsPreview.renderedWidgets` updated when widgets are added or removed.

See #41914, #37887, #40403.
Fixes #41979.


git-svn-id: https://develop.svn.wordpress.org/trunk@41872 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter 2017-10-16 06:45:09 +00:00
parent 75034a401f
commit 850db3c6a0
2 changed files with 35 additions and 0 deletions

View File

@ -100,6 +100,21 @@
control.selectedAttachments.on( 'change', control.renderPreview );
control.selectedAttachments.on( 'reset', control.renderPreview );
control.updateSelectedAttachments();
/*
* Refresh a Gallery widget partial when the user modifies one of the selected attachments.
* This ensures that when an attachment's caption is updated in the media modal the Gallery
* widget in the preview will then be refreshed to show the change. Normally doing this
* would not be necessary because all of the state should be contained inside the changeset,
* as everything done in the Customizer should not make a change to the site unless the
* changeset itself is published. Attachments are a current exception to this rule.
* For a proposal to include attachments in the customized state, see #37887.
*/
if ( wp.customize && wp.customize.previewer ) {
control.selectedAttachments.on( 'change', function() {
wp.customize.previewer.send( 'refresh-widget-partial', control.model.get( 'widget_id' ) );
} );
}
},
/**

View File

@ -39,6 +39,24 @@ wp.customize.widgetsPreview = wp.customize.WidgetCustomizerPreview = (function(
api.preview.bind( 'active', function() {
self.highlightControls();
} );
/*
* Refresh a partial when the controls pane requests it. This is used currently just by the
* Gallery widget so that when an attachment's caption is updated in the media modal,
* the widget in the preview will then be refreshed to show the change. Normally doing this
* would not be necessary because all of the state should be contained inside the changeset,
* as everything done in the Customizer should not make a change to the site unless the
* changeset itself is published. Attachments are a current exception to this rule.
* For a proposal to include attachments in the customized state, see #37887.
*/
api.preview.bind( 'refresh-widget-partial', function( widgetId ) {
var partialId = 'widget[' + widgetId + ']';
if ( api.selectiveRefresh.partial.has( partialId ) ) {
api.selectiveRefresh.partial( partialId ).refresh();
} else if ( self.renderedWidgets[ widgetId ] ) {
api.preview.send( 'refresh' ); // Fallback in case theme does not support 'customize-selective-refresh-widgets'.
}
} );
};
/**
@ -451,6 +469,7 @@ wp.customize.widgetsPreview = wp.customize.WidgetCustomizerPreview = (function(
}
} );
}
delete self.renderedWidgets[ removedWidgetId ];
} );
// Handle insertion of widgets.
@ -458,6 +477,7 @@ wp.customize.widgetsPreview = wp.customize.WidgetCustomizerPreview = (function(
_.each( widgetsAdded, function( addedWidgetId ) {
var widgetPartial = sidebarPartial.ensureWidgetPlacementContainers( addedWidgetId );
addedWidgetPartials.push( widgetPartial );
self.renderedWidgets[ addedWidgetId ] = true;
} );
_.each( addedWidgetPartials, function( widgetPartial ) {