| 1 |
import { useState } from "react"; |
| 2 |
import { |
| 3 |
InspectorControls, |
| 4 |
BlockControls, |
| 5 |
HeightControl, |
| 6 |
__experimentalLinkControl as LinkControl, |
| 7 |
// @ts-ignore |
| 8 |
JustifyContentControl, |
| 9 |
} from "@wordpress/block-editor"; |
| 10 |
import { |
| 11 |
PanelBody, |
| 12 |
Popover, |
| 13 |
RangeControl, |
| 14 |
SelectControl, |
| 15 |
TabPanel, |
| 16 |
TextControl, |
| 17 |
ToolbarGroup, |
| 18 |
ToolbarButton, |
| 19 |
} from "@wordpress/components"; |
| 20 |
import { __ } from "@wordpress/i18n"; |
| 21 |
import { link } from "@wordpress/icons"; |
| 22 |
import { |
| 23 |
BorderControl, |
| 24 |
BorderRadiusControl, |
| 25 |
ColorControl, |
| 26 |
SpacingControl, |
| 27 |
} from "@tableberg/components"; |
| 28 |
import { IconPickerMini } from "@tableberg/components/icon-library"; |
| 29 |
import { CellKey } from "../../attributes"; |
| 30 |
import { useTableStore } from "../../store"; |
| 31 |
import { IconElementAttributes, iconAttrDefaults } from "."; |
| 32 |
import { ElementBindings } from "../../dynamic-data/types"; |
| 33 |
import { DynamicDataPanel } from "../../components/DynamicDataPanel"; |
| 34 |
import { ElementDeleteButton } from "../../components/ElementDeleteButton"; |
| 35 |
import { ElementOptionsBlockControls } from "../../components/ElementOptionsButton"; |
| 36 |
|
| 37 |
export function IconElementControls({ |
| 38 |
attributes, |
| 39 |
bindings, |
| 40 |
cellCoords, |
| 41 |
elementIndex, |
| 42 |
}: { |
| 43 |
attributes: IconElementAttributes; |
| 44 |
bindings?: ElementBindings; |
| 45 |
cellCoords: CellKey; |
| 46 |
elementIndex: number; |
| 47 |
}) { |
| 48 |
const { icon, size, behavior, link: iconLink, styles } = attributes; |
| 49 |
|
| 50 |
const updateAttrs = useTableStore( |
| 51 |
state => state.updateSelectedElementAttrs |
| 52 |
); |
| 53 |
|
| 54 |
const [isEditingURL, setIsEditingURL] = useState(false); |
| 55 |
const [iconMode, setIconMode] = useState<"url" | "icon">( |
| 56 |
icon?.url ? "url" : "icon" |
| 57 |
); |
| 58 |
|
| 59 |
const updateStyles = (newStyles: Partial<typeof styles>) => { |
| 60 |
updateAttrs({ styles: { ...styles, ...newStyles } }); |
| 61 |
}; |
| 62 |
|
| 63 |
const colorTabs: any[] = [ |
| 64 |
{ |
| 65 |
name: "normal", |
| 66 |
title: __("Normal", "tableberg"), |
| 67 |
component: ( |
| 68 |
<> |
| 69 |
<ColorControl |
| 70 |
label={__("Icon Color", "tableberg")} |
| 71 |
value={styles.color} |
| 72 |
onChange={(newColor: string) => |
| 73 |
updateStyles({ color: newColor }) |
| 74 |
} |
| 75 |
onDeselect={() => |
| 76 |
updateStyles({ |
| 77 |
color: iconAttrDefaults.styles.color, |
| 78 |
}) |
| 79 |
} |
| 80 |
/> |
| 81 |
<ColorControl |
| 82 |
label={__("Background", "tableberg")} |
| 83 |
value={styles.background} |
| 84 |
onChange={(newBg: string) => |
| 85 |
updateStyles({ background: newBg }) |
| 86 |
} |
| 87 |
onDeselect={() => |
| 88 |
updateStyles({ |
| 89 |
background: iconAttrDefaults.styles.background, |
| 90 |
}) |
| 91 |
} |
| 92 |
/> |
| 93 |
</> |
| 94 |
), |
| 95 |
}, |
| 96 |
{ |
| 97 |
name: "hover", |
| 98 |
title: __("Hover", "tableberg"), |
| 99 |
component: ( |
| 100 |
<> |
| 101 |
<ColorControl |
| 102 |
label={__("Icon Color", "tableberg")} |
| 103 |
value={styles.colorHover} |
| 104 |
onChange={(newColor: string) => |
| 105 |
updateStyles({ colorHover: newColor }) |
| 106 |
} |
| 107 |
onDeselect={() => |
| 108 |
updateStyles({ |
| 109 |
colorHover: iconAttrDefaults.styles.colorHover, |
| 110 |
}) |
| 111 |
} |
| 112 |
/> |
| 113 |
<ColorControl |
| 114 |
label={__("Background", "tableberg")} |
| 115 |
value={styles.backgroundHover} |
| 116 |
onChange={(newBg: string) => |
| 117 |
updateStyles({ backgroundHover: newBg }) |
| 118 |
} |
| 119 |
onDeselect={() => |
| 120 |
updateStyles({ |
| 121 |
backgroundHover: |
| 122 |
iconAttrDefaults.styles.backgroundHover, |
| 123 |
}) |
| 124 |
} |
| 125 |
/> |
| 126 |
</> |
| 127 |
), |
| 128 |
}, |
| 129 |
]; |
| 130 |
|
| 131 |
return ( |
| 132 |
<> |
| 133 |
<BlockControls> |
| 134 |
<ToolbarGroup> |
| 135 |
<JustifyContentControl |
| 136 |
value={styles.align} |
| 137 |
allowedControls={["left", "center", "right"]} |
| 138 |
onChange={(newJustify: "left" | "center" | "right") => { |
| 139 |
updateStyles({ align: newJustify }); |
| 140 |
}} |
| 141 |
/> |
| 142 |
</ToolbarGroup> |
| 143 |
<ToolbarGroup> |
| 144 |
<ToolbarButton |
| 145 |
icon={link} |
| 146 |
title={__("Link", "tableberg")} |
| 147 |
onClick={() => setIsEditingURL(true)} |
| 148 |
isActive={!!iconLink.url} |
| 149 |
/> |
| 150 |
</ToolbarGroup> |
| 151 |
<ElementDeleteButton |
| 152 |
cellCoords={cellCoords} |
| 153 |
elementIndex={elementIndex} |
| 154 |
/> |
| 155 |
</BlockControls> |
| 156 |
<ElementOptionsBlockControls |
| 157 |
cellCoords={cellCoords} |
| 158 |
elementIndex={elementIndex} |
| 159 |
/> |
| 160 |
{isEditingURL && ( |
| 161 |
<Popover |
| 162 |
placement="bottom" |
| 163 |
onClose={() => setIsEditingURL(false)} |
| 164 |
focusOnMount={true} |
| 165 |
> |
| 166 |
<LinkControl |
| 167 |
value={{ |
| 168 |
url: iconLink.url, |
| 169 |
opensInNewTab: iconLink.target === "_blank", |
| 170 |
}} |
| 171 |
onChange={({ |
| 172 |
url = "", |
| 173 |
opensInNewTab, |
| 174 |
}: { |
| 175 |
url?: string; |
| 176 |
opensInNewTab?: boolean; |
| 177 |
}) => { |
| 178 |
updateAttrs({ |
| 179 |
link: { |
| 180 |
url, |
| 181 |
target: opensInNewTab ? "_blank" : "_self", |
| 182 |
}, |
| 183 |
}); |
| 184 |
}} |
| 185 |
onRemove={() => { |
| 186 |
updateAttrs({ |
| 187 |
link: iconAttrDefaults.link, |
| 188 |
}); |
| 189 |
setIsEditingURL(false); |
| 190 |
}} |
| 191 |
/> |
| 192 |
</Popover> |
| 193 |
)} |
| 194 |
<InspectorControls> |
| 195 |
<PanelBody title={__("Icon Settings", "tableberg")} initialOpen> |
| 196 |
<SelectControl |
| 197 |
__nextHasNoMarginBottom |
| 198 |
label={__("Behave As", "tableberg")} |
| 199 |
value={behavior} |
| 200 |
options={[ |
| 201 |
{ |
| 202 |
value: "paragraph", |
| 203 |
label: __("Paragraph", "tableberg"), |
| 204 |
}, |
| 205 |
{ |
| 206 |
value: "char", |
| 207 |
label: __("Character", "tableberg"), |
| 208 |
}, |
| 209 |
]} |
| 210 |
onChange={(newBehavior: string) => |
| 211 |
updateAttrs({ |
| 212 |
behavior: newBehavior as "paragraph" | "char", |
| 213 |
}) |
| 214 |
} |
| 215 |
/> |
| 216 |
<HeightControl |
| 217 |
value={size} |
| 218 |
label={__("Icon Size", "tableberg")} |
| 219 |
onChange={(newSize: string) => |
| 220 |
updateAttrs({ size: newSize || "40px" }) |
| 221 |
} |
| 222 |
/> |
| 223 |
<RangeControl |
| 224 |
__nextHasNoMarginBottom |
| 225 |
max={180} |
| 226 |
min={-180} |
| 227 |
allowReset |
| 228 |
resetFallbackValue={0} |
| 229 |
value={styles.rotation} |
| 230 |
label={__("Rotation", "tableberg")} |
| 231 |
onChange={(newRotation: number | undefined) => |
| 232 |
updateStyles({ rotation: newRotation ?? 0 }) |
| 233 |
} |
| 234 |
/> |
| 235 |
</PanelBody> |
| 236 |
<PanelBody title={__("Icon", "tableberg")} initialOpen> |
| 237 |
<SelectControl |
| 238 |
__nextHasNoMarginBottom |
| 239 |
label={__("Icon Source", "tableberg")} |
| 240 |
options={[ |
| 241 |
{ |
| 242 |
value: "icon", |
| 243 |
label: __("Select from library", "tableberg"), |
| 244 |
}, |
| 245 |
{ |
| 246 |
value: "url", |
| 247 |
label: __("Import from URL", "tableberg"), |
| 248 |
}, |
| 249 |
]} |
| 250 |
value={iconMode} |
| 251 |
onChange={(mode: string) => { |
| 252 |
if (mode === "url" || mode === "icon") { |
| 253 |
setIconMode(mode); |
| 254 |
} |
| 255 |
}} |
| 256 |
/> |
| 257 |
{iconMode === "icon" ? ( |
| 258 |
<IconPickerMini |
| 259 |
onSelect={newIcon => updateAttrs({ icon: newIcon })} |
| 260 |
maxHeight="180px" |
| 261 |
/> |
| 262 |
) : ( |
| 263 |
<TextControl |
| 264 |
__nextHasNoMarginBottom |
| 265 |
label={__("Icon URL", "tableberg")} |
| 266 |
value={icon?.url || ""} |
| 267 |
onChange={(url: string) => |
| 268 |
updateAttrs({ |
| 269 |
icon: { |
| 270 |
iconName: "custom", |
| 271 |
url: encodeURI(url), |
| 272 |
}, |
| 273 |
}) |
| 274 |
} |
| 275 |
/> |
| 276 |
)} |
| 277 |
</PanelBody> |
| 278 |
</InspectorControls> |
| 279 |
<InspectorControls group="color"> |
| 280 |
<div style={{ marginTop: "0", gridColumn: "1/-1" }}> |
| 281 |
<TabPanel tabs={colorTabs}>{tab => tab.component}</TabPanel> |
| 282 |
</div> |
| 283 |
</InspectorControls> |
| 284 |
<InspectorControls group="border"> |
| 285 |
<BorderControl |
| 286 |
label={__("Border", "tableberg")} |
| 287 |
value={styles.border} |
| 288 |
onChange={(newBorder: any) => |
| 289 |
updateStyles({ border: newBorder }) |
| 290 |
} |
| 291 |
onDeselect={() => |
| 292 |
updateStyles({ border: iconAttrDefaults.styles.border }) |
| 293 |
} |
| 294 |
hasValue={() => |
| 295 |
!!( |
| 296 |
styles.border?.top || |
| 297 |
styles.border?.right || |
| 298 |
styles.border?.bottom || |
| 299 |
styles.border?.left |
| 300 |
) |
| 301 |
} |
| 302 |
/> |
| 303 |
<BorderRadiusControl |
| 304 |
label={__("Border Radius", "tableberg")} |
| 305 |
value={styles.borderRadius} |
| 306 |
onChange={(newRadius: any) => |
| 307 |
updateStyles({ borderRadius: newRadius }) |
| 308 |
} |
| 309 |
onDeselect={() => |
| 310 |
updateStyles({ |
| 311 |
borderRadius: iconAttrDefaults.styles.borderRadius, |
| 312 |
}) |
| 313 |
} |
| 314 |
hasValue={() => |
| 315 |
!!( |
| 316 |
styles.borderRadius?.topLeft || |
| 317 |
styles.borderRadius?.topRight || |
| 318 |
styles.borderRadius?.bottomLeft || |
| 319 |
styles.borderRadius?.bottomRight |
| 320 |
) |
| 321 |
} |
| 322 |
/> |
| 323 |
</InspectorControls> |
| 324 |
<InspectorControls group="dimensions"> |
| 325 |
<SpacingControl |
| 326 |
label={__("Padding", "tableberg")} |
| 327 |
value={styles.padding} |
| 328 |
onChange={(newPadding: any) => |
| 329 |
updateStyles({ padding: newPadding }) |
| 330 |
} |
| 331 |
onDeselect={() => |
| 332 |
updateStyles({ |
| 333 |
padding: iconAttrDefaults.styles.padding, |
| 334 |
}) |
| 335 |
} |
| 336 |
hasValue={() => |
| 337 |
!!( |
| 338 |
styles.padding?.top || |
| 339 |
styles.padding?.right || |
| 340 |
styles.padding?.bottom || |
| 341 |
styles.padding?.left |
| 342 |
) |
| 343 |
} |
| 344 |
/> |
| 345 |
</InspectorControls> |
| 346 |
<InspectorControls> |
| 347 |
<DynamicDataPanel elementType="icon" bindings={bindings} /> |
| 348 |
</InspectorControls> |
| 349 |
</> |
| 350 |
); |
| 351 |
} |
| 352 |
|