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

Post Title: Move selection at the end after pasting over the text #64665

Merged
merged 1 commit into from
Aug 22, 2024
Merged
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
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
Loading