-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Visual Editor: Adding the arrangement toolbar (up/down arrows) #412
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
Changes from all commits
53d2444
8b6ad11
9ef582e
4988152
e15327e
99865dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { connect } from 'react-redux'; | ||
| import classnames from 'classnames'; | ||
| import { first, last } from 'lodash'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import './style.scss'; | ||
| import IconButton from 'components/icon-button'; | ||
|
|
||
| function BlockMover( { onMoveUp, onMoveDown, isFirst, isLast } ) { | ||
| return ( | ||
| <div className="editor-block-mover"> | ||
| <IconButton | ||
| className={ classnames( 'editor-block-mover__control', { 'is-disabled': isFirst } ) } | ||
| onClick={ onMoveUp } | ||
| icon="arrow-up-alt2" | ||
| /> | ||
| <IconButton | ||
| className={ classnames( 'editor-block-mover__control', { 'is-disabled': isLast } ) } | ||
| onClick={ onMoveDown } | ||
| icon="arrow-down-alt2" | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export default connect( | ||
| ( state, ownProps ) => ( { | ||
| isFirst: first( state.blocks.order ) === ownProps.uid, | ||
| isLast: last( state.blocks.order ) === ownProps.uid | ||
| } ), | ||
| ( dispatch, ownProps ) => ( { | ||
| onMoveDown() { | ||
| dispatch( { | ||
| type: 'MOVE_BLOCK_DOWN', | ||
| uid: ownProps.uid | ||
| } ); | ||
| }, | ||
| onMoveUp() { | ||
| dispatch( { | ||
| type: 'MOVE_BLOCK_UP', | ||
| uid: ownProps.uid | ||
| } ); | ||
| } | ||
| } ) | ||
| )( BlockMover ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| .editor-block-mover { | ||
| position: absolute; | ||
| top: 10px; | ||
| left: 0; | ||
| } | ||
|
|
||
| .editor-block-mover__control { | ||
| display: block; | ||
| padding: 0; | ||
| border: none; | ||
| outline: none; | ||
| background: none; | ||
| color: $light-gray-600; | ||
| cursor: pointer; | ||
| width: 20px; | ||
| height: 20px; | ||
|
|
||
| &:hover { | ||
| color: $dark-gray-900; | ||
| } | ||
|
|
||
| &.is-disabled { | ||
| color: $light-gray-100; | ||
| } | ||
|
|
||
| .dashicon { | ||
| display: block; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| * External dependencies | ||
| */ | ||
| import { combineReducers, createStore } from 'redux'; | ||
| import { keyBy } from 'lodash'; | ||
| import { keyBy, last } from 'lodash'; | ||
|
|
||
| /** | ||
| * Reducer returning editor blocks state, an combined reducer of keys byUid, | ||
|
|
@@ -31,9 +31,37 @@ export const blocks = combineReducers( { | |
| return state; | ||
| }, | ||
| order( state = [], action ) { | ||
| let index; | ||
| let swappedUid; | ||
| switch ( action.type ) { | ||
| case 'REPLACE_BLOCKS': | ||
| return action.blockNodes.map( ( { uid } ) => uid ); | ||
|
|
||
| case 'MOVE_BLOCK_UP': | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it would be easier to have only a single action, something like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a small preference for the explicit actions but I don't mind if we change this later.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also need to consider drag and drop. |
||
| if ( action.uid === state[ 0 ] ) { | ||
| return state; | ||
| } | ||
| index = state.indexOf( action.uid ); | ||
| swappedUid = state[ index - 1 ]; | ||
| return [ | ||
| ...state.slice( 0, index - 1 ), | ||
| action.uid, | ||
| swappedUid, | ||
| ...state.slice( index + 1 ) | ||
| ]; | ||
|
|
||
| case 'MOVE_BLOCK_DOWN': | ||
| if ( action.uid === last( state ) ) { | ||
| return state; | ||
| } | ||
| index = state.indexOf( action.uid ); | ||
| swappedUid = state[ index + 1 ]; | ||
| return [ | ||
| ...state.slice( 0, index ), | ||
| swappedUid, | ||
| action.uid, | ||
| ...state.slice( index + 2 ) | ||
| ]; | ||
| } | ||
|
|
||
| return state; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The classes should be
block-mover__only.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 We should update our coding guidelines https://github.com/WordPress/gutenberg/blob/master/docs/coding-guidelines.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should keep it this way in this PR and drop the
editorprefix from all the components in a specific PR. (To avoid having a mix of conventions)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, this is following the fact I want to move these components out of the editor and make them only about blocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #408 (comment)