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

Blocks: Avoid setAttributes on end-of-paragraph split #7482

Merged
merged 4 commits into from
Jun 29, 2018
Merged
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
63 changes: 46 additions & 17 deletions core-blocks/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ const FONT_SIZES = [
class ParagraphBlock extends Component {
constructor() {
super( ...arguments );

this.onReplace = this.onReplace.bind( this );
this.toggleDropCap = this.toggleDropCap.bind( this );
this.getFontSize = this.getFontSize.bind( this );
this.setFontSize = this.setFontSize.bind( this );
this.splitBlock = this.splitBlock.bind( this );
}

onReplace( blocks ) {
Expand Down Expand Up @@ -137,11 +139,53 @@ class ParagraphBlock extends Component {
} );
}

/**
* Split handler for RichText value, namely when content is pasted or the
* user presses the Enter key.
*
* @param {?Array} before Optional before value, to be used as content
* in place of what exists currently for the
* block. If undefined, the block is deleted.
* @param {?Array} after Optional after value, to be appended in a new
* paragraph block to the set of blocks passed
* as spread.
* @param {...WPBlock} blocks Optional blocks inserted between the before
* and after value blocks.
*/
splitBlock( before, after, ...blocks ) {
const {
attributes,
insertBlocksAfter,
setAttributes,
onReplace,
} = this.props;

if ( after ) {
// Append "After" content as a new paragraph block to the end of
// any other blocks being inserted after the current paragraph.
blocks.push( createBlock( name, { content: after } ) );
}

if ( blocks.length && insertBlocksAfter ) {
insertBlocksAfter( blocks );
}

const { content } = attributes;
if ( ! before ) {
// If before content is omitted, treat as intent to delete block.
onReplace( [] );
} else if ( content !== before ) {
// Only update content if it has in-fact changed. In case that user
// has created a new paragraph at end of an existing one, the value
// of before will be strictly equal to the current content.
setAttributes( { content: before } );
}
}

render() {
const {
attributes,
setAttributes,
insertBlocksAfter,
mergeBlocks,
onReplace,
className,
Expand Down Expand Up @@ -230,22 +274,7 @@ class ParagraphBlock extends Component {
content: nextContent,
} );
} }
onSplit={ insertBlocksAfter ?
( before, after, ...blocks ) => {
if ( after ) {
blocks.push( createBlock( name, { content: after } ) );
}

insertBlocksAfter( blocks );

if ( before ) {
setAttributes( { content: before } );
} else {
onReplace( [] );
}
} :
undefined
}
onSplit={ this.splitBlock }
onMerge={ mergeBlocks }
onReplace={ this.onReplace }
onRemove={ () => onReplace( [] ) }
Expand Down
47 changes: 31 additions & 16 deletions editor/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,17 +573,20 @@ export class RichText extends Component {
* @param {Object} context The context for splitting.
*/
splitContent( blocks = [], context = {} ) {
if ( ! this.props.onSplit ) {
const { onSplit } = this.props;
if ( ! onSplit ) {
return;
}

const { dom } = this.editor;
const rootNode = this.editor.getBody();
const beforeRange = dom.createRng();
const afterRange = dom.createRng();
const selectionRange = this.editor.selection.getRng();

let before, after;
if ( rootNode.childNodes.length ) {
const { dom } = this.editor;
const beforeRange = dom.createRng();
const afterRange = dom.createRng();
const selectionRange = this.editor.selection.getRng();

beforeRange.setStart( rootNode, 0 );
beforeRange.setEnd( selectionRange.startContainer, selectionRange.startOffset );

Expand All @@ -594,20 +597,32 @@ export class RichText extends Component {
const afterFragment = afterRange.extractContents();

const { format } = this.props;
let before = domToFormat( filterEmptyNodes( beforeFragment.childNodes ), format, this.editor );
let after = domToFormat( filterEmptyNodes( afterFragment.childNodes ), format, this.editor );
before = domToFormat( filterEmptyNodes( beforeFragment.childNodes ), format, this.editor );
after = domToFormat( filterEmptyNodes( afterFragment.childNodes ), format, this.editor );
} else {
before = [];
after = [];
}

if ( context.paste ) {
before = this.isEmpty( before ) ? null : before;
after = this.isEmpty( after ) ? null : after;
}
// In case split occurs at the trailing or leading edge of the field,
// assume that the before/after values respectively reflect the current
// value. This also provides an opportunity for the parent component to
// determine whether the before/after value has changed using a trivial
// strict equality operation.
if ( this.isEmpty( after ) ) {
before = this.props.value;
} else if ( this.isEmpty( before ) ) {
after = this.props.value;
}

this.restoreContentAndSplit( before, after, blocks );
} else if ( context.paste ) {
this.restoreContentAndSplit( null, null, blocks );
} else {
this.restoreContentAndSplit( [], [], blocks );
// If pasting and the split would result in no content other than the
// pasted blocks, remove the before and after blocks.
if ( context.paste ) {
before = this.isEmpty( before ) ? null : before;
after = this.isEmpty( after ) ? null : after;
}

this.restoreContentAndSplit( before, after, blocks );
}

onNewBlock() {
Expand Down