import React, { memo } from "react"; import { intentClassName, IntentTypes } from "../../../common/Intent"; import { Depiction, DepictionProps } from "../../../components/Depiction/Depiction"; import { ValidIconName } from "../../../components/Icon/canonicalIconNames"; import Icon from "../../../components/Icon/Icon"; import OverflowText from "../../../components/Typography/OverflowText"; import { CLASSPREFIX as eccgui } from "../../../configuration/constants"; export interface EdgeLabelProps extends React.HTMLAttributes { /** * Depiction element displayed left from the label. */ depiction?: ValidIconName | React.ReactElement; /** * Label of the edge. * Cannot overflow the parent container. */ text: string | React.JSX.Element; /** * One or multiple other elements displayed right from label. */ actions?: React.JSX.Element | React.JSX.Element[]; /** * The element is increased in its size. */ large?: boolean; /** * Stretches the label component to the full available width. */ fullWidth?: boolean; /** * Label is displayed without a box that comes with borders and background color. */ loose?: boolean; /** * Add a info state to the label, visualized by color. */ intent?: IntentTypes; } export const EdgeLabel = memo( ({ depiction, text, actions, large, fullWidth, loose, intent, title, className, ...otherDivProps }: EdgeLabelProps) => { const depEl = !!depiction && typeof depiction === "string" ? ( } /> ) : ( depiction ); return (
{!!depEl && (
{React.cloneElement(depEl, { padding: "tiny", ratio: "1:1", resizing: "contain", forceInlineSvg: true, border: false, backgroundColor: undefined, })}
)} {(title || text) && (
{text && (typeof text === "string" ? {text} : text)}
)} {!!actions &&
{actions}
}
); }, ); export interface EdgeLabelObjectProps extends React.SVGAttributes { /** * The `` element that need to be displayed. */ children: React.ReactElement; /** * Property from the `renderLabel` callback method. */ edgeCenter: [number, number, number, number]; /** * Number of milliseconds. * If set and larger then zero then the re-sizing process is repeated after this time. */ resizeTimeout?: number; } export const EdgeLabelObject = memo( ({ children, edgeCenter, resizeTimeout = -1, ...otherForeignObjectProps }: EdgeLabelObjectProps) => { const containerCallback = React.useCallback( (containerRef: SVGForeignObjectElement | null) => { if (containerRef) labelSize(containerRef); }, [edgeCenter], ); const labelSize = (container: SVGForeignObjectElement) => { const labelElement = container.getElementsByClassName(`${eccgui}-graphviz__edge-label`); if (labelElement.length > 0) { const width = (labelElement[0] as HTMLElement).offsetWidth; const height = (labelElement[0] as HTMLElement).offsetHeight; container.setAttribute("x", (edgeCenter[0] - width / 2).toString()); container.setAttribute("y", (edgeCenter[1] - height / 2).toString()); container.setAttribute("width", width.toString()); container.setAttribute("height", height.toString()); } else if (resizeTimeout > 0) { // Content is not ready yet, recall resizing process after timeout. // This can happen in case the children is actually not a `EdgeLabel`. setTimeout(() => { labelSize(container); }, resizeTimeout); } }; return ( {children} ); }, );