| 1 |
import { CSSProperties, useState } from "react"; |
| 2 |
import { MediaPlaceholder, 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 imageIcon from "@tableberg/shared/icons/image"; |
| 7 |
|
| 8 |
import { ImageElementAttributes, imageAttrDefaults } from "./element"; |
| 9 |
import metadata from "./block.json"; |
| 10 |
import { ImageElementControls } 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 ImageEdit({ |
| 19 |
attributes, |
| 20 |
setAttributes, |
| 21 |
isSelected, |
| 22 |
}: BlockEditProps<ImageElementAttributes>) { |
| 23 |
const merged = mergeAttrsWithDefaultsAndApplyBindings( |
| 24 |
attributes, |
| 25 |
imageAttrDefaults, |
| 26 |
undefined, |
| 27 |
{}, |
| 28 |
__("(No data)", "tableberg") |
| 29 |
); |
| 30 |
const { media, alt, align, width, height, aspectRatio, scale } = merged; |
| 31 |
const { border, borderRadius } = merged; |
| 32 |
const [showCaption, setShowCaption] = useState(!!merged.caption); |
| 33 |
|
| 34 |
const blockProps = useBlockProps({ |
| 35 |
style: alignmentWrapperStyle(align), |
| 36 |
}); |
| 37 |
|
| 38 |
const controls = isSelected && media?.url && ( |
| 39 |
<ImageElementControls |
| 40 |
attributes={merged} |
| 41 |
bindings={(attributes as { bindings?: ElementBindings }).bindings} |
| 42 |
showCaption={showCaption} |
| 43 |
setShowCaption={setShowCaption} |
| 44 |
cellCoords={NO_COORDS} |
| 45 |
elementIndex={0} |
| 46 |
updateAttrs={attrs => setAttributes(attrs)} |
| 47 |
/> |
| 48 |
); |
| 49 |
|
| 50 |
if (!media?.url) { |
| 51 |
return ( |
| 52 |
<div {...blockProps}> |
| 53 |
<MediaPlaceholder |
| 54 |
icon="format-image" |
| 55 |
labels={{ title: __("Image", "tableberg") }} |
| 56 |
accept="image/*" |
| 57 |
allowedTypes={["image"]} |
| 58 |
onSelect={selected => |
| 59 |
setAttributes({ |
| 60 |
media: { id: selected.id, url: selected.url }, |
| 61 |
alt: selected.alt ?? "", |
| 62 |
}) |
| 63 |
} |
| 64 |
/> |
| 65 |
</div> |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
return ( |
| 70 |
<div {...blockProps}> |
| 71 |
{isSelected && ( |
| 72 |
<ElementAlignmentToolbar |
| 73 |
align={align} |
| 74 |
onChange={newAlign => setAttributes({ align: newAlign })} |
| 75 |
/> |
| 76 |
)} |
| 77 |
{controls} |
| 78 |
<img |
| 79 |
src={media.url} |
| 80 |
alt={alt} |
| 81 |
style={{ |
| 82 |
// An empty width falls back to the default like the PHP |
| 83 |
// renderer does (the merge helper keeps ""), and never |
| 84 |
// lets a large image push its column wider than the cell. |
| 85 |
width: width || imageAttrDefaults.width, |
| 86 |
maxWidth: "100%", |
| 87 |
height: height || undefined, |
| 88 |
aspectRatio: aspectRatio || undefined, |
| 89 |
objectFit: scale as CSSProperties["objectFit"], |
| 90 |
borderTop: border?.top || undefined, |
| 91 |
borderRight: border?.right || undefined, |
| 92 |
borderBottom: border?.bottom || undefined, |
| 93 |
borderLeft: border?.left || undefined, |
| 94 |
borderTopLeftRadius: borderRadius?.topLeft || undefined, |
| 95 |
borderTopRightRadius: borderRadius?.topRight || undefined, |
| 96 |
borderBottomRightRadius: |
| 97 |
borderRadius?.bottomRight || undefined, |
| 98 |
borderBottomLeftRadius: |
| 99 |
borderRadius?.bottomLeft || undefined, |
| 100 |
}} |
| 101 |
/> |
| 102 |
</div> |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
export function registerImageBlock() { |
| 107 |
registerBlockType(metadata.name, { |
| 108 |
...(metadata as any), |
| 109 |
icon: imageIcon, |
| 110 |
edit: ImageEdit, |
| 111 |
save: () => null, |
| 112 |
}); |
| 113 |
} |
| 114 |
|