Skip to content

Commit

Permalink
Nested locking basis.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed May 29, 2018
1 parent bfafd1b commit 816f43c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 21 deletions.
4 changes: 2 additions & 2 deletions editor/components/inner-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*/
import { withContext } from '@wordpress/components';

function InnerBlocks( { BlockList, layouts, allowedBlocks, template } ) {
return <BlockList { ...{ layouts, allowedBlocks, template } } />;
function InnerBlocks( { BlockList, layouts, allowedBlocks, locking, template } ) {
return <BlockList { ...{ layouts, allowedBlocks, locking, template } } />;
}

InnerBlocks = withContext( 'BlockList' )()( InnerBlocks );
Expand Down
21 changes: 20 additions & 1 deletion editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ export const canInsertBlockType = createSelector(
}

const parentBlockListSettings = getBlockListSettings( state, parentUID );
const parentAllowedBlocks = get( parentBlockListSettings, [ 'supportedBlocks' ] );
const parentAllowedBlocks = get( parentBlockListSettings, [ 'allowedBlocks' ] );
const hasParentAllowedBlock = checkAllowList( parentAllowedBlocks, blockName );

const blockAllowedParentBlocks = blockType.parent;
Expand Down Expand Up @@ -1744,3 +1744,22 @@ export function getSupportedBlocks( state, uid, globallyEnabledBlockTypes ) {
export function getEditorSettings( state ) {
return state.settings;
}

/*
* Returns the locking in the context of a given root block.
*
* @param {Object} state Editor state.
* @param {?string} rootUID Block UID.
*
* @return {?string} Locking in the context of a given block.
*/
export function getLocking( state, rootUID ) {
if ( ! rootUID ) {
return getTemplateLock( state );
}
const blockListSettings = getBlockListSettings( state, rootUID );
if ( ! blockListSettings ) {
return null;
}
return blockListSettings.locking;
}
6 changes: 3 additions & 3 deletions editor/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2756,7 +2756,7 @@ describe( 'selectors', () => {
},
blockListSettings: {
block1: {
supportedBlocks: [ 'core/test-block-c' ],
allowedBlocks: [ 'core/test-block-c' ],
},
},
settings: {},
Expand All @@ -2775,7 +2775,7 @@ describe( 'selectors', () => {
},
blockListSettings: {
block1: {
supportedBlocks: [ 'core/test-block-b' ],
allowedBlocks: [ 'core/test-block-b' ],
},
},
settings: {},
Expand All @@ -2794,7 +2794,7 @@ describe( 'selectors', () => {
},
blockListSettings: {
block1: {
supportedBlocks: [],
allowedBlocks: [],
},
},
settings: {},
Expand Down
50 changes: 35 additions & 15 deletions editor/utils/block-list.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { isEqual, noop, omit } from 'lodash';
import { noop, omit } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -11,6 +11,7 @@ import {
synchronizeBlocksWithTemplate,
} from '@wordpress/blocks';
import { withSelect, withDispatch } from '@wordpress/data';
import isShallowEqual from '@wordpress/is-shallow-equal';

/**
* Internal dependencies
Expand All @@ -24,6 +25,24 @@ import BlockList from '../components/block-list';
*/
const INNER_BLOCK_LIST_CACHE = {};

const LOCKING_HIERARCHY = {
insert: 1,
all: 2,
};

const getHighestLocking = ( locking1, locking2 ) => {
if ( ! locking1 ) {
return locking2;
}
if ( ! locking2 ) {
return locking1;
}
if ( LOCKING_HIERARCHY[ locking1 ] > LOCKING_HIERARCHY[ locking2 ] ) {
return locking1;
}
return locking2;
};

/**
* Returns a BlockList component which is already pre-bound to render with a
* given UID as its rootUID prop. It is necessary to cache these components
Expand All @@ -40,10 +59,19 @@ const INNER_BLOCK_LIST_CACHE = {};
export function createInnerBlockList( uid, renderBlockMenu = noop ) {
if ( ! INNER_BLOCK_LIST_CACHE[ uid ] ) {
const InnerBlockListComponent = class extends Component {
componentWillReceiveProps( nextProps ) {
this.updateNestedSettings( {
supportedBlocks: nextProps.allowedBlocks,
} );
constructor( props ) {
super( props );
this.state = {};
}

static getDerivedStateFromProps( props ) {
const newSettings = {
allowedBlocks: props.allowedBlocks,
locking: getHighestLocking( props.locking, props.parentLocking ),
};
if ( ! isShallowEqual( props.blockListSettings, newSettings ) ) {
props.updateNestedSettings( newSettings );
}
}

componentWillUnmount() {
Expand All @@ -56,9 +84,6 @@ export function createInnerBlockList( uid, renderBlockMenu = noop ) {

componentDidMount() {
INNER_BLOCK_LIST_CACHE[ uid ][ 1 ]++;
this.updateNestedSettings( {
supportedBlocks: this.props.allowedBlocks,
} );
this.insertTemplateBlocks( this.props.template );
}

Expand All @@ -71,12 +96,6 @@ export function createInnerBlockList( uid, renderBlockMenu = noop ) {
}
}

updateNestedSettings( newSettings ) {
if ( ! isEqual( this.props.blockListSettings, newSettings ) ) {
this.props.updateNestedSettings( newSettings );
}
}

render() {
return (
<BlockList
Expand All @@ -98,10 +117,11 @@ export function createInnerBlockList( uid, renderBlockMenu = noop ) {

const InnerBlockListComponentContainer = compose(
withSelect( ( select ) => {
const { getBlock, getBlockListSettings } = select( 'core/editor' );
const { getBlock, getBlockListSettings, getBlockRootUID, getLocking } = select( 'core/editor' );
return {
block: getBlock( uid ),
blockListSettings: getBlockListSettings( uid ),
parentLocking: getLocking( getBlockRootUID( uid ) ),
};
} ),
withDispatch( ( dispatch ) => {
Expand Down

0 comments on commit 816f43c

Please sign in to comment.