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

Editable: Split caption if empty #2664

Closed
wants to merge 1 commit 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
16 changes: 13 additions & 3 deletions blocks/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,17 @@ export default class Editable extends Component {
onBeforePastePreProcess( event ) {
// Allows us to ask for this information when we get a report.
window.console.log( 'Received HTML:\n\n', event.content );
const inlinePaste = ! this.props.onSplit || this.props.splitIfEmpty;

const content = pasteHandler( {
content: event.content,
inline: ! this.props.onSplit,
inline: inlinePaste,
} );

if ( typeof content === 'string' ) {
// Let MCE process further with the given content.
event.content = content;
} else if ( this.props.onSplit ) {
} else if ( ! inlinePaste ) {
// Abort pasting to split the content
event.preventDefault();

Expand Down Expand Up @@ -321,6 +322,15 @@ export default class Editable extends Component {
// If we click shift+Enter on inline Editables, we avoid creating two contenteditables
// We also split the content and call the onSplit prop if provided.
if ( event.keyCode === ENTER ) {
// If the editable is empty and splitIfEmpty equal true call onSplit
if ( this.props.onSplit && this.props.splitIfEmpty ) {
if ( this.state.empty ) {
event.preventDefault();
this.props.onSplit();
}
return;
}

if ( this.props.multiline ) {
if ( ! this.props.onSplit ) {
return;
Expand Down Expand Up @@ -397,7 +407,7 @@ export default class Editable extends Component {
}

onNewBlock() {
if ( this.props.multiline !== 'p' || ! this.props.onSplit ) {
if ( this.props.multiline !== 'p' || ! this.props.onSplit || this.splitIfEmpty ) {
return;
}

Expand Down
10 changes: 10 additions & 0 deletions blocks/library/image/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import BlockAlignmentToolbar from '../../block-alignment-toolbar';
import BlockDescription from '../../block-description';
import UrlInputButton from '../../url-input/button';
import ImageSize from './image-size';
import { createBlock } from '../../api';

class ImageBlock extends Component {
constructor() {
Expand All @@ -35,6 +36,7 @@ class ImageBlock extends Component {
this.updateAlignment = this.updateAlignment.bind( this );
this.onSelectImage = this.onSelectImage.bind( this );
this.onSetHref = this.onSetHref.bind( this );
this.onSplitCaption = this.onSplitCaption.bind( this );
this.updateImageSize = this.updateImageSize.bind( this );
this.state = {
availableSizes: {},
Expand Down Expand Up @@ -62,6 +64,12 @@ class ImageBlock extends Component {
this.props.setAttributes( { href: value } );
}

onSplitCaption() {
this.props.insertBlocksAfter(
createBlock( 'core/paragraph' ),
);
}

updateAlt( newAlt ) {
this.props.setAttributes( { alt: newAlt } );
}
Expand Down Expand Up @@ -247,6 +255,8 @@ class ImageBlock extends Component {
focus={ focus && focus.editable === 'caption' ? focus : undefined }
onFocus={ focusCaption }
onChange={ ( value ) => setAttributes( { caption: value } ) }
onSplit={ this.onSplitCaption }
splitIfEmpty
inlineToolbar
/>
) : null }
Expand Down