Skip to content

Commit

Permalink
Added InnerBlock locking mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jun 19, 2018
1 parent 67abc5b commit 03123ee
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 53 deletions.
7 changes: 3 additions & 4 deletions editor/components/block-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,10 @@ export default compose(
},
};
} ),
withSelect( ( select ) => {
const { templateLock } = select( 'core/editor' ).getEditorSettings();

withSelect( ( select, { rootUID } ) => {
const { getLockedState } = select( 'core/editor' );
return {
isLocked: !! templateLock,
isLocked: !! getLockedState( rootUID ),
};
} )
)( BlockDropZone );
5 changes: 3 additions & 2 deletions editor/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,10 +618,11 @@ const applyWithSelect = withSelect( ( select, { uid, rootUID } ) => {
getSelectedBlocksInitialCaretPosition,
getEditorSettings,
hasSelectedInnerBlock,
getLockedState,
} = select( 'core/editor' );
const isSelected = isBlockSelected( uid );
const isParentOfSelectedBlock = hasSelectedInnerBlock( uid );
const { templateLock, hasFixedToolbar } = getEditorSettings();
const { hasFixedToolbar } = getEditorSettings();
const block = getBlock( uid );
const previousBlockUid = getPreviousBlockUid( uid );
const previousBlock = getBlock( previousBlockUid );
Expand All @@ -642,7 +643,7 @@ const applyWithSelect = withSelect( ( select, { uid, rootUID } ) => {
initialPosition: getSelectedBlocksInitialCaretPosition(),
isEmptyDefaultBlock: block && isUnmodifiedDefaultBlock( block ),
isPreviousBlockADefaultEmptyBlock: previousBlock && isUnmodifiedDefaultBlock( previousBlock ),
isLocked: !! templateLock,
isLocked: !! getLockedState( rootUID ),
previousBlockUid,
block,
isSelected,
Expand Down
6 changes: 3 additions & 3 deletions editor/components/block-list/insertion-point.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default compose(
getBlock,
isBlockInsertionPointVisible,
isTyping,
getEditorSettings,
getLockedState,
} = select( 'core/editor' );
const blockIndex = uid ? getBlockIndex( uid, rootUID ) : -1;
const insertIndex = blockIndex;
Expand All @@ -92,13 +92,13 @@ export default compose(
);

return {
templateLock: getEditorSettings().templateLock,
isLocked: !! getLockedState( insertionPoint.rootUID ),
showInserter: ! isTyping() && canShowInserter,
index: insertIndex,
showInsertionPoint,
};
} ),
ifCondition( ( { templateLock } ) => ! templateLock ),
ifCondition( ( { isLocked } ) => ! isLocked ),
withDispatch( ( dispatch ) => {
const { insertDefaultBlock, startTyping } = dispatch( 'core/editor' );
return {
Expand Down
5 changes: 2 additions & 3 deletions editor/components/block-mover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,14 @@ export class BlockMover extends Component {

export default compose(
withSelect( ( select, { uids, rootUID } ) => {
const { getBlock, getBlockIndex, getEditorSettings } = select( 'core/editor' );
const { getBlock, getBlockIndex, getLockedState } = select( 'core/editor' );
const firstUID = first( castArray( uids ) );
const block = getBlock( firstUID );
const { templateLock } = getEditorSettings();

return {
firstIndex: getBlockIndex( firstUID, rootUID ),
blockType: block ? getBlockType( block.name ) : null,
isLocked: templateLock === 'all',
isLocked: getLockedState( rootUID ) === 'all',
};
} ),
withDispatch( ( dispatch, { uids, rootUID } ) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ export function BlockDuplicateButton( { blocks, onDuplicate, onClick = noop, isL

export default compose(
withSelect( ( select, { uids, rootUID } ) => {
const { getBlocksByUID, getBlockIndex, getEditorSettings } = select( 'core/editor' );
const { templateLock } = getEditorSettings();
const { getBlocksByUID, getBlockIndex, getLockedState } = select( 'core/editor' );
return {
blocks: getBlocksByUID( uids ),
index: getBlockIndex( last( castArray( uids ) ), rootUID ),
isLocked: !! templateLock,
isLocked: !! getLockedState( rootUID ),
};
} ),
withDispatch( ( dispatch, { blocks, index, rootUID } ) => ( {
Expand Down
11 changes: 6 additions & 5 deletions editor/components/block-settings-menu/block-remove-button.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { flow, noop } from 'lodash';
import { castArray, flow, noop, some } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -36,11 +36,12 @@ export default compose(
dispatch( 'core/editor' ).removeBlocks( uids );
},
} ) ),
withSelect( ( select ) => {
const { templateLock } = select( 'core/editor' ).getEditorSettings();

withSelect( ( select, { uids } ) => {
const { getBlockRootUID, getLockedState } = select( 'core/editor' );
return {
isLocked: !! templateLock,
isLocked: some( castArray( uids ), ( uid ) => {
return !! getLockedState( getBlockRootUID( uid ) );
} ),
};
} ),
)( BlockRemoveButton );
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { noop } from 'lodash';
import { noop, some } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -52,10 +52,9 @@ function BlockTransformations( { blocks, small = false, onTransform, onClick = n
}
export default compose( [
withSelect( ( select, { uids } ) => {
const { getEditorSettings, getBlocksByUID } = select( 'core/editor' );
const { templateLock } = getEditorSettings();
const { getBlockRootUID, getBlocksByUID, getLockedState } = select( 'core/editor' );
return {
isLocked: !! templateLock,
isLocked: some( uids, ( uid ) => !! getLockedState( getBlockRootUID( uid ) ) ),
blocks: getBlocksByUID( uids ),
};
} ),
Expand Down
10 changes: 7 additions & 3 deletions editor/components/block-switcher/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { castArray, some } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -98,11 +103,10 @@ export function BlockSwitcher( { blocks, onTransform, isLocked } ) {

export default compose(
withSelect( ( select, ownProps ) => {
const { getBlock, getEditorSettings } = select( 'core/editor' );
const { templateLock } = getEditorSettings();
const { getBlock, getBlockRootUID, getLockedState } = select( 'core/editor' );
return {
blocks: ownProps.uids.map( getBlock ),
isLocked: !! templateLock,
isLocked: some( castArray( ownProps.uids ), ( uid ) => !! getLockedState( getBlockRootUID( uid ) ) ),
};
} ),
withDispatch( ( dispatch, ownProps ) => ( {
Expand Down
6 changes: 3 additions & 3 deletions editor/components/default-block-appender/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ export function DefaultBlockAppender( {
}
export default compose(
withSelect( ( select, ownProps ) => {
const { getBlockCount, getBlock, getEditorSettings } = select( 'core/editor' );
const { getBlockCount, getBlock, getEditorSettings, getLockedState } = select( 'core/editor' );
const { isTipVisible } = select( 'core/nux' );

const isEmpty = ! getBlockCount( ownProps.rootUID );
const lastBlock = getBlock( ownProps.lastBlockUID );
const isLastBlockDefault = get( lastBlock, [ 'name' ] ) === getDefaultBlockName();
const { templateLock, bodyPlaceholder } = getEditorSettings();
const { bodyPlaceholder } = getEditorSettings();

return {
isVisible: isEmpty || ! isLastBlockDefault,
showPrompt: isEmpty,
isLocked: !! templateLock,
isLocked: !! getLockedState( ownProps.rootUID ),
placeholder: bodyPlaceholder,
hasTip: isTipVisible( 'core/editor.inserter' ),
};
Expand Down
9 changes: 5 additions & 4 deletions editor/components/editor-global-keyboard-shortcuts/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { first, last } from 'lodash';
import { first, last, some } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -98,16 +98,17 @@ export default compose( [
getBlockOrder,
getMultiSelectedBlockUids,
hasMultiSelection,
getEditorSettings,
isEditedPostDirty,
getBlockRootUID,
getLockedState,
} = select( 'core/editor' );
const { templateLock } = getEditorSettings();
const multiSelectedBlockUids = getMultiSelectedBlockUids();

return {
uids: getBlockOrder(),
multiSelectedBlockUids: getMultiSelectedBlockUids(),
hasMultiSelection: hasMultiSelection(),
isLocked: !! templateLock,
isLocked: some( multiSelectedBlockUids, ( uid ) => !! getLockedState( getBlockRootUID( uid ) ) ),
isDirty: isEditedPostDirty(),
};
} ),
Expand Down
3 changes: 2 additions & 1 deletion editor/components/inner-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function InnerBlocks( {
BlockList,
layouts,
allowedBlocks,
lock,
template,
isSmallScreen,
isSelectedBlockInRoot,
Expand All @@ -30,7 +31,7 @@ function InnerBlocks( {

return (
<div className={ classes }>
<BlockList { ...{ layouts, allowedBlocks, template } } />
<BlockList { ...{ layouts, allowedBlocks, lock, template } } />
</div>
);
}
Expand Down
5 changes: 2 additions & 3 deletions editor/components/inserter-with-shortcuts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ function InserterWithShortcuts( { items, isLocked, onInsert } ) {

export default compose(
withSelect( ( select, { rootUID } ) => {
const { getEditorSettings, getInserterItems } = select( 'core/editor' );
const { templateLock } = getEditorSettings();
const { getInserterItems, getLockedState } = select( 'core/editor' );
return {
items: getInserterItems( rootUID ),
isLocked: !! templateLock,
isLocked: !! getLockedState( rootUID ),
};
} ),
withDispatch( ( dispatch, ownProps ) => {
Expand Down
27 changes: 23 additions & 4 deletions editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1326,20 +1326,20 @@ export const canInsertBlockType = createSelector(
return false;
}

const { allowedBlockTypes, templateLock } = getEditorSettings( state );
const { allowedBlockTypes } = getEditorSettings( state );

const isBlockAllowedInEditor = checkAllowList( allowedBlockTypes, blockName, true );
if ( ! isBlockAllowedInEditor ) {
return false;
}

const isEditorLocked = !! templateLock;
if ( isEditorLocked ) {
const isLocked = !! getLockedState( state, parentUID );
if ( isLocked ) {
return false;
}

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 @@ -1834,3 +1834,22 @@ export function getSupportedBlocks( state, uid, globallyEnabledBlockTypes ) {
export function getEditorSettings( state ) {
return state.settings;
}

/*
* Returns the locked state in the context of a given root block.
*
* @param {Object} state Editor state.
* @param {?string} rootUID Block UID.
*
* @return {?string} Locked state in the context of a given block.
*/
export function getLockedState( state, rootUID = null ) {
if ( rootUID === null ) {
return getTemplateLock( state );
}
const blockListSettings = getBlockListSettings( state, rootUID );
if ( ! blockListSettings ) {
return null;
}
return blockListSettings.lock;
}
6 changes: 3 additions & 3 deletions editor/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2964,7 +2964,7 @@ describe( 'selectors', () => {
},
blockListSettings: {
block1: {
supportedBlocks: [ 'core/test-block-c' ],
allowedBlocks: [ 'core/test-block-c' ],
},
},
settings: {},
Expand All @@ -2983,7 +2983,7 @@ describe( 'selectors', () => {
},
blockListSettings: {
block1: {
supportedBlocks: [ 'core/test-block-b' ],
allowedBlocks: [ 'core/test-block-b' ],
},
},
settings: {},
Expand All @@ -3002,7 +3002,7 @@ describe( 'selectors', () => {
},
blockListSettings: {
block1: {
supportedBlocks: [],
allowedBlocks: [],
},
},
settings: {},
Expand Down
25 changes: 17 additions & 8 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 Down Expand Up @@ -56,12 +57,14 @@ export function createInnerBlockList( uid, renderBlockMenu = noop ) {

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

componentDidUpdate() {
this.updateNestedSettings();
}

insertTemplateBlocks( template ) {
const { block, insertBlocks } = this.props;
if ( template && ! block.innerBlocks.length ) {
Expand All @@ -71,9 +74,14 @@ export function createInnerBlockList( uid, renderBlockMenu = noop ) {
}
}

updateNestedSettings( newSettings ) {
if ( ! isEqual( this.props.blockListSettings, newSettings ) ) {
this.props.updateNestedSettings( newSettings );
updateNestedSettings() {
const { allowedBlocks, lock, parentLock, blockListSettings, updateNestedSettings } = this.props;
const newSettings = {
allowedBlocks,
lock: lock === undefined ? parentLock : lock,
};
if ( ! isShallowEqual( blockListSettings, newSettings ) ) {
updateNestedSettings( newSettings );
}
}

Expand All @@ -98,10 +106,11 @@ export function createInnerBlockList( uid, renderBlockMenu = noop ) {

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

0 comments on commit 03123ee

Please sign in to comment.