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

Fix undo on paragraph split #6964

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
19 changes: 9 additions & 10 deletions core-blocks/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { isFinite, find, omit } from 'lodash';
import { isFinite, find, last, omit } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -84,7 +84,7 @@ class ParagraphBlock extends Component {
this.setFontSize = this.setFontSize.bind( this );
}

onReplace( blocks ) {
onReplace( blocks, blockToSelect ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this change only being applied to the paragraph block. Should it be generalised to all blocks that have an onSplit prop, such as headings?

const { attributes, onReplace } = this.props;
onReplace( blocks.map( ( block, index ) => (
index === 0 && block.name === name ?
Expand All @@ -93,9 +93,10 @@ class ParagraphBlock extends Component {
...attributes,
...block.attributes,
},
uid: this.props.id,
} :
block
) ) );
) ), blockToSelect );
}

toggleDropCap() {
Expand Down Expand Up @@ -236,14 +237,12 @@ class ParagraphBlock extends Component {
if ( after ) {
blocks.push( createBlock( name, { content: after } ) );
}
const replaceArrayStart = before ?
[ createBlock( name, { ...attributes, content: before } ) ] :
[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a subtle difference here with the previous behavior. We're creating a new block with a new UID which resets all other properties of the blocks and cause a remount instead of a rerender. Maybe it's fine, just noting.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true we change the uid and we have a remount but I think doing that is ok.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we reuse the block that is being replaced in the reducer if the name is the same?

Copy link
Member Author

@jorgefilipecosta jorgefilipecosta Jun 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had already an onReplace in the paragraph with logic to reuse attribute when replacing with the same block I added one line there that also reuses the UID. I think it simplifies the things and removes the need for any TinyMCE hacks.


insertBlocksAfter( blocks );

if ( before ) {
setAttributes( { content: before } );
} else {
onReplace( [] );
}
const newBlocks = replaceArrayStart.concat( blocks );
this.onReplace( newBlocks, newBlocks.length > 1 ? last( newBlocks ).uid : undefined );
} :
undefined
}
Expand Down
7 changes: 5 additions & 2 deletions editor/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,12 +684,15 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps ) => {
onMerge( ...args ) {
mergeBlocks( ...args );
},
onReplace( blocks ) {
onReplace( blocks, blockToSelect ) {
const { layout } = ownProps;
blocks = castArray( blocks ).map( ( block ) => (
cloneBlock( block, { layout } )
{ ...block, layout }
) );
replaceBlocks( [ ownProps.uid ], blocks );
if ( blockToSelect ) {
selectBlock( blockToSelect );
}
},
onMetaChange( meta ) {
editPost( { meta } );
Expand Down