From a96eddf32c4f68647a1b34ddf10caa2310bafe60 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Wed, 7 Oct 2026 14:25:14 -0400 Subject: [PATCH] feat(npc-dialogue): bigger flow handles + delete edges via Delete - Enlarge flow-editor wire handles from size-2 (8px) to size-3 (12px) so they are easier to click and grab. - Select an edge by clicking it, then remove it with the Delete key. Selection is tracked in state and the highlight renders through BaseEdge stroke props so the chosen line stands out. - Edges are a derived projection, so deletion scrubs the hop from the source node data via a new pure removeHopFromNode() helper in dialogue.ts (choice row, condition branch, or single continuation). - Add tests/int/npc-dialogue-remove-hop.int.spec.ts (4 tests). --- .../admin/npc-dialogue/NpcDialogueEditor.tsx | 80 ++++++++++++++++--- .../admin/npc-dialogue/NpcDialogueNode.tsx | 10 +-- .../admin/npc-dialogue/RoutedFlowEdge.tsx | 8 +- src/lib/npc-dialogue/dialogue.ts | 23 ++++++ tests/int/npc-dialogue-remove-hop.int.spec.ts | 45 +++++++++++ 5 files changed, 147 insertions(+), 19 deletions(-) create mode 100644 tests/int/npc-dialogue-remove-hop.int.spec.ts diff --git a/src/components/admin/npc-dialogue/NpcDialogueEditor.tsx b/src/components/admin/npc-dialogue/NpcDialogueEditor.tsx index 1f7552c1..b48f435f 100644 --- a/src/components/admin/npc-dialogue/NpcDialogueEditor.tsx +++ b/src/components/admin/npc-dialogue/NpcDialogueEditor.tsx @@ -33,7 +33,7 @@ import type { FlowData, NpcDialogueNodeType, } from "@/lib/npc-dialogue/dialogue"; -import { deriveEdges, validateGraph } from "@/lib/npc-dialogue/dialogue"; +import { deriveEdges, removeHopFromNode, validateGraph } from "@/lib/npc-dialogue/dialogue"; import { NpcDialogueNode } from "./NpcDialogueNode"; import { NodeEditDialog } from "./NodeEditDialog"; import { RoutedFlowEdge } from "./RoutedFlowEdge"; @@ -96,6 +96,7 @@ function FlowEditor() { const [paneMenu, setPaneMenu] = useState(null); const [expanded, setExpanded] = useState(false); const [snap, setSnap] = useState(true); + const [selectedEdgeId, setSelectedEdgeId] = useState(null); const lastSerialized = useRef(""); const menuRef = useRef(null); @@ -133,13 +134,15 @@ function FlowEditor() { // unrelated cards instead of slicing through them. const syncedEdges = useMemo( () => - deriveEdges(nodes as DialogueNode[]).map((edge) => ({ - ...edge, - type: "routed", - animated: true, - markerEnd: EDGE_MARKER, - })), - [nodes], + deriveEdges(nodes as DialogueNode[]) + .map((edge) => ({ + ...edge, + type: "routed", + animated: true, + markerEnd: EDGE_MARKER, + selected: edge.id === selectedEdgeId, + })), + [nodes, selectedEdgeId], ); const serialize = useCallback((nds: Node[]) => { @@ -172,6 +175,12 @@ function FlowEditor() { [onNodesChange], ); + // Clicking the pane or an edge clears node selection; edge selection lives + // in its own state so it survives the node re-render. + const clearNodeSelection = useCallback(() => { + setNodes((nds) => nds.map((n) => (n.selected ? { ...n, selected: false } : n))); + }, [setNodes]); + useEffect(() => { if (!pendingPositionSave.current) return; pendingPositionSave.current = false; @@ -250,6 +259,25 @@ function FlowEditor() { [nodes, setNodes, serialize, editingNodeId], ); + /** + * Deletes one edge. Edges are derived, so this scrubs the hop it represents + * from the source node data: a choice row, a condition branch, or the single + * continuation. `sourceHandle` identifies which one (c0..cN / true / false / + * undefined). + */ + const deleteEdge = useCallback( + (edge: Edge) => { + const source = (nodes as DialogueNode[]).find((n) => n.id === edge.source); + if (!source) return; + const next = (nodes as DialogueNode[]).map((n) => + n.id === edge.source ? removeHopFromNode(n, edge.sourceHandle) : n, + ); + setNodes(next); + serialize(next); + }, + [nodes, setNodes, serialize], + ); + const onNodeContextMenu = useCallback( (e: ReactMouseEvent, node: Node) => { e.preventDefault(); @@ -359,8 +387,23 @@ function FlowEditor() { const onPaneClick = useCallback(() => { setContextMenu(null); setPaneMenu(null); + setSelectedEdgeId(null); }, []); + /** + * Selects an edge so it can be removed with the Delete key. Edges are a + * derived projection, so this only toggles selection: a second click on the + * same edge clears it, and either way node selection is cleared first. + */ + const onEdgeClick = useCallback( + (e: ReactMouseEvent, edge: Edge) => { + e.stopPropagation(); + setSelectedEdgeId((prev) => (prev === edge.id ? null : edge.id)); + clearNodeSelection(); + }, + [clearNodeSelection], + ); + const validation = useMemo(() => validateGraph(nodes as DialogueNode[]), [nodes]); // Close both context menus on escape / pointer outside. @@ -416,6 +459,16 @@ function FlowEditor() { ) { return; } + // An edge selection takes precedence over node selection. + if (selectedEdgeId) { + const edge = syncedEdges.find((candidate) => candidate.id === selectedEdgeId); + if (edge) { + e.preventDefault(); + setSelectedEdgeId(null); + deleteEdge(edge); + } + return; + } const selected = nodes.filter((n) => n.selected).map((n) => n.id); if (selected.length === 0) return; e.preventDefault(); @@ -423,7 +476,7 @@ function FlowEditor() { }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); - }, [nodes, editingNodeId, contextMenu, paneMenu, deleteNodes]); + }, [nodes, editingNodeId, contextMenu, paneMenu, selectedEdgeId, syncedEdges, deleteEdge, deleteNodes]); const buttonClass = "px-2 py-1 text-[10px] font-medium rounded border transition-colors cursor-pointer"; @@ -545,6 +598,7 @@ function FlowEditor() { // Single click selects (React Flow default); double-click edits. onNodeDoubleClick={(_e, node) => setEditingNodeId(node.id)} onNodeContextMenu={onNodeContextMenu} + onEdgeClick={onEdgeClick} onPaneClick={onPaneClick} onPaneContextMenu={onPaneContextMenu} nodeTypes={nodeTypes} @@ -566,10 +620,10 @@ function FlowEditor() {

- Click a node to select it, double-click to edit. Select and press Del (or right-click) to - delete. Double-click the canvas for a new dialogue node; right-click it for any type. Drag - a row's dot to wire that exact choice or branch. Use Expand to fill the screen (Esc to - shrink). + Click a node to select it, double-click to edit. Click an edge to select it, then press Del + (or right-click a node) to delete. Double-click the canvas for a new dialogue node; right-click + it for any type. Drag a row's dot to wire that exact choice or branch. Use Expand to fill + the screen (Esc to shrink).

{contextMenu && ( diff --git a/src/components/admin/npc-dialogue/NpcDialogueNode.tsx b/src/components/admin/npc-dialogue/NpcDialogueNode.tsx index de2eb5b3..ca560523 100644 --- a/src/components/admin/npc-dialogue/NpcDialogueNode.tsx +++ b/src/components/admin/npc-dialogue/NpcDialogueNode.tsx @@ -106,7 +106,7 @@ export function NpcDialogueNode({ data, type, selected }: NodeProps) { id={`c${index}`} type="source" position={position} - className="!size-2 !bg-amber-400/80" + className="!size-3 !bg-amber-400/80" style={{ right: -16 }} /> @@ -183,7 +183,7 @@ export function NpcDialogueNode({ data, type, selected }: NodeProps) { id="true" type="source" position={position} - className="!size-2 !bg-emerald-400/80" + className="!size-3 !bg-emerald-400/80" style={{ top: "auto", bottom: 26 }} />
@@ -194,7 +194,7 @@ export function NpcDialogueNode({ data, type, selected }: NodeProps) { id="false" type="source" position={position} - className="!size-2 !bg-rose-400/80" + className="!size-3 !bg-rose-400/80" style={{ bottom: 4 }} />
@@ -214,10 +214,10 @@ export function NpcDialogueNode({ data, type, selected }: NodeProps) { {genericSource && ( - + )} {!d.isStart && ( - + )} ); diff --git a/src/components/admin/npc-dialogue/RoutedFlowEdge.tsx b/src/components/admin/npc-dialogue/RoutedFlowEdge.tsx index c6fe42f5..50f70a7b 100644 --- a/src/components/admin/npc-dialogue/RoutedFlowEdge.tsx +++ b/src/components/admin/npc-dialogue/RoutedFlowEdge.tsx @@ -36,6 +36,7 @@ export function RoutedFlowEdge({ targetPosition, markerEnd, label, + selected, }: EdgeProps) { const nodeLookup = useStore((s) => s.nodeLookup); @@ -71,7 +72,12 @@ export function RoutedFlowEdge({ return ( <> - + {labelText ? (
({ + id: `${type}-1`, + type, + position: { x: 0, y: 0 }, + data, +}); + +describe("removeHopFromNode (edge deletion scrubs the derived hop)", () => { + it("removes a single choice row by its handle", () => { + const n = node("choice", { choices: [{ label: "a", target: "x" }, { label: "b", target: "y" }] }); + const next = removeHopFromNode(n, "c0"); + expect(next.data.choices).toEqual([{ label: "b", target: "y" }]); + // the other row is untouched + expect(n.data.choices).toEqual([ + { label: "a", target: "x" }, + { label: "b", target: "y" }, + ]); + }); + + it("removes a condition branch by name", () => { + const yes = node("condition", { condition: { kind: "flag", key: "k", op: "is_true" }, trueTarget: "t", falseTarget: "f" }); + expect(removeHopFromNode(yes, "true").data.trueTarget).toBeUndefined(); + expect(removeHopFromNode(yes, "true").data.falseTarget).toBe("f"); + + const no = node("condition", { condition: { kind: "flag", key: "k", op: "is_true" }, trueTarget: "t", falseTarget: "f" }); + const falseRemoved = removeHopFromNode(no, "false"); + expect(falseRemoved.data.falseTarget).toBeUndefined(); + expect(falseRemoved.data.trueTarget).toBe("t"); + }); + + it("clears the single continuation on a plain node", () => { + const n = node("dialogue", { body: "hi", next: { label: "next", target: "t" } }); + expect(removeHopFromNode(n).data.next).toBeUndefined(); + }); + + it("leaves a choice node unchanged when the handle is not a numbered row", () => { + const n = node("choice", { choices: [{ label: "a", target: "x" }] }); + expect(removeHopFromNode(n, undefined)).toEqual(n); + }); +});