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

Fix DFM ui toggling bugs #59061

Merged
merged 2 commits into from Feb 15, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -5,6 +5,7 @@ import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect, useRef } from '@wordpress/element';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { store as editorStore } from '@wordpress/editor';
import { store as preferencesStore } from '@wordpress/preferences';

/**
* Internal dependencies
Expand All @@ -22,19 +23,25 @@ import {
* @param {number} postId The current post id.
*/
export const useBlockSelectionListener = ( postId ) => {
const { hasBlockSelection, isEditorSidebarOpened } = useSelect(
( select ) => ( {
hasBlockSelection:
!! select( blockEditorStore ).getBlockSelectionStart(),
isEditorSidebarOpened: select( STORE_NAME ).isEditorSidebarOpened(),
} ),
[ postId ]
);
const { hasBlockSelection, isEditorSidebarOpened, isDistractionFree } =
useSelect(
( select ) => {
const { get } = select( preferencesStore );
return {
hasBlockSelection:
!! select( blockEditorStore ).getBlockSelectionStart(),
isEditorSidebarOpened:
select( STORE_NAME ).isEditorSidebarOpened(),
isDistractionFree: get( 'core', 'distractionFree' ),
};
},
[ postId ]
);

const { openGeneralSidebar } = useDispatch( STORE_NAME );

useEffect( () => {
if ( ! isEditorSidebarOpened ) {
if ( ! isEditorSidebarOpened || isDistractionFree ) {
return;
}
if ( hasBlockSelection ) {
Expand Down
Expand Up @@ -44,6 +44,12 @@ describe( 'listener hook tests', () => {
isViewportMatch: jest.fn(),
},
},
'core/preferences': {
...storeConfig,
selectors: {
get: jest.fn(),
},
},
[ STORE_NAME ]: {
...storeConfig,
actions: {
Expand Down Expand Up @@ -112,6 +118,7 @@ describe( 'listener hook tests', () => {
'getBlockSelectionStart',
true
);
setMockReturnValue( 'core/preferences', 'get', false );

render( <TestedOutput /> );

Expand All @@ -120,19 +127,52 @@ describe( 'listener hook tests', () => {
).toHaveBeenCalledWith( 'edit-post/block' );
} );
it( 'opens document sidebar if block is not selected', () => {
setMockReturnValue( STORE_NAME, 'isEditorSidebarOpened', true );
setMockReturnValue( STORE_NAME, 'isEditorSidebarOpened', true );
setMockReturnValue(
'core/block-editor',
'getBlockSelectionStart',
false
);
setMockReturnValue( 'core/preferences', 'get', false );

render( <TestedOutput /> );

expect(
getSpyedAction( STORE_NAME, 'openGeneralSidebar' )
).toHaveBeenCalledWith( 'edit-post/document' );
} );
it( 'does not open block sidebar if block is selected and distraction free mode is on', () => {
setMockReturnValue( STORE_NAME, 'isEditorSidebarOpened', true );
setMockReturnValue(
'core/block-editor',
'getBlockSelectionStart',
true
);
setMockReturnValue( 'core/preferences', 'get', true );

render( <TestedOutput /> );

expect(
getSpyedAction( STORE_NAME, 'openGeneralSidebar' )
).toHaveBeenCalledTimes( 0 );
} );
it( 'does not open document sidebar if block is not selected and distraction free is on', () => {
setMockReturnValue( STORE_NAME, 'isEditorSidebarOpened', true );
setMockReturnValue( STORE_NAME, 'isEditorSidebarOpened', true );
setMockReturnValue(
'core/block-editor',
'getBlockSelectionStart',
false
);
setMockReturnValue( 'core/preferences', 'get', true );

render( <TestedOutput /> );

expect(
getSpyedAction( STORE_NAME, 'openGeneralSidebar' )
).toHaveBeenCalledTimes( 0 );
} );
} );

describe( 'useUpdatePostLinkListener', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-post/src/components/layout/index.js
Expand Up @@ -385,7 +385,7 @@ function Layout( { initialPost } ) {
<PostSyncStatusModal />
<StartPageOptions />
<PluginArea onError={ onPluginAreaError } />
<SettingsSidebar />
{ ! isDistractionFree && <SettingsSidebar /> }
</>
);
}
Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/src/components/editor/index.js
Expand Up @@ -263,6 +263,7 @@ export default function Editor( { isLoading } ) {
( shouldShowListView && <ListViewSidebar /> ) )
}
sidebar={
! isDistractionFree &&
isEditMode &&
isRightSidebarOpen && (
<>
Expand Down
38 changes: 22 additions & 16 deletions packages/editor/src/components/document-tools/index.js
Expand Up @@ -46,6 +46,7 @@ function DocumentTools( {
const { setIsInserterOpened, setIsListViewOpened } =
useDispatch( editorStore );
const {
isDistractionFree,
isInserterOpened,
isListViewOpen,
listViewShortcut,
Expand All @@ -69,6 +70,7 @@ function DocumentTools( {
listViewToggleRef: getListViewToggleRef(),
hasFixedToolbar: getSettings().hasFixedToolbar,
showIconLabels: get( 'core', 'showIconLabels' ),
isDistractionFree: get( 'core', 'distractionFree' ),
};
}, [] );

Expand Down Expand Up @@ -158,22 +160,26 @@ function DocumentTools( {
variant={ showIconLabels ? 'tertiary' : undefined }
size="compact"
/>
<ToolbarItem
as={ Button }
className="editor-document-tools__document-overview-toggle"
icon={ listView }
disabled={ disableBlockTools }
isPressed={ isListViewOpen }
/* translators: button label text should, if possible, be under 16 characters. */
label={ listViewLabel }
onClick={ toggleListView }
shortcut={ listViewShortcut }
showTooltip={ ! showIconLabels }
variant={ showIconLabels ? 'tertiary' : undefined }
aria-expanded={ isListViewOpen }
ref={ listViewToggleRef }
size="compact"
/>
{ ! isDistractionFree && (
<ToolbarItem
as={ Button }
className="editor-document-tools__document-overview-toggle"
icon={ listView }
disabled={ disableBlockTools }
isPressed={ isListViewOpen }
/* translators: button label text should, if possible, be under 16 characters. */
label={ listViewLabel }
onClick={ toggleListView }
shortcut={ listViewShortcut }
showTooltip={ ! showIconLabels }
variant={
showIconLabels ? 'tertiary' : undefined
}
aria-expanded={ isListViewOpen }
ref={ listViewToggleRef }
size="compact"
/>
) }
</>
) }
{ children }
Expand Down