From bf9fd09752417038e76ae21e5c3bd61c8df40b90 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Tue, 1 Dec 2026 14:12:09 -0500 Subject: [PATCH] perf(npc-dialogue): stop re-parsing the markup preview on every keystroke Typing a line in the editor felt sticky. The line box renders a live markup preview, and that preview is a full parse and render of the authored string: it ran on every character, so a long line with colours, bold and a link was re-parsed per keypress while the whole edit dialog re-rendered around it. The preview now waits 220ms after the last keystroke (the textarea itself stays instant), and both the preview and DialogueText are memoised, so an unchanged line never re-parses. DialogueText is shared with the canvas, which re-renders every card on any diagram change. Measured in the running app on a 24 node diagram and a 150 character line, 40 programmatic keystrokes through the browser's own metrics: script 319ms to 221ms (8.0 to 5.5 per key), total main-thread task 767ms to 633ms, style recalc 386ms to 338ms. The preview still updates 220ms after typing stops. What is left is the dialog re-render itself: on this file a keystroke still re-renders every section of the edit dialog. Removing that means giving the line box its own state and committing on a debounce, which needs the Save path to flush, and belongs after the in-progress poll work in NodeEditDialog lands. --- .../admin/npc-dialogue/MarkupEditor.tsx | 36 ++++++++++++++++--- src/lib/npc-dialogue/DialogueText.tsx | 10 ++++-- .../npc-dialogue-settings-global.int.spec.ts | 5 ++- 3 files changed, 42 insertions(+), 9 deletions(-) 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",