/** * Draftably's layout and router for graphs from any tool: `@draftably/runtime/graph`. * * const placed = await layout({ nodes, edges }); // draftably's own shape * const { reactFlow } = await layout({ reactFlow: { nodes, edges } }); * const lines = route({ nodes: placedBoxes, edges }); // lines only * * A keyed call returns the same shape it took, under the same key. */ import { layout as layoutGraph, route as routeGraph, type GraphIn, type LayoutResult, type RouteIn, type Routed, type RoutingOptions, } from './core'; import { fromFlow, placedFlow, routedFlow, toFlow, type FlowEdge, type FlowEdgeOut, type FlowGraph, type FlowNode, type FlowNodeOut, type FlowOptions, type SvelteFlowNodeOut, } from './react-flow'; import { layoutElk, type ElkNode } from './elk'; export { AUTO_FULL_LIMIT, layoutSync } from './core'; export { DraftablyELK } from './elk'; export { dagreLayout } from './dagre'; export { draftablyLayout } from './cytoscape'; export type { ElkEdge, ElkEdgeSection, ElkLabel, ElkNode, ElkPort } from './elk'; export type { DagreGraph } from './dagre'; export type { CytoscapeLayoutOptions } from './cytoscape'; export type * from './core'; export type { DraftablyEdgeData, FlowEdge, FlowEdgeOut, FlowGraph, FlowNode, FlowNodeOut, FlowOptions, SvelteFlowNodeOut, } from './react-flow'; /** Settings a keyed call takes beside its graph. */ export type Common = Pick; type FlowLayout = FlowGraph, FlowEdgeOut>; type SvelteLayout = FlowGraph< SvelteFlowNodeOut, FlowEdgeOut >; type FlowRouted = FlowGraph>; export function layout(graph: GraphIn): Promise; export function layout( input: { reactFlow: FlowGraph } & FlowOptions & Common, ): Promise<{ reactFlow: FlowLayout; result: LayoutResult }>; export function layout( input: { svelteFlow: FlowGraph } & FlowOptions & Common, ): Promise<{ svelteFlow: SvelteLayout; result: LayoutResult }>; export function layout( input: { elk: ElkNode } & Common, ): Promise<{ elk: ElkNode; result: LayoutResult }>; export async function layout(input: object): Promise { if ('elk' in input) { const { elk, ...common } = input as { elk: ElkNode } & Common; return layoutElk(elk, undefined, common); } for (const key of ['reactFlow', 'svelteFlow'] as const) if (key in input) { const { [key]: flow, shape, ...common } = input as Record & FlowOptions & Common; const result = await layoutGraph({ ...fromFlow(flow, { shape }), ...common }); return { [key]: toFlow(flow, result, key === 'svelteFlow'), result }; } return layoutGraph(input as GraphIn); } export function route(graph: RouteIn): Routed; export function route( input: { reactFlow: FlowGraph; routing?: RoutingOptions } & FlowOptions, ): { reactFlow: FlowRouted; result: Routed }; export function route( input: { svelteFlow: FlowGraph; routing?: RoutingOptions } & FlowOptions, ): { svelteFlow: FlowRouted; result: Routed }; export function route(input: object): unknown { for (const key of ['reactFlow', 'svelteFlow'] as const) if (key in input) { const { [key]: flow, shape, routing, } = input as Record & FlowOptions & { routing?: RoutingOptions }; const result = routeGraph({ ...placedFlow(flow, { shape }), routing }); return { [key]: { nodes: flow.nodes, edges: routedFlow(flow.edges, result) }, result }; } return routeGraph(input as RouteIn); }