diff --git a/src/components/admin/npc-dialogue/NpcDialogueEditor.tsx b/src/components/admin/npc-dialogue/NpcDialogueEditor.tsx index e755adea..7aa99795 100644 --- a/src/components/admin/npc-dialogue/NpcDialogueEditor.tsx +++ b/src/components/admin/npc-dialogue/NpcDialogueEditor.tsx @@ -21,7 +21,7 @@ import { type XYPosition, } from "@xyflow/react"; import "@xyflow/react/dist/style.css"; -import { Maximize2, Minimize2 } from "lucide-react"; +import { Magnet, Maximize2, Minimize2 } from "lucide-react"; import { useField } from "@payloadcms/ui"; @@ -51,6 +51,10 @@ const edgeTypes = { routed: RoutedFlowEdge }; /** Arrowhead tinted to match the animated cyan edge stroke. */ const EDGE_MARKER = { type: MarkerType.ArrowClosed, width: 14, height: 14, color: "#67e8f9" }; +/** Drag snapping grid (px); matches the Background dot gap. */ +const SNAP_GRID = 20; +const SNAP_STORAGE_KEY = "ptf:npc-dialogue:snap-to-grid"; + const defaultFlow: FlowData = { nodes: [], edges: [] }; type ContextMenu = { x: number; y: number; nodeId: string } | null; @@ -90,11 +94,38 @@ function FlowEditor() { const [contextMenu, setContextMenu] = useState(null); const [paneMenu, setPaneMenu] = useState(null); const [expanded, setExpanded] = useState(false); + const [snap, setSnap] = useState(true); const lastSerialized = useRef(""); const menuRef = useRef(null); const paneMenuRef = useRef(null); const canvasRef = useRef(null); + // Nodes created but never saved: closing their dialog without saving + // discards them instead of leaving an empty card on the canvas. + const draftIds = useRef>(new Set()); + + // Snap-to-grid is a per-browser preference (default on), like the tech + // tree's; reading it after mount keeps SSR/CSR markup identical. + useEffect(() => { + try { + const stored = window.localStorage.getItem(SNAP_STORAGE_KEY); + if (stored === "off") setSnap(false); + } catch { + // Storage unavailable: keep the default. + } + }, []); + + const toggleSnap = useCallback(() => { + setSnap((prev) => { + const next = !prev; + try { + window.localStorage.setItem(SNAP_STORAGE_KEY, next ? "on" : "off"); + } catch { + // Preference just won't persist. + } + return next; + }); + }, []); // Edges are a derived projection of the node data, never edited directly. // Every edge renders through the routed edge type so lines bend around @@ -152,6 +183,7 @@ function FlowEditor() { const next = [...nodes, node]; setNodes(next); serialize(next); + draftIds.current.add(node.id); setEditingNodeId(node.id); setPaneMenu(null); }, @@ -163,6 +195,7 @@ function FlowEditor() { const next = nodes.map((n) => (n.id === nodeId ? { ...n, data } : n)); setNodes(next); serialize(next); + draftIds.current.delete(nodeId); setEditingNodeId(null); }, [nodes, setNodes, serialize], @@ -420,6 +453,20 @@ function FlowEditor() { {nodes.length} node{nodes.length !== 1 ? "s" : ""} +