@wordpress/block-library
Version:
Block library for the WordPress editor.
1,083 lines (1,019 loc) • 26.4 kB
JavaScript
import { createElement } from "@wordpress/element";
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { RichText, getColorClassName, useBlockProps, __experimentalGetGradientClass, __experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles, __experimentalGetColorClassesAndStyles as getColorClassesAndStyles, __experimentalGetSpacingClassesAndStyles as getSpacingClassesAndStyles } from '@wordpress/block-editor';
import { compose } from '@wordpress/compose';
/**
* Internal dependencies
*/
import migrateFontFamily from '../utils/migrate-font-family';
const migrateBorderRadius = attributes => {
var _newAttributes$style, _newAttributes$style$, _newAttributes$style2;
const {
borderRadius,
...newAttributes
} = attributes; // We have to check old property `borderRadius` and if
// `styles.border.radius` is a `number`
const oldBorderRadius = [borderRadius, (_newAttributes$style = newAttributes.style) === null || _newAttributes$style === void 0 ? void 0 : (_newAttributes$style$ = _newAttributes$style.border) === null || _newAttributes$style$ === void 0 ? void 0 : _newAttributes$style$.radius].find(possibleBorderRadius => {
return typeof possibleBorderRadius === 'number' && possibleBorderRadius !== 0;
});
if (!oldBorderRadius) {
return newAttributes;
}
return { ...newAttributes,
style: { ...newAttributes.style,
border: { ...((_newAttributes$style2 = newAttributes.style) === null || _newAttributes$style2 === void 0 ? void 0 : _newAttributes$style2.border),
radius: `${oldBorderRadius}px`
}
}
};
};
const migrateCustomColorsAndGradients = attributes => {
if (!attributes.customTextColor && !attributes.customBackgroundColor && !attributes.customGradient) {
return attributes;
}
const style = {
color: {}
};
if (attributes.customTextColor) {
style.color.text = attributes.customTextColor;
}
if (attributes.customBackgroundColor) {
style.color.background = attributes.customBackgroundColor;
}
if (attributes.customGradient) {
style.color.gradient = attributes.customGradient;
}
const {
customTextColor,
customBackgroundColor,
customGradient,
...restAttributes
} = attributes;
return { ...restAttributes,
style
};
};
const oldColorsMigration = attributes => {
const {
color,
textColor,
...restAttributes
} = { ...attributes,
customTextColor: attributes.textColor && '#' === attributes.textColor[0] ? attributes.textColor : undefined,
customBackgroundColor: attributes.color && '#' === attributes.color[0] ? attributes.color : undefined
};
return migrateCustomColorsAndGradients(restAttributes);
};
const blockAttributes = {
url: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'href'
},
title: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'title'
},
text: {
type: 'string',
source: 'html',
selector: 'a'
}
};
const v11 = {
attributes: {
url: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'href'
},
title: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'title'
},
text: {
type: 'string',
source: 'html',
selector: 'a'
},
linkTarget: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'target'
},
rel: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'rel'
},
placeholder: {
type: 'string'
},
backgroundColor: {
type: 'string'
},
textColor: {
type: 'string'
},
gradient: {
type: 'string'
},
width: {
type: 'number'
}
},
supports: {
anchor: true,
align: true,
alignWide: false,
color: {
__experimentalSkipSerialization: true,
gradients: true,
__experimentalDefaultControls: {
background: true,
text: true
}
},
typography: {
fontSize: true,
__experimentalFontFamily: true,
__experimentalDefaultControls: {
fontSize: true
}
},
reusable: false,
spacing: {
__experimentalSkipSerialization: true,
padding: ['horizontal', 'vertical'],
__experimentalDefaultControls: {
padding: true
}
},
__experimentalBorder: {
radius: true,
__experimentalSkipSerialization: true,
__experimentalDefaultControls: {
radius: true
}
},
__experimentalSelector: '.wp-block-button__link'
},
save(_ref) {
var _style$border, _style$typography;
let {
attributes,
className
} = _ref;
const {
fontSize,
linkTarget,
rel,
style,
text,
title,
url,
width
} = attributes;
if (!text) {
return null;
}
const borderProps = getBorderClassesAndStyles(attributes);
const colorProps = getColorClassesAndStyles(attributes);
const spacingProps = getSpacingClassesAndStyles(attributes);
const buttonClasses = classnames('wp-block-button__link', colorProps.className, borderProps.className, {
// For backwards compatibility add style that isn't provided via
// block support.
'no-border-radius': (style === null || style === void 0 ? void 0 : (_style$border = style.border) === null || _style$border === void 0 ? void 0 : _style$border.radius) === 0
});
const buttonStyle = { ...borderProps.style,
...colorProps.style,
...spacingProps.style
}; // The use of a `title` attribute here is soft-deprecated, but still applied
// if it had already been assigned, for the sake of backward-compatibility.
// A title will no longer be assigned for new or updated button block links.
const wrapperClasses = classnames(className, {
[`has-custom-width wp-block-button__width-${width}`]: width,
[`has-custom-font-size`]: fontSize || (style === null || style === void 0 ? void 0 : (_style$typography = style.typography) === null || _style$typography === void 0 ? void 0 : _style$typography.fontSize)
});
return createElement("div", useBlockProps.save({
className: wrapperClasses
}), createElement(RichText.Content, {
tagName: "a",
className: buttonClasses,
href: url,
title: title,
style: buttonStyle,
value: text,
target: linkTarget,
rel: rel
}));
}
};
const v10 = {
attributes: {
url: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'href'
},
title: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'title'
},
text: {
type: 'string',
source: 'html',
selector: 'a'
},
linkTarget: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'target'
},
rel: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'rel'
},
placeholder: {
type: 'string'
},
backgroundColor: {
type: 'string'
},
textColor: {
type: 'string'
},
gradient: {
type: 'string'
},
width: {
type: 'number'
}
},
supports: {
anchor: true,
align: true,
alignWide: false,
color: {
__experimentalSkipSerialization: true,
gradients: true
},
typography: {
fontSize: true,
__experimentalFontFamily: true
},
reusable: false,
spacing: {
__experimentalSkipSerialization: true,
padding: ['horizontal', 'vertical'],
__experimentalDefaultControls: {
padding: true
}
},
__experimentalBorder: {
radius: true,
__experimentalSkipSerialization: true
},
__experimentalSelector: '.wp-block-button__link'
},
save(_ref2) {
var _style$border2, _style$typography2;
let {
attributes,
className
} = _ref2;
const {
fontSize,
linkTarget,
rel,
style,
text,
title,
url,
width
} = attributes;
if (!text) {
return null;
}
const borderProps = getBorderClassesAndStyles(attributes);
const colorProps = getColorClassesAndStyles(attributes);
const spacingProps = getSpacingClassesAndStyles(attributes);
const buttonClasses = classnames('wp-block-button__link', colorProps.className, borderProps.className, {
// For backwards compatibility add style that isn't provided via
// block support.
'no-border-radius': (style === null || style === void 0 ? void 0 : (_style$border2 = style.border) === null || _style$border2 === void 0 ? void 0 : _style$border2.radius) === 0
});
const buttonStyle = { ...borderProps.style,
...colorProps.style,
...spacingProps.style
}; // The use of a `title` attribute here is soft-deprecated, but still applied
// if it had already been assigned, for the sake of backward-compatibility.
// A title will no longer be assigned for new or updated button block links.
const wrapperClasses = classnames(className, {
[`has-custom-width wp-block-button__width-${width}`]: width,
[`has-custom-font-size`]: fontSize || (style === null || style === void 0 ? void 0 : (_style$typography2 = style.typography) === null || _style$typography2 === void 0 ? void 0 : _style$typography2.fontSize)
});
return createElement("div", useBlockProps.save({
className: wrapperClasses
}), createElement(RichText.Content, {
tagName: "a",
className: buttonClasses,
href: url,
title: title,
style: buttonStyle,
value: text,
target: linkTarget,
rel: rel
}));
},
migrate: migrateFontFamily,
isEligible(_ref3) {
var _style$typography3;
let {
style
} = _ref3;
return style === null || style === void 0 ? void 0 : (_style$typography3 = style.typography) === null || _style$typography3 === void 0 ? void 0 : _style$typography3.fontFamily;
}
};
const deprecated = [v11, v10, {
supports: {
anchor: true,
align: true,
alignWide: false,
color: {
__experimentalSkipSerialization: true,
gradients: true
},
typography: {
fontSize: true,
__experimentalFontFamily: true
},
reusable: false,
__experimentalSelector: '.wp-block-button__link'
},
attributes: { ...blockAttributes,
linkTarget: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'target'
},
rel: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'rel'
},
placeholder: {
type: 'string'
},
backgroundColor: {
type: 'string'
},
textColor: {
type: 'string'
},
gradient: {
type: 'string'
},
width: {
type: 'number'
}
},
isEligible(_ref4) {
var _style$border3;
let {
style
} = _ref4;
return typeof (style === null || style === void 0 ? void 0 : (_style$border3 = style.border) === null || _style$border3 === void 0 ? void 0 : _style$border3.radius) === 'number';
},
save(_ref5) {
var _style$border4, _style$border5, _style$typography4;
let {
attributes,
className
} = _ref5;
const {
fontSize,
linkTarget,
rel,
style,
text,
title,
url,
width
} = attributes;
if (!text) {
return null;
}
const borderRadius = style === null || style === void 0 ? void 0 : (_style$border4 = style.border) === null || _style$border4 === void 0 ? void 0 : _style$border4.radius;
const colorProps = getColorClassesAndStyles(attributes);
const buttonClasses = classnames('wp-block-button__link', colorProps.className, {
'no-border-radius': (style === null || style === void 0 ? void 0 : (_style$border5 = style.border) === null || _style$border5 === void 0 ? void 0 : _style$border5.radius) === 0
});
const buttonStyle = {
borderRadius: borderRadius ? borderRadius : undefined,
...colorProps.style
}; // The use of a `title` attribute here is soft-deprecated, but still applied
// if it had already been assigned, for the sake of backward-compatibility.
// A title will no longer be assigned for new or updated button block links.
const wrapperClasses = classnames(className, {
[`has-custom-width wp-block-button__width-${width}`]: width,
[`has-custom-font-size`]: fontSize || (style === null || style === void 0 ? void 0 : (_style$typography4 = style.typography) === null || _style$typography4 === void 0 ? void 0 : _style$typography4.fontSize)
});
return createElement("div", useBlockProps.save({
className: wrapperClasses
}), createElement(RichText.Content, {
tagName: "a",
className: buttonClasses,
href: url,
title: title,
style: buttonStyle,
value: text,
target: linkTarget,
rel: rel
}));
},
migrate: compose(migrateFontFamily, migrateBorderRadius)
}, {
supports: {
anchor: true,
align: true,
alignWide: false,
color: {
__experimentalSkipSerialization: true
},
reusable: false,
__experimentalSelector: '.wp-block-button__link'
},
attributes: { ...blockAttributes,
linkTarget: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'target'
},
rel: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'rel'
},
placeholder: {
type: 'string'
},
borderRadius: {
type: 'number'
},
backgroundColor: {
type: 'string'
},
textColor: {
type: 'string'
},
gradient: {
type: 'string'
},
style: {
type: 'object'
},
width: {
type: 'number'
}
},
save(_ref6) {
let {
attributes,
className
} = _ref6;
const {
borderRadius,
linkTarget,
rel,
text,
title,
url,
width
} = attributes;
const colorProps = getColorClassesAndStyles(attributes);
const buttonClasses = classnames('wp-block-button__link', colorProps.className, {
'no-border-radius': borderRadius === 0
});
const buttonStyle = {
borderRadius: borderRadius ? borderRadius + 'px' : undefined,
...colorProps.style
}; // The use of a `title` attribute here is soft-deprecated, but still applied
// if it had already been assigned, for the sake of backward-compatibility.
// A title will no longer be assigned for new or updated button block links.
const wrapperClasses = classnames(className, {
[`has-custom-width wp-block-button__width-${width}`]: width
});
return createElement("div", useBlockProps.save({
className: wrapperClasses
}), createElement(RichText.Content, {
tagName: "a",
className: buttonClasses,
href: url,
title: title,
style: buttonStyle,
value: text,
target: linkTarget,
rel: rel
}));
},
migrate: compose(migrateFontFamily, migrateBorderRadius)
}, {
supports: {
anchor: true,
align: true,
alignWide: false,
color: {
__experimentalSkipSerialization: true
},
reusable: false,
__experimentalSelector: '.wp-block-button__link'
},
attributes: { ...blockAttributes,
linkTarget: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'target'
},
rel: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'rel'
},
placeholder: {
type: 'string'
},
borderRadius: {
type: 'number'
},
backgroundColor: {
type: 'string'
},
textColor: {
type: 'string'
},
gradient: {
type: 'string'
},
style: {
type: 'object'
},
width: {
type: 'number'
}
},
save(_ref7) {
let {
attributes,
className
} = _ref7;
const {
borderRadius,
linkTarget,
rel,
text,
title,
url,
width
} = attributes;
const colorProps = getColorClassesAndStyles(attributes);
const buttonClasses = classnames('wp-block-button__link', colorProps.className, {
'no-border-radius': borderRadius === 0
});
const buttonStyle = {
borderRadius: borderRadius ? borderRadius + 'px' : undefined,
...colorProps.style
}; // The use of a `title` attribute here is soft-deprecated, but still applied
// if it had already been assigned, for the sake of backward-compatibility.
// A title will no longer be assigned for new or updated button block links.
const wrapperClasses = classnames(className, {
[`has-custom-width wp-block-button__width-${width}`]: width
});
return createElement("div", useBlockProps.save({
className: wrapperClasses
}), createElement(RichText.Content, {
tagName: "a",
className: buttonClasses,
href: url,
title: title,
style: buttonStyle,
value: text,
target: linkTarget,
rel: rel
}));
},
migrate: compose(migrateFontFamily, migrateBorderRadius)
}, {
supports: {
align: true,
alignWide: false,
color: {
gradients: true
}
},
attributes: { ...blockAttributes,
linkTarget: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'target'
},
rel: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'rel'
},
placeholder: {
type: 'string'
},
borderRadius: {
type: 'number'
},
backgroundColor: {
type: 'string'
},
textColor: {
type: 'string'
},
gradient: {
type: 'string'
},
style: {
type: 'object'
}
},
save(_ref8) {
let {
attributes
} = _ref8;
const {
borderRadius,
linkTarget,
rel,
text,
title,
url
} = attributes;
const buttonClasses = classnames('wp-block-button__link', {
'no-border-radius': borderRadius === 0
});
const buttonStyle = {
borderRadius: borderRadius ? borderRadius + 'px' : undefined
};
return createElement(RichText.Content, {
tagName: "a",
className: buttonClasses,
href: url,
title: title,
style: buttonStyle,
value: text,
target: linkTarget,
rel: rel
});
},
migrate: migrateBorderRadius
}, {
supports: {
align: true,
alignWide: false
},
attributes: { ...blockAttributes,
linkTarget: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'target'
},
rel: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'rel'
},
placeholder: {
type: 'string'
},
borderRadius: {
type: 'number'
},
backgroundColor: {
type: 'string'
},
textColor: {
type: 'string'
},
customBackgroundColor: {
type: 'string'
},
customTextColor: {
type: 'string'
},
customGradient: {
type: 'string'
},
gradient: {
type: 'string'
}
},
isEligible: attributes => !!attributes.customTextColor || !!attributes.customBackgroundColor || !!attributes.customGradient,
migrate: compose(migrateBorderRadius, migrateCustomColorsAndGradients),
save(_ref9) {
let {
attributes
} = _ref9;
const {
backgroundColor,
borderRadius,
customBackgroundColor,
customTextColor,
customGradient,
linkTarget,
gradient,
rel,
text,
textColor,
title,
url
} = attributes;
const textClass = getColorClassName('color', textColor);
const backgroundClass = !customGradient && getColorClassName('background-color', backgroundColor);
const gradientClass = __experimentalGetGradientClass(gradient);
const buttonClasses = classnames('wp-block-button__link', {
'has-text-color': textColor || customTextColor,
[textClass]: textClass,
'has-background': backgroundColor || customBackgroundColor || customGradient || gradient,
[backgroundClass]: backgroundClass,
'no-border-radius': borderRadius === 0,
[gradientClass]: gradientClass
});
const buttonStyle = {
background: customGradient ? customGradient : undefined,
backgroundColor: backgroundClass || customGradient || gradient ? undefined : customBackgroundColor,
color: textClass ? undefined : customTextColor,
borderRadius: borderRadius ? borderRadius + 'px' : undefined
}; // The use of a `title` attribute here is soft-deprecated, but still applied
// if it had already been assigned, for the sake of backward-compatibility.
// A title will no longer be assigned for new or updated button block links.
return createElement("div", null, createElement(RichText.Content, {
tagName: "a",
className: buttonClasses,
href: url,
title: title,
style: buttonStyle,
value: text,
target: linkTarget,
rel: rel
}));
}
}, {
attributes: { ...blockAttributes,
align: {
type: 'string',
default: 'none'
},
backgroundColor: {
type: 'string'
},
textColor: {
type: 'string'
},
customBackgroundColor: {
type: 'string'
},
customTextColor: {
type: 'string'
},
linkTarget: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'target'
},
rel: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'rel'
},
placeholder: {
type: 'string'
}
},
isEligible(attribute) {
return attribute.className && attribute.className.includes('is-style-squared');
},
migrate(attributes) {
let newClassName = attributes.className;
if (newClassName) {
newClassName = newClassName.replace(/is-style-squared[\s]?/, '').trim();
}
return migrateBorderRadius(migrateCustomColorsAndGradients({ ...attributes,
className: newClassName ? newClassName : undefined,
borderRadius: 0
}));
},
save(_ref10) {
let {
attributes
} = _ref10;
const {
backgroundColor,
customBackgroundColor,
customTextColor,
linkTarget,
rel,
text,
textColor,
title,
url
} = attributes;
const textClass = getColorClassName('color', textColor);
const backgroundClass = getColorClassName('background-color', backgroundColor);
const buttonClasses = classnames('wp-block-button__link', {
'has-text-color': textColor || customTextColor,
[textClass]: textClass,
'has-background': backgroundColor || customBackgroundColor,
[backgroundClass]: backgroundClass
});
const buttonStyle = {
backgroundColor: backgroundClass ? undefined : customBackgroundColor,
color: textClass ? undefined : customTextColor
};
return createElement("div", null, createElement(RichText.Content, {
tagName: "a",
className: buttonClasses,
href: url,
title: title,
style: buttonStyle,
value: text,
target: linkTarget,
rel: rel
}));
}
}, {
attributes: { ...blockAttributes,
align: {
type: 'string',
default: 'none'
},
backgroundColor: {
type: 'string'
},
textColor: {
type: 'string'
},
customBackgroundColor: {
type: 'string'
},
customTextColor: {
type: 'string'
}
},
migrate: oldColorsMigration,
save(_ref11) {
let {
attributes
} = _ref11;
const {
url,
text,
title,
backgroundColor,
textColor,
customBackgroundColor,
customTextColor
} = attributes;
const textClass = getColorClassName('color', textColor);
const backgroundClass = getColorClassName('background-color', backgroundColor);
const buttonClasses = classnames('wp-block-button__link', {
'has-text-color': textColor || customTextColor,
[textClass]: textClass,
'has-background': backgroundColor || customBackgroundColor,
[backgroundClass]: backgroundClass
});
const buttonStyle = {
backgroundColor: backgroundClass ? undefined : customBackgroundColor,
color: textClass ? undefined : customTextColor
};
return createElement("div", null, createElement(RichText.Content, {
tagName: "a",
className: buttonClasses,
href: url,
title: title,
style: buttonStyle,
value: text
}));
}
}, {
attributes: { ...blockAttributes,
color: {
type: 'string'
},
textColor: {
type: 'string'
},
align: {
type: 'string',
default: 'none'
}
},
save(_ref12) {
let {
attributes
} = _ref12;
const {
url,
text,
title,
align,
color,
textColor
} = attributes;
const buttonStyle = {
backgroundColor: color,
color: textColor
};
const linkClass = 'wp-block-button__link';
return createElement("div", {
className: `align${align}`
}, createElement(RichText.Content, {
tagName: "a",
className: linkClass,
href: url,
title: title,
style: buttonStyle,
value: text
}));
},
migrate: oldColorsMigration
}, {
attributes: { ...blockAttributes,
color: {
type: 'string'
},
textColor: {
type: 'string'
},
align: {
type: 'string',
default: 'none'
}
},
save(_ref13) {
let {
attributes
} = _ref13;
const {
url,
text,
title,
align,
color,
textColor
} = attributes;
return createElement("div", {
className: `align${align}`,
style: {
backgroundColor: color
}
}, createElement(RichText.Content, {
tagName: "a",
href: url,
title: title,
style: {
color: textColor
},
value: text
}));
},
migrate: oldColorsMigration
}];
export default deprecated;
//# sourceMappingURL=deprecated.js.map