1
0
Fork 0

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.
This commit is contained in:
Jason Fraley 2026-12-01 14:12:09 -05:00
parent 4ed5b4ee79
commit bf9fd09752
3 changed files with 42 additions and 9 deletions

View file

@ -1,6 +1,6 @@
"use client"; "use client";
import { useEffect, useRef, useState } from "react"; import { memo, useEffect, useRef, useState } from "react";
import { DialogueText } from "@/lib/npc-dialogue/DialogueText"; import { DialogueText } from "@/lib/npc-dialogue/DialogueText";
@ -18,6 +18,26 @@ const COLOR_CHOICES = [
"slate", "slate",
] as const; ] 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 (
<div className="min-w-0 break-words rounded border border-white/10 bg-black/40 px-2 py-1.5 text-sm text-white/80">
<DialogueText text={text} />
</div>
);
});
/** /**
* Textarea for authored dialogue strings with a formatting toolbar, a * Textarea for authored dialogue strings with a formatting toolbar, a
* right-click markup menu, and a live `DialogueText` preview. Storage stays * right-click markup menu, and a live `DialogueText` preview. Storage stays
@ -48,6 +68,14 @@ export function MarkupEditor({
const innerRef = useRef<HTMLTextAreaElement | null>(null); const innerRef = useRef<HTMLTextAreaElement | null>(null);
const el = () => textareaRef?.current ?? innerRef.current; const el = () => textareaRef?.current ?? innerRef.current;
const [menu, setMenu] = useState<{ x: number; y: number } | null>(null); 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. // Close the context menu on any outside click or Escape.
useEffect(() => { 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" "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() !== "" && (
<div className="flex flex-col gap-0.5"> <div className="flex flex-col gap-0.5">
<span className="text-[9px] uppercase tracking-wider text-white/30">Preview</span> <span className="text-[9px] uppercase tracking-wider text-white/30">Preview</span>
<div className="min-w-0 break-words rounded border border-white/10 bg-black/40 px-2 py-1.5 text-sm text-white/80"> <MarkupPreview text={previewText} />
<DialogueText text={value} />
</div>
</div> </div>
)} )}
{menu && ( {menu && (

View file

@ -1,6 +1,6 @@
"use client"; "use client";
import { useEffect, useState, type ReactNode } from "react"; import { memo, useEffect, useState, type ReactNode } from "react";
import { ExternalLinkIcon, XIcon } from "lucide-react"; import { ExternalLinkIcon, XIcon } from "lucide-react";
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog"; 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 * 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 `**`. * 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, text,
className, className,
reveal, reveal,
@ -72,7 +74,9 @@ export function DialogueText({
{renderBlocks(text, { i: 0 }, budget)} {renderBlocks(text, { i: 0 }, budget)}
</span> </span>
); );
} });
export { DialogueText };
/** /**
* Visible characters in a line, counted the way `reveal` counts them (markup * Visible characters in a line, counted the way `reveal` counts them (markup

View file

@ -23,7 +23,10 @@ beforeAll(async () => {
const update = (data: { const update = (data: {
voice?: string | null; voice?: string | null;
extraRules?: string | null; extraRules?: string | null;
presentation?: { typewriterDefault?: boolean | null; pacing?: string | null }; presentation?: {
typewriterDefault?: boolean | null;
pacing?: "subtle" | "natural" | "pronounced" | null;
};
}) => }) =>
payload.updateGlobal({ payload.updateGlobal({
slug: "npc-dialogue-settings", slug: "npc-dialogue-settings",