Skip to content

Commit

Permalink
Merge pull request #5107 from ampproject/update/non-amp-customize-link
Browse files Browse the repository at this point in the history
Keep non-AMP Customizer link referencing the currently-previewed URL
  • Loading branch information
westonruter authored Jul 27, 2020
2 parents 5085d11 + 1c47a0f commit 2b2add5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@
"navigator": false,
"sessionStorage": false
}
},
{
"files": [
"assets/src/customizer/amp-customize-controls.js"
],
"globals": {
"HTMLAnchorElement": false
}
}
]
}
24 changes: 24 additions & 0 deletions assets/src/customizer/amp-customize-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ window.ampCustomizeControls = ( function( api, $ ) {
'use strict';

const component = {
nonAmpCustomizerLink: null,
data: {
queryVar: '',
l10n: {
Expand All @@ -29,6 +30,7 @@ window.ampCustomizeControls = ( function( api, $ ) {
component.extendRootDescription();

$.ajaxPrefilter( component.injectAmpIntoAjaxRequests );
api.bind( 'ready', component.updateNonAmpCustomizeLink );
api.bind( 'ready', component.forceAmpPreviewUrl );
api.bind( 'ready', component.addOptionSettingNotices );
api.bind( 'ready', component.addNavMenuPanelNotice );
Expand All @@ -40,6 +42,28 @@ window.ampCustomizeControls = ( function( api, $ ) {
component.updatePreviewNotice = function updatePreviewNotice() {
const previewNotice = $( '#customize-info .preview-notice' );
previewNotice.html( component.data.l10n.ampVersionNotice ); // Contents have been sanitized with wp_kses_post().
component.nonAmpCustomizerLink = previewNotice.find( 'a[href]' )[ 0 ];
};

/**
* Make sure the non-AMP Customizer link keeps referencing to the currently-previewed URL.
*/
component.updateNonAmpCustomizeLink = function updateNonAmpCustomizeLink() {
if ( ! ( component.nonAmpCustomizerLink instanceof HTMLAnchorElement ) ) {
return;
}

const update = () => {
const previewUrl = new URL( api.previewer.previewUrl() );
previewUrl.searchParams.delete( component.data.queryVar );

const customizeUrl = new URL( component.nonAmpCustomizerLink.href );
customizeUrl.searchParams.set( 'url', previewUrl );
component.nonAmpCustomizerLink.href = customizeUrl.href;
};

update();
api.previewer.previewUrl.bind( update );
};

/**
Expand Down

0 comments on commit 2b2add5

Please sign in to comment.