/* eslint-disable @typescript-eslint/no-namespace */ import type { Rect, Viewport } from '@xyflow/system'; import type { Node, Edge, ViewportHelperFunctions, InternalNode } from '.'; export type ReactFlowJsonObject = { nodes: NodeType[]; edges: EdgeType[]; viewport: Viewport; }; export type DeleteElementsOptions = { nodes?: (Node | { id: Node['id'] })[]; edges?: (Edge | { id: Edge['id'] })[]; }; export namespace Instance { export type GetNodes = () => NodeType[]; export type SetNodes = ( payload: NodeType[] | ((nodes: NodeType[]) => NodeType[]) ) => void; export type AddNodes = (payload: NodeType[] | NodeType) => void; export type GetNode = (id: string) => NodeType | undefined; export type GetInternalNode = (id: string) => InternalNode | undefined; export type GetEdges = () => EdgeType[]; export type SetEdges = ( payload: EdgeType[] | ((edges: EdgeType[]) => EdgeType[]) ) => void; export type GetEdge = (id: string) => EdgeType | undefined; export type AddEdges = (payload: EdgeType[] | EdgeType) => void; export type ToObject = () => ReactFlowJsonObject< NodeType, EdgeType >; export type DeleteElements = (params: DeleteElementsOptions) => Promise<{ deletedNodes: Node[]; deletedEdges: Edge[]; }>; export type GetIntersectingNodes = ( node: NodeType | { id: Node['id'] } | Rect, partially?: boolean, nodes?: NodeType[] ) => NodeType[]; export type IsNodeIntersecting = ( node: NodeType | { id: Node['id'] } | Rect, area: Rect, partially?: boolean ) => boolean; export type UpdateNode = ( id: string, nodeUpdate: Partial | ((node: NodeType) => Partial), options?: { replace: boolean } ) => void; export type UpdateNodeData = ( id: string, dataUpdate: object | ((node: NodeType) => object), options?: { replace: boolean } ) => void; } export type ReactFlowInstance = { /** * Returns nodes. * * @returns nodes array */ getNodes: Instance.GetNodes; /** * Sets nodes. * * @param payload - the nodes to set or a function that receives the current nodes and returns the new nodes */ setNodes: Instance.SetNodes; /** * Adds nodes. * * @param payload - the nodes to add */ addNodes: Instance.AddNodes; /** * Returns a node by id. * * @param id - the node id * @returns the node or undefined if no node was found */ getNode: Instance.GetNode; /** * Returns an internal node by id. * * @param id - the node id * @returns the internal node or undefined if no node was found */ getInternalNode: Instance.GetInternalNode; /** * Returns edges. * * @returns edges array */ getEdges: Instance.GetEdges; /** * Sets edges. * * @param payload - the edges to set or a function that receives the current edges and returns the new edges */ setEdges: Instance.SetEdges; /** * Adds edges. * * @param payload - the edges to add */ addEdges: Instance.AddEdges; /** * Returns an edge by id. * * @param id - the edge id * @returns the edge or undefined if no edge was found */ getEdge: Instance.GetEdge; /** * Returns the nodes, edges and the viewport as a JSON object. * * @returns the nodes, edges and the viewport as a JSON object */ toObject: Instance.ToObject; /** * Deletes nodes and edges. * * @param params.nodes - optional nodes array to delete * @param params.edges - optional edges array to delete * * @returns a promise that resolves with the deleted nodes and edges */ deleteElements: Instance.DeleteElements; /** * Returns all nodes that intersect with the given node or rect. * * @param node - the node or rect to check for intersections * @param partially - if true, the node is considered to be intersecting if it partially overlaps with the passed node or rect * @param nodes - optional nodes array to check for intersections * * @returns an array of intersecting nodes */ getIntersectingNodes: Instance.GetIntersectingNodes; /** * Checks if the given node or rect intersects with the passed rect. * * @param node - the node or rect to check for intersections * @param area - the rect to check for intersections * @param partially - if true, the node is considered to be intersecting if it partially overlaps with the passed react * * @returns true if the node or rect intersects with the given area */ isNodeIntersecting: Instance.IsNodeIntersecting; /** * Updates a node. * * @param id - id of the node to update * @param nodeUpdate - the node update as an object or a function that receives the current node and returns the node update * @param options.replace - if true, the node is replaced with the node update, otherwise the changes get merged * * @example * updateNode('node-1', (node) => ({ position: { x: node.position.x + 10, y: node.position.y } })); */ updateNode: Instance.UpdateNode; /** * Updates the data attribute of a node. * * @param id - id of the node to update * @param dataUpdate - the data update as an object or a function that receives the current data and returns the data update * @param options.replace - if true, the data is replaced with the data update, otherwise the changes get merged * * @example * updateNodeData('node-1', { label: 'A new label' }); */ updateNodeData: Instance.UpdateNodeData; viewportInitialized: () => boolean; } & Omit;