| 1 |
/** |
| 2 |
* External dependencies |
| 3 |
*/ |
| 4 |
|
| 5 |
/** |
| 6 |
* WordPress dependencies |
| 7 |
*/ |
| 8 |
import { __ } from "@wordpress/i18n"; |
| 9 |
import { addFilter } from "@wordpress/hooks"; |
| 10 |
import { createHigherOrderComponent } from "@wordpress/compose"; |
| 11 |
import { InspectorControls } from "@wordpress/block-editor"; |
| 12 |
import { PanelBody } from "@wordpress/components"; |
| 13 |
|
| 14 |
/** |
| 15 |
* Internal dependencies |
| 16 |
*/ |
| 17 |
import { getSettingFieldValue } from "sdk/utils"; |
| 18 |
import { CodeEditor } from "../../components/code-editor"; |
| 19 |
import { hasSupportCSS } from "./index"; |
| 20 |
import { labels, getProLabel } from "../../utils/labels"; |
| 21 |
import { |
| 22 |
useStyleBlockControls, |
| 23 |
userCanEditCSS, |
| 24 |
useCustomCSSWarning, |
| 25 |
} from "../../utils"; |
| 26 |
|
| 27 |
/** |
| 28 |
* Define feature name |
| 29 |
*/ |
| 30 |
const featureName = "customCSS"; |
| 31 |
|
| 32 |
/** |
| 33 |
* Override the default edit UI to include new inspector controls for custom settings. |
| 34 |
* |
| 35 |
* @param {Function} BlockEdit Block edit component. |
| 36 |
* @return {Function} BlockEdit Modified block edit component. |
| 37 |
*/ |
| 38 |
const withInspectorControls = createHigherOrderComponent((BlockEdit) => { |
| 39 |
return (props) => { |
| 40 |
if (!hasSupportCSS(props.name)) { |
| 41 |
return <BlockEdit {...props} />; |
| 42 |
} |
| 43 |
|
| 44 |
const displayBlockControls = useStyleBlockControls(); |
| 45 |
|
| 46 |
const { isSelected, attributes, clientId } = props; |
| 47 |
|
| 48 |
const customCSS = getSettingFieldValue({ |
| 49 |
fieldName: "customCSS", |
| 50 |
attributes, |
| 51 |
defaultValue: {}, |
| 52 |
}); |
| 53 |
|
| 54 |
const { value = "" } = customCSS; |
| 55 |
|
| 56 |
const canEditCSS = userCanEditCSS(); |
| 57 |
|
| 58 |
// Show the warning |
| 59 |
useCustomCSSWarning({ canEditCSS, hasCustomCSS: !!value.trim() }); |
| 60 |
|
| 61 |
if (!canEditCSS) { |
| 62 |
return <BlockEdit {...props} />; |
| 63 |
} |
| 64 |
|
| 65 |
return ( |
| 66 |
<> |
| 67 |
<BlockEdit {...props} /> |
| 68 |
{isSelected && displayBlockControls && ( |
| 69 |
<InspectorControls group="styles"> |
| 70 |
<PanelBody |
| 71 |
title={getProLabel(labels.customCSS)} |
| 72 |
initialOpen={!!value} |
| 73 |
className="cbb-settings" |
| 74 |
> |
| 75 |
{!!value ? ( |
| 76 |
<CodeEditor |
| 77 |
id={`cbb-css-${clientId}`} |
| 78 |
value={value} |
| 79 |
help={labels.customCSSHelp} |
| 80 |
/> |
| 81 |
) : ( |
| 82 |
<p>{labels.customCSSHelp}</p> |
| 83 |
)} |
| 84 |
</PanelBody> |
| 85 |
</InspectorControls> |
| 86 |
)} |
| 87 |
</> |
| 88 |
); |
| 89 |
}; |
| 90 |
}, "withInspectorControls"); |
| 91 |
addFilter( |
| 92 |
"editor.BlockEdit", |
| 93 |
`boldblocks/${featureName}/withInspectorControls`, |
| 94 |
withInspectorControls, |
| 95 |
); |
| 96 |
|