Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gutenberg: Update selected editor when switching to classic (Calypso) #32421

Merged
merged 2 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 9 additions & 23 deletions apps/wpcom-block-editor/src/common/switch-to-classic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global _currentSiteId, wpcomGutenberg */
/* global wpcomGutenberg */

/**
* External dependencies
Expand All @@ -15,28 +15,14 @@ function addSwitchToClassicButton() {
setTimeout( () => {
$( '.edit-post-more-menu__content .components-menu-group:last-child > div[role=menu]' )
.append( `
<button type="button" aria-label="${ wpcomGutenberg.switchToClassic.label }" role="menuitem"
class="components-button components-menu-item__button components-menu-item__button-switch">
${ wpcomGutenberg.switchToClassic.label }
</button>
` );
$( '.components-menu-item__button-switch' ).on( 'click', () => {
$.wpcom_proxy_request( {
method: 'POST',
path: `/sites/${ _currentSiteId }/gutenberg`,
apiNamespace: 'wpcom/v2',
query: {
platform: 'web',
editor: 'classic',
},
} ).done( () => {
if ( wpcomGutenberg.isCalypsoify ) {
top.window.location.replace( wpcomGutenberg.switchToClassic.url );
} else {
top.window.location.reload();
}
} );
} );
<a
href="${ wpcomGutenberg.switchToClassic.url }" target="_top" role="menuitem"
aria-label="${ wpcomGutenberg.switchToClassic.label }"
class="components-button components-menu-item__button"
>
${ wpcomGutenberg.switchToClassic.label }
</a>
` );
}, 0 );
} );
}
Expand Down
22 changes: 16 additions & 6 deletions client/post-editor/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import isCalypsoifyGutenbergEnabled from 'state/selectors/is-calypsoify-gutenber
import isGutenlypsoEnabled from 'state/selectors/is-gutenlypso-enabled';
import isSiteAtomic from 'state/selectors/is-site-automated-transfer';
import getEditorUrl from 'state/selectors/get-editor-url';
import { requestSelectedEditor } from 'state/selected-editor/actions';
import { requestSelectedEditor, setSelectedEditor } from 'state/selected-editor/actions';

function getPostID( context ) {
if ( ! context.params.post || 'new' === context.params.post ) {
Expand Down Expand Up @@ -174,6 +174,21 @@ async function redirectIfBlockEditor( context, next ) {
const state = context.store.getState();
const siteId = getSelectedSiteId( state );

// URLs with a set-editor=<editorName> param are used for indicating that the user wants to use always the given
// editor, so we update the selected editor for the current user/site pair.
const newEditorChoice = get( context.query, 'set-editor' );
const oldEditorChoice = getSelectedEditor( state, siteId );
const allowedEditors = [ 'classic', 'gutenberg' ];

if ( allowedEditors.indexOf( newEditorChoice ) !== -1 && newEditorChoice !== oldEditorChoice ) {
context.store.dispatch( setSelectedEditor( siteId, newEditorChoice ) );
}

// If the new editor is classic, we bypass the selected editor check.
if ( 'classic' === newEditorChoice ) {
return next();
}

if (
! isCalypsoifyGutenbergEnabled( state, siteId ) &&
! isGutenlypsoEnabled( state, siteId ) &&
Expand Down Expand Up @@ -330,11 +345,6 @@ export default {
next();
}

// Bypass the selected editor check if the URL contains a force=true param
if ( get( context.query, 'force', false ) ) {
return next();
}

redirectIfBlockEditor( context, next );
},
};