Skip to content

Commit

Permalink
Split pasted content between title and body
Browse files Browse the repository at this point in the history
  • Loading branch information
guarani committed Dec 13, 2021
1 parent 3660a92 commit 5e4cd27
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions packages/editor/src/components/post-title/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import { isEmpty } from 'lodash';
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import {
__experimentalRichText as RichText,
create,
insert,
} from '@wordpress/rich-text';
import { __experimentalRichText as RichText } from '@wordpress/rich-text';
import { decodeEntities } from '@wordpress/html-entities';
import { withDispatch, withSelect } from '@wordpress/data';
import { withFocusOutside } from '@wordpress/components';
Expand All @@ -32,6 +28,7 @@ class PostTitle extends Component {
super( props );

this.setRef = this.setRef.bind( this );
this.onPaste = this.onPaste.bind( this );
}
componentDidUpdate( prevProps ) {
// Unselect if any other block is selected and blur the RichText
Expand Down Expand Up @@ -61,16 +58,26 @@ class PostTitle extends Component {
this.props.onSelect();
}

onPaste( { value, onChange, plainText } ) {
onPaste( { html, plainText } ) {
const { title, onInsertBlockAfter, onUpdate } = this.props;

const content = pasteHandler( {
HTML: html,
plainText,
mode: 'INLINE',
tagName: 'p',
} );

if ( typeof content === 'string' ) {
const valueToInsert = create( { html: content } );
onChange( insert( value, valueToInsert ) );
if ( typeof content !== 'string' && content.length ) {
const [ firstBlock ] = content;
if (
! title &&
( firstBlock.name === 'core/heading' ||
firstBlock.name === 'core/paragraph' )
) {
onUpdate( firstBlock.attributes.content );
onInsertBlockAfter( content.slice( 1 ) );
} else {
onInsertBlockAfter( content );
}
}
}

Expand Down Expand Up @@ -184,20 +191,23 @@ export default compose(

return {
postType: getEditedPostAttribute( 'type' ),
title: getEditedPostAttribute( 'title' ),
isAnyBlockSelected: !! selectedId,
isSelected: isPostTitleSelected(),
isDimmed: selectionIsNested,
globalStyles,
};
} ),
withDispatch( ( dispatch ) => {
const { undo, redo, togglePostTitleSelection } = dispatch(
const { undo, redo, togglePostTitleSelection, editPost } = dispatch(
editorStore
);

const { clearSelectedBlock, insertDefaultBlock } = dispatch(
blockEditorStore
);
const {
clearSelectedBlock,
insertDefaultBlock,
insertBlocks,
} = dispatch( blockEditorStore );

return {
onEnterPress() {
Expand All @@ -212,6 +222,12 @@ export default compose(
onUnselect() {
togglePostTitleSelection( false );
},
onUpdate( title ) {
editPost( { title } );
},
onInsertBlockAfter( blocks ) {
insertBlocks( blocks, 0 );
},
};
} ),
withInstanceId,
Expand Down

0 comments on commit 5e4cd27

Please sign in to comment.