Skip to content

Commit

Permalink
Move publish sidebar logic to core/editor store
Browse files Browse the repository at this point in the history
This logic is used by the PostPublishPanel, which lives in editor.
We don't want editor to depend on anything from edit-post.

An alternative approach could be to keep the logic in edit-post
and use the the PrePublish and PostPublish extensions.
In my view, those are meant for allowing plugins to inject their
own panels into the PublishPanel. Dismissing the panel is a core
action and using the extensions to circumvent the editor/edit-post
separation would be a convoluted way to add support for dismissing the panel.
  • Loading branch information
oandregal committed Sep 11, 2018
1 parent 9351e3f commit 28262d2
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 67 deletions.
2 changes: 1 addition & 1 deletion edit-post/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default compose(
withSelect( ( select ) => ( {
isEditorSidebarOpened: select( 'core/edit-post' ).isEditorSidebarOpened(),
isPublishSidebarOpened: select( 'core/edit-post' ).isPublishSidebarOpened(),
isPublishSidebarEnabled: select( 'core/edit-post' ).isPublishSidebarEnabled(),
isPublishSidebarEnabled: select( 'core/editor' ).isPublishSidebarEnabled(),
hasActiveMetaboxes: select( 'core/edit-post' ).hasMetaBoxes(),
isSaving: select( 'core/edit-post' ).isSavingMetaBoxes(),
hasBlockSelection: !! select( 'core/editor' ).getBlockSelectionStart(),
Expand Down
4 changes: 2 additions & 2 deletions edit-post/components/header/publish-sidebar-toggle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ const PublishSidebarToggle = function( { onToggle, isEnabled } ) {

export default compose( [
withSelect( ( select ) => ( {
isEnabled: select( 'core/edit-post' ).isPublishSidebarEnabled(),
isEnabled: select( 'core/editor' ).isPublishSidebarEnabled(),
} ) ),
withDispatch( ( dispatch, ownProps ) => ( {
onToggle() {
const { disablePublishSidebar, enablePublishSidebar } = dispatch( 'core/edit-post' );
const { disablePublishSidebar, enablePublishSidebar } = dispatch( 'core/editor' );
if ( ownProps.isEnabled ) {
disablePublishSidebar();
} else {
Expand Down
22 changes: 0 additions & 22 deletions edit-post/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,6 @@ export function togglePublishSidebar() {
};
}

/**
* Returns an action object used in signalling that the user has enabled the publish sidebar.
*
* @return {Object} Action object
*/
export function enablePublishSidebar() {
return {
type: 'ENABLE_PUBLISH_SIDEBAR',
};
}

/**
* Returns an action object used in signalling that the user has disabled the publish sidebar.
*
* @return {Object} Action object
*/
export function disablePublishSidebar() {
return {
type: 'DISABLE_PUBLISH_SIDEBAR',
};
}

/**
* Returns an action object used in signalling that use toggled a panel in the editor.
*
Expand Down
9 changes: 0 additions & 9 deletions edit-post/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,6 @@ export const preferences = combineReducers( {

return state;
},
isPublishSidebarEnabled( state = true, action ) {
switch ( action.type ) {
case 'ENABLE_PUBLISH_SIDEBAR':
case 'DISABLE_PUBLISH_SIDEBAR':
return action.type === 'ENABLE_PUBLISH_SIDEBAR';
}

return state;
},
panels( state = PREFERENCES_DEFAULTS.panels, action ) {
if ( action.type === 'TOGGLE_GENERAL_SIDEBAR_EDITOR_PANEL' ) {
return {
Expand Down
12 changes: 0 additions & 12 deletions edit-post/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@ export function getEditorMode( state ) {
return getPreference( state, 'editorMode', 'visual' );
}

/**
* Returns whether the pre-publish panel show be shown
* or hidden on publish.
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether the pre-publish panel should be shown or not.
*/
export function isPublishSidebarEnabled( state ) {
return getPreference( state, 'isPublishSidebarEnabled', true );
}

/**
* Returns true if the editor sidebar is opened.
*
Expand Down
19 changes: 0 additions & 19 deletions edit-post/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ describe( 'state', () => {
expect( state ).toEqual( {
editorMode: 'visual',
isGeneralSidebarDismissed: false,
isPublishSidebarEnabled: true,
panels: { 'post-status': true },
features: { fixedToolbar: false },
pinnedPluginItems: {},
Expand Down Expand Up @@ -52,24 +51,6 @@ describe( 'state', () => {
expect( state.isGeneralSidebarDismissed ).toBe( true );
} );

it( 'should disable the publish sidebar', () => {
const original = deepFreeze( preferences( undefined, { } ) );
const state = preferences( original, {
type: 'DISABLE_PUBLISH_SIDEBAR',
} );

expect( state.isPublishSidebarEnabled ).toBe( false );
} );

it( 'should enable the publish sidebar', () => {
const original = deepFreeze( preferences( { isPublishSidebarEnabled: false }, { } ) );
const state = preferences( original, {
type: 'ENABLE_PUBLISH_SIDEBAR',
} );

expect( state.isPublishSidebarEnabled ).toBe( true );
} );

it( 'should set the sidebar panel open flag to true if unset', () => {
const state = preferences( deepFreeze( { panels: {} } ), {
type: 'TOGGLE_GENERAL_SIDEBAR_EDITOR_PANEL',
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/components/post-publish-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default compose( [
isSavingPost,
isEditedPostDirty,
} = select( 'core/editor' );
const { isPublishSidebarEnabled } = select( 'core/edit-post' );
const { isPublishSidebarEnabled } = select( 'core/editor' );
return {
postType: getCurrentPostType(),
hasPublishAction: get( getCurrentPost(), [ '_links', 'wp:action-publish' ], false ),
Expand All @@ -129,7 +129,7 @@ export default compose( [
};
} ),
withDispatch( ( dispatch, { isPublishSidebarEnabled } ) => {
const { disablePublishSidebar, enablePublishSidebar } = dispatch( 'core/edit-post' );
const { disablePublishSidebar, enablePublishSidebar } = dispatch( 'core/editor' );
return {
onTogglePublishSidebar: ( ) => {
if ( isPublishSidebarEnabled ) {
Expand Down
22 changes: 22 additions & 0 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,3 +763,25 @@ export function unregisterToken( name ) {
name,
};
}

/**
* Returns an action object used in signalling that the user has enabled the publish sidebar.
*
* @return {Object} Action object
*/
export function enablePublishSidebar() {
return {
type: 'ENABLE_PUBLISH_SIDEBAR',
};
}

/**
* Returns an action object used in signalling that the user has disabled the publish sidebar.
*
* @return {Object} Action object
*/
export function disablePublishSidebar() {
return {
type: 'DISABLE_PUBLISH_SIDEBAR',
};
}
1 change: 1 addition & 0 deletions packages/editor/src/store/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { __ } from '@wordpress/i18n';

export const PREFERENCES_DEFAULTS = {
insertUsage: {},
isPublishSidebarEnabled: true,
};

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,13 @@ export function preferences( state = PREFERENCES_DEFAULTS, action ) {
...state,
insertUsage: omitBy( state.insertUsage, ( { insert } ) => insert.ref === action.id ),
};

case 'ENABLE_PUBLISH_SIDEBAR':
case 'DISABLE_PUBLISH_SIDEBAR':
return {
...state,
isPublishSidebarEnabled: action.type === 'ENABLE_PUBLISH_SIDEBAR',
};
}

return state;
Expand Down
12 changes: 12 additions & 0 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1879,3 +1879,15 @@ export function getTokenSettings( state, name ) {
export function canUserUseUnfilteredHTML( state ) {
return has( getCurrentPost( state ), [ '_links', 'wp:action-unfiltered_html' ] );
}

/**
* Returns whether the pre-publish panel should be shown
* or hidden on publish.
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether the pre-publish panel should be shown or not.
*/
export function isPublishSidebarEnabled( state ) {
return state.preferences.isPublishSidebarEnabled;
}
19 changes: 19 additions & 0 deletions packages/editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1580,9 +1580,28 @@ describe( 'state', () => {

expect( state ).toEqual( {
insertUsage: {},
isPublishSidebarEnabled: true,
} );
} );

it( 'should disable the publish sidebar', () => {
const original = deepFreeze( preferences( undefined, { } ) );
const state = preferences( original, {
type: 'DISABLE_PUBLISH_SIDEBAR',
} );

expect( state.isPublishSidebarEnabled ).toBe( false );
} );

it( 'should enable the publish sidebar', () => {
const original = deepFreeze( preferences( { isPublishSidebarEnabled: false }, { } ) );
const state = preferences( original, {
type: 'ENABLE_PUBLISH_SIDEBAR',
} );

expect( state.isPublishSidebarEnabled ).toBe( true );
} );

it( 'should record recently used blocks', () => {
const state = preferences( deepFreeze( { insertUsage: {} } ), {
type: 'INSERT_BLOCKS',
Expand Down

0 comments on commit 28262d2

Please sign in to comment.