evo-ai/frontend/app/agents/workflows/HelperLines.tsx

99 lines
4.6 KiB
TypeScript

/*
┌──────────────────────────────────────────────────────────────────────────────┐
│ @author: Davidson Gomes │
│ @file: /app/agents/workflows/HelperLines.tsx │
│ Developed by: Davidson Gomes │
│ Creation date: May 13, 2025 │
│ Contact: contato@evolution-api.com │
├──────────────────────────────────────────────────────────────────────────────┤
│ @copyright © Evolution API 2025. All rights reserved. │
│ Licensed under the Apache License, Version 2.0 │
│ │
│ You may not use this file except in compliance with the License. │
│ You may obtain a copy of the License at │
│ │
│ http://www.apache.org/licenses/LICENSE-2.0 │
│ │
│ Unless required by applicable law or agreed to in writing, software │
│ distributed under the License is distributed on an "AS IS" BASIS, │
│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │
│ See the License for the specific language governing permissions and │
│ limitations under the License. │
├──────────────────────────────────────────────────────────────────────────────┤
│ @important │
│ For any future changes to the code in this file, it is recommended to │
│ include, together with the modification, the information of the developer │
│ who changed it and the date of modification. │
└──────────────────────────────────────────────────────────────────────────────┘
*/
import { ReactFlowState, useStore } from "@xyflow/react";
import { CSSProperties, useEffect, useRef } from "react";
const canvasStyle: CSSProperties = {
width: "100%",
height: "100%",
position: "absolute",
zIndex: 10,
pointerEvents: "none",
};
const storeSelector = (state: ReactFlowState) => ({
width: state.width,
height: state.height,
transform: state.transform,
});
export type HelperLinesProps = {
horizontal?: number;
vertical?: number;
};
// a simple component to display the helper lines
// it puts a canvas on top of the React Flow pane and draws the lines using the canvas API
function HelperLinesRenderer({ horizontal, vertical }: HelperLinesProps) {
const { width, height, transform } = useStore(storeSelector);
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const canvas = canvasRef.current;
const ctx = canvas?.getContext("2d");
if (!ctx || !canvas) {
return;
}
const dpi = window.devicePixelRatio;
canvas.width = width * dpi;
canvas.height = height * dpi;
ctx.scale(dpi, dpi);
ctx.clearRect(0, 0, width, height);
ctx.strokeStyle = "#1d5ade";
ctx.lineWidth = 1;
ctx.setLineDash([5, 5]);
if (typeof vertical === "number") {
ctx.moveTo(vertical * transform[2] + transform[0], 0);
ctx.lineTo(vertical * transform[2] + transform[0], height);
ctx.stroke();
}
if (typeof horizontal === "number") {
ctx.moveTo(0, horizontal * transform[2] + transform[1]);
ctx.lineTo(width, horizontal * transform[2] + transform[1]);
ctx.stroke();
}
}, [width, height, transform, horizontal, vertical]);
return (
<canvas
ref={canvasRef}
className="react-flow__canvas"
style={canvasStyle}
/>
);
}
export default HelperLinesRenderer;