Skip to content

Commit

Permalink
Block API: Use an extension to ensure matchers' backwards compatibiliy
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 20, 2017
1 parent 8252a6b commit 7f5281c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
3 changes: 0 additions & 3 deletions blocks/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,3 @@ export {
hasBlockSupport,
} from './registration';

// Deprecated matchers
import * as source from './source';
export { source };
13 changes: 1 addition & 12 deletions blocks/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* External dependencies
*/
import { get, isFunction, some, mapValues } from 'lodash';
import { get, isFunction, some } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -119,17 +119,6 @@ export function registerBlockType( name, settings ) {
...settings,
};

// Resolve deprecated attributes
settings.attributes = mapValues( settings.attributes, ( attribute ) => {
if ( isFunction( attribute.source ) ) {
return {
...attribute,
...attribute.source(),
};
}
return attribute;
} );

settings = applyFilters( 'registerBlockType', settings, name );

return blocks[ name ] = settings;
Expand Down
2 changes: 2 additions & 0 deletions blocks/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import createHooks from '@wordpress/hooks';
*/
import anchor from './anchor';
import customClassName from './custom-class-name';
import matchers from './matchers';

const hooks = createHooks();

Expand Down Expand Up @@ -47,3 +48,4 @@ export {

anchor( hooks );
customClassName( hooks );
matchers( hooks );
30 changes: 30 additions & 0 deletions blocks/api/source.js → blocks/hooks/matchers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { isFunction, mapValues } from 'lodash';

function warnAboutDeprecatedMatcher() {
// eslint-disable-next-line no-console
console.warn(
Expand Down Expand Up @@ -64,3 +69,28 @@ export const node = ( selector ) => () => {
selector,
};
};

/**
* Resolve the matchers attributes for backwards compatibilty
*
* @param {Object} settings Original block settings
* @return {Object} Filtered block settings
*/
export function resolveAttributes( settings ) {
// Resolve deprecated attributes
settings.attributes = mapValues( settings.attributes, ( attribute ) => {
if ( isFunction( attribute.source ) ) {
return {
...attribute,
...attribute.source(),
};
}
return attribute;
} );

return settings;
}

export default function anchor( { addFilter } ) {
addFilter( 'registerBlockType', 'core\matchers', resolveAttributes );
}
4 changes: 4 additions & 0 deletions blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ export { default as InspectorControls } from './inspector-controls';
export { default as MediaUploadButton } from './media-upload-button';
export { default as UrlInput } from './url-input';
export { default as UrlInputButton } from './url-input/button';

// Deprecated matchers
import { attr, prop, text, html, query, node, children } from './hooks/matchers';
export const source = { attr, prop, text, html, query, node, children };

0 comments on commit 7f5281c

Please sign in to comment.