import { CSSProperties, useState } from "react"; import { MediaPlaceholder, useBlockProps } from "@wordpress/block-editor"; import { BlockEditProps, registerBlockType } from "@wordpress/blocks"; import { __ } from "@wordpress/i18n"; import { mergeAttrsWithDefaultsAndApplyBindings } from "@tableberg/shared/utils/merge-attrs-with-defaults-and-apply-bindings"; import imageIcon from "@tableberg/shared/icons/image"; import { ImageElementAttributes, imageAttrDefaults } from "./element"; import metadata from "./block.json"; import { ImageElementControls } from "./controls"; import { ElementBindings } from "../../dynamic-data/types"; import { ElementAlignmentToolbar, NO_COORDS, alignmentWrapperStyle, } from "../element-edit-common"; function ImageEdit({ attributes, setAttributes, isSelected, }: BlockEditProps) { const merged = mergeAttrsWithDefaultsAndApplyBindings( attributes, imageAttrDefaults, undefined, {}, __("(No data)", "tableberg") ); const { media, alt, align, width, height, aspectRatio, scale } = merged; const { border, borderRadius } = merged; const [showCaption, setShowCaption] = useState(!!merged.caption); const blockProps = useBlockProps({ style: alignmentWrapperStyle(align), }); const controls = isSelected && media?.url && ( setAttributes(attrs)} /> ); if (!media?.url) { return (
setAttributes({ media: { id: selected.id, url: selected.url }, alt: selected.alt ?? "", }) } />
); } return (
{isSelected && ( setAttributes({ align: newAlign })} /> )} {controls} {alt}
); } export function registerImageBlock() { registerBlockType(metadata.name, { ...(metadata as any), icon: imageIcon, edit: ImageEdit, save: () => null, }); }