Build the source for a single `tl.method(selector, vars, position)` call.
(timelineVar: string, anim: Omit<GsapAnimation, "id">)
| 1382 | |
| 1383 | /** Build the source for a single `tl.method(selector, vars, position)` call. */ |
| 1384 | function buildTweenStatementCode(timelineVar: string, anim: Omit<GsapAnimation, "id">): string { |
| 1385 | const selector = JSON.stringify(anim.targetSelector); |
| 1386 | const props: Record<string, number | string> = { ...anim.properties }; |
| 1387 | if (anim.method !== "set" && anim.duration !== undefined) props.duration = anim.duration; |
| 1388 | if (anim.ease) props.ease = anim.ease; |
| 1389 | const entries = Object.entries(props).map(([k, v]) => `${safeKey(k)}: ${valueToCode(v)}`); |
| 1390 | // immediateRender forces GSAP to apply the set when added to the timeline, |
| 1391 | // not on the first seek — without it, tl.set at position 0 on a paused |
| 1392 | // timeline is invisible until the playhead moves past 0. A base `gsap.set` |
| 1393 | // already runs immediately, so it doesn't need (or get) the flag. |
| 1394 | if (anim.method === "set" && !anim.global) entries.push("immediateRender: true"); |
| 1395 | if (anim.extras) { |
| 1396 | for (const [k, v] of Object.entries(anim.extras)) { |
| 1397 | entries.push(`${safeKey(k)}: ${valueToCode(v as number | string)}`); |
| 1398 | } |
| 1399 | } |
| 1400 | const objCode = `{ ${entries.join(", ")} }`; |
| 1401 | const posCode = valueToCode( |
| 1402 | typeof anim.position === "number" ? anim.position : (anim.position ?? 0), |
| 1403 | ); |
| 1404 | if (anim.method === "fromTo") { |
| 1405 | const fromEntries = Object.entries(anim.fromProperties ?? {}).map( |
| 1406 | ([k, v]) => `${safeKey(k)}: ${valueToCode(v)}`, |
| 1407 | ); |
| 1408 | const fromCode = `{ ${fromEntries.join(", ")} }`; |
| 1409 | return `${timelineVar}.fromTo(${selector}, ${fromCode}, ${objCode}, ${posCode});`; |
| 1410 | } |
| 1411 | // A base `gsap.set` is off the timeline: no timeline var, no position arg. |
| 1412 | if (anim.method === "set" && anim.global) { |
| 1413 | return `gsap.set(${selector}, ${objCode});`; |
| 1414 | } |
| 1415 | return `${timelineVar}.${anim.method}(${selector}, ${objCode}, ${posCode});`; |
| 1416 | } |
| 1417 | |
| 1418 | export function updateAnimationInScript( |
| 1419 | script: string, |
no test coverage detected