Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jun 28, 2019
1 parent 95c515a commit 5adb8ab
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 174 deletions.
40 changes: 3 additions & 37 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { get, reduce, size, first, last } from 'lodash';
import { first, last } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -649,42 +649,8 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, { select } ) => {

return {
setAttributes( newAttributes ) {
const { name, clientId } = ownProps;
const type = getBlockType( name );

function isMetaAttribute( key ) {
return get( type, [ 'attributes', key, 'source' ] ) === 'meta';
}

// Partition new attributes to delegate update behavior by source.
//
// TODO: A consolidated approach to external attributes sourcing
// should be devised to avoid specific handling for meta, enable
// additional attributes sources.
//
// See: https://github.com/WordPress/gutenberg/issues/2759
const {
blockAttributes,
metaAttributes,
} = reduce( newAttributes, ( result, value, key ) => {
if ( isMetaAttribute( key ) ) {
result.metaAttributes[ type.attributes[ key ].meta ] = value;
} else {
result.blockAttributes[ key ] = value;
}

return result;
}, { blockAttributes: {}, metaAttributes: {} } );

if ( size( blockAttributes ) ) {
updateBlockAttributes( clientId, blockAttributes );
}

if ( size( metaAttributes ) ) {
const { getSettings } = select( 'core/block-editor' );
const onChangeMeta = getSettings().__experimentalMetaSource.onChange;
onChangeMeta( metaAttributes );
}
const { clientId } = ownProps;
updateBlockAttributes( clientId, newAttributes );
},
onSelect( clientId = ownProps.clientId, initialPosition ) {
selectBlock( clientId, initialPosition );
Expand Down
10 changes: 5 additions & 5 deletions packages/block-editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class BlockEditorProvider extends Component {
this.attachChangeObserver( registry );
}

if ( this.isSyncingOutcomingValue ) {
this.isSyncingOutcomingValue = false;
if ( this.isSyncingOutcomingValue === value ) {
this.isSyncingOutcomingValue = null;
} else if ( value !== prevProps.value ) {
this.isSyncingIncomingValue = true;
this.isSyncingIncomingValue = value;
resetBlocks( value );
}
}
Expand Down Expand Up @@ -87,7 +87,7 @@ class BlockEditorProvider extends Component {
__unstableIsLastBlockChangeIgnored()
)
) {
this.isSyncingIncomingValue = false;
this.isSyncingIncomingValue = null;
blocks = newBlocks;
isPersistent = newIsPersistent;
return;
Expand All @@ -101,7 +101,7 @@ class BlockEditorProvider extends Component {
// When knowing the blocks value is changing, assign instance
// value to skip reset in subsequent `componentDidUpdate`.
if ( newBlocks !== blocks ) {
this.isSyncingOutcomingValue = true;
this.isSyncingOutcomingValue = newBlocks;
}

blocks = newBlocks;
Expand Down
73 changes: 10 additions & 63 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ const MILLISECONDS_PER_WEEK = 7 * 24 * 3600 * 1000;
*/
const EMPTY_ARRAY = [];

/**
* Shared reference to an empty object for cases where it is important to avoid
* returning a new object reference on every invocation.
*
* @type {Object}
*/
const EMPTY_OBJECT = {};

/**
* Returns a new reference when the inner blocks of a given block client ID
* change. This is used exclusively as a memoized selector dependant, relying
Expand Down Expand Up @@ -130,42 +122,14 @@ export function isBlockValid( state, clientId ) {
*
* @return {Object?} Block attributes.
*/
export const getBlockAttributes = createSelector(
( state, clientId ) => {
const block = state.blocks.byClientId[ clientId ];
if ( ! block ) {
return null;
}

let attributes = state.blocks.attributes[ clientId ];

// Inject custom source attribute values.
//
// TODO: Create generic external sourcing pattern, not explicitly
// targeting meta attributes.
const type = getBlockType( block.name );
if ( type ) {
attributes = reduce( type.attributes, ( result, value, key ) => {
if ( value.source === 'meta' ) {
if ( result === attributes ) {
result = { ...result };
}

result[ key ] = getPostMeta( state, value.meta );
}

return result;
}, attributes );
}
export function getBlockAttributes( state, clientId ) {
const block = state.blocks.byClientId[ clientId ];
if ( ! block ) {
return null;
}

return attributes;
},
( state, clientId ) => [
state.blocks.byClientId[ clientId ],
state.blocks.attributes[ clientId ],
getPostMeta( state ),
]
);
return state.blocks.attributes[ clientId ];
}

/**
* Returns a block given its client ID. This is a parsed copy of the block,
Expand All @@ -192,7 +156,8 @@ export const getBlock = createSelector(
};
},
( state, clientId ) => [
...getBlockAttributes.getDependants( state, clientId ),
state.blocks.byClientId[ clientId ],
state.blocks.attributes[ clientId ],
getBlockDependantsCacheBust( state, clientId ),
]
);
Expand All @@ -211,7 +176,7 @@ export const __unstableGetBlockWithoutInnerBlocks = createSelector(
},
( state, clientId ) => [
state.blocks.byClientId[ clientId ],
...getBlockAttributes.getDependants( state, clientId ),
state.blocks.attributes[ clientId ],
]
);

Expand Down Expand Up @@ -314,7 +279,6 @@ export const getBlocksByClientId = createSelector(
( clientId ) => getBlock( state, clientId )
),
( state ) => [
getPostMeta( state ),
state.blocks.byClientId,
state.blocks.order,
state.blocks.attributes,
Expand Down Expand Up @@ -691,7 +655,6 @@ export const getMultiSelectedBlocks = createSelector(
state.blocks.byClientId,
state.blocks.order,
state.blocks.attributes,
getPostMeta( state ),
]
);

Expand Down Expand Up @@ -1455,22 +1418,6 @@ export function __unstableIsLastBlockChangeIgnored( state ) {
return state.blocks.isIgnoredChange;
}

/**
* Returns the value of a post meta from the editor settings.
*
* @param {Object} state Global application state.
* @param {string} key Meta Key to retrieve
*
* @return {*} Meta value
*/
function getPostMeta( state, key ) {
if ( key === undefined ) {
return get( state, [ 'settings', '__experimentalMetaSource', 'value' ], EMPTY_OBJECT );
}

return get( state, [ 'settings', '__experimentalMetaSource', 'value', key ] );
}

/**
* Returns the available reusable blocks
*
Expand Down
48 changes: 0 additions & 48 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,54 +473,6 @@ describe( 'selectors', () => {
} ],
} );
} );

it( 'should merge meta attributes for the block', () => {
registerBlockType( 'core/meta-block', {
save: ( props ) => props.attributes.text,
category: 'common',
title: 'test block',
attributes: {
foo: {
type: 'string',
source: 'meta',
meta: 'foo',
},
},
} );

const state = {
settings: {
__experimentalMetaSource: {
value: {
foo: 'bar',
},
},
},
blocks: {
byClientId: {
123: { clientId: 123, name: 'core/meta-block' },
},
attributes: {
123: {},
},
order: {
'': [ 123 ],
123: [],
},
},
};

expect( getBlock( state, 123 ) ).toEqual( {
clientId: 123,
name: 'core/meta-block',
attributes: {
foo: 'bar',
},
innerBlocks: [],
} );

unregisterBlockType( 'core/meta-block' );
} );
} );

describe( 'getBlocks', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ export function createBlockWithFallback( blockNode ) {
// provided source value with the serialized output before there are any modifications to
// the block. When both match, the block is marked as valid.
if ( ! isFallbackBlock ) {
block.isValid = isValidBlockContent( blockType, block.attributes, innerHTML );
// block.isValid = isValidBlockContent( blockType, block.attributes, innerHTML );
}

// Preserve original content for future use in case the block is parsed as
Expand Down
39 changes: 22 additions & 17 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import memize from 'memize';
*/
import { compose } from '@wordpress/compose';
import { Component } from '@wordpress/element';
import { withDispatch, withSelect } from '@wordpress/data';
import { withDispatch, withSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { BlockEditorProvider, transformStyles } from '@wordpress/block-editor';
import apiFetch from '@wordpress/api-fetch';
Expand All @@ -20,6 +20,7 @@ import { decodeEntities } from '@wordpress/html-entities';
* Internal dependencies
*/
import withRegistryProvider from './with-registry-provider';
import createUseBlockSources from './use-block-sources';
import { mediaUpload } from '../../utils';
import ReusableBlocksButtons from '../reusable-blocks-buttons';
import ConvertToGroupButtons from '../convert-to-group-buttons';
Expand All @@ -40,6 +41,22 @@ const fetchLinkSuggestions = async ( search ) => {
} ) );
};

const useBlockSources = createUseBlockSources( [
() => {
const { editPost } = useDispatch( 'core/editor' );

return [ 'meta', {
onChange: ( key, value ) => editPost( { meta: { [ key ]: value } } ),
} ];
},
] );

function BlockEditorProviderWithSources( props ) {
const blocks = useBlockSources( props.value );

return <BlockEditorProvider { ...props } value={ blocks } />;
}

class EditorProvider extends Component {
constructor( props ) {
super( ...arguments );
Expand Down Expand Up @@ -72,7 +89,7 @@ class EditorProvider extends Component {
}
}

getBlockEditorSettings( settings, meta, onMetaChange, reusableBlocks, hasUploadPermissions ) {
getBlockEditorSettings( settings, reusableBlocks, hasUploadPermissions ) {
return {
...pick( settings, [
'alignWide',
Expand All @@ -95,10 +112,6 @@ class EditorProvider extends Component {
'templateLock',
'titlePlaceholder',
] ),
__experimentalMetaSource: {
value: meta,
onChange: onMetaChange,
},
__experimentalReusableBlocks: reusableBlocks,
__experimentalMediaUpload: hasUploadPermissions ? mediaUpload : undefined,
__experimentalFetchLinkSuggestions: fetchLinkSuggestions,
Expand Down Expand Up @@ -136,8 +149,6 @@ class EditorProvider extends Component {
resetEditorBlocks,
isReady,
settings,
meta,
onMetaChange,
reusableBlocks,
resetEditorBlocksWithoutUndoLevel,
hasUploadPermissions,
Expand All @@ -148,11 +159,11 @@ class EditorProvider extends Component {
}

const editorSettings = this.getBlockEditorSettings(
settings, meta, onMetaChange, reusableBlocks, hasUploadPermissions
settings, reusableBlocks, hasUploadPermissions
);

return (
<BlockEditorProvider
<BlockEditorProviderWithSources
value={ blocks }
onInput={ resetEditorBlocksWithoutUndoLevel }
onChange={ resetEditorBlocks }
Expand All @@ -162,7 +173,7 @@ class EditorProvider extends Component {
{ children }
<ReusableBlocksButtons />
<ConvertToGroupButtons />
</BlockEditorProvider>
</BlockEditorProviderWithSources>
);
}
}
Expand All @@ -173,15 +184,13 @@ export default compose( [
const {
__unstableIsEditorReady: isEditorReady,
getEditorBlocks,
getEditedPostAttribute,
__experimentalGetReusableBlocks,
} = select( 'core/editor' );
const { canUser } = select( 'core' );

return {
isReady: isEditorReady(),
blocks: getEditorBlocks(),
meta: getEditedPostAttribute( 'meta' ),
reusableBlocks: __experimentalGetReusableBlocks(),
hasUploadPermissions: defaultTo( canUser( 'create', 'media' ), true ),
};
Expand All @@ -191,7 +200,6 @@ export default compose( [
setupEditor,
updatePostLock,
resetEditorBlocks,
editPost,
updateEditorSettings,
} = dispatch( 'core/editor' );
const { createWarningNotice } = dispatch( 'core/notices' );
Expand All @@ -207,9 +215,6 @@ export default compose( [
__unstableShouldCreateUndoLevel: false,
} );
},
onMetaChange( meta ) {
editPost( { meta } );
},
};
} ),
] )( EditorProvider );
Loading

0 comments on commit 5adb8ab

Please sign in to comment.