From 1c47a0f8365682208bea8f3d3f13cd163cc4447d Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 26 Jul 2020 23:18:22 -0700 Subject: [PATCH] Keep non-AMP Customizer link referencing the currently-previewed URL --- .eslintrc | 8 +++++++ .../src/customizer/amp-customize-controls.js | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/.eslintrc b/.eslintrc index 0c4eb6bfaaf..9ca57237473 100644 --- a/.eslintrc +++ b/.eslintrc @@ -115,6 +115,14 @@ "navigator": false, "sessionStorage": false } + }, + { + "files": [ + "assets/src/customizer/amp-customize-controls.js" + ], + "globals": { + "HTMLAnchorElement": false + } } ] } diff --git a/assets/src/customizer/amp-customize-controls.js b/assets/src/customizer/amp-customize-controls.js index 23f5b91fa8e..c499a9329a5 100644 --- a/assets/src/customizer/amp-customize-controls.js +++ b/assets/src/customizer/amp-customize-controls.js @@ -4,6 +4,7 @@ window.ampCustomizeControls = ( function( api, $ ) { 'use strict'; const component = { + nonAmpCustomizerLink: null, data: { queryVar: '', l10n: { @@ -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 ); @@ -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 ); }; /**