Skip to content

Commit

Permalink
Block: Refactor insertion point within sibling inserter
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Oct 13, 2017
1 parent 6477a7a commit 82c3e27
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 47 deletions.
57 changes: 14 additions & 43 deletions editor/modes/visual-editor/block-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import VisualEditorSiblingInserter from './sibling-inserter';
import BlockDropZone from './block-drop-zone';
import {
getBlockUids,
getBlockInsertionPoint,
isBlockInsertionPointVisible,
getMultiSelectedBlocksStartUid,
getMultiSelectedBlocksEndUid,
getMultiSelectedBlocks,
Expand All @@ -29,8 +27,6 @@ import {
} from '../../selectors';
import { insertBlock, startMultiSelect, stopMultiSelect, multiSelect, selectBlock } from '../../actions';

const INSERTION_POINT_PLACEHOLDER = '[[insertion-point]]';

class VisualEditorBlockList extends Component {
constructor( props ) {
super( props );
Expand Down Expand Up @@ -196,47 +192,24 @@ class VisualEditorBlockList extends Component {
}

render() {
const {
blocks,
showInsertionPoint,
insertionPoint,
} = this.props;

const blocksWithInsertionPoint = showInsertionPoint
? [
...blocks.slice( 0, insertionPoint ),
INSERTION_POINT_PLACEHOLDER,
...blocks.slice( insertionPoint ),
]
: blocks;
const { blocks } = this.props;

return (
<div>
{ !! blocks.length && <VisualEditorSiblingInserter insertIndex={ 0 } /> }
{ !! blocks.length && blocksWithInsertionPoint.map( ( uid, index ) => {
if ( uid === INSERTION_POINT_PLACEHOLDER ) {
return (
<div
key={ INSERTION_POINT_PLACEHOLDER }
className="editor-visual-editor__insertion-point"
/>
);
}

return [
<VisualEditorBlock
key={ 'block-' + uid }
uid={ uid }
blockRef={ ( ref ) => this.setBlockRef( ref, uid ) }
onSelectionStart={ this.onSelectionStart }
onShiftSelection={ this.onShiftSelection }
/>,
<VisualEditorSiblingInserter
key={ 'sibling-inserter-' + uid }
insertIndex={ index + 1 }
/>,
];
} ) }
{ blocks.map( ( uid, index ) => [
<VisualEditorBlock
key={ 'block-' + uid }
uid={ uid }
blockRef={ ( ref ) => this.setBlockRef( ref, uid ) }
onSelectionStart={ this.onSelectionStart }
onShiftSelection={ this.onShiftSelection }
/>,
<VisualEditorSiblingInserter
key={ 'sibling-inserter-' + uid }
insertIndex={ index + 1 }
/>,
] ) }
{ ! blocks.length &&
<div className="editor-visual-editor__placeholder">
<BlockDropZone />
Expand All @@ -258,8 +231,6 @@ class VisualEditorBlockList extends Component {
export default connect(
( state ) => ( {
blocks: getBlockUids( state ),
insertionPoint: getBlockInsertionPoint( state ),
showInsertionPoint: isBlockInsertionPointVisible( state ),
selectionStart: getMultiSelectedBlocksStartUid( state ),
selectionEnd: getMultiSelectedBlocksEndUid( state ),
multiSelectedBlocks: getMultiSelectedBlocks( state ),
Expand Down
23 changes: 20 additions & 3 deletions editor/modes/visual-editor/sibling-inserter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';
import classnames from 'classnames';

/**
Expand All @@ -13,6 +14,10 @@ import { focus } from '@wordpress/utils';
* Internal dependencies
*/
import Inserter from '../../inserter';
import {
getBlockInsertionPoint,
isBlockInsertionPointVisible,
} from '../../selectors';

class VisualEditorSiblingInserter extends Component {
constructor() {
Expand Down Expand Up @@ -82,11 +87,11 @@ class VisualEditorSiblingInserter extends Component {
}

render() {
const { insertIndex } = this.props;
const { insertIndex, showInsertionPoint } = this.props;
const { isVisible } = this.state;

const classes = classnames( 'editor-visual-editor__sibling-inserter', {
'is-visible': isVisible,
'is-visible': isVisible || showInsertionPoint,
} );

return (
Expand All @@ -99,6 +104,9 @@ class VisualEditorSiblingInserter extends Component {
onMouseEnter={ this.show }
onMouseLeave={ this.hide }
tabIndex={ 0 }>
{ showInsertionPoint && (
<div className="editor-visual-editor__insertion-point" />
) }
{ isVisible && [
<hr
key="rule"
Expand All @@ -116,4 +124,13 @@ class VisualEditorSiblingInserter extends Component {
}
}

export default VisualEditorSiblingInserter;
export default connect(
( state, ownProps ) => {
return {
showInsertionPoint: (
isBlockInsertionPointVisible( state ) &&
getBlockInsertionPoint( state ) === ownProps.insertIndex
),
};
}
)( VisualEditorSiblingInserter );
2 changes: 1 addition & 1 deletion editor/modes/visual-editor/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@

&:before {
position: absolute;
top: -3px;
top: -1px;
height: 2px;
left: 0;
right: 0;
Expand Down

0 comments on commit 82c3e27

Please sign in to comment.