| 1 |
/** |
| 2 |
* External dependencies |
| 3 |
*/ |
| 4 |
import { isEmpty, isObject, isString, isUndefined } from "lodash"; |
| 5 |
|
| 6 |
/** |
| 7 |
* WordPress dependencies |
| 8 |
*/ |
| 9 |
import { addFilter } from "@wordpress/hooks"; |
| 10 |
import { createHigherOrderComponent } from "@wordpress/compose"; |
| 11 |
import { AlignmentToolbar } from "@wordpress/block-editor"; |
| 12 |
import { hasBlockSupport } from "@wordpress/blocks"; |
| 13 |
|
| 14 |
/** |
| 15 |
* Internal dependencies |
| 16 |
*/ |
| 17 |
import { |
| 18 |
getBreakpointType, |
| 19 |
getAllBreakpoints, |
| 20 |
handleChangeResponsiveSettingField, |
| 21 |
getResponsiveSettingFieldValue, |
| 22 |
addEditProps, |
| 23 |
usePropsStyle, |
| 24 |
useBlockFeature, |
| 25 |
} from "sdk/utils"; |
| 26 |
import { boldblocksHasSupport } from "../../utils"; |
| 27 |
import CBBBlockControls from "../../components/block-controls"; |
| 28 |
|
| 29 |
/** |
| 30 |
* Import styles |
| 31 |
*/ |
| 32 |
// import "./index.style.scss"; |
| 33 |
|
| 34 |
/** |
| 35 |
* Define feature name |
| 36 |
*/ |
| 37 |
const featureName = "textAlignment"; |
| 38 |
|
| 39 |
/** |
| 40 |
* Get styles for the feature. |
| 41 |
* |
| 42 |
* @param {Object} textAlignment |
| 43 |
*/ |
| 44 |
function getFeatureStyle(textAlignment) { |
| 45 |
let style = {}; |
| 46 |
if (isObject(textAlignment) && !isEmpty(textAlignment)) { |
| 47 |
Object.keys(textAlignment).forEach((breakpoint) => { |
| 48 |
const { value, inherit } = textAlignment[breakpoint]; |
| 49 |
if (isUndefined(value)) { |
| 50 |
if (inherit && isString(inherit)) { |
| 51 |
const { value: inheritValue } = textAlignment[inherit] ?? {}; |
| 52 |
if (inheritValue) { |
| 53 |
style = { |
| 54 |
...style, |
| 55 |
[`--bb--text-alignment--${breakpoint}`]: `var(--bb--text-alignment--${inherit})`, |
| 56 |
}; |
| 57 |
} |
| 58 |
} |
| 59 |
} else { |
| 60 |
if (value) { |
| 61 |
style = { |
| 62 |
...style, |
| 63 |
[`--bb--text-alignment--${breakpoint}`]: value + "", |
| 64 |
}; |
| 65 |
} |
| 66 |
} |
| 67 |
}); |
| 68 |
} |
| 69 |
|
| 70 |
return style; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Override the default edit UI to include new block controls for custom settings. |
| 75 |
* |
| 76 |
* @param {Function} BlockEdit Block edit component. |
| 77 |
* @return {Function} BlockEdit Modified block edit component. |
| 78 |
*/ |
| 79 |
const withBlockControls = createHigherOrderComponent((BlockEdit) => { |
| 80 |
return (props) => { |
| 81 |
if (!boldblocksHasSupport(props.name, featureName)) { |
| 82 |
return <BlockEdit {...props} />; |
| 83 |
} |
| 84 |
|
| 85 |
const { attributes, setAttributes } = props; |
| 86 |
|
| 87 |
const { shouldDisplayBlockControls, blocks, updateBlockAttributes } = |
| 88 |
useBlockFeature(props); |
| 89 |
|
| 90 |
// Get all breakpoints |
| 91 |
const allBreakpoints = getAllBreakpoints(); |
| 92 |
|
| 93 |
// Get current breakpoint |
| 94 |
const breakpoint = getBreakpointType(); |
| 95 |
|
| 96 |
const onFeatureChange = (fieldName) => (newValue) => { |
| 97 |
handleChangeResponsiveSettingField({ |
| 98 |
fieldName, |
| 99 |
setAttributes, |
| 100 |
attributes, |
| 101 |
breakpoint, |
| 102 |
allBreakpoints, |
| 103 |
blocks, |
| 104 |
updateBlockAttributes, |
| 105 |
})(newValue); |
| 106 |
}; |
| 107 |
|
| 108 |
return ( |
| 109 |
<> |
| 110 |
<BlockEdit {...props} /> |
| 111 |
{shouldDisplayBlockControls && ( |
| 112 |
<CBBBlockControls> |
| 113 |
<AlignmentToolbar |
| 114 |
value={getResponsiveSettingFieldValue({ |
| 115 |
fieldName: "textAlignment", |
| 116 |
attributes, |
| 117 |
breakpoint, |
| 118 |
})} |
| 119 |
onChange={onFeatureChange("textAlignment")} |
| 120 |
/> |
| 121 |
</CBBBlockControls> |
| 122 |
)} |
| 123 |
</> |
| 124 |
); |
| 125 |
}; |
| 126 |
}, "withBlockControls"); |
| 127 |
addFilter( |
| 128 |
"editor.BlockEdit", |
| 129 |
`boldblocks/${featureName}/withBlockControls`, |
| 130 |
withBlockControls, |
| 131 |
); |
| 132 |
|
| 133 |
/** |
| 134 |
* Override props assigned to save component to inject the CSS variables definition. |
| 135 |
* |
| 136 |
* @param {Object} props Additional props applied to save element. |
| 137 |
* @param {Object} blockType Block type. |
| 138 |
* @param {Object} attributes Block attributes. |
| 139 |
* |
| 140 |
* @return {Object} Filtered props applied to save element. |
| 141 |
*/ |
| 142 |
export function addSaveProps(props, blockType, attributes) { |
| 143 |
if (!boldblocksHasSupport(blockType, featureName)) { |
| 144 |
return props; |
| 145 |
} |
| 146 |
|
| 147 |
const { boldblocks: { textAlignment = {} } = {} } = attributes; |
| 148 |
|
| 149 |
const isDeprecatedDynamicStyle = hasBlockSupport( |
| 150 |
blockType, |
| 151 |
"CBBDeprecatedDynamicStyleV1", |
| 152 |
); |
| 153 |
|
| 154 |
let featureStyles = {}; |
| 155 |
if (attributes?.isBlockEdit) { |
| 156 |
featureStyles = usePropsStyle({ |
| 157 |
value: textAlignment, |
| 158 |
getStyle: (value) => () => getFeatureStyle(value), |
| 159 |
}); |
| 160 |
} else { |
| 161 |
if (isDeprecatedDynamicStyle) { |
| 162 |
featureStyles = getFeatureStyle(textAlignment); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
props.style = { |
| 167 |
...props.style, |
| 168 |
...featureStyles, |
| 169 |
}; |
| 170 |
|
| 171 |
return props; |
| 172 |
} |
| 173 |
addFilter( |
| 174 |
"blocks.getSaveContent.extraProps", |
| 175 |
`boldblocks/${featureName}/addSaveProps`, |
| 176 |
addSaveProps, |
| 177 |
); |
| 178 |
|
| 179 |
/** |
| 180 |
* Filters registered block settings to extend the block edit wrapper |
| 181 |
* to apply the desired styles and className properly. |
| 182 |
* |
| 183 |
* @param {Object} settings Original block settings. |
| 184 |
* |
| 185 |
* @return {Object}.Filtered block settings. |
| 186 |
*/ |
| 187 |
addFilter( |
| 188 |
"blocks.registerBlockType", |
| 189 |
`boldblocks/${featureName}/addEditProps`, |
| 190 |
addEditProps( |
| 191 |
addSaveProps, |
| 192 |
(settings, { featureName }) => boldblocksHasSupport(settings, featureName), |
| 193 |
{ featureName }, |
| 194 |
), |
| 195 |
); |
| 196 |
|