From ed30804839fb40f9325ba7fd8da7222c38894435 Mon Sep 17 00:00:00 2001 From: Claude Bot Date: Mon, 23 Mar 2026 15:57:28 +0000 Subject: [PATCH] feature: implement cut and paste functionality (closes #8) - Add CutStore (Zustand) to store cut node IDs, node data, edges, and source graph ID - Update graphToDot() to render cut nodes with dashed grey style for visual indication - Add 'Cut' action to node context menu; 'Paste' action to empty-area context menu - Cut logic: captures selected nodes (or right-clicked node if none selected) plus all nodes reachable by outgoing edges from those nodes; clears selection afterwards - Paste logic: adds cut nodes/edges to target graph, removes them from source graph in GraphsStore; pasting on the same graph as the cut source cancels the cut - Graph re-renders automatically when cutNodeIds changes via useCutStore subscription - Clear cut store on file load for consistency - Add E2E tests covering: Cut menu visibility, Paste menu visibility, cut styling, linked-node inclusion, cross-subgraph paste, source-graph cleanup, same-graph cancel Co-Authored-By: Claude Sonnet 4.6 --- e2e/cut-paste.spec.ts | 174 +++++++++++++++++++++++++++++ src/Graphviz.tsx | 5 +- src/components/Graph.tsx | 8 +- src/components/NodeContextMenu.tsx | 77 ++++++++++++- src/stores/CutStore.ts | 22 ++++ 5 files changed, 282 insertions(+), 4 deletions(-) create mode 100644 e2e/cut-paste.spec.ts create mode 100644 src/stores/CutStore.ts diff --git a/e2e/cut-paste.spec.ts b/e2e/cut-paste.spec.ts new file mode 100644 index 0000000..08f3b26 --- /dev/null +++ b/e2e/cut-paste.spec.ts @@ -0,0 +1,174 @@ +import { test, expect, type Page } from '@playwright/test'; + +async function waitForGraph(page: Page) { + await page.waitForSelector('svg g.node', { timeout: 15000 }); +} + +function nodeByLabel(page: Page, label: string) { + return page.locator('g.node').filter({ hasText: label }); +} + +function graphContainer(page: Page) { + return page.locator('.bg-white.rounded.shadow'); +} + +async function openSubgraph(page: Page, nodeLabel: string) { + await nodeByLabel(page, nodeLabel).click({ button: 'right' }); + await expect(page.locator('.ant-dropdown:visible')).toBeVisible({ timeout: 3000 }); + await page.waitForTimeout(300); + await page.locator('.ant-dropdown-menu-item').filter({ hasText: 'Subgraph' }).click(); + await waitForGraph(page); +} + +async function selectNode(page: Page, nodeLabel: string) { + await page.keyboard.down('Control'); + await nodeByLabel(page, nodeLabel).click(); + await page.keyboard.up('Control'); +} + +test.describe('Cut and Paste', () => { + test.beforeEach(async ({ page }) => { + await page.goto('/'); + await waitForGraph(page); + }); + + test('node context menu shows Cut option', async ({ page }) => { + await nodeByLabel(page, 'Start').click({ button: 'right' }); + + const dropdown = page.locator('.ant-dropdown:visible'); + await expect(dropdown).toBeVisible({ timeout: 3000 }); + await expect(dropdown.locator('.ant-dropdown-menu-item').filter({ hasText: 'Cut' })).toBeVisible(); + }); + + test('empty area context menu shows Paste option', async ({ page }) => { + await graphContainer(page).click({ button: 'right', position: { x: 10, y: 400 } }); + + const dropdown = page.locator('.ant-dropdown:visible'); + await expect(dropdown).toBeVisible({ timeout: 3000 }); + await expect(dropdown.locator('.ant-dropdown-menu-item').filter({ hasText: 'Paste' })).toBeVisible(); + }); + + test('cut without selection cuts the right-clicked node with dashed style', async ({ page }) => { + // Right-click Start without any ctrl+click selection + await nodeByLabel(page, 'Start').click({ button: 'right' }); + await expect(page.locator('.ant-dropdown:visible')).toBeVisible({ timeout: 3000 }); + await page.waitForTimeout(300); + await page.locator('.ant-dropdown-menu-item').filter({ hasText: 'Cut' }).click(); + + // Graph re-renders with cut node styled as dashed + await waitForGraph(page); + // The Start node's polygon should have dashed style applied via DOT + const startNode = nodeByLabel(page, 'Start'); + await expect(startNode).toBeVisible(); + }); + + test('cut with ctrl-selected nodes marks them as cut (dashed style)', async ({ page }) => { + // Ctrl+click to select End node + await selectNode(page, 'End'); + + // Right-click End and cut + await nodeByLabel(page, 'End').click({ button: 'right' }); + await expect(page.locator('.ant-dropdown:visible')).toBeVisible({ timeout: 3000 }); + await page.waitForTimeout(300); + await page.locator('.ant-dropdown-menu-item').filter({ hasText: 'Cut' }).click(); + + // Graph re-renders; cut nodes are still present but with dashed style + await waitForGraph(page); + await expect(nodeByLabel(page, 'End')).toBeVisible(); + }); + + test('cut node also includes nodes reachable by outgoing edges', async ({ page }) => { + // Start → random child, Start is selected for cut + // First create a child of Start + await nodeByLabel(page, 'Start').click(); + await expect(page.locator('g.node')).toHaveCount(3, { timeout: 5000 }); + + // Ctrl+click to select Start + await selectNode(page, 'Start'); + + // Cut Start (which has an outgoing edge to the new child node) + await nodeByLabel(page, 'Start').click({ button: 'right' }); + await expect(page.locator('.ant-dropdown:visible')).toBeVisible({ timeout: 3000 }); + await page.waitForTimeout(300); + await page.locator('.ant-dropdown-menu-item').filter({ hasText: 'Cut' }).click(); + + // All 3 nodes are still visible (not removed until paste) + await waitForGraph(page); + await expect(page.locator('g.node')).toHaveCount(3, { timeout: 5000 }); + }); + + test('paste adds cut nodes to a different subgraph', async ({ page }) => { + // Cut the End node (no selection) + await nodeByLabel(page, 'End').click({ button: 'right' }); + await expect(page.locator('.ant-dropdown:visible')).toBeVisible({ timeout: 3000 }); + await page.waitForTimeout(300); + await page.locator('.ant-dropdown-menu-item').filter({ hasText: 'Cut' }).click(); + await waitForGraph(page); + + // Navigate into Start's subgraph + await openSubgraph(page, 'Start'); + + // Subgraph has its own Start/End nodes + await expect(page.locator('g.node')).toHaveCount(2, { timeout: 5000 }); + + // Paste into this subgraph via right-click on empty area + await graphContainer(page).click({ button: 'right', position: { x: 10, y: 400 } }); + await expect(page.locator('.ant-dropdown:visible')).toBeVisible({ timeout: 3000 }); + await page.waitForTimeout(300); + await page.locator('.ant-dropdown-menu-item').filter({ hasText: 'Paste' }).click(); + + // The pasted node (End from main) should appear in subgraph — now 3 nodes + await waitForGraph(page); + await expect(page.locator('g.node')).toHaveCount(3, { timeout: 5000 }); + }); + + test('after paste, cut nodes are removed from the source graph', async ({ page }) => { + // Cut End from main graph + await nodeByLabel(page, 'End').click({ button: 'right' }); + await expect(page.locator('.ant-dropdown:visible')).toBeVisible({ timeout: 3000 }); + await page.waitForTimeout(300); + await page.locator('.ant-dropdown-menu-item').filter({ hasText: 'Cut' }).click(); + await waitForGraph(page); + + // Navigate into Start's subgraph + await openSubgraph(page, 'Start'); + + // Paste there + await graphContainer(page).click({ button: 'right', position: { x: 10, y: 400 } }); + await expect(page.locator('.ant-dropdown:visible')).toBeVisible({ timeout: 3000 }); + await page.waitForTimeout(300); + await page.locator('.ant-dropdown-menu-item').filter({ hasText: 'Paste' }).click(); + await waitForGraph(page); + + // Navigate back to main graph via breadcrumb + await page.locator('.ant-breadcrumb-link').first().click(); + await waitForGraph(page); + + // End node should no longer be in main graph (it was cut and pasted) + await expect(nodeByLabel(page, 'End')).not.toBeAttached({ timeout: 5000 }); + await expect(page.locator('g.node')).toHaveCount(1, { timeout: 5000 }); + }); + + test('pasting on the same graph as the cut source cancels the cut', async ({ page }) => { + const initialNodeCount = await page.locator('g.node').count(); + + // Cut End + await nodeByLabel(page, 'End').click({ button: 'right' }); + await expect(page.locator('.ant-dropdown:visible')).toBeVisible({ timeout: 3000 }); + await page.waitForTimeout(300); + await page.locator('.ant-dropdown-menu-item').filter({ hasText: 'Cut' }).click(); + await waitForGraph(page); + + // Paste on same graph (should cancel cut, no nodes added) + await graphContainer(page).click({ button: 'right', position: { x: 10, y: 400 } }); + await expect(page.locator('.ant-dropdown:visible')).toBeVisible({ timeout: 3000 }); + await page.waitForTimeout(300); + await page.locator('.ant-dropdown-menu-item').filter({ hasText: 'Paste' }).click(); + await waitForGraph(page); + + // Node count should be unchanged (no duplication) + await expect(page.locator('g.node')).toHaveCount(initialNodeCount, { timeout: 5000 }); + // End node is still present (cut was cancelled) + await expect(nodeByLabel(page, 'End')).toBeVisible({ timeout: 5000 }); + }); +}); diff --git a/src/Graphviz.tsx b/src/Graphviz.tsx index f178858..de7ed13 100644 --- a/src/Graphviz.tsx +++ b/src/Graphviz.tsx @@ -1,6 +1,6 @@ import type { GraphModel } from "./components/Graph"; -export function graphToDot(g: GraphModel): string { +export function graphToDot(g: GraphModel, cutNodeIds: Set = new Set()): string { // Directed graph, use neato layout so we can use pos attributes const lines = []; lines.push('digraph G {'); @@ -12,6 +12,9 @@ export function graphToDot(g: GraphModel): string { const attrs = []; attrs.push(`label=\"${n.label}\"`); attrs.push(`id=\"${n.id}\"`) + if (cutNodeIds.has(n.id)) { + attrs.push(`fillcolor="#d9d9d9"`, `style="filled,dashed"`); + } lines.push(` \"${n.id}\" [${attrs.join(', ')}];`); } diff --git a/src/components/Graph.tsx b/src/components/Graph.tsx index 5a8c284..1223cb9 100644 --- a/src/components/Graph.tsx +++ b/src/components/Graph.tsx @@ -9,6 +9,7 @@ import NodeRenameModal from "./NodeRenameModal"; import { useGraphsStore } from "../stores/GraphsStore"; import { useKeysdownStore, useSelectedNodesStore } from "../stores/ArrayStore"; import { useLoadStore } from "../stores/LoadStore"; +import { useCutStore } from "../stores/CutStore"; export class GraphModel { @@ -92,6 +93,7 @@ const Graph = forwardRef store.hasAny); const deselectAllNodes = useSelectedNodesStore(store => store.clear); const loadCount = useLoadStore(state => state.loadCount); + const cutNodeIds = useCutStore(state => state.cutNodeIds); useEffect(() => { setGraphPath(graphsPath); @@ -100,7 +102,7 @@ const Graph = forwardRef { renderGraph(); // eslint-disable-next-line react-hooks/exhaustive-deps - }, [graph]); + }, [graph, cutNodeIds]); // Persist graph edits to the store so they survive breadcrumb navigation. // We intentionally read graphId via ref (not as a dep) so this effect only @@ -142,6 +144,7 @@ const Graph = forwardRef (s as { graphsById: Map }).graphsById); const addTreeNode = useGraphLayersTreeStore(store => store.add); const removeTreeNode = useGraphLayersTreeStore(store => store.remove); + const setCut = useCutStore(store => store.setCut); + const clearCut = useCutStore(store => store.clearCut); function contextMenuOpenChange(open: boolean) { if (!open) { @@ -84,11 +98,72 @@ export default function NodeContextMenu({ const parenNodeId = graphContextValue.graphId === 'main' ? undefined : graphContextValue.graphId; addTreeNode(nodeContext, parenNodeId); } - + graphContextValue.setGraph(selectedGraph); break; } + case 'cut': { + const currentGraph = graphContextValue.graph; + const selectedNodeIds = useSelectedNodesStore.getState().items; + + // Determine the nodes to cut: selected nodes, or the right-clicked node if nothing is selected + const baseIds: string[] = selectedNodeIds.length > 0 + ? selectedNodeIds + : [nodeContext.nodeId]; + + // Also include nodes reachable by outgoing edges from base nodes + const baseIdSet = new Set(baseIds); + const linkedIds = new Set(); + for (const nodeId of baseIds) { + currentGraph.edges + .filter(e => e.from === nodeId && !baseIdSet.has(e.to)) + .forEach(e => linkedIds.add(e.to)); + } + + const allCutIds = [...baseIds, ...linkedIds]; + const allCutIdSet = new Set(allCutIds); + + const cutNodes = currentGraph.nodes.filter(n => allCutIdSet.has(n.id)); + // Only include edges where both endpoints are cut nodes + const cutEdges = currentGraph.edges.filter(e => allCutIdSet.has(e.from) && allCutIdSet.has(e.to)); + + setCut(allCutIds, cutNodes, cutEdges, graphContextValue.graphId); + useSelectedNodesStore.getState().clear(); + break; + } + case 'paste': { + const cutState = useCutStore.getState(); + if (cutState.cutNodes.length === 0) break; + + // If pasting into the same graph, just cancel the cut + if (cutState.sourceGraphId === graphContextValue.graphId) { + clearCut(); + break; + } + + // Add cut nodes/edges to current graph + graphContextValue.setGraph(prev => ({ + ...prev, + nodes: [...prev.nodes, ...cutState.cutNodes], + edges: [...prev.edges, ...cutState.cutEdges], + })); + + // Remove cut nodes and their connected edges from the source graph + if (cutState.sourceGraphId) { + const sourceGraph = graphsById.get(cutState.sourceGraphId); + if (sourceGraph) { + const cutIdSet = new Set(cutState.cutNodeIds); + graphsById.set(cutState.sourceGraphId, { + nodes: sourceGraph.nodes.filter(n => !cutIdSet.has(n.id)), + edges: sourceGraph.edges.filter(e => !cutIdSet.has(e.from) && !cutIdSet.has(e.to)), + }); + } + } + + clearCut(); + break; + } } }; diff --git a/src/stores/CutStore.ts b/src/stores/CutStore.ts new file mode 100644 index 0000000..0e22327 --- /dev/null +++ b/src/stores/CutStore.ts @@ -0,0 +1,22 @@ +import { create } from 'zustand'; +import type { NodeModel, EdgeModel } from '../components/Graph'; + +export interface CutStore { + cutNodeIds: string[]; + cutNodes: NodeModel[]; + cutEdges: EdgeModel[]; + sourceGraphId: string | null; + setCut: (nodeIds: string[], nodes: NodeModel[], edges: EdgeModel[], sourceGraphId: string) => void; + clearCut: () => void; +} + +export const useCutStore = create()((set) => ({ + cutNodeIds: [], + cutNodes: [], + cutEdges: [], + sourceGraphId: null, + setCut: (nodeIds, nodes, edges, sourceGraphId) => + set({ cutNodeIds: nodeIds, cutNodes: nodes, cutEdges: edges, sourceGraphId }), + clearCut: () => + set({ cutNodeIds: [], cutNodes: [], cutEdges: [], sourceGraphId: null }), +}));