Skip to content

Commit

Permalink
Adding an inserter next to the last block
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jan 16, 2018
1 parent 119c136 commit 47f5b07
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 29 deletions.
41 changes: 25 additions & 16 deletions editor/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import BlockContextualToolbar from './block-contextual-toolbar';
import BlockMultiControls from './multi-controls';
import BlockMobileToolbar from './block-mobile-toolbar';
import BlockListSiblingInserter from './sibling-inserter';
import BlockListInsertionPoint from './insertion-point';
import {
clearSelectedBlock,
editPost,
Expand Down Expand Up @@ -63,6 +64,7 @@ import {
isSelectionEnabled,
isTyping,
getBlockMode,
getBlockCount,
} from '../../store/selectors';

const { BACKSPACE, ESCAPE, DELETE, ENTER, UP, RIGHT, DOWN, LEFT } = keycodes;
Expand Down Expand Up @@ -357,7 +359,7 @@ export class BlockListBlock extends Component {
}

render() {
const { block, order, mode, showContextualToolbar, isLocked } = this.props;
const { block, order, mode, showContextualToolbar, isLocked, isLast } = this.props;
const { name: blockName, isValid } = block;
const blockType = getBlockType( blockName );
// translators: %s: Type of block (i.e. Text, Image etc)
Expand All @@ -375,6 +377,7 @@ export class BlockListBlock extends Component {
'is-multi-selected': isMultiSelected,
'is-hovered': isHovered,
'is-reusable': isReusableBlock( blockType ),
'is-last': isLast,
} );

const { onMouseLeave, onFocus, onReplace } = this.props;
Expand Down Expand Up @@ -448,27 +451,33 @@ export class BlockListBlock extends Component {
</div>
{ !! error && <BlockCrashWarning /> }
<BlockListSiblingInserter uid={ block.uid } />
<BlockListInsertionPoint uid={ block.uid } />
</div>
);
/* eslint-enable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
}
}

const mapStateToProps = ( state, { uid } ) => ( {
previousBlock: getPreviousBlock( state, uid ),
nextBlock: getNextBlock( state, uid ),
block: getBlock( state, uid ),
isSelected: isBlockSelected( state, uid ),
isMultiSelected: isBlockMultiSelected( state, uid ),
isFirstMultiSelected: isFirstMultiSelectedBlock( state, uid ),
isHovered: isBlockHovered( state, uid ) && ! isMultiSelecting( state ),
focus: getBlockFocus( state, uid ),
isTyping: isTyping( state ),
order: getBlockIndex( state, uid ),
meta: getEditedPostAttribute( state, 'meta' ),
mode: getBlockMode( state, uid ),
isSelectionEnabled: isSelectionEnabled( state ),
} );
const mapStateToProps = ( state, { uid } ) => {
const index = getBlockIndex( state, uid );

return {
previousBlock: getPreviousBlock( state, uid ),
nextBlock: getNextBlock( state, uid ),
block: getBlock( state, uid ),
isSelected: isBlockSelected( state, uid ),
isMultiSelected: isBlockMultiSelected( state, uid ),
isFirstMultiSelected: isFirstMultiSelectedBlock( state, uid ),
isHovered: isBlockHovered( state, uid ) && ! isMultiSelecting( state ),
focus: getBlockFocus( state, uid ),
isTyping: isTyping( state ),
order: index,
meta: getEditedPostAttribute( state, 'meta' ),
mode: getBlockMode( state, uid ),
isSelectionEnabled: isSelectionEnabled( state ),
isLast: index === getBlockCount( state ) - 1,
};
};

const mapDispatchToProps = ( dispatch, ownProps ) => ( {
onChange( uid, attributes ) {
Expand Down
45 changes: 45 additions & 0 deletions editor/components/block-list/insertion-point.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

/**
* Internal dependencies
*/
import {
getBlockUids,
getBlockInsertionPoint,
isBlockInsertionPointVisible,
} from '../../store/selectors';
import {
clearSelectedBlock,
} from '../../store/actions';

/**
* Component showing the block's insertion point.
*
* @param {Object} props React props passed to the component.
* @returns {Object|false} Rendered insertion point.
*/
function BlockListInsertionPoint( { showInsertionPoint } ) {
return showInsertionPoint && (
<div className="editor-block-list__insertion-point" />
);
}

export default connect(
( state, { uid } ) => {
const blockIndex = uid ? getBlockUids( state ).indexOf( uid ) : -1;
const insertIndex = blockIndex > -1 ? blockIndex + 1 : 0;

return {
showInsertionPoint: (
isBlockInsertionPointVisible( state ) &&
getBlockInsertionPoint( state ) === insertIndex
),
};
},
{
clearSelectedBlock,
}
)( BlockListInsertionPoint );
13 changes: 2 additions & 11 deletions editor/components/block-list/sibling-inserter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import { Component } from '@wordpress/element';
import Inserter from '../inserter';
import {
getBlockUids,
getBlockInsertionPoint,
isBlockInsertionPointVisible,
isBlockWithinSelection,
} from '../../store/selectors';
import {
Expand Down Expand Up @@ -49,21 +47,18 @@ class BlockListSiblingInserter extends Component {
return null;
}

const { insertIndex, showInsertionPoint } = this.props;
const { insertIndex } = this.props;
const { isForcedVisible } = this.state;

const classes = classnames( 'editor-block-list__sibling-inserter', {
'is-forced-visible': isForcedVisible || showInsertionPoint,
'is-forced-visible': isForcedVisible,
} );

return (
<div
ref={ this.bindNode }
data-insert-index={ insertIndex }
className={ classes }>
{ showInsertionPoint && (
<div className="editor-block-list__insertion-point" />
) }
<Inserter
key="inserter"
position="bottom"
Expand All @@ -83,10 +78,6 @@ export default connect(
return {
shouldDisable: isBlockWithinSelection( state, uid ),
insertIndex,
showInsertionPoint: (
isBlockInsertionPointVisible( state ) &&
getBlockInsertionPoint( state ) === insertIndex
),
};
},
{
Expand Down
27 changes: 26 additions & 1 deletion editor/components/block-list/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,16 @@
}

.editor-block-list__insertion-point {
position: relative;
position: absolute;
bottom: 0;
top: auto;
left: 0;
right: 0;

@include break-small {
left: $block-mover-padding-visible;
right: $block-mover-padding-visible;
}

&:before {
position: absolute;
Expand Down Expand Up @@ -452,6 +461,22 @@
}
}

.editor-block-list__block.is-last > .editor-block-mover {
display: none;
}

.editor-block-list__block.is-last > .editor-block-list__sibling-inserter {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: $block-mover-padding-visible;
}

.editor-block-list__block.is-last.is-selected > .editor-block-list__sibling-inserter .editor-inserter__toggle {
opacity: 1;
}

.editor-block-list__block > .editor-block-list__sibling-inserter {
position: absolute;
bottom: 0;
Expand Down
2 changes: 1 addition & 1 deletion editor/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Inserter extends Component {
insertIndex,
onToggle,
} = this.props;

console.log( 'isOpen', isOpen );
if ( isOpen ) {
this.props.showInsertionPoint( insertIndex );
} else {
Expand Down

0 comments on commit 47f5b07

Please sign in to comment.