Skip to content

Commit

Permalink
eliminate ADJUST_SIDEBAR control and move logic into action
Browse files Browse the repository at this point in the history
  • Loading branch information
nerrad committed Apr 27, 2019
1 parent 8287c2b commit db6f2ca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 55 deletions.
29 changes: 27 additions & 2 deletions packages/edit-post/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { castArray } from 'lodash';
/**
* Internal dependencies
*/
import { __unstableSubscribe, __experimentalAdjustSidebar } from './controls';
import { __unstableSubscribe } from './controls';
import { onChangeListener } from './utils';
import { STORE_KEY, VIEW_AS_LINK_SELECTOR } from './constants';

Expand Down Expand Up @@ -268,7 +268,32 @@ export function* __unstableInitialize() {
}
) );
// hide/show the sidebar depending on size of viewport.
yield __experimentalAdjustSidebar();
yield __unstableSubscribe( ( registry ) => onChangeListener(
() => registry.select( 'core/viewport' )
.isViewportMatch( '< medium' ),
( () => {
let sidebarToReOpenOnExpand = null;
return ( isSmall ) => {
const { getActiveGeneralSidebarName } = registry.select( STORE_KEY );
const {
closeGeneralSidebar: closeSidebar,
openGeneralSidebar: openSidebar,
} = registry.dispatch( STORE_KEY );
if ( isSmall ) {
sidebarToReOpenOnExpand = getActiveGeneralSidebarName();
if ( sidebarToReOpenOnExpand ) {
closeSidebar();
}
} else if (
sidebarToReOpenOnExpand &&
! getActiveGeneralSidebarName()
) {
openSidebar( sidebarToReOpenOnExpand );
}
};
} )(),
true
) );
// Update View Post link in the admin bar when permalink is updated.
yield __unstableSubscribe( ( registry ) => onChangeListener(
() => registry.select( 'core/editor' ).getCurrentPost().link,
Expand Down
53 changes: 0 additions & 53 deletions packages/edit-post/src/store/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
*/
import { createRegistryControl } from '@wordpress/data';

/**
* Internal dependencies
*/
import { STORE_KEY } from './constants';
import { onChangeListener } from './utils';

/**
* Calls a selector using the current state.
*
Expand Down Expand Up @@ -38,15 +32,6 @@ export function __unstableSubscribe( listenerCallback ) {
return { type: 'SUBSCRIBE', listenerCallback };
}

/**
* A control for adjusting the sidebar when viewport mobile size is triggered.
*
* @return {Object} control.descriptor.
*/
export function __experimentalAdjustSidebar() {
return { type: 'ADJUST_SIDEBAR' };
}

const controls = {
SELECT: createRegistryControl(
( registry ) => ( { storeName, selectorName, args } ) => {
Expand All @@ -58,44 +43,6 @@ const controls = {
return registry.subscribe( listenerCallback( registry ) );
}
),
ADJUST_SIDEBAR: createRegistryControl(
( registry ) => () => {
const isMobileViewPort = () => registry.select( 'core/viewport' )
.isViewportMatch( '< medium' );
const adjuster = ( () => {
// contains the sidebar we close when going to viewport sizes lower than
// medium. This allows to reopen it when going again to viewport sizes
// greater than medium.
let sidebarToReOpenOnExpand = null;
return ( isSmall ) => {
const { getActiveGeneralSidebarName } = registry.select( STORE_KEY );
const {
closeGeneralSidebar,
openGeneralSidebar,
} = registry.dispatch( STORE_KEY );
if ( isSmall ) {
sidebarToReOpenOnExpand = getActiveGeneralSidebarName();
if ( sidebarToReOpenOnExpand ) {
closeGeneralSidebar();
}
} else if (
sidebarToReOpenOnExpand &&
! getActiveGeneralSidebarName()
) {
openGeneralSidebar( sidebarToReOpenOnExpand );
}
};
} )();
adjuster( isMobileViewPort() );

// Collapse sidebar when viewport shrinks.
// Reopen sidebar it if viewport expands and it was closed because of a
// previous shrink.
return registry.subscribe(
onChangeListener( isMobileViewPort, adjuster )
);
}
),
};

export default controls;

0 comments on commit db6f2ca

Please sign in to comment.