Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blocks: Reselect the current block if we click again on it #556

Merged
merged 3 commits into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions blocks/components/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ export default class Editable extends wp.element.Component {
return;
}

this.savedContent = this.getContent();
this.editor.save();
this.props.onChange( this.getContent() );
this.props.onChange( this.savedContent );
}

isStartOfEditor() {
Expand Down Expand Up @@ -216,7 +217,8 @@ export default class Editable extends wp.element.Component {

updateContent() {
const bookmark = this.editor.selection.getBookmark( 2, true );
this.setContent( this.props.value );
this.savedContent = this.props.value;
this.setContent( this.savedContent );
this.editor.selection.moveToBookmark( bookmark );
// Saving the editor on updates avoid unecessary onChanges calls
// These calls can make the focus jump
Expand Down Expand Up @@ -266,10 +268,13 @@ export default class Editable extends wp.element.Component {
this.focus();
}

// The savedContent var allows us to avoid updating the content right after an onChange call
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow this. Why does the value of this.props.value differ from prevProps.value after the onChange? And if it does, why do we not want this to be reflected in the editable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you make a change on the editable. Say the old value is a and the new one is ab, we don't want to trigger the updateContent even if the old value prop and the new one are different. That's what I'm trying to achieve here by caching the savedContent.

if (
this.props.tagName === prevProps.tagName &&
this.props.value !== prevProps.value &&
! isEqual( this.props.value, prevProps.value )
this.props.value !== this.savedContent &&
! isEqual( this.props.value, prevProps.value ) &&
! isEqual( this.props.value, this.savedContent )
) {
this.updateContent();
}
Expand Down
5 changes: 3 additions & 2 deletions blocks/library/quote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ registerBlock( 'core/quote', {

edit( { attributes, setAttributes, focus, setFocus } ) {
const { value, citation, style = 1 } = attributes;
const focusedEditable = focus ? focus.editable || 'value' : null;

return (
<blockquote className={ `blocks-quote blocks-quote-style-${ style }` }>
Expand All @@ -52,7 +53,7 @@ registerBlock( 'core/quote', {
value: nextValue
} )
}
focus={ focus && focus.editable === 'value' ? focus : null }
focus={ focusedEditable === 'value' ? focus : null }
onFocus={ () => setFocus( { editable: 'value' } ) }
/>
{ ( citation || !! focus ) && (
Expand All @@ -64,7 +65,7 @@ registerBlock( 'core/quote', {
citation: nextCitation
} )
}
focus={ focus && focus.editable === 'citation' ? focus : null }
focus={ focusedEditable === 'citation' ? focus : null }
onFocus={ () => setFocus( { editable: 'citation' } ) }
/>
</footer>
Expand Down
8 changes: 4 additions & 4 deletions editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,13 @@ class VisualEditorBlock extends wp.element.Component {
wrapperProps = settings.getEditWrapperProps( block.attributes );
}

// Disable reason: Each block can receive focus but must be able to contain
// block children. Tab keyboard navigation enabled by tabIndex assignment.
// Disable reason: Each block can be selected by clicking on it
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes here break tab navigation:

  1. Focus text block
  2. Press tab

Expected: Image block is selected
Actual: Third block (next text block) is selected

The disable reason doesn't seem acceptable for accessibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 11d81e4

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the comment be reverted back to its previous text? Or at least updated to reflect that it accepts focus? Seems like suggesting "clicking on it" as reason for disabling an accessibility rule is destined to... provoke response 😄


/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role */
return (
<div
ref={ this.bindBlockNode }
tabIndex="0"
onClick={ onSelect }
onFocus={ onSelect }
onBlur={ this.maybeDeselect }
onKeyDown={ onStartTyping }
Expand All @@ -153,6 +152,7 @@ class VisualEditorBlock extends wp.element.Component {
onMouseLeave={ onMouseLeave }
className={ className }
data-type={ block.blockType }
tabIndex="0"
{ ...wrapperProps }
>
{ ( ( isSelected && ! isTyping ) || isHovered ) && <BlockMover uid={ block.uid } /> }
Expand Down
8 changes: 6 additions & 2 deletions editor/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,13 @@ export function selectedBlock( state = {}, action ) {
if ( ! action.selected ) {
return state.uid === action.uid ? {} : state;
}
return action.uid === state.uid
return action.uid === state.uid && ! state.typing
? state
: { uid: action.uid, typing: false, focus: {} };
: {
uid: action.uid,
typing: false,
focus: action.uid === state.uid ? state.focus : {}
};

case 'MOVE_BLOCK_UP':
case 'MOVE_BLOCK_DOWN':
Expand Down
19 changes: 17 additions & 2 deletions editor/test/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ describe( 'state', () => {
expect( state ).to.eql( { uid: 'kumquat', typing: false, focus: {} } );
} );

it( 'should not update the state if already selected', () => {
const original = deepFreeze( { uid: 'kumquat', typing: true, focus: {} } );
it( 'should not update the state if already selected and not typing', () => {
const original = deepFreeze( { uid: 'kumquat', typing: false, focus: {} } );
const state = selectedBlock( original, {
type: 'TOGGLE_BLOCK_SELECTED',
uid: 'kumquat',
Expand All @@ -269,6 +269,21 @@ describe( 'state', () => {
expect( state ).to.equal( original );
} );

it( 'should update the state if already selected and typing', () => {
const original = deepFreeze( { uid: 'kumquat', typing: true, focus: { editable: 'content' } } );
const state = selectedBlock( original, {
type: 'TOGGLE_BLOCK_SELECTED',
uid: 'kumquat',
selected: true
} );

expect( state ).to.eql( {
uid: 'kumquat',
typing: false,
focus: { editable: 'content' }
} );
} );

it( 'should unselect the block if currently selected', () => {
const original = deepFreeze( { uid: 'kumquat', typing: true, focus: {} } );
const state = selectedBlock( original, {
Expand Down