( script: string, animation: Omit<GsapAnimation, "id">, )
| 1513 | } |
| 1514 | |
| 1515 | export function addAnimationToScript( |
| 1516 | script: string, |
| 1517 | animation: Omit<GsapAnimation, "id">, |
| 1518 | ): { script: string; id: string } { |
| 1519 | let parsed: ParsedGsapAst; |
| 1520 | try { |
| 1521 | parsed = parseGsapAst(script); |
| 1522 | } catch (e) { |
| 1523 | console.warn("[gsap-parser] addAnimationToScript parse failed:", e); |
| 1524 | return { script, id: "" }; |
| 1525 | } |
| 1526 | // Nothing to anchor against and no timeline to target — treat as parse failure. |
| 1527 | if (parsed.located.length === 0 && parsed.detection.ref === null) { |
| 1528 | return { script, id: "" }; |
| 1529 | } |
| 1530 | |
| 1531 | const id = `anim-${Date.now()}`; |
| 1532 | const statementCode = buildTweenStatementCode(parsed.timelineVar, animation); |
| 1533 | const newStatement = parseScript(statementCode).program.body[0]; |
| 1534 | insertAfterAnchor(parsed, newStatement); |
| 1535 | return { script: recast.print(parsed.ast).code, id }; |
| 1536 | } |
| 1537 | |
| 1538 | export function addAnimationWithKeyframesToScript( |
| 1539 | script: string, |
no test coverage detected