Files
Portal-Chart-Builder/src/serialize.ts
T
2026-07-13 15:44:13 +03:00

37 lines
1.1 KiB
TypeScript

import type { Node, Edge } from '@xyflow/react';
import type { DiagramDocument, DiagramEdge, DiagramNode, NodeKind } from './types';
/**
* React Flow's runtime Node/Edge carry UI-only fields (measured, selected,
* dragging, zIndex, ...). Only this subset gets persisted.
*/
export function serializeDocument(nodes: Node[], edges: Edge[]): DiagramDocument {
return {
nodes: nodes.map(
(node): DiagramNode => ({
id: node.id,
type: (node.type ?? 'task') as NodeKind,
position: { x: node.position.x, y: node.position.y },
data: node.data as DiagramNode['data'],
}),
),
edges: edges.map(
(edge): DiagramEdge => ({
id: edge.id,
source: edge.source,
target: edge.target,
type: edge.type,
data: edge.data as DiagramEdge['data'],
}),
),
};
}
export function debounce<Args extends unknown[]>(fn: (...args: Args) => void, delayMs: number) {
let timer: ReturnType<typeof setTimeout> | undefined;
return (...args: Args) => {
if (timer) clearTimeout(timer);
timer = setTimeout(() => fn(...args), delayMs);
};
}