From 5e4cd27bf0d3bde8c4421f20a070012aa8bfff50 Mon Sep 17 00:00:00 2001 From: Paul Von Schrottky Date: Mon, 6 Dec 2021 17:56:51 -0300 Subject: [PATCH 1/3] Split pasted content between title and body --- .../src/components/post-title/index.native.js | 46 +++++++++++++------ 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/packages/editor/src/components/post-title/index.native.js b/packages/editor/src/components/post-title/index.native.js index 4e117f73fb315b..a0f85698b9e5db 100644 --- a/packages/editor/src/components/post-title/index.native.js +++ b/packages/editor/src/components/post-title/index.native.js @@ -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'; @@ -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 @@ -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 ); + } } } @@ -184,6 +191,7 @@ export default compose( return { postType: getEditedPostAttribute( 'type' ), + title: getEditedPostAttribute( 'title' ), isAnyBlockSelected: !! selectedId, isSelected: isPostTitleSelected(), isDimmed: selectionIsNested, @@ -191,13 +199,15 @@ export default compose( }; } ), 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() { @@ -212,6 +222,12 @@ export default compose( onUnselect() { togglePostTitleSelection( false ); }, + onUpdate( title ) { + editPost( { title } ); + }, + onInsertBlockAfter( blocks ) { + insertBlocks( blocks, 0 ); + }, }; } ), withInstanceId, From fbbd0e57d97723af8ed0ea2b3460f26bf9ae47bc Mon Sep 17 00:00:00 2001 From: Paul Von Schrottky Date: Wed, 15 Dec 2021 22:51:16 -0300 Subject: [PATCH 2/3] Fix pasting in plain strings in post title --- .../src/components/post-title/index.native.js | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/editor/src/components/post-title/index.native.js b/packages/editor/src/components/post-title/index.native.js index a0f85698b9e5db..cecd6944d88b3c 100644 --- a/packages/editor/src/components/post-title/index.native.js +++ b/packages/editor/src/components/post-title/index.native.js @@ -8,7 +8,11 @@ import { isEmpty } from 'lodash'; * WordPress dependencies */ import { Component } from '@wordpress/element'; -import { __experimentalRichText as RichText } from '@wordpress/rich-text'; +import { + __experimentalRichText as RichText, + create, + insert, +} from '@wordpress/rich-text'; import { decodeEntities } from '@wordpress/html-entities'; import { withDispatch, withSelect } from '@wordpress/data'; import { withFocusOutside } from '@wordpress/components'; @@ -58,7 +62,7 @@ class PostTitle extends Component { this.props.onSelect(); } - onPaste( { html, plainText } ) { + onPaste( { value, onChange, plainText, html } ) { const { title, onInsertBlockAfter, onUpdate } = this.props; const content = pasteHandler( { @@ -66,17 +70,22 @@ class PostTitle extends Component { plainText, } ); - 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 ) ); + if ( content.length ) { + if ( typeof content === 'string' ) { + const valueToInsert = create( { html: content } ); + onChange( insert( value, valueToInsert ) ); } else { - onInsertBlockAfter( content ); + const [ firstBlock ] = content; + if ( + ! title && + ( firstBlock.name === 'core/heading' || + firstBlock.name === 'core/paragraph' ) + ) { + onUpdate( firstBlock.attributes.content ); + onInsertBlockAfter( content.slice( 1 ) ); + } else { + onInsertBlockAfter( content ); + } } } } From c4e48f9fcfc8f34697ad923144c6a439d11a0c93 Mon Sep 17 00:00:00 2001 From: Derek Blank Date: Thu, 13 Apr 2023 15:54:12 +1000 Subject: [PATCH 3/3] Update prettier formatting to fix linter errors --- .../editor/src/components/post-title/index.native.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/packages/editor/src/components/post-title/index.native.js b/packages/editor/src/components/post-title/index.native.js index 88ad63e815cd5c..0b58ee774b699e 100644 --- a/packages/editor/src/components/post-title/index.native.js +++ b/packages/editor/src/components/post-title/index.native.js @@ -205,15 +205,11 @@ export default compose( }; } ), withDispatch( ( dispatch ) => { - const { undo, redo, togglePostTitleSelection, editPost } = dispatch( - editorStore - ); + const { undo, redo, togglePostTitleSelection, editPost } = + dispatch( editorStore ); - const { - clearSelectedBlock, - insertDefaultBlock, - insertBlocks, - } = dispatch( blockEditorStore ); + const { clearSelectedBlock, insertDefaultBlock, insertBlocks } = + dispatch( blockEditorStore ); return { onEnterPress() {