import React, { memo } from "react"; import { Handle as HandleV9, HandleProps as ReactFlowHandleV9Props } from "react-flow-renderer"; import { Classes as BlueprintClasses } from "@blueprintjs/core"; import { Handle as HandleV12, HandleProps as ReactFlowHandleV12Props } from "@xyflow/react"; import { IntentBlueprint, intentClassName, IntentTypes } from "../../../common/Intent"; import { TooltipProps } from "../../../components"; import { CLASSPREFIX as eccgui } from "../../../configuration/constants"; import { ReacFlowVersionSupportProps, useReactFlowVersion } from "../versionsupport"; import { HandleContent, HandleContentProps } from "./HandleContent"; export type HandleCategory = "configuration" | "flexible" | "fixed" | "unknown" | "dependency"; interface HandleExtensionProps extends ReacFlowVersionSupportProps, Omit, "id" | "children"> { /** * Defines the handle category, mainly used to adjust layout. */ category?: HandleCategory; /** * Extended handle data. */ data?: HandleContentProps; /** * Simple text tooltip displayed as title on hover. */ tooltip?: string; /** * Feedback state of the handle. */ intent?: IntentTypes; children?: HandleContentProps["children"]; } /** * @deprecated (v27) use only `HandleDefaultProps` */ export interface HandleV9Props extends HandleExtensionProps, ReactFlowHandleV9Props {} /** * @deprecated (v27) use only `HandleDefaultProps` */ export interface HandleV12Props extends HandleExtensionProps, ReactFlowHandleV12Props {} /** * Combined interface, later this will be only a copy of `HandleV12Props`. */ export type HandleDefaultProps = HandleV9Props | HandleV12Props; export const HandleDefault = memo( ({ flowVersion, data, tooltip, children, category, intent, ...handleProps }: HandleDefaultProps) => { const evaluateFlowVersion = useReactFlowVersion(); const flowVersionCheck = flowVersion || evaluateFlowVersion; const handleDefaultRef = React.useRef(null); const [extendedTooltipDisplayed, setExtendedTooltipDisplayed] = React.useState(false); let toolsTarget: HTMLCollectionOf; React.useEffect(() => { if (handleDefaultRef.current) { toolsTarget = handleDefaultRef.current.getElementsByClassName(`${eccgui}-graphviz__handletools-target`); if (toolsTarget && toolsTarget[0]) { // Polyfill for FF that does not support the `:has()` pseudo selector until at least version 119 or 120 // need to be re-evaluated then // @see https://connect.mozilla.org/t5/ideas/when-is-has-css-selector-going-to-be-fully-implemented-in/idi-p/23794 handleDefaultRef.current.classList.add(`ffpolyfill-has-${eccgui}-graphviz__handletools-target`); } } }); const tooltipTitle = tooltip ? { title: tooltip } : {}; const handleContentTooltipProps: Partial = { placement: handleProps.position === "left" || handleProps.position === "right" ? `${handleProps.position}-end` : undefined, modifiers: { offset: { enabled: true, options: { offset: [0, 20], }, }, }, intent: intent as IntentBlueprint, className: `${eccgui}-graphviz__handle__tooltip-target`, isOpen: extendedTooltipDisplayed, }; const handleContentProps = { ...data, tooltipProps: { ...handleContentTooltipProps, ...data?.tooltipProps, } as TooltipProps, }; const handleContent = {children}; let switchTooltipTimerOn: ReturnType; let switchToolsTimerOff: ReturnType; const handleConfig = React.useMemo( () => ({ ...handleProps, ...tooltipTitle, className: `${eccgui}-graphviz__handle ${eccgui}-graphviz__handle--${flowVersionCheck}` + (intent ? ` ${intentClassName(intent)}` : ""), onClick: (e: React.MouseEvent) => { if (handleProps.onClick) { handleProps.onClick(e); } if (toolsTarget && toolsTarget.length > 0 && e.currentTarget === handleDefaultRef.current) { setExtendedTooltipDisplayed(false); (toolsTarget[0] as HTMLElement).click(); } }, "data-category": category, onMouseEnter: (e: React.MouseEvent) => { if (switchToolsTimerOff) clearTimeout(switchToolsTimerOff); if (e.currentTarget === handleDefaultRef.current) { switchTooltipTimerOn = setTimeout( () => setExtendedTooltipDisplayed(true), data?.tooltipProps?.hoverOpenDelay ?? 500, ); } }, onMouseLeave: () => { if (switchTooltipTimerOn) clearTimeout(switchTooltipTimerOn); if ( toolsTarget && toolsTarget.length > 0 && toolsTarget[0].classList.contains(BlueprintClasses.POPOVER_OPEN) ) { switchToolsTimerOff = setTimeout(() => (toolsTarget[0] as HTMLElement).click(), 500); } setExtendedTooltipDisplayed(false); }, }), [ intent, category, tooltip, flowVersionCheck, handleProps.isConnectable, handleProps.style, handleProps.onClick, data?.tooltipProps?.hoverOpenDelay, ], ); switch (flowVersionCheck) { case "v9": return ( {handleContent} ); case "v12": return ( {handleContent} ); default: return <>; } }, );