| 1 |
import { CSSProperties } from "react"; |
| 2 |
import { RichText, useBlockProps } from "@wordpress/block-editor"; |
| 3 |
import { BlockEditProps, registerBlockType } from "@wordpress/blocks"; |
| 4 |
import { __ } from "@wordpress/i18n"; |
| 5 |
import { mergeAttrsWithDefaultsAndApplyBindings } from "@tableberg/shared/utils/merge-attrs-with-defaults-and-apply-bindings"; |
| 6 |
import buttonIcon from "@tableberg/shared/icons/button"; |
| 7 |
|
| 8 |
import { ButtonElementAttributes, buttonAttrDefaults } from "./element"; |
| 9 |
import metadata from "./block.json"; |
| 10 |
import { ButtonElementControls } from "./controls"; |
| 11 |
import { ElementBindings } from "../../dynamic-data/types"; |
| 12 |
import { |
| 13 |
ElementAlignmentToolbar, |
| 14 |
NO_COORDS, |
| 15 |
alignmentWrapperStyle, |
| 16 |
} from "../element-edit-common"; |
| 17 |
|
| 18 |
function ButtonEdit({ |
| 19 |
attributes, |
| 20 |
setAttributes, |
| 21 |
isSelected, |
| 22 |
}: BlockEditProps<ButtonElementAttributes>) { |
| 23 |
const merged = mergeAttrsWithDefaultsAndApplyBindings( |
| 24 |
attributes, |
| 25 |
buttonAttrDefaults, |
| 26 |
undefined, |
| 27 |
{}, |
| 28 |
__("(No data)", "tableberg") |
| 29 |
); |
| 30 |
const { content, align, styles } = merged; |
| 31 |
|
| 32 |
const blockProps = useBlockProps({ |
| 33 |
style: alignmentWrapperStyle(align), |
| 34 |
}); |
| 35 |
|
| 36 |
const buttonStyle: CSSProperties = { |
| 37 |
backgroundColor: styles.backgroundColor || undefined, |
| 38 |
color: styles.textColor || undefined, |
| 39 |
fontSize: (styles as { fontSize?: string }).fontSize || undefined, |
| 40 |
textAlign: styles.textAlign as CSSProperties["textAlign"], |
| 41 |
width: styles.width === "auto" ? undefined : styles.width, |
| 42 |
paddingTop: styles.padding?.top, |
| 43 |
paddingRight: styles.padding?.right, |
| 44 |
paddingBottom: styles.padding?.bottom, |
| 45 |
paddingLeft: styles.padding?.left, |
| 46 |
borderTopLeftRadius: styles.borderRadius?.topLeft, |
| 47 |
borderTopRightRadius: styles.borderRadius?.topRight, |
| 48 |
borderBottomRightRadius: styles.borderRadius?.bottomRight, |
| 49 |
borderBottomLeftRadius: styles.borderRadius?.bottomLeft, |
| 50 |
border: "none", |
| 51 |
cursor: "text", |
| 52 |
}; |
| 53 |
|
| 54 |
return ( |
| 55 |
<div {...blockProps}> |
| 56 |
{isSelected && ( |
| 57 |
<ElementAlignmentToolbar |
| 58 |
align={align} |
| 59 |
onChange={newAlign => setAttributes({ align: newAlign })} |
| 60 |
/> |
| 61 |
)} |
| 62 |
{isSelected && ( |
| 63 |
<ButtonElementControls |
| 64 |
attributes={merged} |
| 65 |
bindings={ |
| 66 |
(attributes as { bindings?: ElementBindings }).bindings |
| 67 |
} |
| 68 |
cellCoords={NO_COORDS} |
| 69 |
elementIndex={0} |
| 70 |
updateStyles={newStyles => |
| 71 |
setAttributes({ |
| 72 |
styles: { |
| 73 |
...(attributes.styles ?? merged.styles), |
| 74 |
...newStyles, |
| 75 |
}, |
| 76 |
}) |
| 77 |
} |
| 78 |
updateAttrs={attrs => setAttributes(attrs)} |
| 79 |
/> |
| 80 |
)} |
| 81 |
<RichText |
| 82 |
tagName="div" |
| 83 |
className="tableberg-button-element" |
| 84 |
placeholder={__("Button text", "tableberg")} |
| 85 |
value={content} |
| 86 |
onChange={newContent => |
| 87 |
setAttributes({ content: newContent }) |
| 88 |
} |
| 89 |
style={buttonStyle} |
| 90 |
/> |
| 91 |
</div> |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
export function registerButtonBlock() { |
| 96 |
registerBlockType(metadata.name, { |
| 97 |
...(metadata as any), |
| 98 |
icon: buttonIcon, |
| 99 |
edit: ButtonEdit, |
| 100 |
save: () => null, |
| 101 |
}); |
| 102 |
} |
| 103 |
|