Skip to content

Commit bf7951e

Browse files
committed
Create a new Gutenberg store keeping track of the selectedSiteId changes
1 parent 3d3e7c9 commit bf7951e

File tree

6 files changed

+56
-7
lines changed

6 files changed

+56
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const setSelectedSiteId = selectedSiteId => ( {
2+
type: 'GUTENLYPSO_SELECTED_SITE_ID_SET',
3+
selectedSiteId,
4+
} );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* WordPress Dependencies
3+
*/
4+
import { registerStore } from '@wordpress/data';
5+
6+
/**
7+
* Internal dependencies
8+
*/
9+
import reducer from './reducer';
10+
import * as actions from './actions';
11+
import * as selectors from './selectors';
12+
13+
const store = registerStore( 'gutenberg/calypso', {
14+
reducer,
15+
actions,
16+
selectors,
17+
} );
18+
19+
export default store;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* eslint-disable wpcalypso/import-docblock */
2+
/**
3+
* WordPress dependencies
4+
*/
5+
import { combineReducers } from '@wordpress/data';
6+
7+
const selectedSiteId = ( state = null, action ) =>
8+
'GUTENLYPSO_SELECTED_SITE_ID_SET' === action.type ? action.selectedSiteId : state;
9+
10+
export default combineReducers( { selectedSiteId } );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const getSelectedSiteId = state => state.selectedSiteId;

client/gutenberg/editor/controller.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { has, set, uniqueId } from 'lodash';
1212
* WordPress dependencies
1313
*/
1414
import { setLocaleData } from '@wordpress/i18n';
15-
import { dispatch } from '@wordpress/data';
15+
import { dispatch, select } from '@wordpress/data';
1616

1717
/**
1818
* Internal dependencies
@@ -110,6 +110,22 @@ export const loadGutenbergBlockAvailability = store => {
110110
} );
111111
};
112112

113+
export const resetGutenbergState = ( registry, selectedSiteId ) => {
114+
// Always reset core/editor, core/notices, and other UI parts
115+
registry.reset( 'core/editor' );
116+
registry.reset( 'core/notices' );
117+
dispatch( 'core/edit-post' ).closePublishSidebar();
118+
dispatch( 'core/edit-post' ).closeModal();
119+
120+
// Only reset core/data on site change
121+
const previousGutenbergSiteId = select( 'gutenberg/calypso' ).getSelectedSiteId();
122+
123+
if ( !! previousGutenbergSiteId && previousGutenbergSiteId !== selectedSiteId ) {
124+
registry.reset( 'core/data' );
125+
}
126+
dispatch( 'gutenberg/calypso' ).setSelectedSiteId( selectedSiteId );
127+
};
128+
113129
export const redirect = ( { store: { getState } }, next ) => {
114130
const state = getState();
115131
const siteId = getSelectedSiteId( state );
@@ -140,11 +156,7 @@ export const post = async ( context, next ) => {
140156

141157
const { Editor, registry } = initGutenberg( userId, context.store );
142158

143-
// Reset the Gutenberg state
144-
registry.reset( 'core/editor' );
145-
registry.reset( 'core/notices' );
146-
dispatch( 'core/edit-post' ).closePublishSidebar();
147-
dispatch( 'core/edit-post' ).closeModal();
159+
resetGutenbergState( registry, siteId );
148160

149161
return props => (
150162
<Editor { ...{ siteId, postId, postType, uniqueDraftKey, isDemoContent, ...props } } />

client/gutenberg/editor/init.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@ const addResetToRegistry = registry => {
6161
export const initGutenberg = once( ( userId, store ) => {
6262
debug( 'Starting Gutenberg editor initialization...' );
6363

64+
const registry = use( addResetToRegistry );
65+
6466
debug( 'Registering data plugins' );
6567
const storageKey = 'WP_DATA_USER_' + userId;
6668
use( plugins.persistence, { storageKey: storageKey } );
6769
use( plugins.controls );
6870

69-
const registry = use( addResetToRegistry );
71+
debug( 'Initializing gutenberg/calypso store' );
72+
require( 'gutenberg/editor/calypso-store' );
7073

7174
// We need to ensure that core-data is loaded after the data plugins have been registered.
7275
debug( 'Initializing core-data store' );

0 commit comments

Comments
 (0)