feat(npc-dialogue): selection ring, draft-cancel removal, snap-to-grid toggle
- Selected node cards gain a gentle cyan ring so selection state is obvious without fighting the per-type border colors. - A node created via double-click/toolbar/pane menu opens straight into its dialog; closing that dialog with Cancel (or Escape/backdrop/X) now discards the empty draft instead of persisting it. Saving clears the draft marker, so cancelling a later edit never deletes anything. - New Snap toggle in the editor toolbar enables React Flow grid snapping (20px, matching the background dots); the preference is remembered per browser like the tech tree's.
This commit is contained in:
parent
5a9bd0052b
commit
46f1abd4da
2 changed files with 62 additions and 4 deletions
|
|
@ -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<ContextMenu>(null);
|
||||
const [paneMenu, setPaneMenu] = useState<PaneMenu>(null);
|
||||
const [expanded, setExpanded] = useState(false);
|
||||
const [snap, setSnap] = useState(true);
|
||||
|
||||
const lastSerialized = useRef<string>("");
|
||||
const menuRef = useRef<HTMLDivElement | null>(null);
|
||||
const paneMenuRef = useRef<HTMLDivElement | null>(null);
|
||||
const canvasRef = useRef<HTMLDivElement | null>(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<Set<string>>(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() {
|
|||
<span className="ml-auto text-[10px] text-zinc-500">
|
||||
{nodes.length} node{nodes.length !== 1 ? "s" : ""}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
title={snap ? "Nodes snap to the grid while dragging" : "Grid snapping is off"}
|
||||
aria-pressed={snap}
|
||||
onClick={toggleSnap}
|
||||
className={`${buttonClass} inline-flex items-center gap-1 ${
|
||||
snap
|
||||
? "bg-cyan-950 border-cyan-700 text-cyan-200 hover:bg-cyan-900"
|
||||
: "bg-zinc-800 border-zinc-600 text-zinc-400 hover:bg-zinc-700"
|
||||
}`}
|
||||
>
|
||||
<Magnet size={12} />
|
||||
Snap {snap ? "On" : "Off"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
title={expanded ? "Shrink editor (Esc)" : "Expand editor to fill the screen"}
|
||||
|
|
@ -477,6 +524,8 @@ function FlowEditor() {
|
|||
onPaneContextMenu={onPaneContextMenu}
|
||||
nodeTypes={nodeTypes}
|
||||
edgeTypes={edgeTypes}
|
||||
snapToGrid={snap}
|
||||
snapGrid={[SNAP_GRID, SNAP_GRID]}
|
||||
fitView
|
||||
zoomOnDoubleClick={false}
|
||||
colorMode="dark"
|
||||
|
|
@ -553,7 +602,12 @@ function FlowEditor() {
|
|||
allNodes={nodes as DialogueNode[]}
|
||||
open={editingNodeId !== null}
|
||||
onOpenChange={(open) => {
|
||||
if (!open) setEditingNodeId(null);
|
||||
if (open || !editingNodeId) return;
|
||||
const id = editingNodeId;
|
||||
setEditingNodeId(null);
|
||||
// A brand-new node abandoned in its own dialog is a cancelled
|
||||
// draft: remove it instead of persisting an empty card.
|
||||
if (draftIds.current.delete(id)) deleteNodes([id]);
|
||||
}}
|
||||
onSave={handleNodeSave}
|
||||
/>
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ const position: Position = Position.Right;
|
|||
const inputPosition: Position = Position.Left;
|
||||
|
||||
/** Single reusable canvas node for every dialogue node type. */
|
||||
export function NpcDialogueNode({ data, type }: NodeProps) {
|
||||
export function NpcDialogueNode({ data, type, selected }: NodeProps) {
|
||||
const d = data as DialogueNodeData;
|
||||
const nodeType = type as NpcDialogueNodeType;
|
||||
const meta = DIALOGUE_NODES[nodeType];
|
||||
|
|
@ -33,7 +33,11 @@ export function NpcDialogueNode({ data, type }: NodeProps) {
|
|||
|
||||
return (
|
||||
<div
|
||||
className={`w-[20rem] max-w-[20rem] rounded border bg-zinc-900 shadow-lg ${meta.border}`}
|
||||
className={`w-[20rem] max-w-[20rem] rounded border bg-zinc-900 shadow-lg ${meta.border} ${
|
||||
// Gentle glow so the selection reads without shouting over the
|
||||
// per-type border colors.
|
||||
selected ? "ring-2 ring-cyan-400/50" : ""
|
||||
}`}
|
||||
>
|
||||
<div className="flex items-center gap-2 border-b border-white/10 px-3 py-1.5">
|
||||
<NodeIcon type={nodeType} />
|
||||
|
|
|
|||
Loading…
Reference in a new issue