Skip to content

Commit

Permalink
Editor: Move editor toggle commands to the editor package (WordPress#…
Browse files Browse the repository at this point in the history
…62093)

Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
  • Loading branch information
3 people authored and patil-vipul committed Jun 17, 2024
1 parent 45659c9 commit 8a4f14c
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 201 deletions.
48 changes: 48 additions & 0 deletions packages/edit-post/src/commands/use-commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { fullscreen } from '@wordpress/icons';
import { useCommand } from '@wordpress/commands';
import { store as preferencesStore } from '@wordpress/preferences';
import { store as noticesStore } from '@wordpress/notices';

export default function useCommands() {
const { isFullscreen } = useSelect( ( select ) => {
const { get } = select( preferencesStore );

return {
isFullscreen: get( 'core/edit-post', 'fullscreenMode' ),
};
}, [] );
const { toggle } = useDispatch( preferencesStore );
const { createInfoNotice } = useDispatch( noticesStore );

useCommand( {
name: 'core/toggle-fullscreen-mode',
label: isFullscreen
? __( 'Exit fullscreen' )
: __( 'Enter fullscreen' ),
icon: fullscreen,
callback: ( { close } ) => {
toggle( 'core/edit-post', 'fullscreenMode' );
close();
createInfoNotice(
isFullscreen ? __( 'Fullscreen off.' ) : __( 'Fullscreen on.' ),
{
id: 'core/edit-post/toggle-fullscreen-mode/notice',
type: 'snackbar',
actions: [
{
label: __( 'Undo' ),
onClick: () => {
toggle( 'core/edit-post', 'fullscreenMode' );
},
},
],
}
);
},
} );
}
4 changes: 2 additions & 2 deletions packages/edit-post/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import MetaBoxes from '../meta-boxes';
import WelcomeGuide from '../welcome-guide';
import { store as editPostStore } from '../../store';
import { unlock } from '../../lock-unlock';
import useCommonCommands from '../../hooks/commands/use-common-commands';
import useEditPostCommands from '../../commands/use-commands';

const { getLayoutStyles } = unlock( blockEditorPrivateApis );
const { useCommands } = unlock( coreCommandsPrivateApis );
Expand Down Expand Up @@ -131,7 +131,7 @@ function useEditorStyles() {

function Layout( { initialPost } ) {
useCommands();
useCommonCommands();
useEditPostCommands();

const isMobileViewport = useViewportMatch( 'medium', '<' );
const isWideViewport = useViewportMatch( 'large' );
Expand Down
126 changes: 0 additions & 126 deletions packages/edit-post/src/hooks/commands/use-common-commands.js

This file was deleted.

73 changes: 2 additions & 71 deletions packages/edit-site/src/hooks/commands/use-edit-mode-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,11 @@
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { __, sprintf, isRTL } from '@wordpress/i18n';
import {
trash,
rotateLeft,
rotateRight,
layout,
page,
drawerLeft,
drawerRight,
blockDefault,
} from '@wordpress/icons';
import { trash, rotateLeft, rotateRight, layout, page } from '@wordpress/icons';
import { useCommandLoader } from '@wordpress/commands';
import { decodeEntities } from '@wordpress/html-entities';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import {
store as editorStore,
privateApis as editorPrivateApis,
} from '@wordpress/editor';
import { store as editorStore } from '@wordpress/editor';

/**
* Internal dependencies
Expand All @@ -32,7 +20,6 @@ import { unlock } from '../../lock-unlock';
import { TEMPLATE_POST_TYPE } from '../../utils/constants';
import { useLink } from '../../components/routes/link';

const { interfaceStore } = unlock( editorPrivateApis );
const { useHistory } = unlock( routerPrivateApis );

function usePageContentFocusCommands() {
Expand Down Expand Up @@ -171,57 +158,6 @@ function useManipulateDocumentCommands() {
};
}

function useEditUICommands() {
const { openGeneralSidebar, closeGeneralSidebar } =
useDispatch( editSiteStore );
const { canvasMode, activeSidebar } = useSelect( ( select ) => {
return {
canvasMode: unlock( select( editSiteStore ) ).getCanvasMode(),
activeSidebar:
select( interfaceStore ).getActiveComplementaryArea( 'core' ),
};
}, [] );

if ( canvasMode !== 'edit' ) {
return { isLoading: false, commands: [] };
}

const commands = [];

commands.push( {
name: 'core/open-settings-sidebar',
label: __( 'Toggle settings sidebar' ),
icon: isRTL() ? drawerLeft : drawerRight,
callback: ( { close } ) => {
close();
if ( activeSidebar === 'edit-post/document' ) {
closeGeneralSidebar();
} else {
openGeneralSidebar( 'edit-post/document' );
}
},
} );

commands.push( {
name: 'core/open-block-inspector',
label: __( 'Toggle block inspector' ),
icon: blockDefault,
callback: ( { close } ) => {
close();
if ( activeSidebar === 'edit-site/block' ) {
closeGeneralSidebar();
} else {
openGeneralSidebar( 'edit-site/block' );
}
},
} );

return {
isLoading: false,
commands,
};
}

export function useEditModeCommands() {
useCommandLoader( {
name: 'core/edit-site/page-content-focus',
Expand All @@ -233,9 +169,4 @@ export function useEditModeCommands() {
name: 'core/edit-site/manipulate-document',
hook: useManipulateDocumentCommands,
} );

useCommandLoader( {
name: 'core/edit-site/edit-ui',
hook: useEditUICommands,
} );
}
64 changes: 62 additions & 2 deletions packages/editor/src/components/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { __, isRTL } from '@wordpress/i18n';
import {
blockDefault,
code,
drawerLeft,
drawerRight,
edit,
formatListBullets,
listView,
external,
keyboard,
Expand Down Expand Up @@ -38,6 +42,7 @@ function useEditorCommandLoader() {
isViewable,
isCodeEditingEnabled,
isRichEditingEnabled,
isPublishSidebarEnabled,
} = useSelect( ( select ) => {
const { get } = select( preferencesStore );
const { isListViewOpened, getCurrentPostType, getEditorSettings } =
Expand All @@ -56,8 +61,11 @@ function useEditorCommandLoader() {
isViewable: getPostType( getCurrentPostType() )?.viewable ?? false,
isCodeEditingEnabled: getEditorSettings().codeEditingEnabled,
isRichEditingEnabled: getEditorSettings().richEditingEnabled,
isPublishSidebarEnabled:
select( editorStore ).isPublishSidebarEnabled(),
};
}, [] );
const { getActiveComplementaryArea } = useSelect( interfaceStore );
const { toggle } = useDispatch( preferencesStore );
const { createInfoNotice } = useDispatch( noticesStore );
const {
Expand All @@ -66,7 +74,8 @@ function useEditorCommandLoader() {
switchEditorMode,
toggleDistractionFree,
} = useDispatch( editorStore );
const { openModal } = useDispatch( interfaceStore );
const { openModal, enableComplementaryArea, disableComplementaryArea } =
useDispatch( interfaceStore );
const { getCurrentPostId } = useSelect( editorStore );
const allowSwitchEditorMode = isCodeEditingEnabled && isRichEditingEnabled;

Expand Down Expand Up @@ -211,6 +220,57 @@ function useEditorCommandLoader() {
},
} );

commands.push( {
name: 'core/open-settings-sidebar',
label: __( 'Toggle settings sidebar' ),
icon: isRTL() ? drawerLeft : drawerRight,
callback: ( { close } ) => {
const activeSidebar = getActiveComplementaryArea( 'core' );
close();
if ( activeSidebar === 'edit-post/document' ) {
disableComplementaryArea( 'core' );
} else {
enableComplementaryArea( 'core', 'edit-post/document' );
}
},
} );

commands.push( {
name: 'core/open-block-inspector',
label: __( 'Toggle block inspector' ),
icon: blockDefault,
callback: ( { close } ) => {
const activeSidebar = getActiveComplementaryArea( 'core' );
close();
if ( activeSidebar === 'edit-post/block' ) {
disableComplementaryArea( 'core' );
} else {
enableComplementaryArea( 'core', 'edit-post/block' );
}
},
} );

commands.push( {
name: 'core/toggle-publish-sidebar',
label: isPublishSidebarEnabled
? __( 'Disable pre-publish checks' )
: __( 'Enable pre-publish checks' ),
icon: formatListBullets,
callback: ( { close } ) => {
close();
toggle( 'core', 'isPublishSidebarEnabled' );
createInfoNotice(
isPublishSidebarEnabled
? __( 'Pre-publish checks disabled.' )
: __( 'Pre-publish checks enabled.' ),
{
id: 'core/editor/publish-sidebar/notice',
type: 'snackbar',
}
);
},
} );

if ( isViewable ) {
commands.push( {
name: 'core/preview-link',
Expand Down

0 comments on commit 8a4f14c

Please sign in to comment.