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

Refactor BlockCompare block to use React hooks #22909

Merged
merged 3 commits into from
Jun 5, 2020
Merged
Changes from 1 commit
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
84 changes: 40 additions & 44 deletions packages/block-editor/src/components/block-compare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@ import { diffChars } from 'diff/lib/diff/character';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { getSaveContent, getSaveElement } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import BlockView from './block-view';

class BlockCompare extends Component {
getDifference( originalContent, newContent ) {
function BlockCompare( {
Copy link
Member

@ZebulanStanphill ZebulanStanphill Jun 4, 2020

Choose a reason for hiding this comment

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

Nitpick: you could do export default function here instead of later:

Suggested change
function BlockCompare( {
export default function BlockCompare( {

block,
onKeep,
onConvert,
convertor,
convertButtonText,
} ) {
const getDifference = ( originalContent, newContent ) => {
truchot marked this conversation as resolved.
Show resolved Hide resolved
const difference = diffChars( originalContent, newContent );

return difference.map( ( item, pos ) => {
Expand All @@ -35,18 +40,18 @@ class BlockCompare extends Component {
</span>
);
} );
}
};

getOriginalContent( block ) {
const getOriginalContent = () => {
return {
rawContent: block.originalContent,
renderedContent: getSaveElement( block.name, block.attributes ),
};
}
};

getConvertedContent( block ) {
const getConvertedContent = ( convertedBlock ) => {
// The convertor may return an array of items or a single item
const newBlocks = castArray( block );
const newBlocks = castArray( convertedBlock );

// Get converted block details
const newContent = newBlocks.map( ( item ) =>
Expand All @@ -60,45 +65,36 @@ class BlockCompare extends Component {
rawContent: newContent.join( '' ),
renderedContent,
};
}
};

render() {
const {
block,
onKeep,
onConvert,
convertor,
convertButtonText,
} = this.props;
const original = this.getOriginalContent( block );
const converted = this.getConvertedContent( convertor( block ) );
const difference = this.getDifference(
original.rawContent,
converted.rawContent
);
const original = getOriginalContent();
truchot marked this conversation as resolved.
Show resolved Hide resolved
const converted = getConvertedContent( convertor( block ) );
const difference = getDifference(
original.rawContent,
converted.rawContent
);

return (
<div className="block-editor-block-compare__wrapper">
<BlockView
title={ __( 'Current' ) }
className="block-editor-block-compare__current"
action={ onKeep }
actionText={ __( 'Convert to HTML' ) }
rawContent={ original.rawContent }
renderedContent={ original.renderedContent }
/>
return (
<div className="block-editor-block-compare__wrapper">
<BlockView
title={ __( 'Current' ) }
className="block-editor-block-compare__current"
action={ onKeep }
actionText={ __( 'Convert to HTML' ) }
rawContent={ original.rawContent }
renderedContent={ original.renderedContent }
/>

<BlockView
title={ __( 'After Conversion' ) }
className="block-editor-block-compare__converted"
action={ onConvert }
actionText={ convertButtonText }
rawContent={ difference }
renderedContent={ converted.renderedContent }
/>
</div>
);
}
<BlockView
title={ __( 'After Conversion' ) }
className="block-editor-block-compare__converted"
action={ onConvert }
actionText={ convertButtonText }
rawContent={ difference }
renderedContent={ converted.renderedContent }
/>
</div>
);
}

export default BlockCompare;