fix: persist edge animation mode (None/Snake/Flow) end-to-end
- canvasStore.onConnect: include animated in edge data object (was silently dropped)
- Backend schemas: normalize animated bool/int to string ('none'/'snake'/'flow') via field_validator
- ORM model: change animated column from Boolean to String
- DB migration: convert existing 0/1 boolean rows to 'none'/'snake' strings
This commit is contained in:
@@ -98,18 +98,32 @@ describe('canvasStore', () => {
|
||||
expect(useCanvasStore.getState().selectedNodeId).toBeNull()
|
||||
})
|
||||
|
||||
it('onNodesChange marks unsaved', () => {
|
||||
it('onNodesChange marks unsaved for position changes', () => {
|
||||
useCanvasStore.getState().addNode(makeNode('n1'))
|
||||
useCanvasStore.getState().markSaved()
|
||||
useCanvasStore.getState().onNodesChange([{ type: 'select', id: 'n1', selected: true }])
|
||||
useCanvasStore.getState().onNodesChange([{ type: 'position', id: 'n1', dragging: false }])
|
||||
expect(useCanvasStore.getState().hasUnsavedChanges).toBe(true)
|
||||
})
|
||||
|
||||
it('onEdgesChange marks unsaved', () => {
|
||||
it('onNodesChange does not mark unsaved for select-only changes', () => {
|
||||
useCanvasStore.getState().addNode(makeNode('n1'))
|
||||
useCanvasStore.getState().markSaved()
|
||||
useCanvasStore.getState().onNodesChange([{ type: 'select', id: 'n1', selected: true }])
|
||||
expect(useCanvasStore.getState().hasUnsavedChanges).toBe(false)
|
||||
})
|
||||
|
||||
it('onEdgesChange marks unsaved for remove changes', () => {
|
||||
useCanvasStore.setState((s) => ({ edges: [...s.edges, makeEdge('e1', 'n1', 'n2')] }))
|
||||
useCanvasStore.getState().markSaved()
|
||||
useCanvasStore.getState().onEdgesChange([{ type: 'remove', id: 'e1' }])
|
||||
expect(useCanvasStore.getState().hasUnsavedChanges).toBe(true)
|
||||
})
|
||||
|
||||
it('onEdgesChange does not mark unsaved for select-only changes', () => {
|
||||
useCanvasStore.setState((s) => ({ edges: [...s.edges, makeEdge('e1', 'n1', 'n2')] }))
|
||||
useCanvasStore.getState().markSaved()
|
||||
useCanvasStore.getState().onEdgesChange([{ type: 'select', id: 'e1', selected: true }])
|
||||
expect(useCanvasStore.getState().hasUnsavedChanges).toBe(true)
|
||||
expect(useCanvasStore.getState().hasUnsavedChanges).toBe(false)
|
||||
})
|
||||
|
||||
it('onConnect adds an edge between two nodes', () => {
|
||||
@@ -130,6 +144,13 @@ describe('canvasStore', () => {
|
||||
expect(edges[0].data?.label).toBe('uplink')
|
||||
})
|
||||
|
||||
it('onConnect preserves animated from edge data', () => {
|
||||
const conn = Object.assign({ source: 'n1', target: 'n2', sourceHandle: null, targetHandle: null }, { type: 'ethernet', animated: 'snake' })
|
||||
useCanvasStore.getState().onConnect(conn)
|
||||
const { edges } = useCanvasStore.getState()
|
||||
expect(edges[0].data?.animated).toBe('snake')
|
||||
})
|
||||
|
||||
it('onConnect preserves sourceHandle and targetHandle for cluster edges', () => {
|
||||
const conn = Object.assign({ source: 'n1', target: 'n2', sourceHandle: 'cluster-right', targetHandle: 'cluster-left' }, { type: 'cluster' })
|
||||
useCanvasStore.getState().onConnect(conn)
|
||||
@@ -140,6 +161,15 @@ describe('canvasStore', () => {
|
||||
expect(edges[0].type).toBe('cluster')
|
||||
})
|
||||
|
||||
it('deleteNode also removes children with matching parentId', () => {
|
||||
useCanvasStore.getState().addNode(makeNode('parent'))
|
||||
useCanvasStore.getState().addNode(makeNode('child', { parent_id: 'parent' }))
|
||||
useCanvasStore.getState().deleteNode('parent')
|
||||
const { nodes } = useCanvasStore.getState()
|
||||
expect(nodes.find((n) => n.id === 'parent')).toBeUndefined()
|
||||
expect(nodes.find((n) => n.id === 'child')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('addNode with parent_id sets parentId and extent', () => {
|
||||
useCanvasStore.getState().addNode(makeNode('parent'))
|
||||
useCanvasStore.getState().addNode(makeNode('child', { parent_id: 'parent' }))
|
||||
|
||||
@@ -127,13 +127,13 @@ export const useCanvasStore = create<CanvasState>((set) => ({
|
||||
onNodesChange: (changes) =>
|
||||
set((state) => ({
|
||||
nodes: applyNodeChanges(changes, state.nodes),
|
||||
hasUnsavedChanges: true,
|
||||
hasUnsavedChanges: state.hasUnsavedChanges || changes.some((c) => c.type !== 'select'),
|
||||
})),
|
||||
|
||||
onEdgesChange: (changes) =>
|
||||
set((state) => ({
|
||||
edges: applyEdgeChanges(changes, state.edges),
|
||||
hasUnsavedChanges: true,
|
||||
hasUnsavedChanges: state.hasUnsavedChanges || changes.some((c) => c.type !== 'select'),
|
||||
})),
|
||||
|
||||
onConnect: (connection) =>
|
||||
@@ -150,7 +150,7 @@ export const useCanvasStore = create<CanvasState>((set) => ({
|
||||
sourceHandle: normalizeHandle(extra.sourceHandle),
|
||||
targetHandle: normalizeHandle(extra.targetHandle),
|
||||
type: edgeType,
|
||||
data: { type: edgeType, label: extra.label, vlan_id: extra.vlan_id, custom_color: extra.custom_color, path_style: extra.path_style },
|
||||
data: { type: edgeType, label: extra.label, vlan_id: extra.vlan_id, custom_color: extra.custom_color, path_style: extra.path_style, animated: extra.animated },
|
||||
}, state.edges),
|
||||
hasUnsavedChanges: true,
|
||||
}
|
||||
@@ -163,10 +163,13 @@ export const useCanvasStore = create<CanvasState>((set) => ({
|
||||
const enriched = node.data.parent_id
|
||||
? { ...node, parentId: node.data.parent_id, extent: 'parent' as const }
|
||||
: node
|
||||
// Parents must come before children in the array
|
||||
// Parents must come before children in the array (React Flow requirement)
|
||||
const withoutNew = state.nodes.filter((n) => n.id !== node.id)
|
||||
if (enriched.parentId) {
|
||||
return { nodes: [...withoutNew, enriched], hasUnsavedChanges: true }
|
||||
const parentIdx = withoutNew.findIndex((n) => n.id === enriched.parentId)
|
||||
const insertAt = parentIdx >= 0 ? parentIdx + 1 : withoutNew.length
|
||||
const nodes = [...withoutNew.slice(0, insertAt), enriched, ...withoutNew.slice(insertAt)]
|
||||
return { nodes, hasUnsavedChanges: true }
|
||||
}
|
||||
return { nodes: [...withoutNew, enriched], hasUnsavedChanges: true }
|
||||
}),
|
||||
@@ -180,12 +183,20 @@ export const useCanvasStore = create<CanvasState>((set) => ({
|
||||
})),
|
||||
|
||||
deleteNode: (id) =>
|
||||
set((state) => ({
|
||||
nodes: state.nodes.filter((n) => n.id !== id),
|
||||
edges: state.edges.filter((e) => e.source !== id && e.target !== id),
|
||||
selectedNodeId: state.selectedNodeId === id ? null : state.selectedNodeId,
|
||||
hasUnsavedChanges: true,
|
||||
})),
|
||||
set((state) => {
|
||||
const idsToRemove = new Set<string>()
|
||||
const collect = (nodeId: string) => {
|
||||
idsToRemove.add(nodeId)
|
||||
state.nodes.filter((n) => n.parentId === nodeId).forEach((n) => collect(n.id))
|
||||
}
|
||||
collect(id)
|
||||
return {
|
||||
nodes: state.nodes.filter((n) => !idsToRemove.has(n.id)),
|
||||
edges: state.edges.filter((e) => !idsToRemove.has(e.source) && !idsToRemove.has(e.target)),
|
||||
selectedNodeId: idsToRemove.has(state.selectedNodeId ?? '') ? null : state.selectedNodeId,
|
||||
hasUnsavedChanges: true,
|
||||
}
|
||||
}),
|
||||
|
||||
updateEdge: (id, data) =>
|
||||
set((state) => ({
|
||||
@@ -245,6 +256,6 @@ export const useCanvasStore = create<CanvasState>((set) => ({
|
||||
// React Flow requires parents before children in the array
|
||||
const parents = nodes.filter((n) => !n.parentId)
|
||||
const children = nodes.filter((n) => !!n.parentId)
|
||||
set({ nodes: [...parents, ...children], edges, hasUnsavedChanges: false, selectedNodeId: null })
|
||||
set({ nodes: [...parents, ...children], edges, hasUnsavedChanges: false, selectedNodeId: null, past: [], future: [], clipboard: [] })
|
||||
},
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user