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

Block API: Ensure backwards compatibility for block matchers #3519

Merged
merged 2 commits into from
Nov 20, 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
1 change: 1 addition & 0 deletions blocks/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export {
getBlockTypes,
hasBlockSupport,
} from './registration';

File renamed without changes.
2 changes: 1 addition & 1 deletion blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getBlockType, getUnknownTypeHandlerName } from './registration';
import { createBlock } from './factory';
import { isValidBlock } from './validation';
import { getCommentDelimitedContent } from './serializer';
import { attr, prop, html, text, query, node, children } from './source';
import { attr, prop, html, text, query, node, children } from './matchers';

/**
* Returns value coerced to the specified JSON schema type string
Expand Down
4 changes: 2 additions & 2 deletions blocks/api/test/source.js → blocks/api/test/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { renderToString } from '@wordpress/element';
/**
* Internal dependencies
*/
import * as sources from '../source';
import * as sources from '../matchers';

describe( 'sources', () => {
describe( 'matchers', () => {
describe( 'children()', () => {
it( 'should return a source function', () => {
const source = sources.children();
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 );
96 changes: 96 additions & 0 deletions blocks/hooks/matchers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* External dependencies
*/
import { isFunction, mapValues } from 'lodash';

function warnAboutDeprecatedMatcher() {
// eslint-disable-next-line no-console
console.warn(
'Attributes matchers are deprecated and they will be removed in a future version of Gutenberg. ' +
'Please update your attributes definition https://wordpress.org/gutenberg/handbook/reference/attributes/'
);
}

export const attr = ( selector, attribute ) => () => {
warnAboutDeprecatedMatcher();
return {
source: 'attribute',
attribute: attribute === undefined ? selector : attribute,
selector: attribute === undefined ? undefined : selector,
};
};

export const prop = ( selector, property ) => () => {
warnAboutDeprecatedMatcher();
return {
source: 'property',
property: property === undefined ? selector : property,
selector: property === undefined ? undefined : selector,
};
};

export const html = ( selector ) => () => {
warnAboutDeprecatedMatcher();
return {
source: 'html',
selector,
};
};

export const text = ( selector ) => () => {
warnAboutDeprecatedMatcher();
return {
source: 'text',
selector,
};
};

export const query = ( selector, subMatchers ) => () => {
warnAboutDeprecatedMatcher();
return {
source: 'query',
selector,
query: subMatchers,
};
};

export const children = ( selector ) => () => {
warnAboutDeprecatedMatcher();
return {
source: 'children',
selector,
};
};

export const node = ( selector ) => () => {
warnAboutDeprecatedMatcher();
return {
source: 'node',
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 } ) {
Copy link
Member

Choose a reason for hiding this comment

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

Copypasta.

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 };
1 change: 1 addition & 0 deletions blocks/library/button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class ButtonBlock extends Component {
</span>,
focus && (
<form
key="form-link"
Copy link
Member

Choose a reason for hiding this comment

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

Was this change intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, this was a bug fix I wanted to publish separately and accidentally made it to this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can extract it to a separate PR but maybe not worth it

className="blocks-button__inline-link"
onSubmit={ ( event ) => event.preventDefault() }>
<Dashicon icon="admin-links" />
Expand Down