diff --git a/src/components/admin/npc-dialogue/MarkupEditor.tsx b/src/components/admin/npc-dialogue/MarkupEditor.tsx index 2fee4644..53f92f0b 100644 --- a/src/components/admin/npc-dialogue/MarkupEditor.tsx +++ b/src/components/admin/npc-dialogue/MarkupEditor.tsx @@ -1,6 +1,6 @@ "use client"; -import { useEffect, useRef, useState } from "react"; +import { memo, useEffect, useRef, useState } from "react"; import { DialogueText } from "@/lib/npc-dialogue/DialogueText"; @@ -18,6 +18,26 @@ const COLOR_CHOICES = [ "slate", ] as const; +/** + * How long the preview waits after the last keystroke. + * + * The preview is a full markup parse and render, and it used to run on every + * character: typing a long line with colours, bold and a link meant re-parsing + * the whole line per keypress, which is what made the box feel sticky. A fifth + * of a second is below noticing for a preview and removes the work entirely while + * someone is typing. + */ +const PREVIEW_DEBOUNCE_MS = 220; + +/** The rendered preview, memoised so an unchanged line never re-parses. */ +const MarkupPreview = memo(function MarkupPreview({ text }: { text: string }) { + return ( +
+ +
+ ); +}); + /** * Textarea for authored dialogue strings with a formatting toolbar, a * right-click markup menu, and a live `DialogueText` preview. Storage stays @@ -48,6 +68,14 @@ export function MarkupEditor({ const innerRef = useRef(null); const el = () => textareaRef?.current ?? innerRef.current; const [menu, setMenu] = useState<{ x: number; y: number } | null>(null); + /** What the preview shows: the value as of the last pause in typing. */ + const [previewText, setPreviewText] = useState(value); + + useEffect(() => { + if (previewText === value) return; + const timer = setTimeout(() => setPreviewText(value), PREVIEW_DEBOUNCE_MS); + return () => clearTimeout(timer); + }, [value, previewText]); // Close the context menu on any outside click or Escape. useEffect(() => { @@ -171,12 +199,10 @@ export function MarkupEditor({ "bg-zinc-800 border border-white/20 rounded px-2 py-1.5 text-sm text-white placeholder:text-white/30 resize-y" } /> - {preview && value.trim() !== "" && ( + {preview && previewText.trim() !== "" && (
Preview -
- -
+
)} {menu && ( diff --git a/src/lib/npc-dialogue/DialogueText.tsx b/src/lib/npc-dialogue/DialogueText.tsx index bb987199..44ffc49f 100644 --- a/src/lib/npc-dialogue/DialogueText.tsx +++ b/src/lib/npc-dialogue/DialogueText.tsx @@ -1,6 +1,6 @@ "use client"; -import { useEffect, useState, type ReactNode } from "react"; +import { memo, useEffect, useState, type ReactNode } from "react"; import { ExternalLinkIcon, XIcon } from "lucide-react"; import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog"; @@ -55,7 +55,9 @@ const UNLIMITED_REVEAL = Number.MAX_SAFE_INTEGER; * the whole line. Truncation happens on the rendered strings, never on the * source, so a half-typed line can never show `{gre` or an unclosed `**`. */ -export function DialogueText({ +/** Memoised: the canvas and the editor preview render the same line many times + * per interaction, and a re-parse is the expensive part. */ +const DialogueText = memo(function DialogueTextImpl({ text, className, reveal, @@ -72,7 +74,9 @@ export function DialogueText({ {renderBlocks(text, { i: 0 }, budget)} ); -} +}); + +export { DialogueText }; /** * Visible characters in a line, counted the way `reveal` counts them (markup diff --git a/tests/int/npc-dialogue-settings-global.int.spec.ts b/tests/int/npc-dialogue-settings-global.int.spec.ts index aa003191..86def494 100644 --- a/tests/int/npc-dialogue-settings-global.int.spec.ts +++ b/tests/int/npc-dialogue-settings-global.int.spec.ts @@ -23,7 +23,10 @@ beforeAll(async () => { const update = (data: { voice?: string | null; extraRules?: string | null; - presentation?: { typewriterDefault?: boolean | null; pacing?: string | null }; + presentation?: { + typewriterDefault?: boolean | null; + pacing?: "subtle" | "natural" | "pronounced" | null; + }; }) => payload.updateGlobal({ slug: "npc-dialogue-settings",