Skip to content

Commit

Permalink
Post Title: Move selection at the end after pasting over the text (#6…
Browse files Browse the repository at this point in the history
…4665)


Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: ironprogrammer <ironprogrammer@git.wordpress.org>
Co-authored-by: annezazu <annezazu@git.wordpress.org>
Co-authored-by: MadtownLems <madtownlems@git.wordpress.org>
  • Loading branch information
5 people authored Aug 22, 2024
1 parent 3ce5053 commit 5266ed8
Showing 1 changed file with 30 additions and 40 deletions.
70 changes: 30 additions & 40 deletions packages/editor/src/components/post-title/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { pasteHandler } from '@wordpress/blocks';
import {
__unstableUseRichText as useRichText,
create,
toHTMLString,
insert,
} from '@wordpress/rich-text';
import { useMergeRefs } from '@wordpress/compose';
Expand Down Expand Up @@ -50,9 +49,35 @@ function PostTitle( _, forwardedRef ) {
const { clearSelectedBlock, insertBlocks, insertDefaultBlock } =
useDispatch( blockEditorStore );

function onChange( value ) {
onUpdate( value.replace( REGEXP_NEWLINES, ' ' ) );
}
const decodedPlaceholder =
decodeEntities( placeholder ) || __( 'Add title' );

const {
value,
onChange,
ref: richTextRef,
} = useRichText( {
value: title,
onChange( newValue ) {
onUpdate( newValue.replace( REGEXP_NEWLINES, ' ' ) );
},
placeholder: decodedPlaceholder,
selectionStart: selection.start,
selectionEnd: selection.end,
onSelectionChange( newStart, newEnd ) {
setSelection( ( sel ) => {
const { start, end } = sel;
if ( start === newStart && end === newEnd ) {
return sel;
}
return {
start: newStart,
end: newEnd,
};
} );
},
__unstableDisableFormats: false,
} );

function onInsertBlockAfter( blocks ) {
insertBlocks( blocks, 0 );
Expand Down Expand Up @@ -130,49 +155,14 @@ function PostTitle( _, forwardedRef ) {
onInsertBlockAfter( content );
}
} else {
const value = {
...create( { html: title } ),
...selection,
};

// Strip HTML to avoid unwanted HTML being added to the title.
// In the majority of cases it is assumed that HTML in the title
// is undesirable.
const contentNoHTML = stripHTML( content );

const newValue = insert( value, create( { html: contentNoHTML } ) );
onUpdate( toHTMLString( { value: newValue } ) );
setSelection( {
start: newValue.start,
end: newValue.end,
} );
onChange( insert( value, create( { html: contentNoHTML } ) ) );
}
}

const decodedPlaceholder =
decodeEntities( placeholder ) || __( 'Add title' );

const { ref: richTextRef } = useRichText( {
value: title,
onChange,
placeholder: decodedPlaceholder,
selectionStart: selection.start,
selectionEnd: selection.end,
onSelectionChange( newStart, newEnd ) {
setSelection( ( sel ) => {
const { start, end } = sel;
if ( start === newStart && end === newEnd ) {
return sel;
}
return {
start: newStart,
end: newEnd,
};
} );
},
__unstableDisableFormats: false,
} );

// The wp-block className is important for editor styles.
// This same block is used in both the visual and the code editor.
const className = clsx( DEFAULT_CLASSNAMES, {
Expand Down

0 comments on commit 5266ed8

Please sign in to comment.