Skip to content

Commit

Permalink
Adds the distraction free mode keyboard shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
draganescu committed Jun 23, 2023
1 parent 7236c95 commit ed86a44
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 30 deletions.
2 changes: 0 additions & 2 deletions packages/edit-post/src/components/keyboard-shortcuts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,6 @@ function KeyboardShortcuts() {
} );

useShortcut( 'core/edit-post/toggle-distraction-free', () => {
closeGeneralSidebar();
setIsListViewOpened( false );
toggleDistractionFree();
toggleFeature( 'distractionFree' );
createInfoNotice(
Expand Down
37 changes: 35 additions & 2 deletions packages/edit-site/src/components/keyboard-shortcuts/edit-mode.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useShortcut } from '@wordpress/keyboard-shortcuts';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { store as interfaceStore } from '@wordpress/interface';
import { createBlock } from '@wordpress/blocks';
import { store as preferencesStore } from '@wordpress/preferences';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand All @@ -29,15 +32,31 @@ function KeyboardShortcutsEditMode() {
[]
);
const { redo, undo } = useDispatch( coreStore );
const { setIsListViewOpened, switchEditorMode } =
useDispatch( editSiteStore );
const {
isFeatureActive,
setIsListViewOpened,
switchEditorMode,
toggleFeature,
setIsInserterOpened,
closeGeneralSidebar,
} = useDispatch( editSiteStore );
const { enableComplementaryArea, disableComplementaryArea } =
useDispatch( interfaceStore );

const { replaceBlocks } = useDispatch( blockEditorStore );
const { getBlockName, getSelectedBlockClientId, getBlockAttributes } =
useSelect( blockEditorStore );

const { set: setPreference } = useDispatch( preferencesStore );
const { createInfoNotice } = useDispatch( noticesStore );

const toggleDistractionFree = () => {
setPreference( 'core/edit-site', 'fixedToolbar', false );
setIsInserterOpened( false );
setIsListViewOpened( false );
closeGeneralSidebar();
};

const handleTextLevelShortcut = ( event, level ) => {
event.preventDefault();
const destinationBlockName =
Expand Down Expand Up @@ -114,6 +133,20 @@ function KeyboardShortcutsEditMode() {
);
} );

useShortcut( 'core/edit-site/toggle-distraction-free', () => {
toggleDistractionFree();
toggleFeature( 'distractionFree' );
createInfoNotice(
isFeatureActive( 'distractionFree' )
? __( 'Distraction free mode turned on.' )
: __( 'Distraction free mode turned off.' ),
{
id: 'core/edit-site/distraction-free-mode/notice',
type: 'snackbar',
}
);
} );

return null;
}

Expand Down
10 changes: 10 additions & 0 deletions packages/edit-site/src/components/keyboard-shortcuts/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ function KeyboardShortcutsRegister() {
},
} );
} );

registerShortcut( {
name: 'core/edit-site/toggle-distraction-free',
category: 'global',
description: __( 'Toggle distraction free mode.' ),
keyCombination: {
modifier: 'primaryShift',
character: '\\',
},
} );
}, [ registerShortcut ] );

return null;
Expand Down
55 changes: 30 additions & 25 deletions packages/edit-site/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,36 @@ export default function Layout() {
const isMobileViewport = useViewportMatch( 'medium', '<' );
const isListPage = getIsListPage( params, isMobileViewport );
const isEditorPage = ! isListPage;
<<<<<<< HEAD
const { hasFixedToolbar, canvasMode, previousShortcut, nextShortcut } =
useSelect( ( select ) => {
const { getAllShortcutKeyCombinations } = select(
keyboardShortcutsStore
);
const { getCanvasMode } = unlock( select( editSiteStore ) );
return {
canvasMode: getCanvasMode(),
previousShortcut: getAllShortcutKeyCombinations(
'core/edit-site/previous-region'
),
nextShortcut: getAllShortcutKeyCombinations(
'core/edit-site/next-region'
),
hasFixedToolbar: select( preferencesStore ).get(
'core/edit-site',
'fixedToolbar'
),
isDistractionFree: select( preferencesStore ).get(
'core/edit-site',
'distractionFree'
),
};
}, [] );

const {
isDistractionFree,
hasFixedToolbar,
canvasMode,
previousShortcut,
nextShortcut,
} = useSelect( ( select ) => {
const { getAllShortcutKeyCombinations } = select(
keyboardShortcutsStore
);
const { getCanvasMode } = unlock( select( editSiteStore ) );
return {
canvasMode: getCanvasMode(),
previousShortcut: getAllShortcutKeyCombinations(
'core/edit-site/previous-region'
),
nextShortcut: getAllShortcutKeyCombinations(
'core/edit-site/next-region'
),
hasFixedToolbar: select( preferencesStore ).get(
'core/edit-site',
'fixedToolbar'
),
isDistractionFree: select( preferencesStore ).get(
'core/edit-site',
'distractionFree'
),
};
}, [] );
const isEditing = canvasMode === 'edit';
const navigateRegionsProps = useNavigateRegions( {
previous: previousShortcut,
Expand Down
5 changes: 4 additions & 1 deletion packages/edit-site/src/components/layout/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@
// so the fixed toolbar can be positioned on top of it
// but only on desktop
@include break-medium() {
.edit-site-layout__canvas-container {
z-index: 5;
}
.edit-site-site-hub {
z-index: 4;
}
Expand All @@ -270,7 +273,7 @@
top: 0;
left: 0;
right: 0;
z-index: 9998;
z-index: z-index(".edit-site-layout__header-container");
width: 100%;

// We need ! important because we override inline styles
Expand Down

0 comments on commit ed86a44

Please sign in to comment.