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

Try: Components decorator extensibility pattern #1023

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 0 additions & 7 deletions blocks/library/button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ registerBlockType( 'core/button', {
},
],

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'center' === align ) {
return { 'data-align': align };
}
},

edit( { attributes, setAttributes, focus, setFocus } ) {
const { text, url, title } = attributes;

Expand Down
7 changes: 0 additions & 7 deletions blocks/library/embed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ registerBlockType( 'core/embed', {
},
],

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align ) {
return { 'data-align': align };
}
},

edit: class extends wp.element.Component {
constructor() {
super( ...arguments );
Expand Down
22 changes: 0 additions & 22 deletions blocks/library/embed/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,4 @@ div[data-type="core/embed"] {
float: right;
margin-left: $block-padding;
}

&[data-align="wide"] {
padding-left: 0;
padding-right: 0;
margin-right: -#{ $block-padding + $block-mover-margin }; /* Compensate for .editor-visual-editor centering padding */

&:before {
left: 0;
border-left-width: 0;
border-right-width: 0;
}

.editor-block-mover {
display: none;
}

.editor-visual-editor__block-controls {
max-width: #{ $visual-editor-max-width - $block-padding - ( $block-padding * 2 + $block-mover-margin ) };
margin-left: auto;
margin-right: auto;
}
}
}
7 changes: 0 additions & 7 deletions blocks/library/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ registerBlockType( 'core/image', {
},
],

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align ) {
return { 'data-align': align };
}
},

edit( { attributes, setAttributes, focus, setFocus } ) {
const { url, alt, caption } = attributes;

Expand Down
23 changes: 0 additions & 23 deletions blocks/library/image/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,6 @@
float: right;
margin-left: $block-padding;
}

&[data-align="wide"] {
padding-left: 0;
padding-right: 0;
margin-right: -#{ $block-padding + $block-mover-margin }; /* Compensate for .editor-visual-editor centering padding */

&:before {
left: 0;
border-left-width: 0;
border-right-width: 0;
}

.editor-block-mover {
display: none;
}

.editor-visual-editor__block-controls {
max-width: #{ $visual-editor-max-width - $block-padding - ( $block-padding * 2 + $block-mover-margin ) };
margin-left: auto;
margin-right: auto;
}

}
}

.blocks-image {
Expand Down
7 changes: 0 additions & 7 deletions blocks/library/table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ registerBlockType( 'core/table', {
},
],

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align ) {
return { 'data-align': align };
}
},

edit( { attributes, setAttributes, focus, setFocus } ) {
const focussedKey = focus ? focus.editable || 'body.0.0' : null;

Expand Down
54 changes: 54 additions & 0 deletions editor/extensions/wide-align/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* WordPress dependencies
*/
import { registerExtension } from 'extensions';
import { Component, cloneElement } from 'element';

/**
* Internal dependencies
*/
import VisualEditorBlock from '../../modes/visual-editor/block';
import Layout from '../../layout';

registerExtension( 'wide-align', {
decorators: [
[ Layout, ( WrappedComponent ) => (
class extends Component {
componentDidMount() {
this.props.setExtensionState( {
layoutWidth: this.node.clientWidth,
} );
}

render() {
return (
<div ref={ ( node ) => this.node = node }>
<WrappedComponent { ...this.props } />
</div>
);
}
}
) ],
[ VisualEditorBlock, ( WrappedComponent ) => (
class extends Component {
render() {
const { block, extensionState } = this.props;
const { align } = block.attributes;
const element = <WrappedComponent { ...this.props } />;
if ( 'wide' !== align ) {
return element;
}

const { layoutWidth } = extensionState;
const offset = ( ( layoutWidth / -2 ) + ( 668 / 2 ) );
return cloneElement( element, {
style: {
marginLeft: offset,
marginRight: offset,
},
} );
}
}
) ],
],
} );
1 change: 1 addition & 0 deletions editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { omit } from 'lodash';
* Internal dependencies
*/
import './assets/stylesheets/main.scss';
import './extensions/wide-align';
import Layout from './layout';
import { createReduxStore } from './state';

Expand Down
17 changes: 13 additions & 4 deletions editor/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
*/
import { connect } from 'react-redux';
import classnames from 'classnames';
import { flow } from 'lodash';

/**
* WordPress dependencies
*/
import { applyComponentDecorators } from 'extensions';

/**
* Internal dependencies
Expand Down Expand Up @@ -30,7 +36,10 @@ function Layout( { mode, isSidebarOpened } ) {
);
}

export default connect( ( state ) => ( {
mode: getEditorMode( state ),
isSidebarOpened: isEditorSidebarOpened( state ),
} ) )( Layout );
export default flow(
applyComponentDecorators,
connect( ( state ) => ( {
mode: getEditorMode( state ),
isSidebarOpened: isEditorSidebarOpened( state ),
} ) )
)( Layout );
152 changes: 75 additions & 77 deletions editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import { connect } from 'react-redux';
import classnames from 'classnames';
import { Slot } from 'react-slot-fill';
import { partial } from 'lodash';
import { partial, flow } from 'lodash';
import CSSTransitionGroup from 'react-transition-group/CSSTransitionGroup';

/**
* WordPress dependencies
*/
import { Children } from 'element';
import { Toolbar } from 'components';
import { applyComponentDecorators } from 'extensions';
import { BACKSPACE, ESCAPE } from 'utils/keycodes';

/**
Expand Down Expand Up @@ -201,12 +202,6 @@ class VisualEditorBlock extends wp.element.Component {

const { onSelect, onMouseLeave, onFocus, onInsertAfter } = this.props;

// Determine whether the block has props to apply to the wrapper.
let wrapperProps;
if ( blockType.getEditWrapperProps ) {
wrapperProps = blockType.getEditWrapperProps( block.attributes );
}

// Disable reason: Each block can be selected by clicking on it
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
return (
Expand All @@ -227,7 +222,7 @@ class VisualEditorBlock extends wp.element.Component {
className={ className }
data-type={ block.name }
tabIndex="0"
{ ...wrapperProps }
style={ this.props.style }
>
{ ( showUI || isHovered ) && <BlockMover uids={ [ block.uid ] } /> }
{ showUI &&
Expand Down Expand Up @@ -276,78 +271,81 @@ class VisualEditorBlock extends wp.element.Component {
}
}

export default connect(
( state, ownProps ) => {
return {
previousBlock: getPreviousBlock( state, ownProps.uid ),
nextBlock: getNextBlock( state, ownProps.uid ),
block: getBlock( state, ownProps.uid ),
isSelected: isBlockSelected( state, ownProps.uid ),
isMultiSelected: isBlockMultiSelected( state, ownProps.uid ),
isFirstSelected: isFirstSelectedBlock( state, ownProps.uid ),
selectedBlocks: getSelectedBlocks( state ),
isHovered: isBlockHovered( state, ownProps.uid ),
focus: getBlockFocus( state, ownProps.uid ),
isTyping: isTypingInBlock( state, ownProps.uid ),
order: getBlockIndex( state, ownProps.uid ),
};
},
( dispatch, ownProps ) => ( {
onChange( uid, updates ) {
dispatch( {
type: 'UPDATE_BLOCK',
uid,
updates,
} );
},
onSelect() {
dispatch( {
type: 'TOGGLE_BLOCK_SELECTED',
selected: true,
uid: ownProps.uid,
} );
},
onDeselect() {
dispatch( { type: 'CLEAR_SELECTED_BLOCK' } );
},
onStartTyping() {
dispatch( {
type: 'START_TYPING',
uid: ownProps.uid,
} );
},
onHover() {
dispatch( {
type: 'TOGGLE_BLOCK_HOVERED',
hovered: true,
uid: ownProps.uid,
} );
},
onMouseLeave() {
dispatch( {
type: 'TOGGLE_BLOCK_HOVERED',
hovered: false,
uid: ownProps.uid,
} );
export default flow(
applyComponentDecorators,
Copy link
Member

Choose a reason for hiding this comment

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

This is exactly what we need to Collaborative Editing to be able to display a different UI state when collaborator is editing the block 👍

connect(
( state, ownProps ) => {
return {
previousBlock: getPreviousBlock( state, ownProps.uid ),
nextBlock: getNextBlock( state, ownProps.uid ),
block: getBlock( state, ownProps.uid ),
isSelected: isBlockSelected( state, ownProps.uid ),
isMultiSelected: isBlockMultiSelected( state, ownProps.uid ),
isFirstSelected: isFirstSelectedBlock( state, ownProps.uid ),
selectedBlocks: getSelectedBlocks( state ),
isHovered: isBlockHovered( state, ownProps.uid ),
focus: getBlockFocus( state, ownProps.uid ),
isTyping: isTypingInBlock( state, ownProps.uid ),
order: getBlockIndex( state, ownProps.uid ),
};
},
( dispatch, ownProps ) => ( {
onChange( uid, updates ) {
dispatch( {
type: 'UPDATE_BLOCK',
uid,
updates,
} );
},
onSelect() {
dispatch( {
type: 'TOGGLE_BLOCK_SELECTED',
selected: true,
uid: ownProps.uid,
} );
},
onDeselect() {
dispatch( { type: 'CLEAR_SELECTED_BLOCK' } );
},
onStartTyping() {
dispatch( {
type: 'START_TYPING',
uid: ownProps.uid,
} );
},
onHover() {
dispatch( {
type: 'TOGGLE_BLOCK_HOVERED',
hovered: true,
uid: ownProps.uid,
} );
},
onMouseLeave() {
dispatch( {
type: 'TOGGLE_BLOCK_HOVERED',
hovered: false,
uid: ownProps.uid,
} );
},

onInsertAfter( block ) {
dispatch( insertBlock( block, ownProps.uid ) );
},
onInsertAfter( block ) {
dispatch( insertBlock( block, ownProps.uid ) );
},

onFocus( ...args ) {
dispatch( focusBlock( ...args ) );
},
onFocus( ...args ) {
dispatch( focusBlock( ...args ) );
},

onRemove( uids ) {
dispatch( {
type: 'REMOVE_BLOCKS',
uids,
} );
},
onRemove( uids ) {
dispatch( {
type: 'REMOVE_BLOCKS',
uids,
} );
},

onMerge( ...args ) {
dispatch( mergeBlocks( ...args ) );
},
} )
onMerge( ...args ) {
dispatch( mergeBlocks( ...args ) );
},
} )
)
)( VisualEditorBlock );
Loading