Skip to content

Commit

Permalink
register the shortcut in ZoomOutToggle
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Oct 24, 2024
1 parent 226ef00 commit fc50e88
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { store as blockEditorStore } from '@wordpress/block-editor';
* Internal dependencies
*/
import { store as editorStore } from '../../store';
import { unlock } from '../../lock-unlock';

/**
* Handles the keyboard shortcuts for the editor.
Expand All @@ -25,12 +24,7 @@ export default function EditorKeyboardShortcuts() {
select( editorStore ).getEditorSettings();
return ! richEditingEnabled || ! codeEditingEnabled;
}, [] );
const { getBlockSelectionStart, isZoomOut } = unlock(
useSelect( blockEditorStore )
);
const { setZoomLevel, resetZoomLevel } = unlock(
useDispatch( blockEditorStore )
);
const { getBlockSelectionStart } = useSelect( blockEditorStore );
const { getActiveComplementaryArea } = useSelect( interfaceStore );
const { enableComplementaryArea, disableComplementaryArea } =
useDispatch( interfaceStore );
Expand Down Expand Up @@ -124,13 +118,5 @@ export default function EditorKeyboardShortcuts() {
}
} );

useShortcut( 'core/editor/zoom', () => {
if ( isZoomOut() ) {
resetZoomLevel();
} else {
setZoomLevel( 'auto-scaled' );
}
} );

return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,6 @@ function EditorKeyboardShortcutsRegister() {
},
],
} );

registerShortcut( {
name: 'core/editor/zoom',
category: 'global',
description: __( 'Enter or exit zoom out.' ),
keyCombination: {
modifier: 'primaryShift',
character: 'z',
},
} );
}, [ registerShortcut ] );

return <BlockEditorKeyboardShortcuts.Register />;
Expand Down
32 changes: 31 additions & 1 deletion packages/editor/src/components/zoom-out-toggle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
*/
import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

import { useEffect } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { square as zoomOutIcon } from '@wordpress/icons';
import { store as preferencesStore } from '@wordpress/preferences';
import {
useShortcut,
store as keyboardShortcutsStore,
} from '@wordpress/keyboard-shortcuts';

/**
* Internal dependencies
Expand All @@ -26,6 +30,32 @@ const ZoomOutToggle = ( { disabled } ) => {
const { resetZoomLevel, setZoomLevel } = unlock(
useDispatch( blockEditorStore )
);
const { registerShortcut, unregisterShortcut } = useDispatch(
keyboardShortcutsStore
);

useEffect( () => {
registerShortcut( {
name: 'core/editor/zoom',
category: 'global',
description: __( 'Enter or exit zoom out.' ),
keyCombination: {
modifier: 'primaryShift',
character: 'z',
},
} );
return () => {
unregisterShortcut( 'core/editor/zoom' );
};
}, [ registerShortcut, unregisterShortcut ] );

useShortcut( 'core/editor/zoom', () => {
if ( isZoomOut ) {
resetZoomLevel();
} else {
setZoomLevel( 'auto-scaled' );
}
} );

const handleZoomOut = () => {
if ( isZoomOut ) {
Expand Down

0 comments on commit fc50e88

Please sign in to comment.