| 1 |
import { |
| 2 |
FontSizePicker, |
| 3 |
InspectorControls, |
| 4 |
useBlockEditContext, |
| 5 |
} from "@wordpress/block-editor"; |
| 6 |
import { __ } from "@wordpress/i18n"; |
| 7 |
import { ColorControl, SpacingControl } from "@tableberg/components"; |
| 8 |
import { |
| 9 |
BaseControl, |
| 10 |
PanelBody, |
| 11 |
__experimentalToolsPanel as ToolsPanel, |
| 12 |
} from "@wordpress/components"; |
| 13 |
|
| 14 |
import { textAttributeDefaults, TextElementAttributes } from "./element"; |
| 15 |
import { useTableStore } from "../../store"; |
| 16 |
import { ElementBindings } from "../../dynamic-data/types"; |
| 17 |
|
| 18 |
const hasSides = (sides: { |
| 19 |
top: string; |
| 20 |
right: string; |
| 21 |
bottom: string; |
| 22 |
left: string; |
| 23 |
}) => !!sides.top || !!sides.right || !!sides.bottom || !!sides.left; |
| 24 |
|
| 25 |
export function TextElementControls({ |
| 26 |
attributes, |
| 27 |
bindings, |
| 28 |
updateStyles: updateStylesProp, |
| 29 |
}: { |
| 30 |
attributes: TextElementAttributes; |
| 31 |
bindings?: ElementBindings; |
| 32 |
// Native block edits inject a setAttributes-based updater; store-backed |
| 33 |
// element renderers use the table-store fallback. |
| 34 |
updateStyles?: (styles: Partial<TextElementAttributes["styles"]>) => void; |
| 35 |
}) { |
| 36 |
const storeUpdateStyles = useTableStore( |
| 37 |
state => state.updateSelectedElementStyles |
| 38 |
); |
| 39 |
const updateStyles = updateStylesProp ?? storeUpdateStyles; |
| 40 |
const { clientId } = useBlockEditContext(); |
| 41 |
|
| 42 |
const textColorIsBound = !!bindings?.["styles.textColor"]; |
| 43 |
const linkColorIsBound = !!bindings?.["styles.linkColor"]; |
| 44 |
const backgroundColorIsBound = !!bindings?.["styles.backgroundColor"]; |
| 45 |
const fontSizeIsBound = !!bindings?.["styles.fontSize"]; |
| 46 |
|
| 47 |
return ( |
| 48 |
<> |
| 49 |
<InspectorControls group="color"> |
| 50 |
<ColorControl |
| 51 |
label={ |
| 52 |
textColorIsBound |
| 53 |
? __("Text Color (dynamic)", "tableberg") |
| 54 |
: __("Text Color", "tableberg") |
| 55 |
} |
| 56 |
value={attributes.styles.textColor} |
| 57 |
onChange={textColor => updateStyles({ textColor })} |
| 58 |
onDeselect={() => |
| 59 |
updateStyles({ |
| 60 |
textColor: textAttributeDefaults.styles.textColor, |
| 61 |
}) |
| 62 |
} |
| 63 |
/> |
| 64 |
<ColorControl |
| 65 |
label={ |
| 66 |
linkColorIsBound |
| 67 |
? __("Link Color (dynamic)", "tableberg") |
| 68 |
: __("Link Color", "tableberg") |
| 69 |
} |
| 70 |
value={attributes.styles.linkColor} |
| 71 |
onChange={linkColor => updateStyles({ linkColor })} |
| 72 |
onDeselect={() => |
| 73 |
updateStyles({ |
| 74 |
linkColor: textAttributeDefaults.styles.linkColor, |
| 75 |
}) |
| 76 |
} |
| 77 |
/> |
| 78 |
<ColorControl |
| 79 |
label={ |
| 80 |
backgroundColorIsBound |
| 81 |
? __("Background Color (dynamic)", "tableberg") |
| 82 |
: __("Background Color", "tableberg") |
| 83 |
} |
| 84 |
value={attributes.styles.backgroundColor} |
| 85 |
onChange={backgroundColor => |
| 86 |
updateStyles({ backgroundColor }) |
| 87 |
} |
| 88 |
onDeselect={() => |
| 89 |
updateStyles({ |
| 90 |
backgroundColor: |
| 91 |
textAttributeDefaults.styles.backgroundColor, |
| 92 |
}) |
| 93 |
} |
| 94 |
/> |
| 95 |
</InspectorControls> |
| 96 |
<InspectorControls> |
| 97 |
<PanelBody title={__("Typography", "tableberg")}> |
| 98 |
<BaseControl |
| 99 |
__nextHasNoMarginBottom |
| 100 |
help={ |
| 101 |
fontSizeIsBound |
| 102 |
? __("Overridden by dynamic data", "tableberg") |
| 103 |
: "" |
| 104 |
} |
| 105 |
> |
| 106 |
<FontSizePicker |
| 107 |
value={attributes.styles.fontSize || undefined} |
| 108 |
onChange={fontSize => |
| 109 |
updateStyles({ |
| 110 |
fontSize: fontSize |
| 111 |
? String(fontSize) |
| 112 |
: textAttributeDefaults.styles.fontSize, |
| 113 |
}) |
| 114 |
} |
| 115 |
/> |
| 116 |
</BaseControl> |
| 117 |
</PanelBody> |
| 118 |
</InspectorControls> |
| 119 |
<InspectorControls> |
| 120 |
<ToolsPanel |
| 121 |
label={__("Dimensions", "tableberg")} |
| 122 |
panelId={clientId} |
| 123 |
resetAll={() => |
| 124 |
updateStyles({ |
| 125 |
padding: { |
| 126 |
...textAttributeDefaults.styles.padding, |
| 127 |
}, |
| 128 |
margin: { |
| 129 |
...textAttributeDefaults.styles.margin, |
| 130 |
}, |
| 131 |
}) |
| 132 |
} |
| 133 |
> |
| 134 |
<SpacingControl |
| 135 |
label={__("Padding", "tableberg")} |
| 136 |
value={attributes.styles.padding} |
| 137 |
hasValue={() => hasSides(attributes.styles.padding)} |
| 138 |
onChange={padding => updateStyles({ padding })} |
| 139 |
onDeselect={() => |
| 140 |
updateStyles({ |
| 141 |
padding: { |
| 142 |
...textAttributeDefaults.styles.padding, |
| 143 |
}, |
| 144 |
}) |
| 145 |
} |
| 146 |
/> |
| 147 |
<SpacingControl |
| 148 |
label={__("Margin", "tableberg")} |
| 149 |
value={attributes.styles.margin} |
| 150 |
hasValue={() => hasSides(attributes.styles.margin)} |
| 151 |
onChange={margin => updateStyles({ margin })} |
| 152 |
onDeselect={() => |
| 153 |
updateStyles({ |
| 154 |
margin: { |
| 155 |
...textAttributeDefaults.styles.margin, |
| 156 |
}, |
| 157 |
}) |
| 158 |
} |
| 159 |
/> |
| 160 |
</ToolsPanel> |
| 161 |
</InspectorControls> |
| 162 |
</> |
| 163 |
); |
| 164 |
} |
| 165 |
|