Skip to content

Commit

Permalink
Delay focus until MCE init
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jan 31, 2018
1 parent 927a86a commit eb6ee66
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
4 changes: 3 additions & 1 deletion editor/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
isSelectionEnabled,
isTyping,
getBlockMode,
shouldBlockFocusNext,
} from '../../store/selectors';

const { BACKSPACE, ESCAPE, DELETE, ENTER, UP, RIGHT, DOWN, LEFT } = keycodes;
Expand Down Expand Up @@ -460,7 +461,7 @@ export class BlockListBlock extends Component {
{ isValid && mode === 'visual' && (
<BlockEdit
name={ blockName }
focus={ focus }
focus={ focus || this.props.shouldFocusNext }
attributes={ block.attributes }
setAttributes={ this.setAttributes }
insertBlocksAfter={ isLocked ? undefined : this.insertBlocksAfter }
Expand Down Expand Up @@ -504,6 +505,7 @@ const mapStateToProps = ( state, { uid } ) => ( {
isFirstMultiSelected: isFirstMultiSelectedBlock( state, uid ),
isHovered: isBlockHovered( state, uid ) && ! isMultiSelecting( state ),
focus: getBlockFocus( state, uid ),
shouldFocusNext: shouldBlockFocusNext( state, uid ),
isTyping: isTyping( state ),
order: getBlockIndex( state, uid ),
meta: getEditedPostAttribute( state, 'meta' ),
Expand Down
8 changes: 4 additions & 4 deletions editor/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ export function blockSelection( state = {
focus: null,
isMultiSelecting: false,
isEnabled: true,
next: null,
}, action ) {
switch ( action.type ) {
case 'CLEAR_SELECTED_BLOCK':
Expand Down Expand Up @@ -427,14 +428,13 @@ export function blockSelection( state = {
start: action.uid,
end: action.uid,
focus: action.config || {},
next: null,
};
case 'INSERT_BLOCKS':
return {
...state,
start: action.blocks[ 0 ].uid,
end: action.blocks[ 0 ].uid,
focus: {},
isMultiSelecting: false,
// We must wait for Editable to initialise.
next: action.blocks[ 0 ].uid,
};
case 'REPLACE_BLOCKS':
if ( ! action.blocks || ! action.blocks.length || action.uids.indexOf( state.start ) === -1 ) {
Expand Down
12 changes: 12 additions & 0 deletions editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,18 @@ export function getBlockFocus( state, uid ) {
return state.blockSelection.focus;
}

/**
* Returns true if the specified block should focus next.
*
* @param {Object} state Global application state.
* @param {string} uid Block unique ID.
*
* @returns {boolean} Whether the block should focus next.
*/
export function blockShouldFocusNext( state, uid ) {
return state.blockSelection.next === uid;
}

/**
* Whether in the process of multi-selecting or not.
*
Expand Down
8 changes: 5 additions & 3 deletions editor/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,7 @@ describe( 'state', () => {
focus: {},
isMultiSelecting: false,
isEnabled: true,
next: null,
} );
} );

Expand Down Expand Up @@ -892,7 +893,7 @@ describe( 'state', () => {
} ],
} );

expect( state3 ).toEqual( { start: 'ribs', end: 'ribs', focus: {}, isMultiSelecting: false } );
expect( state3 ).toEqual( { start: 'ribs', end: 'chicken', next: 'ribs' } );
} );

it( 'should not update the state if the block moved is already selected', () => {
Expand All @@ -918,18 +919,19 @@ describe( 'state', () => {
focus: { editable: 'citation' },
isMultiSelecting: false,
isEnabled: true,
next: null,
} );
} );

it( 'should update the focus and merge the existing state', () => {
const original = deepFreeze( { start: 'ribs', end: 'ribs', focus: {}, isMultiSelecting: true } );
const original = deepFreeze( { start: 'ribs', end: 'ribs', focus: {}, isMultiSelecting: true, next: null } );
const state = blockSelection( original, {
type: 'UPDATE_FOCUS',
uid: 'ribs',
config: { editable: 'citation' },
} );

expect( state ).toEqual( { start: 'ribs', end: 'ribs', focus: { editable: 'citation' }, isMultiSelecting: true } );
expect( state ).toEqual( { start: 'ribs', end: 'ribs', focus: { editable: 'citation' }, isMultiSelecting: true, next: null } );
} );

it( 'should replace the selected block', () => {
Expand Down

0 comments on commit eb6ee66

Please sign in to comment.