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

Framework: Adding the possibility to lock the editor #3686

Merged
merged 3 commits into from
Dec 6, 2017
Merged
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
18 changes: 11 additions & 7 deletions blocks/library/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,17 @@ registerBlockType( 'core/heading', {
onFocus={ setFocus }
onChange={ ( value ) => setAttributes( { content: value } ) }
onMerge={ mergeBlocks }
onSplit={ ( before, after, ...blocks ) => {
setAttributes( { content: before } );
insertBlocksAfter( [
...blocks,
createBlock( 'core/paragraph', { content: after } ),
] );
} }
onSplit={
insertBlocksAfter ?
Copy link
Member

Choose a reason for hiding this comment

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

Should this be the responsibility of the block implementer to account for? Or could we just turn insertBlocksAfter and setAttributes into noop for a locked editor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mmm, I didn't think about it. Sounds reasonable

( before, after, ...blocks ) => {
setAttributes( { content: before } );
insertBlocksAfter( [
...blocks,
createBlock( 'core/paragraph', { content: after } ),
] );
} :
undefined
}
style={ { textAlign: align } }
placeholder={ placeholder || __( 'Write heading…' ) }
/>,
Expand Down
34 changes: 19 additions & 15 deletions blocks/library/list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,21 +332,25 @@ registerBlockType( 'core/list', {
wrapperClassName="blocks-list"
placeholder={ __( 'Write list…' ) }
onMerge={ mergeBlocks }
onSplit={ ( before, after, ...blocks ) => {
if ( ! blocks.length ) {
blocks.push( createBlock( 'core/paragraph' ) );
}

if ( after.length ) {
blocks.push( createBlock( 'core/list', {
nodeName,
values: after,
} ) );
}

setAttributes( { values: before } );
insertBlocksAfter( blocks );
} }
onSplit={
insertBlocksAfter ?
( before, after, ...blocks ) => {
if ( ! blocks.length ) {
blocks.push( createBlock( 'core/paragraph' ) );
}

if ( after.length ) {
blocks.push( createBlock( 'core/list', {
nodeName,
values: after,
} ) );
}

setAttributes( { values: before } );
insertBlocksAfter( blocks );
} :
undefined
}
/>,
];
}
Expand Down
17 changes: 10 additions & 7 deletions blocks/library/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,16 @@ class ParagraphBlock extends Component {
} }
focus={ focus }
onFocus={ setFocus }
onSplit={ ( before, after, ...blocks ) => {
setAttributes( { content: before } );
insertBlocksAfter( [
...blocks,
createBlock( 'core/paragraph', { content: after } ),
] );
} }
onSplit={ insertBlocksAfter ?
( before, after, ...blocks ) => {
setAttributes( { content: before } );
insertBlocksAfter( [
...blocks,
createBlock( 'core/paragraph', { content: after } ),
] );
} :
undefined
}
onMerge={ mergeBlocks }
onReplace={ onReplace }
placeholder={ placeholder || __( 'Add text or type / to insert content' ) }
Expand Down
25 changes: 19 additions & 6 deletions editor/components/block-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@
* External Dependencies
*/
import { connect } from 'react-redux';
import { reduce, get, find } from 'lodash';
import { reduce, get, find, flow } from 'lodash';

/**
* WordPress dependencies
*/
import { DropZone } from '@wordpress/components';
import { DropZone, withContext } from '@wordpress/components';
import { getBlockTypes } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { insertBlocks } from '../../actions';

function BlockDropZone( { index, ...props } ) {
function BlockDropZone( { index, isLocked, ...props } ) {
if ( isLocked ) {
return null;
}

const dropFiles = ( files, position ) => {
const transformation = reduce( getBlockTypes(), ( ret, blockType ) => {
if ( ret ) {
Expand Down Expand Up @@ -45,7 +49,16 @@ function BlockDropZone( { index, ...props } ) {
);
}

export default connect(
undefined,
{ insertBlocks }
export default flow(
connect(
undefined,
{ insertBlocks }
),
withContext( 'editor' )( ( settings ) => {
const { templateLock } = settings;

return {
isLocked: !! templateLock,
};
} )
)( BlockDropZone );
31 changes: 20 additions & 11 deletions editor/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { get, partial, reduce, size } from 'lodash';
import { Component, compose, createElement } from '@wordpress/element';
import { keycodes } from '@wordpress/utils';
import { getBlockType, BlockEdit, getBlockDefaultClassname, createBlock, hasBlockSupport } from '@wordpress/blocks';
import { withFilters } from '@wordpress/components';
import { withFilters, withContext } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';

/**
Expand Down Expand Up @@ -296,7 +296,7 @@ class BlockListBlock extends Component {
case ENTER:
// Insert default block after current block if enter and event
// not already handled by descendant.
if ( target === this.node ) {
if ( target === this.node && ! this.props.isLocked ) {
event.preventDefault();

this.props.onInsertBlocks( [
Expand All @@ -318,12 +318,14 @@ class BlockListBlock extends Component {
case DELETE:
// Remove block on backspace.
if ( target === this.node ) {
const { uid, onRemove, previousBlock, onFocus, isLocked } = this.props;
event.preventDefault();
const { uid, onRemove, previousBlock, onFocus } = this.props;
onRemove( uid );
if ( ! isLocked ) {
onRemove( uid );

if ( previousBlock ) {
onFocus( previousBlock.uid, { offset: -1 } );
if ( previousBlock ) {
onFocus( previousBlock.uid, { offset: -1 } );
}
}
}
break;
Expand All @@ -340,7 +342,7 @@ class BlockListBlock extends Component {
}

render() {
const { block, order, mode, showContextualToolbar } = this.props;
const { block, order, mode, showContextualToolbar, isLocked } = this.props;
const { name: blockName, isValid } = block;
const blockType = getBlockType( blockName );
// translators: %s: Type of block (i.e. Text, Image etc)
Expand Down Expand Up @@ -410,10 +412,10 @@ class BlockListBlock extends Component {
focus={ focus }
attributes={ block.attributes }
setAttributes={ this.setAttributes }
insertBlocksAfter={ this.insertBlocksAfter }
onReplace={ onReplace }
insertBlocksAfter={ isLocked ? undefined : this.insertBlocksAfter }
onReplace={ isLocked ? undefined : onReplace }
setFocus={ partial( onFocus, block.uid ) }
mergeBlocks={ this.mergeBlocks }
mergeBlocks={ isLocked ? undefined : this.mergeBlocks }
className={ className }
id={ block.uid }
isSelectionEnabled={ this.props.isSelectionEnabled }
Expand Down Expand Up @@ -524,5 +526,12 @@ const mapDispatchToProps = ( dispatch, ownProps ) => ( {

export default compose(
withFilters( 'Editor.BlockItem' ),
connect( mapStateToProps, mapDispatchToProps )
connect( mapStateToProps, mapDispatchToProps ),
withContext( 'editor' )( ( settings ) => {
const { templateLock } = settings;

return {
isLocked: !! templateLock,
};
} ),
)( BlockListBlock );
77 changes: 45 additions & 32 deletions editor/components/block-mover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* External dependencies
*/
import { connect } from 'react-redux';
import { first, last } from 'lodash';
import { first, last, flow } from 'lodash';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { IconButton } from '@wordpress/components';
import { IconButton, withContext } from '@wordpress/components';
import { getBlockType } from '@wordpress/blocks';

/**
Expand All @@ -19,7 +19,11 @@ import { getBlockMoverLabel } from './mover-label';
import { isFirstBlock, isLastBlock, getBlockIndex, getBlock } from '../../selectors';
import { selectBlock } from '../../actions';

export function BlockMover( { onMoveUp, onMoveDown, isFirst, isLast, uids, blockType, firstIndex } ) {
export function BlockMover( { onMoveUp, onMoveDown, isFirst, isLast, uids, blockType, firstIndex, isLocked } ) {
if ( isLocked ) {
return null;
}

// We emulate a disabled state because forcefully applying the `disabled`
// attribute on the button while it has focus causes the screen to change
// to an unfocused state (body as active element) without firing blur on,
Expand Down Expand Up @@ -60,37 +64,46 @@ export function BlockMover( { onMoveUp, onMoveDown, isFirst, isLast, uids, block
);
}

export default connect(
( state, ownProps ) => {
const block = getBlock( state, first( ownProps.uids ) );

return ( {
isFirst: isFirstBlock( state, first( ownProps.uids ) ),
isLast: isLastBlock( state, last( ownProps.uids ) ),
firstIndex: getBlockIndex( state, first( ownProps.uids ) ),
blockType: block ? getBlockType( block.name ) : null,
} );
},
( dispatch, ownProps ) => ( {
onMoveDown() {
if ( ownProps.uids.length === 1 ) {
dispatch( selectBlock( first( ownProps.uids ) ) );
}
export default flow(
connect(
( state, ownProps ) => {
const block = getBlock( state, first( ownProps.uids ) );

dispatch( {
type: 'MOVE_BLOCKS_DOWN',
uids: ownProps.uids,
return ( {
isFirst: isFirstBlock( state, first( ownProps.uids ) ),
isLast: isLastBlock( state, last( ownProps.uids ) ),
firstIndex: getBlockIndex( state, first( ownProps.uids ) ),
blockType: block ? getBlockType( block.name ) : null,
} );
},
onMoveUp() {
if ( ownProps.uids.length === 1 ) {
dispatch( selectBlock( first( ownProps.uids ) ) );
}
( dispatch, ownProps ) => ( {
onMoveDown() {
if ( ownProps.uids.length === 1 ) {
dispatch( selectBlock( first( ownProps.uids ) ) );
}

dispatch( {
type: 'MOVE_BLOCKS_UP',
uids: ownProps.uids,
} );
},
} )
dispatch( {
type: 'MOVE_BLOCKS_DOWN',
uids: ownProps.uids,
} );
},
onMoveUp() {
if ( ownProps.uids.length === 1 ) {
dispatch( selectBlock( first( ownProps.uids ) ) );
}

dispatch( {
type: 'MOVE_BLOCKS_UP',
uids: ownProps.uids,
} );
},
} )
),
withContext( 'editor' )( ( settings ) => {
const { templateLock } = settings;

return {
isLocked: templateLock === 'all',
};
} ),
)( BlockMover );
5 changes: 5 additions & 0 deletions editor/components/block-mover/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ describe( 'BlockMover', () => {
title: 'yolo-block',
};

it( 'should not render if the editor is locked', () => {
const wrapper = shallow( <BlockMover isLocked /> );
expect( wrapper.type() ).toBe( null );
} );

it( 'should render two IconButton components with the following props', () => {
const blockMover = shallow( <BlockMover uids={ selectedUids } blockType={ blockType } firstIndex={ 0 } /> );
expect( blockMover.hasClass( 'editor-block-mover' ) ).toBe( true );
Expand Down
31 changes: 22 additions & 9 deletions editor/components/block-settings-menu/block-delete-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import { flow, noop } from 'lodash';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { IconButton } from '@wordpress/components';
import { IconButton, withContext } from '@wordpress/components';

/**
* Internal dependencies
*/
import { removeBlocks } from '../../actions';

export function BlockDeleteButton( { onDelete, onClick = noop, small = false } ) {
export function BlockDeleteButton( { onDelete, onClick = noop, isLocked, small = false } ) {
if ( isLocked ) {
return null;
}

const label = __( 'Delete' );

return (
Expand All @@ -30,11 +34,20 @@ export function BlockDeleteButton( { onDelete, onClick = noop, small = false } )
);
}

export default connect(
undefined,
( dispatch, ownProps ) => ( {
onDelete() {
dispatch( removeBlocks( ownProps.uids ) );
},
} )
export default flow(
connect(
undefined,
( dispatch, ownProps ) => ( {
onDelete() {
dispatch( removeBlocks( ownProps.uids ) );
},
} )
),
withContext( 'editor' )( ( settings ) => {
const { templateLock } = settings;

return {
isLocked: !! templateLock,
};
} ),
)( BlockDeleteButton );
Loading