| 1 |
/** |
| 2 |
* External dependencies |
| 3 |
*/ |
| 4 |
import clsx from "clsx"; |
| 5 |
|
| 6 |
/** |
| 7 |
* WordPress dependencies |
| 8 |
*/ |
| 9 |
import { __ } from "@wordpress/i18n"; |
| 10 |
import { addFilter } from "@wordpress/hooks"; |
| 11 |
import { createHigherOrderComponent } from "@wordpress/compose"; |
| 12 |
|
| 13 |
/** |
| 14 |
* Internal dependencies |
| 15 |
*/ |
| 16 |
import { boldblocksHasSupport, refineCustomCSS } from "../../utils"; |
| 17 |
|
| 18 |
/** |
| 19 |
* Define feature name |
| 20 |
*/ |
| 21 |
const featureName = "customCSS"; |
| 22 |
|
| 23 |
/** |
| 24 |
* If the current block support custom CSS |
| 25 |
* |
| 26 |
* @param {String} name |
| 27 |
* @returns {Boolean} |
| 28 |
*/ |
| 29 |
export const hasSupportCSS = (name) => |
| 30 |
boldblocksHasSupport(name, featureName) || |
| 31 |
["core/template-part"].includes(name); |
| 32 |
|
| 33 |
/** |
| 34 |
* Override the default block list element to add custom styles. |
| 35 |
* |
| 36 |
* @param {Function} BlockListBlock Original component |
| 37 |
* @return {Function} Wrapped component |
| 38 |
*/ |
| 39 |
export const withCustomStyles = createHigherOrderComponent( |
| 40 |
(BlockListBlock) => (props) => { |
| 41 |
if (!hasSupportCSS(props.name)) { |
| 42 |
return <BlockListBlock {...props} />; |
| 43 |
} |
| 44 |
|
| 45 |
const { |
| 46 |
attributes: { boldblocks: { customCSS: { value = "" } = {} } = {} }, |
| 47 |
clientId, |
| 48 |
} = props; |
| 49 |
|
| 50 |
const selector = `b-${clientId}`; |
| 51 |
let style; |
| 52 |
if (value) { |
| 53 |
style = refineCustomCSS(value, { selector: `.${selector}` }); |
| 54 |
} |
| 55 |
|
| 56 |
return ( |
| 57 |
<> |
| 58 |
<BlockListBlock |
| 59 |
{...props} |
| 60 |
className={clsx(props?.className, { |
| 61 |
[selector]: style, |
| 62 |
})} |
| 63 |
/> |
| 64 |
{style && <style>{style}</style>} |
| 65 |
</> |
| 66 |
); |
| 67 |
}, |
| 68 |
); |
| 69 |
addFilter( |
| 70 |
"editor.BlockListBlock", |
| 71 |
`boldblocks/${featureName}/withCustomStyles`, |
| 72 |
withCustomStyles, |
| 73 |
); |
| 74 |
|