Init commit
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
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);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user