Skip to content

Commit

Permalink
Make getLastFocus and setLastFocus private (#57612)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeryj committed Jan 9, 2024
1 parent 240f303 commit a8b4cc2
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 58 deletions.
24 changes: 0 additions & 24 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,18 +588,6 @@ _Properties_
- _isDisabled_ `boolean`: Whether or not the user should be prevented from inserting this item.
- _frecency_ `number`: Heuristic that combines frequency and recency.

### getLastFocus

Returns the element of the last element that had focus when focus left the editor canvas.

_Parameters_

- _state_ `Object`: Block editor state.

_Returns_

- `Object`: Element.

### getLastMultiSelectedBlockClientId

Returns the client ID of the last block in the multi-selection set, or null if there is no multi-selection.
Expand Down Expand Up @@ -1663,18 +1651,6 @@ _Parameters_
- _clientId_ `string`: The block's clientId.
- _hasControlledInnerBlocks_ `boolean`: True if the block's inner blocks are controlled.

### setLastFocus

Action that sets the element that had focus when focus leaves the editor canvas.

_Parameters_

- _lastFocus_ `Object`: The last focused element.

_Returns_

- `Object`: Action object.

### setNavigationMode

Action that enables or disables the navigation mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ESCAPE } from '@wordpress/keycodes';
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import { unlock } from '../../lock-unlock';

function hasOnlyToolbarItem( elements ) {
const dataProp = 'toolbarItem';
Expand Down Expand Up @@ -169,7 +170,7 @@ function useToolbarFocus( {
};
}, [ initialIndex, initialFocusOnMount, onIndexChange, toolbarRef ] );

const { getLastFocus } = useSelect( blockEditorStore );
const { getLastFocus } = unlock( useSelect( blockEditorStore ) );
/**
* Handles returning focus to the block editor canvas when pressing escape.
*/
Expand Down
14 changes: 7 additions & 7 deletions packages/block-editor/src/components/writing-flow/use-tab-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useRef } from '@wordpress/element';
*/
import { store as blockEditorStore } from '../../store';
import { isInSameBlock, isInsideRootBlock } from '../../utils/dom';
import { unlock } from '../../lock-unlock';

export default function useTabNav() {
const container = useRef();
Expand All @@ -20,16 +21,15 @@ export default function useTabNav() {

const { hasMultiSelection, getSelectedBlockClientId, getBlockCount } =
useSelect( blockEditorStore );
const { setNavigationMode, setLastFocus } = useDispatch( blockEditorStore );
const { setNavigationMode, setLastFocus } = unlock(
useDispatch( blockEditorStore )
);
const isNavigationMode = useSelect(
( select ) => select( blockEditorStore ).isNavigationMode(),
[]
);

const lastFocus = useSelect(
( select ) => select( blockEditorStore ).getLastFocus(),
[]
);
const { getLastFocus } = unlock( useSelect( blockEditorStore ) );

// Don't allow tabbing to this element in Navigation mode.
const focusCaptureTabIndex = ! isNavigationMode ? '0' : undefined;
Expand All @@ -45,7 +45,7 @@ export default function useTabNav() {
} else if ( hasMultiSelection() ) {
container.current.focus();
} else if ( getSelectedBlockClientId() ) {
lastFocus.current.focus();
getLastFocus()?.current.focus();
} else {
setNavigationMode( true );

Expand Down Expand Up @@ -163,7 +163,7 @@ export default function useTabNav() {
}

function onFocusOut( event ) {
setLastFocus( { ...lastFocus, current: event.target } );
setLastFocus( { ...getLastFocus(), current: event.target } );

const { ownerDocument } = node;

Expand Down
15 changes: 0 additions & 15 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1919,18 +1919,3 @@ export function unsetBlockEditingMode( clientId = '' ) {
clientId,
};
}

/**
* Action that sets the element that had focus when focus leaves the editor canvas.
*
* @param {Object} lastFocus The last focused element.
*
*
* @return {Object} Action object.
*/
export function setLastFocus( lastFocus = null ) {
return {
type: 'LAST_FOCUS',
lastFocus,
};
}
15 changes: 15 additions & 0 deletions packages/block-editor/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,18 @@ export function syncDerivedUpdates( callback ) {
} );
};
}

/**
* Action that sets the element that had focus when focus leaves the editor canvas.
*
* @param {Object} lastFocus The last focused element.
*
*
* @return {Object} Action object.
*/
export function setLastFocus( lastFocus = null ) {
return {
type: 'LAST_FOCUS',
lastFocus,
};
}
11 changes: 11 additions & 0 deletions packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,14 @@ export const hasAllowedPatterns = createSelector(
),
]
);

/**
* Returns the element of the last element that had focus when focus left the editor canvas.
*
* @param {Object} state Block editor state.
*
* @return {Object} Element.
*/
export function getLastFocus( state ) {
return state.lastFocus;
}
11 changes: 0 additions & 11 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2946,14 +2946,3 @@ export const isGroupable = createRegistrySelector(
);
}
);

/**
* Returns the element of the last element that had focus when focus left the editor canvas.
*
* @param {Object} state Block editor state.
*
* @return {Object} Element.
*/
export function getLastFocus( state ) {
return state.lastFocus;
}

0 comments on commit a8b4cc2

Please sign in to comment.