diff --git a/src/components/admin/npc-dialogue/NodeEditDialog.tsx b/src/components/admin/npc-dialogue/NodeEditDialog.tsx index f4211a6f..8025b29a 100644 --- a/src/components/admin/npc-dialogue/NodeEditDialog.tsx +++ b/src/components/admin/npc-dialogue/NodeEditDialog.tsx @@ -1,7 +1,7 @@ "use client"; import { useCallback, useEffect, useRef, useState } from "react"; -import { XIcon } from "lucide-react"; +import { ChevronDownIcon, ChevronUpIcon, XIcon } from "lucide-react"; import { Dialog, @@ -26,6 +26,7 @@ import type { NpcDialogueNodeType, } from "@/lib/npc-dialogue/dialogue"; import { EFFECT_TYPES } from "@/lib/npc-dialogue/dialogue"; +import { reorderChoice } from "@/lib/npc-dialogue/reorder"; interface NodeEditDialogProps { readonly node: DialogueNode | null; @@ -184,6 +185,13 @@ export function NodeEditDialog({ node, allNodes, open, onOpenChange, onSave }: N const removeChoice = (index: number) => patch({ choices: (draft.choices ?? []).filter((_, i) => i !== index) }); + /** Swap a choice with its neighbour so choices can be reordered. No-op at + * the ends, so the buttons can be disabled there. */ + const moveChoice = (index: number, direction: -1 | 1) => { + const ordered = reorderChoice(draft.choices ?? [], index, direction); + if (ordered) patch({ choices: ordered }); + }; + /** Toggle/update a choice's visibility gate; null removes the gate. */ const setChoiceCondition = (index: number, partial: Partial | null) => patch({ @@ -285,6 +293,30 @@ export function NodeEditDialog({ node, allNodes, open, onOpenChange, onSave }: N {(draft.choices ?? []).map((choice, index) => (
+
+ + +
= choices.length) return null; + const ordered = [...choices]; + [ordered[index], ordered[target]] = [ordered[target], ordered[index]]; + return ordered; +} diff --git a/tests/int/npc-dialogue-reorder.int.spec.ts b/tests/int/npc-dialogue-reorder.int.spec.ts new file mode 100644 index 00000000..4b26df77 --- /dev/null +++ b/tests/int/npc-dialogue-reorder.int.spec.ts @@ -0,0 +1,32 @@ +import { describe, expect, it } from "vitest"; + +import { reorderChoice } from "@/lib/npc-dialogue/reorder"; +import type { DialogueNext } from "@/lib/npc-dialogue/dialogue"; + +/** Build a choice with a unique target so reordering is observable. */ +const choice = (label: string): DialogueNext => ({ label, target: `node-${label}` }); + +describe("reorderChoice (editor choice ordering)", () => { + const choices = [choice("a"), choice("b"), choice("c")]; + + it("swaps a choice with its neighbour", () => { + expect(reorderChoice(choices, 0, 1)).toEqual([choice("b"), choice("a"), choice("c")]); + // direction -1 from index 1 lands on the same swap + expect(reorderChoice(choices, 1, -1)).toEqual([choice("b"), choice("a"), choice("c")]); + expect(reorderChoice(choices, 2, -1)).toEqual([choice("a"), choice("c"), choice("b")]); + }); + + it("returns null at the edges so nothing moves", () => { + expect(reorderChoice(choices, 0, -1)).toBeNull(); + expect(reorderChoice(choices, 2, 1)).toBeNull(); + // A single-item list is an edge in every direction. + expect(reorderChoice([choice("only")], 0, 1)).toBeNull(); + expect(reorderChoice([choice("only")], 0, -1)).toBeNull(); + }); + + it("does not mutate the input array", () => { + const moved = reorderChoice(choices, 1, 1); + expect(moved).not.toBe(choices); + expect(choices).toEqual([choice("a"), choice("b"), choice("c")]); + }); +});