diff --git a/editor/reducer.js b/editor/reducer.js index 6a8431fbe0265..6e59e9c26021c 100644 --- a/editor/reducer.js +++ b/editor/reducer.js @@ -15,7 +15,14 @@ import { getBlockTypes } from '@wordpress/blocks'; */ import { combineUndoableReducers } from './utils/undoable-reducer'; -const isMobile = window.innerWidth < 782; +/** + * Module constants + */ +const DEFAULT_PREFERENCES = { + mode: 'visual', + isSidebarOpened: window.innerWidth >= 782, + panels: { 'post-status': true }, +}; /** * Returns a post attribute value, flattening nested rendered content using its @@ -426,22 +433,16 @@ export function showInsertionPoint( state = false, action ) { } /** - * Reducer returning current editor mode, either "visual" or "text". + * Reducer returning the user preferences: * - * @param {string} state Current state - * @param {Object} action Dispatched action - * @return {string} Updated state + * @param {Object} state Current state + * @param {string} state.mode Current editor mode, either "visual" or "text". + * @param {Boolean} state.isSidebarOpened Whether the sidebar is opened or closed + * @param {Object} state.panels The state of the different sidebar panels + * @param {Object} action Dispatched action + * @return {string} Updated state */ -export function mode( state = 'visual', action ) { - switch ( action.type ) { - case 'SWITCH_MODE': - return action.mode; - } - - return state; -} - -export function preferences( state = { isSidebarOpened: ! isMobile, panels: { 'post-status': true } }, action ) { +export function preferences( state = DEFAULT_PREFERENCES, action ) { switch ( action.type ) { case 'TOGGLE_SIDEBAR': return { @@ -456,6 +457,11 @@ export function preferences( state = { isSidebarOpened: ! isMobile, panels: { 'p [ action.panel ]: ! get( state, [ 'panels', action.panel ], false ), }, }; + case 'SWITCH_MODE': + return { + ...state, + mode: action.mode, + }; } return state; @@ -530,7 +536,6 @@ export default optimist( combineReducers( { blockSelection, hoveredBlock, showInsertionPoint, - mode, preferences, panel, saving, diff --git a/editor/selectors.js b/editor/selectors.js index 5286bc8aa4e73..f8faf62cc27b0 100644 --- a/editor/selectors.js +++ b/editor/selectors.js @@ -19,7 +19,7 @@ import { addQueryArgs } from '@wordpress/url'; * @return {String} Editing mode */ export function getEditorMode( state ) { - return state.mode; + return getPreference( state, 'mode', 'visual' ); } /** @@ -46,11 +46,13 @@ export function getPreferences( state ) { * * @param {Object} state Global application state * @param {String} preferenceKey Preference Key + * @param {Mixed} defaultValue Default Value * @return {Mixed} Preference Value */ -export function getPreference( state, preferenceKey ) { +export function getPreference( state, preferenceKey, defaultValue ) { const preferences = getPreferences( state ); - return preferences[ preferenceKey ]; + const value = preferences[ preferenceKey ]; + return value === undefined ? defaultValue : value; } /** diff --git a/editor/sidebar/discussion-panel/index.js b/editor/sidebar/discussion-panel/index.js index 96e766c1377ac..b42a76b648d07 100644 --- a/editor/sidebar/discussion-panel/index.js +++ b/editor/sidebar/discussion-panel/index.js @@ -20,10 +20,9 @@ import { editPost, toggleSidebarPanel } from '../../actions'; */ const PANEL_NAME = 'discussion-panel'; -function DiscussionPanel( { pingStatus = 'open', commentStatus = 'open', instanceId, isOpened, ...props } ) { +function DiscussionPanel( { pingStatus = 'open', commentStatus = 'open', instanceId, isOpened, onTogglePanel, ...props } ) { const onTogglePingback = () => props.editPost( { ping_status: pingStatus === 'open' ? 'closed' : 'open' } ); const onToggleComments = () => props.editPost( { comment_status: commentStatus === 'open' ? 'closed' : 'open' } ); - const onTogglePanel = () => props.toggleSidebarPanel( PANEL_NAME ); const commentsToggleId = 'allow-comments-toggle-' + instanceId; const pingbacksToggleId = 'allow-pingbacks-toggle-' + instanceId; @@ -60,6 +59,11 @@ export default connect( isOpened: isEditorSidebarPanelOpened( state, PANEL_NAME ), }; }, - { editPost, toggleSidebarPanel } + { + editPost, + onTogglePanel() { + return toggleSidebarPanel( PANEL_NAME ); + }, + } )( withInstanceId( DiscussionPanel ) ); diff --git a/editor/sidebar/featured-image/index.js b/editor/sidebar/featured-image/index.js index a936787bf3f46..b46b49e9662c8 100644 --- a/editor/sidebar/featured-image/index.js +++ b/editor/sidebar/featured-image/index.js @@ -23,11 +23,9 @@ import { editPost, toggleSidebarPanel } from '../../actions'; */ const PANEL_NAME = 'featured-image'; -function FeaturedImage( { featuredImageId, onUpdateImage, onRemoveImage, media, isOpened, ...props } ) { - const onToggle = () => props.toggleSidebarPanel( PANEL_NAME ); - +function FeaturedImage( { featuredImageId, onUpdateImage, onRemoveImage, media, isOpened, onTogglePanel } ) { return ( - +
{ !! featuredImageId &&