Skip to content

Commit

Permalink
Editor Module: Replace connect usage with withSelect/withDispatch (1s…
Browse files Browse the repository at this point in the history
…t round) (#6142)
  • Loading branch information
youknowriad authored Apr 13, 2018
1 parent e0342fa commit 751245a
Show file tree
Hide file tree
Showing 22 changed files with 323 additions and 381 deletions.
34 changes: 12 additions & 22 deletions editor/components/autosave-monitor/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';

/**
* Internal dependencies
*/
import { autosave } from '../../store/actions';
import {
isEditedPostDirty,
isEditedPostSaveable,
} from '../../store/selectors';
import { Component, compose } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';

export class AutosaveMonitor extends Component {
componentDidUpdate( prevProps ) {
Expand Down Expand Up @@ -46,12 +33,15 @@ export class AutosaveMonitor extends Component {
}
}

export default connect(
( state ) => {
export default compose( [
withSelect( ( select ) => {
const { isEditedPostDirty, isEditedPostSaveable } = select( 'core/editor' );
return {
isDirty: isEditedPostDirty( state ),
isSaveable: isEditedPostSaveable( state ),
isDirty: isEditedPostDirty(),
isSaveable: isEditedPostSaveable(),
};
},
{ autosave }
)( AutosaveMonitor );
} ),
withDispatch( ( dispatch ) => ( {
autosave: dispatch( 'core/editor' ).autosave,
} ) ),
] )( AutosaveMonitor );
12 changes: 6 additions & 6 deletions editor/components/block-inspector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { isEmpty } from 'lodash';
import { connect } from 'react-redux';

/**
* WordPress dependencies
Expand All @@ -15,13 +14,13 @@ import {
InspectorAdvancedControls,
} from '@wordpress/blocks';
import { PanelBody } from '@wordpress/components';
import { withSelect } from '@wordpress/data';

/**
* Internal Dependencies
*/
import './style.scss';
import SkipToSelectedBlock from '../skip-to-selected-block';
import { getSelectedBlock, getSelectedBlockCount } from '../../store/selectors';

const BlockInspector = ( { selectedBlock, count } ) => {
if ( count > 1 ) {
Expand Down Expand Up @@ -60,11 +59,12 @@ const BlockInspector = ( { selectedBlock, count } ) => {
];
};

export default connect(
( state ) => {
export default withSelect(
( select ) => {
const { getSelectedBlock, getSelectedBlockCount } = select( 'core/editor' );
return {
selectedBlock: getSelectedBlock( state ),
count: getSelectedBlockCount( state ),
selectedBlock: getSelectedBlock(),
count: getSelectedBlockCount(),
};
}
)( BlockInspector );
31 changes: 13 additions & 18 deletions editor/components/block-list/block-html.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
/**
* WordPress Dependencies
*/
import { isEqual } from 'lodash';
import { Component } from '@wordpress/element';
import { getBlockAttributes, getBlockContent, getBlockType, isValidBlock } from '@wordpress/blocks';

/**
* External Dependencies
*/
import { connect } from 'react-redux';
import TextareaAutosize from 'react-autosize-textarea';
import { isEqual } from 'lodash';

/**
* Internal Dependencies
* WordPress Dependencies
*/
import { updateBlock } from '../../store/actions';
import { getBlock } from '../../store/selectors';
import { Component, compose } from '@wordpress/element';
import { getBlockAttributes, getBlockContent, getBlockType, isValidBlock } from '@wordpress/blocks';
import { withSelect, withDispatch } from '@wordpress/data';

class BlockHTML extends Component {
constructor( props ) {
Expand Down Expand Up @@ -59,13 +54,13 @@ class BlockHTML extends Component {
}
}

export default connect(
( state, ownProps ) => ( {
block: getBlock( state, ownProps.uid ),
} ),
{
export default compose( [
withSelect( ( select, ownProps ) => ( {
block: select( 'core/editor' ).getBlock( ownProps.uid ),
} ) ),
withDispatch( dispatch => ( {
onChange( uid, attributes, originalContent, isValid ) {
return updateBlock( uid, { attributes, originalContent, isValid } );
dispatch( 'core/editor' ).updateBlock( uid, { attributes, originalContent, isValid } );
},
}
)( BlockHTML );
} ) ),
] )( BlockHTML );
72 changes: 33 additions & 39 deletions editor/components/block-list/insertion-point.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { isUnmodifiedDefaultBlock } from '@wordpress/blocks';
import { Component, compose } from '@wordpress/element';
import { ifCondition, withContext } from '@wordpress/components';

/**
* Internal dependencies
*/
import {
getBlockIndex,
getBlockInsertionPoint,
isBlockInsertionPointVisible,
getBlock,
isTyping,
} from '../../store/selectors';
import {
insertDefaultBlock,
startTyping,
} from '../../store/actions';
import { withSelect, withDispatch } from '@wordpress/data';

class BlockInsertionPoint extends Component {
constructor() {
super( ...arguments );
this.onClick = this.onClick.bind( this );
}

onClick() {
const { layout, rootUID, index, ...props } = this.props;
props.insertDefaultBlock( { layout }, rootUID, index );
Expand All @@ -57,24 +39,36 @@ class BlockInsertionPoint extends Component {
export default compose(
withContext( 'editor' )( ( { templateLock } ) => ( { templateLock } ) ),
ifCondition( ( { templateLock } ) => ! templateLock ),
connect(
( state, { uid, rootUID } ) => {
const blockIndex = uid ? getBlockIndex( state, uid, rootUID ) : -1;
const insertIndex = blockIndex;
const insertionPoint = getBlockInsertionPoint( state );
const block = uid ? getBlock( state, uid ) : null;
withSelect( ( select, { uid, rootUID } ) => {
const {
getBlockIndex,
getBlockInsertionPoint,
getBlock,
isBlockInsertionPointVisible,
isTyping,
} = select( 'core/editor' );
const blockIndex = uid ? getBlockIndex( uid, rootUID ) : -1;
const insertIndex = blockIndex;
const insertionPoint = getBlockInsertionPoint();
const block = uid ? getBlock( uid ) : null;
const showInsertionPoint = (
isBlockInsertionPointVisible() &&
insertionPoint.index === insertIndex &&
insertionPoint.rootUID === rootUID &&
( ! block || ! isUnmodifiedDefaultBlock( block ) )
);

return {
showInsertionPoint: (
isBlockInsertionPointVisible( state ) &&
insertionPoint.index === insertIndex &&
insertionPoint.rootUID === rootUID &&
( ! block || ! isUnmodifiedDefaultBlock( block ) )
),
showInserter: ! isTyping( state ),
index: insertIndex,
};
},
{ insertDefaultBlock, startTyping }
)
return {
showInserter: ! isTyping(),
index: insertIndex,
showInsertionPoint,
};
} ),
withDispatch( ( dispatch ) => {
const { insertDefaultBlock, startTyping } = dispatch( 'core/editor' );
return {
insertDefaultBlock,
startTyping,
};
} )
)( BlockInsertionPoint );
25 changes: 10 additions & 15 deletions editor/components/block-list/invalid-block-warning.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

/**
* WordPress dependencies
*/
Expand All @@ -13,11 +8,11 @@ import {
createBlock,
rawHandler,
} from '@wordpress/blocks';
import { withDispatch } from '@wordpress/data';

/**
* Internal dependencies
*/
import { replaceBlock } from '../../store/actions';
import Warning from '../warning';

function InvalidBlockWarning( { convertToHTML, convertToBlocks } ) {
Expand All @@ -41,19 +36,19 @@ function InvalidBlockWarning( { convertToHTML, convertToBlocks } ) {
);
}

export default connect(
null,
( dispatch, { block } ) => ( {
export default withDispatch( ( dispatch, { block } ) => {
const { replaceBlock } = dispatch( 'core/editor' );
return {
convertToHTML() {
dispatch( replaceBlock( block.uid, createBlock( 'core/html', {
replaceBlock( block.uid, createBlock( 'core/html', {
content: block.originalContent,
} ) ) );
} ) );
},
convertToBlocks() {
dispatch( replaceBlock( block.uid, rawHandler( {
replaceBlock( block.uid, rawHandler( {
HTML: block.originalContent,
mode: 'BLOCKS',
} ) ) );
} ) );
},
} )
)( InvalidBlockWarning );
};
} )( InvalidBlockWarning );
69 changes: 35 additions & 34 deletions editor/components/block-list/layout.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';
import {
findLast,
map,
Expand All @@ -17,7 +16,8 @@ import 'element-closest';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { Component, compose } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -26,13 +26,6 @@ import './style.scss';
import BlockListBlock from './block';
import IgnoreNestedEvents from './ignore-nested-events';
import DefaultBlockAppender from '../default-block-appender';
import {
isSelectionEnabled,
isMultiSelecting,
getMultiSelectedBlocksStartUid,
getMultiSelectedBlocksEndUid,
} from '../../store/selectors';
import { startMultiSelect, stopMultiSelect, multiSelect, selectBlock } from '../../store/actions';

class BlockListLayout extends Component {
constructor( props ) {
Expand Down Expand Up @@ -243,29 +236,37 @@ class BlockListLayout extends Component {
}
}

export default connect(
( state ) => ( {
// Reference block selection value directly, since current selectors
// assume either multi-selection (getMultiSelectedBlocksStartUid) or
// singular-selection (getSelectedBlock) exclusively.
selectionStart: getMultiSelectedBlocksStartUid( state ),
selectionEnd: getMultiSelectedBlocksEndUid( state ),
selectionStartUID: state.blockSelection.start,
isSelectionEnabled: isSelectionEnabled( state ),
isMultiSelecting: isMultiSelecting( state ),
export default compose( [
withSelect( ( select ) => {
const {
isSelectionEnabled,
isMultiSelecting,
getMultiSelectedBlocksStartUid,
getMultiSelectedBlocksEndUid,
getBlockSelectionStart,
} = select( 'core/editor' );

return {
selectionStart: getMultiSelectedBlocksStartUid(),
selectionEnd: getMultiSelectedBlocksEndUid(),
selectionStartUID: getBlockSelectionStart(),
isSelectionEnabled: isSelectionEnabled(),
isMultiSelecting: isMultiSelecting(),
};
} ),
withDispatch( ( dispatch ) => {
const {
startMultiSelect,
stopMultiSelect,
multiSelect,
selectBlock,
} = dispatch( 'core/editor' );

return {
onStartMultiSelect: startMultiSelect,
onStopMultiSelect: stopMultiSelect,
onMultiSelect: multiSelect,
onSelect: selectBlock,
};
} ),
( dispatch ) => ( {
onStartMultiSelect() {
dispatch( startMultiSelect() );
},
onStopMultiSelect() {
dispatch( stopMultiSelect() );
},
onMultiSelect( start, end ) {
dispatch( multiSelect( start, end ) );
},
onSelect( uid ) {
dispatch( selectBlock( uid ) );
},
} )
)( BlockListLayout );
] )( BlockListLayout );
Loading

0 comments on commit 751245a

Please sign in to comment.