import React, { useRef, useCallback, PropsWithChildren } from "react" import { Tooltip, Icon, cbModal } from "@contentstack/venus-components" import { Resizable } from "re-resizable" import EmbedBtn from "./EmbedBtn" import ImageEditModal from "./ImageEditModal" import type { IRteParam, IRteElementType, } from "@contentstack/app-sdk/dist/src/RTE/types" import type { InsertResponse } from "~/types/imagevault" type ImageElementProps = PropsWithChildren & { element: IRteElementType & { attrs: InsertResponse } rte: IRteParam } export function ImageElement({ children, element, rte }: ImageElementProps) { const assetUrl = element.attrs.MediaConversions[0].Url const isSelected = rte?.selection?.isSelected() const isFocused = rte?.selection?.isFocused() const isHighlight = isFocused && isSelected const imgRef = useRef(null) const handleEdit = useCallback(() => { cbModal({ // @ts-expect-error: Component is badly typed component: (compProps) => ImageEditModal({ element, rte, path: rte.getPath(element), ...compProps, }), modalProps: { size: "max", style: { content: { maxHeight: "90dvh", width: "auto", }, overlay: {}, }, }, }) }, [element, rte]) const ToolTipButtons = () => { return (
rte?.removeNode(element)} >
) } const onResizeStop = () => { const { attrs: elementAttrs } = element const { offsetWidth: width, offsetHeight: height } = imgRef?.current ?? {} const newAttrs: { [key: string]: unknown } = { ...elementAttrs, style: { ...(elementAttrs?.style ?? {}), "max-width": `${width}px`, }, ...(width && height ? { width: `${width.toString()}px`, height: `${height.toString()}px` } : {}), } rte?._adv?.Transforms?.setNodes( rte._adv.editor, { attrs: newAttrs }, { at: rte.getPath(element) } ) } let alignmentStyles = {} const marginAlignment: Record = { center: { margin: "auto" }, left: { marginRight: "auto" }, right: { marginLeft: "auto" }, } if (typeof element.attrs.position === "string") { alignmentStyles = marginAlignment[element.attrs.position] } return (
} >
{ event.currentTarget.src = "https://placehold.co/600x400" }} style={{ width: "100%", height: "auto", aspectRatio: element.attrs.MediaConversions[0].AspectRatio, borderRadius: "8px", }} alt={element.attrs.altText} title={element.attrs?.Name} />
{children}
) }