|
| 1 | +/** |
| 2 | + * Where in the rack an automation lane's parameter actually lives. |
| 3 | + * |
| 4 | + * A lane names `fx.<nodeId>.<param>`, but the rack does not show one flat list |
| 5 | + * of nodes: the carve is one module standing for the filters it compiled, EQ |
| 6 | + * bands are folded into their own module, and preset runs are collapsible |
| 7 | + * groups. A node id therefore resolves to one of several surfaces, and the |
| 8 | + * caller has to open the RIGHT one — opening `openNode` on a carve band, whose |
| 9 | + * row is filtered out of `handBuilt`, would open nothing at all and read as the |
| 10 | + * click doing nothing. |
| 11 | + */ |
| 12 | + |
| 13 | +import { parseAutomationTarget } from "@hyperframes/core/audio-automation"; |
| 14 | +import { escapeCssString } from "./domEditingDom"; |
| 15 | +import type { HfAudioFxChain } from "@hyperframes/core/audio-fx"; |
| 16 | + |
| 17 | +export type AudioFxRevealTarget = |
| 18 | + /** A hand-built effect, addressed by its index in the chain. */ |
| 19 | + | { kind: "node"; index: number; nodeId: string } |
| 20 | + /** An EQ module, addressed by the id its bands share. */ |
| 21 | + | { kind: "eq"; eqId: string } |
| 22 | + /** A preset run, addressed the way `collapsedRuns` keys it. */ |
| 23 | + | { kind: "preset"; runKey: string } |
| 24 | + /** The carve module, which owns every `fromCarve` node collectively. */ |
| 25 | + | { kind: "carve" } |
| 26 | + /** The track's own volume — no rack row; the reveal is the rack itself. */ |
| 27 | + | { kind: "volume" }; |
| 28 | + |
| 29 | +/** |
| 30 | + * Resolve a lane target to the surface that shows it, or null when the chain |
| 31 | + * does not contain it (a stale lane, or one whose effect was removed). |
| 32 | + */ |
| 33 | +export function audioFxRevealTarget( |
| 34 | + target: string, |
| 35 | + chain: HfAudioFxChain | null, |
| 36 | +): AudioFxRevealTarget | null { |
| 37 | + const parsed = parseAutomationTarget(target); |
| 38 | + if (!parsed) return null; |
| 39 | + if (parsed.kind === "volume") return { kind: "volume" }; |
| 40 | + if (parsed.kind === "preset") { |
| 41 | + // A preset-level lane names the preset, not a node inside it: find its run |
| 42 | + // by the first node that belongs to it, which is how `runKey` is built. |
| 43 | + const index = (chain?.nodes ?? []).findIndex((node) => node.fromPreset === parsed.presetId); |
| 44 | + return index >= 0 ? { kind: "preset", runKey: `${parsed.presetId}-${index}` } : null; |
| 45 | + } |
| 46 | + const index = (chain?.nodes ?? []).findIndex((node) => node.id === parsed.nodeId); |
| 47 | + const node = index >= 0 ? chain?.nodes[index] : undefined; |
| 48 | + if (!node) return null; |
| 49 | + // Order matters: a carve band can also carry `fromEq`/`fromPreset` tags, and |
| 50 | + // the carve module is the one that actually renders it. |
| 51 | + if (node.fromCarve) return { kind: "carve" }; |
| 52 | + if (node.fromEq) return { kind: "eq", eqId: node.fromEq }; |
| 53 | + if (node.fromPreset) { |
| 54 | + const first = (chain?.nodes ?? []).findIndex((n) => n.fromPreset === node.fromPreset); |
| 55 | + return { kind: "preset", runKey: `${node.fromPreset}-${first}` }; |
| 56 | + } |
| 57 | + return { kind: "node", index, nodeId: parsed.nodeId }; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * The DOM selector for the row a reveal target lives in, or null when the target |
| 62 | + * names no surface this panel renders. |
| 63 | + * |
| 64 | + * A preset run is keyed by the preset id the run element actually exposes, not by |
| 65 | + * `runKey`, which is the collapse map's key and carries an index suffix. |
| 66 | + */ |
| 67 | +function revealRowSelector(where: AudioFxRevealTarget | null): string | null { |
| 68 | + if (!where) return null; |
| 69 | + switch (where.kind) { |
| 70 | + case "node": |
| 71 | + return where.nodeId ? `[data-fx-node-id="${escapeCssString(where.nodeId)}"]` : null; |
| 72 | + case "eq": |
| 73 | + return `[data-fx-eq="${escapeCssString(where.eqId)}"]`; |
| 74 | + case "carve": |
| 75 | + return ".hf-fx-carve-module"; |
| 76 | + case "preset": |
| 77 | + return `[data-fx-preset="${escapeCssString(where.runKey.replace(/-\d+$/, ""))}"]`; |
| 78 | + default: |
| 79 | + return null; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Scroll the row that owns `target` into view, and report whether it was found. |
| 85 | + * |
| 86 | + * The target is resolved against the chain again rather than remembered: which |
| 87 | + * surface owns a parameter is a fact about the chain, and the chain may have |
| 88 | + * been edited between the reveal request and the pass that can act on it. A |
| 89 | + * false return means the row has not mounted yet, so the caller keeps the |
| 90 | + * request pending. |
| 91 | + */ |
| 92 | +export function scrollRevealedRowIntoView( |
| 93 | + root: HTMLElement | null, |
| 94 | + target: string, |
| 95 | + chain: HfAudioFxChain, |
| 96 | +): boolean { |
| 97 | + const selector = revealRowSelector(audioFxRevealTarget(target, chain)); |
| 98 | + // `querySelector` throws SyntaxError on a malformed selector, and this runs |
| 99 | + // inside a render-phase effect — an unescapable id would take the whole |
| 100 | + // property panel down instead of leaving the request pending, which is what |
| 101 | + // returning false means. `parseAudioFxNode` accepts any non-empty string as |
| 102 | + // an id and `parseAutomationTarget` only splits on `.`, so a hand- or |
| 103 | + // LLM-authored chain can carry one. |
| 104 | + let row: HTMLElement | null = null; |
| 105 | + try { |
| 106 | + row = selector ? (root?.querySelector<HTMLElement>(selector) ?? null) : null; |
| 107 | + } catch { |
| 108 | + return false; |
| 109 | + } |
| 110 | + if (!row) return false; |
| 111 | + row.scrollIntoView({ block: "nearest", behavior: "smooth" }); |
| 112 | + return true; |
| 113 | +} |
0 commit comments