|
| 1 | +import type { FrameStatus } from "./types.js"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Surgical writers for `STORYBOARD.md`. |
| 5 | + * |
| 6 | + * These update a single frame's metadata in place — preserving all other |
| 7 | + * content, formatting, comments, and non-frame sections — rather than |
| 8 | + * re-serializing the parsed manifest (which would be lossy). Used by the |
| 9 | + * storyboard frame-focus editor to persist `voiceover` / `status` edits. |
| 10 | + * |
| 11 | + * The patterns below mirror the read-side detection in `parseStoryboard.ts`. |
| 12 | + */ |
| 13 | + |
| 14 | +// Linear (single bounded whitespace run before a literal) to avoid the polynomial |
| 15 | +// backtracking CodeQL flags (js/polynomial-redos); mirrors parseStoryboard.ts. |
| 16 | +const FRAME_HEADING_RE = /^(#{2,3})[ \t]+(?:frame|beat|scene)\b/i; |
| 17 | +const HEADING_LEVEL_RE = /^(#{1,6})[ \t]+/; |
| 18 | +/** |
| 19 | + * `- key: value` capturing bullet, key, separator, and value for in-place replacement. |
| 20 | + * Value is greedy-to-EOL (not `(.+?)\s*$`) so it can't backtrack on trailing whitespace. |
| 21 | + */ |
| 22 | +const META_LINE_RE = /^([ \t]*[-*][ \t]+)([A-Za-z_][\w-]*)([ \t]*:[ \t]*)(.*)$/; |
| 23 | + |
| 24 | +/** Aliases the parser accepts for the voiceover field. */ |
| 25 | +export const VOICEOVER_ALIASES = ["voiceover", "vo", "voice_over", "narration"]; |
| 26 | + |
| 27 | +interface FrameBounds { |
| 28 | + /** 0-based line index of the frame heading. */ |
| 29 | + start: number; |
| 30 | + /** 0-based line index just past the frame's content (exclusive). */ |
| 31 | + end: number; |
| 32 | + /** Heading depth (`#` count) that opened the frame. */ |
| 33 | + level: number; |
| 34 | +} |
| 35 | + |
| 36 | +/** Locate every frame's line range, using the same boundary rules as the parser. */ |
| 37 | +// fallow-ignore-next-line complexity |
| 38 | +function frameBounds(lines: string[]): FrameBounds[] { |
| 39 | + const bounds: FrameBounds[] = []; |
| 40 | + let current: FrameBounds | null = null; |
| 41 | + for (let i = 0; i < lines.length; i++) { |
| 42 | + const line = lines[i] ?? ""; |
| 43 | + const frameMatch = FRAME_HEADING_RE.exec(line); |
| 44 | + if (frameMatch) { |
| 45 | + if (current) current.end = i; |
| 46 | + current = { start: i, end: lines.length, level: (frameMatch[1] ?? "##").length }; |
| 47 | + bounds.push(current); |
| 48 | + continue; |
| 49 | + } |
| 50 | + const heading = HEADING_LEVEL_RE.exec(line); |
| 51 | + if (current && heading && (heading[1] ?? "").length <= current.level) { |
| 52 | + current.end = i; |
| 53 | + current = null; |
| 54 | + } |
| 55 | + } |
| 56 | + return bounds; |
| 57 | +} |
| 58 | + |
| 59 | +function formatValue(value: string, quote: boolean): string { |
| 60 | + // Metadata is a single line: collapse any newlines (from a multi-line textarea) |
| 61 | + // to spaces so the value can never split the `- key:` line and corrupt the file. |
| 62 | + const clean = value.replace(/\s*\r?\n\s*/g, " ").trim(); |
| 63 | + // Always wrap when quoting. The parser's stripQuotes removes exactly one outer |
| 64 | + // pair, so wrapping round-trips losslessly even for empty values or values that |
| 65 | + // themselves contain quotes (`"foo"` → `""foo""` → parses back to `"foo"`). |
| 66 | + return quote ? `"${clean}"` : clean; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Set (or insert) a metadata field on the frame at `frameIndex` (1-based). |
| 71 | + * Replaces an existing `- key: …` line (matching any alias) in place; otherwise |
| 72 | + * inserts a new line right after the frame heading. |
| 73 | + * |
| 74 | + * Throws when the frame doesn't exist, so a stale/raced index (e.g. the frame |
| 75 | + * was deleted on disk after render) surfaces as an error instead of a silent |
| 76 | + * no-op the UI would report as a successful save. |
| 77 | + */ |
| 78 | +export function setFrameField( |
| 79 | + source: string, |
| 80 | + frameIndex: number, |
| 81 | + key: string, |
| 82 | + value: string, |
| 83 | + opts: { aliases?: string[]; quote?: boolean } = {}, |
| 84 | +): string { |
| 85 | + const lines = source.split(/\r?\n/); |
| 86 | + const target = frameBounds(lines)[frameIndex - 1]; |
| 87 | + if (!target) throw new Error(`storyboard frame ${frameIndex} not found`); |
| 88 | + |
| 89 | + const aliases = new Set([key, ...(opts.aliases ?? [])].map((k) => k.toLowerCase())); |
| 90 | + const formatted = formatValue(value, opts.quote ?? false); |
| 91 | + |
| 92 | + for (let i = target.start + 1; i < target.end; i++) { |
| 93 | + const match = META_LINE_RE.exec(lines[i] ?? ""); |
| 94 | + if (match && aliases.has((match[2] ?? "").toLowerCase())) { |
| 95 | + lines[i] = `${match[1]}${match[2]}${match[3]}${formatted}`; |
| 96 | + return lines.join("\n"); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + lines.splice(target.start + 1, 0, `- ${key}: ${formatted}`); |
| 101 | + return lines.join("\n"); |
| 102 | +} |
| 103 | + |
| 104 | +/** Set the voiceover (guide) line for a frame, matching any voiceover alias. */ |
| 105 | +export function setFrameVoiceover(source: string, frameIndex: number, value: string): string { |
| 106 | + return setFrameField(source, frameIndex, "voiceover", value, { |
| 107 | + aliases: VOICEOVER_ALIASES, |
| 108 | + quote: true, |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +/** Set the lifecycle status for a frame. */ |
| 113 | +export function setFrameStatus(source: string, frameIndex: number, status: FrameStatus): string { |
| 114 | + return setFrameField(source, frameIndex, "status", status); |
| 115 | +} |
0 commit comments