-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ export { | |
getBlockTypes, | ||
hasBlockSupport, | ||
} from './registration'; | ||
|
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 } ) { | ||
addFilter( 'registerBlockType', 'core\matchers', resolveAttributes ); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,6 +156,7 @@ class ButtonBlock extends Component { | |
</span>, | ||
focus && ( | ||
<form | ||
key="form-link" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this change intentional? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" /> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copypasta.