Skip to content

Commit

Permalink
Avoid focusing blocks when inserting them into the canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jan 20, 2021
1 parent f2d3c09 commit 28b1c51
Show file tree
Hide file tree
Showing 17 changed files with 41 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ _Returns_

Returns the initial caret position for the selected block.
This position is to used to position the caret properly when the selected block changes.
If the current block is not a RichText, having initial position set to 0 means "focus block"

_Parameters_

Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, { select } ) => {
const { clientId, rootClientId } = ownProps;
const { getBlockIndex } = select( 'core/block-editor' );
const index = getBlockIndex( clientId, rootClientId );
insertBlocks( blocks, index + 1, rootClientId );
insertBlocks( blocks, index + 1, rootClientId, 0 );
},
onMerge( forward ) {
const { clientId } = ownProps;
Expand Down Expand Up @@ -408,7 +408,7 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, { select } ) => {
[ ownProps.clientId ],
blocks,
indexToSelect,
initialPosition
initialPosition || 0
);
},
toggleSelection( selectionEnabled ) {
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import BlockListAppender from '../block-list-appender';
import useBlockDropZone from '../use-block-drop-zone';
import useInsertionPoint from './insertion-point';
import BlockPopover from './block-popover';
import { useScrollSelectionIntoView } from '../selection-scroll-into-view';

/**
* If the block count exceeds the threshold, we disable the reordering animation
Expand All @@ -31,6 +32,7 @@ export default function BlockList( { className } ) {
const ref = useRef();
const [ blockNodes, setBlockNodes ] = useState( {} );
const insertionPoint = useInsertionPoint( ref );
useScrollSelectionIntoView( ref );

return (
<BlockNodes.Provider value={ blockNodes }>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ function useInitialPosition( clientId ) {
}

// If there's no initial position, return 0 to focus the start.
return getSelectedBlocksInitialCaretPosition() || 0;
return getSelectedBlocksInitialCaretPosition();
},
[ clientId ]
);
}

/**
* Transitions focus to the block or inner tabbable when the block becomes
* selected.
* selected and an initial position is set.
*
* @param {RefObject} ref React ref with the block element.
* @param {string} clientId Block client ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export default function BlockNavigationAppender( {
<Inserter
rootClientId={ parentBlockClientId }
__experimentalIsQuick
__experimentalSelectBlockOnInsert={ false }
aria-describedby={ descriptionId }
toggleProps={ { ref, tabIndex, onFocus } }
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,13 @@ import { Icon, plus } from '@wordpress/icons';
import Inserter from '../inserter';

function ButtonBlockAppender(
{
rootClientId,
className,
__experimentalSelectBlockOnInsert: selectBlockOnInsert,
onFocus,
tabIndex,
},
{ rootClientId, className, onFocus, tabIndex },
ref
) {
return (
<Inserter
position="bottom center"
rootClientId={ rootClientId }
__experimentalSelectBlockOnInsert={ selectBlockOnInsert }
__experimentalIsQuick
renderToggle={ ( {
onToggle,
Expand Down
5 changes: 1 addition & 4 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ export { default as Inserter } from './inserter';
export { default as __experimentalLibrary } from './inserter/library';
export { default as __experimentalSearchForm } from './inserter/search-form';
export { default as BlockEditorKeyboardShortcuts } from './keyboard-shortcuts';
export {
default as MultiSelectScrollIntoView,
useScrollMultiSelectionIntoView as __unstableUseScrollMultiSelectionIntoView,
} from './multi-select-scroll-into-view';
export { MultiSelectScrollIntoView } from './selection-scroll-into-view';
export { default as NavigableToolbar } from './navigable-toolbar';
export {
default as ObserveTyping,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import { useCallback } from '@wordpress/element';
* block with this ID.
* @property {boolean=} isAppender Whether the inserter is an appender
* or not.
* @property {boolean=} selectBlockOnInsert Whether the block should be
* selected on insert.
* @property {Function=} onSelect Called after insertion.
*/

Expand All @@ -34,7 +32,6 @@ function useInsertionPoint( {
insertionIndex,
clientId,
isAppender,
selectBlockOnInsert,
onSelect,
} ) {
const {
Expand Down Expand Up @@ -108,28 +105,26 @@ function useInsertionPoint( {
selectedBlock.clientId,
blocks,
null,
null,
undefined,
meta
);
} else {
insertBlocks(
blocks,
destinationIndex,
destinationRootClientId,
selectBlockOnInsert,
false,
meta
);
}

if ( ! selectBlockOnInsert ) {
// translators: %d: the name of the block that has been added
const message = _n(
'%d block added.',
'%d blocks added.',
blocks.length
);
speak( message );
}
// translators: %d: the name of the block that has been added
const message = _n(
'%d block added.',
'%d blocks added.',
blocks.length
);
speak( message );

if ( onSelect ) {
onSelect();
Expand All @@ -142,7 +137,6 @@ function useInsertionPoint( {
insertBlocks,
destinationRootClientId,
destinationIndex,
selectBlockOnInsert,
onSelect,
]
);
Expand Down
24 changes: 8 additions & 16 deletions packages/block-editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ class Inserter extends Component {
clientId,
isAppender,
showInserterHelpPanel,
__experimentalSelectBlockOnInsert: selectBlockOnInsert,

// This prop is experimental to give some time for the quick inserter to mature
// Feel free to make them stable after a few releases.
Expand All @@ -147,7 +146,6 @@ class Inserter extends Component {
rootClientId={ rootClientId }
clientId={ clientId }
isAppender={ isAppender }
selectBlockOnInsert={ selectBlockOnInsert }
/>
);
}
Expand All @@ -161,7 +159,6 @@ class Inserter extends Component {
clientId={ clientId }
isAppender={ isAppender }
showInserterHelpPanel={ showInserterHelpPanel }
__experimentalSelectBlockOnInsert={ selectBlockOnInsert }
/>
);
}
Expand Down Expand Up @@ -238,12 +235,9 @@ export default compose( [
rootClientId,
clientId,
isAppender,
onSelectOrClose,
} = ownProps;
const {
hasSingleBlockType,
allowedBlockType,
__experimentalSelectBlockOnInsert: selectBlockOnInsert,
onSelectOrClose,
} = ownProps;

if ( ! hasSingleBlockType ) {
Expand Down Expand Up @@ -280,21 +274,19 @@ export default compose( [
blockToInsert,
getInsertionIndex(),
rootClientId,
selectBlockOnInsert
false
);

if ( onSelectOrClose ) {
onSelectOrClose();
}

if ( ! selectBlockOnInsert ) {
const message = sprintf(
// translators: %s: the name of the block that has been added
__( '%s block added' ),
allowedBlockType.title
);
speak( message );
}
const message = sprintf(
// translators: %s: the name of the block that has been added
__( '%s block added' ),
allowedBlockType.title
);
speak( message );
},
};
} ),
Expand Down
4 changes: 0 additions & 4 deletions packages/block-editor/src/components/inserter/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ function InserterLibrary( {
isAppender,
showInserterHelpPanel,
showMostUsedBlocks = false,
__experimentalSelectBlockOnInsert,
__experimentalInsertionIndex,
onSelect = noop,
} ) {
Expand All @@ -42,9 +41,6 @@ function InserterLibrary( {
isAppender={ isAppender }
showInserterHelpPanel={ showInserterHelpPanel }
showMostUsedBlocks={ showMostUsedBlocks }
__experimentalSelectBlockOnInsert={
__experimentalSelectBlockOnInsert
}
__experimentalInsertionIndex={ __experimentalInsertionIndex }
/>
);
Expand Down
5 changes: 0 additions & 5 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ function InserterMenu( {
rootClientId,
clientId,
isAppender,
__experimentalSelectBlockOnInsert,
__experimentalInsertionIndex,
onSelect,
showInserterHelpPanel,
Expand All @@ -43,7 +42,6 @@ function InserterMenu( {
rootClientId,
clientId,
isAppender,
selectBlockOnInsert: __experimentalSelectBlockOnInsert,
insertionIndex: __experimentalInsertionIndex,
} );
const { hasPatterns, hasReusableBlocks } = useSelect( ( select ) => {
Expand Down Expand Up @@ -177,9 +175,6 @@ function InserterMenu( {
rootClientId={ rootClientId }
clientId={ clientId }
isAppender={ isAppender }
selectBlockOnInsert={
__experimentalSelectBlockOnInsert
}
showBlockDirectory
/>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ export default function QuickInserter( {
rootClientId,
clientId,
isAppender,
selectBlockOnInsert,
} ) {
const [ filterValue, setFilterValue ] = useState( '' );
const [ destinationRootClientId, onInsertBlocks ] = useInsertionPoint( {
onSelect,
rootClientId,
clientId,
isAppender,
selectBlockOnInsert,
} );
const [ blockTypes ] = useBlockTypesState(
destinationRootClientId,
Expand Down Expand Up @@ -104,7 +102,6 @@ export default function QuickInserter( {
rootClientId={ rootClientId }
clientId={ clientId }
isAppender={ isAppender }
selectBlockOnInsert={ selectBlockOnInsert }
maxBlockPatterns={ showPatterns ? SHOWN_BLOCK_PATTERNS : 0 }
maxBlockTypes={ SHOWN_BLOCK_TYPES }
isDraggable={ false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ function InserterSearchResults( {
rootClientId,
clientId,
isAppender,
selectBlockOnInsert,
maxBlockPatterns,
maxBlockTypes,
showBlockDirectory = false,
Expand All @@ -45,7 +44,6 @@ function InserterSearchResults( {
rootClientId,
clientId,
isAppender,
selectBlockOnInsert,
} );
const [
blockTypes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,11 @@ import { getScrollContainer } from '@wordpress/dom';
*/
import { getBlockDOMNode } from '../../utils/dom';

export function useScrollMultiSelectionIntoView( ref ) {
const selectionEnd = useSelect( ( select ) => {
const {
getBlockSelectionEnd,
hasMultiSelection,
isMultiSelecting,
} = select( 'core/block-editor' );

const blockSelectionEnd = getBlockSelectionEnd();

if (
! blockSelectionEnd ||
isMultiSelecting() ||
! hasMultiSelection()
) {
return;
}

return blockSelectionEnd;
}, [] );
export function useScrollSelectionIntoView( ref ) {
const selectionEnd = useSelect(
( select ) => select( 'core/block-editor' ).getBlockSelectionEnd(),
[]
);

useEffect( () => {
if ( ! selectionEnd ) {
Expand Down Expand Up @@ -66,8 +51,8 @@ export function useScrollMultiSelectionIntoView( ref ) {
* Scrolls the multi block selection end into view if not in view already. This
* is important to do after selection by keyboard.
*/
export default function MultiSelectScrollIntoView() {
export function MultiSelectScrollIntoView() {
const ref = useRef();
useScrollMultiSelectionIntoView( ref );
useScrollSelectionIntoView( ref );
return <div ref={ ref } />;
}
13 changes: 7 additions & 6 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ function selectionHelper( state = {}, action ) {
case 'REPLACE_INNER_BLOCKS': // REPLACE_INNER_BLOCKS and INSERT_BLOCKS should follow the same logic.
case 'INSERT_BLOCKS': {
// REPLACE_INNER_BLOCKS can be called with an empty array.
if ( ! action.updateSelection || ! action.blocks.length ) {
if ( ! action.blocks.length ) {
return state;
}

Expand Down Expand Up @@ -1225,11 +1225,7 @@ function selectionHelper( state = {}, action ) {
return state;
}

const newState = { clientId: blockToSelect.clientId };
if ( typeof action.initialPosition === 'number' ) {
newState.initialPosition = action.initialPosition;
}
return newState;
return { clientId: blockToSelect.clientId };
}
}

Expand Down Expand Up @@ -1372,6 +1368,11 @@ export function initialPosition( state, action ) {
return state;
} else if ( action.type === 'START_TYPING' ) {
return state;
} else if (
action.type === 'INSERT_BLOCKS' ||
action.type === 'REPLACE_INNER_BLOCKS'
) {
return action.updateSelection ? 0 : undefined;
}

// Reset the state by default (for any action not handled).
Expand Down
Loading

0 comments on commit 28b1c51

Please sign in to comment.