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

[WIP] Core blocks: Refactor Gallery and Image blocks to avoid RichText warnings #6871

Closed
wants to merge 3 commits into from
Closed
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
1 change: 0 additions & 1 deletion core-blocks/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ class GalleryEdit extends Component {
if ( ! nextProps.isSelected && this.props.isSelected ) {
this.setState( {
selectedImage: null,
captionSelected: false,
} );
}
}
Expand Down
52 changes: 9 additions & 43 deletions core-blocks/gallery/gallery-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,14 @@ class GalleryImage extends Component {
constructor() {
super( ...arguments );

this.onImageClick = this.onImageClick.bind( this );
this.onSelectCaption = this.onSelectCaption.bind( this );
this.onKeyDown = this.onKeyDown.bind( this );
this.bindContainer = this.bindContainer.bind( this );

this.state = {
captionSelected: false,
};
}

bindContainer( ref ) {
this.container = ref;
}

onSelectCaption() {
if ( ! this.state.captionSelected ) {
this.setState( {
captionSelected: true,
} );
}

if ( ! this.props.isSelected ) {
this.props.onSelect();
}
}

onImageClick() {
if ( ! this.props.isSelected ) {
this.props.onSelect();
}

if ( this.state.captionSelected ) {
this.setState( {
captionSelected: false,
} );
}
}

onKeyDown( event ) {
if (
this.container === document.activeElement &&
Expand All @@ -71,21 +41,13 @@ class GalleryImage extends Component {
}
}

componentWillReceiveProps( { isSelected, image, url } ) {
componentWillReceiveProps( { image, url } ) {
if ( image && ! url ) {
this.props.setAttributes( {
url: image.source_url,
alt: image.alt_text,
} );
}

// unselect the caption so when the user selects other image and comeback
// the caption is not immediately selected
if ( this.state.captionSelected && ! isSelected && this.props.isSelected ) {
this.setState( {
captionSelected: false,
} );
}
}

render() {
Expand All @@ -105,7 +67,7 @@ class GalleryImage extends Component {
// Disable reason: Image itself is not meant to be
// interactive, but should direct image selection and unfocus caption fields
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/click-events-have-key-events
const img = url ? <img src={ url } alt={ alt } data-id={ id } onClick={ this.onImageClick } /> : <Spinner />;
const img = url ? <img src={ url } alt={ alt } data-id={ id } /> : <Spinner />;

const className = classnames( {
'is-selected': isSelected,
Expand All @@ -115,7 +77,13 @@ class GalleryImage extends Component {
// Disable reason: Each block can be selected by clicking on it and we should keep the same saved markup
/* eslint-disable jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
return (
<figure className={ className } tabIndex="-1" onKeyDown={ this.onKeyDown } ref={ this.bindContainer }>
<figure
className={ className }
tabIndex="-1"
onFocus={ this.props.onSelect }
onKeyDown={ this.onKeyDown }
ref={ this.bindContainer }
>
{ isSelected &&
<div className="core-blocks-gallery-item__inline-menu">
<IconButton
Expand All @@ -132,9 +100,7 @@ class GalleryImage extends Component {
tagName="figcaption"
placeholder={ __( 'Write caption…' ) }
value={ caption }
isSelected={ this.state.captionSelected }
onChange={ ( newCaption ) => setAttributes( { caption: newCaption } ) }
onFocus={ this.onSelectCaption }
inlineToolbar
/>
) : null }
Expand Down
34 changes: 1 addition & 33 deletions core-blocks/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,12 @@ class ImageEdit extends Component {
super( ...arguments );
this.updateAlt = this.updateAlt.bind( this );
this.updateAlignment = this.updateAlignment.bind( this );
this.onFocusCaption = this.onFocusCaption.bind( this );
this.onImageClick = this.onImageClick.bind( this );
this.onSelectImage = this.onSelectImage.bind( this );
this.onSetHref = this.onSetHref.bind( this );
this.updateImageURL = this.updateImageURL.bind( this );
this.updateWidth = this.updateWidth.bind( this );
this.updateHeight = this.updateHeight.bind( this );
this.updateDimensions = this.updateDimensions.bind( this );

this.state = {
captionFocused: false,
};
}

componentDidMount() {
Expand Down Expand Up @@ -99,14 +93,6 @@ class ImageEdit extends Component {
}
}

componentWillReceiveProps( { isSelected } ) {
if ( ! isSelected && this.props.isSelected && this.state.captionFocused ) {
this.setState( {
captionFocused: false,
} );
}
}

onSelectImage( media ) {
if ( ! media ) {
this.props.setAttributes( {
Expand All @@ -128,22 +114,6 @@ class ImageEdit extends Component {
this.props.setAttributes( { href: value } );
}

onFocusCaption() {
if ( ! this.state.captionFocused ) {
this.setState( {
captionFocused: true,
} );
}
}

onImageClick() {
if ( this.state.captionFocused ) {
this.setState( {
captionFocused: false,
} );
}
}

updateAlt( newAlt ) {
this.props.setAttributes( { alt: newAlt } );
}
Expand Down Expand Up @@ -332,7 +302,7 @@ class ImageEdit extends Component {
// Disable reason: Image itself is not meant to be
// interactive, but should direct focus to block
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
const img = <img src={ url } alt={ alt } onClick={ this.onImageClick } />;
const img = <img src={ url } alt={ alt } />;

if ( ! isResizable || ! imageWidthWithinContainer ) {
return (
Expand Down Expand Up @@ -393,9 +363,7 @@ class ImageEdit extends Component {
tagName="figcaption"
placeholder={ __( 'Write caption…' ) }
value={ caption || [] }
onFocus={ this.onFocusCaption }
onChange={ ( value ) => setAttributes( { caption: value } ) }
isSelected={ this.state.captionFocused }
inlineToolbar
/>
) : null }
Expand Down